fix projection
This commit is contained in:
@@ -1,41 +1,50 @@
|
|||||||
use bevy::app::{App, Plugin};
|
use bevy::app::{App, Plugin, PostUpdate};
|
||||||
use bevy::core_pipeline::core_3d::graph::Core3d;
|
use bevy::core_pipeline::core_3d::graph::Core3d;
|
||||||
use bevy::core_pipeline::core_3d::Camera3d;
|
use bevy::core_pipeline::core_3d::Camera3d;
|
||||||
use bevy::core_pipeline::tonemapping::{DebandDither, Tonemapping};
|
use bevy::core_pipeline::tonemapping::{DebandDither, Tonemapping};
|
||||||
use bevy::ecs::bundle::Bundle;
|
use bevy::ecs::bundle::Bundle;
|
||||||
use bevy::ecs::component::Component;
|
use bevy::ecs::component::Component;
|
||||||
use bevy::ecs::reflect::ReflectComponent;
|
use bevy::ecs::reflect::ReflectComponent;
|
||||||
|
use bevy::ecs::schedule::IntoSystemConfigs;
|
||||||
use bevy::math::{Mat4, Vec3A};
|
use bevy::math::{Mat4, Vec3A};
|
||||||
|
use bevy::reflect::std_traits::ReflectDefault;
|
||||||
use bevy::reflect::Reflect;
|
use bevy::reflect::Reflect;
|
||||||
use bevy::render::camera::{
|
use bevy::render::camera::{
|
||||||
Camera, CameraMainTextureUsages, CameraProjection, CameraProjectionPlugin, CameraRenderGraph,
|
Camera, CameraMainTextureUsages, CameraProjection, CameraProjectionPlugin, CameraRenderGraph,
|
||||||
Exposure,
|
Exposure,
|
||||||
};
|
};
|
||||||
|
use bevy::render::extract_component::{ExtractComponent, ExtractComponentPlugin};
|
||||||
use bevy::render::primitives::Frustum;
|
use bevy::render::primitives::Frustum;
|
||||||
use bevy::render::view::{ColorGrading, VisibleEntities};
|
use bevy::render::view::{update_frusta, ColorGrading, VisibilitySystems, VisibleEntities};
|
||||||
use bevy::transform::components::{GlobalTransform, Transform};
|
use bevy::transform::components::{GlobalTransform, Transform};
|
||||||
|
use bevy::transform::TransformSystem;
|
||||||
|
|
||||||
pub struct XrCameraPlugin;
|
pub struct XrCameraPlugin;
|
||||||
|
|
||||||
impl Plugin for XrCameraPlugin {
|
impl Plugin for XrCameraPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_plugins(CameraProjectionPlugin::<XrProjection>::default());
|
app.add_plugins(CameraProjectionPlugin::<XrProjection>::default());
|
||||||
|
app.add_systems(
|
||||||
|
PostUpdate,
|
||||||
|
update_frusta::<XrProjection>
|
||||||
|
.after(TransformSystem::TransformPropagate)
|
||||||
|
.before(VisibilitySystems::UpdatePerspectiveFrusta),
|
||||||
|
);
|
||||||
|
app.add_plugins(ExtractComponentPlugin::<XrProjection>::default());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Component, Reflect, Debug)]
|
#[derive(Debug, Clone, Component, Reflect, ExtractComponent)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component, Default)]
|
||||||
pub struct XrProjection {
|
pub struct XrProjection {
|
||||||
pub projection_matrix: Mat4,
|
pub projection_matrix: Mat4,
|
||||||
pub near: f32,
|
pub near: f32,
|
||||||
pub far: f32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for XrProjection {
|
impl Default for XrProjection {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
near: 0.1,
|
near: 0.1,
|
||||||
far: 1000.,
|
|
||||||
projection_matrix: Mat4::IDENTITY,
|
projection_matrix: Mat4::IDENTITY,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -53,12 +62,32 @@ impl CameraProjection for XrProjection {
|
|||||||
fn update(&mut self, _width: f32, _height: f32) {}
|
fn update(&mut self, _width: f32, _height: f32) {}
|
||||||
|
|
||||||
fn far(&self) -> f32 {
|
fn far(&self) -> f32 {
|
||||||
self.far
|
let far = self.projection_matrix.to_cols_array()[14]
|
||||||
|
/ (self.projection_matrix.to_cols_array()[10] + 1.0);
|
||||||
|
|
||||||
|
far
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO calculate this properly
|
// TODO calculate this properly
|
||||||
fn get_frustum_corners(&self, _z_near: f32, _z_far: f32) -> [Vec3A; 8] {
|
fn get_frustum_corners(&self, _z_near: f32, _z_far: f32) -> [Vec3A; 8] {
|
||||||
Default::default()
|
let ndc_corners = [
|
||||||
|
Vec3A::new(1.0, -1.0, 1.0), // Bottom-right far
|
||||||
|
Vec3A::new(1.0, 1.0, 1.0), // Top-right far
|
||||||
|
Vec3A::new(-1.0, 1.0, 1.0), // Top-left far
|
||||||
|
Vec3A::new(-1.0, -1.0, 1.0), // Bottom-left far
|
||||||
|
Vec3A::new(1.0, -1.0, -1.0), // Bottom-right near
|
||||||
|
Vec3A::new(1.0, 1.0, -1.0), // Top-right near
|
||||||
|
Vec3A::new(-1.0, 1.0, -1.0), // Top-left near
|
||||||
|
Vec3A::new(-1.0, -1.0, -1.0), // Bottom-left near
|
||||||
|
];
|
||||||
|
|
||||||
|
let mut view_space_corners = [Vec3A::ZERO; 8];
|
||||||
|
let inverse_matrix = self.projection_matrix.inverse();
|
||||||
|
for (i, corner) in ndc_corners.into_iter().enumerate() {
|
||||||
|
view_space_corners[i] = inverse_matrix.transform_point3a(corner);
|
||||||
|
}
|
||||||
|
|
||||||
|
view_space_corners
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user