Skip to content

Commit

Permalink
rename and add example
Browse files Browse the repository at this point in the history
  • Loading branch information
IceSentry committed Sep 7, 2023
1 parent f86f3a7 commit c6774bd
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,17 @@ description = "A shader and a material that uses it"
category = "Shaders"
wasm = true

[[example]]
name = "shader_depth_position"
path = "examples/shader/shader_depth_position.rs"
doc-scrape-examples = true

[package.metadata.example.shader_depth_position]
name = "shader_depth_position"
description = ""
category = "Shaders"
wasm = false

[[example]]
name = "shader_prepass"
path = "examples/shader/shader_prepass.rs"
Expand Down
8 changes: 7 additions & 1 deletion crates/bevy_pbr/src/render/depth_functions.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ fn depth_to_world_position(uv: vec2<f32>, depth: f32, inverse_projection: mat4x4
return p_world.xyz;
}

fn reconstruct_view_space_position(uv: vec2<f32>, depth: f32, inverse_projection: mat4x4<f32>) -> vec3<f32> {
fn depth_to_world_position_two(uv: vec2<f32>, depth: f32, inverse_projection: mat4x4<f32>, view_world_pos: vec3<f32>) -> vec3<f32>{
let view_pos = depth_to_view_space_position(uv, depth, inverse_projection);
let world_pos = view_pos - view_world_pos;
return world_pos;
}

fn depth_to_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;
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_pbr/src/ssao/gtao.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +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
#import bevy_pbr::depth_functions depth_to_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 @@ -81,7 +81,7 @@ fn load_normal_view_space(uv: vec2<f32>) -> vec3<f32> {

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(uv, depth, view.inverse_projection);
return depth_to_view_space_position(uv, depth, view.inverse_projection);
}

@compute
Expand All @@ -101,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(uv, pixel_depth, view.inverse_projection);
let pixel_position = depth_to_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
85 changes: 85 additions & 0 deletions examples/shader/shader_depth_position.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//! A shader and a material that uses it.

use bevy::{
prelude::*,
reflect::TypePath,
render::render_resource::{AsBindGroup, ShaderRef},
};
use bevy_internal::core_pipeline::prepass::DepthPrepass;

pub const SHADER_HANDLE: Handle<Shader> = Handle::weak_from_u128(8695250969165824);

fn main() {
App::new()
.add_plugins((DefaultPlugins, MaterialPlugin::<CustomMaterial>::default()))
.add_systems(Startup, setup)
.run();
}

const SHADER_CODE: &str = r"
#import bevy_pbr::mesh_vertex_output MeshVertexOutput
#import bevy_pbr::mesh_view_bindings view
#import bevy_pbr::depth_functions depth_to_view_space_position, depth_to_world_position, depth_to_world_position_two
#import bevy_pbr::prepass_utils
@fragment
fn fragment(
mesh: MeshVertexOutput,
) -> @location(0) vec4<f32> {
let depth = bevy_pbr::prepass_utils::prepass_depth(mesh.position, 0u);
let frag_coord = mesh.position;
let uv = frag_coord.xy;
let world_position = depth_to_world_position_two(uv, depth, view.inverse_projection, view.world_position);
// let view_pos = depth_to_view_space_position(depth, uv);
return vec4(world_position / 10.0, 1.0);
// return material.color * textureSample(base_color_texture, base_color_sampler, mesh.uv);
}
";

/// set up a simple 3D scene
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<CustomMaterial>>,
asset_server: Res<AssetServer>,
mut shaders: ResMut<Assets<Shader>>,
) {
shaders.insert(SHADER_HANDLE, Shader::from_wgsl(SHADER_CODE, file!()));

// cube
commands.spawn(MaterialMeshBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
material: materials.add(CustomMaterial {}),
..default()
});

// plane
commands.spawn(MaterialMeshBundle {
mesh: meshes.add(shape::Plane::from_size(5.0).into()),
material: materials.add(CustomMaterial {}),
..default()
});

// camera
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
DepthPrepass,
));
}

/// The Material trait is very configurable, but comes with sensible defaults for all methods.
/// You only need to implement functions for features that need non-default behavior. See the Material api docs for details!
impl Material for CustomMaterial {
fn fragment_shader() -> ShaderRef {
SHADER_HANDLE.into()
}
}

// This is the struct that will be passed to your shader
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
pub struct CustomMaterial {}

0 comments on commit c6774bd

Please sign in to comment.