reze-engine 0.68.3 → 0.68.5

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 +40 -2
  3. package/dist/engine.d.ts.map +1 -1
  4. package/dist/engine.js +119 -8
  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 +37 -0
  10. package/dist/jelly-eyes.d.ts.map +1 -0
  11. package/dist/jelly-eyes.js +381 -0
  12. package/dist/shaders/materials/common.d.ts.map +1 -1
  13. package/dist/shaders/materials/common.js +133 -2
  14. package/package.json +1 -1
  15. package/src/engine.ts +121 -8
  16. package/src/graph/slots.ts +13 -4
  17. package/src/index.ts +1 -0
  18. package/src/jelly-eyes.ts +434 -0
  19. package/src/shaders/materials/common.ts +133 -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,434 @@
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 shrinks and
7
+ // springs back 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
+ /** The idle taps: a small bounce every few seconds while nothing else moves them.
26
+ * Default true; false leaves only what her blinks and motion set off. */
27
+ idle?: boolean
28
+ }
29
+
30
+ // The closing morphs, by the names MMD, VRoid and the usual converters give
31
+ // them. Group morphs (a smile that closes the eyes) resolve into these through
32
+ // the effective weights, so only the leaves need naming.
33
+ const BOTH = ["まばたき", "笑い", "はぅ", "blink", "Blink", "eye_close", "Fcl_EYE_Close"]
34
+ const LEFT = ["ウィンク", "ウィンク2", "ウィンク", "ウィンク2", "wink", "Wink", "blink_L", "Fcl_EYE_Close_L"]
35
+ const RIGHT = ["ウィンク右", "ウィンク2右", "ウィンク右", "ウィンク2右", "wink_R", "Wink_R", "blink_R", "Fcl_EYE_Close_R"]
36
+
37
+ // The squash: how far a closed eye presses the picture, and the spring that
38
+ // follows it — lightly damped, so one knock ripples: the squash, a rebound past
39
+ // rest, and several smaller wobbles before it settles. One dip and done read as
40
+ // a scale, not a jelly; the ripple is the "Duang" the effect is named for.
41
+ // Only a CHANGE of closure squashes: the eye's closure less a slow follower of
42
+ // it, so a blink squeezes then bounces, while a held half-close (a smile)
43
+ // leaves the iris at rest instead of squashed for as long as she smiles.
44
+ const SQUASH_CLOSED = 0.35
45
+ const SQUASH_HZ = 1.8
46
+ const SQUASH_ZETA = 0.08
47
+ const CLOSURE_FOLLOW_S = 0.35
48
+ // The lag: the picture trails the eye's turn and catches up past it. Radians of
49
+ // lag to eye radii of offset; the head's own turn counts for a third, so a
50
+ // sudden stop of the head wobbles the eyes without a dance keeping them
51
+ // sloshing.
52
+ const LAG_HZ = 3.5
53
+ const LAG_ZETA = 0.4
54
+ // The lag is an angle: how far the iris trails the eye's own turn, as a share
55
+ // of the lag in look, capped at a quarter of eye tracking's default 20° reach —
56
+ // a wobble on the turn, never a second turn. It becomes iris radii through the
57
+ // eye's own lever (iris centre to eye bone), the same displacement a real turn
58
+ // of that angle gives, so it scales with whatever the rig's eyes do.
59
+ const LAG_GAIN = 0.5
60
+ const LAG_MAX = (3 * Math.PI) / 180
61
+ // And never past this many iris radii, whatever the lever, at strength 1.
62
+ const OFFSET_MAX = 0.2
63
+ const HEAD_SHARE = 0.35
64
+ // The inertia: the iris has a little mass, hung on a spring from the head, so
65
+ // her own motion — a bob, a step, a sway of the dance — leaves it behind and
66
+ // it swings back. Model units of lag to iris radii, scaled by INERTIA_GAIN.
67
+ const INERTIA_HZ = 3.0
68
+ const INERTIA_ZETA = 0.4
69
+ const INERTIA_GAIN = 0.04
70
+ // The jelly's give: the iris never moves, it swells. The lag and the inertia
71
+ // are how hard it is being knocked (in iris radii, as if it could move); that
72
+ // knock grows it a little and it ripples back, as the squash does — the bounce on top
73
+ // of the blink's squash. Eased, not linear: the swell is PULSE_MAX·tanh(knock /
74
+ // PULSE_REF), so the small steady knocks of a dance still show while a jolt
75
+ // (play from a pause: the head going from still to moving at once) cannot
76
+ // swell it past the cap.
77
+ const PULSE_MAX = 0.07
78
+ const PULSE_REF = 0.006
79
+ // And the same knock squashes it as a blink does — nearly round, on
80
+ // the blink's spring — so her motion reads as plainly as a blink. Eased by the
81
+ // same curve, up to MOTION_SQUASH (a blink's opening gives about 0.2).
82
+ const MOTION_SQUASH = 0.18
83
+ const PULSE_HZ = 2.0
84
+ const PULSE_ZETA = 0.08
85
+ // The idle taps: every few seconds, at uneven times, a small knock into the
86
+ // same springs her motion drives — the iris dips, bounces and settles, then
87
+ // rests — the way a jelly on a table is never quite still. A steady sine was
88
+ // tried, as size (a zoom) and as wider-then-taller (queasy); a settling tap is
89
+ // what reads as jelly. Velocity kicks, so the bounce keeps the springs' own
90
+ // timing; the gaps come from a hash of the tap's count, so the scene clock
91
+ // alone decides them and an export taps where the screen did.
92
+ const IDLE_SQUASH_KICK = 0.45
93
+ const IDLE_PULSE_KICK = 0.25
94
+ const IDLE_GAP: [number, number] = [1.8, 3.8]
95
+ const idleGap = (n: number): number => {
96
+ const x = Math.sin(n * 12.9898 + 78.233) * 43758.5453
97
+ return IDLE_GAP[0] + (x - Math.floor(x)) * (IDLE_GAP[1] - IDLE_GAP[0])
98
+ }
99
+ // Integration step, and the jump past which a delta is a seek, not time.
100
+ const SUBSTEP = 1 / 240
101
+ const SEEK = 0.25
102
+
103
+ interface EyeState {
104
+ closure: number
105
+ // The slow follower of closure; closure − settled is what squashes.
106
+ settled: number
107
+ squash: number
108
+ squashV: number
109
+ // Lagged look (yaw, pitch) and its velocity; the offset is lagged − actual.
110
+ lag: [number, number]
111
+ lagV: [number, number]
112
+ look: [number, number]
113
+ shine: number
114
+ pulse: number
115
+ pulseV: number
116
+ }
117
+
118
+ // The head's inertial mass, shared by both eyes: where it hangs (world) and how
119
+ // fast it moves, and where the head was last frame.
120
+ interface Inertia {
121
+ p: [number, number, number]
122
+ v: [number, number, number]
123
+ head: [number, number, number]
124
+ // Its lag in the head's frame, in iris radii: (right, up).
125
+ offset: [number, number]
126
+ }
127
+
128
+ interface EyeRest {
129
+ // The iris in BIND space — where restPos puts it — and its half extents.
130
+ centre: [number, number, number]
131
+ radius: [number, number]
132
+ // Iris centre to the eye bone's head: what a turn moves the iris by, per
133
+ // radian.
134
+ lever: number
135
+ }
136
+
137
+ const newState = (): EyeState => ({
138
+ closure: 0,
139
+ settled: 0,
140
+ squash: 0,
141
+ squashV: 0,
142
+ lag: [0, 0],
143
+ lagV: [0, 0],
144
+ look: [0, 0],
145
+ shine: 0,
146
+ pulse: 0,
147
+ pulseV: 0,
148
+ })
149
+
150
+ export class JellyEyes {
151
+ strength: number
152
+ idle: boolean
153
+ // Scene time stepped so far, and the idle taps: how many, and when the next.
154
+ private clock = 0
155
+ private taps = 0
156
+ private nextTap = idleGap(0)
157
+ private readonly rest: [EyeRest, EyeRest]
158
+ private readonly state: [EyeState, EyeState] = [newState(), newState()]
159
+ private readonly morphs: [number[], number[], number[]]
160
+ private readonly eyeBones: [number, number]
161
+ private readonly headBone: number
162
+ private fresh = true
163
+ private readonly inertia: Inertia = { p: [0, 0, 0], v: [0, 0, 0], head: [0, 0, 0], offset: [0, 0] }
164
+
165
+ constructor(model: Model, options: JellyEyeOptions) {
166
+ this.strength = clampStrength(options.strength)
167
+ this.idle = options.idle ?? true
168
+ const bones = model.getSkeleton().bones
169
+ const index = (name: string) => bones.findIndex((b) => b.name === name)
170
+ const both = index("両目")
171
+ const l = index("左目")
172
+ const r = index("右目")
173
+ this.eyeBones = [l >= 0 ? l : both, r >= 0 ? r : both]
174
+ this.headBone = index("頭")
175
+ const names = model.getMorphing().morphs.map((m) => m.name)
176
+ const find = (list: string[]) => list.map((n) => names.indexOf(n)).filter((i) => i >= 0)
177
+ this.morphs = [find(BOTH), find(LEFT), find(RIGHT)]
178
+ this.rest = findIrises(model, l, r, both)
179
+ }
180
+
181
+ /** Advance both springs by dt seconds of scene time. */
182
+ step(dt: number, model: Model): void {
183
+ const weights = model.getEffectiveMorphWeights()
184
+ const sum = (list: number[]) => list.reduce((s, i) => s + (weights[i] ?? 0), 0)
185
+ const both = sum(this.morphs[0])
186
+ const closure = [Math.min(1, both + sum(this.morphs[1])), Math.min(1, both + sum(this.morphs[2]))]
187
+ const head = this.headBone >= 0 ? model.getBoneWorldMatrixAt(this.headBone) : null
188
+ const seek = this.fresh || dt > SEEK || dt < 0
189
+ if (dt > 0) this.clock += dt
190
+ let tap = false
191
+ if (!seek && this.idle && this.clock >= this.nextTap) {
192
+ tap = true
193
+ this.taps++
194
+ this.nextTap = this.clock + idleGap(this.taps)
195
+ }
196
+ this.stepInertia(dt, head, seek)
197
+ for (let e = 0; e < 2; e++) {
198
+ const s = this.state[e]
199
+ s.closure = closure[e]
200
+ // Her HEAD's turn, not her eyes': eye tracking turns the eyes after the
201
+ // camera, so the eyes' own turn would make the camera shake the jelly.
202
+ // Only her motion — the head turning and moving (the inertia) — and her
203
+ // blinks do.
204
+ look(head, null, s.look)
205
+ if (seek) s.settled = s.closure
206
+ else s.settled += (s.closure - s.settled) * Math.min(1, dt / CLOSURE_FOLLOW_S)
207
+ // Either way a lid moves squashes — opening too, which is the half of a
208
+ // blink you can see (closing, the lid hides the iris anyway).
209
+ const knock = seek ? 0 : Math.tanh(Math.hypot(...this.offset(s, e)) / PULSE_REF)
210
+ const squashTarget = SQUASH_CLOSED * Math.abs(s.closure - s.settled) + MOTION_SQUASH * knock
211
+ if (seek) {
212
+ s.squash = squashTarget
213
+ s.squashV = 0
214
+ s.lag[0] = s.look[0]
215
+ s.lag[1] = s.look[1]
216
+ s.lagV[0] = s.lagV[1] = 0
217
+ s.shine = 0
218
+ s.pulse = s.pulseV = 0
219
+ continue
220
+ }
221
+ if (tap) {
222
+ s.squashV += IDLE_SQUASH_KICK
223
+ s.pulseV -= IDLE_PULSE_KICK
224
+ }
225
+ const steps = Math.ceil(dt / SUBSTEP)
226
+ const h = steps > 0 ? dt / steps : 0
227
+ const pulseTarget = PULSE_MAX * knock
228
+ for (let i = 0; i < steps; i++) {
229
+ s.pulseV += h * spring(s.pulse - pulseTarget, s.pulseV, PULSE_HZ, PULSE_ZETA)
230
+ s.pulse += h * s.pulseV
231
+ s.squashV += h * spring(s.squash - squashTarget, s.squashV, SQUASH_HZ, SQUASH_ZETA)
232
+ s.squash += h * s.squashV
233
+ for (let k = 0; k < 2; k++) {
234
+ s.lagV[k] += h * spring(s.lag[k] - s.look[k], s.lagV[k], LAG_HZ, LAG_ZETA)
235
+ s.lag[k] += h * s.lagV[k]
236
+ }
237
+ }
238
+ // Shine follows the motion: bright while the jelly moves, fading as it
239
+ // settles, eased so a single wobble does not strobe.
240
+ const motion = Math.min(1, 2.5 * Math.abs(s.squash - squashTarget) + 4 * Math.hypot(...this.offset(s, e)))
241
+ s.shine += (motion - s.shine) * Math.min(1, dt * 12)
242
+ }
243
+ this.fresh = false
244
+ }
245
+
246
+ /** Write jelly[0..5] into an ObjectLight copy (floats 48..71). */
247
+ write(o: Float32Array): void {
248
+ const [L, R] = this.rest
249
+ o.set([L.centre[0], L.centre[1], L.centre[2], this.strength], 48)
250
+ // One scale bounce for both: the mean of the two eyes', so they swell together.
251
+ const pulse = ((this.state[0].pulse + this.state[1].pulse) / 2) * this.strength
252
+ o.set([R.centre[0], R.centre[1], R.centre[2], pulse], 52)
253
+ for (let e = 0; e < 2; e++) {
254
+ const s = this.state[e]
255
+ const rest = this.rest[e]
256
+ o.set([rest.radius[0], rest.radius[1], s.closure, this.eyeBones[e]], 56 + e * 4)
257
+ // Offset words (65, 66) stay 0: the iris holds its place; only scale.
258
+ o.set([s.squash * this.strength, 0, 0, s.shine], 64 + e * 4)
259
+ }
260
+ }
261
+
262
+ /** Where the iris sits off rest, in iris radii (right, up): the lag through
263
+ * the eye's lever, plus the inertia. */
264
+ private offset(s: EyeState, e: number): [number, number] {
265
+ const angle = (x: number) => Math.max(-LAG_MAX, Math.min(LAG_MAX, x * LAG_GAIN))
266
+ const rest = this.rest[e]
267
+ const r = (rest.radius[0] + rest.radius[1]) / 2
268
+ const per = rest.lever / r
269
+ const io = this.inertia.offset
270
+ const x = angle(s.lag[0] - s.look[0]) * per + io[0]
271
+ const y = angle(s.lag[1] - s.look[1]) * per + io[1]
272
+ const n = Math.hypot(x, y)
273
+ const cap = n > OFFSET_MAX ? OFFSET_MAX / n : 1
274
+ return [x * cap, y * cap]
275
+ }
276
+
277
+ /** The mass on the head: semi-implicit steps toward where the head is, the
278
+ * head moving linearly across the frame. */
279
+ private stepInertia(dt: number, head: Float32Array | null, seek: boolean): void {
280
+ const m = this.inertia
281
+ if (!head) {
282
+ m.offset[0] = m.offset[1] = 0
283
+ return
284
+ }
285
+ const now: [number, number, number] = [head[12], head[13], head[14]]
286
+ if (seek) {
287
+ m.p = [...now]
288
+ m.v = [0, 0, 0]
289
+ m.head = [...now]
290
+ m.offset[0] = m.offset[1] = 0
291
+ return
292
+ }
293
+ const hv = [0, 1, 2].map((k) => (dt > 0 ? (now[k] - m.head[k]) / dt : 0))
294
+ const steps = Math.ceil(dt / SUBSTEP)
295
+ const h = steps > 0 ? dt / steps : 0
296
+ for (let i = 1; i <= steps; i++) {
297
+ const t = i / steps
298
+ for (let k = 0; k < 3; k++) {
299
+ const at = m.head[k] + (now[k] - m.head[k]) * t
300
+ m.v[k] += h * spring(m.p[k] - at, m.v[k] - hv[k], INERTIA_HZ, INERTIA_ZETA)
301
+ m.p[k] += h * m.v[k]
302
+ }
303
+ }
304
+ m.head = now
305
+ const u = [m.p[0] - now[0], m.p[1] - now[1], m.p[2] - now[2]]
306
+ const right = [head[0], head[1], head[2]]
307
+ const up = [head[4], head[5], head[6]]
308
+ 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)
309
+ const cap = (x: number) => Math.max(-OFFSET_MAX, Math.min(OFFSET_MAX, x))
310
+ m.offset[0] = cap((dot(u, right) / Math.hypot(...right) / size) * INERTIA_GAIN)
311
+ m.offset[1] = cap((dot(u, up) / Math.hypot(...up) / size) * INERTIA_GAIN)
312
+ }
313
+ }
314
+
315
+ /** Zero jelly[0..5] — strength 0 is what the shader reads as off. */
316
+ export function clearJellyEyes(o: Float32Array): void {
317
+ o.fill(0, 48, 72)
318
+ }
319
+
320
+ export function clampStrength(s: number | undefined): number {
321
+ return Math.max(0, Math.min(2, s ?? 1))
322
+ }
323
+
324
+ /** Acceleration of a damped spring at displacement x, velocity v. */
325
+ function spring(x: number, v: number, hz: number, zeta: number): number {
326
+ const w = 2 * Math.PI * hz
327
+ return -w * w * x - 2 * zeta * w * v
328
+ }
329
+
330
+ /**
331
+ * Where an eye looks, as (yaw, pitch) radians: the eye in the head's frame,
332
+ * plus a share of the head in the model's. Bone matrices are column-major and
333
+ * MMD rests facing −Z, so forward is the negated third column.
334
+ */
335
+ function look(eye: Float32Array | null, head: Float32Array | null, out: [number, number]): void {
336
+ out[0] = out[1] = 0
337
+ if (!eye) return
338
+ const f = [-eye[8], -eye[9], -eye[10]]
339
+ if (head && head !== eye) {
340
+ const right = [head[0], head[1], head[2]]
341
+ const up = [head[4], head[5], head[6]]
342
+ const hf = [-head[8], -head[9], -head[10]]
343
+ const fz = dot(f, hf)
344
+ out[0] = Math.atan2(dot(f, right), fz)
345
+ out[1] = Math.atan2(dot(f, up), fz)
346
+ out[0] += HEAD_SHARE * Math.atan2(hf[0], -hf[2])
347
+ out[1] += HEAD_SHARE * Math.asin(Math.max(-1, Math.min(1, hf[1] / Math.hypot(hf[0], hf[1], hf[2]))))
348
+ } else {
349
+ // Yaw positive toward +X, as in the head-frame branch: the shader warps
350
+ // along bind-space +X, so the two must agree.
351
+ out[0] = Math.atan2(f[0], -f[2])
352
+ out[1] = Math.asin(Math.max(-1, Math.min(1, f[1] / Math.hypot(f[0], f[1], f[2]))))
353
+ }
354
+ }
355
+
356
+ const dot = (a: number[], b: number[]) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
357
+
358
+ /**
359
+ * Each iris in bind space: the vertices the eye bones carry are exactly the
360
+ * part of the eye that turns to look — the iris and pupil — so their centroid
361
+ * is its centre and their spread its size. A rig that carries both eyes on
362
+ * 両目 alone is split by side. With no carried vertices at all, the eye bones'
363
+ * own heads stand in, sized from how far apart they are.
364
+ */
365
+ function findIrises(model: Model, l: number, r: number, both: number): [EyeRest, EyeRest] {
366
+ const bones = model.getSkeleton().bones
367
+ const head = (i: number): [number, number, number] | null => {
368
+ if (i < 0) return null
369
+ const p: [number, number, number] = [0, 0, 0]
370
+ for (let b = i; b >= 0; b = bones[b].parentIndex) {
371
+ p[0] += bones[b].bindTranslation[0]
372
+ p[1] += bones[b].bindTranslation[1]
373
+ p[2] += bones[b].bindTranslation[2]
374
+ }
375
+ return p
376
+ }
377
+ const lHead = head(l)
378
+ const rHead = head(r)
379
+ // +X is the model's left; trust the bones when they say otherwise.
380
+ const leftSign = lHead && rHead ? Math.sign(lHead[0] - rHead[0]) || 1 : 1
381
+ const { positions } = model.getGeometry()
382
+ const { joints, weights } = model.getSkinning()
383
+ const sets: [number[], number[]] = [[], []]
384
+ const n = positions.length / 3
385
+ for (let v = 0; v < n; v++) {
386
+ let wl = 0
387
+ let wr = 0
388
+ let wb = 0
389
+ for (let k = 0; k < 4; k++) {
390
+ const j = joints[v * 4 + k]
391
+ const w = weights[v * 4 + k]
392
+ if (j === l) wl += w
393
+ else if (j === r) wr += w
394
+ else if (j === both) wb += w
395
+ }
396
+ if (wl >= 128) sets[0].push(v)
397
+ else if (wr >= 128) sets[1].push(v)
398
+ else if (wb >= 128) sets[Math.sign(positions[v * 3]) === leftSign ? 0 : 1].push(v)
399
+ }
400
+ const apart = lHead && rHead ? Math.hypot(lHead[0] - rHead[0], lHead[1] - rHead[1]) : 0
401
+ const fallbackRadius = apart > 0 ? apart * 0.22 : 0.3
402
+ const rest = (set: number[], bone: [number, number, number] | null, side: number): EyeRest => {
403
+ if (set.length < 3) {
404
+ const c: [number, number, number] = bone ?? [side * fallbackRadius * 2, 0, 0]
405
+ return { centre: c, radius: [fallbackRadius, fallbackRadius], lever: fallbackRadius * 2 }
406
+ }
407
+ let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity, z = 0
408
+ for (const v of set) {
409
+ const x = positions[v * 3]
410
+ const y = positions[v * 3 + 1]
411
+ minX = Math.min(minX, x)
412
+ maxX = Math.max(maxX, x)
413
+ minY = Math.min(minY, y)
414
+ maxY = Math.max(maxY, y)
415
+ z += positions[v * 3 + 2]
416
+ }
417
+ // A carried set wider than the gap between the eyes is not an iris (a
418
+ // whole-face rig weighted to the eye bone): fall back rather than wobble
419
+ // half the face.
420
+ const rx = (maxX - minX) / 2
421
+ const ry = (maxY - minY) / 2
422
+ if (apart > 0 && (rx > apart * 0.6 || ry > apart * 0.8)) {
423
+ return { centre: bone ?? [side * fallbackRadius * 2, 0, 0], radius: [fallbackRadius, fallbackRadius], lever: fallbackRadius * 2 }
424
+ }
425
+ const centre: [number, number, number] = [(minX + maxX) / 2, (minY + maxY) / 2, z / set.length]
426
+ const radius: [number, number] = [Math.max(rx, 1e-3), Math.max(ry, 1e-3)]
427
+ // An eye bone sitting on the iris itself would give no lever; such a rig
428
+ // still turns its eyes visibly, so read it as a typical eyeball's.
429
+ const r = (radius[0] + radius[1]) / 2
430
+ const lever = Math.max(bone ? Math.hypot(centre[0] - bone[0], centre[1] - bone[1], centre[2] - bone[2]) : 0, r * 1.5)
431
+ return { centre, radius, lever }
432
+ }
433
+ return [rest(sets[0], lHead, leftSign), rest(sets[1], rHead, -leftSign)]
434
+ }
@@ -216,9 +216,139 @@ 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
+ * shrunk about its centre while a lid moves, a little more down than across,
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 squash, nearly round: it shrinks both ways, a little more down than
263
+ // across, so the iris keeps its circle and only leans oval; and the bounce
264
+ // both eyes share (jelly[1].w). Held under 1.08 so a swell cannot rise
265
+ // past the upper lid.
266
+ let k = clamp(vec2f(1.0 - 0.8 * st.x, 1.0 - st.x) + j[1].w, vec2f(0.78, 0.75), vec2f(1.08, 1.08));
267
+ let moved = c.xy + (p.xy - c.xy) * k;
268
+ out = vec3f(mix(p.xy, moved, w), p.z);
269
+ }
270
+ return out;
271
+ }
272
+
273
+ /**
274
+ * The glints' frame for an eye fragment: its place in the iris in iris radii,
275
+ * from the bind-space position, which the jelly moved the iris with — so the
276
+ * glints ride the sloshing iris. Strength 0 gives w 0, which turns them off.
277
+ */
278
+ fn rzJellyEye(rp: vec3f) -> RzJelly {
279
+ let j = objectLight.jelly;
280
+ let qL = (rp.xy - j[0].xy) / max(j[2].xy, vec2f(1e-4));
281
+ let qR = (rp.xy - j[1].xy) / max(j[3].xy, vec2f(1e-4));
282
+ let right = dot(qR, qR) < dot(qL, qL);
283
+ let a = select(qL, qR, right);
284
+ let st = select(j[4], j[5], right);
285
+ let w = (1.0 - smoothstep(0.85, 1.0, length(a))) * select(0.0, 1.0, j[0].w > 0.0);
286
+ return RzJelly(a, w, select(j[2].z, j[3].z, right), st.w);
287
+ }
288
+
289
+ /**
290
+ * The jelly shine, added after the graph: two hard glints where the light
291
+ * would catch a bead — placed by the half vector in the head's frame, so they
292
+ * hold still while the iris turns under them — and the eye brightening while
293
+ * it wobbles. Flat, cel discs rather than a gradient lobe; HDR, so bloom
294
+ * takes them. Shut eyes catch nothing.
295
+ */
296
+ fn rzJellyGlow(jj: RzJelly, tex: vec3f, alpha: f32, v: vec3f, l: vec3f, sun: vec3f, shadow: f32) -> vec3f {
297
+ let strength = objectLight.jelly[0].w;
298
+ var right = vec3f(1.0, 0.0, 0.0);
299
+ var up = vec3f(0.0, 1.0, 0.0);
300
+ var fwd = vec3f(0.0, 0.0, -1.0);
301
+ if (material.headBoneIndex >= 0.0) {
302
+ let hm = skinMats[u32(material.headBoneIndex)];
303
+ right = normalize(hm[0].xyz);
304
+ up = normalize(hm[1].xyz);
305
+ fwd = -normalize(hm[2].xyz);
306
+ }
307
+ let h = normalize(l + v);
308
+ // Held in the middle and lower iris: the top of an iris sits under the upper
309
+ // lid, and a glint there — or its bloom halo — pokes out over it.
310
+ var g = vec2f(dot(h, right), dot(h, up)) * 0.6;
311
+ g = g * min(1.0, 0.25 / max(length(g), 1e-6)) - vec2f(0.0, 0.1);
312
+ g.y = min(g.y, 0.0);
313
+ let aa = max(length(fwidth(jj.src)), 1e-4);
314
+ let glint = 1.0 - smoothstep(0.16 - aa, 0.16 + aa, distance(jj.src, g));
315
+ let second = 1.0 - smoothstep(0.07 - aa, 0.07 + aa, distance(jj.src, -0.55 * g + vec2f(0.1, -0.12)));
316
+ let facing = saturate(dot(fwd, v) * 2.0);
317
+ let key = clamp(dot(sun, vec3f(0.2126, 0.7152, 0.0722)), 0.3, 2.0);
318
+ let lit = facing * key * (0.35 + 0.65 * shadow);
319
+ // HDR, well past 1, so bloom takes them as the hard sparkle of a gem.
320
+ // Bright enough for bloom to catch, not so bright its halo spreads past the
321
+ // iris onto the lid.
322
+ let glints = (glint + 0.8 * second) * lit * (1.8 + 0.8 * jj.shine);
323
+ // Gone as the lid starts down, not only once shut: a half-closed smile puts
324
+ // the lid right over where the glint sits.
325
+ let open = 1.0 - smoothstep(0.1, 0.45, jj.closure);
326
+ // Only where the eye's own picture is (its alpha), never on the iris's top
327
+ // rim, and gone as the lid comes down.
328
+ let solid = smoothstep(0.5, 0.9, alpha);
329
+ let onEye = solid * (1.0 - smoothstep(0.05, 0.35, jj.src.y));
330
+ // Light through the jelly: the light enters where the glint is and comes out
331
+ // the far side, so the iris glows in its own colour in a crescent opposite
332
+ // the glint — the lit-from-within look of a gem, or a jelly.
333
+ let r = length(jj.src);
334
+ let away = -normalize(g + vec2f(0.0, -1e-3));
335
+ let crescent = smoothstep(0.15, 0.7, dot(jj.src, away)) * (1.0 - smoothstep(0.8, 1.0, r));
336
+ let through = tex * crescent * (2.2 + 1.0 * jj.shine) * lit;
337
+ // And the colour runs deeper: the iris's own hue pushed, never its greys.
338
+ let lum = dot(tex, vec3f(0.2126, 0.7152, 0.0722));
339
+ let vivid = max(tex - vec3f(lum), vec3f(0.0)) * 1.2;
340
+ // The glass over it: a broad soft sheen across the upper iris, under the
341
+ // lid line, the dome of a bead catching the room.
342
+ let dome = (1.0 - smoothstep(0.0, 0.5, distance(jj.src * vec2f(0.8, 1.4), vec2f(0.0, 0.2)))) * 0.45 * lit;
343
+ // The glints keep off the iris's top; the soft light (sheen, glow through,
344
+ // colour) has no halo to spill, so it only needs the eye's own picture.
345
+ let gem = (vec3f(glints) * onEye + (vec3f(dome) + through + vivid) * solid) * open * jj.w;
346
+ // The whole eye shines a little brighter, more while it wobbles — not just
347
+ // the iris, so this one is not held to it (jj.w).
348
+ let lift = tex * (0.25 + 0.35 * jj.shine) * select(0.0, 1.0, strength > 0.0);
349
+ return (gem + lift) * min(strength, 1.0);
350
+ }
351
+
222
352
  /** The light arriving at a surface of this object facing n: its own ambient
223
353
  * where loaded data gave it one (a stage's probes, a character's), else the
224
354
  * world's. The same polynomial rzWorldAmbient evaluates. */
@@ -329,10 +459,11 @@ const COMMON_VS_WGSL = /* wgsl */ `
329
459
  var output: VertexOutput;
330
460
  output.vcolor = vcolor;
331
461
  output.uv2 = uv2;
332
- let pos4 = vec4f(position, 1.0);
333
462
  let weightSum = weights0.x + weights0.y + weights0.z + weights0.w;
334
463
  let invWeightSum = select(1.0, 1.0 / weightSum, weightSum > 0.0001);
335
464
  let nw = select(vec4f(1.0, 0.0, 0.0, 0.0), weights0 * invWeightSum, weightSum > 0.0001);
465
+ var pos4 = vec4f(position, 1.0);
466
+ if (objectLight.jelly[0].w > 0.0) { pos4 = vec4f(rzJellyVertex(position, joints0, nw), 1.0); }
336
467
  var skinnedPos = vec4f(0.0);
337
468
  var skinnedNrm = vec3f(0.0);
338
469
  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"}