internal: remove bounding box asset when parent asset removed

This commit is contained in:
Nick Fisher
2025-01-02 10:30:13 +08:00
parent 31e453a4e6
commit 7717387909
3 changed files with 45 additions and 17 deletions

View File

@@ -6,6 +6,7 @@ import 'package:thermion_dart/src/viewer/src/ffi/src/thermion_viewer_ffi.dart';
import 'package:thermion_dart/thermion_dart.dart';
class FFIAsset extends ThermionAsset {
final Pointer<TSceneAsset> pointer;
final Pointer<TSceneManager> sceneManager;
Pointer<TRenderableManager> get renderableManager =>
@@ -188,7 +189,7 @@ class FFIAsset extends ThermionAsset {
}
}
ThermionAsset? _boundingBoxAsset;
FFIAsset? boundingBoxAsset;
Future<Aabb3> getBoundingBox() async {
late ThermionEntity targetEntity;
@@ -201,9 +202,10 @@ class FFIAsset extends ThermionAsset {
return aabb3;
}
@override
Future<void> setBoundingBoxVisibility(bool visible) async {
if (_boundingBoxAsset == null) {
if (boundingBoxAsset == null) {
final boundingBox = await getBoundingBox();
final min = [
boundingBox.centerX - boundingBox.halfExtentX,
@@ -277,19 +279,19 @@ class FFIAsset extends ThermionAsset {
primitiveType: PrimitiveType.LINES,
);
_boundingBoxAsset = await viewer.createGeometry(
boundingBoxAsset = await viewer.createGeometry(
geometry,
materialInstances: [material],
keepData: false,
);
) as FFIAsset;
TransformManager_setParent(Engine_getTransformManager(engine),
_boundingBoxAsset!.entity, entity, false);
boundingBoxAsset!.entity, entity, false);
}
if (visible) {
await _boundingBoxAsset!.addToScene();
await boundingBoxAsset!.addToScene();
} else {
await _boundingBoxAsset!.removeFromScene();
await boundingBoxAsset!.removeFromScene();
}
}
}

View File

@@ -510,8 +510,8 @@ class ThermionViewerFFI extends ThermionViewer {
throw Exception("An error occurred loading the asset at $path");
}
var thermionAsset =
FFIAsset(asset, _sceneManager!, _engine!, _unlitMaterialProvider!, this);
var thermionAsset = FFIAsset(
asset, _sceneManager!, _engine!, _unlitMaterialProvider!, this);
return thermionAsset;
}
@@ -973,6 +973,12 @@ class ThermionViewerFFI extends ThermionViewer {
///
@override
Future removeAsset(covariant FFIAsset asset) async {
if (asset.boundingBoxAsset != null) {
await asset.setBoundingBoxVisibility(false);
await withVoidCallback((callback) =>
SceneManager_destroyAssetRenderThread(
_sceneManager!, asset.boundingBoxAsset!.pointer, callback));
}
await withVoidCallback((callback) => SceneManager_destroyAssetRenderThread(
_sceneManager!, asset.pointer, callback));
}
@@ -1641,8 +1647,8 @@ class ThermionViewerFFI extends ThermionViewer {
throw Exception("Failed to create geometry");
}
var asset =
FFIAsset(assetPtr, _sceneManager!, _engine!, _unlitMaterialProvider!, this);
var asset = FFIAsset(
assetPtr, _sceneManager!, _engine!, _unlitMaterialProvider!, this);
return asset;
}
@@ -1760,7 +1766,8 @@ class ThermionViewerFFI extends ThermionViewer {
_sceneManager!, material.pointer, cb);
}
});
_grid = FFIAsset(ptr, _sceneManager!, _engine!, _unlitMaterialProvider!, this);
_grid = FFIAsset(
ptr, _sceneManager!, _engine!, _unlitMaterialProvider!, this);
}
await _grid!.addToScene();
await setLayerVisibility(VisibilityLayers.OVERLAY, true);