reze-engine 0.25.0 → 0.25.1

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,205 +1,207 @@
1
- // Shared WGSL blocks concatenated by every material shader.
2
- // Splits the boilerplate (uniform structs, bind group layout, skinning VS, PCF shadow)
3
- // away from the per-material fragment code so each material file only contains what
4
- // makes it visually distinct.
5
- //
6
- // Concat order in every material:
7
- // NODES_WGSL (nodes.ts — math/noise/BSDF helpers)
8
- // COMMON_BINDINGS_WGSL (uniform structs + @group/@binding declarations)
9
- // SAMPLE_SHADOW_WGSL (3×3 PCF shadow sampler; reads bindings above)
10
- // COMMON_VS_WGSL (skinning vertex shader; reads bindings above)
11
- // <material's own constants + @fragment fn fs>
12
- //
13
- // WGSL is a whole-module compile — declaration order at module scope doesn't matter,
14
- // but the readable order is: types → bindings → helpers → entry points.
15
-
16
- // ─── Uniform structs + bind group layout ────────────────────────────
17
- // Every material pipeline uses the same bind group layout, so the same bindings are
18
- // declared here once. Groups:
19
- // group(0): per-frame scene (camera, lights, shadow map, BRDF LUT via nodes.ts)
20
- // group(1): per-model skinning
21
- // group(2): per-material (diffuse texture + material uniforms)
22
-
23
- export const COMMON_BINDINGS_WGSL = /* wgsl */ `
24
-
25
- struct CameraUniforms {
26
- view: mat4x4f,
27
- projection: mat4x4f,
28
- viewPos: vec3f,
29
- _padding: f32,
30
- };
31
-
32
- struct Light {
33
- direction: vec4f,
34
- color: vec4f,
35
- };
36
-
37
- struct LightUniforms {
38
- ambientColor: vec4f,
39
- lights: array<Light, 4>,
40
- };
41
-
42
- // Per-material uniforms. Every material binds this layout even if it ignores fields;
43
- // the engine keeps one bind group layout across all material pipelines. The PMX
44
- // classic-material fields (ambient/specular/shininess) are carried for graph nodes that
45
- // want them; most graphs read only diffuseColor + alpha.
46
- struct MaterialUniforms {
47
- diffuseColor: vec3f, // PMX diffuse rgb — the material_diffuse node reads this
48
- alpha: f32, // 0 → discard; <1 → transparent draw call
49
- ambient: vec3f, // PMX ambient rgb
50
- shininess: f32, // PMX specular power
51
- specular: vec3f, // PMX specular rgb
52
- sphereMode: f32, // 0 none · 1 multiply (sph) · 2 add (spa)
53
- // Skeleton index of the 頭 (head) bone, or -1. Lets the eye shader gate
54
- // the post-alpha-eye stencil by camera-vs-face hemisphere.
55
- headBoneIndex: f32,
56
- _pad0: f32,
57
- _pad1: f32,
58
- _pad2: f32,
59
- };
60
-
61
- struct VertexOutput {
62
- @builtin(position) position: vec4f,
63
- @location(0) normal: vec3f,
64
- @location(1) uv: vec2f,
65
- @location(2) worldPos: vec3f,
66
- // Bind-pose object-space position (the raw pre-skin vertex attribute). Procedural
67
- // textures (noise bump, sparkle, Generated-coord gradients) key off this instead of
68
- // worldPos so the pattern rides with the surface — otherwise the mesh swims through a
69
- // world-static noise field under any skinning deformation or root (センター) motion.
70
- // At rest skinMats are identity so restPos == worldPos, which is why existing noise-
71
- // scale constants stay valid without retuning.
72
- @location(3) restPos: vec3f,
73
- };
74
-
75
- struct LightVP { viewProj: mat4x4f, };
76
-
77
- @group(0) @binding(0) var<uniform> camera: CameraUniforms;
78
- @group(0) @binding(1) var<uniform> light: LightUniforms;
79
- @group(0) @binding(2) var diffuseSampler: sampler;
80
- @group(0) @binding(3) var shadowMap: texture_depth_2d;
81
- @group(0) @binding(4) var shadowSampler: sampler_comparison;
82
- @group(0) @binding(5) var<uniform> lightVP: LightVP;
83
- // binding(9) brdfLut is declared inside NODES_WGSL (nodes.ts).
84
- @group(1) @binding(0) var<storage, read> skinMats: array<mat4x4f>;
85
- @group(2) @binding(0) var diffuseTexture: texture_2d<f32>;
86
- @group(2) @binding(1) var<uniform> material: MaterialUniforms;
87
- // Reserved for future sphere/toon graph nodes; graphs that don't read them get the
88
- // 1×1 white fallback bound here.
89
- @group(2) @binding(2) var toonTexture: texture_2d<f32>;
90
- @group(2) @binding(3) var sphereTexture: texture_2d<f32>;
91
-
92
- // Four-bone blended normals can cancel to ~zero on physics-driven parts
93
- // (opposing bone rotations at 50/50 weights) — normalize(0) is 0/0 = NaN,
94
- // which poisons the whole shading stack and flashes through bloom. Fall
95
- // back to up for degenerate normals instead.
96
- fn safe_normal(nIn: vec3f) -> vec3f {
97
- let l2 = dot(nIn, nIn);
98
- if (l2 < 1e-12) { return vec3f(0.0, 1.0, 0.0); }
99
- return nIn * inverseSqrt(l2);
100
- }
101
-
102
- `;
103
-
104
- // ─── Shadow sampler (3×3 PCF) ───────────────────────────────────────
105
- // 2048-map, normal-bias 0.08, depth-bias 0.001. Unrolled — Safari's Metal backend
106
- // doesn't unroll nested shadow loops reliably, and the early out on back-facing
107
- // fragments saves 9 texture taps per skipped pixel.
108
-
109
- export const SAMPLE_SHADOW_WGSL = /* wgsl */ `
110
-
111
- fn sampleShadow(worldPos: vec3f, n: vec3f) -> f32 {
112
- if (dot(n, -light.lights[0].direction.xyz) <= 0.0) { return 0.0; }
113
- let biasedPos = worldPos + n * 0.08;
114
- let lclip = lightVP.viewProj * vec4f(biasedPos, 1.0);
115
- let ndc = lclip.xyz / max(lclip.w, 1e-6);
116
- let suv = vec2f(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5);
117
- let cmpZ = ndc.z - 0.001;
118
- let ts = 1.0 / 2048.0;
119
- let s00 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(-ts, -ts), cmpZ);
120
- let s10 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(0.0, -ts), cmpZ);
121
- let s20 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f( ts, -ts), cmpZ);
122
- let s01 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(-ts, 0.0), cmpZ);
123
- let s11 = textureSampleCompareLevel(shadowMap, shadowSampler, suv, cmpZ);
124
- let s21 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f( ts, 0.0), cmpZ);
125
- let s02 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(-ts, ts), cmpZ);
126
- let s12 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(0.0, ts), cmpZ);
127
- let s22 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f( ts, ts), cmpZ);
128
- return (s00 + s10 + s20 + s01 + s11 + s21 + s02 + s12 + s22) * (1.0 / 9.0);
129
- }
130
-
131
- `;
132
-
133
- // ─── Skinning vertex shader ─────────────────────────────────────────
134
- // Four-bone linear blend skinning. Renormalizes weights when they don't sum to 1
135
- // (PMX models occasionally ship with unnormalized weights on extras like hair tips).
136
- // VS normalize on the outgoing normal is skipped interpolation denormalizes it
137
- // anyway and every fragment shader does `normalize(input.normal)` as its first line.
138
-
139
- export const COMMON_VS_WGSL = /* wgsl */ `
140
-
141
- @vertex fn vs(
142
- @location(0) position: vec3f,
143
- @location(1) normal: vec3f,
144
- @location(2) uv: vec2f,
145
- @location(3) joints0: vec4<u32>,
146
- @location(4) weights0: vec4<f32>
147
- ) -> VertexOutput {
148
- var output: VertexOutput;
149
- let pos4 = vec4f(position, 1.0);
150
- let weightSum = weights0.x + weights0.y + weights0.z + weights0.w;
151
- let invWeightSum = select(1.0, 1.0 / weightSum, weightSum > 0.0001);
152
- let nw = select(vec4f(1.0, 0.0, 0.0, 0.0), weights0 * invWeightSum, weightSum > 0.0001);
153
- var skinnedPos = vec4f(0.0);
154
- var skinnedNrm = vec3f(0.0);
155
- for (var i = 0u; i < 4u; i++) {
156
- let m = skinMats[joints0[i]];
157
- let w = nw[i];
158
- skinnedPos += (m * pos4) * w;
159
- skinnedNrm += (mat3x3f(m[0].xyz, m[1].xyz, m[2].xyz) * normal) * w;
160
- }
161
- output.position = camera.projection * camera.view * vec4f(skinnedPos.xyz, 1.0);
162
- output.normal = skinnedNrm;
163
- output.uv = uv;
164
- output.worldPos = skinnedPos.xyz;
165
- output.restPos = position;
166
- return output;
167
- }
168
-
169
- `;
170
-
171
- // ─── FS output struct ───────────────────────────────────────────────
172
- // Location 0: final radiance+alpha (blended into rg11b10ufloat; the HDR target
173
- // has no alpha channel, but the blend equation still uses the .a you write here
174
- // as the src-alpha factor that premultiplies rgb into the HDR target).
175
- // Location 1: auxiliary rg8unorm carrying
176
- // .r = bloom mask (1 = contributes to bloom, 0 = skip — e.g. ground).
177
- // .g = accumulated canvas alpha — the channel that used to live in hdr.a
178
- // before the switch to rg11b10ufloat. Sampled by composite to
179
- // un-premultiply color for tonemap and to set the final drawable alpha
180
- // (needed for the `premultiplied` canvas alphaMode that blends the
181
- // WebGPU surface over the page background).
182
- // FS output at location 1 must be vec4f — the blend state references src.a, and
183
- // WebGPU requires the fragment output to provide an alpha component even though
184
- // the rg8unorm target only stores .r and .g (extra components are discarded).
185
- // Materials write mask = vec4f(1.0, 1.0, 0.0, color.a); ground writes
186
- // vec4f(0.0, 1.0, 0.0, edgeFade). With src.a coming from the 4th component and
187
- // src-alpha blending enabled:
188
- // out.r = mask_r · src.a + dst.r · (1-src.a) (bloom mask, weighted by alpha)
189
- // out.g = 1.0 · src.a + dst.g · (1-src.a) (canonical premultiplied alpha-over)
190
-
191
- export const COMMON_FS_OUT_WGSL = /* wgsl */ `
192
-
193
- struct FSOut {
194
- @location(0) color: vec4f,
195
- @location(1) mask: vec4f,
196
- };
197
-
198
- `;
199
-
200
- // ─── Convenience: full shared prelude ───────────────────────────────
201
- // Material files compose this as `${NODES_WGSL}${COMMON_MATERIAL_PRELUDE_WGSL}` to
202
- // pull in everything structural. Each material then adds its own constants + fs().
203
-
204
- export const COMMON_MATERIAL_PRELUDE_WGSL =
205
- COMMON_BINDINGS_WGSL + SAMPLE_SHADOW_WGSL + COMMON_VS_WGSL + COMMON_FS_OUT_WGSL
1
+ // Shared WGSL blocks concatenated by every material shader.
2
+ // Splits the boilerplate (uniform structs, bind group layout, skinning VS, PCF shadow)
3
+ // away from the per-material fragment code so each material file only contains what
4
+ // makes it visually distinct.
5
+ //
6
+ // Concat order in every material:
7
+ // NODES_WGSL (nodes.ts — math/noise/BSDF helpers)
8
+ // COMMON_BINDINGS_WGSL (uniform structs + @group/@binding declarations)
9
+ // SAMPLE_SHADOW_WGSL (3×3 PCF shadow sampler; reads bindings above)
10
+ // COMMON_VS_WGSL (skinning vertex shader; reads bindings above)
11
+ // <material's own constants + @fragment fn fs>
12
+ //
13
+ // WGSL is a whole-module compile — declaration order at module scope doesn't matter,
14
+ // but the readable order is: types → bindings → helpers → entry points.
15
+
16
+ // ─── Uniform structs + bind group layout ────────────────────────────
17
+ // Every material pipeline uses the same bind group layout, so the same bindings are
18
+ // declared here once. Groups:
19
+ // group(0): per-frame scene (camera, lights, shadow map, BRDF LUT via nodes.ts)
20
+ // group(1): per-model skinning
21
+ // group(2): per-material (diffuse texture + material uniforms)
22
+
23
+ export const COMMON_BINDINGS_WGSL = /* wgsl */ `
24
+
25
+ struct CameraUniforms {
26
+ view: mat4x4f,
27
+ projection: mat4x4f,
28
+ viewPos: vec3f,
29
+ _padding: f32,
30
+ };
31
+
32
+ struct Light {
33
+ direction: vec4f,
34
+ color: vec4f,
35
+ };
36
+
37
+ struct LightUniforms {
38
+ ambientColor: vec4f,
39
+ lights: array<Light, 4>,
40
+ };
41
+
42
+ // Per-material uniforms. Every material binds this layout even if it ignores fields;
43
+ // the engine keeps one bind group layout across all material pipelines. The PMX
44
+ // classic-material fields (ambient/specular/shininess) are carried for graph nodes that
45
+ // want them; most graphs read only diffuseColor + alpha.
46
+ struct MaterialUniforms {
47
+ diffuseColor: vec3f, // PMX diffuse rgb — the material_diffuse node reads this
48
+ alpha: f32, // 0 → discard; <1 → transparent draw call
49
+ ambient: vec3f, // PMX ambient rgb
50
+ shininess: f32, // PMX specular power
51
+ specular: vec3f, // PMX specular rgb
52
+ sphereMode: f32, // 0 none · 1 multiply (sph) · 2 add (spa)
53
+ // Skeleton index of the 頭 (head) bone, or -1. Lets the eye shader gate
54
+ // the post-alpha-eye stencil by camera-vs-face hemisphere.
55
+ headBoneIndex: f32,
56
+ _pad0: f32,
57
+ _pad1: f32,
58
+ _pad2: f32,
59
+ };
60
+
61
+ struct VertexOutput {
62
+ @builtin(position) position: vec4f,
63
+ @location(0) normal: vec3f,
64
+ @location(1) uv: vec2f,
65
+ @location(2) worldPos: vec3f,
66
+ // Bind-pose object-space position (the raw pre-skin vertex attribute). Procedural
67
+ // textures (noise bump, sparkle, Generated-coord gradients) key off this instead of
68
+ // worldPos so the pattern rides with the surface — otherwise the mesh swims through a
69
+ // world-static noise field under any skinning deformation or root (センター) motion.
70
+ // At rest skinMats are identity so restPos == worldPos, which is why existing noise-
71
+ // scale constants stay valid without retuning.
72
+ @location(3) restPos: vec3f,
73
+ };
74
+
75
+ struct LightVP { viewProj: mat4x4f, };
76
+
77
+ @group(0) @binding(0) var<uniform> camera: CameraUniforms;
78
+ @group(0) @binding(1) var<uniform> light: LightUniforms;
79
+ @group(0) @binding(2) var diffuseSampler: sampler;
80
+ @group(0) @binding(3) var shadowMap: texture_depth_2d;
81
+ @group(0) @binding(4) var shadowSampler: sampler_comparison;
82
+ @group(0) @binding(5) var<uniform> lightVP: LightVP;
83
+ // binding(9) brdfLut is declared inside NODES_WGSL (nodes.ts).
84
+ @group(1) @binding(0) var<storage, read> skinMats: array<mat4x4f>;
85
+ @group(2) @binding(0) var diffuseTexture: texture_2d<f32>;
86
+ @group(2) @binding(1) var<uniform> material: MaterialUniforms;
87
+ // Reserved for future sphere/toon graph nodes; graphs that don't read them get the
88
+ // 1×1 white fallback bound here.
89
+ @group(2) @binding(2) var toonTexture: texture_2d<f32>;
90
+ @group(2) @binding(3) var sphereTexture: texture_2d<f32>;
91
+
92
+ // Four-bone blended normals can cancel to ~zero on physics-driven parts
93
+ // (opposing bone rotations at 50/50 weights) — normalize(0) is 0/0 = NaN,
94
+ // which poisons the whole shading stack and flashes through bloom. Fall
95
+ // back to up for degenerate normals instead.
96
+ fn safe_normal(nIn: vec3f) -> vec3f {
97
+ let l2 = dot(nIn, nIn);
98
+ if (l2 < 1e-12) { return vec3f(0.0, 1.0, 0.0); }
99
+ return nIn * inverseSqrt(l2);
100
+ }
101
+
102
+ `;
103
+
104
+ // ─── Shadow sampler (3×3 PCF) ───────────────────────────────────────
105
+ // 4096-map (MUST match Engine.SHADOW_MAP_SIZE), normal-bias 0.08, depth-bias
106
+ // 0.001. Unrolled — Safari's Metal backend doesn't unroll nested shadow loops
107
+ // reliably. The texel size was stale at 1/2048 after the map grew to 4096:
108
+ // PCF taps landed TWO texels apart, quantizing self-shadow edges into
109
+ // texel-sized gray/black squares that crawled with the animation.
110
+
111
+ export const SAMPLE_SHADOW_WGSL = /* wgsl */ `
112
+
113
+ fn sampleShadow(worldPos: vec3f, n: vec3f) -> f32 {
114
+ if (dot(n, -light.lights[0].direction.xyz) <= 0.0) { return 0.0; }
115
+ let biasedPos = worldPos + n * 0.08;
116
+ let lclip = lightVP.viewProj * vec4f(biasedPos, 1.0);
117
+ let ndc = lclip.xyz / max(lclip.w, 1e-6);
118
+ let suv = vec2f(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5);
119
+ let cmpZ = ndc.z - 0.001;
120
+ let ts = 1.0 / 4096.0;
121
+ let s00 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(-ts, -ts), cmpZ);
122
+ let s10 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(0.0, -ts), cmpZ);
123
+ let s20 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f( ts, -ts), cmpZ);
124
+ let s01 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(-ts, 0.0), cmpZ);
125
+ let s11 = textureSampleCompareLevel(shadowMap, shadowSampler, suv, cmpZ);
126
+ let s21 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f( ts, 0.0), cmpZ);
127
+ let s02 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(-ts, ts), cmpZ);
128
+ let s12 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(0.0, ts), cmpZ);
129
+ let s22 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f( ts, ts), cmpZ);
130
+ return (s00 + s10 + s20 + s01 + s11 + s21 + s02 + s12 + s22) * (1.0 / 9.0);
131
+ }
132
+
133
+ `;
134
+
135
+ // ─── Skinning vertex shader ─────────────────────────────────────────
136
+ // Four-bone linear blend skinning. Renormalizes weights when they don't sum to 1
137
+ // (PMX models occasionally ship with unnormalized weights on extras like hair tips).
138
+ // VS normalize on the outgoing normal is skipped — interpolation denormalizes it
139
+ // anyway and every fragment shader does `normalize(input.normal)` as its first line.
140
+
141
+ export const COMMON_VS_WGSL = /* wgsl */ `
142
+
143
+ @vertex fn vs(
144
+ @location(0) position: vec3f,
145
+ @location(1) normal: vec3f,
146
+ @location(2) uv: vec2f,
147
+ @location(3) joints0: vec4<u32>,
148
+ @location(4) weights0: vec4<f32>
149
+ ) -> VertexOutput {
150
+ var output: VertexOutput;
151
+ let pos4 = vec4f(position, 1.0);
152
+ let weightSum = weights0.x + weights0.y + weights0.z + weights0.w;
153
+ let invWeightSum = select(1.0, 1.0 / weightSum, weightSum > 0.0001);
154
+ let nw = select(vec4f(1.0, 0.0, 0.0, 0.0), weights0 * invWeightSum, weightSum > 0.0001);
155
+ var skinnedPos = vec4f(0.0);
156
+ var skinnedNrm = vec3f(0.0);
157
+ for (var i = 0u; i < 4u; i++) {
158
+ let m = skinMats[joints0[i]];
159
+ let w = nw[i];
160
+ skinnedPos += (m * pos4) * w;
161
+ skinnedNrm += (mat3x3f(m[0].xyz, m[1].xyz, m[2].xyz) * normal) * w;
162
+ }
163
+ output.position = camera.projection * camera.view * vec4f(skinnedPos.xyz, 1.0);
164
+ output.normal = skinnedNrm;
165
+ output.uv = uv;
166
+ output.worldPos = skinnedPos.xyz;
167
+ output.restPos = position;
168
+ return output;
169
+ }
170
+
171
+ `;
172
+
173
+ // ─── FS output struct ───────────────────────────────────────────────
174
+ // Location 0: final radiance+alpha (blended into rg11b10ufloat; the HDR target
175
+ // has no alpha channel, but the blend equation still uses the .a you write here
176
+ // as the src-alpha factor that premultiplies rgb into the HDR target).
177
+ // Location 1: auxiliary rg8unorm carrying
178
+ // .r = bloom mask (1 = contributes to bloom, 0 = skip — e.g. ground).
179
+ // .g = accumulated canvas alpha the channel that used to live in hdr.a
180
+ // before the switch to rg11b10ufloat. Sampled by composite to
181
+ // un-premultiply color for tonemap and to set the final drawable alpha
182
+ // (needed for the `premultiplied` canvas alphaMode that blends the
183
+ // WebGPU surface over the page background).
184
+ // FS output at location 1 must be vec4f the blend state references src.a, and
185
+ // WebGPU requires the fragment output to provide an alpha component even though
186
+ // the rg8unorm target only stores .r and .g (extra components are discarded).
187
+ // Materials write mask = vec4f(1.0, 1.0, 0.0, color.a); ground writes
188
+ // vec4f(0.0, 1.0, 0.0, edgeFade). With src.a coming from the 4th component and
189
+ // src-alpha blending enabled:
190
+ // out.r = mask_r · src.a + dst.r · (1-src.a) (bloom mask, weighted by alpha)
191
+ // out.g = 1.0 · src.a + dst.g · (1-src.a) (canonical premultiplied alpha-over)
192
+
193
+ export const COMMON_FS_OUT_WGSL = /* wgsl */ `
194
+
195
+ struct FSOut {
196
+ @location(0) color: vec4f,
197
+ @location(1) mask: vec4f,
198
+ };
199
+
200
+ `;
201
+
202
+ // ─── Convenience: full shared prelude ───────────────────────────────
203
+ // Material files compose this as `${NODES_WGSL}${COMMON_MATERIAL_PRELUDE_WGSL}` to
204
+ // pull in everything structural. Each material then adds its own constants + fs().
205
+
206
+ export const COMMON_MATERIAL_PRELUDE_WGSL =
207
+ COMMON_BINDINGS_WGSL + SAMPLE_SHADOW_WGSL + COMMON_VS_WGSL + COMMON_FS_OUT_WGSL
@@ -0,0 +1,57 @@
1
+ // Depth-only prepass for the TRANSPARENT bucket. Transparent color draws keep
2
+ // depth write OFF so self-overlapping sheer cloth blends both layers instead of
3
+ // a triangle-order patchwork — but that leaves no depth record, so anything
4
+ // drawn later (the outline hulls) shows straight through the fabric as black
5
+ // shapes. This pass re-draws the transparent geometry depth-only AFTER its
6
+ // color pass: solid-enough texels (alpha ≥ 0.5) write depth, so outlines get
7
+ // occluded behind fabric exactly like they are behind opaque cloth, while
8
+ // truly sheer texels (a veil) stay non-occluding.
9
+ //
10
+ // Reuses mainPipelineLayout: camera g0b0, diffuseSampler g0b2, skinMats g1b0,
11
+ // diffuse texture g2b0, material uniforms g2b1 — the same bind groups the
12
+ // color draws already set, so drawing it costs no extra binding work.
13
+
14
+ export const TRANSPARENT_DEPTH_PREPASS_WGSL = /* wgsl */ `
15
+ struct CameraUniforms { view: mat4x4f, projection: mat4x4f, viewPos: vec3f, _p: f32, };
16
+ struct MaterialUniforms {
17
+ diffuseColor: vec3f,
18
+ alpha: f32,
19
+ };
20
+
21
+ @group(0) @binding(0) var<uniform> camera: CameraUniforms;
22
+ @group(0) @binding(2) var diffuseSampler: sampler;
23
+ @group(1) @binding(0) var<storage, read> skinMats: array<mat4x4f>;
24
+ @group(2) @binding(0) var diffuseTexture: texture_2d<f32>;
25
+ @group(2) @binding(1) var<uniform> material: MaterialUniforms;
26
+
27
+ struct VSOut {
28
+ @builtin(position) position: vec4f,
29
+ @location(0) uv: vec2f,
30
+ };
31
+
32
+ @vertex fn vs(
33
+ @location(0) position: vec3f,
34
+ @location(1) normal: vec3f,
35
+ @location(2) uv: vec2f,
36
+ @location(3) joints0: vec4<u32>,
37
+ @location(4) weights0: vec4<f32>,
38
+ ) -> VSOut {
39
+ let pos4 = vec4f(position, 1.0);
40
+ let weightSum = weights0.x + weights0.y + weights0.z + weights0.w;
41
+ let invWeightSum = select(1.0, 1.0 / weightSum, weightSum > 0.0001);
42
+ let w = select(vec4f(1.0, 0.0, 0.0, 0.0), weights0 * invWeightSum, weightSum > 0.0001);
43
+ var skinned = vec4f(0.0);
44
+ for (var i = 0u; i < 4u; i++) {
45
+ skinned += (skinMats[joints0[i]] * pos4) * w[i];
46
+ }
47
+ var o: VSOut;
48
+ o.position = camera.projection * camera.view * vec4f(skinned.xyz, 1.0);
49
+ o.uv = uv;
50
+ return o;
51
+ }
52
+
53
+ @fragment fn fs(in: VSOut) {
54
+ let a = material.alpha * textureSample(diffuseTexture, diffuseSampler, in.uv).a;
55
+ if (a < 0.5) { discard; }
56
+ }
57
+ `