Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update CurveModifier ref api #2236

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions docs/modifiers/curve-modifier.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@ sourcecode: src/core/CurveModifier.tsx

[![](https://img.shields.io/badge/-storybook-%23ff69b4)](https://drei.pmnd.rs/?path=/story/modifiers-curvemodifier)

Given a curve will replace the children of this component with a mesh that move along said curve calling the property `moveAlongCurve` on the passed ref. Uses [three's Curve Modifier](https://threejs.org/examples/#webgl_modifier_curve)
Given a curve will replace the children of this component with a mesh that move along said curve calling the property `moveAlongCurve` or modifying the `uniforms.pathOffset` value on the passed ref. Uses [three's Curve Modifier](https://threejs.org/examples/#webgl_modifier_curve)

```jsx
const curveRef = useRef()
```tsx
const curveRef = useRef<CurveModifierRef>()
const scroll = useScroll()

const curve = React.useMemo(() => new THREE.CatmullRomCurve3([...handlePos], true, 'centripetal'), [handlePos])

useFrame(() => {
if (curveRef.current) {
// Move continuously along the curve
curveRef.current.moveAlongCurve(0.001)

// Move along the curve using the scrollbar
curveRef.current.uniforms.pathOffset.value = scroll.offset
}
})

return (
<CurveModifier ref={curveRef} curve={curve}>
<mesh>
Expand All @@ -20,3 +31,26 @@ return (
</CurveModifier>
)
```

## Reference api

```tsx
type CurveModifierRef = {
curveArray: Curve<any>[];
curveLengthArray: number[];
object3D: TMesh;
splineTexure: DataTexture;
uniforms: CurveModifierUniforms;
updateCurve<TCurve extends Curve<any>>(index: number, curve: TCurve): void;
moveAlongCurve(amount: number): void;
}

type CurveModifierUniforms = {
spineTexture: IUniform<DataTexture>;
pathOffset: INumericUniform;
pathSegment: INumericUniform;
spineOffset: INumericUniform;
spineLength: INumericUniform;
flow: INumericUniform;
}
```
12 changes: 4 additions & 8 deletions src/core/CurveModifier.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ export interface CurveModifierProps {
curve?: THREE.Curve<THREE.Vector3>
}

export type CurveModifierRef = Pick<Flow, 'moveAlongCurve'>
export type CurveModifierRef = Flow

export const CurveModifier: ForwardRefComponent<CurveModifierProps, CurveModifierRef> =
/* @__PURE__ */ React.forwardRef(({ children, curve }: CurveModifierProps, ref) => {
const [scene] = React.useState(() => new THREE.Scene())
const [obj, set] = React.useState<THREE.Object3D>()
const modifier = React.useRef<Flow>()
const modifier = React.useRef<Flow>(null!)

React.useEffect(() => {
React.useLayoutEffect(() => {
modifier.current = new Flow(
scene.children[0] as THREE.Mesh<THREE.BufferGeometry, THREE.Material | THREE.Material[]>
)
Expand All @@ -28,11 +28,7 @@ export const CurveModifier: ForwardRefComponent<CurveModifierProps, CurveModifie
if (curve) modifier.current?.updateCurve(0, curve)
}, [curve])

React.useImperativeHandle(ref, () => ({
moveAlongCurve: (val: number) => {
modifier.current?.moveAlongCurve(val)
},
}))
React.useImperativeHandle(ref, () => modifier.current)

return (
<>
Expand Down
Loading