@null3d/particles 0.0.0 → 0.1.0
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 +92 -0
- package/lib/textures.js +9 -4
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# Particles for null3D
|
|
2
|
+
|
|
3
|
+
`@null3d/particles` adds GPU particle systems to a [null3D](https://github.com/null3d-engine/null3d)
|
|
4
|
+
scene: sparks, fire, smoke, dust, rain, falling leaves and fireflies. A system is an emitter and a
|
|
5
|
+
buffer of particles. The emitter decides where and when each particle is born. The GPU works out
|
|
6
|
+
each particle from its birth and its age, so no pass simulates them. A system costs the CPU one call
|
|
7
|
+
in a frame, and it draws the same on WebGPU and WebGL2.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
bun add @null3d/particles @null3d/engine
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Use
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { defineSketch } from '@null3d/engine';
|
|
19
|
+
import { createParticles, particleTextures } from '@null3d/particles';
|
|
20
|
+
|
|
21
|
+
export default defineSketch(async (ctx) => {
|
|
22
|
+
const { scene, textures } = ctx;
|
|
23
|
+
scene.setActiveCamera(scene.createPerspectiveCamera({ position: [0, 2, 6], target: [0, 1, 0] }));
|
|
24
|
+
const sparks = await createParticles(ctx, {
|
|
25
|
+
...particleTextures.spark(textures),
|
|
26
|
+
position: [0, 0.5, 0],
|
|
27
|
+
rate: 200,
|
|
28
|
+
lifetime: [0.6, 1],
|
|
29
|
+
shape: { type: 'sphere', radius: 0.05 },
|
|
30
|
+
speed: [2, 4],
|
|
31
|
+
gravity: [0, -9.8, 0],
|
|
32
|
+
facing: 'velocity',
|
|
33
|
+
stretch: 0.04,
|
|
34
|
+
size: [0.03, 0.05],
|
|
35
|
+
colors: ['#ffb060', '#ffd090'],
|
|
36
|
+
intensity: 4,
|
|
37
|
+
colorOverLife: ['#ffffff', '#ff7030'],
|
|
38
|
+
alphaOverLife: [1, 0],
|
|
39
|
+
});
|
|
40
|
+
return {
|
|
41
|
+
onLateUpdate() {
|
|
42
|
+
sparks.update();
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Pass the whole sketch context as the first argument. Call `update()` once per frame in
|
|
49
|
+
`onLateUpdate`, where each object is already at its place for the frame. An emitter that follows a
|
|
50
|
+
moving object then starts its particles where the object is now.
|
|
51
|
+
|
|
52
|
+
`particleTextures` makes particle textures in code, so a page downloads no image for them. It has
|
|
53
|
+
`glow`, `spark`, `smoke`, `flame`, `rain` and `leaves`. Each one takes the sketch's `textures` and
|
|
54
|
+
returns a map and its flipbook, which you spread into the options.
|
|
55
|
+
|
|
56
|
+
## Emitters and look
|
|
57
|
+
|
|
58
|
+
- Each entry of `bursts` gives `count` particles at `time` seconds, then again every `interval`
|
|
59
|
+
seconds. `system.burst(count)` adds a burst at any time.
|
|
60
|
+
- `subEmitters` start another system where a particle dies or first meets the ground, as a
|
|
61
|
+
firework's rocket bursts into sparks. Update that system after its parent in each frame.
|
|
62
|
+
- `softness` fades a particle as it nears the surface behind it, so it shows no hard line where it
|
|
63
|
+
cuts into the ground or a wall.
|
|
64
|
+
- `flipbook` plays a grid of frames from the map on each particle. The `smoke` and `flame` textures
|
|
65
|
+
hold sixteen frames each.
|
|
66
|
+
|
|
67
|
+
The options `gravity`, `drag`, `wind` and `turbulence` move the particles. With `groundHeight` and
|
|
68
|
+
`groundMode`, they bounce, stick or die on a ground plane. Give the field of `@null3d/wind` as
|
|
69
|
+
`windField`, and smoke and dust move with the same gusts as the plants. Then call the wind's
|
|
70
|
+
`update()` before the system's in each frame. Call `system.set(values)` to change the motion and
|
|
71
|
+
the look at any time.
|
|
72
|
+
|
|
73
|
+
## Costs and limits
|
|
74
|
+
|
|
75
|
+
- Each live particle costs the GPU one quad. The core writes 32 bytes per birth, and the engine
|
|
76
|
+
uploads only the records born in the frame.
|
|
77
|
+
- A large `capacity` costs vertex work for every record, live or not. Size it for the particles you
|
|
78
|
+
need.
|
|
79
|
+
- A system is one draw. Its particles draw in the order they were born, so `additive` blending,
|
|
80
|
+
which needs no order, suits most effects.
|
|
81
|
+
- Soft particles cost one full-screen copy of the camera's depth per frame. The Low preset turns
|
|
82
|
+
them off.
|
|
83
|
+
- Particles cast no shadows, and lights do not light them.
|
|
84
|
+
|
|
85
|
+
## Docs
|
|
86
|
+
|
|
87
|
+
The [particles docs page](https://github.com/null3d-engine/null3d/blob/generated/docs/api/particles.md)
|
|
88
|
+
describes every option. The API is experimental, so it can still change between versions.
|
|
89
|
+
|
|
90
|
+
## License
|
|
91
|
+
|
|
92
|
+
Licensed under either of the Apache License 2.0 or the MIT license, at your option.
|
package/lib/textures.js
CHANGED
|
@@ -105,7 +105,9 @@ export const particleTextures = {
|
|
|
105
105
|
const body = smooth(edge + 0.25, edge - 0.35, r + (billow - 0.5) * 0.9);
|
|
106
106
|
// Lit from above: the top of the puff is lighter than its underside.
|
|
107
107
|
out[0] = 0.72 + 0.28 * smooth(-1, 1, v + (billow - 0.5));
|
|
108
|
-
|
|
108
|
+
// The puff thins to nothing before the frame's edge, as it grows, so no particle shows
|
|
109
|
+
// the straight sides of its square.
|
|
110
|
+
out[1] = body * (0.25 + 0.75 * billow) * (1 - 0.55 * age) * smooth(1, 0.8, r);
|
|
109
111
|
});
|
|
110
112
|
},
|
|
111
113
|
/** Sixteen frames of a flickering flame tongue, for fires, torches and jets. It plays at 24 frames per second. */
|
|
@@ -139,10 +141,13 @@ export const particleTextures = {
|
|
|
139
141
|
const round = [0.42, 0.62, 0.32, 0.5][frame];
|
|
140
142
|
const tipBend = [0.1, -0.08, 0.15, 0][frame];
|
|
141
143
|
const x = u - tipBend * (v + 1) ** 2 * 0.25;
|
|
142
|
-
|
|
144
|
+
// The leaf, its stem included, stays clear of the frame's edges, so no particle shows a
|
|
145
|
+
// leaf cut straight by the side of its square.
|
|
146
|
+
const along = (v + 0.88) / 1.76;
|
|
143
147
|
const half = round * Math.sin(Math.PI * Math.min(Math.max(along, 0), 1)) ** 0.8;
|
|
144
|
-
const
|
|
145
|
-
const
|
|
148
|
+
const ends = smooth(-0.03, 0.01, along) * smooth(1.03, 0.99, along);
|
|
149
|
+
const inside = smooth(half + 0.04, half - 0.04, Math.abs(x)) * ends;
|
|
150
|
+
const stem = along < 0 && along > -0.045 && Math.abs(x) < 0.03 ? 1 : 0;
|
|
146
151
|
const rib = Math.exp(-x * x * 900);
|
|
147
152
|
const offset = ((Math.abs(x) * 1.6 - along * 4 + 10) % 0.5) - 0.25;
|
|
148
153
|
const vein = Math.exp(-offset * offset * 400);
|
package/lib/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const VERSION = "0.
|
|
1
|
+
export declare const VERSION = "0.1.0";
|
package/lib/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@null3d/particles",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "GPU particle systems for null3D sketches: emitters, bursts and sub-emitters, soft particles and flipbooks made in code, with closed-form motion that costs no simulation pass.",
|
|
5
5
|
"license": "MIT OR Apache-2.0",
|
|
6
6
|
"author": "Ramesh Nair (https://hiddentao.com)",
|