-
Notifications
You must be signed in to change notification settings - Fork 714
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
1 parent
dc310ca
commit 8b4c8bd
Showing
4 changed files
with
134 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,69 @@ | ||
import { useFrame } from '@react-three/fiber' | ||
import * as React from 'react' | ||
import * as THREE from 'three' | ||
import { BoxHelper, CameraHelper } from 'three' | ||
import { VertexNormalsHelper } from 'three-stdlib' | ||
import { Helper, PerspectiveCamera, Sphere } from '../../src' | ||
import { Setup } from '../Setup' | ||
|
||
export default { | ||
title: 'Gizmos/Helper', | ||
component: Helper, | ||
decorators: [(storyFn) => <Setup>{storyFn()}</Setup>], | ||
args: { | ||
showHelper: true, | ||
}, | ||
argTypes: { | ||
showHelper: { | ||
type: 'boolean', | ||
}, | ||
}, | ||
} | ||
|
||
type StoryProps = { | ||
showHelper: boolean | ||
} | ||
|
||
const Scene: React.FC<StoryProps> = ({ showHelper }) => { | ||
return ( | ||
<Sphere> | ||
<meshBasicMaterial /> | ||
|
||
{showHelper && ( | ||
<> | ||
<Helper type={BoxHelper} args={['royalblue']} /> | ||
<Helper type={VertexNormalsHelper} args={[1, 0xff0000]} /> | ||
</> | ||
)} | ||
</Sphere> | ||
) | ||
} | ||
|
||
export const DefaultStory = (args: StoryProps) => <Scene {...args} /> | ||
DefaultStory.storyName = 'Default' | ||
|
||
const CameraScene: React.FC<StoryProps> = ({ showHelper }) => { | ||
const camera = React.useRef<THREE.PerspectiveCamera>() | ||
|
||
useFrame(({ clock }) => { | ||
const t = clock.getElapsedTime() | ||
|
||
if (camera.current) { | ||
camera.current.lookAt(0, 0, 0) | ||
|
||
camera.current.position.x = Math.sin(t) * 4 | ||
camera.current.position.z = Math.cos(t) * 4 | ||
} | ||
}) | ||
|
||
return ( | ||
<PerspectiveCamera makeDefault={false} position={[0, 3, 3]} near={1} far={4} ref={camera}> | ||
<meshBasicMaterial /> | ||
|
||
{showHelper && <Helper type={CameraHelper} />} | ||
</PerspectiveCamera> | ||
) | ||
} | ||
|
||
export const CameraStory = (args: StoryProps) => <CameraScene {...args} /> | ||
CameraStory.storyName = 'Camera Helper' |
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,47 @@ | ||
import { useFrame, useThree } from '@react-three/fiber' | ||
import * as React from 'react' | ||
import { Object3D } from 'three' | ||
|
||
type HelperType = Object3D & { update: () => void; dispose: () => void } | ||
type HelperConstructor = new (...args: any[]) => HelperType | ||
type HelperArgs<T> = T extends [infer _, ...infer R] ? R : never | ||
|
||
export type HelperProps<T extends HelperConstructor> = { | ||
type: T | ||
args?: HelperArgs<ConstructorParameters<T>> | ||
} | ||
|
||
export const Helper = <T extends HelperConstructor>({ | ||
type: helperConstructor, | ||
args = [] as never, | ||
}: HelperProps<T>) => { | ||
const objectRef = React.useRef<Object3D>(null!) | ||
const helperRef = React.useRef<HelperType>() | ||
|
||
const scene = useThree((state) => state.scene) | ||
|
||
React.useLayoutEffect(() => { | ||
const parent = objectRef.current?.parent | ||
|
||
if (!helperConstructor || !parent) return | ||
|
||
const helper = new helperConstructor(parent, ...args) | ||
|
||
helperRef.current = helper | ||
|
||
// Prevent the helpers from blocking rays | ||
helper.traverse((child) => (child.raycast = () => null)) | ||
|
||
scene.add(helper) | ||
|
||
return () => { | ||
helperRef.current = undefined | ||
scene.remove(helper) | ||
helper.dispose?.() | ||
} | ||
}, [scene, helperConstructor, ...args]) | ||
|
||
useFrame(() => void helperRef.current?.update?.()) | ||
|
||
return <object3D ref={objectRef} /> | ||
} |
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