Skip to content

Commit

Permalink
Try to normalise matrix naming
Browse files Browse the repository at this point in the history
  • Loading branch information
ricky26 committed May 23, 2024
1 parent da1e6e6 commit 7956006
Show file tree
Hide file tree
Showing 56 changed files with 466 additions and 462 deletions.
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>,
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/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
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 @@ -601,15 +601,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

0 comments on commit 7956006

Please sign in to comment.