-
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
6 changed files
with
158 additions
and
15 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,85 @@ | ||
import * as THREE from 'three' | ||
import * as React from 'react' | ||
|
||
import { Vector3 } from 'three' | ||
import { Meta, StoryObj } from '@storybook/react' | ||
|
||
import { Setup } from '../Setup' | ||
|
||
import { Raycaster } from '../../src' | ||
import { ComponentProps, useRef } from 'react' | ||
import { useFrame } from '@react-three/fiber' | ||
|
||
export default { | ||
title: 'Abstractions/Raycaster', | ||
component: Raycaster, | ||
decorators: [ | ||
(Story) => ( | ||
<Setup cameraPosition={new Vector3(0, 0, 10)}> | ||
<Story /> | ||
</Setup> | ||
), | ||
], | ||
argTypes: { | ||
near: { control: { type: 'range', min: 0, max: 15 } }, | ||
far: { control: { type: 'range', min: 0, max: 15 } }, | ||
helper: { control: { type: 'boolean' } }, | ||
}, | ||
} satisfies Meta<typeof Raycaster> | ||
|
||
type Story = StoryObj<typeof Raycaster> | ||
|
||
function RaycasterScene(props: React.ComponentProps<typeof Raycaster>) { | ||
const raycasterRef = useRef<THREE.Raycaster>(null) | ||
|
||
React.useEffect(() => { | ||
console.log('raycasterRef', raycasterRef) | ||
}) | ||
|
||
return ( | ||
<> | ||
<color attach="background" args={['#303030']} /> | ||
|
||
<Raycaster ref={raycasterRef} {...props} /> | ||
|
||
<Capsule position-x={-2} /> | ||
<Capsule /> | ||
<Capsule position-x={2} /> | ||
</> | ||
) | ||
} | ||
|
||
export const RaycasterSt = { | ||
render: (args) => <RaycasterScene {...args} />, | ||
args: { | ||
origin: [-4, 0, 0], | ||
direction: [1, 0, 0], | ||
near: 1, | ||
far: 8, | ||
helper: true, | ||
}, | ||
|
||
name: 'Default', | ||
} satisfies Story | ||
|
||
const Capsule = ({ | ||
// layers, | ||
...props | ||
}: ComponentProps<'mesh'>) => { | ||
const meshRef = useRef<THREE.Mesh>(null) | ||
|
||
useFrame(({ clock }) => { | ||
if (!meshRef.current) return | ||
meshRef.current.position.y = Math.sin(clock.getElapsedTime() * 0.5 + meshRef.current.position.x) | ||
meshRef.current.rotation.z = Math.sin(clock.getElapsedTime() * 0.5) * Math.PI * 1 | ||
}) | ||
|
||
return ( | ||
<mesh ref={meshRef} {...props}> | ||
{/* <Layers layers={layers} /> */} | ||
|
||
<capsuleGeometry args={[0.5, 0.5, 4, 32]} /> | ||
<meshNormalMaterial side={THREE.DoubleSide} /> | ||
</mesh> | ||
) | ||
} |
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
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 { ComponentProps, forwardRef, useRef, useState } from 'react' | ||
import { useFrame, type Vector3 } from '@react-three/fiber' | ||
import { RaycasterHelper } from '@gsimone/three-raycaster-helper' | ||
|
||
import { useHelper } from '..' | ||
|
||
type RaycasterProps = Omit<ComponentProps<'raycaster'>, 'args'> & { | ||
origin: Vector3 | ||
direction: Vector3 | ||
} & { | ||
helper?: boolean | ||
} | ||
|
||
function toThreeVec3(v: Vector3) { | ||
return v instanceof THREE.Vector3 ? v : new THREE.Vector3(...(typeof v === 'number' ? [v, v, v] : v)) | ||
} | ||
|
||
export const Raycaster = forwardRef<THREE.Raycaster, RaycasterProps>( | ||
({ origin: _origin, direction: _direction, near, far, helper, ...props }, fref) => { | ||
const origin = toThreeVec3(_origin) | ||
const direction = toThreeVec3(_direction) | ||
|
||
const [r] = useState(() => new THREE.Raycaster(origin, direction)) | ||
|
||
const raycasterRef = useRef<THREE.Raycaster>(null) | ||
const ref = fref || raycasterRef | ||
const isCallbackRef = typeof ref === 'function' | ||
|
||
const raycasterHelper = useHelper(helper && !isCallbackRef && ref, RaycasterHelper) | ||
|
||
// Update the hits with intersection results | ||
useFrame(({ scene }) => { | ||
if (!helper) return | ||
if (!ref || isCallbackRef) return | ||
|
||
if (!raycasterHelper.current || !ref.current) return | ||
// @ts-ignore | ||
raycasterHelper.current.hits = ref.current.intersectObjects(scene.children) | ||
}) | ||
|
||
return <primitive ref={ref} object={r} {...{ origin, direction, near, far }} {...props} /> | ||
} | ||
) |
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