camera projection wip
This commit is contained in:
23
src/app.rs
23
src/app.rs
@@ -1,12 +1,19 @@
|
||||
use bevy::{
|
||||
asset::RenderAssetUsages,
|
||||
math::VectorSpace,
|
||||
prelude::*,
|
||||
render::render_resource::{Extent3d, TextureDimension, TextureFormat, TextureUsages},
|
||||
render::{
|
||||
render_resource::{Extent3d, TextureDimension, TextureFormat, TextureUsages},
|
||||
view::RenderLayers,
|
||||
},
|
||||
window::PrimaryWindow,
|
||||
};
|
||||
use iyes_perf_ui::prelude::*;
|
||||
|
||||
use crate::render::pipeline::{TracerPipelinePlugin, TracerRenderTextures, TracerUniforms};
|
||||
use crate::{
|
||||
components::rt::RTCamera,
|
||||
render::pipeline::{TracerPipelinePlugin, TracerRenderTextures, TracerUniforms},
|
||||
};
|
||||
|
||||
pub struct Blackhole;
|
||||
|
||||
@@ -18,6 +25,7 @@ impl Plugin for Blackhole {
|
||||
app.add_plugins(TracerPipelinePlugin);
|
||||
app.insert_resource(TracerUniforms {
|
||||
sky_color: LinearRgba::BLUE,
|
||||
..default()
|
||||
});
|
||||
|
||||
//Perf UI
|
||||
@@ -74,5 +82,16 @@ fn setup(mut commands: Commands, mut images: ResMut<Assets<Image>>, window: Sing
|
||||
|
||||
commands.spawn(Camera2d);
|
||||
|
||||
commands.spawn((
|
||||
// Camera3d::default(),
|
||||
Projection::Perspective(PerspectiveProjection {
|
||||
aspect_ratio: size.x as f32 / size.y as f32,
|
||||
..default()
|
||||
}),
|
||||
RTCamera,
|
||||
Transform::from_xyz(0.0, 0.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
|
||||
Name::new("RT Camera"),
|
||||
));
|
||||
|
||||
commands.insert_resource(TracerRenderTextures(img0, img1));
|
||||
}
|
||||
|
||||
1
src/components/mod.rs
Normal file
1
src/components/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod rt;
|
||||
4
src/components/rt.rs
Normal file
4
src/components/rt.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
use bevy::{prelude::*, render::render_resource::encase::private::ShaderType};
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct RTCamera;
|
||||
@@ -4,6 +4,7 @@ use bevy::window::PresentMode;
|
||||
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
|
||||
|
||||
mod app;
|
||||
pub mod components;
|
||||
mod render;
|
||||
|
||||
pub const SHADER_ASSET_PATH: &str = "trace.wgsl";
|
||||
|
||||
@@ -4,6 +4,7 @@ use bevy::{
|
||||
prelude::*,
|
||||
render::{
|
||||
Render, RenderApp, RenderSet,
|
||||
camera::CameraProjection,
|
||||
extract_resource::{ExtractResource, ExtractResourcePlugin},
|
||||
render_asset::RenderAssets,
|
||||
render_graph::{RenderGraph, RenderLabel},
|
||||
@@ -15,11 +16,10 @@ use bevy::{
|
||||
},
|
||||
renderer::{RenderDevice, RenderQueue},
|
||||
texture::GpuImage,
|
||||
view::{ViewUniform, ViewUniforms},
|
||||
},
|
||||
};
|
||||
|
||||
use crate::{SHADER_ASSET_PATH, render::node::TracerNode};
|
||||
use crate::{SHADER_ASSET_PATH, components::rt::RTCamera, render::node::TracerNode};
|
||||
|
||||
#[derive(Debug, Hash, PartialEq, Eq, Clone, RenderLabel)]
|
||||
pub struct TracerLabel;
|
||||
@@ -31,6 +31,8 @@ pub struct TracerRenderTextures(pub Handle<Image>, pub Handle<Image>);
|
||||
#[derive(Resource, Clone, ExtractResource, ShaderType, Default)]
|
||||
pub struct TracerUniforms {
|
||||
pub sky_color: LinearRgba,
|
||||
pub world_from_clip: Mat4,
|
||||
pub world_position: Vec3,
|
||||
}
|
||||
|
||||
pub struct TracerPipelinePlugin;
|
||||
@@ -43,6 +45,8 @@ impl Plugin for TracerPipelinePlugin {
|
||||
));
|
||||
app.init_resource::<TracerUniforms>()
|
||||
.add_systems(Update, switch_textures);
|
||||
|
||||
app.add_systems(First, update_tracer_uniforms);
|
||||
let render_app = app.sub_app_mut(RenderApp);
|
||||
|
||||
// render_app.add_systems(Startup, init_pipeline);
|
||||
@@ -86,7 +90,6 @@ impl FromWorld for TracerPipeline {
|
||||
texture_storage_2d(TextureFormat::Rgba32Float, StorageTextureAccess::ReadOnly),
|
||||
texture_storage_2d(TextureFormat::Rgba32Float, StorageTextureAccess::WriteOnly),
|
||||
uniform_buffer::<TracerUniforms>(false),
|
||||
uniform_buffer::<ViewUniform>(false),
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -135,7 +138,6 @@ fn init_pipeline(
|
||||
texture_storage_2d(TextureFormat::Rgba32Float, StorageTextureAccess::ReadOnly),
|
||||
texture_storage_2d(TextureFormat::Rgba32Float, StorageTextureAccess::WriteOnly),
|
||||
uniform_buffer::<TracerUniforms>(false),
|
||||
uniform_buffer::<ViewUniform>(false),
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -170,6 +172,24 @@ fn init_pipeline(
|
||||
#[derive(Resource)]
|
||||
pub struct TracerImageBindGroups(pub [BindGroup; 2]);
|
||||
|
||||
fn update_tracer_uniforms(
|
||||
mut tracer_uniforms: ResMut<TracerUniforms>,
|
||||
rt_camera: Single<(&GlobalTransform, &Projection), With<RTCamera>>,
|
||||
) {
|
||||
let (transform, projection) = rt_camera.into_inner();
|
||||
let view = transform.compute_matrix().inverse();
|
||||
let clip_from_view = match projection {
|
||||
Projection::Perspective(perspective_projection) => perspective_projection.get_clip_from_view(),
|
||||
_ => unreachable!("This should never happen: Invalid projection type on RT Camera"),
|
||||
};
|
||||
let clip_from_world = clip_from_view * view;
|
||||
let world_from_clip = clip_from_world.inverse();
|
||||
info_once!("world_from_clip = {:?}", world_from_clip);
|
||||
|
||||
tracer_uniforms.world_from_clip = world_from_clip;
|
||||
tracer_uniforms.world_position = transform.translation();
|
||||
}
|
||||
|
||||
fn prepare_bind_groups(
|
||||
mut commands: Commands,
|
||||
pipeline: Res<TracerPipeline>,
|
||||
@@ -178,13 +198,10 @@ fn prepare_bind_groups(
|
||||
tracer_uniforms: Res<TracerUniforms>,
|
||||
render_device: Res<RenderDevice>,
|
||||
queue: Res<RenderQueue>,
|
||||
view_uniforms: Res<ViewUniforms>,
|
||||
) {
|
||||
let view_a = gpu_images.get(&tracer_images.0).unwrap();
|
||||
let view_b = gpu_images.get(&tracer_images.1).unwrap();
|
||||
|
||||
//Todo: Insert View Uniforms
|
||||
|
||||
// Uniform buffer is used here to demonstrate how to set up a uniform in a compute shader
|
||||
// Alternatives such as storage buffers or push constants may be more suitable for your use case
|
||||
let mut uniform_buffer = UniformBuffer::from(tracer_uniforms.into_inner());
|
||||
|
||||
Reference in New Issue
Block a user