-
Notifications
You must be signed in to change notification settings - Fork 717
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import * as THREE from 'three' | ||
import * as React from 'react' | ||
import { withKnobs } from '@storybook/addon-knobs' | ||
|
||
import { Setup } from '../Setup' | ||
|
||
import { Box, Resize, ResizeProps } from '../../src' | ||
|
||
export default { | ||
title: 'Staging/Resize', | ||
component: Resize, | ||
decorators: [ | ||
withKnobs, | ||
(storyFn) => ( | ||
<Setup camera={{ position: [1, 1, 1], zoom: 150 }} orthographic> | ||
{storyFn()} | ||
</Setup> | ||
), | ||
], | ||
} | ||
|
||
export const ResizeSt = ({ width, height, depth }: ResizeProps) => ( | ||
<> | ||
<axesHelper /> | ||
|
||
<Resize width={width} height={height} depth={depth}> | ||
<Box args={[70, 40, 20]}> | ||
<meshBasicMaterial wireframe /> | ||
</Box> | ||
</Resize> | ||
</> | ||
) | ||
ResizeSt.args = { | ||
width: undefined, | ||
height: undefined, | ||
depth: undefined, | ||
} | ||
|
||
ResizeSt.argTypes = { | ||
width: { control: { type: 'boolean' } }, | ||
height: { control: { type: 'boolean' } }, | ||
depth: { control: { type: 'boolean' } }, | ||
} | ||
|
||
ResizeSt.storyName = 'Default' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as THREE from 'three' | ||
import * as React from 'react' | ||
|
||
export type ResizeProps = JSX.IntrinsicElements['group'] & { | ||
/** Whether to fit into width (x axis), undefined */ | ||
width?: boolean | ||
/** Whether to fit into height (y axis), undefined */ | ||
height?: boolean | ||
/** Whether to fit into depth (z axis), undefined */ | ||
depth?: boolean | ||
/** You can optionally pass the Box3, otherwise will be computed, undefined */ | ||
box3?: THREE.Box3 | ||
/** See https://threejs.org/docs/index.html?q=box3#api/en/math/Box3.setFromObject */ | ||
precise?: boolean | ||
} | ||
|
||
export const Resize = React.forwardRef<THREE.Group, ResizeProps>( | ||
({ children, width, height, depth, box3, precise = true, ...props }, fRef) => { | ||
const ref = React.useRef<THREE.Group>(null!) | ||
const outer = React.useRef<THREE.Group>(null!) | ||
const inner = React.useRef<THREE.Group>(null!) | ||
|
||
React.useLayoutEffect(() => { | ||
outer.current.matrixWorld.identity() | ||
|
||
box3 ||= new THREE.Box3().setFromObject(inner.current, precise) | ||
const w = box3.max.x - box3.min.x | ||
const h = box3.max.y - box3.min.y | ||
const d = box3.max.z - box3.min.z | ||
|
||
let dimension = Math.max(w, h, d) | ||
if (width) dimension = w | ||
if (height) dimension = h | ||
if (depth) dimension = d | ||
|
||
outer.current.scale.setScalar(1 / dimension) | ||
}, [width, height, depth, box3, precise]) | ||
|
||
React.useImperativeHandle(fRef, () => ref.current, []) | ||
|
||
return ( | ||
<group ref={ref} {...props}> | ||
<group ref={outer}> | ||
<group ref={inner}>{children}</group> | ||
</group> | ||
</group> | ||
) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c918df0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
drei – ./
drei-git-master-pmndrs.vercel.app
drei.vercel.app
drei.react-spring.io
drei-pmndrs.vercel.app
drei.pmnd.rs