diff --git a/thermion_dart/lib/src/filament/src/material.dart b/thermion_dart/lib/src/filament/src/material.dart index 375625f5..c67e82f6 100644 --- a/thermion_dart/lib/src/filament/src/material.dart +++ b/thermion_dart/lib/src/filament/src/material.dart @@ -111,6 +111,8 @@ abstract class MaterialInstance { Future setParameterFloat3Array(String name, List data); Future setParameterFloat4( String name, double x, double y, double z, double w); + Future setParameterMat4( + String name, Matrix4 matrix); Future setParameterInt(String name, int value); Future setParameterBool(String name, bool value); diff --git a/thermion_dart/lib/src/utils/src/geometry.dart b/thermion_dart/lib/src/utils/src/geometry.dart index 482e0970..2297f8fb 100644 --- a/thermion_dart/lib/src/utils/src/geometry.dart +++ b/thermion_dart/lib/src/utils/src/geometry.dart @@ -1,14 +1,12 @@ import 'dart:math'; import 'dart:typed_data'; -import 'package:vector_math/vector_math_64.dart'; - import '../../../thermion_dart.dart'; class GeometryHelper { static Geometry fullscreenQuad() { final vertices = Float32List.fromList( - [-1.0, -1.0, 1.0, 1.0, 3.0, -1.0, 1.0, 1.0, -1.0, 3.0, 1.0, 1.0]); + [-1.0, -1.0, 1.0, 3.0, -1.0, 1.0, -1.0, 3.0, 1.0]); final indices = [0, 1, 2]; return Geometry(vertices, indices); } diff --git a/thermion_dart/lib/src/viewer/src/ffi/src/background_image.dart b/thermion_dart/lib/src/viewer/src/ffi/src/background_image.dart index a4574428..2023091f 100644 --- a/thermion_dart/lib/src/viewer/src/ffi/src/background_image.dart +++ b/thermion_dart/lib/src/viewer/src/ffi/src/background_image.dart @@ -27,7 +27,7 @@ class BackgroundImage extends ThermionAsset { Future destroy() async { Scene_removeEntity(scene.scene, entity); - + await texture!.dispose(); await sampler!.dispose(); await mi.destroy(); @@ -40,6 +40,9 @@ class BackgroundImage extends ThermionAsset { var backgroundImage = await viewer.createGeometry(GeometryHelper.fullscreenQuad()); + await imageMaterialInstance.setParameterInt("showImage", 0); + await imageMaterialInstance.setParameterMat4("transform", Matrix4.identity()); + backgroundImage.setMaterialInstanceAt(imageMaterialInstance); await scene.add(backgroundImage as FFIAsset); return BackgroundImage._( @@ -61,7 +64,6 @@ class BackgroundImage extends ThermionAsset { await mi.setParameterTexture( "image", texture as FFITexture, sampler as FFITextureSampler); - } /// diff --git a/thermion_dart/lib/src/viewer/src/ffi/src/callbacks.dart b/thermion_dart/lib/src/viewer/src/ffi/src/callbacks.dart index 9ed40923..18a7ef77 100644 --- a/thermion_dart/lib/src/viewer/src/ffi/src/callbacks.dart +++ b/thermion_dart/lib/src/viewer/src/ffi/src/callbacks.dart @@ -13,6 +13,17 @@ void using(Pointer ptr, Future Function(Pointer ptr) function) async { allocator.free(ptr); } +Future withVoidCallback2(Function() func) async { + final completer = Completer(); + void Function() callback = () { + func.call(); + completer.complete(); + }; + final nativeCallable = NativeCallable.listener(callback); + await completer.future; + nativeCallable.close(); +} + Future withVoidCallback( Function(Pointer>) func) async { final completer = Completer(); @@ -27,8 +38,7 @@ Future withVoidCallback( } Future> withPointerCallback( - Function(Pointer)>>) - func) async { + Function(Pointer)>>) func) async { final completer = Completer>(); // ignore: prefer_function_declarations_over_variables void Function(Pointer) callback = (Pointer ptr) { @@ -101,4 +111,3 @@ Future withCharPtrCallback( nativeCallable.close(); return completer.future; } - diff --git a/thermion_dart/lib/src/viewer/src/ffi/src/ffi_filament_app.dart b/thermion_dart/lib/src/viewer/src/ffi/src/ffi_filament_app.dart index c73c4195..286e9438 100644 --- a/thermion_dart/lib/src/viewer/src/ffi/src/ffi_filament_app.dart +++ b/thermion_dart/lib/src/viewer/src/ffi/src/ffi_filament_app.dart @@ -563,6 +563,13 @@ class FFIFilamentApp extends FilamentApp { await withBoolCallback((cb) { Renderer_beginFrameRenderThread(renderer, swapChain!.swapChain, 0, cb); }); + await withVoidCallback((cb) { + Renderer_renderRenderThread( + renderer, + view.view, + cb, + ); + }); await withVoidCallback((cb) { Renderer_readPixelsRenderThread( renderer, 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 4f240a9a..10a02aee 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 @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:typed_data'; import 'package:thermion_dart/src/viewer/src/ffi/src/callbacks.dart'; @@ -192,4 +193,16 @@ class FFIMaterialInstance extends MaterialInstance { MaterialInstance_setParameterBool( pointer, name.toNativeUtf8().cast(), value); } + + @override + Future setParameterMat4(String name, Matrix4 matrix) async { + final completer = Completer(); + final func = () { + MaterialInstance_setParameterMat4(pointer, name.toNativeUtf8().cast(), matrix.storage.address); + completer.complete(); + }; + final nativeCallable = NativeCallable.listener(func); + RenderLoop_addTask(nativeCallable.nativeFunction); + await completer.future; + } } diff --git a/thermion_dart/lib/src/viewer/src/ffi/src/thermion_dart.g.dart b/thermion_dart/lib/src/viewer/src/ffi/src/thermion_dart.g.dart index 2be08ff5..84d703cd 100644 --- a/thermion_dart/lib/src/viewer/src/ffi/src/thermion_dart.g.dart +++ b/thermion_dart/lib/src/viewer/src/ffi/src/thermion_dart.g.dart @@ -136,6 +136,15 @@ external void MaterialInstance_setParameterFloat4( double z, ); +@ffi.Native< + ffi.Void Function(ffi.Pointer, ffi.Pointer, + ffi.Pointer)>(isLeaf: true) +external void MaterialInstance_setParameterMat4( + ffi.Pointer materialInstance, + ffi.Pointer propertyName, + ffi.Pointer matrix, +); + @ffi.Native< ffi.Void Function(ffi.Pointer, ffi.Pointer, ffi.Int)>(isLeaf: true) @@ -1511,6 +1520,13 @@ external void RenderTicker_renderRenderThread( ffi.Pointer> onComplete, ); +@ffi.Native< + ffi.Void Function( + ffi.Pointer>)>(isLeaf: true) +external void RenderLoop_addTask( + ffi.Pointer> task, +); + @ffi.Native< ffi.Void Function( ffi.Pointer, diff --git a/thermion_dart/native/include/MathUtils.hpp b/thermion_dart/native/include/MathUtils.hpp index 52c49d7f..8a870bc3 100644 --- a/thermion_dart/native/include/MathUtils.hpp +++ b/thermion_dart/native/include/MathUtils.hpp @@ -4,6 +4,16 @@ namespace thermion { +// Helper function to convert double* to filament::math::mat4f +static filament::math::mat4f convert_double_to_mat4f(double* data) +{ + return filament::math::mat4f { + filament::math::float4{static_cast(data[0]), static_cast(data[1]), static_cast(data[2]), static_cast(data[3])}, + filament::math::float4{static_cast(data[4]), static_cast(data[5]), static_cast(data[6]), static_cast(data[7])}, + filament::math::float4{static_cast(data[8]), static_cast(data[9]), static_cast(data[10]), static_cast(data[11])}, + filament::math::float4{static_cast(data[12]), static_cast(data[13]), static_cast(data[14]), static_cast(data[15])}}; +} + // Helper function to convert filament::math::mat4 to double4x4 static double4x4 convert_mat4_to_double4x4(const filament::math::mat4 &mat) { diff --git a/thermion_dart/native/include/c_api/TMaterialInstance.h b/thermion_dart/native/include/c_api/TMaterialInstance.h index 0ff40a61..f1351d7e 100644 --- a/thermion_dart/native/include/c_api/TMaterialInstance.h +++ b/thermion_dart/native/include/c_api/TMaterialInstance.h @@ -82,6 +82,7 @@ extern "C" EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat3(TMaterialInstance *materialInstance, const char *propertyName, double x, double y, double z); EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat3Array(TMaterialInstance *tMaterialInstance, const char *propertyName, double *raw, uint32_t length); EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat4(TMaterialInstance *materialInstance, const char *propertyName, double x, double y, double w, double z); + EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterMat4(TMaterialInstance *materialInstance, const char *propertyName, double *matrix); EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterInt(TMaterialInstance *materialInstance, const char *propertyName, int value); EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterBool(TMaterialInstance *materialInstance, const char *propertyName, bool value); EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterTexture(TMaterialInstance *materialInstance, const char *propertyName, TTexture *texture, TTextureSampler *sampler); diff --git a/thermion_dart/native/include/c_api/ThermionDartRenderThreadApi.h b/thermion_dart/native/include/c_api/ThermionDartRenderThreadApi.h index d5dfb0d2..afdd81aa 100644 --- a/thermion_dart/native/include/c_api/ThermionDartRenderThreadApi.h +++ b/thermion_dart/native/include/c_api/ThermionDartRenderThreadApi.h @@ -18,7 +18,7 @@ namespace thermion EMSCRIPTEN_KEEPALIVE void RenderLoop_destroy(); EMSCRIPTEN_KEEPALIVE void RenderLoop_requestAnimationFrame(void (*onComplete)()); EMSCRIPTEN_KEEPALIVE void RenderTicker_renderRenderThread(TRenderTicker *tRenderTicker, uint64_t frameTimeInNanos, void (*onComplete)()); - // EMSCRIPTEN_KEEPALIVE void RenderLoop_addTask(TRenderLoop* tRenderLoop, void (*task)()); + EMSCRIPTEN_KEEPALIVE void RenderLoop_addTask(void (*task)()); EMSCRIPTEN_KEEPALIVE void AnimationManager_createRenderThread(TEngine *tEngine, TScene *tScene, void (*onComplete)(TAnimationManager *)); diff --git a/thermion_dart/native/src/c_api/TMaterialInstance.cpp b/thermion_dart/native/src/c_api/TMaterialInstance.cpp index ea36ebd3..96689e36 100644 --- a/thermion_dart/native/src/c_api/TMaterialInstance.cpp +++ b/thermion_dart/native/src/c_api/TMaterialInstance.cpp @@ -8,7 +8,7 @@ #include #include "Log.hpp" - +#include "MathUtils.hpp" #include "material/image.h" #include "material/grid.h" @@ -125,6 +125,11 @@ namespace thermion } + EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterMat4(TMaterialInstance *tMaterialInstance, const char *propertyName, double *matrix) { + auto *mi = reinterpret_cast(tMaterialInstance); + mi->setParameter(propertyName, convert_double_to_mat4f(matrix)); + } + EMSCRIPTEN_KEEPALIVE void MaterialInstance_setDepthFunc(TMaterialInstance *tMaterialInstance, TSamplerCompareFunc tDepthFunc) { auto *materialInstance = reinterpret_cast<::filament::MaterialInstance *>(tMaterialInstance); diff --git a/thermion_dart/native/src/c_api/ThermionDartRenderThreadApi.cpp b/thermion_dart/native/src/c_api/ThermionDartRenderThreadApi.cpp index 3ecaa434..c6eae607 100644 --- a/thermion_dart/native/src/c_api/ThermionDartRenderThreadApi.cpp +++ b/thermion_dart/native/src/c_api/ThermionDartRenderThreadApi.cpp @@ -50,6 +50,15 @@ extern "C" } } + EMSCRIPTEN_KEEPALIVE void RenderLoop_addTask(void (*task)()) { + std::packaged_task lambda( + [=]() mutable + { + task(); + }); + auto fut = _rl->add_task(lambda); + } + EMSCRIPTEN_KEEPALIVE void RenderLoop_requestAnimationFrame(void (*onComplete)()) { _rl->requestFrame(onComplete); }