Skip to content

Commit

Permalink
Allow prepass to run without ATTRIBUTE_NORMAL (#17881)
Browse files Browse the repository at this point in the history
# Objective

Allow prepass to run without ATTRIBUTE_NORMAL. 
This is needed for custom materials with non-standard vertex attributes.
For example a voxel material with manually packed vertex data.

Fixes #13054.

This PR covers the first part of the **stale** PR #13569 to only focus
on fixing #13054.

## Solution

- Only push normals `vertex_attributes` when the layout contains
`Mesh::ATTRIBUTE_NORMAL`

## Testing

- Did you test these changes? If so, how?
**Tested the fix on my own project with a mesh without normal
attribute.**
- Are there any parts that need more testing?
  **I don't think so.**
- How can other people (reviewers) test your changes? Is there anything
specific they need to know?
**Prepass should not be blocked on a mesh without normal attributes
(with or without custom material).**
- If relevant, what platforms did you test these changes on, and are
there any important ones you can't test?
  **Probably irrelevant, but Windows/Vulkan.**
  • Loading branch information
terahlunah authored Feb 24, 2025
1 parent 7d7c43d commit 910e405
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion crates/bevy_pbr/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,15 @@ where
.mesh_key
.intersects(MeshPipelineKey::NORMAL_PREPASS | MeshPipelineKey::DEFERRED_PREPASS)
{
vertex_attributes.push(Mesh::ATTRIBUTE_NORMAL.at_shader_location(3));
shader_defs.push("NORMAL_PREPASS_OR_DEFERRED_PREPASS".into());
if layout.0.contains(Mesh::ATTRIBUTE_NORMAL) {
shader_defs.push("VERTEX_NORMALS".into());
vertex_attributes.push(Mesh::ATTRIBUTE_NORMAL.at_shader_location(3));
} else if key.mesh_key.contains(MeshPipelineKey::NORMAL_PREPASS) {
warn!(
"The default normal prepass expects the mesh to have vertex normal attributes."
);
}
if layout.0.contains(Mesh::ATTRIBUTE_TANGENT) {
shader_defs.push("VERTEX_TANGENTS".into());
vertex_attributes.push(Mesh::ATTRIBUTE_TANGENT.at_shader_location(4));
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_pbr/src/prepass/prepass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ fn vertex(vertex_no_morph: Vertex) -> VertexOutput {
#endif // VERTEX_UVS_B

#ifdef NORMAL_PREPASS_OR_DEFERRED_PREPASS
#ifdef VERTEX_NORMALS
#ifdef SKINNED
out.world_normal = skinning::skin_normals(world_from_local, vertex.normal);
#else // SKINNED
Expand All @@ -106,6 +107,7 @@ fn vertex(vertex_no_morph: Vertex) -> VertexOutput {
vertex_no_morph.instance_index
);
#endif // SKINNED
#endif // VERTEX_NORMALS

#ifdef VERTEX_TANGENTS
out.world_tangent = mesh_functions::mesh_tangent_local_to_world(
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_pbr/src/prepass/prepass_io.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ struct Vertex {
#endif

#ifdef NORMAL_PREPASS_OR_DEFERRED_PREPASS
#ifdef VERTEX_NORMALS
@location(3) normal: vec3<f32>,
#endif
#ifdef VERTEX_TANGENTS
@location(4) tangent: vec4<f32>,
#endif
Expand Down

0 comments on commit 910e405

Please sign in to comment.