Skip to content

Commit

Permalink
feat: Add to method to Bounds (#759)
Browse files Browse the repository at this point in the history
Co-authored-by: Kris Baumgartner <kris@trine.la>
Co-authored-by: Josh <37798644+joshuaellis@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 21, 2022
1 parent 35c3600 commit b77d6fc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -2564,19 +2564,21 @@ Calculates a boundary box and centers the camera accordingly. If you are using c
<mesh />
</Bounds>
```

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() {
const bounds = useBounds()
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]}})
<Bounds>
<Foo />
```
Expand Down
21 changes: 20 additions & 1 deletion src/core/Bounds.tsx
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

1 comment on commit b77d6fc

@vercel
Copy link

@vercel vercel bot commented on b77d6fc Nov 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.