@zaisma/effects 1.2.0 → 2.0.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 +8 -6
- package/batches/chunks.js +88 -105
- package/batches/marks.js +59 -71
- package/batches/motion-nodes.js +65 -0
- package/batches/ribbons.js +26 -30
- package/batches/shared.js +169 -123
- package/batches/sprites.js +222 -240
- package/batches/warps.js +56 -126
- package/budget.js +3 -3
- package/flashes.js +54 -28
- package/index.d.ts +8 -8
- package/index.js +42 -40
- package/package.json +2 -1
- package/piece.json +7 -6
- package/piece.json.js +7 -6
- package/shapes.js +54 -25
- package/tables.js +1 -1
- package/weather.js +21 -38
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @zaisma/effects
|
|
2
2
|
|
|
3
|
-
Spectacle that holds a phone's frame rate, for three.js: thousands of lit particles, hits, explosions, magic, fire, smoke, trails, shockwaves, marks on surfaces, screen warps, debris that bounces and weather, drawn in the look a game picks. An effect is a small file an AI writes, and 92 come with the piece.
|
|
3
|
+
Spectacle that holds a phone's frame rate, for three.js's `WebGPURenderer` (it draws on WebGPU, and on WebGL 2 where a device has none): thousands of lit particles, hits, explosions, magic, fire, smoke, trails, shockwaves, marks on surfaces, screen warps, debris that bounces and weather, drawn in the look a game picks. An effect is a small file an AI writes, and 92 come with the piece.
|
|
4
4
|
|
|
5
5
|
```
|
|
6
6
|
npx @zaisma/cli pieces add @zaisma/effects
|
|
@@ -8,13 +8,15 @@ npx @zaisma/cli pieces add @zaisma/effects
|
|
|
8
8
|
|
|
9
9
|
```js
|
|
10
10
|
import { createEffects } from "./pieces/@zaisma/effects/index.js";
|
|
11
|
+
import { createLighting } from "./pieces/@zaisma/lighting/index.js";
|
|
11
12
|
|
|
12
|
-
const
|
|
13
|
+
const lighting = await createLighting({ canvas, scene }); // it makes the renderer and draws the frame
|
|
14
|
+
const effects = createEffects({ scene, renderer: lighting.renderer, lighting, settings: { look: "glowing" } });
|
|
13
15
|
await effects.load(["explosion_large", "hit_heavy", "campfire", "./effects/my_spell.json"]);
|
|
14
16
|
|
|
15
17
|
function frame() {
|
|
16
18
|
effects.update(camera); // before rendering
|
|
17
|
-
|
|
19
|
+
lighting.render(camera);
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
effects.play("explosion_large", { positionM: { x: 4, y: 0, z: -2 } });
|
|
@@ -25,7 +27,7 @@ fire.stop();
|
|
|
25
27
|
effects.settings.set({ rainPct: 60, windSpeedMPerSec: 9 }); // the weather turns, over a few seconds
|
|
26
28
|
```
|
|
27
29
|
|
|
28
|
-
A page with no bundler needs the import map `zaisma pieces add` prints, so the piece finds three.js.
|
|
30
|
+
A page with no bundler needs the import map `zaisma pieces add` prints, so the piece finds three.js. Without the lighting piece, give a `THREE.WebGPURenderer` of the game's own (from `three/webgpu`) and draw with `renderer.render(scene, camera)`. A game that stays on `THREE.WebGLRenderer` keeps `@zaisma/effects` 1.
|
|
29
31
|
|
|
30
32
|
## What it does
|
|
31
33
|
|
|
@@ -33,9 +35,9 @@ A page with no bundler needs the import map `zaisma pieces add` prints, so the p
|
|
|
33
35
|
- **A look is a setting.** `look` draws every effect as `cartoon` (few big shapes, crisp edges, flat tones), `glowing` (soft light that blooms), `faceted` (little low-poly solids with flat faces) or `realistic` (textured, lit by the scene, soft where it meets a surface). It changes while playing, and `effects.defineOwnLook({ basedOnLookName, materials })` makes a game's own from any of them. Every shape is drawn by the piece, so it ships no pictures.
|
|
34
36
|
- **An effect is a file.** What it gives off, how that moves and fades, its flash, its mark, its warp and its shake, in plain fields each named with its unit. [`EFFECT-FILES.md`](EFFECT-FILES.md) is the format, written for an AI; the files in `library/` are the examples. A wrong field is refused with its name and the fix (`checkEffect`).
|
|
35
37
|
- **The library:** hits, explosions, magic (casts, charges, bolts, auras, lightning, frost, healing, portals), fire, smoke, sparks, splashes, trails, shockwaves, marks, warps, debris, and impacts and footsteps that differ by surface. `effects.load("all")` loads every one; a game loads the ones it plays. `library/index.json` lists them.
|
|
36
|
-
- **Lit by the scene.** With the lighting piece given, particles take its sun or moon
|
|
38
|
+
- **Lit by the scene.** With the lighting piece given, particles take its sun or moon and its fill light, and stand in its air: fog, haze, ground fog and the glow round its lamps. The brightest, nearest flashes throw real light on the level through the lighting's own cheap lights (two at medium quality, four at high) and the rest only glow, so twenty explosions cost no more than two. Light brighter than white blooms where the game has bloom on.
|
|
37
39
|
- **Marks, debris, warps, shakes.** Scorches and cracks lie on the surface where an effect landed, found with the physics piece's ray test or a `castRay` of the game's own; chunks bounce off the level; a shockwave bends the picture behind it; a blast shakes the camera (the camera piece's, or `effects.shake` for a game's own).
|
|
38
|
-
- **Weather** is settings: `rainPct`, `snowPct`, `fogPct`, `windSpeedMPerSec`, `windDirectionDeg` and `windGustPct`, eased over `weatherChangeDurationSec`. Rain stays out from under roofs and splashes where it lands; wind leans every particle by as much as its effect says. `effects.weather` reads what it is right now, for sound and foliage.
|
|
40
|
+
- **Weather** is settings: `rainPct`, `snowPct`, `fogPct`, `windSpeedMPerSec`, `windDirectionDeg` and `windGustPct`, eased over `weatherChangeDurationSec`. Rain stays out from under roofs and splashes where it lands; wind leans every particle by as much as its effect says. With the lighting piece given, its weather falls here too: set a storm there and it rains, with the sky, the wet ground and the lightning to match. `effects.weather` reads what it is right now, for sound and foliage.
|
|
39
41
|
- **Free with the other pieces.** Given the character, each footfall raises dust, a jump and a landing raise a puff sized by the fall, and water splashes. Given physics, every collision throws an impact sized by its speed and weight. `footstepEffectsEnabled`, `landingEffectsEnabled` and `impactEffectsEnabled` turn each off; `beforeEffect` swaps or tints any of them, and `surfaceKindAt` says what the ground is.
|
|
40
42
|
- **A budget, never a stall.** 2,000 particles at `low`, 6,000 at `medium`, 20,000 at `high` (`particleBudgetPct` scales it). Over it, the least important and farthest effects thin first, and smoke thins before it fills the screen. `qualityLevel` `auto` follows the lighting piece's, or steps down by the same rule without it. `effects.stats` reads live particles, draw calls and the time an update took.
|
|
41
43
|
|
package/batches/chunks.js
CHANGED
|
@@ -5,12 +5,13 @@
|
|
|
5
5
|
//
|
|
6
6
|
// All the shapes share one draw: each instance has 60 corners, and reads its shape's corners from a small table by
|
|
7
7
|
// its corner's number, so a tetrahedron and an icosahedron are the same geometry to the graphics chip.
|
|
8
|
-
import * as THREE from "three";
|
|
8
|
+
import * as THREE from "three/webgpu";
|
|
9
|
+
import { Fn, If, attribute, cameraProjectionMatrix, cameraViewMatrix, cos, cross, packNormalToRGB, dot, float, floor, int, ivec2, length, max, min, mix, mrt, normalize, output, sin, sqrt, texture, varyingProperty, vec3, vec4, vertexIndex } from "three/tsl";
|
|
9
10
|
import { CHUNK_SHAPE_NAMES } from "../looks.js";
|
|
10
|
-
import { MOTION_GLSL } from "../motion.js";
|
|
11
11
|
import { createSlotPool } from "../tables.js";
|
|
12
|
+
import { hash, localPlace, overLife, turnBy, worldDrift } from "./motion-nodes.js";
|
|
12
13
|
import { ELEMENT_ROW } from "./sprites.js";
|
|
13
|
-
import {
|
|
14
|
+
import { batchMaterial } from "./shared.js";
|
|
14
15
|
|
|
15
16
|
const CORNER_COUNT = 60;
|
|
16
17
|
const FLOAT_COUNT_PER_CHUNK = 12;
|
|
@@ -52,107 +53,96 @@ function makeShapeTable() {
|
|
|
52
53
|
return texture;
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
const
|
|
56
|
-
attribute vec4 aBirth;
|
|
57
|
-
attribute vec4 aVelocity;
|
|
58
|
-
attribute vec4 aSeed;
|
|
59
|
-
uniform highp sampler2D uChunkShapes;
|
|
60
|
-
${SHARED_VERTEX_GLSL}
|
|
61
|
-
${MOTION_GLSL}
|
|
62
|
-
varying vec3 vColor;
|
|
63
|
-
#include <fog_pars_vertex>
|
|
56
|
+
const turnRound = (axis, angle, point) => point.mul(cos(angle)).add(cross(axis, point).mul(sin(angle))).add(axis.mul(dot(axis, point)).mul(cos(angle).oneMinus()));
|
|
64
57
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
58
|
+
/** The chunks' vertex and fragment stages, on a material. shared: createSharedUniforms' answer. */
|
|
59
|
+
function drawChunks(material, shared, shapeTable) {
|
|
60
|
+
const birthAttribute = attribute("aBirth", "vec4");
|
|
61
|
+
const velocityAttribute = attribute("aVelocity", "vec4");
|
|
62
|
+
const seedAttribute = attribute("aSeed", "vec4");
|
|
63
|
+
const colorVarying = varyingProperty("vec3", "zaismaColor");
|
|
64
|
+
const normalVarying = varyingProperty("vec3", "zaismaNormal");
|
|
65
|
+
const shapes = texture(shapeTable);
|
|
66
|
+
const { element, play } = shared;
|
|
69
67
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
vec4 motionA = zaismaElement(row, ${ELEMENT_ROW.motionA});
|
|
90
|
-
vec4 motionB = zaismaElement(row, ${ELEMENT_ROW.motionB});
|
|
91
|
-
vec4 facing = zaismaElement(row, ${ELEMENT_ROW.facing});
|
|
92
|
-
vec4 playPlace = zaismaPlay(playRow, 0);
|
|
93
|
-
vec4 playTurn = zaismaPlay(playRow, 1);
|
|
94
|
-
vec4 playTint = zaismaPlay(playRow, 2);
|
|
68
|
+
material.vertexNode = Fn(() => {
|
|
69
|
+
const row = int(seedAttribute.y.add(0.5)).toVar();
|
|
70
|
+
const playRow = int(seedAttribute.z.add(0.5)).toVar();
|
|
71
|
+
const seed = seedAttribute.x;
|
|
72
|
+
const age = shared.uTime.sub(birthAttribute.w).toVar();
|
|
73
|
+
const life = velocityAttribute.w;
|
|
74
|
+
const lifeShare = age.div(life).toVar();
|
|
75
|
+
const style = element(row, ELEMENT_ROW.style).toVar();
|
|
76
|
+
const shapeRow = int(style.w.add(0.5)).mul(2).toVar();
|
|
77
|
+
// A shape with fewer corners than the batch's sixty leaves the rest out, before any of the work below.
|
|
78
|
+
const cornerHeld = shapes.load(ivec2(int(vertexIndex), shapeRow)).toVar();
|
|
79
|
+
const clip = vec4(0.0, 0.0, 2.0, 1.0).toVar();
|
|
80
|
+
If(age.greaterThanEqual(0.0).and(lifeShare.lessThan(1.0)).and(cornerHeld.w.greaterThanEqual(0.5)), () => {
|
|
81
|
+
const firstMotion = element(row, ELEMENT_ROW.motionA).toVar();
|
|
82
|
+
const secondMotion = element(row, ELEMENT_ROW.motionB).toVar();
|
|
83
|
+
const facing = element(row, ELEMENT_ROW.facing).toVar();
|
|
84
|
+
const playPlace = play(playRow, 0).toVar();
|
|
85
|
+
const playTurn = play(playRow, 1).toVar();
|
|
86
|
+
const playTint = play(playRow, 2).toVar();
|
|
95
87
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
88
|
+
const isAttached = secondMotion.w.greaterThan(0.5);
|
|
89
|
+
const center = isAttached.select(vec3(0.0), playPlace.xyz).toVar();
|
|
90
|
+
const axis = isAttached.select(vec3(0.0, 1.0, 0.0), turnBy(playTurn, vec3(0.0, 1.0, 0.0))).toVar();
|
|
91
|
+
const placeAt = (atAge) => {
|
|
92
|
+
const local = localPlace(birthAttribute.xyz, velocityAttribute.xyz, atAge, life, seed, firstMotion, secondMotion, center, axis).toVar();
|
|
93
|
+
return isAttached.select(playPlace.xyz.add(turnBy(playTurn, local)), local).add(worldDrift(atAge, firstMotion, shared.uWind));
|
|
94
|
+
};
|
|
95
|
+
const world = placeAt(age).toVar();
|
|
101
96
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
float opacity = zaismaOverLife(zaismaElement(row, ${ELEMENT_ROW.opacityKeys}), keyCounts.y, lifeShare) * playTint.a;
|
|
107
|
-
size *= sqrt(min(opacity * 1.6, 1.0));
|
|
97
|
+
const keyCounts = element(row, ELEMENT_ROW.keyCounts).toVar();
|
|
98
|
+
// A solid never fades: it shrinks away as its effect would have faded it.
|
|
99
|
+
const opacity = overLife(element(row, ELEMENT_ROW.opacityKeys), keyCounts.y, lifeShare).mul(playTint.a);
|
|
100
|
+
const particleSize = overLife(element(row, ELEMENT_ROW.sizeKeys), keyCounts.x, lifeShare).mul(playPlace.w).mul(keyCounts.w.mul(hash(seed, 1.0).mul(2.0).sub(1.0)).add(1.0)).mul(sqrt(opacity.mul(1.6).min(1.0))).toVar();
|
|
108
101
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
world += corner * size;
|
|
131
|
-
vec4 mvPosition = viewMatrix * vec4(world, 1.0);
|
|
132
|
-
gl_Position = projectionMatrix * mvPosition;
|
|
102
|
+
const corner = vec3(cornerHeld.xyz).toVar();
|
|
103
|
+
const normal = shapes.load(ivec2(int(vertexIndex), shapeRow.add(1))).xyz.toVar();
|
|
104
|
+
If(facing.x.greaterThan(0.5).and(facing.x.lessThan(1.5)), () => {
|
|
105
|
+
// Pointing along its own motion, stretched by its streak time.
|
|
106
|
+
const earlier = age.sub(0.02).max(0.0).toVar();
|
|
107
|
+
const velocity = world.sub(placeAt(earlier)).div(age.sub(earlier).max(1e-4)).toVar();
|
|
108
|
+
const speed = length(velocity).toVar();
|
|
109
|
+
const along = speed.greaterThan(1e-3).select(velocity.div(speed.max(1e-3)), vec3(0.0, 1.0, 0.0)).toVar();
|
|
110
|
+
const side = normalize(along.y.abs().lessThan(0.99).select(cross(vec3(0.0, 1.0, 0.0), along), vec3(1.0, 0.0, 0.0))).toVar();
|
|
111
|
+
const forward = cross(side, along).toVar();
|
|
112
|
+
const stretch = max(1.0, speed.mul(facing.y).div(particleSize.max(1e-3))).toVar();
|
|
113
|
+
corner.assign(side.mul(cornerHeld.x).add(along.mul(cornerHeld.y).mul(stretch)).add(forward.mul(cornerHeld.z)));
|
|
114
|
+
normal.assign(normalize(side.mul(normal.x).add(along.mul(normal.y).div(stretch)).add(forward.mul(normal.z))));
|
|
115
|
+
}).Else(() => {
|
|
116
|
+
const tumbleAxis = normalize(vec3(hash(seed, 4.0), hash(seed, 5.0), hash(seed, 6.0)).mul(2.0).sub(1.0).add(1e-3)).toVar();
|
|
117
|
+
const angle = seedAttribute.w.add(facing.z.mul(age).mul(hash(seed, 3.0).add(0.5))).toVar();
|
|
118
|
+
corner.assign(turnRound(tumbleAxis, angle, cornerHeld.xyz));
|
|
119
|
+
normal.assign(turnRound(tumbleAxis, angle, normal));
|
|
120
|
+
});
|
|
121
|
+
world.addAssign(corner.mul(particleSize));
|
|
133
122
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
123
|
+
const colorPlace = lifeShare.clamp().mul(keyCounts.z.sub(1.0)).toVar();
|
|
124
|
+
const colorIndex = min(floor(colorPlace), keyCounts.z.sub(2.0).max(0.0)).toVar();
|
|
125
|
+
const colorFrom = element(row, int(colorIndex).add(int(ELEMENT_ROW.colorKeys)));
|
|
126
|
+
const colorTo = element(row, int(min(colorIndex.add(1.0), keyCounts.z.sub(1.0))).add(int(ELEMENT_ROW.colorKeys)));
|
|
127
|
+
const glow = element(row, ELEMENT_ROW.colorKeys).a;
|
|
128
|
+
const lit = element(row, ELEMENT_ROW.colorKeys + 2).a;
|
|
129
|
+
// Each chunk takes its own shade of the colour, so a pile of them reads as separate pieces.
|
|
130
|
+
const shade = hash(seed, 7.0).mul(0.36).add(0.82);
|
|
131
|
+
const color = mix(colorFrom.rgb, colorTo.rgb, colorPlace.sub(colorIndex)).mul(playTint.rgb).mul(glow).mul(shade).mul(mix(vec3(1.0), shared.lightAt(world, normal, float(0.15)), lit));
|
|
132
|
+
const air = shared.airAt(world).toVar();
|
|
133
|
+
colorVarying.assign(color.mul(air.a).add(air.rgb));
|
|
134
|
+
normalVarying.assign(cameraViewMatrix.mul(vec4(normal, 0.0)).xyz);
|
|
135
|
+
clip.assign(cameraProjectionMatrix.mul(cameraViewMatrix).mul(vec4(world, 1.0)));
|
|
136
|
+
});
|
|
137
|
+
return clip;
|
|
138
|
+
})();
|
|
145
139
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
#include <fog_fragment>
|
|
153
|
-
#include <tonemapping_fragment>
|
|
154
|
-
#include <colorspace_fragment>
|
|
155
|
-
}`;
|
|
140
|
+
material.fragmentNode = vec4(colorVarying, 1.0);
|
|
141
|
+
// Where the picture keeps each surface's normal (the lighting piece's, for outlines and corner shade), a chunk
|
|
142
|
+
// gives its own flat faces.
|
|
143
|
+
material.mrtNode = mrt({ output, normal: packNormalToRGB(normalize(normalVarying)) });
|
|
144
|
+
return material;
|
|
145
|
+
}
|
|
156
146
|
|
|
157
147
|
/** Makes the batch. Answers { mesh, takeSlab, giveSlab, write, rewrite, update, liveCount, capacity, dispose }. */
|
|
158
148
|
export function createChunkBatch({ chunkCount, sharedUniforms }) {
|
|
@@ -167,14 +157,7 @@ export function createChunkBatch({ chunkCount, sharedUniforms }) {
|
|
|
167
157
|
geometry.setAttribute("aSeed", new THREE.InterleavedBufferAttribute(buffer, 4, 8));
|
|
168
158
|
geometry.instanceCount = 0;
|
|
169
159
|
const shapeTable = makeShapeTable();
|
|
170
|
-
const material =
|
|
171
|
-
name: "zaisma_effect_chunks",
|
|
172
|
-
vertexShader: CHUNK_VERTEX,
|
|
173
|
-
fragmentShader: CHUNK_FRAGMENT,
|
|
174
|
-
uniforms: { ...fogUniforms(), ...sharedUniforms, uChunkShapes: { value: shapeTable } },
|
|
175
|
-
fog: true,
|
|
176
|
-
side: THREE.DoubleSide,
|
|
177
|
-
});
|
|
160
|
+
const material = drawChunks(batchMaterial("zaisma_effect_chunks", { side: THREE.DoubleSide }), sharedUniforms, shapeTable);
|
|
178
161
|
const mesh = new THREE.Mesh(geometry, material);
|
|
179
162
|
mesh.name = "zaisma_effect_chunks";
|
|
180
163
|
mesh.frustumCulled = false;
|
package/batches/marks.js
CHANGED
|
@@ -1,68 +1,61 @@
|
|
|
1
1
|
// The mark batch: what effects leave on surfaces (a scorch, a crack, a splat), in one draw call. A mark is a flat
|
|
2
2
|
// shape laid on the surface where it was made, lit by the scene, that glows while hot and fades as its life ends.
|
|
3
3
|
// They're kept in a ring: when it's full, a new mark takes the oldest one's place.
|
|
4
|
-
import * as THREE from "three";
|
|
5
|
-
import {
|
|
4
|
+
import * as THREE from "three/webgpu";
|
|
5
|
+
import { Fn, If, attribute, cameraProjectionMatrix, cameraViewMatrix, cos, cross, float, min, normalize, sin, varyingProperty, vec2, vec3, vec4 } from "three/tsl";
|
|
6
|
+
import { ONE_BLEND, batchMaterial, leaveNormals } from "./shared.js";
|
|
6
7
|
|
|
7
8
|
const FLOAT_COUNT_PER_MARK = 20;
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
attribute vec4
|
|
12
|
-
attribute vec4
|
|
13
|
-
attribute vec4
|
|
14
|
-
attribute vec4
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
10
|
+
/** The marks' vertex and fragment stages, on a material. shared: createSharedUniforms' answer. */
|
|
11
|
+
function drawMarks(material, shared) {
|
|
12
|
+
const placeAttribute = attribute("aPlace", "vec4");
|
|
13
|
+
const facingAttribute = attribute("aFacing", "vec4");
|
|
14
|
+
const lifeAttribute = attribute("aLife", "vec4");
|
|
15
|
+
const tintAttribute = attribute("aTint", "vec4");
|
|
16
|
+
const glowAttribute = attribute("aGlow", "vec4");
|
|
17
|
+
const corner = attribute("position", "vec3").xy;
|
|
18
|
+
const uvVarying = varyingProperty("vec2", "zaismaUv");
|
|
19
|
+
const colorVarying = varyingProperty("vec4", "zaismaColor");
|
|
20
|
+
const glowVarying = varyingProperty("vec4", "zaismaGlow");
|
|
21
|
+
const airVarying = varyingProperty("vec4", "zaismaAir");
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
#include <fog_vertex>
|
|
45
|
-
}`;
|
|
23
|
+
material.vertexNode = Fn(() => {
|
|
24
|
+
const age = shared.uTime.sub(lifeAttribute.x).toVar();
|
|
25
|
+
const life = lifeAttribute.y;
|
|
26
|
+
const clip = vec4(0.0, 0.0, 2.0, 1.0).toVar();
|
|
27
|
+
If(age.greaterThanEqual(0.0).and(age.lessThan(life)), () => {
|
|
28
|
+
const normal = facingAttribute.xyz;
|
|
29
|
+
const side = normalize(normal.y.abs().lessThan(0.99).select(cross(vec3(0.0, 1.0, 0.0), normal), vec3(1.0, 0.0, 0.0))).toVar();
|
|
30
|
+
const forward = cross(normal, side);
|
|
31
|
+
const cosTurn = cos(facingAttribute.w).toVar();
|
|
32
|
+
const sinTurn = sin(facingAttribute.w).toVar();
|
|
33
|
+
const turned = vec2(corner.x.mul(cosTurn).sub(corner.y.mul(sinTurn)), corner.x.mul(sinTurn).add(corner.y.mul(cosTurn))).toVar();
|
|
34
|
+
const world = placeAttribute.xyz.add(side.mul(turned.x).add(forward.mul(turned.y)).mul(placeAttribute.w).mul(0.5)).add(normal.mul(0.012)).toVar();
|
|
35
|
+
uvVarying.assign(lifeAttribute.zw.add(corner.mul(0.5).add(0.5).mul(0.98 / 8)).add(0.00125));
|
|
36
|
+
// It settles in over a moment, and fades out over the last of its life.
|
|
37
|
+
const fade = age.div(0.06).min(1.0).mul(life.sub(age).div(min(life.mul(0.25), 2.0)).clamp());
|
|
38
|
+
colorVarying.assign(vec4(tintAttribute.rgb.mul(shared.lightAt(world, normal, float(0.0))), tintAttribute.a.mul(fade)));
|
|
39
|
+
const hot = glowAttribute.a.greaterThan(0.0).select(age.div(glowAttribute.a.max(1e-4)).oneMinus().max(0.0), float(0.0)).toVar();
|
|
40
|
+
glowVarying.assign(vec4(glowAttribute.rgb, hot.mul(hot)));
|
|
41
|
+
airVarying.assign(shared.airAt(world));
|
|
42
|
+
clip.assign(cameraProjectionMatrix.mul(cameraViewMatrix).mul(vec4(world, 1.0)));
|
|
43
|
+
});
|
|
44
|
+
return clip;
|
|
45
|
+
})();
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
void main() {
|
|
55
|
-
vec4 held = texture2D(uAtlas, vUv);
|
|
56
|
-
float zaismaAddShare = 0.0;
|
|
57
|
-
gl_FragColor = vec4(vColor.rgb * (0.7 + 0.3 * held.b) + vGlow.rgb * vGlow.a * held.b * 3.0, held.g * vColor.a);
|
|
58
|
-
${BLEND_END_GLSL}
|
|
59
|
-
}`;
|
|
47
|
+
material.fragmentNode = Fn(() => {
|
|
48
|
+
const held = shared.uAtlas.sample(uvVarying);
|
|
49
|
+
const color = colorVarying.rgb.mul(held.b.mul(0.3).add(0.7)).add(glowVarying.rgb.mul(glowVarying.a).mul(held.b).mul(3.0));
|
|
50
|
+
return shared.blendEnd(vec4(color, held.g.mul(colorVarying.a)), 0.0, airVarying);
|
|
51
|
+
})();
|
|
52
|
+
return material;
|
|
53
|
+
}
|
|
60
54
|
|
|
61
55
|
/** Makes the batch. Answers { mesh, setCapacity, add, clear, liveCount, dispose }. */
|
|
62
56
|
export function createMarkBatch({ markCount, sharedUniforms }) {
|
|
63
|
-
|
|
64
|
-
geometry
|
|
65
|
-
geometry.setIndex([0, 1, 2, 2, 1, 3]);
|
|
57
|
+
// A new size is a new geometry: three's renderer keeps what it made for the old one's buffers.
|
|
58
|
+
let geometry = null;
|
|
66
59
|
let values;
|
|
67
60
|
let buffer;
|
|
68
61
|
let capacityCount = 0;
|
|
@@ -70,7 +63,18 @@ export function createMarkBatch({ markCount, sharedUniforms }) {
|
|
|
70
63
|
let writtenCount = 0;
|
|
71
64
|
const endSec = [];
|
|
72
65
|
|
|
66
|
+
const material = leaveNormals(drawMarks(batchMaterial("zaisma_effect_marks", { polygonOffset: true, polygonOffsetFactor: -2, polygonOffsetUnits: -2, ...ONE_BLEND }), sharedUniforms));
|
|
67
|
+
const mesh = new THREE.Mesh(undefined, material);
|
|
68
|
+
mesh.name = "zaisma_effect_marks";
|
|
69
|
+
mesh.frustumCulled = false;
|
|
70
|
+
mesh.renderOrder = 800;
|
|
71
|
+
mesh.visible = false;
|
|
72
|
+
|
|
73
73
|
function setCapacity(newCapacity) {
|
|
74
|
+
geometry?.dispose();
|
|
75
|
+
geometry = new THREE.InstancedBufferGeometry();
|
|
76
|
+
geometry.setAttribute("position", new THREE.BufferAttribute(new Float32Array([-1, -1, 0, 1, -1, 0, -1, 1, 0, 1, 1, 0]), 3));
|
|
77
|
+
geometry.setIndex([0, 1, 2, 2, 1, 3]);
|
|
74
78
|
capacityCount = newCapacity;
|
|
75
79
|
values = new Float32Array(Math.max(capacityCount, 1) * FLOAT_COUNT_PER_MARK);
|
|
76
80
|
buffer = new THREE.InstancedInterleavedBuffer(values, FLOAT_COUNT_PER_MARK);
|
|
@@ -80,26 +84,10 @@ export function createMarkBatch({ markCount, sharedUniforms }) {
|
|
|
80
84
|
writtenCount = 0;
|
|
81
85
|
endSec.length = 0;
|
|
82
86
|
geometry.instanceCount = 0;
|
|
87
|
+
mesh.geometry = geometry;
|
|
83
88
|
}
|
|
84
89
|
setCapacity(markCount);
|
|
85
90
|
|
|
86
|
-
const material = new THREE.ShaderMaterial({
|
|
87
|
-
name: "zaisma_effect_marks",
|
|
88
|
-
vertexShader: MARK_VERTEX,
|
|
89
|
-
fragmentShader: MARK_FRAGMENT,
|
|
90
|
-
uniforms: { ...fogUniforms(), ...sharedUniforms },
|
|
91
|
-
fog: true,
|
|
92
|
-
polygonOffset: true,
|
|
93
|
-
polygonOffsetFactor: -2,
|
|
94
|
-
polygonOffsetUnits: -2,
|
|
95
|
-
...ONE_BLEND,
|
|
96
|
-
});
|
|
97
|
-
const mesh = new THREE.Mesh(geometry, material);
|
|
98
|
-
mesh.name = "zaisma_effect_marks";
|
|
99
|
-
mesh.frustumCulled = false;
|
|
100
|
-
mesh.renderOrder = 800;
|
|
101
|
-
mesh.visible = false;
|
|
102
|
-
|
|
103
91
|
/**
|
|
104
92
|
* Lays a mark: { x, y, z, normalX, normalY, normalZ, sizeM, turnRad, birthSec, lifeSec, cellU, cellV, color: Color,
|
|
105
93
|
* opacityShare, glowColor: Color, glowDurationSec }.
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// motion.js's formulas once more, in three's shading language: what the batches' vertex stages work a particle's
|
|
2
|
+
// place out with. Keep them in step with motion.js by hand, line for line.
|
|
3
|
+
import { Fn, If, cos, cross, dot, exp, float, floor, fract, mix, sin, vec3 } from "three/tsl";
|
|
4
|
+
import { WIND_GRIP_PER_SEC } from "../motion.js";
|
|
5
|
+
|
|
6
|
+
/** How long a particle with drag has really travelled at an age. */
|
|
7
|
+
export const travelSec = (drag, age) => drag.greaterThan(1e-4).select(exp(drag.mul(age).negate()).oneMinus().div(drag.max(1e-4)), age);
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Everything the effect itself does to a particle (in the effect's own space for attached particles, the world
|
|
11
|
+
* otherwise). motionA: drag, gravity (m/s2 down), wind share, turbulence (m). motionB: turbulence per sec, swirl
|
|
12
|
+
* (rad/s), pull share.
|
|
13
|
+
*/
|
|
14
|
+
export const localPlace = Fn(([birthPlace, birthVelocity, age, life, seed, firstMotion, secondMotion, center, axis]) => {
|
|
15
|
+
const place = birthPlace.add(birthVelocity.mul(travelSec(firstMotion.x, age))).toVar();
|
|
16
|
+
If(firstMotion.w.greaterThan(0.0), () => {
|
|
17
|
+
const turn = age.mul(secondMotion.x).toVar();
|
|
18
|
+
const phase = seed.mul(6.2831853).toVar();
|
|
19
|
+
const sway = vec3(
|
|
20
|
+
sin(turn.mul(2.1).add(phase.mul(3.0))).add(sin(turn.mul(1.3).add(phase.mul(7.0)))),
|
|
21
|
+
sin(turn.mul(1.7).add(phase.mul(5.0))).add(sin(turn.mul(2.9).add(phase.mul(11.0)))),
|
|
22
|
+
sin(turn.mul(1.9).add(phase.mul(13.0))).add(sin(turn.mul(1.1).add(phase.mul(17.0)))),
|
|
23
|
+
);
|
|
24
|
+
place.addAssign(sway.mul(firstMotion.w.mul(turn.min(1.0)).mul(0.5)));
|
|
25
|
+
});
|
|
26
|
+
If(secondMotion.y.notEqual(0.0), () => {
|
|
27
|
+
const angle = secondMotion.y.mul(age).toVar();
|
|
28
|
+
const cosAngle = cos(angle).toVar();
|
|
29
|
+
const offset = place.sub(center).toVar();
|
|
30
|
+
place.assign(center.add(offset.mul(cosAngle)).add(cross(axis, offset).mul(sin(angle))).add(axis.mul(dot(axis, offset)).mul(cosAngle.oneMinus())));
|
|
31
|
+
});
|
|
32
|
+
If(secondMotion.z.notEqual(0.0), () => {
|
|
33
|
+
const lifeShare = age.div(life).min(1.0).toVar();
|
|
34
|
+
const keep = secondMotion.z.greaterThan(0.0).select(secondMotion.z.mul(lifeShare).mul(lifeShare).oneMinus(), secondMotion.z.mul(lifeShare).oneMinus());
|
|
35
|
+
place.assign(center.add(place.sub(center).mul(keep)));
|
|
36
|
+
});
|
|
37
|
+
return place;
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/** Gravity and the wind, added in the world after everything the effect itself does. */
|
|
41
|
+
export const worldDrift = Fn(([age, firstMotion, wind]) => {
|
|
42
|
+
const drag = firstMotion.x;
|
|
43
|
+
const fallen = drag.greaterThan(1e-4).select(age.sub(travelSec(drag, age)).div(drag.max(1e-4)), age.mul(age).mul(0.5));
|
|
44
|
+
const drift = vec3(0.0, firstMotion.y.mul(fallen).negate(), 0.0).toVar();
|
|
45
|
+
If(firstMotion.z.greaterThan(0.0), () => {
|
|
46
|
+
const grip = drag.add(WIND_GRIP_PER_SEC);
|
|
47
|
+
drift.addAssign(wind.mul(age.sub(exp(grip.mul(age).negate()).oneMinus().div(grip)).mul(firstMotion.z)));
|
|
48
|
+
});
|
|
49
|
+
return drift;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
/** A place turned by a quaternion. */
|
|
53
|
+
export const turnBy = (turn, place) => place.add(cross(turn.xyz, cross(turn.xyz, place).add(place.mul(turn.w))).mul(2.0));
|
|
54
|
+
|
|
55
|
+
/** A value read off up to four keys spread evenly over a life. */
|
|
56
|
+
export const overLife = Fn(([keys, keyCount, lifeShare]) => {
|
|
57
|
+
const place = lifeShare.clamp().mul(keyCount.sub(1.0)).toVar();
|
|
58
|
+
const index = floor(place).min(keyCount.sub(2.0)).toVar();
|
|
59
|
+
const from = index.lessThan(0.5).select(keys.x, index.lessThan(1.5).select(keys.y, keys.z));
|
|
60
|
+
const to = index.lessThan(0.5).select(keys.y, index.lessThan(1.5).select(keys.z, keys.w));
|
|
61
|
+
return mix(from, to, place.sub(index));
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
/** A number from 0 to 1 that a seed and a salt always give the same. */
|
|
65
|
+
export const hash = (seed, salt) => fract(sin(seed.mul(91.3458).add(float(salt).mul(47.9898))).mul(43758.5453));
|
package/batches/ribbons.js
CHANGED
|
@@ -2,40 +2,36 @@
|
|
|
2
2
|
// (a blade's edge, a projectile), fading from its head to its tail. A beam is a strip between two places, straight
|
|
3
3
|
// or jagged like lightning. Both are rebuilt each frame from a handful of points, which costs little: there are
|
|
4
4
|
// never many, and each is a few dozen points.
|
|
5
|
-
import * as THREE from "three";
|
|
5
|
+
import * as THREE from "three/webgpu";
|
|
6
|
+
import { Fn, attribute, cameraProjectionMatrix, cameraViewMatrix, varyingProperty, vec4 } from "three/tsl";
|
|
6
7
|
import { hashNumbers, randomFrom, valueOverLife } from "../format.js";
|
|
7
|
-
import {
|
|
8
|
+
import { ONE_BLEND, batchMaterial, leaveNormals } from "./shared.js";
|
|
8
9
|
|
|
9
10
|
const FLOAT_COUNT_PER_CORNER = 9;
|
|
10
11
|
const MAXIMUM_CORNER_COUNT = 8192;
|
|
11
12
|
const MAXIMUM_POINT_COUNT = 64;
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
attribute
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
float middle = 1.0 - vAcross.x * vAcross.x;
|
|
35
|
-
float zaismaAddShare = vAcross.y;
|
|
36
|
-
gl_FragColor = vec4(vColor.rgb * (0.6 + 0.6 * middle), vColor.a * middle);
|
|
37
|
-
${BLEND_END_GLSL}
|
|
38
|
-
}`;
|
|
14
|
+
/** The ribbons' vertex and fragment stages, on a material. shared: createSharedUniforms' answer. */
|
|
15
|
+
function drawRibbons(material, shared) {
|
|
16
|
+
const place = attribute("position", "vec3");
|
|
17
|
+
const colorVarying = varyingProperty("vec4", "zaismaColor");
|
|
18
|
+
const acrossVarying = varyingProperty("vec2", "zaismaAcross");
|
|
19
|
+
const airVarying = varyingProperty("vec4", "zaismaAir");
|
|
20
|
+
|
|
21
|
+
material.vertexNode = Fn(() => {
|
|
22
|
+
colorVarying.assign(attribute("aColor", "vec4"));
|
|
23
|
+
acrossVarying.assign(attribute("aAcross", "vec2"));
|
|
24
|
+
airVarying.assign(shared.airAt(place));
|
|
25
|
+
return cameraProjectionMatrix.mul(cameraViewMatrix).mul(vec4(place, 1.0));
|
|
26
|
+
})();
|
|
27
|
+
|
|
28
|
+
material.fragmentNode = Fn(() => {
|
|
29
|
+
// Brightest along its middle, soft toward its edges.
|
|
30
|
+
const middle = acrossVarying.x.mul(acrossVarying.x).oneMinus();
|
|
31
|
+
return shared.blendEnd(vec4(colorVarying.rgb.mul(middle.mul(0.6).add(0.6)), colorVarying.a.mul(middle)), acrossVarying.y, airVarying);
|
|
32
|
+
})();
|
|
33
|
+
return material;
|
|
34
|
+
}
|
|
39
35
|
|
|
40
36
|
const head = new THREE.Vector3();
|
|
41
37
|
const edge = new THREE.Vector3();
|
|
@@ -51,7 +47,7 @@ function colorOverLength(colors, sharePct, into) {
|
|
|
51
47
|
}
|
|
52
48
|
|
|
53
49
|
/** Makes the batch. Answers { mesh, addRibbon, addBeam, update, liveCount, dispose }. */
|
|
54
|
-
export function createRibbonBatch() {
|
|
50
|
+
export function createRibbonBatch({ sharedUniforms }) {
|
|
55
51
|
const values = new Float32Array(MAXIMUM_CORNER_COUNT * FLOAT_COUNT_PER_CORNER);
|
|
56
52
|
const buffer = new THREE.InterleavedBuffer(values, FLOAT_COUNT_PER_CORNER);
|
|
57
53
|
buffer.setUsage(THREE.DynamicDrawUsage);
|
|
@@ -64,7 +60,7 @@ export function createRibbonBatch() {
|
|
|
64
60
|
index.setUsage(THREE.DynamicDrawUsage);
|
|
65
61
|
geometry.setIndex(index);
|
|
66
62
|
geometry.setDrawRange(0, 0);
|
|
67
|
-
const material =
|
|
63
|
+
const material = leaveNormals(drawRibbons(batchMaterial("zaisma_effect_ribbons", { side: THREE.DoubleSide, ...ONE_BLEND }), sharedUniforms));
|
|
68
64
|
const mesh = new THREE.Mesh(geometry, material);
|
|
69
65
|
mesh.name = "zaisma_effect_ribbons";
|
|
70
66
|
mesh.frustumCulled = false;
|