reze-engine 0.68.21 → 0.68.23

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,2 +1,2 @@
1
- export declare const SHADOW_DEPTH_SHADER_WGSL = "\n\n/** Clump size, in object-space units \u2014 MMD's are roughly centimetres.\n *\n * Raised from 0.42: at that size the pieces are near enough to speckle that a\n * departure reads as a fade rather than as a body coming apart, which is the\n * thing the clumping exists to avoid. Hand-sized pieces are visible ones. */\nconst RZ_DISSOLVE_GRAIN: f32 = 1.2;\n/** How much the clumps pull a fragment off its own grit value \u2014 the whole of\n * the LOOK, and none of the rate. See rz_dissolve_threshold. */\nconst RZ_DISSOLVE_CLUMP: f32 = 0.6;\n/** Grit cell, as a fraction of a clump. See rz_dissolve_threshold: this is what\n * keeps a material SMALLER than a clump from having one threshold for all of\n * it, and it has to be smaller than the SMALLEST material a model carries \u2014\n * an eye highlight, a button, a buckle \u2014 not merely small. Two and a half\n * millimetres on a normal model, which is under all of them. */\nconst RZ_DISSOLVE_GRIT: f32 = 0.021;\n/** How wide the glowing front is, in threshold units. Thin: the front is a\n * rim, and a wide one lights whole limbs at once. */\nconst RZ_DISSOLVE_EDGE: f32 = 0.11;\n/** What the front burns with. Emission, added after lighting and after the\n * graph \u2014 a colour that ramps or takes a shadow reads as paint, not as heat.\n * Above 1 on purpose: it is meant to reach the bloom pyramid. Blue-violet,\n * the same light the motes leaving her are drawn and lit with. */\nconst RZ_BURN_COLOR: vec3f = vec3f(0.45, 0.62, 2.40);\n\nfn rz_dissolve_hash(p: vec3f) -> f32 {\n // INTEGER, for exactly the reason _hash33 in nodes.ts is, and its comment\n // says it: above 24 bits an f32 loses the low ones. The float hash this\n // replaces multiplied three terms into the hundreds of millions and took\n // fract() of the product \u2014 which past 2^24 is not noise, it is a quantised\n // staircase, and wherever that staircase landed on zero the threshold was\n // zero. A surface whose threshold is zero never crosses it: it stays behind\n // while the rest of her leaves, and it sits inside the burning front the\n // whole time she is gone. Those were the white pieces that would not go.\n //\n // Floored first, unlike _hash33: vec3i() truncates toward zero, so -0.5 and\n // +0.5 are the same cell, and half this model is at negative x.\n var h = vec3u(vec3i(floor(p)) + vec3i(32768));\n h = h * vec3u(1664525u, 1013904223u, 2654435761u);\n h = (h.yzx ^ h) * vec3u(2246822519u, 3266489917u, 668265263u);\n h = h ^ (h >> vec3u(16u));\n return f32((h.x ^ h.y ^ h.z) & 16777215u) * (1.0 / 16777216.0);\n}\n\n/** Value noise over that hash \u2014 smooth within a flake, uncorrelated between. */\nfn rz_dissolve_field(p: vec3f) -> f32 {\n let i = floor(p);\n let f = fract(p);\n let u = f * f * (3.0 - 2.0 * f);\n let c000 = rz_dissolve_hash(i);\n let c100 = rz_dissolve_hash(i + vec3f(1.0, 0.0, 0.0));\n let c010 = rz_dissolve_hash(i + vec3f(0.0, 1.0, 0.0));\n let c110 = rz_dissolve_hash(i + vec3f(1.0, 1.0, 0.0));\n let c001 = rz_dissolve_hash(i + vec3f(0.0, 0.0, 1.0));\n let c101 = rz_dissolve_hash(i + vec3f(1.0, 0.0, 1.0));\n let c011 = rz_dissolve_hash(i + vec3f(0.0, 1.0, 1.0));\n let c111 = rz_dissolve_hash(i + vec3f(1.0, 1.0, 1.0));\n let x00 = mix(c000, c100, u.x);\n let x10 = mix(c010, c110, u.x);\n let x01 = mix(c001, c101, u.x);\n let x11 = mix(c011, c111, u.x);\n return mix(mix(x00, x10, u.y), mix(x01, x11, u.y), u.z);\n}\n\n/**\n * The threshold this fragment is measured against.\n *\n * NOISE ALONE, so the whole body comes apart at once \u2014 a hand, a shoulder and\n * an ankle all thinning together rather than a line travelling up her. It was\n * tilted by height at first, on the argument that a sweep reads as something\n * happening TO her; what it actually reads as is a wipe, and a wipe has a\n * direction, an edge and a speed the rest of the effect knows nothing about.\n * Everywhere at once is also the honest match for what leaves: the motes are\n * shed from every limb in the same breath.\n *\n * TWO SCALES. Clumps alone meant every fragment of a material SMALLER than one\n * cell shared a single threshold: an eye highlight, a brow, a button, a hair\n * clip. All of it sat inside the glowing front at once, so the piece flashed\n * white as a whole and then popped \u2014 the two things a disintegration must never\n * do. The grit is a per-cell hash at a fraction of the clump, so a part a few\n * millimetres across still comes apart in pieces.\n *\n * THE GRIT IS THE BASE, though, and the clumps only push a fragment off it.\n * That ordering is what makes the dissolve smooth, and it is not obvious: the\n * two used to be WEIGHTED AND ADDED, and a sum is not uniformly distributed\n * however uniform each half is. It piles up in the middle, so most of the\n * surface had a threshold near a half \u2014 all of it went as the dissolve swept\n * through the middle, and what remained was the thin tail near zero. That tail\n * was the second stage everyone could see: a body that mostly vanished, then a\n * scatter of glowing scraps, then nothing.\n *\n * WRAPPED, not added. Offsetting a uniform value and taking fract() leaves it\n * EXACTLY uniform, where scaling a sum back into range only flattens the middle\n * \u2014 measured over four hundred thousand samples, the sum removed 12% of her per\n * step at the halfway mark and 0.1% at each end (a 110x swing), the scaled sum\n * still swung 89x, and the wrap is flat: 5.0% per step from the first flake to\n * the last, a ratio of 1.0.\n *\n * That flatness IS the smoothness. An uneven rate is what produced the two\n * stages you could see \u2014 a body that mostly vanished at once, then a scatter of\n * scraps that took as long again to follow.\n *\n * The clumping survives the wrap: neighbouring fragments share a clump value,\n * so the offset moves them together. What wraps past 1 comes back at 0 and goes\n * at the other end instead, which costs a little speckle in a region and buys\n * an even rate everywhere.\n */\nfn rz_dissolve_threshold(restPos: vec3f) -> f32 {\n let grit = rz_dissolve_hash(floor(restPos / (RZ_DISSOLVE_GRAIN * RZ_DISSOLVE_GRIT)));\n let clump = rz_dissolve_field(restPos / RZ_DISSOLVE_GRAIN) - 0.5;\n return fract(grit + clump * RZ_DISSOLVE_CLUMP);\n}\n\nstruct LightVP { viewProj: mat4x4f, };\n@group(0) @binding(0) var<uniform> lp: LightVP;\n@group(0) @binding(1) var<storage, read> skinMats: array<mat4x4f>;\n@group(0) @binding(2) var texSampler: sampler;\n@group(1) @binding(0) var diffuseTexture: texture_2d<f32>;\n// The head of the material block and the one field at its tail \u2014 the same\n// reach the depth prepass takes, and for the same reason: a shadow cast by a\n// body that is no longer drawn is the tell that the vanishing is a trick.\nstruct MaterialDiffuse {\n diffuse: vec4f,\n _skip0: vec4f,\n _skip1: vec4f,\n _skip2: f32,\n materialId: f32,\n _skip3: f32,\n dissolve: f32,\n};\n@group(1) @binding(1) var<uniform> material: MaterialDiffuse;\n// The model's ObjectLight, for its jelly eyes: the map must hold the iris where\n// the colour pass draws it.\nstruct ObjectLight { layers: vec4u, ambient: vec4f, sh: array<vec4f, 9>, fill: vec4f, jelly: array<vec4f, 12> }\n@group(0) @binding(3) var<uniform> objectLight: ObjectLight;\n\nfn _rzJellyTurn(q: vec2f, a: f32) -> vec2f {\n let c = cos(a);\n let s = sin(a);\n return vec2f(c * q.x - s * q.y, s * q.x + c * q.y);\n}\n\n/**\n * Where the jelly puts an iris vertex. The iris is the part of the eye the eye\n * bones carry, so it is moved as a whole, in bind space before skinning:\n * scaled about its centre (the CPU composes the blink's spring, the lid's\n * press and her motion's knock into one scale), turned and drifted a hair on\n * the spring, brought forward a hair, and rolled about the eye bone with a\n * closing expression \u2014 inside the white, which stays where it is. A highlight\n * layer (this draw's material is one of jelly[1].w's) takes the iris's move,\n * then its own scale, turn and tremble, before the roll. Vertices take it by their weight on the eye bones\n * (jelly[2].w, jelly[3].w), so nothing else on the face moves.\n */\nfn rzJellyVertex(p: vec3f, joints: vec4<u32>, nw: vec4f) -> vec3f {\n let j = objectLight.jelly;\n let ids = u32(j[1].w);\n let mid = u32(material.materialId);\n let hl = mid > 0u && (mid == (ids & 4095u) || mid == (ids >> 12u));\n var out = p;\n for (var e = 0u; e < 2u; e++) {\n let bone = j[2u + e].w;\n if (bone < 0.0) { continue; }\n let b = u32(bone);\n let w = dot(nw, select(vec4f(0.0), vec4f(1.0), joints == vec4<u32>(b)));\n if (w <= 0.0) { continue; }\n let c = j[e].xyz;\n // A rig that carries both eyes on one bone: each vertex follows its own side.\n if (j[2].w == j[3].w && e == 0u && distance(p.xy, j[1].xy) < distance(p.xy, j[0].xy)) { continue; }\n if (j[2].w == j[3].w && e == 1u && distance(p.xy, j[0].xy) <= distance(p.xy, j[1].xy)) { continue; }\n let t = j[4u + e];\n let m = j[6u + e];\n let h = j[10u + e];\n var q = _rzJellyTurn((p.xy - c.xy) * t.xy, t.z) + m.xy;\n // Forward is \u2212Z (MMD faces \u2212Z).\n var push = h.z;\n if (hl) {\n // Sliding over the iris's dome on its own, a highlight sheet a hair in\n // front of it would sink into it where the dome bulges more and the two\n // fight for depth; so it comes forward by as far as it slid \u2014 a slope of\n // one, steeper than the dome anywhere it draws.\n let before = q;\n q = _rzJellyTurn(q * h.xy, t.w) + m.zw;\n push += length(q - before);\n }\n var moved = vec3f(c.xy + q, p.z - push);\n let roll = j[8u + e];\n if (roll.w != 0.0) {\n let d = moved - roll.xyz;\n let rd = _rzJellyTurn(d.yz, roll.w);\n moved = roll.xyz + vec3f(d.x, rd.x, rd.y);\n }\n out = mix(p, moved, w);\n }\n return out;\n}\n\n\nstruct VSOut {\n @builtin(position) position: vec4f,\n @location(0) uv: vec2f,\n /** Bind-pose position, for the dissolve test \u2014 object space, as everywhere. */\n @location(1) restPos: vec3f,\n /** The triangle's own threshold, flat \u2014 see the material VertexOutput. A\n * face that is gone must stop casting, and stop as a whole face. */\n @location(2) @interpolate(flat) faceT: f32,\n};\n\n@vertex fn vs(@location(0) position: vec3f, @location(1) normal: vec3f, @location(2) uv: vec2f,\n @location(3) joints0: vec4<u32>, @location(4) weights0: vec4<f32>) -> VSOut {\n let ws = weights0.x + weights0.y + weights0.z + weights0.w;\n let inv = select(1.0, 1.0 / ws, ws > 0.0001);\n let nw = select(vec4f(1.0,0.0,0.0,0.0), weights0 * inv, ws > 0.0001);\n var pos4 = vec4f(position, 1.0);\n if (objectLight.jelly[0].w > 0.0) { pos4 = vec4f(rzJellyVertex(position, joints0, nw), 1.0); }\n var sp = vec4f(0.0);\n for (var i = 0u; i < 4u; i++) { sp += (skinMats[joints0[i]] * pos4) * nw[i]; }\n var out: VSOut;\n out.position = lp.viewProj * vec4f(sp.xyz, 1.0);\n out.uv = uv;\n out.restPos = position;\n out.faceT = rz_dissolve_threshold(position);\n return out;\n}\n\n@fragment fn fs(in: VSOut) {\n let alpha = textureSample(diffuseTexture, texSampler, in.uv).a * material.diffuse.a;\n if (alpha < 0.5) { discard; }\n // The dissolve, run as the colour pass runs it. A flake that is gone stops\n // casting: without this she leaves a whole shadow standing on the floor while\n // her body is in the air.\n if (material.dissolve < 0.9995 && in.faceT > material.dissolve) { discard; }\n}\n";
1
+ export declare const SHADOW_DEPTH_SHADER_WGSL = "\n\n/** Clump size, in object-space units \u2014 MMD's are roughly centimetres.\n *\n * Raised from 0.42: at that size the pieces are near enough to speckle that a\n * departure reads as a fade rather than as a body coming apart, which is the\n * thing the clumping exists to avoid. Hand-sized pieces are visible ones. */\nconst RZ_DISSOLVE_GRAIN: f32 = 1.2;\n/** How much the clumps pull a fragment off its own grit value \u2014 the whole of\n * the LOOK, and none of the rate. See rz_dissolve_threshold. */\nconst RZ_DISSOLVE_CLUMP: f32 = 0.6;\n/** Grit cell, as a fraction of a clump. See rz_dissolve_threshold: this is what\n * keeps a material SMALLER than a clump from having one threshold for all of\n * it, and it has to be smaller than the SMALLEST material a model carries \u2014\n * an eye highlight, a button, a buckle \u2014 not merely small. Two and a half\n * millimetres on a normal model, which is under all of them. */\nconst RZ_DISSOLVE_GRIT: f32 = 0.021;\n/** How wide the glowing front is, in threshold units. Thin: the front is a\n * rim, and a wide one lights whole limbs at once. */\nconst RZ_DISSOLVE_EDGE: f32 = 0.11;\n/** What the front burns with. Emission, added after lighting and after the\n * graph \u2014 a colour that ramps or takes a shadow reads as paint, not as heat.\n * Above 1 on purpose: it is meant to reach the bloom pyramid. Blue-violet,\n * the same light the motes leaving her are drawn and lit with. */\nconst RZ_BURN_COLOR: vec3f = vec3f(0.45, 0.62, 2.40);\n\nfn rz_dissolve_hash(p: vec3f) -> f32 {\n // INTEGER, for exactly the reason _hash33 in nodes.ts is, and its comment\n // says it: above 24 bits an f32 loses the low ones. The float hash this\n // replaces multiplied three terms into the hundreds of millions and took\n // fract() of the product \u2014 which past 2^24 is not noise, it is a quantised\n // staircase, and wherever that staircase landed on zero the threshold was\n // zero. A surface whose threshold is zero never crosses it: it stays behind\n // while the rest of her leaves, and it sits inside the burning front the\n // whole time she is gone. Those were the white pieces that would not go.\n //\n // Floored first, unlike _hash33: vec3i() truncates toward zero, so -0.5 and\n // +0.5 are the same cell, and half this model is at negative x.\n var h = vec3u(vec3i(floor(p)) + vec3i(32768));\n h = h * vec3u(1664525u, 1013904223u, 2654435761u);\n h = (h.yzx ^ h) * vec3u(2246822519u, 3266489917u, 668265263u);\n h = h ^ (h >> vec3u(16u));\n return f32((h.x ^ h.y ^ h.z) & 16777215u) * (1.0 / 16777216.0);\n}\n\n/** Value noise over that hash \u2014 smooth within a flake, uncorrelated between. */\nfn rz_dissolve_field(p: vec3f) -> f32 {\n let i = floor(p);\n let f = fract(p);\n let u = f * f * (3.0 - 2.0 * f);\n let c000 = rz_dissolve_hash(i);\n let c100 = rz_dissolve_hash(i + vec3f(1.0, 0.0, 0.0));\n let c010 = rz_dissolve_hash(i + vec3f(0.0, 1.0, 0.0));\n let c110 = rz_dissolve_hash(i + vec3f(1.0, 1.0, 0.0));\n let c001 = rz_dissolve_hash(i + vec3f(0.0, 0.0, 1.0));\n let c101 = rz_dissolve_hash(i + vec3f(1.0, 0.0, 1.0));\n let c011 = rz_dissolve_hash(i + vec3f(0.0, 1.0, 1.0));\n let c111 = rz_dissolve_hash(i + vec3f(1.0, 1.0, 1.0));\n let x00 = mix(c000, c100, u.x);\n let x10 = mix(c010, c110, u.x);\n let x01 = mix(c001, c101, u.x);\n let x11 = mix(c011, c111, u.x);\n return mix(mix(x00, x10, u.y), mix(x01, x11, u.y), u.z);\n}\n\n/**\n * The threshold this fragment is measured against.\n *\n * NOISE ALONE, so the whole body comes apart at once \u2014 a hand, a shoulder and\n * an ankle all thinning together rather than a line travelling up her. It was\n * tilted by height at first, on the argument that a sweep reads as something\n * happening TO her; what it actually reads as is a wipe, and a wipe has a\n * direction, an edge and a speed the rest of the effect knows nothing about.\n * Everywhere at once is also the honest match for what leaves: the motes are\n * shed from every limb in the same breath.\n *\n * TWO SCALES. Clumps alone meant every fragment of a material SMALLER than one\n * cell shared a single threshold: an eye highlight, a brow, a button, a hair\n * clip. All of it sat inside the glowing front at once, so the piece flashed\n * white as a whole and then popped \u2014 the two things a disintegration must never\n * do. The grit is a per-cell hash at a fraction of the clump, so a part a few\n * millimetres across still comes apart in pieces.\n *\n * THE GRIT IS THE BASE, though, and the clumps only push a fragment off it.\n * That ordering is what makes the dissolve smooth, and it is not obvious: the\n * two used to be WEIGHTED AND ADDED, and a sum is not uniformly distributed\n * however uniform each half is. It piles up in the middle, so most of the\n * surface had a threshold near a half \u2014 all of it went as the dissolve swept\n * through the middle, and what remained was the thin tail near zero. That tail\n * was the second stage everyone could see: a body that mostly vanished, then a\n * scatter of glowing scraps, then nothing.\n *\n * WRAPPED, not added. Offsetting a uniform value and taking fract() leaves it\n * EXACTLY uniform, where scaling a sum back into range only flattens the middle\n * \u2014 measured over four hundred thousand samples, the sum removed 12% of her per\n * step at the halfway mark and 0.1% at each end (a 110x swing), the scaled sum\n * still swung 89x, and the wrap is flat: 5.0% per step from the first flake to\n * the last, a ratio of 1.0.\n *\n * That flatness IS the smoothness. An uneven rate is what produced the two\n * stages you could see \u2014 a body that mostly vanished at once, then a scatter of\n * scraps that took as long again to follow.\n *\n * The clumping survives the wrap: neighbouring fragments share a clump value,\n * so the offset moves them together. What wraps past 1 comes back at 0 and goes\n * at the other end instead, which costs a little speckle in a region and buys\n * an even rate everywhere.\n */\nfn rz_dissolve_threshold(restPos: vec3f) -> f32 {\n let grit = rz_dissolve_hash(floor(restPos / (RZ_DISSOLVE_GRAIN * RZ_DISSOLVE_GRIT)));\n let clump = rz_dissolve_field(restPos / RZ_DISSOLVE_GRAIN) - 0.5;\n return fract(grit + clump * RZ_DISSOLVE_CLUMP);\n}\n\nstruct LightVP { viewProj: mat4x4f, };\n@group(0) @binding(0) var<uniform> lp: LightVP;\n@group(0) @binding(1) var<storage, read> skinMats: array<mat4x4f>;\n@group(0) @binding(2) var texSampler: sampler;\n@group(1) @binding(0) var diffuseTexture: texture_2d<f32>;\n// The head of the material block and the one field at its tail \u2014 the same\n// reach the depth prepass takes, and for the same reason: a shadow cast by a\n// body that is no longer drawn is the tell that the vanishing is a trick.\nstruct MaterialDiffuse {\n diffuse: vec4f,\n _skip0: vec4f,\n _skip1: vec4f,\n _skip2: f32,\n materialId: f32,\n _skip3: f32,\n dissolve: f32,\n};\n@group(1) @binding(1) var<uniform> material: MaterialDiffuse;\n// The model's ObjectLight, for its jelly eyes: the map must hold the iris where\n// the colour pass draws it.\nstruct ObjectLight { layers: vec4u, ambient: vec4f, sh: array<vec4f, 9>, fill: vec4f, jelly: array<vec4f, 12>, bloom: vec4f }\n@group(0) @binding(3) var<uniform> objectLight: ObjectLight;\n\nfn _rzJellyTurn(q: vec2f, a: f32) -> vec2f {\n let c = cos(a);\n let s = sin(a);\n return vec2f(c * q.x - s * q.y, s * q.x + c * q.y);\n}\n\n/**\n * Where the jelly puts an iris vertex. The iris is the part of the eye the eye\n * bones carry, so it is moved as a whole, in bind space before skinning:\n * scaled about its centre (the CPU composes the blink's spring, the lid's\n * press and her motion's knock into one scale), turned and drifted a hair on\n * the spring, brought forward a hair, and rolled about the eye bone with a\n * closing expression \u2014 inside the white, which stays where it is. A highlight\n * layer (this draw's material is one of jelly[1].w's) takes the iris's move,\n * then its own scale, turn and tremble, before the roll. Vertices take it by their weight on the eye bones\n * (jelly[2].w, jelly[3].w), so nothing else on the face moves.\n */\nfn rzJellyVertex(p: vec3f, joints: vec4<u32>, nw: vec4f) -> vec3f {\n let j = objectLight.jelly;\n let ids = u32(j[1].w);\n let mid = u32(material.materialId);\n let hl = mid > 0u && (mid == (ids & 4095u) || mid == (ids >> 12u));\n var out = p;\n for (var e = 0u; e < 2u; e++) {\n let bone = j[2u + e].w;\n if (bone < 0.0) { continue; }\n let b = u32(bone);\n let w = dot(nw, select(vec4f(0.0), vec4f(1.0), joints == vec4<u32>(b)));\n if (w <= 0.0) { continue; }\n let c = j[e].xyz;\n // A rig that carries both eyes on one bone: each vertex follows its own side.\n if (j[2].w == j[3].w && e == 0u && distance(p.xy, j[1].xy) < distance(p.xy, j[0].xy)) { continue; }\n if (j[2].w == j[3].w && e == 1u && distance(p.xy, j[0].xy) <= distance(p.xy, j[1].xy)) { continue; }\n let t = j[4u + e];\n let m = j[6u + e];\n let h = j[10u + e];\n var q = _rzJellyTurn((p.xy - c.xy) * t.xy, t.z) + m.xy;\n // Forward is \u2212Z (MMD faces \u2212Z).\n var push = h.z;\n if (hl) {\n // Sliding over the iris's dome on its own, a highlight sheet a hair in\n // front of it would sink into it where the dome bulges more and the two\n // fight for depth; so it comes forward by as far as it slid \u2014 a slope of\n // one, steeper than the dome anywhere it draws.\n let before = q;\n q = _rzJellyTurn(q * h.xy, t.w) + m.zw;\n push += length(q - before);\n }\n var moved = vec3f(c.xy + q, p.z - push);\n let roll = j[8u + e];\n if (roll.w != 0.0) {\n let d = moved - roll.xyz;\n let rd = _rzJellyTurn(d.yz, roll.w);\n moved = roll.xyz + vec3f(d.x, rd.x, rd.y);\n }\n out = mix(p, moved, w);\n }\n return out;\n}\n\n\nstruct VSOut {\n @builtin(position) position: vec4f,\n @location(0) uv: vec2f,\n /** Bind-pose position, for the dissolve test \u2014 object space, as everywhere. */\n @location(1) restPos: vec3f,\n /** The triangle's own threshold, flat \u2014 see the material VertexOutput. A\n * face that is gone must stop casting, and stop as a whole face. */\n @location(2) @interpolate(flat) faceT: f32,\n};\n\n@vertex fn vs(@location(0) position: vec3f, @location(1) normal: vec3f, @location(2) uv: vec2f,\n @location(3) joints0: vec4<u32>, @location(4) weights0: vec4<f32>) -> VSOut {\n let ws = weights0.x + weights0.y + weights0.z + weights0.w;\n let inv = select(1.0, 1.0 / ws, ws > 0.0001);\n let nw = select(vec4f(1.0,0.0,0.0,0.0), weights0 * inv, ws > 0.0001);\n var pos4 = vec4f(position, 1.0);\n if (objectLight.jelly[0].w > 0.0) { pos4 = vec4f(rzJellyVertex(position, joints0, nw), 1.0); }\n var sp = vec4f(0.0);\n for (var i = 0u; i < 4u; i++) { sp += (skinMats[joints0[i]] * pos4) * nw[i]; }\n var out: VSOut;\n out.position = lp.viewProj * vec4f(sp.xyz, 1.0);\n out.uv = uv;\n out.restPos = position;\n out.faceT = rz_dissolve_threshold(position);\n return out;\n}\n\n@fragment fn fs(in: VSOut) {\n let alpha = textureSample(diffuseTexture, texSampler, in.uv).a * material.diffuse.a;\n if (alpha < 0.5) { discard; }\n // The dissolve, run as the colour pass runs it. A flake that is gone stops\n // casting: without this she leaves a whole shadow standing on the floor while\n // her body is in the air.\n if (material.dissolve < 0.9995 && in.faceT > material.dissolve) { discard; }\n}\n";
2
2
  //# sourceMappingURL=shadow.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"shadow.d.ts","sourceRoot":"","sources":["../../../src/shaders/passes/shadow.ts"],"names":[],"mappings":"AASA,eAAO,MAAM,wBAAwB,uyWA6DpC,CAAA"}
