@zaisma/effects 1.1.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.
@@ -1,15 +1,16 @@
1
1
  // The sprite batch: every flat particle of every effect, in one draw call. A particle is four corners written once
2
- // as it's born; the vertex shader works out where it is from its age (motion.js), sizes, colours and lights it from
2
+ // as it's born; the vertex stage works out where it is from its age (motion-nodes.js), sizes, colours and lights it from
3
3
  // its element's row, and turns it to face the camera, its own motion or the ground. Smoke that covers and fire that
4
4
  // adds its light share one blend, so they layer right with each other.
5
5
  //
6
6
  // Each emitter holds one run of particles (a slab) and writes into it as a ring. What's drawn is an index list built
7
7
  // each frame from the live part of every slab, the farthest effect first.
8
- import * as THREE from "three";
9
- import { MOTION_GLSL } from "../motion.js";
8
+ import * as THREE from "three/webgpu";
9
+ import { Fn, If, attribute, bool, cameraPosition, cameraProjectionMatrix, cameraViewMatrix, cos, cross, dot, float, floor, fract, fwidth, int, length, max, min, mix, normalize, sin, smoothstep, sqrt, varyingProperty, vec2, vec3, vec4 } from "three/tsl";
10
10
  import { FILM_FRAME_COUNT } from "../shapes.js";
11
11
  import { createSlotPool } from "../tables.js";
12
- import { BLEND_END_GLSL, LANDING_LAYER_INDEX, ONE_BLEND, SHARED_VERTEX_GLSL, fogUniforms } from "./shared.js";
12
+ import { hash, localPlace, overLife, turnBy, worldDrift } from "./motion-nodes.js";
13
+ import { LANDING_LAYER_INDEX, ONE_BLEND, batchMaterial, leaveNormals } from "./shared.js";
13
14
 
14
15
  /** What each texel of an element's row holds. */
