From c6774bddd80c1c4b37d97496c0d2f6e1507bd93f Mon Sep 17 00:00:00 2001 From: IceSentry Date: Thu, 7 Sep 2023 18:42:39 -0400 Subject: [PATCH] rename and add example --- Cargo.toml | 11 +++ .../bevy_pbr/src/render/depth_functions.wgsl | 8 +- crates/bevy_pbr/src/ssao/gtao.wgsl | 6 +- examples/shader/shader_depth_position.rs | 85 +++++++++++++++++++ 4 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 examples/shader/shader_depth_position.rs diff --git a/Cargo.toml b/Cargo.toml index b0f2d6f6109124..7f5ad7062c4676 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/crates/bevy_pbr/src/render/depth_functions.wgsl b/crates/bevy_pbr/src/render/depth_functions.wgsl index 6d12373486ef9a..a80ac3ac1b15fe 100644 --- a/crates/bevy_pbr/src/render/depth_functions.wgsl +++ b/crates/bevy_pbr/src/render/depth_functions.wgsl @@ -13,7 +13,13 @@ fn depth_to_world_position(uv: vec2, depth: f32, inverse_projection: mat4x4 return p_world.xyz; } -fn reconstruct_view_space_position(uv: vec2, depth: f32, inverse_projection: mat4x4) -> vec3 { +fn depth_to_world_position_two(uv: vec2, depth: f32, inverse_projection: mat4x4, view_world_pos: vec3) -> vec3{ + 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, depth: f32, inverse_projection: mat4x4) -> vec3 { let clip_xy = uv_to_clip(uv); let t = inverse_projection * vec4(clip_xy, depth, 1.0); let view_xyz = t.xyz / t.w; diff --git a/crates/bevy_pbr/src/ssao/gtao.wgsl b/crates/bevy_pbr/src/ssao/gtao.wgsl index 78f2e6858ac4b5..bd8c1acc580092 100644 --- a/crates/bevy_pbr/src/ssao/gtao.wgsl +++ b/crates/bevy_pbr/src/ssao/gtao.wgsl @@ -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; @group(0) @binding(1) var normals: texture_2d; @@ -81,7 +81,7 @@ fn load_normal_view_space(uv: vec2) -> vec3 { fn load_and_reconstruct_view_space_position(uv: vec2, sample_mip_level: f32) -> vec3 { 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 @@ -101,7 +101,7 @@ fn gtao(@builtin(global_invocation_id) global_id: vec3) { 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); diff --git a/examples/shader/shader_depth_position.rs b/examples/shader/shader_depth_position.rs new file mode 100644 index 00000000000000..df88afe7f0a092 --- /dev/null +++ b/examples/shader/shader_depth_position.rs @@ -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 = Handle::weak_from_u128(8695250969165824); + +fn main() { + App::new() + .add_plugins((DefaultPlugins, MaterialPlugin::::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 { + 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>, + mut materials: ResMut>, + asset_server: Res, + mut shaders: ResMut>, +) { + 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 {}