From ee8e3a50fe98ed584f4026758ae99d98e5d6b260 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 16 Oct 2021 11:52:01 -0400 Subject: [PATCH] Add zoom to fit on first load --- .../src/components/IdeViewer/IdeViewer.tsx | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/app/web/src/components/IdeViewer/IdeViewer.tsx b/app/web/src/components/IdeViewer/IdeViewer.tsx index 4342d94..4a03670 100644 --- a/app/web/src/components/IdeViewer/IdeViewer.tsx +++ b/app/web/src/components/IdeViewer/IdeViewer.tsx @@ -26,9 +26,10 @@ function Asset({ }: { geometry: any dataType: 'INIT' | ArtifactTypes - controlsRef: React.Ref + controlsRef: React.MutableRefObject }) { const threeInstance = useThree() + const [initZoom, setInitZoom] = useState(true) const mesh = useEdgeSplit((thresholdAngle * Math.PI) / 180, true, incomingGeo) const edges = React.useMemo( () => @@ -48,8 +49,22 @@ function Asset({ return bbox.getBoundingSphere(new THREE.Sphere()) } const bSphere = getBoundingSphere() - // do something with bounding sphere and threeInstance - console.log({bSphere, threeInstance, controlsRef}) + + const zoomToFit = () => { + const { center, radius } = bSphere + const { camera } = threeInstance + const offset = 4 + controlsRef.current.reset() + controlsRef.current.target.copy(center) + + camera.position.copy(center.clone().add(new THREE.Vector3(offset * radius, -offset * radius, offset * radius))) + camera.updateProjectionMatrix() + } + if(initZoom){ + zoomToFit() + setInitZoom(false) + } + }, [incomingGeo, dataType]) const PrimitiveArray = React.useMemo( () =>