-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathExample.tsx
More file actions
77 lines (61 loc) · 1.76 KB
/
Example.tsx
File metadata and controls
77 lines (61 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import * as THREE from 'three'
import { useRef } from 'react'
import { useFrame, useThree } from '@react-three/fiber'
import { BlendFunction, Effect } from 'postprocessing'
import { wrapEffect } from '../util'
//
// Effect
//
const ExampleShader = {
fragment: `
uniform vec3 color;
uniform float time;
void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor) {
outputColor = vec4(color, 0.5 + (cos(time) / 2.0 + 0.5));
}
`,
}
type ExampleEffectOptions = {
/** The color for this effect */
color: THREE.Color
/** The blend function of this effect */
blendFunction?: BlendFunction
}
export class ExampleEffect extends Effect {
constructor({ color, blendFunction }: ExampleEffectOptions) {
super('LensFlareEffect', ExampleShader.fragment, {
blendFunction,
uniforms: new Map<string, THREE.Uniform>([
['color', new THREE.Uniform(color)],
['time', new THREE.Uniform(0)],
]),
})
}
update(_renderer: any, _inputBuffer: any, deltaTime: number) {
const time = this.uniforms.get('time')
if (time) {
time.value += deltaTime
}
}
}
//
// Component
//
const ExampleWrapped = wrapEffect(ExampleEffect)
type ExampleProps = React.ComponentPropsWithoutRef<typeof ExampleWrapped> & {
/** mouse */
mouse?: boolean
}
export const Example = ({ mouse = false, ...props }: ExampleProps) => {
const pointer = useThree(({ pointer }) => pointer)
const ref = useRef<ExampleEffect>(null)
useFrame(() => {
if (!mouse) return
if (!ref?.current) return
const uColor = ref.current.uniforms.get('color')
if (!uColor) return
uColor.value.r = pointer.x / 2.0 + 0.5
uColor.value.g = pointer.y / 2.0 + 0.5
})
return <ExampleWrapped ref={ref} {...props} />
}