-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.tsx
182 lines (166 loc) · 5.05 KB
/
index.tsx
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import * as THREE from "three";
import { useMemo, useRef, useState } from "react";
import { useFrame, useThree, createPortal } from "@react-three/fiber";
import {
useNoise,
useFluid,
useFxBlending,
useColorStrata,
useBrightnessPicker,
useSingleFBO,
} from "@/packages/use-shader-fx/src";
function Box(props: any) {
// This reference will give us direct access to the mesh
const meshRef = useRef<THREE.Mesh>();
// Set up state for the hovered and active state
const [hovered, setHover] = useState(false);
const [active, setActive] = useState(false);
// Subscribe this component to the render-loop, rotate the mesh every frame
useFrame((state, delta) => {
meshRef.current!.rotation.x += delta;
meshRef.current!.rotation.y -= delta;
});
// Return view, these are regular three.js elements expressed in JSX
return (
<mesh
{...props}
ref={meshRef}
scale={active ? 2 : 1.5}
onClick={(event) => setActive(!active)}
onPointerOver={(event) => setHover(true)}
onPointerOut={(event) => setHover(false)}>
<boxGeometry args={[1, 1, 1]} />
<meshStandardMaterial color={hovered ? "hotpink" : "orange"} />
</mesh>
);
}
export const Home = () => {
const ref = useRef<THREE.ShaderMaterial>(null);
const { size, viewport, camera } = useThree();
const dpr = viewport.dpr;
const [updateNoise, setNoise] = useNoise({ size, dpr });
const [updateFluid, setFluid] = useFluid({ size, dpr });
const [updateFxBlending, setFxBlending] = useFxBlending({ size, dpr });
const [updateColorStrata, setColorStrata] = useColorStrata({ size, dpr });
const [updateBrightnessPicker] = useBrightnessPicker({
size,
dpr,
});
setFxBlending({
mapIntensity: 0.45,
});
setNoise({
scale: 0.01,
warpOctaves: 1,
noiseOctaves: 1,
fbmOctaves: 1,
timeStrength: 1.2,
warpStrength: 20.0,
});
setFluid({
density_dissipation: 0.96,
velocity_dissipation: 0.99,
curl_strength: 0.0,
splat_radius: 0.0045,
pressure_iterations: 1,
});
setColorStrata({
laminateLayer: 4,
laminateInterval: new THREE.Vector2(1, 1),
laminateDetail: new THREE.Vector2(0.3, 0.3),
distortion: new THREE.Vector2(2, 2),
colorFactor: new THREE.Vector3(6.2, 4.2, 8.8),
timeStrength: new THREE.Vector2(1, 1),
noiseStrength: new THREE.Vector2(1, 1),
});
// This scene is rendered offscreen
const offscreenScene = useMemo(() => new THREE.Scene(), []);
// create FBO for offscreen rendering
const [_, updateRenderTarget] = useSingleFBO({
scene: offscreenScene,
camera,
size,
dpr: viewport.dpr,
samples: 4,
});
useFrame((props) => {
const noise = updateNoise(props);
const fluid = updateFluid(props);
const blending = updateFxBlending(props, {
texture: fluid,
map: noise,
});
const picked = updateBrightnessPicker(props, {
texture: blending,
});
const colorStrata = updateColorStrata(props, {
texture: picked,
noise: noise,
});
ref.current!.uniforms.u_fx.value = colorStrata;
ref.current!.uniforms.u_texture.value = updateRenderTarget(props.gl);
});
return (
<>
{createPortal(
<mesh>
<ambientLight intensity={Math.PI} />
<spotLight
position={[10, 10, 10]}
angle={0.15}
penumbra={1}
decay={0}
intensity={Math.PI}
/>
<pointLight
position={[-10, -10, -10]}
decay={0}
intensity={Math.PI}
/>
<Box position={[-1.5, 0, 0]} />
<Box position={[1.5, 0, 0]} />
</mesh>,
offscreenScene
)}
<mesh>
<planeGeometry args={[2, 2]} />
<shaderMaterial
ref={ref}
transparent
vertexShader={`
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = vec4(position, 1.0);
}
`}
fragmentShader={`
precision highp float;
varying vec2 vUv;
uniform sampler2D u_fx;
uniform sampler2D u_texture;
void main() {
vec2 uv = vUv;
vec3 noiseMap = texture2D(u_fx, uv).rgb;
vec3 nNoiseMap = noiseMap * 2.0 - 1.0;
uv = uv * 2.0 - 1.0;
uv *= mix(vec2(1.0), abs(nNoiseMap.rg), 1.);
uv = (uv + 1.0) / 2.0;
vec3 texColor = texture2D(u_texture, uv).rgb;
vec3 color = mix(texColor,noiseMap,0.5);
float luminance = length(color);
float edge0 = 0.0;
float edge1 = .2;
float alpha = smoothstep(edge0, edge1, luminance);
gl_FragColor = vec4(color,alpha);
}
`}
uniforms={{
u_texture: { value: null },
u_fx: { value: null },
}}
/>
</mesh>
</>
);
};