2022-12-05 23:01:16 +01:00
|
|
|
use bevy::{prelude::*, math::bool, render::primitives::Frustum};
|
|
|
|
use rand::prelude::*;
|
|
|
|
use bevy::render::primitives::Plane;
|
|
|
|
|
2022-07-15 23:42:22 +02:00
|
|
|
|
|
|
|
pub struct BuildScenePlugin;
|
|
|
|
|
|
|
|
impl Plugin for BuildScenePlugin {
|
|
|
|
fn build(&self, app: &mut App) {
|
2022-11-16 23:07:49 +01:00
|
|
|
app.add_startup_system(build_scene) // actual scene
|
2022-11-17 23:18:30 +01:00
|
|
|
.add_system(print_positions); // debugging
|
2022-07-15 23:42:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct Person;
|
|
|
|
|
|
|
|
#[derive(Component)]
|
2022-11-17 23:18:30 +01:00
|
|
|
pub struct Position {
|
|
|
|
x: f32,
|
|
|
|
y: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
pub struct ProjectionScreen {
|
|
|
|
bottom_left: Vec3,
|
|
|
|
bottom_right: Vec3,
|
|
|
|
top_left: Vec3,
|
|
|
|
top_right: Vec3,
|
|
|
|
|
|
|
|
dir_right: Vec3,
|
|
|
|
dir_up: Vec3,
|
|
|
|
dir_normal: Vec3,
|
|
|
|
|
|
|
|
matrix: Mat4
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-12-05 23:01:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
fn generalized_projection_stereo(eyePos : Vec3)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn generalized_projection (
|
2022-11-17 23:18:30 +01:00
|
|
|
projectionScreen: ProjectionScreen,
|
|
|
|
eyePos: Vec3,
|
|
|
|
mut camera: Camera3dBundle,
|
|
|
|
clampNearPlane: bool,
|
|
|
|
)
|
|
|
|
{
|
|
|
|
let pa = projectionScreen.bottom_left;
|
|
|
|
let pb = projectionScreen.bottom_right;
|
|
|
|
let pc = projectionScreen.top_left;
|
|
|
|
let pd = projectionScreen.top_right;
|
|
|
|
|
|
|
|
let vu = projectionScreen.dir_up;
|
|
|
|
let vr = projectionScreen.dir_right;
|
|
|
|
let vn = projectionScreen.dir_normal;
|
|
|
|
|
2022-12-05 23:01:16 +01:00
|
|
|
// let m = projectionScreen.matrix;
|
|
|
|
|
|
|
|
//let m = Mat4::from_cols(vu., y_axis, z_axis, w_axis)
|
2022-11-17 23:18:30 +01:00
|
|
|
|
|
|
|
let va = pa - eyePos;
|
|
|
|
let vb = pb - eyePos;
|
|
|
|
let vc = pc - eyePos;
|
|
|
|
let vd = pd - eyePos;
|
2022-07-15 23:42:22 +02:00
|
|
|
|
2022-11-17 23:18:30 +01:00
|
|
|
let viewDir = eyePos + va + vb + vc + vd;
|
2022-07-15 23:42:22 +02:00
|
|
|
|
2022-11-17 23:18:30 +01:00
|
|
|
let d = -va.dot(vn);
|
2022-11-16 23:32:35 +01:00
|
|
|
|
2022-12-05 23:01:16 +01:00
|
|
|
|
|
|
|
if clampNearPlane {
|
|
|
|
_ = 33;
|
2022-11-17 23:18:30 +01:00
|
|
|
// camera.frustum.planes[4]. // should be near
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build_scene(
|
|
|
|
mut commands: Commands,
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
|
|
) {
|
|
|
|
// plane
|
|
|
|
commands.spawn(PbrBundle {
|
|
|
|
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
|
|
|
|
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
// cube
|
|
|
|
commands.spawn(PbrBundle {
|
|
|
|
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
|
|
|
|
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
|
|
|
|
transform: Transform::from_xyz(0.0, 0.5, 0.0),
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
// light
|
|
|
|
commands.spawn(PointLightBundle {
|
|
|
|
point_light: PointLight {
|
|
|
|
intensity: 1500.0,
|
|
|
|
shadows_enabled: true,
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
transform: Transform::from_xyz(4.0, 8.0, 4.0),
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// camera
|
|
|
|
|
|
|
|
let mut cam = Camera3dBundle {
|
|
|
|
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
|
2022-12-05 23:01:16 +01:00
|
|
|
|
2022-11-17 23:18:30 +01:00
|
|
|
..default()
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-12-05 23:01:16 +01:00
|
|
|
|
|
|
|
// commands.spawn(cam);
|
|
|
|
|
|
|
|
// commands.spawn((Person, Position { x: 10.0, y: 10.0 }));
|
2022-11-16 23:10:13 +01:00
|
|
|
// commands.spawn()
|
|
|
|
// .insert(Person)
|
|
|
|
// .insert(Position { x: 10.0, y: 10.0 } );
|
2022-08-07 22:26:12 +02:00
|
|
|
|
|
|
|
// camera
|
|
|
|
// commands.spawn_bundle(OffAxisProjection {
|
|
|
|
// transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
|
|
|
|
// ..default()
|
|
|
|
// });
|
2022-07-15 23:42:22 +02:00
|
|
|
}
|
|
|
|
|
2022-12-05 23:01:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
pub fn modify_frustum(mut query: Query<&Frustum>)
|
|
|
|
{
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
|
|
|
for mut q in query.iter() {
|
|
|
|
|
|
|
|
for mut p in q.planes {
|
|
|
|
|
|
|
|
println!("{:?}",p);
|
|
|
|
|
|
|
|
let mut n = p.normal_d();
|
|
|
|
|
|
|
|
n.w += rng.gen::<f32>();
|
|
|
|
|
|
|
|
p = Plane::new(n);
|
|
|
|
|
|
|
|
println!("{:?}",p);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// p.normal_d() += rng.gen();
|
|
|
|
}
|
|
|
|
|
|
|
|
// println!("{:?}",q.frustum.planes.len());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn modify_projection(mut query: Query<&Projection>)
|
|
|
|
{
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
|
|
|
for mut q in query.iter() {
|
|
|
|
|
|
|
|
print!("{:?}",q);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 23:18:30 +01:00
|
|
|
pub fn print_positions(query: Query<&Position>) {
|
2022-11-16 23:07:49 +01:00
|
|
|
for _position in query.iter() {
|
|
|
|
_ = 33;
|
2022-08-07 22:26:12 +02:00
|
|
|
// println!("position {:?} {:?}", position.x, position.y)
|
2022-07-15 23:42:22 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-05 23:01:16 +01:00
|
|
|
|
|
|
|
// Problem with off-axis
|
|
|
|
// projection 2401, 240
|