parent
a1305bed2d
commit
c2b62a5f53
@ -1,13 +1,10 @@
|
|||||||
|
|
||||||
[[entities]]
|
[[entities]]
|
||||||
[entities.entity1]
|
|
||||||
name = "terrain.1"
|
name = "terrain.1"
|
||||||
type = "Mesh"
|
type = "Terrain"
|
||||||
mesh = "test-textured.obj"
|
mesh = "test-textured.obj"
|
||||||
|
|
||||||
[[entities]]
|
[[entities]]
|
||||||
[entities.entity2]
|
|
||||||
name = "ball.1"
|
name = "ball.1"
|
||||||
type = ""
|
type = "PhysicsEntity"
|
||||||
mesh = "test-textured.obj"
|
mesh = "ball.obj"
|
||||||
|
|
||||||
|
@ -1,33 +1,159 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
use cgmath::{Euler, Quaternion};
|
use cgmath::{Euler, Quaternion, Deg};
|
||||||
use legion::world::SubWorld;
|
use imgui::FontSource;
|
||||||
use legion::IntoQuery;
|
|
||||||
use legion::*;
|
use legion::*;
|
||||||
|
use legion::IntoQuery;
|
||||||
|
use legion::systems::CommandBuffer;
|
||||||
|
use legion::world::SubWorld;
|
||||||
use nalgebra::Quaternion as naQuaternion;
|
use nalgebra::Quaternion as naQuaternion;
|
||||||
use rapier3d::dynamics::{IntegrationParameters, JointSet, RigidBodySet};
|
use rapier3d::dynamics::{IntegrationParameters, JointSet, RigidBodyBuilder, RigidBodySet};
|
||||||
use rapier3d::geometry::{BroadPhase, ColliderSet, NarrowPhase};
|
use rapier3d::geometry::{BroadPhase, ColliderBuilder, ColliderSet, NarrowPhase};
|
||||||
use rapier3d::pipeline::{PhysicsPipeline, ChannelEventCollector};
|
use rapier3d::na::{Isometry3, Vector, Vector3};
|
||||||
|
use rapier3d::pipeline::{ChannelEventCollector, PhysicsPipeline};
|
||||||
|
|
||||||
use crate::camera::{Camera, CameraController};
|
use crate::camera::{Camera, CameraController};
|
||||||
use crate::components::{Collider, LoopState, Mesh, Physics, Position};
|
use crate::components::{Collider, LoopState, Mesh, Physics, Position};
|
||||||
use imgui::FontSource;
|
use crate::geometry::RawMesh;
|
||||||
use crate::physics::state::PhysicsState;
|
use crate::physics::state::PhysicsState;
|
||||||
|
use crate::render::state::RenderState;
|
||||||
|
use crate::runtime::state::RuntimeState;
|
||||||
|
|
||||||
#[system]
|
#[system]
|
||||||
#[write_component(Collider)]
|
|
||||||
#[write_component(Physics)]
|
|
||||||
#[write_component(Mesh)]
|
#[write_component(Mesh)]
|
||||||
pub fn runtime_load(
|
pub fn runtime_load(
|
||||||
world: &mut SubWorld,
|
world: &mut SubWorld,
|
||||||
#[resource] physics_state: &mut PhysicsState,
|
#[resource] runtime_state: &mut RuntimeState,
|
||||||
#[resource] physics_pipeline: &mut PhysicsPipeline,
|
) {
|
||||||
|
runtime_state.preload_meshes(PathBuf::from("./resources"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[system]
|
||||||
|
#[write_component(Mesh)]
|
||||||
|
pub fn runtime_spawn(
|
||||||
|
cmd: &mut CommandBuffer,
|
||||||
|
world: &mut SubWorld,
|
||||||
|
#[resource] runtime_state: &mut RuntimeState,
|
||||||
|
#[resource] renderer: &mut RenderState,
|
||||||
) {
|
) {
|
||||||
// Make sure all the entities we care about are added to the system
|
|
||||||
let mut query = <(&mut Collider, &mut Physics, &mut Mesh)>::query();
|
for entity in runtime_state.get_configured_entities(){
|
||||||
for (collider, physics, mesh) in query.iter_mut(world) {
|
match entity.ent_type.as_ref() {
|
||||||
|
"Terrain" => {
|
||||||
|
let raw_mesh = match runtime_state.get_mesh(entity.mesh.as_str()) {
|
||||||
|
None => {
|
||||||
|
log::warn!("Skipping entity with invalid mesh file {:?} ", entity.mesh);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Some(mesh) => mesh
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut static_body = RigidBodyBuilder::new_static()
|
||||||
|
.position(Isometry3::new(Vector3::new(0.0, -8.0, 0.0), Vector::y()))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let mesh_collider = ColliderBuilder::trimesh(
|
||||||
|
raw_mesh.vertices
|
||||||
|
.iter()
|
||||||
|
.map(|v| v.position())
|
||||||
|
.collect(),
|
||||||
|
raw_mesh.indices.clone(),
|
||||||
|
).build();
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
|
||||||
|
let entity: Entity = cmd.push((
|
||||||
|
Position {
|
||||||
|
x: 0.0,
|
||||||
|
y: -8.0,
|
||||||
|
z: 0.0,
|
||||||
|
rot: Euler {
|
||||||
|
x: Deg(0.0),
|
||||||
|
y: Deg(0.0),
|
||||||
|
z: Deg(0.0),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
gpu_mesh_buffer,
|
||||||
|
Physics {
|
||||||
|
rigid_body: static_body,
|
||||||
|
rigid_body_handle: None,
|
||||||
|
},
|
||||||
|
Collider {
|
||||||
|
collider: mesh_collider,
|
||||||
|
collider_handle: None,
|
||||||
|
},
|
||||||
|
));
|
||||||
|
},
|
||||||
|
"PhysicsEntity" => {
|
||||||
|
|
||||||
|
let raw_mesh = match runtime_state.get_mesh(entity.mesh.as_str()) {
|
||||||
|
None => {
|
||||||
|
log::warn!("Skipping entity with invalid mesh file {:?} ", entity.mesh);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Some(mesh) => mesh
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut dynamic_body = RigidBodyBuilder::new_dynamic()
|
||||||
|
.can_sleep(false)
|
||||||
|
.mass(1.0)
|
||||||
|
.translation(0.0, 35.0, 0.0)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let collider = ColliderBuilder::trimesh(
|
||||||
|
raw_mesh.vertices
|
||||||
|
.iter()
|
||||||
|
.map(|v| v.position())
|
||||||
|
.collect(),
|
||||||
|
raw_mesh.indices.clone(),
|
||||||
|
).build();
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
let entity: Entity = cmd.push((
|
||||||
|
Position {
|
||||||
|
x: 0.0,
|
||||||
|
y: 20.0,
|
||||||
|
z: 0.0,
|
||||||
|
rot: Euler {
|
||||||
|
x: Deg(25.0),
|
||||||
|
y: Deg(45.0),
|
||||||
|
z: Deg(15.0),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
gpu_mesh_buffer,
|
||||||
|
Physics {
|
||||||
|
rigid_body: dynamic_body,
|
||||||
|
rigid_body_handle: None,
|
||||||
|
},
|
||||||
|
Collider {
|
||||||
|
collider: collider,
|
||||||
|
collider_handle: None,
|
||||||
|
},
|
||||||
|
));
|
||||||
|
}
|
||||||
|
_ => {},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue