add method for setting material color

This commit is contained in:
Nick Fisher
2023-08-19 13:52:57 +08:00
parent ed72c16724
commit 042439a72b
7 changed files with 823 additions and 754 deletions

View File

@@ -218,6 +218,9 @@ class _ExampleWidgetState extends State<ExampleWidget> {
.build(); .build();
_filamentController.setMorphAnimationData(_cube!, animation); _filamentController.setMorphAnimationData(_cube!, animation);
}, "animate morph weights #3 and #4"), }, "animate morph weights #3 and #4"),
_item(() {
_filamentController.setMaterialColor(_cube!, "Cone", 0, Colors.purple);
}, "set cone material color to purple")
]; ];
if (_animations != null) { if (_animations != null) {
children.addAll(_animations!.map((a) => _item(() { children.addAll(_animations!.map((a) => _item(() {

View File

@@ -615,7 +615,18 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
case "setCameraFocusDistance": case "setCameraFocusDistance":
set_camera_focus_distance(viewer, call.arguments as! Float) set_camera_focus_distance(viewer, call.arguments as! Float)
result(true) result(true)
case "setMaterialColor":
guard let args = call.arguments as? [Any], args.count == 5,
let assetManager = args[0] as? Int64,
let asset = args[1] as? EntityId,
let meshName = args[2] as? String,
let materialIndex = args[3] as? Int32,
let color = args[4] as? [Double] else {
result(FlutterError(code: "INVALID_ARGUMENTS", message: "Expected correct arguments for setMaterialColor", details: nil))
return
}
set_material_color(unsafeBitCast(assetManager, to:UnsafeMutableRawPointer.self), asset, meshName, materialIndex, Float(color[0]), Float(color[1]), Float(color[2]), Float(color[3]))
result(true)
case "hideMesh": case "hideMesh":
guard let args = call.arguments as? [Any], args.count == 3, guard let args = call.arguments as? [Any], args.count == 3,

View File

@@ -40,6 +40,7 @@ namespace polyvox {
const utils::Entity* getLightEntities(EntityId e) const noexcept; const utils::Entity* getLightEntities(EntityId e) const noexcept;
size_t getLightEntityCount(EntityId e) const noexcept; size_t getLightEntityCount(EntityId e) const noexcept;
void updateAnimations(); void updateAnimations();
bool setMaterialColor(EntityId e, const char* meshName, int materialInstance, const float r, const float g, const float b, const float a);
bool setMorphAnimationBuffer( bool setMorphAnimationBuffer(
EntityId entityId, EntityId entityId,

View File

@@ -82,6 +82,7 @@ void remove_asset(const void* const viewer, EntityId asset);
void clear_assets(const void* const viewer); void clear_assets(const void* const viewer);
void load_texture(void* assetManager, EntityId asset, const char *assetPath, int renderableIndex); void load_texture(void* assetManager, EntityId asset, const char *assetPath, int renderableIndex);
void set_texture(void* assetManager, EntityId asset); void set_texture(void* assetManager, EntityId asset);
bool set_material_color(void* assetManager, EntityId asset, const char* meshName, int materialIndex, const float r, const float g, const float b, const float a);
void transform_to_unit_cube(void* assetManager, EntityId asset); void transform_to_unit_cube(void* assetManager, EntityId asset);
void set_position(void* assetManager, EntityId asset, float x, float y, float z); void set_position(void* assetManager, EntityId asset, float x, float y, float z);
void set_rotation(void* assetManager, EntityId asset, float rads, float x, float y, float z); void set_rotation(void* assetManager, EntityId asset, float rads, float x, float y, float z);

View File

@@ -273,11 +273,12 @@ void AssetManager::updateAnimations() {
frameNumber = lengthInFrames - frameNumber; frameNumber = lengthInFrames - frameNumber;
} }
auto baseOffset = frameNumber * asset.mMorphAnimationBuffer.mMorphIndices.size(); auto baseOffset = frameNumber * asset.mMorphAnimationBuffer.mMorphIndices.size();
for(auto morphIndex : asset.mMorphAnimationBuffer.mMorphIndices) { for(int i = 0; i < asset.mMorphAnimationBuffer.mMorphIndices.size(); i++) {
auto morphIndex = asset.mMorphAnimationBuffer.mMorphIndices[i];
// set the weights appropriately // set the weights appropriately
rm.setMorphWeights( rm.setMorphWeights(
rm.getInstance(asset.mMorphAnimationBuffer.mMeshTarget), rm.getInstance(asset.mMorphAnimationBuffer.mMeshTarget),
asset.mMorphAnimationBuffer.mFrameData.data() + baseOffset + morphIndex, asset.mMorphAnimationBuffer.mFrameData.data() + baseOffset + i,
1, 1,
morphIndex morphIndex
); );
@@ -459,6 +460,35 @@ bool AssetManager::setMorphAnimationBuffer(
return true; return true;
} }
bool AssetManager::setMaterialColor(EntityId entityId, const char* meshName, int materialIndex, const float r, const float g, const float b, const float a) {
const auto& pos = _entityIdLookup.find(entityId);
if(pos == _entityIdLookup.end()) {
Log("ERROR: asset not found for entity.");
return false;
}
auto& asset = _assets[pos->second];
auto entity = findEntityByName(asset, meshName);
RenderableManager& rm = _engine->getRenderableManager();
auto renderable = rm.getInstance(entity);
if(!renderable.isValid()) {
return false;
}
MaterialInstance* mi = rm.getMaterialInstanceAt(renderable, materialIndex);
if(!mi) {
Log("ERROR: material index must be less than number of material instances");
return false;
}
mi->setParameter("baseColorFactor", RgbaType::sRGB, math::float4(r, g, b, a));
return true;
}
bool AssetManager::setBoneAnimationBuffer( bool AssetManager::setBoneAnimationBuffer(
EntityId entityId, EntityId entityId,
const float* const frameData, const float* const frameData,
@@ -557,6 +587,10 @@ bool AssetManager::setBoneAnimationBuffer(
void AssetManager::playAnimation(EntityId e, int index, bool loop, bool reverse, bool replaceActive, float crossfade) { void AssetManager::playAnimation(EntityId e, int index, bool loop, bool reverse, bool replaceActive, float crossfade) {
if(index < 0) {
Log("ERROR: glTF animation index must be greater than zero.");
return;
}
const auto& pos = _entityIdLookup.find(e); const auto& pos = _entityIdLookup.find(e);
if(pos == _entityIdLookup.end()) { if(pos == _entityIdLookup.end()) {
Log("ERROR: asset not found for entity."); Log("ERROR: asset not found for entity.");

View File

@@ -337,6 +337,10 @@ extern "C" {
// ((AssetManager*)assetManager)->setTexture(); // ((AssetManager*)assetManager)->setTexture();
} }
bool set_material_color(void* assetManager, EntityId asset, const char* meshName, int materialIndex, const float r, const float g, const float b, const float a) {
return ((AssetManager*)assetManager)->setMaterialColor(asset, meshName, materialIndex, r, g, b, a);
}
FLUTTER_PLUGIN_EXPORT void transform_to_unit_cube(void* assetManager, EntityId asset) { FLUTTER_PLUGIN_EXPORT void transform_to_unit_cube(void* assetManager, EntityId asset) {
((AssetManager*)assetManager)->transformToUnitCube(asset); ((AssetManager*)assetManager)->transformToUnitCube(asset);
} }

View File

@@ -260,13 +260,12 @@ class FilamentController {
/// ///
void setMorphAnimationData( void setMorphAnimationData(
FilamentEntity asset, MorphAnimationData animation) async { FilamentEntity asset, MorphAnimationData animation) async {
print("SETTING animation with ${animation.data}");
await _channel.invokeMethod("setMorphAnimation", [ await _channel.invokeMethod("setMorphAnimation", [
_assetManager, _assetManager,
asset, asset,
animation.meshName, animation.meshName,
animation.data, animation.data,
animation.morphIndices, animation.animatedMorphIndices,
animation.numMorphTargets, animation.numMorphTargets,
animation.numFrames, animation.numFrames,
animation.frameLengthInMs animation.frameLengthInMs
@@ -402,6 +401,22 @@ class FilamentController {
await _channel.invokeMethod("setTexture", [_assetManager, asset]); await _channel.invokeMethod("setTexture", [_assetManager, asset]);
} }
void setMaterialColor(FilamentEntity asset, String meshName,
int materialIndex, Color color) async {
await _channel.invokeMethod("setMaterialColor", [
_assetManager,
asset,
meshName,
materialIndex,
[
color.red.toDouble() / 255.0,
color.green.toDouble() / 255.0,
color.blue.toDouble() / 255.0,
color.alpha.toDouble() / 255.0
]
]);
}
void transformToUnitCube(FilamentEntity asset) async { void transformToUnitCube(FilamentEntity asset) async {
await _channel.invokeMethod("transformToUnitCube", [_assetManager, asset]); await _channel.invokeMethod("transformToUnitCube", [_assetManager, asset]);
} }