Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalise matrix naming #13489

Merged
merged 7 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/shaders/array_texture.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn fragment(
is_front,
);

pbr_input.is_orthographic = view.projection[3].w == 1.0;
pbr_input.is_orthographic = view.clip_from_view[3].w == 1.0;

pbr_input.N = normalize(pbr_input.world_normal);

Expand Down
6 changes: 3 additions & 3 deletions assets/shaders/custom_gltf_2d.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import bevy_sprite::{
mesh2d_view_bindings::globals,
mesh2d_functions::{get_model_matrix, mesh2d_position_local_to_clip},
mesh2d_functions::{get_world_from_local, mesh2d_position_local_to_clip},
}

struct Vertex {
Expand All @@ -19,8 +19,8 @@ struct VertexOutput {
@vertex
fn vertex(vertex: Vertex) -> VertexOutput {
var out: VertexOutput;
let model = get_model_matrix(vertex.instance_index);
out.clip_position = mesh2d_position_local_to_clip(model, vec4<f32>(vertex.position, 1.0));
let world_from_local = get_world_from_local(vertex.instance_index);
out.clip_position = mesh2d_position_local_to_clip(world_from_local, vec4<f32>(vertex.position, 1.0));
out.color = vertex.color;
out.barycentric = vertex.barycentric;
return out;
Expand Down
4 changes: 2 additions & 2 deletions assets/shaders/custom_vertex_attribute.wgsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import bevy_pbr::mesh_functions::{get_model_matrix, mesh_position_local_to_clip}
#import bevy_pbr::mesh_functions::{get_world_from_local, mesh_position_local_to_clip}

struct CustomMaterial {
color: vec4<f32>,
Expand All @@ -20,7 +20,7 @@ struct VertexOutput {
fn vertex(vertex: Vertex) -> VertexOutput {
var out: VertexOutput;
out.clip_position = mesh_position_local_to_clip(
get_model_matrix(vertex.instance_index),
get_world_from_local(vertex.instance_index),
vec4<f32>(vertex.position, 1.0),
);
out.blend_color = vertex.blend_color;
Expand Down
6 changes: 3 additions & 3 deletions assets/shaders/instancing.wgsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import bevy_pbr::mesh_functions::{get_model_matrix, mesh_position_local_to_clip}
#import bevy_pbr::mesh_functions::{get_world_from_local, mesh_position_local_to_clip}

struct Vertex {
@location(0) position: vec3<f32>,
Expand All @@ -18,12 +18,12 @@ struct VertexOutput {
fn vertex(vertex: Vertex) -> VertexOutput {
let position = vertex.position * vertex.i_pos_scale.w + vertex.i_pos_scale.xyz;
var out: VertexOutput;
// NOTE: Passing 0 as the instance_index to get_model_matrix() is a hack
// NOTE: Passing 0 as the instance_index to get_world_from_local() is a hack
// for this example as the instance_index builtin would map to the wrong
// index in the Mesh array. This index could be passed in via another
// uniform instead but it's unnecessary for the example.
out.clip_position = mesh_position_local_to_clip(
get_model_matrix(0u),
get_world_from_local(0u),
vec4<f32>(position, 1.0)
);
out.color = vertex.i_color;
Expand Down
8 changes: 4 additions & 4 deletions assets/shaders/irradiance_volume_voxel_visualization.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#import bevy_pbr::mesh_view_bindings

struct VoxelVisualizationIrradianceVolumeInfo {
transform: mat4x4<f32>,
inverse_transform: mat4x4<f32>,
world_from_voxel: mat4x4<f32>,
voxel_from_world: mat4x4<f32>,
superdump marked this conversation as resolved.
Show resolved Hide resolved
resolution: vec3<u32>,
// A scale factor that's applied to the diffuse and specular light from the
// light probe. This is in units of cd/m² (candela per square meter).
Expand All @@ -18,12 +18,12 @@ var<uniform> irradiance_volume_info: VoxelVisualizationIrradianceVolumeInfo;
fn fragment(mesh: VertexOutput) -> @location(0) vec4<f32> {
// Snap the world position we provide to `irradiance_volume_light()` to the
// middle of the nearest texel.
var unit_pos = (irradiance_volume_info.inverse_transform *
var unit_pos = (irradiance_volume_info.voxel_from_world *
vec4(mesh.world_position.xyz, 1.0f)).xyz;
let resolution = vec3<f32>(irradiance_volume_info.resolution);
let stp = clamp((unit_pos + 0.5) * resolution, vec3(0.5f), resolution - vec3(0.5f));
let stp_rounded = round(stp - 0.5f) + 0.5f;
let rounded_world_pos = (irradiance_volume_info.transform * vec4(stp_rounded, 1.0f)).xyz;
let rounded_world_pos = (irradiance_volume_info.world_from_voxel * vec4(stp_rounded, 1.0f)).xyz;

// `irradiance_volume_light()` multiplies by intensity, so cancel it out.
// If we take intensity into account, the cubes will be way too bright.
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core_pipeline/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ pub struct DeferredPrepass;

#[derive(Component, ShaderType, Clone)]
pub struct PreviousViewData {
pub inverse_view: Mat4,
pub view_proj: Mat4,
pub view_from_world: Mat4,
pub clip_from_world: Mat4,
}

#[derive(Resource, Default)]
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core_pipeline/src/skybox/skybox.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn coords_to_ray_direction(position: vec2<f32>, viewport: vec4<f32>) -> vec3<f32
// fragment position.
// Use the position on the near clipping plane to avoid -inf world position
// because the far plane of an infinite reverse projection is at infinity.
let view_position_homogeneous = view.inverse_projection * vec4(
let view_position_homogeneous = view.view_from_clip * vec4(
coords_to_viewport_uv(position, viewport) * vec2(2.0, -2.0) + vec2(-1.0, 1.0),
1.0,
1.0,
Expand All @@ -34,7 +34,7 @@ fn coords_to_ray_direction(position: vec2<f32>, viewport: vec4<f32>) -> vec3<f32
// direction to world space. Note that the w element is set to 0.0, as this is a
// vector direction, not a position, That causes the matrix multiplication to ignore
// the translations from the view matrix.
let ray_direction = (view.view * vec4(view_ray_direction, 0.0)).xyz;
let ray_direction = (view.world_from_view * vec4(view_ray_direction, 0.0)).xyz;

return normalize(ray_direction);
}
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_core_pipeline/src/skybox/skybox_prepass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#import bevy_pbr::view_transformations::uv_to_ndc

struct PreviousViewUniforms {
inverse_view: mat4x4<f32>,
view_proj: mat4x4<f32>,
view_from_world: mat4x4<f32>,
clip_from_world: mat4x4<f32>,
}

@group(0) @binding(0) var<uniform> view: View;
Expand All @@ -13,8 +13,8 @@ struct PreviousViewUniforms {
@fragment
fn fragment(in: FullscreenVertexOutput) -> @location(1) vec4<f32> {
let clip_pos = uv_to_ndc(in.uv); // Convert from uv to clip space
let world_pos = view.inverse_view_proj * vec4(clip_pos, 0.0, 1.0);
let prev_clip_pos = (previous_view.view_proj * world_pos).xy;
let world_pos = view.world_from_clip * vec4(clip_pos, 0.0, 1.0);
let prev_clip_pos = (previous_view.clip_from_world * world_pos).xy;
let velocity = (clip_pos - prev_clip_pos) * vec2(0.5, -0.5); // Copied from mesh motion vectors

return vec4(velocity.x, velocity.y, 0.0, 1.0);
Expand Down
22 changes: 11 additions & 11 deletions crates/bevy_gizmos/src/line_joints.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ fn vertex_bevel(vertex: VertexInput) -> VertexOutput {
);
var position = positions[vertex.index];

var clip_a = view.view_proj * vec4(vertex.position_a, 1.);
var clip_b = view.view_proj * vec4(vertex.position_b, 1.);
var clip_c = view.view_proj * vec4(vertex.position_c, 1.);
var clip_a = view.clip_from_world * vec4(vertex.position_a, 1.);
var clip_b = view.clip_from_world * vec4(vertex.position_b, 1.);
var clip_c = view.clip_from_world * vec4(vertex.position_c, 1.);

// Manual near plane clipping to avoid errors when doing the perspective divide inside this shader.
clip_a = clip_near_plane(clip_a, clip_c);
Expand Down Expand Up @@ -97,10 +97,10 @@ fn vertex_miter(vertex: VertexInput) -> VertexOutput {
vec3(0, 0, 0.5),
);
var position = positions[vertex.index];
var clip_a = view.view_proj * vec4(vertex.position_a, 1.);
var clip_b = view.view_proj * vec4(vertex.position_b, 1.);
var clip_c = view.view_proj * vec4(vertex.position_c, 1.);

var clip_a = view.clip_from_world * vec4(vertex.position_a, 1.);
var clip_b = view.clip_from_world * vec4(vertex.position_b, 1.);
var clip_c = view.clip_from_world * vec4(vertex.position_c, 1.);

// Manual near plane clipping to avoid errors when doing the perspective divide inside this shader.
clip_a = clip_near_plane(clip_a, clip_c);
Expand Down Expand Up @@ -148,9 +148,9 @@ fn vertex_miter(vertex: VertexInput) -> VertexOutput {

@vertex
fn vertex_round(vertex: VertexInput) -> VertexOutput {
var clip_a = view.view_proj * vec4(vertex.position_a, 1.);
var clip_b = view.view_proj * vec4(vertex.position_b, 1.);
var clip_c = view.view_proj * vec4(vertex.position_c, 1.);
var clip_a = view.clip_from_world * vec4(vertex.position_a, 1.);
var clip_b = view.clip_from_world * vec4(vertex.position_b, 1.);
var clip_c = view.clip_from_world * vec4(vertex.position_c, 1.);

// Manual near plane clipping to avoid errors when doing the perspective divide inside this shader.
clip_a = clip_near_plane(clip_a, clip_c);
Expand Down Expand Up @@ -245,4 +245,4 @@ struct FragmentOutput {
fn fragment(in: FragmentInput) -> FragmentOutput {
// return FragmentOutput(vec4(1, 1, 1, 1));
return FragmentOutput(in.color);
}
}
14 changes: 7 additions & 7 deletions crates/bevy_gizmos/src/lines.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ fn vertex(vertex: VertexInput) -> VertexOutput {
let position = positions[vertex.index];

// algorithm based on https://wwwtyro.net/2019/11/18/instanced-lines.html
var clip_a = view.view_proj * vec4(vertex.position_a, 1.);
var clip_b = view.view_proj * vec4(vertex.position_b, 1.);
var clip_a = view.clip_from_world * vec4(vertex.position_a, 1.);
var clip_b = view.clip_from_world * vec4(vertex.position_b, 1.);

// Manual near plane clipping to avoid errors when doing the perspective divide inside this shader.
clip_a = clip_near_plane(clip_a, clip_b);
Expand All @@ -69,13 +69,13 @@ fn vertex(vertex: VertexInput) -> VertexOutput {
line_width /= clip.w;

// get height of near clipping plane in world space
let pos0 = view.inverse_projection * vec4(0, -1, 0, 1); // Bottom of the screen
let pos1 = view.inverse_projection * vec4(0, 1, 0, 1); // Top of the screen
let pos0 = view.view_from_clip * vec4(0, -1, 0, 1); // Bottom of the screen
let pos1 = view.view_from_clip * vec4(0, 1, 0, 1); // Top of the screen
let near_clipping_plane_height = length(pos0.xyz - pos1.xyz);

// We can't use vertex.position_X because we may have changed the clip positions with clip_near_plane
let position_a = view.inverse_view_proj * clip_a;
let position_b = view.inverse_view_proj * clip_b;
let position_a = view.inverse_clip_from_world * clip_a;
let position_b = view.inverse_clip_from_world * clip_b;
let world_distance = length(position_a.xyz - position_b.xyz);

// Offset to compensate for moved clip positions. If removed dots on lines will slide when position a is ofscreen.
Expand All @@ -84,7 +84,7 @@ fn vertex(vertex: VertexInput) -> VertexOutput {
uv = (clipped_offset + position.y * world_distance) * resolution.y / near_clipping_plane_height / line_gizmo.line_width;
#else
// Get the distance of b to the camera along camera axes
let camera_b = view.inverse_projection * clip_b;
let camera_b = view.view_from_clip * clip_b;

// This differentiates between orthographic and perspective cameras.
// For orthographic cameras no depth adaptment (depth_adaptment = 1) is needed.
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,15 +612,15 @@ async fn load_gltf<'a, 'b, 'c>(
.skins()
.map(|gltf_skin| {
let reader = gltf_skin.reader(|buffer| Some(&buffer_data[buffer.index()]));
let inverse_bindposes: Vec<Mat4> = reader
let local_to_bone_bind_matrices: Vec<Mat4> = reader
.read_inverse_bind_matrices()
.unwrap()
.map(|mat| Mat4::from_cols_array_2d(&mat))
.collect();

load_context.add_labeled_asset(
skin_label(&gltf_skin),
SkinnedMeshInverseBindposes::from(inverse_bindposes),
SkinnedMeshInverseBindposes::from(local_to_bone_bind_matrices),
)
})
.collect();
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/deferred/pbr_deferred_functions.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn pbr_input_from_deferred_gbuffer(frag_coord: vec4<f32>, gbuffer: vec4<u32>) ->
let N = octahedral_decode(octahedral_normal);

let world_position = vec4(position_ndc_to_world(frag_coord_to_ndc(frag_coord)), 1.0);
let is_orthographic = view.projection[3].w == 1.0;
let is_orthographic = view.clip_from_view[3].w == 1.0;
let V = pbr_functions::calculate_view(world_position, is_orthographic);

pbr.frag_coord = frag_coord;
Expand Down
Loading