|
|
|
@ -1,14 +1,15 @@
|
|
|
|
|
use std::f32::consts::PI;
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use std::time::Instant;
|
|
|
|
|
|
|
|
|
|
use cgmath::{Euler, Quaternion, Deg, Rad, Point3};
|
|
|
|
|
use cgmath::{Deg, Euler, Point3, Quaternion, Rad};
|
|
|
|
|
use imgui::FontSource;
|
|
|
|
|
use legion::*;
|
|
|
|
|
use legion::IntoQuery;
|
|
|
|
|
use legion::systems::CommandBuffer;
|
|
|
|
|
use legion::world::SubWorld;
|
|
|
|
|
use legion::IntoQuery;
|
|
|
|
|
use legion::*;
|
|
|
|
|
use nalgebra::Quaternion as naQuaternion;
|
|
|
|
|
use rapier3d::dynamics::{IntegrationParameters, JointSet, RigidBodyBuilder, RigidBodySet};
|
|
|
|
|
use rapier3d::dynamics::{IntegrationParameters, JointSet, RigidBodyBuilder, RigidBodySet, MassProperties};
|
|
|
|
|
use rapier3d::geometry::{BroadPhase, ColliderBuilder, ColliderSet, NarrowPhase};
|
|
|
|
|
use rapier3d::na::{Isometry3, Vector, Vector3};
|
|
|
|
|
use rapier3d::pipeline::{ChannelEventCollector, PhysicsPipeline};
|
|
|
|
@ -19,14 +20,10 @@ use crate::geometry::RawMesh;
|
|
|
|
|
use crate::physics::state::PhysicsState;
|
|
|
|
|
use crate::render::state::RenderState;
|
|
|
|
|
use crate::runtime::state::RuntimeState;
|
|
|
|
|
use std::f32::consts::PI;
|
|
|
|
|
|
|
|
|
|
#[system]
|
|
|
|
|
#[write_component(Mesh)]
|
|
|
|
|
pub fn runtime_load(
|
|
|
|
|
world: &mut SubWorld,
|
|
|
|
|
#[resource] runtime_state: &mut RuntimeState,
|
|
|
|
|
) {
|
|
|
|
|
pub fn runtime_load(world: &mut SubWorld, #[resource] runtime_state: &mut RuntimeState) {
|
|
|
|
|
runtime_state.preload_meshes(PathBuf::from("./resources"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -38,7 +35,6 @@ pub fn runtime_spawn(
|
|
|
|
|
#[resource] runtime_state: &mut RuntimeState,
|
|
|
|
|
#[resource] renderer: &mut RenderState,
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
|
|
for entity in runtime_state.get_configured_entities() {
|
|
|
|
|
match entity.ent_type.as_ref() {
|
|
|
|
|
"PhysicsEntity" => {
|
|
|
|
@ -48,44 +44,52 @@ pub fn runtime_spawn(
|
|
|
|
|
log::warn!("Skipping entity with invalid mesh file {:?} ", mesh_name);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Some(mesh) => mesh
|
|
|
|
|
Some(mesh) => mesh,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut dynamic_body = RigidBodyBuilder::new_dynamic()
|
|
|
|
|
.can_sleep(false)
|
|
|
|
|
.mass(1.0)
|
|
|
|
|
.translation(0.0, 0.0, 0.0)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
let collider = ColliderBuilder::trimesh(
|
|
|
|
|
raw_mesh.vertices
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|v| v.position())
|
|
|
|
|
.collect(),
|
|
|
|
|
raw_mesh.indices.clone(),
|
|
|
|
|
).build();
|
|
|
|
|
// let collider = ColliderBuilder::trimesh(
|
|
|
|
|
// raw_mesh.vertices.iter().map(|v| v.position()).collect(),
|
|
|
|
|
// raw_mesh.indices.clone(),
|
|
|
|
|
// ).density(1.0)
|
|
|
|
|
// .build();
|
|
|
|
|
|
|
|
|
|
let collider = ColliderBuilder::ball(2.0).build();
|
|
|
|
|
|
|
|
|
|
let gpu_mesh_buffer = renderer.upload_mesh_to_buffer(
|
|
|
|
|
let gpu_mesh_buffer = renderer
|
|
|
|
|
.upload_mesh_to_buffer(
|
|
|
|
|
raw_mesh,
|
|
|
|
|
Some(wgpu::Color {
|
|
|
|
|
r: 1.0,
|
|
|
|
|
g: 0.7,
|
|
|
|
|
b: 0.3,
|
|
|
|
|
a: 1.0,
|
|
|
|
|
})
|
|
|
|
|
).unwrap();
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let entity: Entity = cmd.push((
|
|
|
|
|
Position {
|
|
|
|
|
let position = match entity.position {
|
|
|
|
|
None => Position {
|
|
|
|
|
x: 0.0,
|
|
|
|
|
y: 20.0,
|
|
|
|
|
y: rand::random::<f32>(),
|
|
|
|
|
z: 0.0,
|
|
|
|
|
rot: Euler {
|
|
|
|
|
x: Deg(25.0),
|
|
|
|
|
y: Deg(45.0),
|
|
|
|
|
z: Deg(15.0),
|
|
|
|
|
x: Deg(0.0),
|
|
|
|
|
y: Deg(0.0),
|
|
|
|
|
z: Deg(0.0),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Some(position) => position
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut dynamic_body = RigidBodyBuilder::new_dynamic()
|
|
|
|
|
.can_sleep(false)
|
|
|
|
|
.mass(100.0)
|
|
|
|
|
.translation(position.x, position.y, position.z)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
let entity: Entity = cmd.push((
|
|
|
|
|
position,
|
|
|
|
|
gpu_mesh_buffer,
|
|
|
|
|
Physics {
|
|
|
|
|
rigid_body: dynamic_body,
|
|
|
|
@ -96,7 +100,7 @@ pub fn runtime_spawn(
|
|
|
|
|
collider_handle: None,
|
|
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
"Terrain" => {
|
|
|
|
|
let mesh_name = entity.mesh.unwrap();
|
|
|
|
|
let raw_mesh = match runtime_state.get_mesh(mesh_name.as_str()) {
|
|
|
|
@ -104,7 +108,7 @@ pub fn runtime_spawn(
|
|
|
|
|
log::warn!("Skipping entity with invalid mesh file {:?} ", mesh_name);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Some(mesh) => mesh
|
|
|
|
|
Some(mesh) => mesh,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut static_body = RigidBodyBuilder::new_static()
|
|
|
|
@ -112,28 +116,27 @@ pub fn runtime_spawn(
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
let mesh_collider = ColliderBuilder::trimesh(
|
|
|
|
|
raw_mesh.vertices
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|v| v.position())
|
|
|
|
|
.collect(),
|
|
|
|
|
raw_mesh.vertices.iter().map(|v| v.position()).collect(),
|
|
|
|
|
raw_mesh.indices.clone(),
|
|
|
|
|
).build();
|
|
|
|
|
)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
let gpu_mesh_buffer = renderer.upload_mesh_to_buffer(
|
|
|
|
|
let gpu_mesh_buffer = renderer
|
|
|
|
|
.upload_mesh_to_buffer(
|
|
|
|
|
raw_mesh,
|
|
|
|
|
Some(wgpu::Color {
|
|
|
|
|
r: 1.0,
|
|
|
|
|
g: 0.7,
|
|
|
|
|
b: 0.3,
|
|
|
|
|
a: 1.0,
|
|
|
|
|
})
|
|
|
|
|
).unwrap();
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let entity: Entity = cmd.push((
|
|
|
|
|
Position {
|
|
|
|
|
let position = match entity.position {
|
|
|
|
|
None => Position {
|
|
|
|
|
x: 0.0,
|
|
|
|
|
y: -8.0,
|
|
|
|
|
y: 0.0,
|
|
|
|
|
z: 0.0,
|
|
|
|
|
rot: Euler {
|
|
|
|
|
x: Deg(0.0),
|
|
|
|
@ -141,6 +144,12 @@ pub fn runtime_spawn(
|
|
|
|
|
z: Deg(0.0),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Some(position) => position
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let entity: Entity = cmd.push((
|
|
|
|
|
position,
|
|
|
|
|
gpu_mesh_buffer,
|
|
|
|
|
Physics {
|
|
|
|
|
rigid_body: static_body,
|
|
|
|
@ -151,9 +160,8 @@ pub fn runtime_spawn(
|
|
|
|
|
collider_handle: None,
|
|
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
"Camera" => {
|
|
|
|
|
|
|
|
|
|
let entity: Entity = cmd.push((
|
|
|
|
|
Camera {
|
|
|
|
|
position: cgmath::Point3 {
|
|
|
|
@ -166,7 +174,7 @@ pub fn runtime_spawn(
|
|
|
|
|
},
|
|
|
|
|
CameraController::new(3.0, 1.0),
|
|
|
|
|
));
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
"Light" => {
|
|
|
|
|
let mesh_name = entity.mesh.unwrap();
|
|
|
|
|
let raw_mesh = match runtime_state.get_mesh(mesh_name.as_str()) {
|
|
|
|
@ -174,40 +182,43 @@ pub fn runtime_spawn(
|
|
|
|
|
log::warn!("Skipping entity with invalid mesh file {:?} ", mesh_name);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Some(mesh) => mesh
|
|
|
|
|
Some(mesh) => mesh,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let gpu_mesh_buffer = renderer.upload_mesh_to_buffer(
|
|
|
|
|
let position = match entity.position {
|
|
|
|
|
None => Position {
|
|
|
|
|
x: 1.0,
|
|
|
|
|
y: 1.0,
|
|
|
|
|
z: 1.0,
|
|
|
|
|
rot: Euler {
|
|
|
|
|
x: Deg(0.0),
|
|
|
|
|
y: Deg(0.0),
|
|
|
|
|
z: Deg(0.0),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Some(position) => position
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let gpu_mesh_buffer = renderer
|
|
|
|
|
.upload_mesh_to_buffer(
|
|
|
|
|
raw_mesh,
|
|
|
|
|
Some(wgpu::Color {
|
|
|
|
|
r: 1.0,
|
|
|
|
|
g: 0.7,
|
|
|
|
|
b: 0.3,
|
|
|
|
|
a: 1.0,
|
|
|
|
|
})
|
|
|
|
|
).unwrap();
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let light_entity: Entity = cmd.push((
|
|
|
|
|
Position {
|
|
|
|
|
x: 5.0,
|
|
|
|
|
y: 10.0,
|
|
|
|
|
z: 5.0,
|
|
|
|
|
rot: Euler {
|
|
|
|
|
x: Deg(0.0),
|
|
|
|
|
y: Deg(-25.0),
|
|
|
|
|
z: Deg(0.0),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
position,
|
|
|
|
|
gpu_mesh_buffer,
|
|
|
|
|
renderer.create_light(),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_ => {},
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|