r3f-vfx 0.0.9 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +68 -68
- package/dist/curveWorker.d.ts +2 -0
- package/dist/curveWorker.js +105 -0
- package/dist/index.d.ts +20 -1
- package/dist/index.js +1793 -1329
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -27,16 +27,16 @@ npm install three @react-three/fiber react
|
|
|
27
27
|
## Quick Start
|
|
28
28
|
|
|
29
29
|
```tsx
|
|
30
|
-
import { Canvas } from '@react-three/fiber'
|
|
31
|
-
import { VFXParticles, Appearance, EmitterShape } from 'r3f-vfx'
|
|
32
|
-
import * as THREE from 'three/webgpu'
|
|
30
|
+
import { Canvas } from '@react-three/fiber'
|
|
31
|
+
import { VFXParticles, Appearance, EmitterShape } from 'r3f-vfx'
|
|
32
|
+
import * as THREE from 'three/webgpu'
|
|
33
33
|
|
|
34
34
|
function App() {
|
|
35
35
|
return (
|
|
36
36
|
<Canvas>
|
|
37
37
|
<VFXParticles debug />
|
|
38
38
|
</Canvas>
|
|
39
|
-
)
|
|
39
|
+
)
|
|
40
40
|
}
|
|
41
41
|
```
|
|
42
42
|
|
|
@@ -114,8 +114,8 @@ The main particle system component.
|
|
|
114
114
|
|
|
115
115
|
```ts
|
|
116
116
|
interface StretchConfig {
|
|
117
|
-
factor: number
|
|
118
|
-
maxStretch: number
|
|
117
|
+
factor: number // Stretch multiplier
|
|
118
|
+
maxStretch: number // Maximum stretch amount
|
|
119
119
|
}
|
|
120
120
|
```
|
|
121
121
|
|
|
@@ -127,9 +127,9 @@ interface StretchConfig {
|
|
|
127
127
|
|
|
128
128
|
```ts
|
|
129
129
|
interface TurbulenceConfig {
|
|
130
|
-
intensity: number
|
|
131
|
-
frequency: number
|
|
132
|
-
speed: number
|
|
130
|
+
intensity: number // Turbulence strength
|
|
131
|
+
frequency: number // Noise scale
|
|
132
|
+
speed: number // Animation speed
|
|
133
133
|
}
|
|
134
134
|
```
|
|
135
135
|
|
|
@@ -142,11 +142,11 @@ interface TurbulenceConfig {
|
|
|
142
142
|
|
|
143
143
|
```ts
|
|
144
144
|
interface AttractorConfig {
|
|
145
|
-
position: [x, y, z]
|
|
146
|
-
strength: number
|
|
147
|
-
radius?: number
|
|
148
|
-
type?: 'point' | 'vortex'
|
|
149
|
-
axis?: [x, y, z]
|
|
145
|
+
position: [x, y, z]
|
|
146
|
+
strength: number // Positive = attract, negative = repel
|
|
147
|
+
radius?: number // 0 = infinite range
|
|
148
|
+
type?: 'point' | 'vortex'
|
|
149
|
+
axis?: [x, y, z] // Vortex rotation axis
|
|
150
150
|
}
|
|
151
151
|
```
|
|
152
152
|
|
|
@@ -158,11 +158,11 @@ interface AttractorConfig {
|
|
|
158
158
|
|
|
159
159
|
```ts
|
|
160
160
|
interface CollisionConfig {
|
|
161
|
-
plane: { y: number }
|
|
162
|
-
bounce?: number
|
|
163
|
-
friction?: number
|
|
164
|
-
die?: boolean
|
|
165
|
-
sizeBasedGravity?: number
|
|
161
|
+
plane: { y: number } // Plane Y position
|
|
162
|
+
bounce?: number // Bounce factor (0-1)
|
|
163
|
+
friction?: number // Horizontal friction
|
|
164
|
+
die?: boolean // Kill on collision
|
|
165
|
+
sizeBasedGravity?: number // Gravity multiplier by size
|
|
166
166
|
}
|
|
167
167
|
```
|
|
168
168
|
|
|
@@ -180,10 +180,10 @@ All curves use Bezier spline format:
|
|
|
180
180
|
```ts
|
|
181
181
|
interface CurveData {
|
|
182
182
|
points: Array<{
|
|
183
|
-
pos: [x, y]
|
|
184
|
-
handleIn?: [x, y]
|
|
185
|
-
handleOut?: [x, y]
|
|
186
|
-
}
|
|
183
|
+
pos: [x, y] // Position (x: 0-1 progress, y: value)
|
|
184
|
+
handleIn?: [x, y] // Bezier handle in (offset)
|
|
185
|
+
handleOut?: [x, y] // Bezier handle out (offset)
|
|
186
|
+
}>
|
|
187
187
|
}
|
|
188
188
|
```
|
|
189
189
|
|
|
@@ -205,21 +205,21 @@ interface CurveData {
|
|
|
205
205
|
| `alphaTestNode` | `NodeFunction` | Alpha test/discard |
|
|
206
206
|
|
|
207
207
|
```ts
|
|
208
|
-
type NodeFunction = (data: ParticleData, defaultColor?: Node) => Node
|
|
208
|
+
type NodeFunction = (data: ParticleData, defaultColor?: Node) => Node
|
|
209
209
|
|
|
210
210
|
interface ParticleData {
|
|
211
|
-
progress: Node
|
|
212
|
-
lifetime: Node
|
|
213
|
-
position: Node
|
|
214
|
-
velocity: Node
|
|
215
|
-
size: Node
|
|
216
|
-
rotation: Node
|
|
217
|
-
colorStart: Node
|
|
218
|
-
colorEnd: Node
|
|
219
|
-
color: Node
|
|
220
|
-
intensifiedColor: Node
|
|
221
|
-
shapeMask: Node
|
|
222
|
-
index: Node
|
|
211
|
+
progress: Node // 0 → 1 over lifetime
|
|
212
|
+
lifetime: Node // 1 → 0 over lifetime
|
|
213
|
+
position: Node // vec3 world position
|
|
214
|
+
velocity: Node // vec3 velocity
|
|
215
|
+
size: Node // float size
|
|
216
|
+
rotation: Node // vec3 rotation
|
|
217
|
+
colorStart: Node // vec3 start color
|
|
218
|
+
colorEnd: Node // vec3 end color
|
|
219
|
+
color: Node // vec3 interpolated color
|
|
220
|
+
intensifiedColor: Node // color × intensity
|
|
221
|
+
shapeMask: Node // float alpha mask
|
|
222
|
+
index: Node // particle index
|
|
223
223
|
}
|
|
224
224
|
```
|
|
225
225
|
|
|
@@ -232,8 +232,8 @@ interface ParticleData {
|
|
|
232
232
|
|
|
233
233
|
```ts
|
|
234
234
|
interface FlipbookConfig {
|
|
235
|
-
rows: number
|
|
236
|
-
columns: number
|
|
235
|
+
rows: number
|
|
236
|
+
columns: number
|
|
237
237
|
}
|
|
238
238
|
```
|
|
239
239
|
|
|
@@ -276,13 +276,13 @@ Decoupled emitter component that links to a VFXParticles system.
|
|
|
276
276
|
|
|
277
277
|
```ts
|
|
278
278
|
interface VFXEmitterAPI {
|
|
279
|
-
emit(): boolean
|
|
280
|
-
burst(count?: number): boolean
|
|
281
|
-
start(): void
|
|
282
|
-
stop(): void
|
|
283
|
-
isEmitting: boolean
|
|
284
|
-
getParticleSystem(): ParticleAPI
|
|
285
|
-
group: THREE.Group
|
|
279
|
+
emit(): boolean // Emit at current position
|
|
280
|
+
burst(count?: number): boolean // Burst emit
|
|
281
|
+
start(): void // Start auto-emission
|
|
282
|
+
stop(): void // Stop auto-emission
|
|
283
|
+
isEmitting: boolean // Current state
|
|
284
|
+
getParticleSystem(): ParticleAPI
|
|
285
|
+
group: THREE.Group // The group element
|
|
286
286
|
}
|
|
287
287
|
```
|
|
288
288
|
|
|
@@ -292,13 +292,13 @@ Programmatic emitter control.
|
|
|
292
292
|
|
|
293
293
|
```tsx
|
|
294
294
|
function MyComponent() {
|
|
295
|
-
const { emit, burst, start, stop } = useVFXEmitter('sparks')
|
|
295
|
+
const { emit, burst, start, stop } = useVFXEmitter('sparks')
|
|
296
296
|
|
|
297
297
|
const handleClick = () => {
|
|
298
|
-
burst([0, 1, 0], 100, { colorStart: ['#ff0000'] })
|
|
299
|
-
}
|
|
298
|
+
burst([0, 1, 0], 100, { colorStart: ['#ff0000'] })
|
|
299
|
+
}
|
|
300
300
|
|
|
301
|
-
return <mesh onClick={handleClick}>...</mesh
|
|
301
|
+
return <mesh onClick={handleClick}>...</mesh>
|
|
302
302
|
}
|
|
303
303
|
```
|
|
304
304
|
|
|
@@ -310,18 +310,18 @@ interface UseVFXEmitterResult {
|
|
|
310
310
|
position?: [x, y, z],
|
|
311
311
|
count?: number,
|
|
312
312
|
overrides?: SpawnOverrides
|
|
313
|
-
): boolean
|
|
313
|
+
): boolean
|
|
314
314
|
burst(
|
|
315
315
|
position?: [x, y, z],
|
|
316
316
|
count?: number,
|
|
317
317
|
overrides?: SpawnOverrides
|
|
318
|
-
): boolean
|
|
319
|
-
start(): boolean
|
|
320
|
-
stop(): boolean
|
|
321
|
-
clear(): boolean
|
|
322
|
-
isEmitting(): boolean
|
|
323
|
-
getUniforms(): Record<string, { value: unknown }
|
|
324
|
-
getParticles(): ParticleAPI
|
|
318
|
+
): boolean
|
|
319
|
+
start(): boolean
|
|
320
|
+
stop(): boolean
|
|
321
|
+
clear(): boolean
|
|
322
|
+
isEmitting(): boolean
|
|
323
|
+
getUniforms(): Record<string, { value: unknown }>
|
|
324
|
+
getParticles(): ParticleAPI
|
|
325
325
|
}
|
|
326
326
|
```
|
|
327
327
|
|
|
@@ -330,17 +330,17 @@ interface UseVFXEmitterResult {
|
|
|
330
330
|
Zustand store for managing particle systems.
|
|
331
331
|
|
|
332
332
|
```ts
|
|
333
|
-
const store = useVFXStore()
|
|
333
|
+
const store = useVFXStore()
|
|
334
334
|
|
|
335
335
|
// Access registered particle systems
|
|
336
|
-
const sparks = store.getParticles('sparks')
|
|
337
|
-
sparks?.spawn(0, 0, 0, 50)
|
|
336
|
+
const sparks = store.getParticles('sparks')
|
|
337
|
+
sparks?.spawn(0, 0, 0, 50)
|
|
338
338
|
|
|
339
339
|
// Store methods
|
|
340
|
-
store.emit('sparks', { x: 0, y: 0, z: 0, count: 20 })
|
|
341
|
-
store.start('sparks')
|
|
342
|
-
store.stop('sparks')
|
|
343
|
-
store.clear('sparks')
|
|
340
|
+
store.emit('sparks', { x: 0, y: 0, z: 0, count: 20 })
|
|
341
|
+
store.start('sparks')
|
|
342
|
+
store.stop('sparks')
|
|
343
|
+
store.clear('sparks')
|
|
344
344
|
```
|
|
345
345
|
|
|
346
346
|
## Examples
|
|
@@ -388,9 +388,9 @@ store.clear('sparks');
|
|
|
388
388
|
### 3D Geometry Particles
|
|
389
389
|
|
|
390
390
|
```tsx
|
|
391
|
-
import { BoxGeometry } from 'three/webgpu'
|
|
391
|
+
import { BoxGeometry } from 'three/webgpu'
|
|
392
392
|
|
|
393
|
-
|
|
393
|
+
;<VFXParticles
|
|
394
394
|
geometry={new BoxGeometry(1, 1, 1)}
|
|
395
395
|
maxParticles={500}
|
|
396
396
|
size={[0.1, 0.2]}
|
|
@@ -404,7 +404,7 @@ import { BoxGeometry } from 'three/webgpu';
|
|
|
404
404
|
]}
|
|
405
405
|
shadow={true}
|
|
406
406
|
lighting={Lighting.STANDARD}
|
|
407
|
-
|
|
407
|
+
/>
|
|
408
408
|
```
|
|
409
409
|
|
|
410
410
|
### Turbulent Smoke
|
|
@@ -464,7 +464,7 @@ import type {
|
|
|
464
464
|
TurbulenceConfig,
|
|
465
465
|
CollisionConfig,
|
|
466
466
|
AttractorConfig,
|
|
467
|
-
} from 'r3f-vfx'
|
|
467
|
+
} from 'r3f-vfx'
|
|
468
468
|
```
|
|
469
469
|
|
|
470
470
|
## License
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// src/curveWorker.js
|
|
2
|
+
var CURVE_RESOLUTION = 256;
|
|
3
|
+
var evaluateBezierSegment = (t, p0, p1, h0Out, h1In) => {
|
|
4
|
+
const cp0 = p0;
|
|
5
|
+
const cp1 = [p0[0] + ((h0Out == null ? void 0 : h0Out[0]) || 0), p0[1] + ((h0Out == null ? void 0 : h0Out[1]) || 0)];
|
|
6
|
+
const cp2 = [p1[0] + ((h1In == null ? void 0 : h1In[0]) || 0), p1[1] + ((h1In == null ? void 0 : h1In[1]) || 0)];
|
|
7
|
+
const cp3 = p1;
|
|
8
|
+
const mt = 1 - t;
|
|
9
|
+
const mt2 = mt * mt;
|
|
10
|
+
const mt3 = mt2 * mt;
|
|
11
|
+
const t2 = t * t;
|
|
12
|
+
const t3 = t2 * t;
|
|
13
|
+
return [
|
|
14
|
+
mt3 * cp0[0] + 3 * mt2 * t * cp1[0] + 3 * mt * t2 * cp2[0] + t3 * cp3[0],
|
|
15
|
+
mt3 * cp0[1] + 3 * mt2 * t * cp1[1] + 3 * mt * t2 * cp2[1] + t3 * cp3[1]
|
|
16
|
+
];
|
|
17
|
+
};
|
|
18
|
+
var sampleCurveAtX = (x, points) => {
|
|
19
|
+
var _a, _b, _c, _d;
|
|
20
|
+
if (!points || points.length < 2) return x;
|
|
21
|
+
if (!((_a = points[0]) == null ? void 0 : _a.pos) || !((_b = points[points.length - 1]) == null ? void 0 : _b.pos)) return x;
|
|
22
|
+
let segmentIdx = 0;
|
|
23
|
+
for (let i = 0; i < points.length - 1; i++) {
|
|
24
|
+
if (((_c = points[i]) == null ? void 0 : _c.pos) && ((_d = points[i + 1]) == null ? void 0 : _d.pos) && x >= points[i].pos[0] && x <= points[i + 1].pos[0]) {
|
|
25
|
+
segmentIdx = i;
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const p0 = points[segmentIdx];
|
|
30
|
+
const p1 = points[segmentIdx + 1];
|
|
31
|
+
if (!(p0 == null ? void 0 : p0.pos) || !(p1 == null ? void 0 : p1.pos)) return x;
|
|
32
|
+
let tLow = 0;
|
|
33
|
+
let tHigh = 1;
|
|
34
|
+
let t = 0.5;
|
|
35
|
+
for (let iter = 0; iter < 20; iter++) {
|
|
36
|
+
const [px] = evaluateBezierSegment(
|
|
37
|
+
t,
|
|
38
|
+
p0.pos,
|
|
39
|
+
p1.pos,
|
|
40
|
+
p0.handleOut,
|
|
41
|
+
p1.handleIn
|
|
42
|
+
);
|
|
43
|
+
if (Math.abs(px - x) < 1e-4) break;
|
|
44
|
+
if (px < x) {
|
|
45
|
+
tLow = t;
|
|
46
|
+
} else {
|
|
47
|
+
tHigh = t;
|
|
48
|
+
}
|
|
49
|
+
t = (tLow + tHigh) / 2;
|
|
50
|
+
}
|
|
51
|
+
const [, py] = evaluateBezierSegment(
|
|
52
|
+
t,
|
|
53
|
+
p0.pos,
|
|
54
|
+
p1.pos,
|
|
55
|
+
p0.handleOut,
|
|
56
|
+
p1.handleIn
|
|
57
|
+
);
|
|
58
|
+
return Math.max(-0.5, Math.min(1.5, py));
|
|
59
|
+
};
|
|
60
|
+
var bakeCurveToArray = (curveData) => {
|
|
61
|
+
const data = new Float32Array(CURVE_RESOLUTION);
|
|
62
|
+
if (!(curveData == null ? void 0 : curveData.points) || !Array.isArray(curveData.points) || curveData.points.length < 2) {
|
|
63
|
+
for (let i = 0; i < CURVE_RESOLUTION; i++) {
|
|
64
|
+
data[i] = 1 - i / (CURVE_RESOLUTION - 1);
|
|
65
|
+
}
|
|
66
|
+
return data;
|
|
67
|
+
}
|
|
68
|
+
const firstPoint = curveData.points[0];
|
|
69
|
+
const lastPoint = curveData.points[curveData.points.length - 1];
|
|
70
|
+
if (!(firstPoint == null ? void 0 : firstPoint.pos) || !(lastPoint == null ? void 0 : lastPoint.pos) || !Array.isArray(firstPoint.pos) || !Array.isArray(lastPoint.pos)) {
|
|
71
|
+
for (let i = 0; i < CURVE_RESOLUTION; i++) {
|
|
72
|
+
data[i] = 1 - i / (CURVE_RESOLUTION - 1);
|
|
73
|
+
}
|
|
74
|
+
return data;
|
|
75
|
+
}
|
|
76
|
+
for (let i = 0; i < CURVE_RESOLUTION; i++) {
|
|
77
|
+
const x = i / (CURVE_RESOLUTION - 1);
|
|
78
|
+
data[i] = sampleCurveAtX(x, curveData.points);
|
|
79
|
+
}
|
|
80
|
+
return data;
|
|
81
|
+
};
|
|
82
|
+
var bakeCombinedCurves = (sizeCurve, opacityCurve, velocityCurve, rotationSpeedCurve) => {
|
|
83
|
+
const sizeData = bakeCurveToArray(sizeCurve);
|
|
84
|
+
const opacityData = bakeCurveToArray(opacityCurve);
|
|
85
|
+
const velocityData = bakeCurveToArray(velocityCurve);
|
|
86
|
+
const rotationSpeedData = bakeCurveToArray(rotationSpeedCurve);
|
|
87
|
+
const rgba = new Float32Array(CURVE_RESOLUTION * 4);
|
|
88
|
+
for (let i = 0; i < CURVE_RESOLUTION; i++) {
|
|
89
|
+
rgba[i * 4] = sizeData[i];
|
|
90
|
+
rgba[i * 4 + 1] = opacityData[i];
|
|
91
|
+
rgba[i * 4 + 2] = velocityData[i];
|
|
92
|
+
rgba[i * 4 + 3] = rotationSpeedData[i];
|
|
93
|
+
}
|
|
94
|
+
return rgba;
|
|
95
|
+
};
|
|
96
|
+
self.onmessage = (e) => {
|
|
97
|
+
const { id, sizeCurve, opacityCurve, velocityCurve, rotationSpeedCurve } = e.data;
|
|
98
|
+
const rgba = bakeCombinedCurves(
|
|
99
|
+
sizeCurve,
|
|
100
|
+
opacityCurve,
|
|
101
|
+
velocityCurve,
|
|
102
|
+
rotationSpeedCurve
|
|
103
|
+
);
|
|
104
|
+
self.postMessage({ id, rgba }, [rgba.buffer]);
|
|
105
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -138,6 +138,12 @@ type VFXParticlesProps = {
|
|
|
138
138
|
} | null;
|
|
139
139
|
/** Show debug control panel */
|
|
140
140
|
debug?: boolean;
|
|
141
|
+
/** Path to pre-baked curve texture (skips runtime baking for faster load) */
|
|
142
|
+
curveTexturePath?: string | null;
|
|
143
|
+
/** Depth test */
|
|
144
|
+
depthTest?: boolean;
|
|
145
|
+
/** Render order (higher values render on top) */
|
|
146
|
+
renderOrder?: number;
|
|
141
147
|
};
|
|
142
148
|
declare const VFXParticles: react.ForwardRefExoticComponent<VFXParticlesProps & react.RefAttributes<unknown>>;
|
|
143
149
|
|
|
@@ -258,4 +264,17 @@ declare const useVFXStore: typeof useVFXStoreImpl & {
|
|
|
258
264
|
getInitialState: () => CoreState;
|
|
259
265
|
};
|
|
260
266
|
|
|
261
|
-
|
|
267
|
+
/**
|
|
268
|
+
* Hook for async curve texture loading
|
|
269
|
+
* Returns a STABLE texture reference that updates in place
|
|
270
|
+
*
|
|
271
|
+
* If curveTexturePath is provided, loads pre-baked texture (fast)
|
|
272
|
+
* If curves are defined, bakes them in web worker
|
|
273
|
+
* If no curves AND no path, returns default texture (no baking, instant)
|
|
274
|
+
*
|
|
275
|
+
* The 4KB default texture is always created for shader compatibility,
|
|
276
|
+
* but baking is skipped when not needed (main performance win).
|
|
277
|
+
*/
|
|
278
|
+
declare const useCurveTextureAsync: (sizeCurve: CurveData | null, opacityCurve: CurveData | null, velocityCurve: CurveData | null, rotationSpeedCurve: CurveData | null, curveTexturePath?: string | null) => THREE.DataTexture;
|
|
279
|
+
|
|
280
|
+
export { VFXEmitter, VFXParticles, useCurveTextureAsync, useVFXEmitter, useVFXStore };
|