Skip to content

Commit

Permalink
fix: change usetrailtexture to array notation
Browse files Browse the repository at this point in the history
  • Loading branch information
drcmda committed Nov 22, 2022
1 parent a71356f commit f0184fd
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .storybook/stories/useTrailTexture.stories.tsx
Expand Up @@ -13,7 +13,7 @@ export default {

function TrailMesh() {
// a convenience hook that uses useLoader and TextureLoader
const { texture, onMove } = useTrailTexture({
const [texture, onMove] = useTrailTexture({
size: number('Size', 256, { min: 64, step: 8 }),
radius: number('Radius', 0.3, { range: true, min: 0.1, max: 1, step: 0.1 }),
maxAge: number('Max age', 750, { range: true, min: 300, max: 1000, step: 100 }),
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -1982,7 +1982,7 @@ return (
<a href="https://codesandbox.io/s/fj1qlg"><img width="20%" src="https://codesandbox.io/api/v1/sandboxes/fj1qlg/screenshot.png" alt="Demo"/></a>
</p>

This hook returns a `THREE.Texture` with a pointer trail which can be used in shaders to control displacement among other things.
This hook returns a `THREE.Texture` with a pointer trail which can be used in shaders to control displacement among other things, and a movement callback `event => void` which reads from `event.uv`.

```tsx
type TrailConfig = {
Expand All @@ -2008,7 +2008,7 @@ type TrailConfig = {
```

```jsx
const { texture, onMove } = useTrailTexture(config)
const [texture, onMove] = useTrailTexture(config)
return (
<mesh onPointerMove={onMove}>
<meshStandardMaterial displacementMap={texture} />
Expand Down
6 changes: 3 additions & 3 deletions src/core/useTrailTexture.tsx
@@ -1,5 +1,5 @@
import { useMemo, useCallback } from 'react'
import { useFrame } from '@react-three/fiber'
import { ThreeEvent, useFrame } from '@react-three/fiber'
import { Texture } from 'three'

/**
Expand Down Expand Up @@ -196,13 +196,13 @@ class TrailTexture {
}
}

export function useTrailTexture(config: Partial<TrailConfig> = {}) {
export function useTrailTexture(config: Partial<TrailConfig> = {}): [Texture, (ThreeEvent) => void] {
const { size, maxAge, radius, intensity, interpolate, smoothing, minForce, blend, ease } = config
const trail = useMemo(
() => new TrailTexture(config),
[size, maxAge, radius, intensity, interpolate, smoothing, minForce, blend, ease]
)
useFrame((_, delta) => void trail.update(delta))
const onMove = useCallback((e) => trail.addTouch(e.uv), [trail])
return { texture: trail.texture, onMove }
return [trail.texture, onMove]
}

2 comments on commit f0184fd

@vercel
Copy link

@vercel vercel bot commented on f0184fd Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ffdead
Copy link
Contributor

@ffdead ffdead commented on f0184fd Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff 👍

Please sign in to comment.