Skip to content

Commit

Permalink
add reconstruct_view_space_position
Browse files Browse the repository at this point in the history
  • Loading branch information
IceSentry committed Sep 7, 2023
1 parent d3b52a8 commit f86f3a7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
5 changes: 3 additions & 2 deletions assets/shaders/show_prepass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#import bevy_pbr::mesh_view_bindings globals, view
#import bevy_pbr::prepass_utils
#import bevy_pbr::mesh_vertex_output MeshVertexOutput
#import bevy_pbr::depth_functions
#import bevy_pbr::depth_functions reconstruct_view_space_position

struct ShowPrepassSettings {
show_depth: u32,
Expand All @@ -29,7 +29,8 @@ fn fragment(
let frag_coord = mesh.position;
let uv = frag_coord.xy;
let world_position = bevy_pbr::depth_functions::depth_to_world_position(uv, depth, view.inverse_projection, view.view);
return vec4(world_position, 1.0);
let view_pos = reconstruct_view_space_position(depth, uv);
return vec4(view_pos, 1.0);
} else if settings.show_normals == 1u {
let normal = bevy_pbr::prepass_utils::prepass_normal(mesh.position, sample_index);
return vec4(normal, 1.0);
Expand Down
15 changes: 13 additions & 2 deletions crates/bevy_pbr/src/render/depth_functions.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@
// and then you make p_ndc = vec4(xy, depth_ndc, 1.0); p_view_homogeneous = view.inverse_projection * p_ndc; p_view = p_view_homogeneous.xyz / p_view_homogeneous.w; p_world = view.view * vec4(p_view, 1.0);

fn depth_to_world_position(uv: vec2<f32>, depth: f32, inverse_projection: mat4x4<f32>, view: mat4x4<f32>) -> vec3<f32>{
let xy = uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0);
let p_ndc = vec4(xy, depth, 1.0);
let clip_xy = uv_to_clip(uv);
let p_ndc = vec4(clip_xy, depth, 1.0);
let p_view_homogeneous = inverse_projection * p_ndc;
let p_view = p_view_homogeneous.xyz / p_view_homogeneous.w;
let p_world = view * vec4(p_view, 1.0);
return p_world.xyz;
}

fn reconstruct_view_space_position(uv: vec2<f32>, depth: f32, inverse_projection: mat4x4<f32>) -> vec3<f32> {
let clip_xy = uv_to_clip(uv);
let t = inverse_projection * vec4(clip_xy, depth, 1.0);
let view_xyz = t.xyz / t.w;
return view_xyz;
}

fn uv_to_clip(uv: vec2<f32>) -> vec2<f32>{
return uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0);
}
12 changes: 3 additions & 9 deletions crates/bevy_pbr/src/ssao/gtao.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import bevy_pbr::utils PI, HALF_PI
#import bevy_render::view View
#import bevy_render::globals Globals
#import bevy_pbr::depth_functions reconstruct_view_space_position

@group(0) @binding(0) var preprocessed_depth: texture_2d<f32>;
@group(0) @binding(1) var normals: texture_2d<f32>;
Expand Down Expand Up @@ -78,16 +79,9 @@ fn load_normal_view_space(uv: vec2<f32>) -> vec3<f32> {
return inverse_view * world_normal;
}

fn reconstruct_view_space_position(depth: f32, uv: vec2<f32>) -> vec3<f32> {
let clip_xy = vec2<f32>(uv.x * 2.0 - 1.0, 1.0 - 2.0 * uv.y);
let t = view.inverse_projection * vec4<f32>(clip_xy, depth, 1.0);
let view_xyz = t.xyz / t.w;
return view_xyz;
}

fn load_and_reconstruct_view_space_position(uv: vec2<f32>, sample_mip_level: f32) -> vec3<f32> {
let depth = textureSampleLevel(preprocessed_depth, point_clamp_sampler, uv, sample_mip_level).r;
return reconstruct_view_space_position(depth, uv);
return reconstruct_view_space_position(uv, depth, view.inverse_projection);
}

@compute
Expand All @@ -107,7 +101,7 @@ fn gtao(@builtin(global_invocation_id) global_id: vec3<u32>) {
var pixel_depth = calculate_neighboring_depth_differences(pixel_coordinates);
pixel_depth += 0.00001; // Avoid depth precision issues

let pixel_position = reconstruct_view_space_position(pixel_depth, uv);
let pixel_position = reconstruct_view_space_position(uv, pixel_depth, view.inverse_projection);
let pixel_normal = load_normal_view_space(uv);
let view_vec = normalize(-pixel_position);

Expand Down

0 comments on commit f86f3a7

Please sign in to comment.