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