refactoring

This commit is contained in:
Nick Fisher
2025-03-22 10:49:24 +08:00
parent a67f42f0de
commit 0cbbc058e0
22 changed files with 675 additions and 463 deletions

View File

@@ -6,6 +6,7 @@ import 'package:thermion_dart/src/filament/src/engine.dart';
import 'package:thermion_dart/src/filament/src/scene.dart';
import 'package:thermion_dart/src/viewer/src/ffi/src/callbacks.dart';
import 'package:thermion_dart/src/viewer/src/ffi/src/ffi_asset.dart';
import 'package:thermion_dart/src/viewer/src/ffi/src/ffi_gizmo.dart';
import 'package:thermion_dart/src/viewer/src/ffi/src/ffi_material.dart';
import 'package:thermion_dart/src/viewer/src/ffi/src/ffi_render_target.dart';
import 'package:thermion_dart/src/viewer/src/ffi/src/ffi_scene.dart';
@@ -609,14 +610,6 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
.firstWhere((x) => _swapChains[x]?.contains(view) == true);
final out = Uint8List(viewport.width * viewport.height * 4);
await withVoidCallback((cb) {
Engine_flushAndWaitRenderThead(engine, cb);
});
var fence = await withPointerCallback<TFence>((cb) {
Engine_createFenceRenderThread(engine, cb);
});
await withBoolCallback((cb) {
Renderer_beginFrameRenderThread(renderer, swapChain.swapChain, 0, cb);
});
@@ -647,20 +640,18 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
});
await withVoidCallback((cb) {
Engine_destroyFenceRenderThread(engine, fence, cb);
Engine_flushAndWaitRenderThead(engine, cb);
});
// await withVoidCallback((cb) {
// Engine_flushAndWaitRenderThead(engine, cb);
// });
return out;
}
///
///
///
Future setClearColor(double r, double g, double b, double a) async {
Renderer_setClearOptions(renderer, r, g, b, a, 0, true, false);
Future setClearOptions(double r, double g, double b, double a,
{int clearStencil = 0, bool discard = false, bool clear = true}) async {
Renderer_setClearOptions(
renderer, r, g, b, a, clearStencil, clear, discard);
}
///
@@ -735,7 +726,7 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
View_setColorGrading(view.view, nullptr);
for (final cg in view.colorGrading.entries) {
await withVoidCallback(
(cb) => Engine_destroyColorGradingRenderThread(engine, cg.value, cb));
(cb) => Engine_destroyColorGradingRenderThread(engine, cg.value, cb));
}
await withVoidCallback(
(cb) => Engine_destroyViewRenderThread(engine, view.view, cb));
@@ -751,6 +742,43 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
ColorGrading_createRenderThread(
engine, TToneMapping.values[mapper.index], cb));
}
FFIMaterial? _gizmoMaterial;
///
///
///
Future<GizmoAsset> createGizmo(covariant FFIView view,
Pointer animationManager, GizmoType gizmoType) async {
if (_gizmoMaterial == null) {
final materialPtr = await withPointerCallback<TMaterial>((cb) {
Material_createGizmoMaterialRenderThread(engine, cb);
});
_gizmoMaterial ??= FFIMaterial(materialPtr, this);
}
var gltfResourceLoader = await withPointerCallback<TGltfResourceLoader>(
(cb) => GltfResourceLoader_createRenderThread(engine, nullptr, cb));
final gizmo = await withPointerCallback<TGizmo>((cb) {
Gizmo_createRenderThread(engine, gltfAssetLoader, gltfResourceLoader, nameComponentManager,
view.view, _gizmoMaterial!.pointer, TGizmoType.values[gizmoType.index], cb);
});
if (gizmo == nullptr) {
throw Exception("Failed to create gizmo");
}
final gizmoEntityCount =
SceneAsset_getChildEntityCount(gizmo.cast<TSceneAsset>());
final gizmoEntities = Int32List(gizmoEntityCount);
SceneAsset_getChildEntities(
gizmo.cast<TSceneAsset>(), gizmoEntities.address);
return FFIGizmo(gizmo.cast<TSceneAsset>(), this,
animationManager.cast<TAnimationManager>(),
view: view,
entities: gizmoEntities.toSet()
..add(SceneAsset_getEntity(gizmo.cast<TSceneAsset>())));
}
}
class FinalizableUint8List implements Finalizable {

View File

@@ -25,9 +25,9 @@ class FFIGizmo extends FFIAsset implements GizmoAsset {
bool isGizmoEntity(ThermionEntity entity) => entities.contains(entity);
FFIGizmo(
super.asset,
super.app,
super.animationManager,
super.asset,
super.app,
super.animationManager,
{
required this.view,
required this.entities,

View File

@@ -35,6 +35,11 @@ external ffi.Pointer<TMaterial> Material_createGridMaterial(
ffi.Pointer<TEngine> tEngine,
);
@ffi.Native<ffi.Pointer<TMaterial> Function(ffi.Pointer<TEngine>)>(isLeaf: true)
external ffi.Pointer<TMaterial> Material_createGizmoMaterial(
ffi.Pointer<TEngine> tEngine,
);
@ffi.Native<ffi.Bool Function(ffi.Pointer<TMaterial>, ffi.Pointer<ffi.Char>)>(
isLeaf: true)
external bool Material_hasParameter(
@@ -1057,22 +1062,40 @@ external void TextureSampler_destroy(
);
@ffi.Native<
ffi.Pointer<TGizmo> Function(ffi.Pointer<TEngine>, ffi.Pointer<TView>,
ffi.Pointer<TGizmo> Function(
ffi.Pointer<TEngine>,
ffi.Pointer<TGltfAssetLoader>,
ffi.Pointer<TGltfResourceLoader>,
ffi.Pointer<TNameComponentManager>,
ffi.Pointer<TView>,
ffi.Pointer<TMaterial>,
ffi.UnsignedInt)>(symbol: "Gizmo_create", isLeaf: true)
external ffi.Pointer<TGizmo> _Gizmo_create(
ffi.Pointer<TEngine> tEngine,
ffi.Pointer<TGltfAssetLoader> assetLoader,
ffi.Pointer<TGltfResourceLoader> tGltfResourceLoader,
ffi.Pointer<TNameComponentManager> tNameComponentManager,
ffi.Pointer<TView> tView,
ffi.Pointer<TMaterial> tMaterial,
int tGizmoType,
);
ffi.Pointer<TGizmo> Gizmo_create(
ffi.Pointer<TEngine> tEngine,
ffi.Pointer<TGltfAssetLoader> assetLoader,
ffi.Pointer<TGltfResourceLoader> tGltfResourceLoader,
ffi.Pointer<TNameComponentManager> tNameComponentManager,
ffi.Pointer<TView> tView,
ffi.Pointer<TMaterial> tMaterial,
TGizmoType tGizmoType,
) =>
_Gizmo_create(
tEngine,
assetLoader,
tGltfResourceLoader,
tNameComponentManager,
tView,
tMaterial,
tGizmoType.value,
);
@@ -1991,6 +2014,19 @@ external void Material_createImageMaterialRenderThread(
onComplete,
);
@ffi.Native<
ffi.Void Function(
ffi.Pointer<TEngine>,
ffi.Pointer<
ffi
.NativeFunction<ffi.Void Function(ffi.Pointer<TMaterial>)>>)>(
isLeaf: true)
external void Material_createGizmoMaterialRenderThread(
ffi.Pointer<TEngine> tEngine,
ffi.Pointer<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<TMaterial>)>>
onComplete,
);
@ffi.Native<
ffi.Void Function(
ffi.Pointer<TEngine>,
@@ -2151,17 +2187,6 @@ external void MaterialProvider_createMaterialInstanceRenderThread(
callback,
);
@ffi.Native<
ffi.Void Function(
ffi.Pointer<TSceneManager>,
ffi.Pointer<TMaterialInstance>,
ffi.Pointer<ffi.NativeFunction<ffi.Void Function()>>)>(isLeaf: true)
external void SceneManager_destroyMaterialInstanceRenderThread(
ffi.Pointer<TSceneManager> tSceneManager,
ffi.Pointer<TMaterialInstance> tMaterialInstance,
ffi.Pointer<ffi.NativeFunction<ffi.Void Function()>> callback,
);
@ffi.Native<
ffi.Void Function(
ffi.Pointer<TAnimationManager>,
@@ -2741,6 +2766,52 @@ external void Scene_addFilamentAssetRenderThread(
ffi.Pointer<ffi.NativeFunction<ffi.Void Function()>> callback,
);
@ffi.Native<
ffi.Void Function(
ffi.Pointer<TEngine>,
ffi.Pointer<TGltfAssetLoader>,
ffi.Pointer<TGltfResourceLoader>,
ffi.Pointer<TNameComponentManager>,
ffi.Pointer<TView>,
ffi.Pointer<TMaterial>,
ffi.UnsignedInt,
ffi.Pointer<
ffi.NativeFunction<ffi.Void Function(ffi.Pointer<TGizmo>)>>)>(
symbol: "Gizmo_createRenderThread", isLeaf: true)
external void _Gizmo_createRenderThread(
ffi.Pointer<TEngine> tEngine,
ffi.Pointer<TGltfAssetLoader> tAssetLoader,
ffi.Pointer<TGltfResourceLoader> tGltfResourceLoader,
ffi.Pointer<TNameComponentManager> tNameComponentManager,
ffi.Pointer<TView> tView,
ffi.Pointer<TMaterial> tMaterial,
int tGizmoType,
ffi.Pointer<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<TGizmo>)>>
callback,
);
void Gizmo_createRenderThread(
ffi.Pointer<TEngine> tEngine,
ffi.Pointer<TGltfAssetLoader> tAssetLoader,
ffi.Pointer<TGltfResourceLoader> tGltfResourceLoader,
ffi.Pointer<TNameComponentManager> tNameComponentManager,
ffi.Pointer<TView> tView,
ffi.Pointer<TMaterial> tMaterial,
TGizmoType tGizmoType,
ffi.Pointer<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<TGizmo>)>>
callback,
) =>
_Gizmo_createRenderThread(
tEngine,
tAssetLoader,
tGltfResourceLoader,
tNameComponentManager,
tView,
tMaterial,
tGizmoType.value,
callback,
);
@ffi.Native<
ffi.Pointer<TGltfResourceLoader> Function(
ffi.Pointer<TEngine>, ffi.Pointer<ffi.Char>)>(isLeaf: true)
@@ -3824,15 +3895,15 @@ final class Aabb3 extends ffi.Struct {
}
enum TGizmoType {
TRANSLATION(0),
ROTATION(1);
GIZMO_TYPE_TRANSLATION(0),
GIZMO_TYPE_ROTATION(1);
final int value;
const TGizmoType(this.value);
static TGizmoType fromValue(int value) => switch (value) {
0 => TRANSLATION,
1 => ROTATION,
0 => GIZMO_TYPE_TRANSLATION,
1 => GIZMO_TYPE_ROTATION,
_ => throw ArgumentError("Unknown value for TGizmoType: $value"),
};
}

View File

@@ -90,9 +90,8 @@ class ThermionViewerFFI extends ThermionViewer {
View_setAntiAliasing(view.view, false, false, false);
View_setDitheringEnabled(view.view, false);
View_setRenderQuality(view.view, TQualityLevel.MEDIUM);
await FilamentApp.instance!.setClearColor(1.0, 0.0, 0.0, 1.0);
await FilamentApp.instance!.setClearOptions(0.0, 0.0, 0.0, 0.0);
scene = FFIScene(Engine_createScene(app.engine));
await view.setScene(scene);
final camera = FFICamera(
@@ -431,8 +430,6 @@ class ThermionViewerFFI extends ThermionViewer {
}
}
///
///
///
@@ -602,10 +599,15 @@ class ThermionViewerFFI extends ThermionViewer {
///
///
///
Future showGridOverlay() async {
Future setGridOverlayVisibility(bool visible) async {
_grid ??= _grid = await GridOverlay.create(app, animationManager);
await scene.add(_grid!);
await view.setLayerVisibility(VisibilityLayers.OVERLAY, true);
if (visible) {
await scene.add(_grid!);
await view.setLayerVisibility(VisibilityLayers.OVERLAY, true);
} else {
await scene.remove(_grid!);
await view.setLayerVisibility(VisibilityLayers.OVERLAY, true);
}
}
///
@@ -720,36 +722,18 @@ class ThermionViewerFFI extends ThermionViewer {
return asset;
}
////
final _gizmos = <GizmoType, GizmoAsset>{};
///
///
///
//
@override
Future<GizmoAsset> createGizmo(FFIView view, GizmoType gizmoType) async {
throw UnimplementedError();
// var scene = View_getScene(view.view);
// final gizmo = await withPointerCallback<TGizmo>((cb) {
// SceneManager_createGizmoRenderThread(_sceneManager!, view.view, scene,
// TGizmoType.values[gizmoType.index], cb);
// });
// if (gizmo == nullptr) {
// throw Exception("Failed to create gizmo");
// }
// final gizmoEntityCount =
// SceneAsset_getChildEntityCount(gizmo.cast<TSceneAsset>());
// final gizmoEntities = Int32List(gizmoEntityCount);
// SceneAsset_getChildEntities(
// gizmo.cast<TSceneAsset>(), gizmoEntities.address);
// return FFIGizmo(
// view,
// gizmo.cast<TSceneAsset>(),
// _sceneManager!,
// app.engine!,
// nullptr,
// this,
// gizmoEntities.toSet()
// ..add(SceneAsset_getEntity(gizmo.cast<TSceneAsset>())));
Future<GizmoAsset> getGizmo(GizmoType gizmoType) async {
if (_gizmos[gizmoType] == null) {
_gizmos[gizmoType] =
await FilamentApp.instance!.createGizmo(view, animationManager, gizmoType);
}
return _gizmos[gizmoType]!;
}
Future addToScene(covariant FFIAsset asset) async {

View File

@@ -239,9 +239,10 @@ abstract class ThermionViewer {
{List<MaterialInstance>? materialInstances, bool keepData = false, bool addToScene=true});
///
/// The gizmo for translating/rotating objects. Only one gizmo can be active for a given view.
/// Returns a gizmo for translating/rotating objects.
/// Only one gizmo can be visible at any given time for this viewer.
///
Future<GizmoAsset> createGizmo(covariant View view, GizmoType type);
Future<GizmoAsset> getGizmo(GizmoType type);
///
/// Register a callback to be invoked when this viewer is disposed.
@@ -261,12 +262,7 @@ abstract class ThermionViewer {
///
///
///
Future showGridOverlay();
///
///
///
Future removeGridOverlay();
Future setGridOverlayVisibility(bool visible);
///
///
@@ -293,6 +289,14 @@ abstract class ThermionViewer {
///
int getCameraCount();
///
/// Adds the asset to the scene, meaning the asset will be rendered/visible.
///
Future addToScene(covariant ThermionAsset asset);
///
/// Removes the asset from the scene, meaning the asset will not be rendered/visible.
/// The asset itself will remain valid.
///
Future removeFromScene(covariant ThermionAsset asset);
}