From 2a3fb0fd8472b59d84ace8068adb384add81f062 Mon Sep 17 00:00:00 2001 From: Davor Hrg Date: Wed, 28 Jul 2021 14:27:50 +0200 Subject: [PATCH] fix for react-fiber not properly cleaning the scene --- app/web/src/components/IdeViewer/IdeViewer.tsx | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app/web/src/components/IdeViewer/IdeViewer.tsx b/app/web/src/components/IdeViewer/IdeViewer.tsx index cf71f89..fcd3efd 100644 --- a/app/web/src/components/IdeViewer/IdeViewer.tsx +++ b/app/web/src/components/IdeViewer/IdeViewer.tsx @@ -10,9 +10,12 @@ import { TextureLoader } from 'three/src/loaders/TextureLoader' const loader = new TextureLoader() const colorMap = loader.load(texture) +let lastGroup + extend({ OrbitControls }) function Asset({ geometry: incomingGeo }) { + const state = useThree() const mesh = useRef() const ref = useRef({}) useLayoutEffect(() => { @@ -21,7 +24,20 @@ function Asset({ geometry: incomingGeo }) { } }, [incomingGeo]) if (!incomingGeo) return null - + + let groupData = incomingGeo.children ? incomingGeo : null + if(lastGroup && lastGroup != groupData){ + state.scene.remove(lastGroup) + lastGroup.children.forEach(c=>c?.geometry?.dispose()) + // returning does not add the new group to the scene + // there is probably some useRef magic that would make this work, but I don't have time to reseach it + /// FIXME - do this properly with useRef or other react magic + if(groupData) state.scene.add(groupData) + } + lastGroup = groupData + + if(groupData) return + if (incomingGeo.children) return return (