reze-engine 0.68.3 → 0.68.4

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.
Files changed (55) hide show
  1. package/dist/_dbg-loader.mjs +1100 -0
  2. package/dist/engine.d.ts +17 -0
  3. package/dist/engine.d.ts.map +1 -1
  4. package/dist/engine.js +45 -5
  5. package/dist/graph/slots.d.ts.map +1 -1
  6. package/dist/graph/slots.js +12 -4
  7. package/dist/index.d.ts +1 -0
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/jelly-eyes.d.ts +30 -0
  10. package/dist/jelly-eyes.d.ts.map +1 -0
  11. package/dist/jelly-eyes.js +340 -0
  12. package/dist/shaders/materials/common.d.ts.map +1 -1
  13. package/dist/shaders/materials/common.js +121 -2
  14. package/package.json +1 -1
  15. package/src/engine.ts +47 -5
  16. package/src/graph/slots.ts +13 -4
  17. package/src/index.ts +1 -0
  18. package/src/jelly-eyes.ts +390 -0
  19. package/src/shaders/materials/common.ts +121 -2
  20. package/dist/graph/presets/pool_floor.d.ts +0 -3
  21. package/dist/graph/presets/pool_floor.d.ts.map +0 -1
  22. package/dist/graph/presets/pool_floor.js +0 -42
  23. package/dist/graph/presets/water.d.ts +0 -3
  24. package/dist/graph/presets/water.d.ts.map +0 -1
  25. package/dist/graph/presets/water.js +0 -31
  26. package/dist/physics/autofit.d.ts +0 -147
  27. package/dist/physics/autofit.d.ts.map +0 -1
  28. package/dist/physics/autofit.js +0 -501
  29. package/dist/shaders/agx-lut.d.ts +0 -11
  30. package/dist/shaders/agx-lut.d.ts.map +0 -1
  31. package/dist/shaders/agx-lut.js +0 -29
  32. package/dist/shaders/dfg_lut.d.ts +0 -3
  33. package/dist/shaders/dfg_lut.d.ts.map +0 -1
  34. package/dist/shaders/dfg_lut.js +0 -129
  35. package/dist/shaders/ltc_mag_lut.d.ts +0 -3
  36. package/dist/shaders/ltc_mag_lut.d.ts.map +0 -1
  37. package/dist/shaders/ltc_mag_lut.js +0 -1033
  38. package/dist/shaders/passes/field-blit.d.ts +0 -26
  39. package/dist/shaders/passes/field-blit.d.ts.map +0 -1
  40. package/dist/shaders/passes/field-blit.js +0 -65
  41. package/dist/shaders/passes/ghosts.d.ts +0 -22
  42. package/dist/shaders/passes/ghosts.d.ts.map +0 -1
  43. package/dist/shaders/passes/ghosts.js +0 -75
  44. package/dist/shaders/passes/ground-noise.d.ts +0 -7
  45. package/dist/shaders/passes/ground-noise.d.ts.map +0 -1
  46. package/dist/shaders/passes/ground-noise.js +0 -88
  47. package/dist/shaders/passes/sim.d.ts +0 -34
  48. package/dist/shaders/passes/sim.d.ts.map +0 -1
  49. package/dist/shaders/passes/sim.js +0 -169
  50. package/dist/shaders/score-api.d.ts +0 -10
  51. package/dist/shaders/score-api.d.ts.map +0 -1
  52. package/dist/shaders/score-api.js +0 -114
  53. package/dist/shaders/shadow-api.d.ts +0 -29
  54. package/dist/shaders/shadow-api.d.ts.map +0 -1
  55. package/dist/shaders/shadow-api.js +0 -112
