refactoring

This commit is contained in:
Nick Fisher
2025-03-19 21:52:59 +08:00
parent a310498c5a
commit 931aab5159
12 changed files with 82 additions and 10 deletions

View File

@@ -111,6 +111,8 @@ abstract class MaterialInstance {
Future setParameterFloat3Array(String name, List<Vector3> 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);

View File

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

View File

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

View File

@@ -13,6 +13,17 @@ void using(Pointer ptr, Future Function(Pointer ptr) function) async {
allocator.free(ptr);
}
Future<void> withVoidCallback2(Function() func) async {
final completer = Completer();
void Function() callback = () {
func.call();
completer.complete();
};
final nativeCallable = NativeCallable<Void Function()>.listener(callback);
await completer.future;
nativeCallable.close();
}
Future<void> withVoidCallback(
Function(Pointer<NativeFunction<Void Function()>>) func) async {
final completer = Completer();
@@ -27,8 +38,7 @@ Future<void> withVoidCallback(
}
Future<Pointer<T>> withPointerCallback<T extends NativeType>(
Function(Pointer<NativeFunction<Void Function(Pointer<T>)>>)
func) async {
Function(Pointer<NativeFunction<Void Function(Pointer<T>)>>) func) async {
final completer = Completer<Pointer<T>>();
// ignore: prefer_function_declarations_over_variables
void Function(Pointer<NativeType>) callback = (Pointer<NativeType> ptr) {
@@ -101,4 +111,3 @@ Future<String> withCharPtrCallback(
nativeCallable.close();
return completer.future;
}

View File

@@ -563,6 +563,13 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
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,

View File

@@ -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<Char>(), value);
}
@override
Future setParameterMat4(String name, Matrix4 matrix) async {
final completer = Completer();
final func = () {
MaterialInstance_setParameterMat4(pointer, name.toNativeUtf8().cast<Char>(), matrix.storage.address);
completer.complete();
};
final nativeCallable = NativeCallable<Void Function()>.listener(func);
RenderLoop_addTask(nativeCallable.nativeFunction);
await completer.future;
}
}

View File

@@ -136,6 +136,15 @@ external void MaterialInstance_setParameterFloat4(
double z,
);
@ffi.Native<
ffi.Void Function(ffi.Pointer<TMaterialInstance>, ffi.Pointer<ffi.Char>,
ffi.Pointer<ffi.Double>)>(isLeaf: true)
external void MaterialInstance_setParameterMat4(
ffi.Pointer<TMaterialInstance> materialInstance,
ffi.Pointer<ffi.Char> propertyName,
ffi.Pointer<ffi.Double> matrix,
);
@ffi.Native<
ffi.Void Function(ffi.Pointer<TMaterialInstance>, ffi.Pointer<ffi.Char>,
ffi.Int)>(isLeaf: true)
@@ -1511,6 +1520,13 @@ external void RenderTicker_renderRenderThread(
ffi.Pointer<ffi.NativeFunction<ffi.Void Function()>> onComplete,
);
@ffi.Native<
ffi.Void Function(
ffi.Pointer<ffi.NativeFunction<ffi.Void Function()>>)>(isLeaf: true)
external void RenderLoop_addTask(
ffi.Pointer<ffi.NativeFunction<ffi.Void Function()>> task,
);
@ffi.Native<
ffi.Void Function(
ffi.Pointer<TEngine>,

View File

@@ -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<float>(data[0]), static_cast<float>(data[1]), static_cast<float>(data[2]), static_cast<float>(data[3])},
filament::math::float4{static_cast<float>(data[4]), static_cast<float>(data[5]), static_cast<float>(data[6]), static_cast<float>(data[7])},
filament::math::float4{static_cast<float>(data[8]), static_cast<float>(data[9]), static_cast<float>(data[10]), static_cast<float>(data[11])},
filament::math::float4{static_cast<float>(data[12]), static_cast<float>(data[13]), static_cast<float>(data[14]), static_cast<float>(data[15])}};
}
// Helper function to convert filament::math::mat4 to double4x4
static double4x4 convert_mat4_to_double4x4(const filament::math::mat4 &mat)
{

View File

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

View File

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

View File

@@ -8,7 +8,7 @@
#include <math/vec2.h>
#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<filament::MaterialInstance *>(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);

View File

@@ -50,6 +50,15 @@ extern "C"
}
}
EMSCRIPTEN_KEEPALIVE void RenderLoop_addTask(void (*task)()) {
std::packaged_task<void()> lambda(
[=]() mutable
{
task();
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void RenderLoop_requestAnimationFrame(void (*onComplete)()) {
_rl->requestFrame(onComplete);
}