From f18f04734d739c019f68697a9d0732d5f978cf25 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Fri, 13 Sep 2024 15:20:45 +0800 Subject: [PATCH] allow setting material property by name --- thermion_dart/native/include/SceneManager.hpp | 3 ++ .../native/include/ThermionDartApi.h | 10 ++++++ thermion_dart/native/src/SceneManager.cpp | 35 +++++++++++++++++++ thermion_dart/native/src/ThermionDartApi.cpp | 12 +++++++ 4 files changed, 60 insertions(+) diff --git a/thermion_dart/native/include/SceneManager.hpp b/thermion_dart/native/include/SceneManager.hpp index 1ee3ad0d..fb23d4b7 100644 --- a/thermion_dart/native/include/SceneManager.hpp +++ b/thermion_dart/native/include/SceneManager.hpp @@ -287,6 +287,9 @@ namespace thermion_filament return _assetLoader->createInstance(asset); } + void setMaterialProperty(EntityId entity, int materialIndex, const char* property, float value); + void setMaterialProperty(EntityId entityId, int materialIndex, const char* property, filament::math::float4 value); + private: gltfio::AssetLoader *_assetLoader = nullptr; const ResourceLoaderWrapperImpl *const _resourceLoaderWrapper; diff --git a/thermion_dart/native/include/ThermionDartApi.h b/thermion_dart/native/include/ThermionDartApi.h index 3a9218d1..baba1538 100644 --- a/thermion_dart/native/include/ThermionDartApi.h +++ b/thermion_dart/native/include/ThermionDartApi.h @@ -59,6 +59,13 @@ extern "C" typedef int32_t _ManipulatorMode; typedef struct CameraPtr CameraPtr; + typedef struct { + float x; + float y; + float z; + float w; + } float4; + typedef struct { double col1[4]; double col2[4]; @@ -270,6 +277,9 @@ extern "C" EMSCRIPTEN_KEEPALIVE void set_gizmo_visibility(void *const sceneManager, bool visible); EMSCRIPTEN_KEEPALIVE void set_stencil_highlight(void *const sceneManager, EntityId entity, float r, float g, float b); EMSCRIPTEN_KEEPALIVE void remove_stencil_highlight(void *const sceneManager, EntityId entity); + EMSCRIPTEN_KEEPALIVE void set_material_property_float(void *const sceneManager, EntityId entity, int materialIndex, const char* property, float value); + EMSCRIPTEN_KEEPALIVE void set_material_property_float4(void *const sceneManager, EntityId entity, int materialIndex, const char* property, float4 value); + #ifdef __cplusplus } diff --git a/thermion_dart/native/src/SceneManager.cpp b/thermion_dart/native/src/SceneManager.cpp index 1351eb8a..80f4490b 100644 --- a/thermion_dart/native/src/SceneManager.cpp +++ b/thermion_dart/native/src/SceneManager.cpp @@ -2460,6 +2460,41 @@ void SceneManager::queueRelativePositionUpdateWorldAxis(EntityId entity, float v ) { return createGeometryWithNormals(vertices, numVertices, nullptr, 0, indices, numIndices, primitiveType, materialPath, keepData); } + + void SceneManager::setMaterialProperty(EntityId entityId, int materialIndex, const char* property, float value) { + auto entity = Entity::import(entityId); + const auto& rm = _engine->getRenderableManager(); + auto renderableInstance = rm.getInstance(entity); + if(!renderableInstance.isValid()) { + Log("ERROR"); + return; + } + auto materialInstance = rm.getMaterialInstanceAt(renderableInstance, materialIndex); + + if(!materialInstance->getMaterial()->hasParameter(property)) { + Log("Parameter %s not found", property); + return; + } + materialInstance->setParameter(property, value); + } + + void SceneManager::setMaterialProperty(EntityId entityId, int materialIndex, const char* property, filament::math::float4 value) { + auto entity = Entity::import(entityId); + const auto& rm = _engine->getRenderableManager(); + auto renderableInstance = rm.getInstance(entity); + if(!renderableInstance.isValid()) { + Log("ERROR"); + return; + } + auto materialInstance = rm.getMaterialInstanceAt(renderableInstance, materialIndex); + + if(!materialInstance->getMaterial()->hasParameter(property)) { + Log("Parameter %s not found", property); + return; + } + materialInstance->setParameter(property, value); + } + } // namespace thermion_filament diff --git a/thermion_dart/native/src/ThermionDartApi.cpp b/thermion_dart/native/src/ThermionDartApi.cpp index e4464a00..16d6fbdc 100644 --- a/thermion_dart/native/src/ThermionDartApi.cpp +++ b/thermion_dart/native/src/ThermionDartApi.cpp @@ -943,4 +943,16 @@ extern "C" { ((SceneManager *)sceneManager)->removeStencilHighlight(entityId); } + + EMSCRIPTEN_KEEPALIVE void set_material_property_float(void *const sceneManager, EntityId entity, int materialIndex, const char* property, float value) { + ((SceneManager *)sceneManager)->setMaterialProperty(entity, materialIndex, property, value); + } + + EMSCRIPTEN_KEEPALIVE void set_material_property_float4(void *const sceneManager, EntityId entity, int materialIndex, const char* property, float4 value) { + filament::math::float4 filamentValue { value.x, value.y, value.z, value.w }; + + ((SceneManager *)sceneManager)->setMaterialProperty(entity, materialIndex, property, filamentValue); + } + + }