@@ -0,0 +1,390 @@
1
+ // Jelly eyes — the MMD 果冻眼 / Live2D highlight wobble, for any PMX.
2
+ //
3
+ // What every working version of the effect has in common, whether it is keyed
4
+ // by hand in MMD, a pendulum in Live2D, or a jiggle bone in Blender: an INPUT
5
+ // (a blink, a turn), something that REMEMBERS the last frame (a spring), and a
6
+ // cheap OUTPUT. Here the output is the iris's SCALE, in place: it flattens and
7
+ // bulges as a lid moves (opening too), swells as her head turns or moves, and
8
+ // wobbles back. It never slides — tried, it read as the iris coming loose. The
9
+ // inputs are her blinks and her head, never her eyes' own turn: eye tracking
10
+ // turns the eyes after the camera, and the camera must not shake the jelly.
11
+ //
12
+ // The springs run on the CPU, stepped by the engine's frame delta — the same
13
+ // exact delta renderFrame() hands an export, so a blink bounces identically on
14
+ // screen and in the file. Their state rides in the model's ObjectLight
15
+ // (jelly[0..5], see materials/common.ts): the vertex shader scales the iris —
16
+ // the vertices the eye bones carry — about its centre, and the eye render class
17
+ // adds the gem look after its graph (glints, light through the iris, deeper
18
+ // colour), so every eye graph becomes jelly without knowing it.
19
+
20
+ import type { Model } from "./model"
21
+
22
+ export interface JellyEyeOptions {
23
+ /** How much the eyes give: 0 still, 1 the intended bounce, up to 2. Default 1. */
24
+ strength?: number
25
+ }
26
+
27
+ // The closing morphs, by the names MMD, VRoid and the usual converters give
28
+ // them. Group morphs (a smile that closes the eyes) resolve into these through
29
+ // the effective weights, so only the leaves need naming.
30
+ const BOTH = ["まばたき", "笑い", "はぅ", "blink", "Blink", "eye_close", "Fcl_EYE_Close"]
31
+ const LEFT = ["ウィンク", "ウィンク2", "ウィンク", "ウィンク2", "wink", "Wink", "blink_L", "Fcl_EYE_Close_L"]
32
+ const RIGHT = ["ウィンク右", "ウィンク2右", "ウィンク右", "ウィンク2右", "wink_R", "Wink_R", "blink_R", "Fcl_EYE_Close_R"]
33
+
34
+ // The squash: how far a closed eye presses the picture, and the spring that
35
+ // follows it — underdamped, so the eye opening overshoots into a stretch and
36
+ // settles in two or three wobbles, the "Duang" the effect is named for.
37
+ // Only a CHANGE of closure squashes: the eye's closure less a slow follower of
38
+ // it, so a blink squeezes then bounces, while a held half-close (a smile)
39
+ // leaves the iris at rest instead of squashed for as long as she smiles.
40
+ const SQUASH_CLOSED = 0.35
41
+ const SQUASH_HZ = 1.4
42
+ const SQUASH_ZETA = 0.22
43
+ const CLOSURE_FOLLOW_S = 0.35
44
+ // The lag: the picture trails the eye's turn and catches up past it. Radians of
45
+ // lag to eye radii of offset; the head's own turn counts for a third, so a
46
+ // sudden stop of the head wobbles the eyes without a dance keeping them
47
+ // sloshing.
48
+ const LAG_HZ = 3.5
49
+ const LAG_ZETA = 0.4
50
+ // The lag is an angle: how far the iris trails the eye's own turn, as a share
51
+ // of the lag in look, capped at a quarter of eye tracking's default 20° reach —
52
+ // a wobble on the turn, never a second turn. It becomes iris radii through the
53
+ // eye's own lever (iris centre to eye bone), the same displacement a real turn
54
+ // of that angle gives, so it scales with whatever the rig's eyes do.
55
+ const LAG_GAIN = 0.5
56
+ const LAG_MAX = (3 * Math.PI) / 180
57
+ // And never past this many iris radii, whatever the lever, at strength 1.
58
+ const OFFSET_MAX = 0.06
59
+ const HEAD_SHARE = 0.35
60
+ // The inertia: the iris has a little mass, hung on a spring from the head, so
61
+ // her own motion — a bob, a step, a sway of the dance — leaves it behind and
62
+ // it swings back. Model units of lag to iris radii, scaled by INERTIA_GAIN.
63
+ const INERTIA_HZ = 3.0
64
+ const INERTIA_ZETA = 0.4
65
+ const INERTIA_GAIN = 0.04
66
+ // The jelly's give: the iris never moves, it swells. The lag and the inertia
67
+ // are how hard it is being knocked (in iris radii, as if it could move); that
68
+ // knock grows it a little and it settles in a wobble — the scale bounce on top
69
+ // of the blink's squash. Iris scale per iris radius of knock.
70
+ const PULSE_GAIN = 1.6
71
+ const PULSE_HZ = 1.6
72
+ const PULSE_ZETA = 0.24
73
+ // Integration step, and the jump past which a delta is a seek, not time.
74
+ const SUBSTEP = 1 / 240
75
+ const SEEK = 0.25
76
+
77
+ interface EyeState {
78
+ closure: number
79
+ // The slow follower of closure; closure − settled is what squashes.
80
+ settled: number
81
+ squash: number
82
+ squashV: number
83
+ // Lagged look (yaw, pitch) and its velocity; the offset is lagged − actual.
84
+ lag: [number, number]
85
+ lagV: [number, number]
86
+ look: [number, number]
87
+ shine: number
88
+ pulse: number
89
+ pulseV: number
90
+ }
91
+
92
+ // The head's inertial mass, shared by both eyes: where it hangs (world) and how
93
+ // fast it moves, and where the head was last frame.
94
+ interface Inertia {
95
+ p: [number, number, number]
96
+ v: [number, number, number]
97
+ head: [number, number, number]
98
+ // Its lag in the head's frame, in iris radii: (right, up).
99
+ offset: [number, number]
100
+ }
101
+
102
+ interface EyeRest {
103
+ // The iris in BIND space — where restPos puts it — and its half extents.
104
+ centre: [number, number, number]
105
+ radius: [number, number]
106
+ // Iris centre to the eye bone's head: what a turn moves the iris by, per
107
+ // radian.
108
+ lever: number
109
+ }
110
+
111
+ const newState = (): EyeState => ({
112
+ closure: 0,
113
+ settled: 0,
114
+ squash: 0,
115
+ squashV: 0,
116
+ lag: [0, 0],
117
+ lagV: [0, 0],
118
+ look: [0, 0],
119
+ shine: 0,
120
+ pulse: 0,
121
+ pulseV: 0,
122
+ })
123
+
124
+ export class JellyEyes {
125
+ strength: number
126
+ private readonly rest: [EyeRest, EyeRest]
127
+ private readonly state: [EyeState, EyeState] = [newState(), newState()]
128
+ private readonly morphs: [number[], number[], number[]]
129
+ private readonly eyeBones: [number, number]
130
+ private readonly headBone: number
131
+ private fresh = true
132
+ private readonly inertia: Inertia = { p: [0, 0, 0], v: [0, 0, 0], head: [0, 0, 0], offset: [0, 0] }
133
+
134
+ constructor(model: Model, options: JellyEyeOptions) {
135
+ this.strength = clampStrength(options.strength)
136
+ const bones = model.getSkeleton().bones
137
+ const index = (name: string) => bones.findIndex((b) => b.name === name)
138
+ const both = index("両目")
139
+ const l = index("左目")
140
+ const r = index("右目")
141
+ this.eyeBones = [l >= 0 ? l : both, r >= 0 ? r : both]
142
+ this.headBone = index("頭")
143
+ const names = model.getMorphing().morphs.map((m) => m.name)
144
+ const find = (list: string[]) => list.map((n) => names.indexOf(n)).filter((i) => i >= 0)
145
+ this.morphs = [find(BOTH), find(LEFT), find(RIGHT)]
146
+ this.rest = findIrises(model, l, r, both)
147
+ }
148
+
149
+ /** Advance both springs by dt seconds of scene time. */
150
+ step(dt: number, model: Model): void {
151
+ const weights = model.getEffectiveMorphWeights()
152
+ const sum = (list: number[]) => list.reduce((s, i) => s + (weights[i] ?? 0), 0)
153
+ const both = sum(this.morphs[0])
154
+ const closure = [Math.min(1, both + sum(this.morphs[1])), Math.min(1, both + sum(this.morphs[2]))]
155
+ const head = this.headBone >= 0 ? model.getBoneWorldMatrixAt(this.headBone) : null
156
+ const seek = this.fresh || dt > SEEK || dt < 0
157
+ this.stepInertia(dt, head, seek)
158
+ for (let e = 0; e < 2; e++) {
159
+ const s = this.state[e]
160
+ s.closure = closure[e]
161
+ // Her HEAD's turn, not her eyes': eye tracking turns the eyes after the
162
+ // camera, so the eyes' own turn would make the camera shake the jelly.
163
+ // Only her motion — the head turning and moving (the inertia) — and her
164
+ // blinks do.
165
+ look(head, null, s.look)
166
+ if (seek) s.settled = s.closure
167
+ else s.settled += (s.closure - s.settled) * Math.min(1, dt / CLOSURE_FOLLOW_S)
168
+ // Either way a lid moves squashes — opening too, which is the half of a
169
+ // blink you can see (closing, the lid hides the iris anyway).
170
+ const squashTarget = SQUASH_CLOSED * Math.abs(s.closure - s.settled)
171
+ if (seek) {
172
+ s.squash = squashTarget
173
+ s.squashV = 0
174
+ s.lag[0] = s.look[0]
175
+ s.lag[1] = s.look[1]
176
+ s.lagV[0] = s.lagV[1] = 0
177
+ s.shine = 0
178
+ s.pulse = s.pulseV = 0
179
+ continue
180
+ }
181
+ const steps = Math.ceil(dt / SUBSTEP)
182
+ const h = steps > 0 ? dt / steps : 0
183
+ const pulseTarget = PULSE_GAIN * Math.hypot(...this.offset(s, e))
184
+ for (let i = 0; i < steps; i++) {
185
+ s.pulseV += h * spring(s.pulse - pulseTarget, s.pulseV, PULSE_HZ, PULSE_ZETA)
186
+ s.pulse += h * s.pulseV
187
+ s.squashV += h * spring(s.squash - squashTarget, s.squashV, SQUASH_HZ, SQUASH_ZETA)
188
+ s.squash += h * s.squashV
189
+ for (let k = 0; k < 2; k++) {
190
+ s.lagV[k] += h * spring(s.lag[k] - s.look[k], s.lagV[k], LAG_HZ, LAG_ZETA)
191
+ s.lag[k] += h * s.lagV[k]
192
+ }
193
+ }
194
+ // Shine follows the motion: bright while the jelly moves, fading as it
195
+ // settles, eased so a single wobble does not strobe.
196
+ const motion = Math.min(1, 2.5 * Math.abs(s.squash - squashTarget) + 4 * Math.hypot(...this.offset(s, e)))
197
+ s.shine += (motion - s.shine) * Math.min(1, dt * 12)
198
+ }
199
+ this.fresh = false
200
+ }
201
+
202
+ /** Write jelly[0..5] into an ObjectLight copy (floats 48..71). */
203
+ write(o: Float32Array): void {
204
+ const [L, R] = this.rest
205
+ o.set([L.centre[0], L.centre[1], L.centre[2], this.strength], 48)
206
+ // One scale bounce for both: the mean of the two eyes', so they swell together.
207
+ const pulse = ((this.state[0].pulse + this.state[1].pulse) / 2) * this.strength
208
+ o.set([R.centre[0], R.centre[1], R.centre[2], pulse], 52)
209
+ for (let e = 0; e < 2; e++) {
210
+ const s = this.state[e]
211
+ const rest = this.rest[e]
212
+ o.set([rest.radius[0], rest.radius[1], s.closure, this.eyeBones[e]], 56 + e * 4)
213
+ // Offset words (65, 66) stay 0: the iris holds its place; only scale.
214
+ o.set([s.squash * this.strength, 0, 0, s.shine], 64 + e * 4)
215
+ }
216
+ }
217
+
218
+ /** Where the iris sits off rest, in iris radii (right, up): the lag through
219
+ * the eye's lever, plus the inertia. */
220
+ private offset(s: EyeState, e: number): [number, number] {
221
+ const angle = (x: number) => Math.max(-LAG_MAX, Math.min(LAG_MAX, x * LAG_GAIN))
222
+ const rest = this.rest[e]
223
+ const r = (rest.radius[0] + rest.radius[1]) / 2
224
+ const per = rest.lever / r
225
+ const io = this.inertia.offset
226
+ const x = angle(s.lag[0] - s.look[0]) * per + io[0]
227
+ const y = angle(s.lag[1] - s.look[1]) * per + io[1]
228
+ const n = Math.hypot(x, y)
229
+ const cap = n > OFFSET_MAX ? OFFSET_MAX / n : 1
230
+ return [x * cap, y * cap]
231
+ }
232
+
233
+ /** The mass on the head: semi-implicit steps toward where the head is, the
234
+ * head moving linearly across the frame. */
235
+ private stepInertia(dt: number, head: Float32Array | null, seek: boolean): void {
236
+ const m = this.inertia
237
+ if (!head) {
238
+ m.offset[0] = m.offset[1] = 0
239
+ return
240
+ }
241
+ const now: [number, number, number] = [head[12], head[13], head[14]]
242
+ if (seek) {
243
+ m.p = [...now]
244
+ m.v = [0, 0, 0]
245
+ m.head = [...now]
246
+ m.offset[0] = m.offset[1] = 0
247
+ return
248
+ }
249
+ const hv = [0, 1, 2].map((k) => (dt > 0 ? (now[k] - m.head[k]) / dt : 0))
250
+ const steps = Math.ceil(dt / SUBSTEP)
251
+ const h = steps > 0 ? dt / steps : 0
252
+ for (let i = 1; i <= steps; i++) {
253
+ const t = i / steps
254
+ for (let k = 0; k < 3; k++) {
255
+ const at = m.head[k] + (now[k] - m.head[k]) * t
256
+ m.v[k] += h * spring(m.p[k] - at, m.v[k] - hv[k], INERTIA_HZ, INERTIA_ZETA)
257
+ m.p[k] += h * m.v[k]
258
+ }
259
+ }
260
+ m.head = now
261
+ const u = [m.p[0] - now[0], m.p[1] - now[1], m.p[2] - now[2]]
262
+ const right = [head[0], head[1], head[2]]
263
+ const up = [head[4], head[5], head[6]]
264
+ const size = Math.max(1e-3, (this.rest[0].radius[0] + this.rest[0].radius[1] + this.rest[1].radius[0] + this.rest[1].radius[1]) / 4)
265
+ const cap = (x: number) => Math.max(-OFFSET_MAX, Math.min(OFFSET_MAX, x))
266
+ m.offset[0] = cap((dot(u, right) / Math.hypot(...right) / size) * INERTIA_GAIN)
267
+ m.offset[1] = cap((dot(u, up) / Math.hypot(...up) / size) * INERTIA_GAIN)
268
+ }
269
+ }
270
+
271
+ /** Zero jelly[0..5] — strength 0 is what the shader reads as off. */
272
+ export function clearJellyEyes(o: Float32Array): void {
273
+ o.fill(0, 48, 72)
274
+ }
275
+
276
+ export function clampStrength(s: number | undefined): number {
277
+ return Math.max(0, Math.min(2, s ?? 1))
278
+ }
279
+
280
+ /** Acceleration of a damped spring at displacement x, velocity v. */
281
+ function spring(x: number, v: number, hz: number, zeta: number): number {
282
+ const w = 2 * Math.PI * hz
283
+ return -w * w * x - 2 * zeta * w * v
284
+ }
285
+
286
+ /**
287
+ * Where an eye looks, as (yaw, pitch) radians: the eye in the head's frame,
288
+ * plus a share of the head in the model's. Bone matrices are column-major and
289
+ * MMD rests facing −Z, so forward is the negated third column.
290
+ */
291
+ function look(eye: Float32Array | null, head: Float32Array | null, out: [number, number]): void {
292
+ out[0] = out[1] = 0
293
+ if (!eye) return
294
+ const f = [-eye[8], -eye[9], -eye[10]]
295
+ if (head && head !== eye) {
296
+ const right = [head[0], head[1], head[2]]
297
+ const up = [head[4], head[5], head[6]]
298
+ const hf = [-head[8], -head[9], -head[10]]
299
+ const fz = dot(f, hf)
300
+ out[0] = Math.atan2(dot(f, right), fz)
301
+ out[1] = Math.atan2(dot(f, up), fz)
302
+ out[0] += HEAD_SHARE * Math.atan2(hf[0], -hf[2])
303
+ out[1] += HEAD_SHARE * Math.asin(Math.max(-1, Math.min(1, hf[1] / Math.hypot(hf[0], hf[1], hf[2]))))
304
+ } else {
305
+ // Yaw positive toward +X, as in the head-frame branch: the shader warps
306
+ // along bind-space +X, so the two must agree.
307
+ out[0] = Math.atan2(f[0], -f[2])
308
+ out[1] = Math.asin(Math.max(-1, Math.min(1, f[1] / Math.hypot(f[0], f[1], f[2]))))
309
+ }
310
+ }
311
+
312
+ const dot = (a: number[], b: number[]) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
313
+
314
+ /**
315
+ * Each iris in bind space: the vertices the eye bones carry are exactly the
316
+ * part of the eye that turns to look — the iris and pupil — so their centroid
317
+ * is its centre and their spread its size. A rig that carries both eyes on
318
+ * 両目 alone is split by side. With no carried vertices at all, the eye bones'
319
+ * own heads stand in, sized from how far apart they are.
320
+ */
321
+ function findIrises(model: Model, l: number, r: number, both: number): [EyeRest, EyeRest] {
322
+ const bones = model.getSkeleton().bones
323
+ const head = (i: number): [number, number, number] | null => {
324
+ if (i < 0) return null
325
+ const p: [number, number, number] = [0, 0, 0]
326
+ for (let b = i; b >= 0; b = bones[b].parentIndex) {
327
+ p[0] += bones[b].bindTranslation[0]
328
+ p[1] += bones[b].bindTranslation[1]
329
+ p[2] += bones[b].bindTranslation[2]
330
+ }
331
+ return p
332
+ }
333
+ const lHead = head(l)
334
+ const rHead = head(r)
335
+ // +X is the model's left; trust the bones when they say otherwise.
336
+ const leftSign = lHead && rHead ? Math.sign(lHead[0] - rHead[0]) || 1 : 1
337
+ const { positions } = model.getGeometry()
338
+ const { joints, weights } = model.getSkinning()
339
+ const sets: [number[], number[]] = [[], []]
340
+ const n = positions.length / 3
341
+ for (let v = 0; v < n; v++) {
342
+ let wl = 0
343
+ let wr = 0
344
+ let wb = 0
345
+ for (let k = 0; k < 4; k++) {
346
+ const j = joints[v * 4 + k]
347
+ const w = weights[v * 4 + k]
348
+ if (j === l) wl += w
349
+ else if (j === r) wr += w
350
+ else if (j === both) wb += w
351
+ }
352
+ if (wl >= 128) sets[0].push(v)
353
+ else if (wr >= 128) sets[1].push(v)
354
+ else if (wb >= 128) sets[Math.sign(positions[v * 3]) === leftSign ? 0 : 1].push(v)
355
+ }
356
+ const apart = lHead && rHead ? Math.hypot(lHead[0] - rHead[0], lHead[1] - rHead[1]) : 0
357
+ const fallbackRadius = apart > 0 ? apart * 0.22 : 0.3
358
+ const rest = (set: number[], bone: [number, number, number] | null, side: number): EyeRest => {
359
+ if (set.length < 3) {
360
+ const c: [number, number, number] = bone ?? [side * fallbackRadius * 2, 0, 0]
361
+ return { centre: c, radius: [fallbackRadius, fallbackRadius], lever: fallbackRadius * 2 }
362
+ }
363
+ let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity, z = 0
364
+ for (const v of set) {
365
+ const x = positions[v * 3]
366
+ const y = positions[v * 3 + 1]
367
+ minX = Math.min(minX, x)
368
+ maxX = Math.max(maxX, x)
369
+ minY = Math.min(minY, y)
370
+ maxY = Math.max(maxY, y)
371
+ z += positions[v * 3 + 2]
372
+ }
373
+ // A carried set wider than the gap between the eyes is not an iris (a
374
+ // whole-face rig weighted to the eye bone): fall back rather than wobble
375
+ // half the face.
376
+ const rx = (maxX - minX) / 2
377
+ const ry = (maxY - minY) / 2
378
+ if (apart > 0 && (rx > apart * 0.6 || ry > apart * 0.8)) {
379
+ return { centre: bone ?? [side * fallbackRadius * 2, 0, 0], radius: [fallbackRadius, fallbackRadius], lever: fallbackRadius * 2 }
380
+ }
381
+ const centre: [number, number, number] = [(minX + maxX) / 2, (minY + maxY) / 2, z / set.length]
382
+ const radius: [number, number] = [Math.max(rx, 1e-3), Math.max(ry, 1e-3)]
383
+ // An eye bone sitting on the iris itself would give no lever; such a rig
384
+ // still turns its eyes visibly, so read it as a typical eyeball's.
385
+ const r = (radius[0] + radius[1]) / 2
386
+ const lever = Math.max(bone ? Math.hypot(centre[0] - bone[0], centre[1] - bone[1], centre[2] - bone[2]) : 0, r * 1.5)
387
+ return { centre, radius, lever }
388
+ }
389
+ return [rest(sets[0], lHead, leftSign), rest(sets[1], rHead, -leftSign)]
390
+ }
@@ -216,9 +216,127 @@ fn _rzCastShadow(wp: vec3f) -> f32 {
216
216
  // sh that ambient's irradiance, folded as the world's (ibl.ts);
217
217
  // fill.rgb its fill (Engine.setModelFill), added after its graph times its
218
218
  // surface colour — zero for a model that has none.
219
- struct ObjectLight { layers: vec4u, ambient: vec4f, sh: array<vec4f, 9>, fill: vec4f }
219
+ // jelly its jelly eyes (Engine.setModelJellyEyes, jelly-eyes.ts), not
220
+ // lighting but per-model all the same, so it rides here:
221
+ // [0] left iris centre (bind space), strength — 0 is off
222
+ // [1] right iris centre, scale bounce (both eyes)
223
+ // [2] left iris half extents, closure, eye bone (−1 none)
224
+ // [3] the right's
225
+ // [4] left squash, 0, 0, shine [5] the right's
226
+ struct ObjectLight { layers: vec4u, ambient: vec4f, sh: array<vec4f, 9>, fill: vec4f, jelly: array<vec4f, 6> }
220
227
  @group(1) @binding(1) var<uniform> objectLight: ObjectLight;
221
228
 
229
+ /** What the glints need to know about a fragment of the eye render class. */
230
+ struct RzJelly {
231
+ /** Where in the iris this fragment is, in iris radii. */
232
+ src: vec2f,
233
+ /** 1 inside the iris, easing to 0 at its rim (0 everywhere when off). */
234
+ w: f32,
235
+ closure: f32,
236
+ shine: f32,
237
+ };
238
+
239
+ /**
240
+ * Where the jelly puts an iris vertex. The iris is the part of the eye the eye
241
+ * bones carry, so it is moved as a whole, in bind space before skinning:
242
+ * flattened about its centre while the eye closes, bulging across as jelly does,
243
+ * grown past rest as it springs back, and swelling as a turn or her motion
244
+ * knocks it — scale only, in place, inside the white, which stays where it is. Vertices take it by
245
+ * their weight on the eye bones (jelly[2].w, jelly[3].w), so nothing else on
246
+ * the face moves.
247
+ */
248
+ fn rzJellyVertex(p: vec3f, joints: vec4<u32>, nw: vec4f) -> vec3f {
249
+ let j = objectLight.jelly;
250
+ var out = p;
251
+ for (var e = 0u; e < 2u; e++) {
252
+ let bone = j[2u + e].w;
253
+ if (bone < 0.0) { continue; }
254
+ let b = u32(bone);
255
+ let w = dot(nw, select(vec4f(0.0), vec4f(1.0), joints == vec4<u32>(b)));
256
+ if (w <= 0.0) { continue; }
257
+ let c = j[e].xyz;
258
+ // A rig that carries both eyes on one bone: each vertex follows its own side.
259
+ if (j[2].w == j[3].w && e == 0u && distance(p.xy, j[1].xy) < distance(p.xy, j[0].xy)) { continue; }
260
+ if (j[2].w == j[3].w && e == 1u && distance(p.xy, j[0].xy) <= distance(p.xy, j[1].xy)) { continue; }
261
+ let st = j[4u + e];
262
+ // The blink's squash, as jelly gives: flattened down, it bulges across
263
+ // (and a stretch thins it), and the bounce both eyes share (jelly[1].w).
264
+ // Height is held under 1.06 so a swell cannot rise past the upper lid.
265
+ let k = clamp(vec2f(1.0 + 0.45 * st.x, 1.0 - st.x) + j[1].w, vec2f(0.9, 0.78), vec2f(1.1, 1.06));
266
+ let moved = c.xy + (p.xy - c.xy) * k;
267
+ out = vec3f(mix(p.xy, moved, w), p.z);
268
+ }
269
+ return out;
270
+ }
271
+
272
+ /**
273
+ * The glints' frame for an eye fragment: its place in the iris in iris radii,
274
+ * from the bind-space position, which the jelly moved the iris with — so the
275
+ * glints ride the sloshing iris. Strength 0 gives w 0, which turns them off.
276
+ */
277
+ fn rzJellyEye(rp: vec3f) -> RzJelly {
278
+ let j = objectLight.jelly;
279
+ let qL = (rp.xy - j[0].xy) / max(j[2].xy, vec2f(1e-4));
280
+ let qR = (rp.xy - j[1].xy) / max(j[3].xy, vec2f(1e-4));
281
+ let right = dot(qR, qR) < dot(qL, qL);
282
+ let a = select(qL, qR, right);
283
+ let st = select(j[4], j[5], right);
284
+ let w = (1.0 - smoothstep(0.85, 1.0, length(a))) * select(0.0, 1.0, j[0].w > 0.0);
285
+ return RzJelly(a, w, select(j[2].z, j[3].z, right), st.w);
286
+ }
287
+
288
+ /**
289
+ * The jelly shine, added after the graph: two hard glints where the light
290
+ * would catch a bead — placed by the half vector in the head's frame, so they
291
+ * hold still while the iris turns under them — and the eye brightening while
292
+ * it wobbles. Flat, cel discs rather than a gradient lobe; HDR, so bloom
293
+ * takes them. Shut eyes catch nothing.
294
+ */
295
+ fn rzJellyGlow(jj: RzJelly, tex: vec3f, alpha: f32, v: vec3f, l: vec3f, sun: vec3f, shadow: f32) -> vec3f {
296
+ let strength = objectLight.jelly[0].w;
297
+ var right = vec3f(1.0, 0.0, 0.0);
298
+ var up = vec3f(0.0, 1.0, 0.0);
299
+ var fwd = vec3f(0.0, 0.0, -1.0);
300
+ if (material.headBoneIndex >= 0.0) {
301
+ let hm = skinMats[u32(material.headBoneIndex)];
302
+ right = normalize(hm[0].xyz);
303
+ up = normalize(hm[1].xyz);
304
+ fwd = -normalize(hm[2].xyz);
305
+ }
306
+ let h = normalize(l + v);
307
+ // Held near the middle of the iris, a little low: the top of an iris sits
308
+ // under the upper lid, and a glint there pokes out over it.
309
+ var g = vec2f(dot(h, right), dot(h, up)) * 0.6;
310
+ g = g * min(1.0, 0.3 / max(length(g), 1e-6)) - vec2f(0.0, 0.08);
311
+ let aa = max(length(fwidth(jj.src)), 1e-4);
312
+ let glint = 1.0 - smoothstep(0.16 - aa, 0.16 + aa, distance(jj.src, g));
313
+ let second = 1.0 - smoothstep(0.07 - aa, 0.07 + aa, distance(jj.src, -0.55 * g + vec2f(0.1, -0.12)));
314
+ let facing = saturate(dot(fwd, v) * 2.0);
315
+ let key = clamp(dot(sun, vec3f(0.2126, 0.7152, 0.0722)), 0.3, 2.0);
316
+ let lit = facing * key * (0.35 + 0.65 * shadow);
317
+ // HDR, well past 1, so bloom takes them as the hard sparkle of a gem.
318
+ let glints = (glint + 0.8 * second) * lit * (2.6 + 1.6 * jj.shine);
319
+ let open = 1.0 - smoothstep(0.3, 0.8, jj.closure);
320
+ // Only where the eye's own picture is (its alpha), never on the iris's top
321
+ // rim, and gone as the lid comes down.
322
+ let onEye = smoothstep(0.5, 0.9, alpha) * (1.0 - smoothstep(0.35, 0.6, jj.src.y));
323
+ // Light through the jelly: the light enters where the glint is and comes out
324
+ // the far side, so the iris glows in its own colour in a crescent opposite
325
+ // the glint — the lit-from-within look of a gem, or a jelly.
326
+ let r = length(jj.src);
327
+ let away = -normalize(g + vec2f(0.0, -1e-3));
328
+ let crescent = smoothstep(0.15, 0.7, dot(jj.src, away)) * (1.0 - smoothstep(0.8, 1.0, r));
329
+ let through = tex * crescent * (0.9 + 0.6 * jj.shine) * lit;
330
+ // And the colour runs deeper: the iris's own hue pushed, never its greys.
331
+ let lum = dot(tex, vec3f(0.2126, 0.7152, 0.0722));
332
+ let vivid = max(tex - vec3f(lum), vec3f(0.0)) * 0.7;
333
+ let gem = (vec3f(glints) + through + vivid) * open * onEye * jj.w;
334
+ // The whole eye shines a little brighter, more while it wobbles — not just
335
+ // the iris, so this one is not held to it (jj.w).
336
+ let lift = tex * (0.15 + 0.35 * jj.shine) * select(0.0, 1.0, strength > 0.0);
337
+ return (gem + lift) * min(strength, 1.0);
338
+ }
339
+
222
340
  /** The light arriving at a surface of this object facing n: its own ambient
223
341
  * where loaded data gave it one (a stage's probes, a character's), else the
224
342
  * world's. The same polynomial rzWorldAmbient evaluates. */
@@ -329,10 +447,11 @@ const COMMON_VS_WGSL = /* wgsl */ `
329
447
  var output: VertexOutput;
330
448
  output.vcolor = vcolor;
331
449
  output.uv2 = uv2;
332
- let pos4 = vec4f(position, 1.0);
333
450
  let weightSum = weights0.x + weights0.y + weights0.z + weights0.w;
334
451
  let invWeightSum = select(1.0, 1.0 / weightSum, weightSum > 0.0001);
335
452
  let nw = select(vec4f(1.0, 0.0, 0.0, 0.0), weights0 * invWeightSum, weightSum > 0.0001);
453
+ var pos4 = vec4f(position, 1.0);
454
+ if (objectLight.jelly[0].w > 0.0) { pos4 = vec4f(rzJellyVertex(position, joints0, nw), 1.0); }
336
455
  var skinnedPos = vec4f(0.0);
337
456
  var skinnedNrm = vec3f(0.0);
338
457
  for (var i = 0u; i < 4u; i++) {
@@ -1,3 +0,0 @@
1
- import type { ShaderGraph } from "../schema";
2
- export declare const POOL_FLOOR_GRAPH: ShaderGraph;
3
- //# sourceMappingURL=pool_floor.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pool_floor.d.ts","sourceRoot":"","sources":["../../../src/graph/presets/pool_floor.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAE5C,eAAO,MAAM,gBAAgB,EAAE,WA6B9B,CAAA"}
@@ -1,42 +0,0 @@
1
- // Pool floor — the tiled bottom seen through water, with caustics on it.
2
- //
3
- // The other half of a pool, and the half that does most of the work. A water
4
- // surface can be tuned for a long time without the shot reading as a pool;
5
- // putting the focused-light web on the FLOOR is what settles it, because that
6
- // is the cue a photograph actually carries. It is a floor material, so it is
7
- // applied to the tile under the water rather than to the water itself.
8
- //
9
- // Caustics land between the albedo and the shading deliberately: they are
10
- // light, so they should still be dimmed by the floor's own shadow rather than
11
- // glowing through it. That is the difference between water-focused sun and a
12
- // decal.
13
- export const POOL_FLOOR_GRAPH = {
14
- version: 1,
15
- name: "Pool floor",
16
- tags: ["stage", "water", "pool"],
17
- nodes: [
18
- { id: "tex", type: "texture" },
19
- { id: "mat", type: "material_diffuse" },
20
- { id: "base", type: "mix/multiply", inputs: { fac: 1.0 } },
21
- { id: "caust", type: "caustics" },
22
- {
23
- id: "principled",
24
- type: "principled",
25
- inputs: { metallic: 0.0, specular_ior_level: 0.4, roughness: 0.45, spec_clamp: 10.0, sheen_weight: 0.0, sheen_tint: 0.0 },
26
- },
27
- ],
28
- links: [
29
- { from: { node: "tex", socket: "color" }, to: { node: "base", socket: "a" } },
30
- { from: { node: "mat", socket: "color" }, to: { node: "base", socket: "b" } },
31
- { from: { node: "base", socket: "color" }, to: { node: "caust", socket: "color" } },
32
- { from: { node: "caust", socket: "color" }, to: { node: "principled", socket: "base_color" } },
33
- ],
34
- output: { node: "principled", socket: "color" },
35
- params: [
36
- { id: "tint", label: "Light tint", target: { node: "caust", socket: "tint" }, kind: "color", default: [1.0, 1.0, 1.0] },
37
- { id: "scale", label: "Caustic scale", target: { node: "caust", socket: "scale" }, kind: "float", min: 0.01, max: 1, default: 0.12 },
38
- { id: "speed", label: "Caustic speed", target: { node: "caust", socket: "speed" }, kind: "float", min: 0, max: 1, default: 0.15 },
39
- { id: "strength", label: "Caustic strength", target: { node: "caust", socket: "strength" }, kind: "float", min: 0, max: 3, default: 0.6 },
40
- { id: "fade", label: "Depth fade", target: { node: "caust", socket: "fade" }, kind: "float", min: 1, max: 200, default: 30 },
41
- ],
42
- };
@@ -1,3 +0,0 @@
1
- import type { ShaderGraph } from "../schema";
2
- export declare const WATER_GRAPH: ShaderGraph;
3
- //# sourceMappingURL=water.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"water.d.ts","sourceRoot":"","sources":["../../../src/graph/presets/water.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAE5C,eAAO,MAAM,WAAW,EAAE,WAgBzB,CAAA"}
@@ -1,31 +0,0 @@
1
- // Water — a planar reflection, rippled, seen through Fresnel.
2
- //
3
- // Put a stage's water materials in a style group carrying this graph and the
4
- // pool starts reflecting. The reflection itself is the engine's mirror pass,
5
- // which this is the only graph to read; everything else here is the shaping
6
- // that keeps it from reading as a mirror laid flat in the floor.
7
- //
8
- // The exposed params are the ones worth a slider while judging a shot. Scale
9
- // and speed are the ripple; a real pool's surface is slow and broad, so both
10
- // defaults sit low. Tint is the water's own colour — a game rip carries that
11
- // in its material, and the converter hands it straight over.
12
- //
13
- // How see-through the water is belongs to the MATERIAL's alpha, not to this
14
- // graph, which is where every other group's coverage comes from too.
15
- export const WATER_GRAPH = {
16
- version: 1,
17
- name: "Water",
18
- tags: ["water", "stage"],
19
- nodes: [{ id: "water", type: "water" }],
20
- links: [],
21
- output: { node: "water", socket: "color" },
22
- params: [
23
- { id: "tint", label: "Tint", target: { node: "water", socket: "tint" }, kind: "color", default: [0.04, 0.62, 0.66] },
24
- { id: "scale", label: "Ripple scale", target: { node: "water", socket: "scale" }, kind: "float", min: 0.01, max: 1, default: 0.18 },
25
- { id: "speed", label: "Ripple speed", target: { node: "water", socket: "speed" }, kind: "float", min: 0, max: 1, default: 0.12 },
26
- { id: "strength", label: "Ripple depth", target: { node: "water", socket: "strength" }, kind: "float", min: 0, max: 2, default: 0.7 },
27
- { id: "blur", label: "Reflection blur", target: { node: "water", socket: "blur" }, kind: "float", min: 0, max: 1, default: 0.25 },
28
- { id: "mirroring", label: "Mirroring", target: { node: "water", socket: "mirroring" }, kind: "float", min: 0, max: 1, default: 0.20 },
29
- { id: "sparkle", label: "Sparkle", target: { node: "water", socket: "sparkle" }, kind: "float", min: 0, max: 4, default: 1.0 },
30
- ],
31
- };