Skip to content

Commit 4b1865f

Browse files
authored
Normalize only nonzero normals for mikktspace normal maps (#10905)
# Objective Fixes #5891. For mikktspace normal maps, normals must be renormalized in vertex shaders to match the way mikktspace bakes vertex tangents and normal maps so that the exact inverse process is applied when shading. However, for invalid normals like `vec3<f32>(0.0, 0.0, 0.0)`, this normalization causes NaN values, and because it's in the vertex shader, it affects the entire triangle and causes it to be shaded as black: ![incorrectly shaded cone](https://github.com/bevyengine/bevy/assets/57632562/3334b3a9-f72a-4a08-853e-8077a346f5c9) *A cone with a tip that has a vertex normal of [0, 0, 0], causing the mesh to be shaded as black.* In some cases, normals of zero are actually *useful*. For example, a smoothly shaded cone without creases requires the apex vertex normal to be zero, because there is no singular normal that works correctly, so the apex shouldn't contribute to the overall shading. Duplicate vertices for the apex fix some shading issues, but it causes visible creases and is more expensive. See #5891 and #10298 for more details. For correctly shaded cones and other similar low-density shapes with sharp tips, vertex normals of zero can not be normalized in the vertex shader. ## Solution Only normalize the vertex normals and tangents in the vertex shader if the normal isn't [0, 0, 0]. This way, mikktspace normal maps should still work for everything except the zero normals, and the zero normals will only be normalized in the fragment shader. This allows us to render cones correctly: ![smooth cone with some banding](https://github.com/bevyengine/bevy/assets/57632562/6b36e264-22c6-453b-a6de-c404b314ca1a) Notice how there is still a weird shadow banding effect in one area. I noticed that it can be fixed by normalizing [here](https://github.com/bevyengine/bevy/blob/d2614f2d802d0fb8000821a81553b600cc85f734/crates/bevy_pbr/src/render/pbr_functions.wgsl#L51), which produces a perfectly smooth cone without duplicate vertices: ![smooth cone](https://github.com/bevyengine/bevy/assets/57632562/64f9ad5d-b249-4eae-880b-a1e61e07ae73) I didn't add this change yet, because it seems a bit arbitrary. I can add it here if that'd be useful or make another PR though.
1 parent 47fa620 commit 4b1865f

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

crates/bevy_pbr/src/render/mesh_functions.wgsl

+32-22
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,20 @@ fn mesh_normal_local_to_world(vertex_normal: vec3<f32>, instance_index: u32) ->
3535
// NOTE: The mikktspace method of normal mapping requires that the world normal is
3636
// re-normalized in the vertex shader to match the way mikktspace bakes vertex tangents
3737
// and normal maps so that the exact inverse process is applied when shading. Blender, Unity,
38-
// Unreal Engine, Godot, and more all use the mikktspace method. Do not change this code
39-
// unless you really know what you are doing.
38+
// Unreal Engine, Godot, and more all use the mikktspace method.
39+
// We only skip normalization for invalid normals so that they don't become NaN.
40+
// Do not change this code unless you really know what you are doing.
4041
// http://www.mikktspace.com/
41-
return normalize(
42-
mat2x4_f32_to_mat3x3_unpack(
43-
mesh[instance_index].inverse_transpose_model_a,
44-
mesh[instance_index].inverse_transpose_model_b,
45-
) * vertex_normal
46-
);
42+
if any(vertex_normal != vec3<f32>(0.0)) {
43+
return normalize(
44+
mat2x4_f32_to_mat3x3_unpack(
45+
mesh[instance_index].inverse_transpose_model_a,
46+
mesh[instance_index].inverse_transpose_model_b,
47+
) * vertex_normal
48+
);
49+
} else {
50+
return vertex_normal;
51+
}
4752
}
4853

4954
// Calculates the sign of the determinant of the 3x3 model matrix based on a
@@ -59,19 +64,24 @@ fn mesh_tangent_local_to_world(model: mat4x4<f32>, vertex_tangent: vec4<f32>, in
5964
// NOTE: The mikktspace method of normal mapping requires that the world tangent is
6065
// re-normalized in the vertex shader to match the way mikktspace bakes vertex tangents
6166
// and normal maps so that the exact inverse process is applied when shading. Blender, Unity,
62-
// Unreal Engine, Godot, and more all use the mikktspace method. Do not change this code
63-
// unless you really know what you are doing.
67+
// Unreal Engine, Godot, and more all use the mikktspace method.
68+
// We only skip normalization for invalid tangents so that they don't become NaN.
69+
// Do not change this code unless you really know what you are doing.
6470
// http://www.mikktspace.com/
65-
return vec4<f32>(
66-
normalize(
67-
mat3x3<f32>(
68-
model[0].xyz,
69-
model[1].xyz,
70-
model[2].xyz
71-
) * vertex_tangent.xyz
72-
),
73-
// NOTE: Multiplying by the sign of the determinant of the 3x3 model matrix accounts for
74-
// situations such as negative scaling.
75-
vertex_tangent.w * sign_determinant_model_3x3m(instance_index)
76-
);
71+
if any(vertex_tangent != vec4<f32>(0.0)) {
72+
return vec4<f32>(
73+
normalize(
74+
mat3x3<f32>(
75+
model[0].xyz,
76+
model[1].xyz,
77+
model[2].xyz
78+
) * vertex_tangent.xyz
79+
),
80+
// NOTE: Multiplying by the sign of the determinant of the 3x3 model matrix accounts for
81+
// situations such as negative scaling.
82+
vertex_tangent.w * sign_determinant_model_3x3m(instance_index)
83+
);
84+
} else {
85+
return vertex_tangent;
86+
}
7787
}

0 commit comments

Comments
 (0)