From f873f7da668b7ea9df26d4a4f1b12f218e59977a Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Tue, 17 Dec 2024 16:36:53 +0800 Subject: [PATCH] feat: add setTransparencyMode to Dart Material class --- .../src/viewer/src/ffi/src/ffi_material.dart | 9 ++++++- .../src/viewer/src/shared_types/material.dart | 26 +++++++++++++++++-- .../native/include/c_api/TMaterialInstance.h | 23 ++++++++++++++-- .../native/src/c_api/TMaterialInstance.cpp | 8 ++++++ 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/thermion_dart/lib/src/viewer/src/ffi/src/ffi_material.dart b/thermion_dart/lib/src/viewer/src/ffi/src/ffi_material.dart index c40a2819..a7eb653e 100644 --- a/thermion_dart/lib/src/viewer/src/ffi/src/ffi_material.dart +++ b/thermion_dart/lib/src/viewer/src/ffi/src/ffi_material.dart @@ -139,7 +139,14 @@ class FFIMaterialInstance extends MaterialInstance { Future dispose() async { await withVoidCallback((cb) { - SceneManager_destroyMaterialInstanceRenderThread(sceneManager, pointer, cb); + SceneManager_destroyMaterialInstanceRenderThread( + sceneManager, pointer, cb); }); } + + @override + Future setTransparencyMode(TransparencyMode mode) async { + MaterialInstance_setTransparencyMode( + pointer, TTransparencyMode.values[mode.index]); + } } diff --git a/thermion_dart/lib/src/viewer/src/shared_types/material.dart b/thermion_dart/lib/src/viewer/src/shared_types/material.dart index 4aa4eda1..3fd207b3 100644 --- a/thermion_dart/lib/src/viewer/src/shared_types/material.dart +++ b/thermion_dart/lib/src/viewer/src/shared_types/material.dart @@ -70,7 +70,28 @@ enum StencilFace { FRONT_AND_BACK } -enum AlphaMode { OPAQUE, MASK, BLEND } +enum AlphaMode { + OPAQUE, + MASK, + BLEND +} + +enum TransparencyMode { + //! the transparent object is drawn honoring the raster state + DEFAULT, + /** + * the transparent object is first drawn in the depth buffer, + * then in the color buffer, honoring the culling mode, but ignoring the depth test function + */ + TWO_PASSES_ONE_SIDE, + + /** + * the transparent object is drawn twice in the color buffer, + * first with back faces only, then with front faces; the culling + * mode is ignored. Can be combined with two-sided lighting + */ + TWO_PASSES_TWO_SIDES +} abstract class Material { Future createInstance(); @@ -115,6 +136,7 @@ abstract class MaterialInstance { Future setStencilReadMask(int mask); Future setStencilWriteMask(int mask); - Future dispose(); + Future setTransparencyMode(TransparencyMode mode); + Future dispose(); } diff --git a/thermion_dart/native/include/c_api/TMaterialInstance.h b/thermion_dart/native/include/c_api/TMaterialInstance.h index f348a4f2..bc794fdc 100644 --- a/thermion_dart/native/include/c_api/TMaterialInstance.h +++ b/thermion_dart/native/include/c_api/TMaterialInstance.h @@ -35,7 +35,6 @@ extern "C" INVERT // Invert the current value }; - // StencilFace equivalent enum TStencilFace { STENCIL_FACE_FRONT = 1, @@ -43,7 +42,6 @@ extern "C" STENCIL_FACE_FRONT_AND_BACK = 3 }; - // Add these enum definitions at the top with the other enums enum TCullingMode { CULLING_MODE_NONE = 0, @@ -52,6 +50,23 @@ extern "C" CULLING_MODE_FRONT_AND_BACK }; + enum TTransparencyMode { + //! the transparent object is drawn honoring the raster state + DEFAULT, + /** + * the transparent object is first drawn in the depth buffer, + * then in the color buffer, honoring the culling mode, but ignoring the depth test function + */ + TWO_PASSES_ONE_SIDE, + + /** + * the transparent object is drawn twice in the color buffer, + * first with back faces only, then with front faces; the culling + * mode is ignored. Can be combined with two-sided lighting + */ + TWO_PASSES_TWO_SIDES + }; + EMSCRIPTEN_KEEPALIVE TMaterialInstance *Material_createInstance(TMaterial *tMaterial); EMSCRIPTEN_KEEPALIVE bool MaterialInstance_isStencilWriteEnabled(TMaterialInstance *materialInstance); EMSCRIPTEN_KEEPALIVE void MaterialInstance_setStencilWrite(TMaterialInstance *materialInstance, bool enabled); @@ -98,6 +113,10 @@ extern "C" TMaterialInstance *materialInstance, uint8_t mask); + EMSCRIPTEN_KEEPALIVE void MaterialInstance_setTransparencyMode( + TMaterialInstance *materialInstance, + TTransparencyMode transparencyMode); + #ifdef __cplusplus } diff --git a/thermion_dart/native/src/c_api/TMaterialInstance.cpp b/thermion_dart/native/src/c_api/TMaterialInstance.cpp index 19f51115..65594e3b 100644 --- a/thermion_dart/native/src/c_api/TMaterialInstance.cpp +++ b/thermion_dart/native/src/c_api/TMaterialInstance.cpp @@ -139,6 +139,14 @@ namespace thermion auto *instance = reinterpret_cast<::filament::MaterialInstance *>(materialInstance); instance->setStencilWriteMask(mask); } + + EMSCRIPTEN_KEEPALIVE void MaterialInstance_setTransparencyMode( + TMaterialInstance *materialInstance, + TTransparencyMode transparencyMode) + { + auto *instance = reinterpret_cast<::filament::MaterialInstance *>(materialInstance); + instance->setTransparencyMode((filament::TransparencyMode)transparencyMode); + } #ifdef __cplusplus } }