Skip to content

Commit 3413e97

Browse files
committed
fix: make wgsl on web happy
1 parent 26b1c5a commit 3413e97

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

packages/ranim-render/src/pipelines/shaders/mesh_item.wgsl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ struct FragmentOutput {
3030
}
3131

3232
fn compute_lighting(world_pos: vec3<f32>, world_normal: vec3<f32>, base_color: vec4<f32>) -> vec4<f32> {
33-
var normal: vec3<f32>;
34-
// If the interpolated normal is near-zero, fall back to flat shading via screen-space derivatives
35-
if (dot(world_normal, world_normal) < 0.0001) {
36-
normal = normalize(cross(dpdx(world_pos), dpdy(world_pos)));
37-
} else {
38-
normal = normalize(world_normal);
39-
}
33+
// Compute both normals unconditionally (derivatives require uniform control flow)
34+
let flat_normal = normalize(cross(dpdx(world_pos), dpdy(world_pos)));
35+
let smooth_normal = normalize(world_normal);
36+
37+
// Select which normal to use based on whether world_normal is near-zero
38+
let use_flat = dot(world_normal, world_normal) < 0.0001;
39+
let normal = select(smooth_normal, flat_normal, use_flat);
40+
4041
let light_dir = normalize(vec3<f32>(0.3, 1.0, 0.5));
4142
let ambient = 0.35;
4243
let diffuse = abs(dot(normal, light_dir));
@@ -71,6 +72,7 @@ fn fs_color(
7172
}
7273

7374
discard;
75+
return vec4<f32>(0.0); // To make wasm happy
7476
}
7577

7678
@fragment

0 commit comments

Comments
 (0)