From 8e8825233780e309abcbcd762abef5c9027292f3 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Thu, 3 Jul 2025 11:51:42 +0800 Subject: [PATCH] attach highlight to all entities, not the ultimate owning asset entity --- .../filament/src/implementation/ffi_view.dart | 68 ++++++++++++------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/thermion_dart/lib/src/filament/src/implementation/ffi_view.dart b/thermion_dart/lib/src/filament/src/implementation/ffi_view.dart index 26211fa6..25b3b077 100644 --- a/thermion_dart/lib/src/filament/src/implementation/ffi_view.dart +++ b/thermion_dart/lib/src/filament/src/implementation/ffi_view.dart @@ -76,8 +76,6 @@ class FFIView extends View> { OverlayManager_setRenderTarget( overlayManager!, overlayRenderTarget!.getNativeHandle()); await oldRenderTarget!.destroy(); - print("Resized render target to ${width}x${height}"); - print(StackTrace.current); }); } } @@ -280,7 +278,7 @@ class FFIView extends View> { RenderTarget? overlayRenderTarget; Material? highlightMaterial; - final _highlighted = {}; + final _highlighted = {}; /// /// @@ -293,8 +291,6 @@ class FFIView extends View> { int? entity, double scale = 1.05, int primitiveIndex = 0}) async { - entity ??= asset.entity; - if (overlayScene == null) { overlayScene = await FilamentApp.instance!.createScene(); final vp = await getViewport(); @@ -309,19 +305,36 @@ class FFIView extends View> { RenderTicker_setOverlayManager(app.renderTicker, overlayManager!); final highlightMaterialPtr = await withPointerCallback( (cb) => Material_createOutlineMaterialRenderThread(app.engine, cb)); - highlightMaterial = FFIMaterial(highlightMaterialPtr, app); + highlightMaterial = + FFIMaterial(highlightMaterialPtr, app); } - var highlightMaterialInstance = await highlightMaterial!.createInstance(); - await highlightMaterialInstance.setParameterFloat("scale", scale); + MaterialInstance? highlightMaterialInstance; - await highlightMaterialInstance.setDepthCullingEnabled(true); - await highlightMaterialInstance.setDepthWriteEnabled(true); + entity ??= asset.entity; + final entities = [entity, ...await asset.getChildEntities()]; - OverlayManager_addComponent( - overlayManager!, entity, highlightMaterialInstance.getNativeHandle()); + for (final entity in entities) { + if (!await FilamentApp.instance!.isRenderable(entity)) { + continue; + } + if (_highlighted.containsKey(entity)) { + _highlighted[entity]!.setParameterFloat4("color", r, g, b, 1.0); + } else { + if (highlightMaterialInstance == null) { + highlightMaterialInstance = await highlightMaterial!.createInstance(); + await highlightMaterialInstance!.setParameterFloat("scale", scale); + await highlightMaterialInstance! + .setParameterFloat4("color", r, g, b, 1.0); - _highlighted[asset] = highlightMaterialInstance; + await highlightMaterialInstance!.setDepthCullingEnabled(true); + await highlightMaterialInstance!.setDepthWriteEnabled(true); + } + OverlayManager_addComponent(overlayManager!, entity, + highlightMaterialInstance.getNativeHandle()); + _highlighted[entity] = highlightMaterialInstance!; + } + } _logger.info("Added stencil highlight for asset (entity ${asset.entity})"); } @@ -331,22 +344,29 @@ class FFIView extends View> { /// @override Future removeStencilHighlight(ThermionAsset asset) async { - if (!_highlighted.containsKey(asset)) { - _logger - .warning("No stencil highlight for asset (entity ${asset.entity})"); + if (overlayManager == null) { return; } - final materialInstance = _highlighted[asset]!; - _highlighted.remove(asset); - _logger - .info("Removing stencil highlight for asset (entity ${asset.entity})"); + final entities = [asset.entity, ...await asset.getChildEntities()]; - OverlayManager_removeComponent(overlayManager!, asset.entity); + for (final entity in entities) { + OverlayManager_removeComponent(overlayManager!, entity); + } - await materialInstance.destroy(); + final destroyed = {}; + for (final entity in entities) { + final materialInstance = _highlighted[entity]; + if (!await FilamentApp.instance!.isRenderable(entity) || + materialInstance == null) { + continue; + } - _logger - .info("Removed stencil highlight for asset (entity ${asset.entity})"); + _highlighted.remove(entity); + if (!destroyed.contains(materialInstance)) { + await materialInstance.destroy(); + destroyed.add(materialInstance); + } + } } void setName(String name) {