1
+ {"version":3,"file":"shadow.d.ts","sourceRoot":"","sources":["../../../src/shaders/passes/shadow.ts"],"names":[],"mappings":"AASA,eAAO,MAAM,wBAAwB,qzWA6DpC,CAAA"}
@@ -1,4 +1,4 @@
1
- import { DISSOLVE_WGSL, JELLY_VERTEX_WGSL } from "../materials/common";
1
+ import { DISSOLVE_WGSL, JELLY_VERTEX_WGSL, OBJECT_LIGHT_STRUCT_WGSL } from "../materials/common";
2
2
  // Shadow map depth pass. Skinned VS + alpha-test FS (depth-only attachment, no
3
3
  // color targets): texels where diffuse-texture alpha × material alpha fall below
4
4
  // the cutoff are discarded, so lace casts lace-shaped shadows and a true veil
@@ -27,7 +27,7 @@ struct MaterialDiffuse {
27
27
  @group(1) @binding(1) var<uniform> material: MaterialDiffuse;
28
28
  // The model's ObjectLight, for its jelly eyes: the map must hold the iris where
29
29
  // the colour pass draws it.
30
- struct ObjectLight { layers: vec4u, ambient: vec4f, sh: array<vec4f, 9>, fill: vec4f, jelly: array<vec4f, 12> }
30
+ ${OBJECT_LIGHT_STRUCT_WGSL}
31
31
  @group(0) @binding(3) var<uniform> objectLight: ObjectLight;
32
32
  ${JELLY_VERTEX_WGSL}
33
33
 
@@ -22,6 +22,16 @@ import { Vec3 } from "./math";
22
22
  * plane and the outermost is the whole frustum, and all take the same depth
23
23
  * range.
24
24
  * tests/shadow-cascades.test.mjs pins it.
25
+ *
26
+ * The exception is the cast. While any of it is in the near slice, the first
27
+ * cascades leave the view and sit on it, the head's tile and then the whole
28
+ * figure's, so her shadow holds one detail from any distance. A shadow edge is
29
+ * known to half a texel whatever filters it, and a cel band prints that as a
30
+ * comb of teeth a texel across, so the face takes the finest tile. These two
31
+ * stand outside the invariant harmlessly: the view cascades after them nest as
32
+ * ever and hold the near slice, so the rest of the frame only drops a step, and
33
+ * a caster lies under its receiver along the light, so nothing the cull rejects
34
+ * can land on her.
25
35
  */
26
36
  export type ShadowCascade = {
27
37
  /** Texels per side of this cascade's tile — sets the snap quantum. */
@@ -72,6 +82,16 @@ export type ShadowView = {
72
82
  far: number;
73
83
  /** Distance from the eye to the camera target, along the view. */
74
84
  focus: number;
85
+ /** The sphere around the posed cast; radius 0 with nobody on stage. */
86
+ subject?: ShadowSphere;
87
+ /** The sphere around the cast's heads; radius 0 with no head bone. */
88
+ head?: ShadowSphere;
89
+ };
90
+ export type ShadowSphere = {
91
+ x: number;
92
+ y: number;
93
+ z: number;
94
+ radius: number;
75
95
  };
76
96
  /** World-space box around everything drawn, or null for an empty scene. */
77
97
  export type ShadowBounds = {
@@ -96,14 +116,17 @@ export declare function cascadeSlices(view: ShadowView, bounds: ShadowBounds): [
96
116
  export declare function sliceCorners(view: ShadowView, n: number, f: number): XYZ[];
97
117
  /**
98
118
  * One cascade's view-projection: an orthographic box around the slice's
99
- * sphere, snapped to the map's texel grid in the light's right/up plane so a
119
+ * sphere (the slice's own unless `sphere` is given), snapped to the map's texel grid in the light's right/up plane so a
100
120
  * moving camera doesn't shimmer its shadow edges, and reaching along the light
101
121
  * from the nearest thing in the scene to the farthest, so everything that
102
122
  * could cast into the slice does.
103
123
  *
104
124
  * Writes the 16 floats into `out` at `offset` and returns `out`.
105
125
  */
106
- export declare function fitShadowVP(view: ShadowView, slice: [number, number], sunDirection: XYZ, bounds: ShadowBounds, cascade: ShadowCascade, out: Float32Array, offset: number): Float32Array;
126
+ export declare function fitShadowVP(view: ShadowView, slice: [number, number], sunDirection: XYZ, bounds: ShadowBounds, cascade: ShadowCascade, out: Float32Array, offset: number, sphere?: {
127
+ center: Vec3;
128
+ radius: number;
129
+ }): Float32Array;
107
130
  /** Every cascade's bounding sphere (centre, radius), inner to outer — the
108
131
  * split spheres a game shader picks its cascade by (_CascadeShadowSplitSpheres). */
109
132
  export declare function cascadeSpheres(view: ShadowView, bounds: ShadowBounds): {
@@ -1 +1 @@
1
- {"version":3,"file":"shadow-cascades.d.ts","sourceRoot":"","sources":["../src/shadow-cascades.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,IAAI,EAAE,MAAM,QAAQ,CAAA;AAEnC;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B,sEAAsE;IACtE,OAAO,EAAE,MAAM,CAAA;IACf,4EAA4E;IAC5E,MAAM,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAClC,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,OAAO,CAAA;AAIrC,eAAO,MAAM,eAAe,EAAE,SAAS,aAAa,EAGjD,CAAA;AAEH;;;iDAGiD;AACjD,eAAO,MAAM,UAAU,KAAK,CAAA;AAE5B;;+EAE+E;AAC/E,MAAM,MAAM,UAAU,GAAG;IACvB,GAAG,EAAE;QAAE,CAAC,EAA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AAM,EAAE,MAAM,CAAC,EAAE,CAwBxF;AA+CD,4EAA4E;AAC5E,wBAAgB,YAAY,CAAC,IAAI,EAAE,UAAU,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,GAAG,EAAE,CAc1E;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CACzB,IAAI,EAAE,UAAU,EAChB,KAAK,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,EACvB,YAAY,EAAE,GAAG,EACjB,MAAM,EAAE,YAAY,EACpB,OAAO,EAAE,aAAa,EACtB,GAAG,EAAE,YAAY,EACjB,MAAM,EAAE,MAAM,EACd,MAAM;YA7EgE,IAAI;YAAU,MAAM;CA6E5C,GAC7C,YAAY,CA0Cd;AAED;qFACqF;AACrF,wBAAgB,cAAc,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,YAAY,GAAG;IAAE,MAAM,EAAE,IAAI,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EAAE,CAEzG;AAED,gEAAgE;AAChE,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,UAAU,EAAE,YAAY,EAAE,GAAG,EAAE,MAAM,EAAE,YAAY,EAAE,GAAG,EAAE,YAAY,GAAG,YAAY,CAK9H"}
@@ -16,6 +16,10 @@ export const SHADOW_CASCADES = [0, 1, 2, 3].map((i) => ({
16
16
  * reach, which keeps her texel where it was at half the tile; the three
17
17
  * outer cascades carry the rest of the room. */
18
18
  export const NEAR_REACH = 20;
19
+ /** The cast's radius rounds up to this, in world units. The radius sets the
20
+ * texel, so following it exactly would swim every shadow edge with her arms. */
21
+ const SUBJECT_RADIUS_STEP = 2;
22
+ const HEAD_RADIUS_STEP = 0.5;
19
23
  /** The deepest a cascade reaches along the view, in world units — the far
20
24
  * plane's old cap, which is where every stage's shadows were fitted before. */
21
25
  const SHADOW_FAR = 8000;
@@ -70,6 +74,27 @@ function sliceSphere(view, n, f) {
70
74
  const center = new Vec3(view.eye.x + view.forward.x * depth, view.eye.y + view.forward.y * depth, view.eye.z + view.forward.z * depth);
71
75
  return { center, radius };
72
76
  }
77
+ const stepped = (s, step) => ({
78
+ center: new Vec3(s.x, s.y, s.z),
79
+ radius: Math.ceil(s.radius / step) * step,
80
+ });
81
+ /** The sphere each cascade fits, inner to outer: the view's slices, unless any
82
+ * of the cast is in the near one. Then the head and the figure go first, and
83
+ * the view keeps what is left. */
84
+ function cascadeFits(view, bounds) {
85
+ const slices = cascadeSlices(view, bounds);
86
+ const spheres = slices.map(([n, f]) => sliceSphere(view, n, f));
87
+ const s = view.subject;
88
+ if (!s || s.radius <= 0)
89
+ return spheres;
90
+ const body = stepped(s, SUBJECT_RADIUS_STEP);
91
+ const near = spheres[0];
92
+ if (Math.hypot(s.x - near.center.x, s.y - near.center.y, s.z - near.center.z) >= near.radius + body.radius)
93
+ return spheres;
94
+ const h = view.head;
95
+ const head = h && h.radius > 0 ? stepped(h, HEAD_RADIUS_STEP) : null;
96
+ return head && head.radius < body.radius ? [head, body, spheres[1], spheres[3]] : [body, spheres[1], spheres[2], spheres[3]];
97
+ }
73
98
  /** The eight corners of a slice, for tests and for the fit's own checks. */
74
99
  export function sliceCorners(view, n, f) {
75
100
  const out = [];
@@ -88,20 +113,20 @@ export function sliceCorners(view, n, f) {
88
113
  }
89
114
  /**
90
115
  * One cascade's view-projection: an orthographic box around the slice's
91
- * sphere, snapped to the map's texel grid in the light's right/up plane so a
116
+ * sphere (the slice's own unless `sphere` is given), snapped to the map's texel grid in the light's right/up plane so a
92
117
  * moving camera doesn't shimmer its shadow edges, and reaching along the light
93
118
  * from the nearest thing in the scene to the farthest, so everything that
94
119
  * could cast into the slice does.
95
120
  *
96
121
  * Writes the 16 floats into `out` at `offset` and returns `out`.
97
122
  */
98
- export function fitShadowVP(view, slice, sunDirection, bounds, cascade, out, offset) {
123
+ export function fitShadowVP(view, slice, sunDirection, bounds, cascade, out, offset, sphere = sliceSphere(view, slice[0], slice[1])) {
99
124
  const dir = new Vec3(sunDirection.x, sunDirection.y, sunDirection.z);
100
125
  dir.normalize();
101
126
  const up = Math.abs(dir.y) > 0.99 ? new Vec3(0, 0, -1) : new Vec3(0, 1, 0);
102
127
  const right = Vec3.crossInto(up, dir, new Vec3(0, 0, 0)).normalize();
103
128
  const upv = Vec3.crossInto(dir, right, new Vec3(0, 0, 0));
104
- const { center, radius } = sliceSphere(view, slice[0], slice[1]);
129
+ const { center, radius } = sphere;
105
130
  // A texel of the map, in world units; the radius is rounded up onto the
106
131
  // grid too, so the box's size does not drift with the fov by fractions.
107
132
  const texel = Math.max((2 * radius) / cascade.mapSize, 1e-4);
@@ -134,12 +159,13 @@ export function fitShadowVP(view, slice, sunDirection, bounds, cascade, out, off
134
159
  /** Every cascade's bounding sphere (centre, radius), inner to outer — the
135
160
  * split spheres a game shader picks its cascade by (_CascadeShadowSplitSpheres). */
136
161
  export function cascadeSpheres(view, bounds) {
137
- return cascadeSlices(view, bounds).map(([n, f]) => sliceSphere(view, n, f));
162
+ return cascadeFits(view, bounds);
138
163
  }
139
164
  /** Every cascade's view-projection in a row, inner to outer. */
140
165
  export function buildShadowCascades(view, sunDirection, bounds, out) {
141
166
  const slices = cascadeSlices(view, bounds);
167
+ const fits = cascadeFits(view, bounds);
142
168
  for (let i = 0; i < SHADOW_CASCADES.length; i++)
143
- fitShadowVP(view, slices[i], sunDirection, bounds, SHADOW_CASCADES[i], out, i * 16);
169
+ fitShadowVP(view, slices[i], sunDirection, bounds, SHADOW_CASCADES[i], out, i * 16, fits[i]);
144
170
  return out;
145
171
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reze-engine",
3
- "version": "0.68.21",
3
+ "version": "0.68.23",
4
4
  "description": "A lightweight WebGPU engine for real-time 3D MMD/PMX model rendering",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
package/src/engine.ts CHANGED
@@ -198,6 +198,29 @@ import { METAL_GRAPH } from "./graph/presets/metal"
198
198
  import { CLOTH_SMOOTH_GRAPH } from "./graph/presets/cloth_smooth"
199
199
  import { CLOTH_ROUGH_GRAPH } from "./graph/presets/cloth_rough"
200
200
 
201
+ /** Grow sphere `s` (x, y, z, radius; radius 0 = empty) to hold another: the
202
+ * standard union, keeping the larger if one contains the other, else growing
203
+ * along the line between the centres. */
204
+ function growSphere(s: Float32Array, x: number, y: number, z: number, rad: number): void {
205
+ if (rad <= 0) return
206
+ const r = s[3]
207
+ const d = Math.hypot(x - s[0], y - s[1], z - s[2])
208
+ if (r > 0 && d + rad <= r) return
209
+ if (r <= 0 || d + r <= rad) {
210
+ s[0] = x
211
+ s[1] = y
212
+ s[2] = z
213
+ s[3] = rad
214
+ return
215
+ }
216
+ const nr = (d + r + rad) * 0.5
217
+ const t = (nr - r) / d
218
+ s[0] += (x - s[0]) * t
219
+ s[1] += (y - s[1]) * t
220
+ s[2] += (z - s[2]) * t
221
+ s[3] = nr
222
+ }
223
+
201
224
  // Material preset dispatch. Consumers supply a MaterialPresetMap assigning material names
202
225
  // to presets; unmapped materials fall back to "default" (Principled BSDF).
203
226
  export type MaterialPreset =
@@ -456,6 +479,9 @@ const MAX_EFFECT_ANCHORS = EFFECT_ANCHORS
456
479
 
457
480
  /** Only for the bounding sphere's height. */
458
481
  const HEAD_BONE = "頭"
482
+ /** Around the head bone in model units: the face, the hair over it and the
483
+ * neck it shadows, on a figure about 20 tall. */
484
+ const SHADOW_HEAD_RADIUS = 3
459
485
  /** Scratch for the per-frame camera-to-model-space hand-off to the eyes. */
460
486
  const _gazeScratch = new Vec3(0, 0, 0)
461
487
 
@@ -1068,7 +1094,7 @@ interface ModelInstance {
1068
1094
  shadowDrawCalls: DrawCall[]
1069
1095
  shadowBindGroups: GPUBindGroup[]
1070
1096
  mainPerInstanceBindGroup: GPUBindGroup
1071
- /** Its ObjectLight — rendering layers, its own ambient, its fill — and the CPU copy it
1097
+ /** Its ObjectLight — rendering layers, its own ambient, its fill, its bloom — and the CPU copy it
1072
1098
  * is written from. Every model has one: its layers decide its lights. */
1073
1099
  lightBuffer: GPUBuffer
1074
1100
  objectLight: Float32Array<ArrayBuffer>
@@ -12666,6 +12692,20 @@ export class Engine {
12666
12692
  return true
12667
12693
  }
12668
12694
 
12695
+ /**
12696
+ * Whether one model feeds the bloom. Off, its surfaces and outlines write 0
12697
+ * to the bloom mask, as the ground does: its own bright pixels stop glowing,
12698
+ * while a glow from behind it still spreads across it. A model drawn by the
12699
+ * game's own shaders (src/unity) writes its own mask and takes none of this.
12700
+ */
12701
+ setModelBloom(name: string, on: boolean): boolean {
12702
+ const inst = this.modelInstances.get(name)
12703
+ if (!inst || !this.device) return false
12704
+ inst.objectLight[96] = on ? 1 : 0
12705
+ this.device.queue.writeBuffer(inst.lightBuffer, 96 * 4, inst.objectLight, 96, 4)
12706
+ return true
12707
+ }
12708
+
12669
12709
  /** The per-model group: its skinning matrices and its ObjectLight. */
12670
12710
  private perInstanceBindGroup(name: string, skinMatrixBuffer: GPUBuffer, light: GPUBuffer): GPUBindGroup {
12671
12711
  return this.device.createBindGroup({
@@ -14531,6 +14571,7 @@ export class Engine {
14531
14571
  }
14532
14572
  if (this.cullModelBuffer) this.device.queue.writeBuffer(this.cullModelBuffer, 0, data.buffer as ArrayBuffer)
14533
14573
  this.updateCasterSphere(data)
14574
+ this.updateShadowSubject(data)
14534
14575
  this.updateShadowSceneBounds(data, flags)
14535
14576
  }
14536
14577
 
@@ -14629,12 +14670,7 @@ export class Engine {
14629
14670
  */
14630
14671
  private updateCasterSphere(data: Float32Array): void {
14631
14672
  const out = this.casterSphere
14632
- out[3] = 0
14633
- let cx = 0
14634
- let cy = 0
14635
- let cz = 0
14636
- let r = 0
14637
- let any = false
14673
+ out.fill(0)
14638
14674
  for (let i = 0; i < this.cullModels.length; i++) {
14639
14675
  const inst = this.cullModels[i]
14640
14676
  if (!inst.model.visible || inst.shadowDrawCalls.length === 0) continue
@@ -14644,44 +14680,44 @@ export class Engine {
14644
14680
  return
14645
14681
  }
14646
14682
  const o = i * Engine.CULL_MODEL_FLOATS + 16
14647
- const x = data[o]
14648
- const y = data[o + 1]
14649
- const z = data[o + 2]
14650
- const rad = data[o + 3]
14651
- if (rad <= 0) continue
14652
- if (!any) {
14653
- cx = x
14654
- cy = y
14655
- cz = z
14656
- r = rad
14657
- any = true
14658
- continue
14659
- }
14660
- // Union of two spheres, the standard construction: if one already contains
14661
- // the other keep it, else grow along the line between the centres.
14662
- const dx = x - cx
14663
- const dy = y - cy
14664
- const dz = z - cz
14665
- const d = Math.hypot(dx, dy, dz)
14666
- if (d + rad <= r) continue
14667
- if (d + r <= rad) {
14668
- cx = x
14669
- cy = y
14670
- cz = z
14671
- r = rad
14672
- continue
14673
- }
14674
- const nr = (d + r + rad) * 0.5
14675
- const t = (nr - r) / d
14676
- cx += dx * t
14677
- cy += dy * t
14678
- cz += dz * t
14679
- r = nr
14683
+ growSphere(out, data[o], data[o + 1], data[o + 2], data[o + 3])
14684
+ }
14685
+ }
14686
+
14687
+ /** The cast's sphere and the sphere around its heads, stage and cards aside:
14688
+ * what the first shadow cascades sit on (shadow-cascades.ts). */
14689
+ private updateShadowSubject(data: Float32Array): void {
14690
+ const out = this.shadowSubjectSphere
14691
+ const heads = this.shadowHeadSphere
14692
+ out.fill(0)
14693
+ heads.fill(0)
14694
+ for (let i = 0; i < this.cullModels.length; i++) {
14695
+ const inst = this.cullModels[i]
14696
+ if (!inst.model.visible || inst.rigid || inst.isStage || inst.isPlane || inst.isProp || inst.shadowDrawCalls.length === 0) continue
14697
+ const o = i * Engine.CULL_MODEL_FLOATS + 16
14698
+ growSphere(out, data[o], data[o + 1], data[o + 2], data[o + 3])
14699
+ const m = inst.model
14700
+ const w = m.getBoneWorldMatrix(HEAD_BONE)
14701
+ if (!w) continue
14702
+ // Model space → world, as the cull's spheres compose it.
14703
+ const c = cullScratchVec
14704
+ c.setXYZ(w[12] * m.scale, w[13] * m.scale, w[14] * m.scale)
14705
+ Quat.rotateVecInto(m.rotation, c, c)
14706
+ growSphere(heads, c.x + m.position.x, c.y + m.position.y, c.z + m.position.z, SHADOW_HEAD_RADIUS * m.scale)
14707
+ }
14708
+ const { subject, head } = this.shadowView
14709
+ if (subject) {
14710
+ subject.x = out[0]
14711
+ subject.y = out[1]
14712
+ subject.z = out[2]
14713
+ subject.radius = out[3]
14714
+ }
14715
+ if (head) {
14716
+ head.x = heads[0]
14717
+ head.y = heads[1]
14718
+ head.z = heads[2]
14719
+ head.radius = heads[3]
14680
14720
  }
14681
- out[0] = cx
14682
- out[1] = cy
14683
- out[2] = cz
14684
- out[3] = any ? r : 0
14685
14721
  }
14686
14722
 
14687
14723
  /** Every shadow caster in one sphere: (x, y, z, radius). radius 0 = nothing
@@ -14689,6 +14725,8 @@ export class Engine {
14689
14725
  /** A card's own texture, by model key — see setPlaneFrame. */
14690
14726
  private planeTextures = new Map<string, GPUTexture>()
14691
14727
  private casterSphere = new Float32Array(4)
14728
+ private shadowSubjectSphere = new Float32Array(4)
14729
+ private shadowHeadSphere = new Float32Array(4)
14692
14730
 
14693
14731
  /** The ground's uniform block, kept so the caster sphere can be refreshed in
14694
14732
  * it every frame rather than rebuilding the buffer (addGround allocates). */
@@ -15431,8 +15469,9 @@ export class Engine {
15431
15469
  // the cast on the character layer as well as the default one. Words:
15432
15470
  // layers [0..3] (y: a stage, which takes the cast's shadow —
15433
15471
  // rzRoofedCastShadow), ambient flag [4..7], SH [8..43], fill [44..47],
15434
- // jelly eyes [48..71] — see materials/common.ts.
15435
- const objectLight = new Float32Array(96)
15472
+ // jelly eyes [48..95], bloom [96..99] — see materials/common.ts.
15473
+ const objectLight = new Float32Array(100)
15474
+ objectLight[96] = 1
15436
15475
  const objectLayers = new Uint32Array(objectLight.buffer)
15437
15476
  objectLayers[0] =
15438
15477
  isStage || isPlane || isProp ? RENDERING_LAYER_DEFAULT : (RENDERING_LAYER_DEFAULT | RENDERING_LAYER_CHARACTER) >>> 0
@@ -15990,6 +16029,8 @@ export class Engine {
15990
16029
  near: 1,
15991
16030
  far: 100,
15992
16031
  focus: 10,
16032
+ subject: { x: 0, y: 0, z: 0, radius: 0 },
16033
+ head: { x: 0, y: 0, z: 0, radius: 0 },
15993
16034
  }
15994
16035
 
15995
16036
  private updateShadowLightVP() {
@@ -19347,7 +19388,8 @@ export class Engine {
19347
19388
  // shoe's sole's hull cut the upper behind it, and the floor showed through
19348
19389
  // as pale lines along the seam. After every surface of this phase it only
19349
19390
  // ever blends over finished ones. Same index ranges; own pipeline and
19350
- // groups 0/2 — group 1 (skinMats) is layout-identical and stays bound.
19391
+ // groups 0/2 — group 1 (skinMats, ObjectLight) is layout-identical and
19392
+ // stays bound.
19351
19393
  if (outlined.length) {
19352
19394
  const mirrored = view.args === "mirror"
19353
19395
  pass.setPipeline(mirrored ? this.outlineMirrorPipeline : this.outlinePipeline)
@@ -250,13 +250,13 @@ ${ID_WRITE} return out;
250
250
 
251
251
  var out: FSOut;
252
252
  out.color = vec4f(final_color${LIT}${BURN}, outAlpha);
253
- ${CAST}${FOG} out.mask = vec4f(1.0, 1.0, _rzSubsurface, out.color.a);
253
+ ${CAST}${FOG} out.mask = vec4f(objectLight.bloom.x, 1.0, _rzSubsurface, out.color.a);
254
254
  ${ID_WRITE} return out;
255
255
  `
256
256
  }
257
257
  return `${ALBEDO} var out: FSOut;
258
258
  out.color = vec4f(final_color${LIT}${BURN}, ${alphaBase});
259
- ${CAST}${FOG} out.mask = vec4f(1.0, 1.0, _rzSubsurface, out.color.a);
259
+ ${CAST}${FOG} out.mask = vec4f(objectLight.bloom.x, 1.0, _rzSubsurface, out.color.a);
260
260
  ${ID_WRITE} return out;
261
261
  `
262
262
  }
@@ -93,6 +93,11 @@ fn rzJellyVertex(p: vec3f, joints: vec4<u32>, nw: vec4f) -> vec3f {
93
93
  }
94
94
  `
95
95
 
96
+ /** A model's ObjectLight as WGSL declares it (see the layout below). One copy
97
+ * for every pass that binds it, so their layouts cannot drift. */
98
+ export const OBJECT_LIGHT_STRUCT_WGSL =
99
+ "struct ObjectLight { layers: vec4u, ambient: vec4f, sh: array<vec4f, 9>, fill: vec4f, jelly: array<vec4f, 12>, bloom: vec4f }"
100
+
96
101
  export const COMMON_BINDINGS_WGSL = /* wgsl */ `
97
102
 
98
103
  struct CameraUniforms {
@@ -299,7 +304,10 @@ fn _rzCastShadow(wp: vec3f) -> f32 {
299
304
  // [9] the right's
300
305
  // [10] left highlight scale x, y, forward push (bind units), 0
301
306
  // [11] the right's
302
- struct ObjectLight { layers: vec4u, ambient: vec4f, sh: array<vec4f, 9>, fill: vec4f, jelly: array<vec4f, 12> }
307
+ // bloom.x 1 while it feeds the bloom, 0 when it is left out
308
+ // (Engine.setModelBloom): the bloom mask its surfaces and
309
+ // outlines write.
310
+ ${OBJECT_LIGHT_STRUCT_WGSL}
303
311
  @group(1) @binding(1) var<uniform> objectLight: ObjectLight;
304
312
 
305
313
  ${JELLY_VERTEX_WGSL}
@@ -17,7 +17,7 @@
17
17
  // a string baked at import cannot know what the device said.
18
18
 
19
19
  import { sceneFsOutWgsl, sceneIdPadWgsl } from "./scene-contract"
20
- import { DISSOLVE_WGSL } from "../materials/common"
20
+ import { DISSOLVE_WGSL, OBJECT_LIGHT_STRUCT_WGSL } from "../materials/common"
21
21
 
22
22
  /**
23
23
  * Where `dissolve` sits in the outline's own uniform, in bytes.
@@ -89,6 +89,8 @@ struct OutlineLights { ambientColor: vec4f, lights: array<OutlineLight, 4>, };
89
89
  @group(0) @binding(1) var edgeSampler: sampler;
90
90
  @group(0) @binding(2) var<uniform> light: OutlineLights;
91
91
  @group(1) @binding(0) var<storage, read> skinMats: array<mat4x4f>;
92
+ ${OBJECT_LIGHT_STRUCT_WGSL}
93
+ @group(1) @binding(1) var<uniform> objectLight: ObjectLight;
92
94
  @group(2) @binding(0) var<uniform> material: MaterialUniforms;
93
95
  @group(2) @binding(1) var diffuseTexture: texture_2d<f32>;
94
96
  /** A look's outline tint (ShaderGraph.outline.tint): one of its images, white
@@ -288,7 +290,7 @@ ${sceneFsOutWgsl({ name: "FSOut", aux: "mask" })}
288
290
  rgb = tex.rgb * tint * mix(material.shadowColor.rgb, rgb, lit);
289
291
  }
290
292
  out.color = vec4f(rgb, material.edgeColor.a * texA * input.coverage);
291
- out.mask = vec4f(1.0, 1.0, 0.0, out.color.a);
293
+ out.mask = vec4f(objectLight.bloom.x, 1.0, 0.0, out.color.a);
292
294
  ${sceneIdPadWgsl("out")} return out;
293
295
  }
294
296
  `
@@ -1,4 +1,4 @@
1
- import { DISSOLVE_WGSL, JELLY_VERTEX_WGSL } from "../materials/common"
1
+ import { DISSOLVE_WGSL, JELLY_VERTEX_WGSL, OBJECT_LIGHT_STRUCT_WGSL } from "../materials/common"
2
2
 
3
3
  // Shadow map depth pass. Skinned VS + alpha-test FS (depth-only attachment, no
4
4
  // color targets): texels where diffuse-texture alpha × material alpha fall below
@@ -29,7 +29,7 @@ struct MaterialDiffuse {
29
29
  @group(1) @binding(1) var<uniform> material: MaterialDiffuse;
30
30
  // The model's ObjectLight, for its jelly eyes: the map must hold the iris where
31
31
  // the colour pass draws it.
32
- struct ObjectLight { layers: vec4u, ambient: vec4f, sh: array<vec4f, 9>, fill: vec4f, jelly: array<vec4f, 12> }
32
+ ${OBJECT_LIGHT_STRUCT_WGSL}
33
33
  @group(0) @binding(3) var<uniform> objectLight: ObjectLight;
34
34
  ${JELLY_VERTEX_WGSL}
35
35
 
@@ -23,6 +23,16 @@ import { Mat4, Vec3 } from "./math"
23
23
  * plane and the outermost is the whole frustum, and all take the same depth
24
24
  * range.
25
25
  * tests/shadow-cascades.test.mjs pins it.
26
+ *
27
+ * The exception is the cast. While any of it is in the near slice, the first
28
+ * cascades leave the view and sit on it, the head's tile and then the whole
29
+ * figure's, so her shadow holds one detail from any distance. A shadow edge is
30
+ * known to half a texel whatever filters it, and a cel band prints that as a
31
+ * comb of teeth a texel across, so the face takes the finest tile. These two
32
+ * stand outside the invariant harmlessly: the view cascades after them nest as
33
+ * ever and hold the near slice, so the rest of the frame only drops a step, and
34
+ * a caster lies under its receiver along the light, so nothing the cull rejects
35
+ * can land on her.
26
36
  */
27
37
  export type ShadowCascade = {
28
38
  /** Texels per side of this cascade's tile — sets the snap quantum. */
@@ -52,6 +62,11 @@ export const SHADOW_CASCADES: readonly ShadowCascade[] = [0, 1, 2, 3].map((i) =>
52
62
  * outer cascades carry the rest of the room. */
53
63
  export const NEAR_REACH = 20
54
64
 
65
+ /** The cast's radius rounds up to this, in world units. The radius sets the
66
+ * texel, so following it exactly would swim every shadow edge with her arms. */
67
+ const SUBJECT_RADIUS_STEP = 2
68
+ const HEAD_RADIUS_STEP = 0.5
69
+
55
70
  /** The view the cascades are fitted to: the camera's eye and basis (world
56
71
  * space, left-handed, +Z forward as the projection is), its vertical field of
57
72
  * view, aspect and clip planes, and how far away the thing it looks at is. */
@@ -66,8 +81,14 @@ export type ShadowView = {
66
81
  far: number
67
82
  /** Distance from the eye to the camera target, along the view. */
68
83
  focus: number
84
+ /** The sphere around the posed cast; radius 0 with nobody on stage. */
85
+ subject?: ShadowSphere
86
+ /** The sphere around the cast's heads; radius 0 with no head bone. */
87
+ head?: ShadowSphere
69
88
  }
70
89
 
90
+ export type ShadowSphere = { x: number; y: number; z: number; radius: number }
91
+
71
92
  /** World-space box around everything drawn, or null for an empty scene. */
72
93
  export type ShadowBounds = { min: [number, number, number]; max: [number, number, number] } | null
73
94
 
@@ -133,6 +154,29 @@ function sliceSphere(view: ShadowView, n: number, f: number): { center: Vec3; ra
133
154
  return { center, radius }
134
155
  }
135
156
 
157
+ type Sphere = { center: Vec3; radius: number }
158
+
159
+ const stepped = (s: ShadowSphere, step: number): Sphere => ({
160
+ center: new Vec3(s.x, s.y, s.z),
161
+ radius: Math.ceil(s.radius / step) * step,
162
+ })
163
+
164
+ /** The sphere each cascade fits, inner to outer: the view's slices, unless any
165
+ * of the cast is in the near one. Then the head and the figure go first, and
166
+ * the view keeps what is left. */
167
+ function cascadeFits(view: ShadowView, bounds: ShadowBounds): Sphere[] {
168
+ const slices = cascadeSlices(view, bounds)
169
+ const spheres = slices.map(([n, f]) => sliceSphere(view, n, f))
170
+ const s = view.subject
171
+ if (!s || s.radius <= 0) return spheres
172
+ const body = stepped(s, SUBJECT_RADIUS_STEP)
173
+ const near = spheres[0]
174
+ if (Math.hypot(s.x - near.center.x, s.y - near.center.y, s.z - near.center.z) >= near.radius + body.radius) return spheres
175
+ const h = view.head
176
+ const head = h && h.radius > 0 ? stepped(h, HEAD_RADIUS_STEP) : null
177
+ return head && head.radius < body.radius ? [head, body, spheres[1], spheres[3]] : [body, spheres[1], spheres[2], spheres[3]]
178
+ }
179
+
136
180
  /** The eight corners of a slice, for tests and for the fit's own checks. */
137
181
  export function sliceCorners(view: ShadowView, n: number, f: number): XYZ[] {
138
182
  const out: XYZ[] = []
@@ -152,7 +196,7 @@ export function sliceCorners(view: ShadowView, n: number, f: number): XYZ[] {
152
196
 
153
197
  /**
154
198
  * One cascade's view-projection: an orthographic box around the slice's
155
- * sphere, snapped to the map's texel grid in the light's right/up plane so a
199
+ * sphere (the slice's own unless `sphere` is given), snapped to the map's texel grid in the light's right/up plane so a
156
200
  * moving camera doesn't shimmer its shadow edges, and reaching along the light
157
201
  * from the nearest thing in the scene to the farthest, so everything that
158
202
  * could cast into the slice does.
@@ -167,6 +211,7 @@ export function fitShadowVP(
167
211
  cascade: ShadowCascade,
168
212
  out: Float32Array,
169
213
  offset: number,
214
+ sphere = sliceSphere(view, slice[0], slice[1]),
170
215
  ): Float32Array {
171
216
  const dir = new Vec3(sunDirection.x, sunDirection.y, sunDirection.z)
172
217
  dir.normalize()
@@ -174,7 +219,7 @@ export function fitShadowVP(
174
219
  const right = Vec3.crossInto(up, dir, new Vec3(0, 0, 0)).normalize()
175
220
  const upv = Vec3.crossInto(dir, right, new Vec3(0, 0, 0))
176
221
 
177
- const { center, radius } = sliceSphere(view, slice[0], slice[1])
222
+ const { center, radius } = sphere
178
223
  // A texel of the map, in world units; the radius is rounded up onto the
179
224
  // grid too, so the box's size does not drift with the fov by fractions.
180
225
  const texel = Math.max((2 * radius) / cascade.mapSize, 1e-4)
@@ -214,12 +259,13 @@ export function fitShadowVP(
214
259
  /** Every cascade's bounding sphere (centre, radius), inner to outer — the
215
260
  * split spheres a game shader picks its cascade by (_CascadeShadowSplitSpheres). */
216
261
  export function cascadeSpheres(view: ShadowView, bounds: ShadowBounds): { center: Vec3; radius: number }[] {
217
- return cascadeSlices(view, bounds).map(([n, f]) => sliceSphere(view, n, f))
262
+ return cascadeFits(view, bounds)
218
263
  }
219
264
 
220
265
  /** Every cascade's view-projection in a row, inner to outer. */
221
266
  export function buildShadowCascades(view: ShadowView, sunDirection: XYZ, bounds: ShadowBounds, out: Float32Array): Float32Array {
222
267
  const slices = cascadeSlices(view, bounds)
223
- for (let i = 0; i < SHADOW_CASCADES.length; i++) fitShadowVP(view, slices[i], sunDirection, bounds, SHADOW_CASCADES[i], out, i * 16)
268
+ const fits = cascadeFits(view, bounds)
269
+ for (let i = 0; i < SHADOW_CASCADES.length; i++) fitShadowVP(view, slices[i], sunDirection, bounds, SHADOW_CASCADES[i], out, i * 16, fits[i])
224
270
  return out
225
271
  }