-
Notifications
You must be signed in to change notification settings - Fork 720
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
126 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,67 @@ | ||
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(1, 2, 4)} cameraFov={60}> | ||
<Story /> | ||
</Setup> | ||
), | ||
], | ||
} satisfies Meta<typeof Raycaster> | ||
|
||
type Story = StoryObj<typeof Raycaster> | ||
|
||
function RaycasterScene({ origin, direction, ...props }: React.ComponentProps<typeof Raycaster>) { | ||
return ( | ||
<> | ||
<color attach="background" args={['#303030']} /> | ||
|
||
<Raycaster origin={new Vector3(-4, 0, 0)} direction={new Vector3(1, 0, 0)} {...props} helper /> | ||
|
||
<El position-x={-2} /> | ||
<El /> | ||
<El position-x={2} /> | ||
</> | ||
) | ||
} | ||
|
||
export const RaycasterSt = { | ||
render: (args) => <RaycasterScene {...args} />, | ||
|
||
name: 'Default', | ||
} satisfies Story | ||
|
||
const El = ({ | ||
// layers, | ||
...props | ||
}: ComponentProps<'mesh'>) => { | ||
const $mesh = useRef<THREE.Mesh>(null) | ||
|
||
useFrame(({ clock }) => { | ||
if (!$mesh.current) return | ||
$mesh.current.position.y = Math.sin(clock.getElapsedTime() * 0.5 + $mesh.current.position.x) | ||
$mesh.current.rotation.z = Math.sin(clock.getElapsedTime() * 0.5) * Math.PI * 1 | ||
}) | ||
|
||
return ( | ||
<mesh ref={$mesh} {...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,32 @@ | ||
import * as THREE from 'three' | ||
import * as React from 'react' | ||
import { ComponentProps, forwardRef, useRef, useState } from 'react' | ||
import { useFrame } from '@react-three/fiber' | ||
import { RaycasterHelper } from '@gsimone/three-raycaster-helper' | ||
|
||
import { useHelper } from '..' | ||
|
||
type RaycasterProps = Omit<ComponentProps<'raycaster'>, 'args'> & { | ||
origin: NonNullable<ComponentProps<'raycaster'>['args']>[0] | ||
direction: NonNullable<ComponentProps<'raycaster'>['args']>[1] | ||
} & { | ||
helper?: boolean | ||
} | ||
|
||
export const Raycaster = forwardRef<THREE.Raycaster, RaycasterProps>( | ||
({ origin, direction, helper, ...props }, fref) => { | ||
const [r] = useState(() => new THREE.Raycaster(origin, direction)) | ||
|
||
const raycasterRef = useRef<THREE.Raycaster>(null) | ||
|
||
const raycasterHelper = useHelper(!!helper && raycasterRef, RaycasterHelper) | ||
useFrame(({ scene }) => { | ||
if (!raycasterHelper.current || !raycasterRef.current) return | ||
// @ts-ignore | ||
raycasterHelper.current.hits = raycasterRef.current.intersectObjects(scene.children) | ||
}) | ||
|
||
// return <raycaster ref={raycasterRef} args={[origin, direction]} {...props} /> | ||
return <primitive object={r} near={props.near} far={props.far} ref={raycasterRef}></primitive> | ||
} | ||
) |
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