15
16
  export const ELEMENT_ROW = {
@@ -38,242 +39,231 @@ const CORNER_COUNT_PER_PARTICLE = 4;
38
39
  const FLOAT_COUNT_PER_PARTICLE = FLOAT_COUNT_PER_CORNER * CORNER_COUNT_PER_PARTICLE;
39
40
  const INDEX_COUNT_PER_PARTICLE = 6;
40
41
 
41
- const SPRITE_VERTEX = /* glsl */ `
42
- attribute vec4 aBirth;
43
- attribute vec4 aVelocity;
44
- attribute vec4 aSeed;
45
- ${SHARED_VERTEX_GLSL}
46
- ${MOTION_GLSL}
47
- varying vec2 vUv;
48
- varying vec2 vUvNext;
49
- varying float vFrameBlend;
50
- varying vec4 vColor;
51
- varying vec3 vLight;
52
- varying vec4 vStyle;
53
- varying vec4 vRound;
54
- varying vec2 vCorner;
55
- varying vec2 vTone;
56
- varying float vAbove;
57
- #include <fog_pars_vertex>
42
+ /** The sprites' vertex and fragment stages, on a material. shared: createSharedUniforms' answer. */
43
+ function drawSprites(material, shared) {
44
+ const birthAttribute = attribute("aBirth", "vec4");
45
+ const velocityAttribute = attribute("aVelocity", "vec4");
46
+ const seedAttribute = attribute("aSeed", "vec4");
47
+ // What the vertex stage hands the fragment stage. uvs: this frame's and the next's. light: rgb, and the blend
48
+ // between the two frames. style: add share, hard share, tone step count, outline share. round: the sun's way
49
+ // in view space, and the round share. shape: the corner, the core share and the shape's depth.
50
+ const uvsVarying = varyingProperty("vec4", "zaismaUvs");
51
+ const colorVarying = varyingProperty("vec4", "zaismaColor");
52
+ const lightVarying = varyingProperty("vec4", "zaismaLight");
53
+ const styleVarying = varyingProperty("vec4", "zaismaStyle");
54
+ const roundVarying = varyingProperty("vec4", "zaismaRound");
55
+ const shapeVarying = varyingProperty("vec4", "zaismaShape");
56
+ const airVarying = varyingProperty("vec4", "zaismaAir");
57
+ const aboveVarying = varyingProperty("float", "zaismaAbove");
58
+ const { element, play } = shared;
58
59
 
59
- vec4 zaismaGone() { return vec4(0.0, 0.0, 2.0, 1.0); }
60
+ material.vertexNode = Fn(() => {
61
+ const row = int(seedAttribute.y.add(0.5)).toVar();
62
+ const playRow = int(seedAttribute.z.add(0.5)).toVar();
63
+ const seed = seedAttribute.x;
64
+ const age = shared.uTime.sub(birthAttribute.w).toVar();
65
+ const life = velocityAttribute.w;
66
+ const lifeShare = age.div(life).toVar();
67
+ const firstMotion = element(row, ELEMENT_ROW.motionA).toVar();
68
+ const secondMotion = element(row, ELEMENT_ROW.motionB).toVar();
69
+ const facing = element(row, ELEMENT_ROW.facing).toVar();
70
+ const style = element(row, ELEMENT_ROW.style).toVar();
71
+ const playPlace = play(playRow, 0).toVar();
72
+ const playTurn = play(playRow, 1).toVar();
73
+ const playTint = play(playRow, 2).toVar();
74
+ const isStreak = facing.x.greaterThan(0.5).and(facing.x.lessThan(1.5)).toVar();
60
75
 
61
- void main() {
62
- int row = int(aSeed.y + 0.5);
63
- int playRow = int(aSeed.z + 0.5);
64
- float seed = aSeed.x;
65
- float age = uTime - aBirth.w;
66
- float life = aVelocity.w;
67
- float lifeShare = age / life;
68
- vec4 motionA = zaismaElement(row, ${ELEMENT_ROW.motionA});
69
- vec4 motionB = zaismaElement(row, ${ELEMENT_ROW.motionB});
70
- vec4 facing = zaismaElement(row, ${ELEMENT_ROW.facing});
71
- vec4 style = zaismaElement(row, ${ELEMENT_ROW.style});
72
- vec4 playPlace = zaismaPlay(playRow, 0);
73
- vec4 playTurn = zaismaPlay(playRow, 1);
74
- vec4 playTint = zaismaPlay(playRow, 2);
76
+ const world = vec3(0.0).toVar();
77
+ const velocity = vec3(0.0, 1.0, 0.0).toVar();
78
+ const edgeFade = float(1.0).toVar();
79
+ const isGone = bool(false).toVar();
80
+ If(style.z.greaterThan(0.5), () => {
81
+ // Weather: a fixed crowd in a box that follows the camera, each wrapped back in as it leaves.
82
+ const layer = int(style.z.sub(0.5)).toVar();
83
+ const box = shared.uWrapBox.element(layer).toVar();
84
+ const boxMin = shared.uWrapCenter.element(layer).sub(box.mul(0.5)).toVar();
85
+ isGone.assign(seed.greaterThanEqual(shared.uWrapShare.element(layer)));
86
+ If(layer.equal(int(LANDING_LAYER_INDEX)), () => {
87
+ // Rain landing: each lands somewhere new every time round, on whatever is under it.
88
+ const turn = shared.uWeatherTime.mul(shared.uWrapVelocity.element(layer).x).add(seed.mul(13.0)).toVar();
89
+ lifeShare.assign(fract(turn));
90
+ const round = floor(turn).toVar();
91
+ const landed = birthAttribute.xz.add(vec2(hash(seed, round), hash(seed.add(0.37), round)).mul(box.xz));
92
+ const spot = boxMin.xz.add(landed.sub(boxMin.xz).mod(box.xz)).toVar();
93
+ world.assign(vec3(spot.x, shared.roofHeightM(spot).add(0.03), spot.y));
94
+ }).Else(() => {
95
+ world.assign(boxMin.add(birthAttribute.xyz.add(shared.uWrapShift.element(layer)).sub(boxMin).mod(box)));
96
+ If(firstMotion.w.greaterThan(0.0), () => {
97
+ const turn = shared.uWeatherTime.mul(secondMotion.x).toVar();
98
+ const phase = seed.mul(6.2831853).toVar();
99
+ const swayX = sin(turn.mul(2.1).add(phase.mul(3.0))).add(sin(turn.mul(1.3).add(phase.mul(7.0))));
100
+ const swayZ = sin(turn.mul(1.9).add(phase.mul(13.0))).add(sin(turn.mul(1.1).add(phase.mul(17.0))));
101
+ world.addAssign(vec3(swayX, 0.0, swayZ).mul(firstMotion.w.mul(0.5)));
102
+ });
103
+ lifeShare.assign(0.5);
104
+ velocity.assign(shared.uWrapVelocity.element(layer));
105
+ If(world.y.lessThan(shared.roofHeightM(world.xz)), () => {
106
+ isGone.assign(true);
107
+ });
108
+ const fromEdge = min(world.sub(boxMin), boxMin.add(box).sub(world)).div(box).toVar();
109
+ edgeFade.assign(smoothstep(0.0, 0.1, fromEdge.x).mul(smoothstep(0.0, 0.1, fromEdge.z)));
110
+ });
111
+ age.assign(lifeShare);
112
+ }).Else(() => {
113
+ isGone.assign(age.lessThan(0.0).or(lifeShare.greaterThanEqual(1.0)));
114
+ const isAttached = secondMotion.w.greaterThan(0.5);
115
+ const center = isAttached.select(vec3(0.0), playPlace.xyz).toVar();
116
+ const axis = isAttached.select(vec3(0.0, 1.0, 0.0), turnBy(playTurn, vec3(0.0, 1.0, 0.0))).toVar();
117
+ const placeAt = (atAge) => {
118
+ const local = localPlace(birthAttribute.xyz, velocityAttribute.xyz, atAge, life, seed, firstMotion, secondMotion, center, axis).toVar();
119
+ return isAttached.select(playPlace.xyz.add(turnBy(playTurn, local)), local).add(worldDrift(atAge, firstMotion, shared.uWind));
120
+ };
121
+ world.assign(placeAt(age));
122
+ If(isStreak, () => {
123
+ const earlier = age.sub(0.02).max(0.0).toVar();
124
+ velocity.assign(world.sub(placeAt(earlier)).div(age.sub(earlier).max(1e-4)));
125
+ });
126
+ });
75
127
 
76
- vec3 world;
77
- vec3 velocity = vec3(0.0, 1.0, 0.0);
78
- float edgeFade = 1.0;
79
- bool isGone = false;
80
- if (style.z > 0.5) {
81
- // Weather: a fixed crowd in a box that follows the camera, each wrapped back in as it leaves.
82
- int layer = int(style.z - 0.5);
83
- vec3 box = uWrapBox[layer];
84
- vec3 boxMin = uWrapCenter[layer] - box * 0.5;
85
- isGone = seed >= uWrapShare[layer];
86
- if (layer == ${LANDING_LAYER_INDEX}) {
87
- // Rain landing: each lands somewhere new every time round, on whatever is under it.
88
- float turn = uWeatherTime * uWrapVelocity[layer].x + seed * 13.0;
89
- lifeShare = fract(turn);
90
- float round = floor(turn);
91
- vec2 landed = aBirth.xz + vec2(zaismaHash(seed, round), zaismaHash(seed + 0.37, round)) * box.xz;
92
- world.xz = boxMin.xz + mod(landed - boxMin.xz, box.xz);
93
- world.y = zaismaRoofHeightM(world.xz) + 0.03;
94
- } else {
95
- world = boxMin + mod(aBirth.xyz + uWrapShift[layer] - boxMin, box);
96
- if (motionA.w > 0.0) {
97
- float turn = uWeatherTime * motionB.x;
98
- float phase = seed * 6.2831853;
99
- world.xz += motionA.w * 0.5 * vec2(sin(turn * 2.1 + phase * 3.0) + sin(turn * 1.3 + phase * 7.0), sin(turn * 1.9 + phase * 13.0) + sin(turn * 1.1 + phase * 17.0));
100
- }
101
- lifeShare = 0.5;
102
- velocity = uWrapVelocity[layer];
103
- if (world.y < zaismaRoofHeightM(world.xz)) isGone = true;
104
- vec3 fromEdge = min(world - boxMin, boxMin + box - world) / box;
105
- edgeFade = smoothstep(0.0, 0.1, fromEdge.x) * smoothstep(0.0, 0.1, fromEdge.z);
106
- }
107
- age = lifeShare;
108
- } else {
109
- isGone = age < 0.0 || lifeShare >= 1.0;
110
- bool isAttached = motionB.w > 0.5;
111
- vec3 center = isAttached ? vec3(0.0) : playPlace.xyz;
112
- vec3 axis = isAttached ? vec3(0.0, 1.0, 0.0) : zaismaTurn(playTurn, vec3(0.0, 1.0, 0.0));
113
- vec3 local = zaismaLocalPlace(aBirth.xyz, aVelocity.xyz, age, life, seed, motionA, motionB, center, axis);
114
- world = (isAttached ? playPlace.xyz + zaismaTurn(playTurn, local) : local) + zaismaWorldDrift(age, motionA, uWind);
115
- if (facing.x > 0.5 && facing.x < 1.5) {
116
- float earlier = max(age - 0.02, 0.0);
117
- vec3 localEarlier = zaismaLocalPlace(aBirth.xyz, aVelocity.xyz, earlier, life, seed, motionA, motionB, center, axis);
118
- vec3 worldEarlier = (isAttached ? playPlace.xyz + zaismaTurn(playTurn, localEarlier) : localEarlier) + zaismaWorldDrift(earlier, motionA, uWind);
119
- velocity = (world - worldEarlier) / max(age - earlier, 1e-4);
120
- }
121
- }
122
- if (isGone) {
123
- gl_Position = zaismaGone();
124
- return;
125
- }
126
-
127
- vec4 keyCounts = zaismaElement(row, ${ELEMENT_ROW.keyCounts});
128
- float size = zaismaOverLife(zaismaElement(row, ${ELEMENT_ROW.sizeKeys}), keyCounts.x, lifeShare);
129
- size *= playPlace.w * (1.0 + keyCounts.w * (zaismaHash(seed, 1.0) * 2.0 - 1.0));
130
- float opacity = zaismaOverLife(zaismaElement(row, ${ELEMENT_ROW.opacityKeys}), keyCounts.y, lifeShare) * playTint.a * edgeFade;
128
+ // Not drawn: a place no screen shows.
129
+ const clip = vec4(0.0, 0.0, 2.0, 1.0).toVar();
130
+ If(isGone.not(), () => {
131
+ const keyCounts = element(row, ELEMENT_ROW.keyCounts).toVar();
132
+ 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)).toVar();
133
+ const opacity = overLife(element(row, ELEMENT_ROW.opacityKeys), keyCounts.y, lifeShare).mul(playTint.a).mul(edgeFade).toVar();
131
134
 
132
- // An effect that stands on a surface keeps its shapes on it: one heading into the ground slides along it instead,
133
- // as a blast spreads over the floor. Only near the effect: farther off, the surface may have ended.
134
- vec4 playSurface = zaismaPlay(playRow, 3);
135
- vec3 fromPlay = world - playPlace.xyz;
136
- bool isOnSurface = facing.x < 1.5 && dot(playSurface.xyz, playSurface.xyz) > 0.5 && dot(fromPlay, fromPlay) < 36.0 * playPlace.w * playPlace.w;
137
- if (isOnSurface) world += playSurface.xyz * max(size * 0.22 - (dot(playSurface.xyz, world) - playSurface.w), 0.0);
138
-
139
- vec4 mvPosition = viewMatrix * vec4(world, 1.0);
140
- float distanceM = max(-mvPosition.z, 0.001);
141
- // Too small to see, or right on the lens: not drawn.
142
- bool isStreak = facing.x > 0.5 && facing.x < 1.5;
143
- if (uPxPerM > 0.0 && !isStreak && size * uPxPerM / distanceM < 0.35) {
144
- gl_Position = zaismaGone();
145
- return;
146
- }
147
- // A streak far off (rain, sparks) is thinner than the screen can draw: it keeps a width that shows, and is fainter for it.
148
- float streakWidthM = isStreak && uPxPerM > 0.0 ? max(size, 1.3 * distanceM / uPxPerM) : size;
149
- opacity *= max(size / streakWidthM, 0.3);
150
- opacity *= clamp((distanceM - 0.08) / (size * 0.5 + 0.15), 0.0, 1.0);
135
+ // An effect that stands on a surface keeps its shapes on it: one heading into the ground slides along it
136
+ // instead, as a blast spreads over the floor. Only near the effect: farther off, the surface may have ended.
137
+ const playSurface = play(playRow, 3).toVar();
138
+ const fromPlay = world.sub(playPlace.xyz).toVar();
139
+ const isOnSurface = facing.x.lessThan(1.5).and(dot(playSurface.xyz, playSurface.xyz).greaterThan(0.5)).and(dot(fromPlay, fromPlay).lessThan(playPlace.w.mul(playPlace.w).mul(36.0))).toVar();
140
+ If(isOnSurface, () => {
141
+ world.addAssign(playSurface.xyz.mul(particleSize.mul(0.22).sub(dot(playSurface.xyz, world).sub(playSurface.w)).max(0.0)));
142
+ });
151
143
 
152
- float cornerIndex = aSeed.w;
153
- vec2 corner = vec2(mod(cornerIndex, 2.0), floor(cornerIndex * 0.5 + 0.01)) * 2.0 - 1.0;
154
- vec2 across = corner;
155
- if (facing.z != 0.0) {
156
- // Below 0 is a shape that stands upright (a flame, a drop) and only sways; above it each turns its own way.
157
- float angle = facing.z < 0.0 ? 0.3 * sin(age * 5.0 + seed * 6.2831853) : zaismaHash(seed, 2.0) * 6.2831853 + facing.z * age * (zaismaHash(seed, 3.0) < 0.5 ? -1.0 : 1.0);
158
- float cosAngle = cos(angle);
159
- float sinAngle = sin(angle);
160
- across = vec2(corner.x * cosAngle - corner.y * sinAngle, corner.x * sinAngle + corner.y * cosAngle);
161
- }
162
- vec3 facingWay;
163
- if (facing.x < 0.5) {
164
- mvPosition.xy += across * size * 0.5;
165
- facingWay = normalize(cameraPosition - world);
166
- } else if (facing.x < 1.5) {
167
- // Stretched along its own motion, as long as the way it travels in its streak time.
168
- vec3 viewVelocity = (viewMatrix * vec4(velocity, 0.0)).xyz;
169
- float speed = length(viewVelocity);
170
- vec2 along = speed > 1e-4 ? viewVelocity.xy / max(length(viewVelocity.xy), 1e-4) : vec2(0.0, 1.0);
171
- float lengthM = max(size, length(viewVelocity.xy) * facing.y);
172
- mvPosition.xy += along * corner.y * lengthM * 0.5 + vec2(-along.y, along.x) * corner.x * streakWidthM * 0.5;
173
- facingWay = normalize(cameraPosition - world);
174
- } else {
175
- // Flat on the ground, or flat across the effect's direction.
176
- vec3 normal = facing.x < 2.5 ? vec3(0.0, 1.0, 0.0) : zaismaTurn(playTurn, vec3(0.0, 1.0, 0.0));
177
- vec3 side = normalize(abs(normal.y) < 0.99 ? cross(vec3(0.0, 1.0, 0.0), normal) : vec3(1.0, 0.0, 0.0));
178
- vec3 forward = cross(normal, side);
179
- world += (side * across.x + forward * across.y) * size * 0.5 + normal * 0.02;
180
- mvPosition = viewMatrix * vec4(world, 1.0);
181
- facingWay = normal;
182
- }
183
- gl_Position = projectionMatrix * mvPosition;
184
- // How far this corner is above the surface its effect stands on, in shares of the band a soft shape fades over.
185
- vAbove = 1.0;
186
- if (isOnSurface) {
187
- vec3 surfaceWay = (viewMatrix * vec4(playSurface.xyz, 0.0)).xyz;
188
- vec3 surfacePoint = (viewMatrix * vec4(playSurface.xyz * playSurface.w, 1.0)).xyz;
189
- vAbove = dot(surfaceWay, mvPosition.xyz - surfacePoint) / max(size * 0.3, 0.03);
190
- }
191
- if (facing.x < 1.5) {
192
- // Its depth is taken half its size nearer, so the ground it stands on doesn't cut a line through it.
193
- vec4 nearer = projectionMatrix * vec4(mvPosition.xy, mvPosition.z + min(size * 0.5, distanceM * 0.5), 1.0);
194
- gl_Position.z = nearer.z / nearer.w * gl_Position.w;
195
- }
144
+ const viewPlace = cameraViewMatrix.mul(vec4(world, 1.0)).toVar();
145
+ const distanceM = viewPlace.z.negate().max(0.001).toVar();
146
+ // Too small to see, or right on the lens: not drawn.
147
+ const isTooSmall = shared.uPxPerM.greaterThan(0.0).and(isStreak.not()).and(particleSize.mul(shared.uPxPerM).div(distanceM).lessThan(0.35));
148
+ If(isTooSmall.not(), () => {
149
+ // A streak far off (rain, sparks) is thinner than the screen can draw: it keeps a width that shows, and is fainter for it.
150
+ const streakWidthM = isStreak.and(shared.uPxPerM.greaterThan(0.0)).select(max(particleSize, distanceM.mul(1.3).div(shared.uPxPerM.max(1e-6))), particleSize).toVar();
151
+ opacity.mulAssign(particleSize.div(streakWidthM).max(0.3));
152
+ opacity.mulAssign(distanceM.sub(0.08).div(particleSize.mul(0.5).add(0.15)).clamp());
196
153
 
197
- // The shape's cell, or two frames of its moving picture to blend between.
198
- vec4 cell = zaismaElement(row, ${ELEMENT_ROW.cell});
199
- vec2 inCell = (corner * 0.5 + 0.5) * 0.98 + 0.01;
200
- vFrameBlend = 0.0;
201
- vUv = cell.xy + inCell * cell.z;
202
- vUvNext = vUv;
203
- if (cell.w > 0.5) {
204
- float frame = clamp(lifeShare, 0.0, 0.999) * (cell.w - 1.0);
205
- float first = floor(frame);
206
- float second = min(first + 1.0, cell.w - 1.0);
207
- vUv = cell.xy + (vec2(mod(first, 4.0), floor(first / 4.0)) + inCell) * cell.z;
208
- vUvNext = cell.xy + (vec2(mod(second, 4.0), floor(second / 4.0)) + inCell) * cell.z;
209
- vFrameBlend = frame - first;
210
- }
154
+ const cornerIndex = seedAttribute.w;
155
+ const corner = vec2(cornerIndex.mod(2.0), floor(cornerIndex.mul(0.5).add(0.01))).mul(2.0).sub(1.0).toVar();
156
+ const across = vec2(corner).toVar();
157
+ If(facing.z.notEqual(0.0), () => {
158
+ // Below 0 is a shape that stands upright (a flame, a drop) and only sways; above it each turns its own way.
159
+ const angle = facing.z.lessThan(0.0).select(sin(age.mul(5.0).add(seed.mul(6.2831853))).mul(0.3), hash(seed, 2.0).mul(6.2831853).add(facing.z.mul(age).mul(hash(seed, 3.0).lessThan(0.5).select(float(-1.0), float(1.0))))).toVar();
160
+ const cosAngle = cos(angle).toVar();
161
+ const sinAngle = sin(angle).toVar();
162
+ across.assign(vec2(corner.x.mul(cosAngle).sub(corner.y.mul(sinAngle)), corner.x.mul(sinAngle).add(corner.y.mul(cosAngle))));
163
+ });
164
+ const facingWay = vec3(0.0, 1.0, 0.0).toVar();
165
+ If(facing.x.lessThan(0.5), () => {
166
+ viewPlace.assign(vec4(viewPlace.xy.add(across.mul(particleSize).mul(0.5)), viewPlace.zw));
167
+ facingWay.assign(normalize(cameraPosition.sub(world)));
168
+ })
169
+ .ElseIf(facing.x.lessThan(1.5), () => {
170
+ // Stretched along its own motion, as long as the way it travels in its streak time.
171
+ const viewVelocity = cameraViewMatrix.mul(vec4(velocity, 0.0)).xyz.toVar();
172
+ const flatSpeed = length(viewVelocity.xy).toVar();
173
+ const along = length(viewVelocity).greaterThan(1e-4).select(viewVelocity.xy.div(flatSpeed.max(1e-4)), vec2(0.0, 1.0)).toVar();
174
+ const lengthM = max(particleSize, flatSpeed.mul(facing.y));
175
+ viewPlace.assign(vec4(viewPlace.xy.add(along.mul(corner.y).mul(lengthM).mul(0.5)).add(vec2(along.y.negate(), along.x).mul(corner.x).mul(streakWidthM).mul(0.5)), viewPlace.zw));
176
+ facingWay.assign(normalize(cameraPosition.sub(world)));
177
+ })
178
+ .Else(() => {
179
+ // Flat on the ground, or flat across the effect's direction.
180
+ const normal = facing.x.lessThan(2.5).select(vec3(0.0, 1.0, 0.0), turnBy(playTurn, vec3(0.0, 1.0, 0.0))).toVar();
181
+ 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();
182
+ const forward = cross(normal, side);
183
+ world.addAssign(side.mul(across.x).add(forward.mul(across.y)).mul(particleSize).mul(0.5).add(normal.mul(0.02)));
184
+ viewPlace.assign(cameraViewMatrix.mul(vec4(world, 1.0)));
185
+ facingWay.assign(normal);
186
+ });
187
+ const position = cameraProjectionMatrix.mul(viewPlace).toVar();
188
+ // How far this corner is above the surface its effect stands on, in shares of the band a soft shape fades over.
189
+ const above = float(1.0).toVar();
190
+ If(isOnSurface, () => {
191
+ const surfaceWay = cameraViewMatrix.mul(vec4(playSurface.xyz, 0.0)).xyz;
192
+ const surfacePoint = cameraViewMatrix.mul(vec4(playSurface.xyz.mul(playSurface.w), 1.0)).xyz;
193
+ above.assign(dot(surfaceWay, viewPlace.xyz.sub(surfacePoint)).div(particleSize.mul(0.3).max(0.03)));
194
+ });
195
+ If(facing.x.lessThan(1.5), () => {
196
+ // Its depth is taken half its size nearer, so the ground it stands on doesn't cut a line through it.
197
+ const nearer = cameraProjectionMatrix.mul(vec4(viewPlace.xy, viewPlace.z.add(min(particleSize.mul(0.5), distanceM.mul(0.5))), 1.0)).toVar();
198
+ position.assign(vec4(position.xy, nearer.z.div(nearer.w).mul(position.w), position.w));
199
+ });
211
200
 
212
- // Its colour through its life, from up to four keys.
213
- float colorPlace = clamp(lifeShare, 0.0, 1.0) * (keyCounts.z - 1.0);
214
- float colorIndex = min(floor(colorPlace), max(keyCounts.z - 2.0, 0.0));
215
- vec4 colorFrom = zaismaElement(row, ${ELEMENT_ROW.colorKeys} + int(colorIndex));
216
- vec4 colorTo = zaismaElement(row, ${ELEMENT_ROW.colorKeys} + int(min(colorIndex + 1.0, keyCounts.z - 1.0)));
217
- vec4 glowKey = zaismaElement(row, ${ELEMENT_ROW.colorKeys});
218
- vec4 addKey = zaismaElement(row, ${ELEMENT_ROW.colorKeys + 1});
219
- vec4 litKey = zaismaElement(row, ${ELEMENT_ROW.colorKeys + 2});
220
- vec4 toneKey = zaismaElement(row, ${ELEMENT_ROW.colorKeys + 3});
221
- vColor = vec4(mix(colorFrom.rgb, colorTo.rgb, colorPlace - colorIndex) * playTint.rgb * glowKey.a, opacity);
222
- vLight = mix(vec3(1.0), zaismaLightAt(world, facingWay, 0.7), litKey.a);
223
- vStyle = vec4(addKey.a, style.x, toneKey.a, style.y);
224
- vRound = vec4(normalize((viewMatrix * vec4(uSunDirection, 0.0)).xyz), facing.w * litKey.a);
225
- vCorner = corner;
226
- vTone = zaismaElement(row, ${ELEMENT_ROW.tone}).xy;
227
- #include <fog_vertex>
228
- }`;
201
+ // The shape's cell, or two frames of its moving picture to blend between.
202
+ const cell = element(row, ELEMENT_ROW.cell).toVar();
203
+ const inCell = corner.mul(0.5).add(0.5).mul(0.98).add(0.01).toVar();
204
+ const uvNow = cell.xy.add(inCell.mul(cell.z)).toVar();
205
+ const uvNext = vec2(uvNow).toVar();
206
+ const frameBlend = float(0.0).toVar();
207
+ If(cell.w.greaterThan(0.5), () => {
208
+ const frame = lifeShare.clamp(0.0, 0.999).mul(cell.w.sub(1.0)).toVar();
209
+ const first = floor(frame).toVar();
210
+ const second = min(first.add(1.0), cell.w.sub(1.0)).toVar();
211
+ uvNow.assign(cell.xy.add(vec2(first.mod(4.0), floor(first.div(4.0))).add(inCell).mul(cell.z)));
212
+ uvNext.assign(cell.xy.add(vec2(second.mod(4.0), floor(second.div(4.0))).add(inCell).mul(cell.z)));
213
+ frameBlend.assign(frame.sub(first));
214
+ });
229
215
 
230
- const SPRITE_FRAGMENT = /* glsl */ `
231
- uniform sampler2D uAtlas;
232
- varying vec2 vUv;
233
- varying vec2 vUvNext;
234
- varying float vFrameBlend;
235
- varying vec4 vColor;
236
- varying vec3 vLight;
237
- varying vec4 vStyle;
238
- varying vec4 vRound;
239
- varying vec2 vCorner;
240
- varying vec2 vTone;
241
- varying float vAbove;
242
- #include <common>
243
- #include <fog_pars_fragment>
216
+ // Its colour through its life, from up to four keys.
217
+ const colorPlace = lifeShare.clamp().mul(keyCounts.z.sub(1.0)).toVar();
218
+ const colorIndex = min(floor(colorPlace), keyCounts.z.sub(2.0).max(0.0)).toVar();
219
+ const colorFrom = element(row, int(colorIndex).add(int(ELEMENT_ROW.colorKeys)));
220
+ const colorTo = element(row, int(min(colorIndex.add(1.0), keyCounts.z.sub(1.0))).add(int(ELEMENT_ROW.colorKeys)));
221
+ const glowKey = element(row, ELEMENT_ROW.colorKeys);
222
+ const addKey = element(row, ELEMENT_ROW.colorKeys + 1);
223
+ const litKey = element(row, ELEMENT_ROW.colorKeys + 2).toVar();
224
+ const toneKey = element(row, ELEMENT_ROW.colorKeys + 3);
225
+ colorVarying.assign(vec4(mix(colorFrom.rgb, colorTo.rgb, colorPlace.sub(colorIndex)).mul(playTint.rgb).mul(glowKey.a), opacity));
226
+ lightVarying.assign(vec4(mix(vec3(1.0), shared.lightAt(world, facingWay, float(0.7)), litKey.a), frameBlend));
227
+ styleVarying.assign(vec4(addKey.a, style.x, toneKey.a, style.y));
228
+ roundVarying.assign(vec4(normalize(cameraViewMatrix.mul(vec4(shared.uSunDirection, 0.0)).xyz), facing.w.mul(litKey.a)));
229
+ shapeVarying.assign(vec4(corner, element(row, ELEMENT_ROW.tone).xy));
230
+ uvsVarying.assign(vec4(uvNow, uvNext));
231
+ airVarying.assign(shared.airAt(world));
232
+ aboveVarying.assign(above);
233
+ clip.assign(position);
234
+ });
235
+ });
236
+ return clip;
237
+ })();
244
238
 
245
- void main() {
246
- vec4 held = texture2D(uAtlas, vUv);
247
- if (vFrameBlend > 0.001) held = mix(held, texture2D(uAtlas, vUvNext), vFrameBlend);
248
- // A crisp edge from how far inside the shape this point is, at any size on screen.
249
- float edgeWidth = max(fwidth(held.r) * 0.75, 0.004);
250
- // A hard shape is never see-through: as its effect fades it, its edge closes in until nothing is left.
251
- float fadeLeft = 1.0 - vColor.a;
252
- float edge = 0.5 + (vTone.y - 0.5) * 0.97 * vStyle.y * fadeLeft * fadeLeft;
253
- float cut = smoothstep(edge - edgeWidth, edge + edgeWidth, held.r);
254
- float solid = mix(held.g, cut, vStyle.y);
255
- float tone = held.b;
256
- if (vStyle.z > 0.5) tone = (floor(tone * vStyle.z * 0.999) + 0.5) / vStyle.z;
257
- vec3 color = vColor.rgb * (0.62 + 0.55 * tone);
258
- // A hot core: the middle of the shape lightens toward white, in the look's tones.
259
- color = mix(color, mix(color, vec3(max(max(color.r, color.g), color.b)), vec3(0.85, 0.65, 0.38)) * 1.15, tone * tone * vTone.x);
260
- if (vStyle.w > 0.001) {
261
- float outer = smoothstep(edge - edgeWidth, edge + edgeWidth, held.r + vStyle.w * 0.1);
262
- color = mix(color * 0.16, color, cut / max(outer, 0.001));
263
- solid = max(solid, outer);
264
- }
265
- // Shaded as a ball, bright toward the sun.
266
- if (vRound.w > 0.001) {
267
- float fromMiddle = min(dot(vCorner, vCorner), 1.0);
268
- vec3 normal = vec3(vCorner, sqrt(1.0 - fromMiddle));
269
- color *= mix(1.0, clamp(dot(normal, vRound.xyz) * 0.55 + 0.75, 0.3, 1.35), vRound.w);
270
- }
271
- float zaismaAddShare = vStyle.x;
272
- // A soft shape fades into the surface it stands on; a hard one is cut by it, as a drawn one would be.
273
- solid *= mix(clamp(vAbove, 0.0, 1.0), 1.0, vStyle.y);
274
- gl_FragColor = vec4(color * vLight, solid * mix(vColor.a, 1.0, vStyle.y));
275
- ${BLEND_END_GLSL}
276
- }`;
239
+ material.fragmentNode = Fn(() => {
240
+ const hard = styleVarying.y;
241
+ const held = mix(shared.uAtlas.sample(uvsVarying.xy), shared.uAtlas.sample(uvsVarying.zw), lightVarying.w).toVar();
242
+ // A crisp edge from how far inside the shape this point is, at any size on screen.
243
+ const edgeWidth = fwidth(held.r).mul(0.75).max(0.004).toVar();
244
+ // A hard shape is never see-through: as its effect fades it, its edge closes in until nothing is left.
245
+ const fadeLeft = colorVarying.a.oneMinus();
246
+ const edge = shapeVarying.w.sub(0.5).mul(0.97).mul(hard).mul(fadeLeft).mul(fadeLeft).add(0.5).toVar();
247
+ const cut = smoothstep(edge.sub(edgeWidth), edge.add(edgeWidth), held.r).toVar();
248
+ const stepCount = styleVarying.z;
249
+ const tone = stepCount.greaterThan(0.5).select(floor(held.b.mul(stepCount).mul(0.999)).add(0.5).div(stepCount.max(1.0)), held.b).toVar();
250
+ const flat = colorVarying.rgb.mul(tone.mul(0.55).add(0.62)).toVar();
251
+ // A hot core: the middle of the shape lightens toward white, in the look's tones.
252
+ const cored = mix(flat, mix(flat, vec3(max(max(flat.r, flat.g), flat.b)), vec3(0.85, 0.65, 0.38)).mul(1.15), tone.mul(tone).mul(shapeVarying.z)).toVar();
253
+ const hasOutline = styleVarying.w.greaterThan(0.001);
254
+ const outer = smoothstep(edge.sub(edgeWidth), edge.add(edgeWidth), held.r.add(styleVarying.w.mul(0.1))).toVar();
255
+ const outlined = hasOutline.select(mix(cored.mul(0.16), cored, cut.div(outer.max(0.001))), cored);
256
+ const covered = mix(held.g, cut, hard);
257
+ // Shaded as a ball, bright toward the sun.
258
+ const fromMiddle = dot(shapeVarying.xy, shapeVarying.xy).min(1.0);
259
+ const ballNormal = vec3(shapeVarying.xy, sqrt(fromMiddle.oneMinus()));
260
+ const rounded = outlined.mul(mix(float(1.0), dot(ballNormal, roundVarying.xyz).mul(0.55).add(0.75).clamp(0.3, 1.35), roundVarying.w));
261
+ // A soft shape fades into the surface it stands on; a hard one is cut by it, as a drawn one would be.
262
+ const solid = hasOutline.select(max(covered, outer), covered).mul(mix(aboveVarying.clamp(), float(1.0), hard));
263
+ return shared.blendEnd(vec4(rounded.mul(lightVarying.xyz), solid.mul(mix(colorVarying.a, float(1.0), hard))), styleVarying.x, airVarying);
264
+ })();
265
+ return material;
266
+ }
277
267
 
278
268
  /**
279
269
  * Makes the batch. shared: the uniforms every batch reads. Answers { mesh, takeSlab, giveSlab, write, update,
@@ -293,15 +283,7 @@ export function createSpriteBatch({ particleCount, sharedUniforms }) {
293
283
  geometry.setIndex(index);
294
284
  geometry.setDrawRange(0, 0);
295
285
 
296
- const material = new THREE.ShaderMaterial({
297
- name: "zaisma_effect_sprites",
298
- vertexShader: SPRITE_VERTEX,
299
- fragmentShader: SPRITE_FRAGMENT,
300
- uniforms: { ...fogUniforms(), ...sharedUniforms },
301
- fog: true,
302
- side: THREE.DoubleSide,
303
- ...ONE_BLEND,
304
- });
286
+ const material = leaveNormals(drawSprites(batchMaterial("zaisma_effect_sprites", { side: THREE.DoubleSide, ...ONE_BLEND }), sharedUniforms));
305
287
  const mesh = new THREE.Mesh(geometry, material);
306
288
  mesh.name = "zaisma_effect_sprites";
307
289
  mesh.frustumCulled = false;