Skip to content

Commit da151ab

Browse files
committed
examples: Fix view z calculation for sort key
1 parent a5b73b4 commit da151ab

File tree

5 files changed

+17
-6
lines changed

5 files changed

+17
-6
lines changed

crates/bevy_pbr/src/light.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,9 @@ pub(crate) fn assign_lights_to_clusters(
799799
lights
800800
.iter()
801801
.map(|light| {
802+
// NOTE: row 2 of the inverse view matrix dotted with the world space
803+
// translation gives the z component of translation of the mesh in view
804+
// space
802805
-inverse_view_row_2.dot(light.translation.extend(1.0)) + light.range
803806
})
804807
.reduce(f32::max)

crates/bevy_pbr/src/wireframe.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ fn queue_wireframes(
147147
entity,
148148
pipeline: pipeline_id,
149149
draw_function: draw_custom,
150+
// NOTE: row 2 of the inverse view matrix dotted with column 3 of the model matrix
151+
// gives the z component of translation of the mesh in view space
150152
distance: inverse_view_row_2.dot(mesh_uniform.transform.col(3)),
151153
});
152154
}

examples/shader/animate_shader.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn queue_custom(
112112
| MeshPipelineKey::from_primitive_topology(PrimitiveTopology::TriangleList);
113113

114114
for (view, mut transparent_phase) in views.iter_mut() {
115-
let view_row_2 = view.view.row(2);
115+
let inverse_view_row_2 = view.view.inverse().row(2);
116116
for (entity, mesh_uniform, mesh_handle) in material_meshes.iter() {
117117
if let Some(mesh) = render_meshes.get(mesh_handle) {
118118
let pipeline = pipelines
@@ -122,7 +122,9 @@ fn queue_custom(
122122
entity,
123123
pipeline,
124124
draw_function: draw_custom,
125-
distance: view_row_2.dot(mesh_uniform.transform.col(3)),
125+
// NOTE: row 2 of the inverse view matrix dotted with column 3 of the model matrix
126+
// gives the z component of translation of the mesh in view space
127+
distance: inverse_view_row_2.dot(mesh_uniform.transform.col(3)),
126128
});
127129
}
128130
}

examples/shader/shader_defs.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ fn queue_custom(
149149
.unwrap();
150150
let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples);
151151
for (view, mut transparent_phase) in views.iter_mut() {
152-
let view_row_2 = view.view.row(2);
152+
let inverse_view_row_2 = view.view.inverse().row(2);
153153
for (entity, mesh_handle, mesh_uniform, is_red) in material_meshes.iter() {
154154
if let Some(mesh) = render_meshes.get(mesh_handle) {
155155
let key =
@@ -166,7 +166,9 @@ fn queue_custom(
166166
entity,
167167
pipeline,
168168
draw_function: draw_custom,
169-
distance: view_row_2.dot(mesh_uniform.transform.col(3)),
169+
// NOTE: row 2 of the inverse view matrix dotted with column 3 of the model matrix
170+
// gives the z component of translation of the mesh in view space
171+
distance: inverse_view_row_2.dot(mesh_uniform.transform.col(3)),
170172
});
171173
}
172174
}

examples/shader/shader_instancing.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ fn queue_custom(
117117
let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples);
118118

119119
for (view, mut transparent_phase) in views.iter_mut() {
120-
let view_row_2 = view.view.row(2);
120+
let inverse_view_row_2 = view.view.inverse().row(2);
121121
for (entity, mesh_uniform, mesh_handle) in material_meshes.iter() {
122122
if let Some(mesh) = meshes.get(mesh_handle) {
123123
let key =
@@ -129,7 +129,9 @@ fn queue_custom(
129129
entity,
130130
pipeline,
131131
draw_function: draw_custom,
132-
distance: view_row_2.dot(mesh_uniform.transform.col(3)),
132+
// NOTE: row 2 of the inverse view matrix dotted with column 3 of the model matrix
133+
// gives the z component of translation of the mesh in view space
134+
distance: inverse_view_row_2.dot(mesh_uniform.transform.col(3)),
133135
});
134136
}
135137
}

0 commit comments

Comments
 (0)