adding texturing, getting sidetracked by config loading

This commit is contained in:
2021-02-20 20:56:20 -08:00
parent 2b77153f10
commit da2c6a436a
7 changed files with 214 additions and 144 deletions

View File

@@ -1,14 +1,16 @@
use wgpu::{TextureView, Buffer, BindGroup};
use std::sync::Arc;
use rapier3d::dynamics::{RigidBody, RigidBodyHandle};
use rapier3d::geometry::ColliderHandle;
use rapier3d::geometry::Collider as r3dCollider;
use std::time::{Duration, Instant};
use cgmath::Deg;
use cgmath::{Deg, Euler};
use rapier3d::dynamics::{RigidBody, RigidBodyHandle};
use rapier3d::geometry::Collider as r3dCollider;
use rapier3d::geometry::ColliderHandle;
use wgpu::{BindGroup, Buffer, TextureView};
use crate::runtime::state::{TomlPositionDescription, TomlRotationDescription};
// a component is any type that is 'static, sized, send and sync
pub struct ImguiWindow<'a> {
pub window: imgui::Window<'a>,
}
@@ -28,6 +30,47 @@ pub struct Position {
pub rot: cgmath::Euler<Deg<f32>>,
}
impl From<TomlPositionDescription> for Position {
fn from(pos: TomlPositionDescription) -> Self {
let euler = match pos.rot {
None => Euler {
x: Deg(0.0),
y: Deg(0.0),
z: Deg(0.0),
},
Some(v) => Euler {
x: Deg(v.x),
y: Deg(v.y),
z: Deg(v.z),
},
};
Position {
x: pos.x,
y: pos.y,
z: pos.z,
rot: euler,
}
}
}
impl From<Option<TomlPositionDescription>> for Position {
fn from(pos: Option<TomlPositionDescription>) -> Self {
match pos {
None => Position {
x: 0.0,
y: 0.0,
z: 0.0,
rot: Euler {
x: Deg(0.0),
y: Deg(0.0),
z: Deg(0.0),
},
},
Some(v) => Position::from(v),
}
}
}
#[derive(Clone, Default, PartialEq, Eq, Hash, Copy, Debug)]
pub struct RangeCopy<Idx> {
pub start: Idx,
@@ -55,4 +98,4 @@ pub struct Physics {
pub struct Collider {
pub collider: r3dCollider,
pub collider_handle: Option<ColliderHandle>,
}
}

View File

@@ -92,12 +92,22 @@ https://github.com/amethyst/legion
ECS
animation
config / save loading (sorta!)
render 3d (good!)
input/io (yep!)
collision / physics (yep!)
entities & behaviours (got the entities!)
scripting!
Todo:
Load scene imgui interface w/ toml files
FPS graph port from voxel raycaster
better imgui interface with components & systems
Figure out eventing, GameInput, passing all events, etc.
+ texturing
*/
//log::info!("");
@@ -112,14 +122,6 @@ fn main() {
let logger = env_logger::builder().filter(Some("minimal_viable_game_engine"), LevelFilter::Info).init();
// for i in settings.get("entities") {
//
// }
let mut world = World::default();
let mut imgui_prepare_schedule = Schedule::builder()

View File

@@ -17,11 +17,55 @@ use crate::components::{Collider, LoopState, Mesh, Physics, Position};
use crate::geometry::{load_obj, RawMesh};
use std::io::Read;
pub struct EntityMeta {
#[derive(Deserialize, Clone)]
pub struct TomlBallPhysicsBodyDescription {
pub radius: String
}
#[derive(Deserialize, Clone)]
pub struct TomlCuboidPhysicsBodyDescription {
pub x: f32,
pub y: f32,
pub z: f32,
}
#[derive(Deserialize, Clone)]
pub struct TomlPhysicsDescription {
pub body_status: String,
pub ball: Option<TomlBallPhysicsBodyDescription>,
pub cuboid: Option<TomlCuboidPhysicsBodyDescription>,
}
#[derive(Deserialize, Clone)]
pub struct TomlRotationDescription {
pub x: f32,
pub y: f32,
pub z: f32,
}
#[derive(Deserialize, Clone)]
pub struct TomlPositionDescription {
pub x: f32,
pub y: f32,
pub z: f32,
pub rot: Option<TomlRotationDescription>,
}
#[derive(Deserialize, Clone)]
pub struct TomlEntityDescription {
pub name: String,
pub ent_type: String,
#[serde(rename = "type")]
pub type_name: String,
pub mesh: Option<String>,
pub position: Option<Position>
pub position: Option<TomlPositionDescription>,
pub physics: Option<TomlPhysicsDescription>,
}
#[derive(Deserialize, Clone)]
pub struct TomlEntityContainer {
pub entities: Vec<TomlEntityDescription>,
}
pub struct RuntimeState {
@@ -29,38 +73,15 @@ pub struct RuntimeState {
mesh_cache: HashMap<String, RawMesh>,
}
#[derive(Deserialize)]
pub struct TomlPositionDescription {
pub x: f32,
pub y: f32,
pub z: f32,
}
#[derive(Deserialize)]
pub struct TomlEntityDescription {
pub name: String,
#[serde(rename = "type")]
pub type_name: String,
pub mesh: Option<String>,
pub position: Option<TomlPositionDescription>,
}
#[derive(Deserialize)]
pub struct TomlEntityContainer {
pub entities: Vec<TomlEntityDescription>,
}
impl RuntimeState {
pub fn new() -> RuntimeState {
// TODO: Hook this file to the gui
let mut file = fs::File::open("./conf/entity_spawns.toml").unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
// TODO: gracefully fail
let mut settings : TomlEntityContainer = toml::from_str(content.as_str()).unwrap();
// settings
// // File::with_name(..) is shorthand for File::from(Path::new(..))
// .merge(File::with_name("conf/entity_spawns.toml"))
// .unwrap();
RuntimeState {
config_db: settings,
@@ -72,40 +93,8 @@ impl RuntimeState {
self.mesh_cache.get(mesh)
}
pub fn get_configured_entities(&mut self) -> Vec<EntityMeta> {
let mut out = Vec::new();
for entity in &self.config_db.entities {
let position = match &entity.position {
None => { None }
Some(pos) => {
Some(Position {
x: pos.x,
y: pos.y,
z: pos.z,
rot: Euler {
x: Deg(0.0),
y: Deg(0.0),
z: Deg(0.0)
}
})
}
};
// log::info!("{:?}", position);
// log::info!("{:?}", entity);
// log::info!("{:?}", entity.into_table());
// log::info!("{:?}", entity.into_array());
out.push(EntityMeta {
name: entity.name.clone(),
ent_type: entity.type_name.clone(),
mesh: entity.mesh.clone(),
position: position
});
}
out
pub fn get_entities(&mut self) -> Vec<TomlEntityDescription> {
self.config_db.entities.clone()
}
pub fn preload_meshes(&mut self, resources_path: PathBuf) {

View File

@@ -9,7 +9,9 @@ use legion::world::SubWorld;
use legion::IntoQuery;
use legion::*;
use nalgebra::Quaternion as naQuaternion;
use rapier3d::dynamics::{IntegrationParameters, JointSet, RigidBodyBuilder, RigidBodySet, MassProperties};
use rapier3d::dynamics::{
IntegrationParameters, JointSet, MassProperties, RigidBodyBuilder, RigidBodySet,
};
use rapier3d::geometry::{BroadPhase, ColliderBuilder, ColliderSet, NarrowPhase};
use rapier3d::na::{Isometry3, Vector, Vector3};
use rapier3d::pipeline::{ChannelEventCollector, PhysicsPipeline};
@@ -35,10 +37,10 @@ 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() {
for entity in &runtime_state.get_entities() {
match entity.type_name.as_ref() {
"PhysicsEntity" => {
let mesh_name = entity.mesh.unwrap();
let mesh_name = entity.mesh.as_ref().unwrap();
let raw_mesh = match runtime_state.get_mesh(mesh_name.as_str()) {
None => {
log::warn!("Skipping entity with invalid mesh file {:?} ", mesh_name);
@@ -47,14 +49,13 @@ pub fn runtime_spawn(
Some(mesh) => mesh,
};
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 collider = ColliderBuilder::capsule_y(2.0, 1.0).build();
let gpu_mesh_buffer = renderer
.upload_mesh_to_buffer(
@@ -68,19 +69,7 @@ pub fn runtime_spawn(
)
.unwrap();
let position = match entity.position {
None => Position {
x: 0.0,
y: rand::random::<f32>(),
z: 0.0,
rot: Euler {
x: Deg(0.0),
y: Deg(0.0),
z: Deg(0.0),
},
},
Some(position) => position
};
let position = Position::from(entity.position.clone());
let mut dynamic_body = RigidBodyBuilder::new_dynamic()
.can_sleep(false)
@@ -102,7 +91,7 @@ pub fn runtime_spawn(
));
}
"Terrain" => {
let mesh_name = entity.mesh.unwrap();
let mesh_name = entity.mesh.clone().unwrap();
let raw_mesh = match runtime_state.get_mesh(mesh_name.as_str()) {
None => {
log::warn!("Skipping entity with invalid mesh file {:?} ", mesh_name);
@@ -111,8 +100,13 @@ pub fn runtime_spawn(
Some(mesh) => mesh,
};
let position = Position::from(entity.position.clone());
let mut static_body = RigidBodyBuilder::new_static()
.position(Isometry3::new(Vector3::new(0.0, -8.0, 0.0), Vector::y()))
.position(Isometry3::new(
Vector3::new(position.x, position.y, position.z),
Vector::y(),
))
.build();
let mesh_collider = ColliderBuilder::trimesh(
@@ -133,21 +127,6 @@ pub fn runtime_spawn(
)
.unwrap();
let position = match entity.position {
None => Position {
x: 0.0,
y: 0.0,
z: 0.0,
rot: Euler {
x: Deg(0.0),
y: Deg(0.0),
z: Deg(0.0),
},
},
Some(position) => position
};
let entity: Entity = cmd.push((
position,
gpu_mesh_buffer,
@@ -162,21 +141,22 @@ pub fn runtime_spawn(
));
}
"Camera" => {
let position = Position::from(entity.position.clone());
let entity: Entity = cmd.push((
Camera {
position: cgmath::Point3 {
x: 0.0,
y: 0.0,
z: 10.0,
x: position.x,
y: position.y,
z: position.z,
},
yaw: Rad(-PI),
pitch: Rad(PI / 2.0),
},
CameraController::new(3.0, 1.0),
CameraController::new(5.0, 1.0),
));
}
"Light" => {
let mesh_name = entity.mesh.unwrap();
let mesh_name = entity.mesh.clone().unwrap();
let raw_mesh = match runtime_state.get_mesh(mesh_name.as_str()) {
None => {
log::warn!("Skipping entity with invalid mesh file {:?} ", mesh_name);
@@ -185,19 +165,7 @@ pub fn runtime_spawn(
Some(mesh) => mesh,
};
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 position = Position::from(entity.position.clone());
let gpu_mesh_buffer = renderer
.upload_mesh_to_buffer(
@@ -211,11 +179,8 @@ pub fn runtime_spawn(
)
.unwrap();
let light_entity: Entity = cmd.push((
position,
gpu_mesh_buffer,
renderer.create_light(),
));
let light_entity: Entity =
cmd.push((position, gpu_mesh_buffer, renderer.create_light()));
}
_ => {}