Skip to content

Commit 20dfae9

Browse files
authored
Factor out up-choice in shadow cubemap sampling orthonormalize (#20052)
# Objective - Another step towards unifying our orthonormal basis construction #20050 - Preserve behavior but fix a bug. Unification will be a followup after these two PRs and will need more thorough testing. ## Solution - Make shadow cubemap sampling orthonormalize have the same function signature as the other orthonormal basis functions in bevy ## Testing - 3d_scene + lighting examples
1 parent b3032e0 commit 20dfae9

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

crates/bevy_pbr/src/render/shadow_sampling.wgsl

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,7 @@ fn sample_shadow_cubemap_gaussian(
422422
) -> f32 {
423423
// Create an orthonormal basis so we can apply a 2D sampling pattern to a
424424
// cubemap.
425-
var up = vec3(0.0, 1.0, 0.0);
426-
if (dot(up, normalize(light_local)) > 0.99) {
427-
up = vec3(1.0, 0.0, 0.0); // Avoid creating a degenerate basis.
428-
}
429-
let basis = orthonormalize(light_local, up) * scale * distance_to_light;
425+
let basis = orthonormalize(normalize(light_local)) * scale * distance_to_light;
430426

431427
var sum: f32 = 0.0;
432428
sum += sample_shadow_cubemap_at_offset(
@@ -469,11 +465,7 @@ fn sample_shadow_cubemap_jittered(
469465
) -> f32 {
470466
// Create an orthonormal basis so we can apply a 2D sampling pattern to a
471467
// cubemap.
472-
var up = vec3(0.0, 1.0, 0.0);
473-
if (dot(up, normalize(light_local)) > 0.99) {
474-
up = vec3(1.0, 0.0, 0.0); // Avoid creating a degenerate basis.
475-
}
476-
let basis = orthonormalize(light_local, up) * scale * distance_to_light;
468+
let basis = orthonormalize(normalize(light_local)) * scale * distance_to_light;
477469

478470
let rotation_matrix = random_rotation_matrix(vec2(1.0), temporal);
479471

@@ -553,11 +545,7 @@ fn search_for_blockers_in_shadow_cubemap(
553545
) -> f32 {
554546
// Create an orthonormal basis so we can apply a 2D sampling pattern to a
555547
// cubemap.
556-
var up = vec3(0.0, 1.0, 0.0);
557-
if (dot(up, normalize(light_local)) > 0.99) {
558-
up = vec3(1.0, 0.0, 0.0); // Avoid creating a degenerate basis.
559-
}
560-
let basis = orthonormalize(light_local, up) * scale * distance_to_light;
548+
let basis = orthonormalize(normalize(light_local)) * scale * distance_to_light;
561549

562550
var sum: vec2<f32> = vec2(0.0);
563551
sum += search_for_blockers_in_shadow_cubemap_at_offset(

crates/bevy_render/src/maths.wgsl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,19 @@ fn mat4x4_to_mat3x3(m: mat4x4<f32>) -> mat3x3<f32> {
6363
return mat3x3<f32>(m[0].xyz, m[1].xyz, m[2].xyz);
6464
}
6565

66-
// Creates an orthonormal basis given a Z vector and an up vector (which becomes
67-
// Y after orthonormalization).
66+
// Creates an orthonormal basis given a normalized Z vector.
6867
//
6968
// The results are equivalent to the Gram-Schmidt process [1].
7069
//
7170
// [1]: https://math.stackexchange.com/a/1849294
72-
fn orthonormalize(z_unnormalized: vec3<f32>, up: vec3<f32>) -> mat3x3<f32> {
73-
let z_basis = normalize(z_unnormalized);
74-
let x_basis = normalize(cross(z_basis, up));
75-
let y_basis = cross(z_basis, x_basis);
76-
return mat3x3(x_basis, y_basis, z_basis);
71+
fn orthonormalize(z_normalized: vec3<f32>) -> mat3x3<f32> {
72+
var up = vec3(0.0, 1.0, 0.0);
73+
if (abs(dot(up, z_normalized)) > 0.99) {
74+
up = vec3(1.0, 0.0, 0.0); // Avoid creating a degenerate basis.
75+
}
76+
let x_basis = normalize(cross(z_normalized, up));
77+
let y_basis = cross(z_normalized, x_basis);
78+
return mat3x3(x_basis, y_basis, z_normalized);
7779
}
7880

7981
// Returns true if any part of a sphere is on the positive side of a plane.

0 commit comments

Comments
 (0)