From ad205679cb64ff42884399b89f663ed607edbcbd Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Fri, 13 Sep 2024 15:27:46 +0800 Subject: [PATCH] allow setting material property by name --- .../lib/thermion_dart/thermion_viewer.dart | 15 ++++++++++ .../thermion_dart/thermion_viewer_ffi.dart | 29 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/thermion_dart/lib/thermion_dart/thermion_viewer.dart b/thermion_dart/lib/thermion_dart/thermion_viewer.dart index e16bde5d..24be69a7 100644 --- a/thermion_dart/lib/thermion_dart/thermion_viewer.dart +++ b/thermion_dart/lib/thermion_dart/thermion_viewer.dart @@ -593,9 +593,23 @@ abstract class ThermionViewer { /// /// Sets the `baseColorFactor` property for the material at index [materialIndex] in [entity] under node [meshName] to [color]. /// + @Deprecated("Use setMaterialPropertyFloat4 instead") Future setMaterialColor(ThermionEntity entity, String meshName, int materialIndex, double r, double g, double b, double a); + /// + /// Sets the material property [propertyName] under material [materialIndex] for [entity] to [value]. + /// [entity] must have a Renderable attached. + /// + Future setMaterialPropertyFloat4(ThermionEntity entity, String propertyName, + int materialIndex, double f1, double f2, double f3, double f4); + + /// + /// Sets the material property [propertyName] under material [materialIndex] for [entity] to [value]. + /// [entity] must have a Renderable attached. + /// + Future setMaterialPropertyFloat(ThermionEntity entity, String propertyName, int materialIndex, double value); + /// /// Scale [entity] to fit within the unit cube. /// @@ -867,4 +881,5 @@ abstract class ThermionViewer { /// Removes the outline around [entity]. Noop if there was no highlight. /// Future removeStencilHighlight(ThermionEntity entity); + } diff --git a/thermion_dart/lib/thermion_dart/thermion_viewer_ffi.dart b/thermion_dart/lib/thermion_dart/thermion_viewer_ffi.dart index eaf2fb09..dbed65dc 100644 --- a/thermion_dart/lib/thermion_dart/thermion_viewer_ffi.dart +++ b/thermion_dart/lib/thermion_dart/thermion_viewer_ffi.dart @@ -1943,4 +1943,33 @@ class ThermionViewerFFI extends ThermionViewer { Future removeStencilHighlight(ThermionEntity entity) async { remove_stencil_highlight(_sceneManager!, entity); } + + /// + /// Sets the material property [propertyName] under material [materialIndex] for [entity] to [value]. + /// [entity] must have a Renderable attached. + /// + Future setMaterialPropertyFloat(ThermionEntity entity, String propertyName, + int materialIndex, double value) async { + final ptr = propertyName.toNativeUtf8(allocator: allocator); + set_material_property_float( + _sceneManager!, entity, materialIndex, ptr.cast(), value); + allocator.free(ptr); + } + + /// + /// Sets the material property [propertyName] under material [materialIndex] for [entity] to {f1,f2,f3,f4}. + /// [entity] must have a Renderable attached. + /// + Future setMaterialPropertyFloat4(ThermionEntity entity, String propertyName, + int materialIndex, double f1, double f2, double f3, double f4) async { + final ptr = propertyName.toNativeUtf8(allocator: allocator); + var struct = Struct.create(); + struct.x = f1; + struct.y = f2; + struct.z = f3; + struct.w = f3; + set_material_property_float4(_sceneManager!, entity, materialIndex, + ptr.cast(), struct); + allocator.free(ptr); + } }