bevy/crates/bevy_pbr/src/render
Joona Aalto 4b1865f8bd
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](d2614f2d80/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.
2023-12-10 11:42:47 +00:00
..
clustered_forward.wgsl update shader imports (#10180) 2023-10-21 11:51:58 +00:00
fog.rs Fix fog color being inaccurate (#10226) 2023-10-23 12:45:18 +00:00
fog.wgsl update shader imports (#10180) 2023-10-21 11:51:58 +00:00
forward_io.wgsl pbr shader cleanup (#10105) 2023-10-13 19:12:40 +00:00
light.rs More inactive camera checks (#10555) 2023-11-14 13:44:42 +00:00
mesh_bindings.rs Bind group layout entries (#10224) 2023-11-28 04:00:49 +00:00
mesh_bindings.wgsl Swap material and mesh bind groups (#10485) 2023-11-28 22:26:22 +00:00
mesh_functions.wgsl Normalize only nonzero normals for mikktspace normal maps (#10905) 2023-12-10 11:42:47 +00:00
mesh_types.wgsl StandardMaterial Light Transmission (#8015) 2023-10-31 20:59:02 +00:00
mesh_view_bindings.rs Fix prepass binding issues causing crashes when not all prepass bindings are used (#10788) 2023-11-29 23:11:12 +00:00
mesh_view_bindings.wgsl StandardMaterial Light Transmission (#8015) 2023-10-31 20:59:02 +00:00
mesh_view_types.wgsl update shader imports (#10180) 2023-10-21 11:51:58 +00:00
mesh.rs Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00
mesh.wgsl Ensure instance_index push constant is always used in prepass.wgsl (#10706) 2023-11-28 01:13:25 +00:00
mod.rs Variable MeshPipeline View Bind Group Layout (#10156) 2023-10-21 11:19:44 +00:00
morph.rs Use EntityHashMap<Entity, T> for render world entity storage for better performance (#9903) 2023-09-27 08:28:28 +00:00
morph.wgsl Swap material and mesh bind groups (#10485) 2023-11-28 22:26:22 +00:00
parallax_mapping.wgsl update shader imports (#10180) 2023-10-21 11:51:58 +00:00
pbr_ambient.wgsl Use “specular occlusion” term to consistently extinguish fresnel on Ambient and Environment Map lights (#10182) 2023-10-23 03:26:20 +00:00
pbr_bindings.wgsl Swap material and mesh bind groups (#10485) 2023-11-28 22:26:22 +00:00
pbr_fragment.wgsl StandardMaterial Light Transmission (#8015) 2023-10-31 20:59:02 +00:00
pbr_functions.wgsl StandardMaterial Light Transmission (#8015) 2023-10-31 20:59:02 +00:00
pbr_lighting.wgsl update shader imports (#10180) 2023-10-21 11:51:58 +00:00
pbr_prepass_functions.wgsl update shader imports (#10180) 2023-10-21 11:51:58 +00:00
pbr_prepass.wgsl double sided normals: fix apply_normal_mapping calls (#10330) 2023-11-01 16:40:25 +00:00
pbr_transmission.wgsl Non uniform transmission samples (#10674) 2023-11-22 18:54:45 +00:00
pbr_types.wgsl StandardMaterial Light Transmission (#8015) 2023-10-31 20:59:02 +00:00
pbr.wgsl update shader imports (#10180) 2023-10-21 11:51:58 +00:00
rgb9e5.wgsl Deferred Renderer (#9258) 2023-10-12 22:10:38 +00:00
shadow_sampling.wgsl StandardMaterial Light Transmission (#8015) 2023-10-31 20:59:02 +00:00
shadows.wgsl update shader imports (#10180) 2023-10-21 11:51:58 +00:00
skin.rs Use EntityHashMap<Entity, T> for render world entity storage for better performance (#9903) 2023-09-27 08:28:28 +00:00
skinning.wgsl Swap material and mesh bind groups (#10485) 2023-11-28 22:26:22 +00:00
utils.wgsl StandardMaterial Light Transmission (#8015) 2023-10-31 20:59:02 +00:00
view_transformations.wgsl View Transformations (#9726) 2023-10-24 21:26:19 +00:00
wireframe.wgsl Swap material and mesh bind groups (#10485) 2023-11-28 22:26:22 +00:00