allow setting material property by name

This commit is contained in:
Nick Fisher
2024-09-13 15:20:45 +08:00
parent 2eb7cc286b
commit f18f04734d
4 changed files with 60 additions and 0 deletions

View File

@@ -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

View File

@@ -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);
}
}