diff --git a/README.md b/README.md index 65594c6ec..cb21279b8 100644 --- a/README.md +++ b/README.md @@ -2564,8 +2564,7 @@ Calculates a boundary box and centers the camera accordingly. If you are using c ``` - -The Bounds component also acts as a context provider, use the `useBounds` hook to refresh the bounds, fit the camera, clip near/far planes or focus objects. `refresh(object?: THREE.Object3D | THREE.Box3)` will recalculate bounds. Since this can be expensive only call it when you know the view has changed. `clip` sets the cameras near/far planes. `fit` zooms and centers the view. +The Bounds component also acts as a context provider, use the `useBounds` hook to refresh the bounds, fit the camera, clip near/far planes, go to camera orientations or focus objects. `refresh(object?: THREE.Object3D | THREE.Box3)` will recalculate bounds, since this can be expensive only call it when you know the view has changed. `clip` sets the cameras near/far planes. `to` sets a position and target for the camera. `fit` zooms and centers the view. ```jsx function Foo() { @@ -2573,10 +2572,13 @@ function Foo() { useEffect(() => { // Calculate scene bounds bounds.refresh().clip().fit() + // Or, focus a specific object or box3 // bounds.refresh(ref.current).clip().fit() // bounds.refresh(new THREE.Box3()).clip().fit() + // Or, send the camera to a specific orientatin + // bounds.to({position: [0, 10, 10], target: {[5, 5, 0]}}) ``` diff --git a/src/core/Bounds.tsx b/src/core/Bounds.tsx index 9dd6856e5..edcc9fc86 100644 --- a/src/core/Bounds.tsx +++ b/src/core/Bounds.tsx @@ -94,7 +94,7 @@ export function Bounds({ children, damping = 6, fit, clip, observe, margin = 1.2 } if (controls?.constructor.name === 'OrthographicTrackballControls') { - // Put camera on a sphere along which it should moves + // Put camera on a sphere along which it should move const { distance } = getSize() const direction = camera.position.clone().sub(controls.target).normalize().multiplyScalar(distance) const newPos = controls.target.clone().add(direction) @@ -113,6 +113,25 @@ export function Bounds({ children, damping = 6, fit, clip, observe, margin = 1.2 invalidate() return this }, + to({ position, target }: { position: [number, number, number]; target?: [number, number, number] }) { + current.camera.copy(camera.position) + const { center } = getSize() + goal.camera.set(...position) + + if (target) { + goal.focus.set(...target) + } else { + goal.focus.copy(center) + } + + if (damping) { + current.animating = true + } else { + camera.position.set(...position) + } + + return this + }, fit() { current.camera.copy(camera.position) if (controls) current.focus.copy(controls.target)