Skip to content

Commit 2010164

Browse files
danchiasuperdumprobtfm
authored
Left-handed y-up cubemap coordinates (#8122)
Co-authored-by: Robert Swain <[email protected]> Co-authored-by: robtfm <[email protected]>
1 parent 5703c75 commit 2010164

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

crates/bevy_pbr/src/render/light.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -472,37 +472,43 @@ pub(crate) struct CubeMapFace {
472472
pub(crate) up: Vec3,
473473
}
474474

475-
// see https://www.khronos.org/opengl/wiki/Cubemap_Texture
475+
// Cubemap faces are [+X, -X, +Y, -Y, +Z, -Z], per https://www.w3.org/TR/webgpu/#texture-view-creation
476+
// Note: Cubemap coordinates are left-handed y-up, unlike the rest of Bevy.
477+
// See https://registry.khronos.org/vulkan/specs/1.2/html/chap16.html#_cube_map_face_selection
478+
//
479+
// For each cubemap face, we take care to specify the appropriate target/up axis such that the rendered
480+
// texture using Bevy's right-handed y-up coordinate space matches the expected cubemap face in
481+
// left-handed y-up cubemap coordinates.
476482
pub(crate) const CUBE_MAP_FACES: [CubeMapFace; 6] = [
477-
// 0 GL_TEXTURE_CUBE_MAP_POSITIVE_X
483+
// +X
478484
CubeMapFace {
479-
target: Vec3::NEG_X,
480-
up: Vec3::NEG_Y,
485+
target: Vec3::X,
486+
up: Vec3::Y,
481487
},
482-
// 1 GL_TEXTURE_CUBE_MAP_NEGATIVE_X
488+
// -X
483489
CubeMapFace {
484-
target: Vec3::X,
485-
up: Vec3::NEG_Y,
490+
target: Vec3::NEG_X,
491+
up: Vec3::Y,
486492
},
487-
// 2 GL_TEXTURE_CUBE_MAP_POSITIVE_Y
493+
// +Y
488494
CubeMapFace {
489-
target: Vec3::NEG_Y,
495+
target: Vec3::Y,
490496
up: Vec3::Z,
491497
},
492-
// 3 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
498+
// -Y
493499
CubeMapFace {
494-
target: Vec3::Y,
500+
target: Vec3::NEG_Y,
495501
up: Vec3::NEG_Z,
496502
},
497-
// 4 GL_TEXTURE_CUBE_MAP_POSITIVE_Z
503+
// +Z (with left-handed conventions, pointing forwards)
498504
CubeMapFace {
499505
target: Vec3::NEG_Z,
500-
up: Vec3::NEG_Y,
506+
up: Vec3::Y,
501507
},
502-
// 5 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
508+
// -Z (with left-handed conventions, pointing backwards)
503509
CubeMapFace {
504510
target: Vec3::Z,
505-
up: Vec3::NEG_Y,
511+
up: Vec3::Y,
506512
},
507513
];
508514

crates/bevy_pbr/src/render/shadows.wgsl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#define_import_path bevy_pbr::shadows
22

3+
const flip_z: vec3<f32> = vec3<f32>(1.0, 1.0, -1.0);
4+
35
fn fetch_point_shadow(light_id: u32, frag_position: vec4<f32>, surface_normal: vec3<f32>) -> f32 {
46
let light = &point_lights.data[light_id];
57

@@ -17,7 +19,7 @@ fn fetch_point_shadow(light_id: u32, frag_position: vec4<f32>, surface_normal: v
1719
let offset_position = frag_position.xyz + normal_offset + depth_offset;
1820

1921
// similar largest-absolute-axis trick as above, but now with the offset fragment position
20-
let frag_ls = (*light).position_radius.xyz - offset_position.xyz;
22+
let frag_ls = offset_position.xyz - (*light).position_radius.xyz ;
2123
let abs_position_ls = abs(frag_ls);
2224
let major_axis_magnitude = max(abs_position_ls.x, max(abs_position_ls.y, abs_position_ls.z));
2325

@@ -28,16 +30,17 @@ fn fetch_point_shadow(light_id: u32, frag_position: vec4<f32>, surface_normal: v
2830
let zw = -major_axis_magnitude * (*light).light_custom_data.xy + (*light).light_custom_data.zw;
2931
let depth = zw.x / zw.y;
3032

31-
// do the lookup, using HW PCF and comparison
33+
// Do the lookup, using HW PCF and comparison. Cubemaps assume a left-handed coordinate space,
34+
// so we have to flip the z-axis when sampling.
3235
// NOTE: Due to the non-uniform control flow above, we must use the Level variant of
3336
// textureSampleCompare to avoid undefined behaviour due to some of the fragments in
3437
// a quad (2x2 fragments) being processed not being sampled, and this messing with
3538
// mip-mapping functionality. The shadow maps have no mipmaps so Level just samples
3639
// from LOD 0.
3740
#ifdef NO_ARRAY_TEXTURES_SUPPORT
38-
return textureSampleCompare(point_shadow_textures, point_shadow_textures_sampler, frag_ls, depth);
41+
return textureSampleCompare(point_shadow_textures, point_shadow_textures_sampler, frag_ls * flip_z, depth);
3942
#else
40-
return textureSampleCompareLevel(point_shadow_textures, point_shadow_textures_sampler, frag_ls, i32(light_id), depth);
43+
return textureSampleCompareLevel(point_shadow_textures, point_shadow_textures_sampler, frag_ls * flip_z, i32(light_id), depth);
4144
#endif
4245
}
4346

0 commit comments

Comments
 (0)