-
Notifications
You must be signed in to change notification settings - Fork 631
/
Cloud.tsx
330 lines (305 loc) · 11.4 KB
/
Cloud.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import * as React from 'react'
import {
REVISION,
DynamicDrawUsage,
Color,
Group,
Texture,
Vector3,
InstancedMesh,
Material,
MeshLambertMaterial,
Matrix4,
Quaternion,
BufferAttribute,
} from 'three'
import { MaterialNode, extend, applyProps, useFrame, ReactThreeFiber } from '@react-three/fiber'
import { useTexture } from './Texture'
import { v4 } from 'uuid'
import { setUpdateRange } from '../helpers/deprecated'
declare global {
namespace JSX {
interface IntrinsicElements {
cloudMaterial: MaterialNode<MeshLambertMaterial, typeof MeshLambertMaterial>
}
}
}
const CLOUD_URL = 'https://rawcdn.githack.com/pmndrs/drei-assets/9225a9f1fbd449d9411125c2f419b843d0308c9f/cloud.png'
type CloudState = {
uuid: string
index: number
segments: number
dist: number
matrix: Matrix4
bounds: Vector3
position: Vector3
volume: number
length: number
ref: React.MutableRefObject<Group>
speed: number
growth: number
opacity: number
fade: number
density: number
rotation: number
rotationFactor: number
color: Color
}
type CloudsProps = JSX.IntrinsicElements['group'] & {
/** Optional cloud texture, points to a default hosted on rawcdn.githack */
texture?: string
/** Maximum number of segments, default: 200 (make this tight to save memory!) */
limit?: number
/** How many segments it renders, default: undefined (all) */
range?: number
/** Which material it will override, default: MeshLambertMaterial */
material?: typeof Material
/** Frustum culling, default: true */
frustumCulled?: boolean
}
type CloudProps = JSX.IntrinsicElements['group'] & {
/** A seeded random will show the same cloud consistently, default: Math.random() */
seed?: number
/** How many segments or particles the cloud will have, default: 20 */
segments?: number
/** The box3 bounds of the cloud, default: [5, 1, 1] */
bounds?: ReactThreeFiber.Vector3
/** How to arrange segment volume inside the bounds, default: inside (cloud are smaller at the edges) */
concentrate?: 'random' | 'inside' | 'outside'
/** The general scale of the segments */
scale?: ReactThreeFiber.Vector3
/** The volume/thickness of the segments, default: 6 */
volume?: number
/** The smallest volume when distributing clouds, default: 0.25 */
smallestVolume?: number
/** An optional function that allows you to distribute points and volumes (overriding all settings), default: null
* Both point and volume are factors, point x/y/z can be between -1 and 1, volume between 0 and 1 */
distribute?: (cloud: CloudState, index: number) => { point: Vector3; volume?: number }
/** Growth factor for animated clouds (speed > 0), default: 4 */
growth?: number
/** Animation factor, default: 0 */
speed?: number
/** Camera distance until the segments will fade, default: 10 */
fade?: number
/** Opacity, default: 1 */
opacity?: number
/** Color, default: white */
color?: ReactThreeFiber.Color
}
const parentMatrix = /* @__PURE__ */ new Matrix4()
const translation = /* @__PURE__ */ new Vector3()
const rotation = /* @__PURE__ */ new Quaternion()
const cpos = /* @__PURE__ */ new Vector3()
const cquat = /* @__PURE__ */ new Quaternion()
const scale = /* @__PURE__ */ new Vector3()
const context = /* @__PURE__ */ React.createContext<React.MutableRefObject<CloudState[]>>(null!)
export const Clouds = /* @__PURE__ */ React.forwardRef<Group, CloudsProps>(
(
{ children, material = MeshLambertMaterial, texture = CLOUD_URL, range, limit = 200, frustumCulled, ...props },
fref
) => {
const CloudMaterial = React.useMemo(() => {
return class extends (material as typeof Material) {
constructor() {
super()
const opaque_fragment = parseInt(REVISION.replace(/\D+/g, '')) >= 154 ? 'opaque_fragment' : 'output_fragment'
this.onBeforeCompile = (shader) => {
shader.vertexShader =
`attribute float opacity;
varying float vOpacity;
` +
shader.vertexShader.replace(
'#include <fog_vertex>',
`#include <fog_vertex>
vOpacity = opacity;
`
)
shader.fragmentShader =
`varying float vOpacity;
` +
shader.fragmentShader.replace(
`#include <${opaque_fragment}>`,
`#include <${opaque_fragment}>
gl_FragColor = vec4(outgoingLight, diffuseColor.a * vOpacity);
`
)
}
}
}
}, [material])
extend({ CloudMaterial })
const instance = React.useRef<InstancedMesh>(null!)
const clouds = React.useRef<CloudState[]>([])
const opacities = React.useMemo(() => new Float32Array(Array.from({ length: limit }, () => 1)), [limit])
const colors = React.useMemo(() => new Float32Array(Array.from({ length: limit }, () => [1, 1, 1]).flat()), [limit])
const cloudTexture = useTexture(texture) as Texture
let t = 0
let index = 0
let config: CloudState
const qat = new Quaternion()
const dir = new Vector3(0, 0, 1)
const pos = new Vector3()
useFrame((state, delta) => {
t = state.clock.getElapsedTime()
parentMatrix.copy(instance.current.matrixWorld).invert()
state.camera.matrixWorld.decompose(cpos, cquat, scale)
for (index = 0; index < clouds.current.length; index++) {
config = clouds.current[index]
config.ref.current.matrixWorld.decompose(translation, rotation, scale)
translation.add(pos.copy(config.position).applyQuaternion(rotation).multiply(scale))
rotation.copy(cquat).multiply(qat.setFromAxisAngle(dir, (config.rotation += delta * config.rotationFactor)))
scale.multiplyScalar(config.volume + ((1 + Math.sin(t * config.density * config.speed)) / 2) * config.growth)
config.matrix.compose(translation, rotation, scale).premultiply(parentMatrix)
config.dist = translation.distanceTo(cpos)
}
// Depth-sort. Instances have no specific draw order, w/o sorting z would be random
clouds.current.sort((a, b) => b.dist - a.dist)
for (index = 0; index < clouds.current.length; index++) {
config = clouds.current[index]
opacities[index] = config.opacity * (config.dist < config.fade - 1 ? config.dist / config.fade : 1)
instance.current.setMatrixAt(index, config.matrix)
instance.current.setColorAt(index, config.color)
}
// Update instance
instance.current.geometry.attributes.opacity.needsUpdate = true
instance.current.instanceMatrix.needsUpdate = true
if (instance.current.instanceColor) instance.current.instanceColor.needsUpdate = true
})
React.useLayoutEffect(() => {
const count = Math.min(limit, range !== undefined ? range : limit, clouds.current.length)
instance.current.count = count
setUpdateRange(instance.current.instanceMatrix, { offset: 0, count: count * 16 })
if (instance.current.instanceColor) {
setUpdateRange(instance.current.instanceColor, { offset: 0, count: count * 3 })
}
setUpdateRange(instance.current.geometry.attributes.opacity as BufferAttribute, { offset: 0, count: count })
})
let imageBounds = [cloudTexture!.image.width ?? 1, cloudTexture!.image.height ?? 1]
let max = Math.max(imageBounds[0], imageBounds[1])
imageBounds = [imageBounds[0] / max, imageBounds[1] / max]
return (
<group ref={fref} {...props}>
<context.Provider value={clouds}>
{children}
<instancedMesh
matrixAutoUpdate={false}
ref={instance}
args={[null as any, null as any, limit]}
frustumCulled={frustumCulled}
>
<instancedBufferAttribute usage={DynamicDrawUsage} attach="instanceColor" args={[colors, 3]} />
<planeGeometry args={[...imageBounds] as any}>
<instancedBufferAttribute usage={DynamicDrawUsage} attach="attributes-opacity" args={[opacities, 1]} />
</planeGeometry>
<cloudMaterial key={material.name} map={cloudTexture} transparent depthWrite={false} />
</instancedMesh>
</context.Provider>
</group>
)
}
)
export const CloudInstance = /* @__PURE__ */ React.forwardRef<Group, CloudProps>(
(
{
opacity = 1,
speed = 0,
bounds = [5, 1, 1],
segments = 20,
color = '#ffffff',
fade = 10,
volume = 6,
smallestVolume = 0.25,
distribute = null,
growth = 4,
concentrate = 'inside',
seed = Math.random(),
...props
},
fref
) => {
function random() {
const x = Math.sin(seed++) * 10000
return x - Math.floor(x)
}
const parent = React.useContext(context)
const ref = React.useRef<Group>(null!)
const [uuid] = React.useState(() => v4())
const clouds: CloudState[] = React.useMemo(() => {
return [...new Array(segments)].map(
(_, index) =>
({
segments,
bounds: new Vector3(1, 1, 1),
position: new Vector3(),
uuid,
index,
ref,
dist: 0,
matrix: new Matrix4(),
color: new Color(),
rotation: index * (Math.PI / segments),
} as CloudState)
)
}, [segments, uuid])
React.useLayoutEffect(() => {
clouds.forEach((cloud, index) => {
applyProps(cloud as any, {
volume,
color,
speed,
growth,
opacity,
fade,
bounds,
density: Math.max(0.5, random()),
rotationFactor: Math.max(0.2, 0.5 * random()) * speed,
})
// Only distribute randomly if there are multiple segments
const distributed = distribute?.(cloud, index)
if (distributed || segments > 1)
cloud.position.copy(cloud.bounds).multiply(
distributed?.point ??
({
x: random() * 2 - 1,
y: random() * 2 - 1,
z: random() * 2 - 1,
} as Vector3)
)
const xDiff = Math.abs(cloud.position.x)
const yDiff = Math.abs(cloud.position.y)
const zDiff = Math.abs(cloud.position.z)
const max = Math.max(xDiff, yDiff, zDiff)
cloud.length = 1
if (xDiff === max) cloud.length -= xDiff / cloud.bounds.x
if (yDiff === max) cloud.length -= yDiff / cloud.bounds.y
if (zDiff === max) cloud.length -= zDiff / cloud.bounds.z
cloud.volume =
(distributed?.volume !== undefined
? distributed.volume
: Math.max(
Math.max(0, smallestVolume),
concentrate === 'random' ? random() : concentrate === 'inside' ? cloud.length : 1 - cloud.length
)) * volume
})
}, [concentrate, bounds, fade, color, opacity, growth, volume, seed, segments, speed])
React.useLayoutEffect(() => {
const temp = clouds
parent.current = [...parent.current, ...temp]
return () => {
parent.current = parent.current.filter((item) => item.uuid !== uuid)
}
}, [clouds])
React.useImperativeHandle(fref, () => ref.current, [])
return <group ref={ref} {...props} />
}
)
export const Cloud = /* @__PURE__ */ React.forwardRef<Group, CloudProps>((props, fref) => {
const parent = React.useContext(context)
if (parent) return <CloudInstance ref={fref} {...props} />
else
return (
<Clouds>
<CloudInstance ref={fref} {...props} />
</Clouds>
)
})