-
Notifications
You must be signed in to change notification settings - Fork 724
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* center * feat: add center story * refactor: remove any from ref typecast Co-authored-by: Josh Ellis <joshua.ellis18@gmail.com>
- Loading branch information
1 parent
1a1492b
commit cf90c76
Showing
6 changed files
with
77 additions
and
4 deletions.
There are no files selected for viewing
Binary file not shown.
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,36 @@ | ||
import * as React from 'react' | ||
import { Vector3 } from 'three' | ||
|
||
import { Setup } from '../Setup' | ||
import { useTurntable } from '../useTurntable' | ||
|
||
import { Box, Center, useGLTF } from '../../src' | ||
|
||
export default { | ||
title: 'Misc/Center', | ||
component: Center, | ||
decorators: [(storyFn) => <Setup cameraPosition={new Vector3(0, 0, -10)}>{storyFn()}</Setup>], | ||
} | ||
|
||
const SimpleExample = () => { | ||
const { scene } = useGLTF('LittlestTokyo.glb') | ||
|
||
const ref = useTurntable() | ||
|
||
return ( | ||
<Center position={[5, 5, 10]}> | ||
<Box args={[10, 10, 10]}> | ||
<meshNormalMaterial attach="material" wireframe /> | ||
</Box> | ||
<primitive ref={ref} object={scene} scale={[0.01, 0.01, 0.01]} /> | ||
</Center> | ||
) | ||
} | ||
|
||
export const DefaultStory = () => ( | ||
<React.Suspense fallback={null}> | ||
<SimpleExample /> | ||
</React.Suspense> | ||
) | ||
|
||
DefaultStory.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,26 @@ | ||
import { Box3, Vector3, Group } from 'three' | ||
import * as React from 'react' | ||
|
||
export const Center = React.forwardRef<Group, JSX.IntrinsicElements['group']>(function Center( | ||
{ children, ...props }, | ||
ref | ||
) { | ||
const outer = React.useRef<Group>() | ||
const inner = React.useRef<Group>() | ||
React.useLayoutEffect(() => { | ||
if (inner.current && outer.current) { | ||
const box = new Box3() | ||
box.setFromObject(inner.current) | ||
const center = new Vector3() | ||
box.getSize(center) | ||
outer.current.position.set(-center.x / 2, -center.y / 2, -center.z / 2) | ||
} | ||
}, [children]) | ||
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
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
cf90c76
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: