Compare commits
No commits in common. "cf3f70dbb99bf305362723329c03ea88f8f6b0cd" and "35815fa0197363daddd507c2b1ccb921e9c430d6" have entirely different histories.
cf3f70dbb9
...
35815fa019
|
@ -1845,9 +1845,7 @@ dependencies = [
|
|||
"gltf",
|
||||
"image",
|
||||
"infer",
|
||||
"instant",
|
||||
"lyra-ecs",
|
||||
"lyra-math",
|
||||
"mime",
|
||||
"notify",
|
||||
"notify-debouncer-full",
|
||||
|
@ -3103,6 +3101,7 @@ dependencies = [
|
|||
"async-std",
|
||||
"fps_counter",
|
||||
"lyra-engine",
|
||||
"lyra-scripting",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
|
|
|
@ -6,9 +6,8 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
lyra-engine = { path = "../../", version = "0.0.1" }
|
||||
#lyra-engine = { path = "../../", version = "0.0.1", features = ["lua_scripting"] }
|
||||
#lyra-scripting = { path = "../../lyra-scripting", features = ["lua", "teal"] }
|
||||
lyra-engine = { path = "../../", version = "0.0.1", features = ["lua_scripting"] }
|
||||
lyra-scripting = { path = "../../lyra-scripting", features = ["lua", "teal"] }
|
||||
#lyra-ecs = { path = "../../lyra-ecs"}
|
||||
anyhow = "1.0.75"
|
||||
async-std = "1.12.0"
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -1,2 +0,0 @@
|
|||
Sponza*
|
||||
Textures
|
|
@ -1 +0,0 @@
|
|||
To keep the size of this repository down, the Sponza scene is omitted from this repo. The files were downloaded from https://github.com/toji/sponza-optimized
|
|
@ -1,7 +1,7 @@
|
|||
use std::ptr::NonNull;
|
||||
|
||||
use lyra_engine::{assets::gltf::Gltf, ecs::{system::{BatchedSystem, Criteria, CriteriaSchedule, IntoSystem}, Component, World}, game::Game, input::{Action, ActionHandler, ActionKind, ActionMapping, ActionMappingId, ActionSource, InputActionPlugin, KeyCode, LayoutId, MouseAxis, MouseInput}, math::{self, Transform, Vec3}, render::light::{directional::DirectionalLight, PointLight, SpotLight}, scene::{CameraComponent, MeshComponent}, DeltaTime};
|
||||
use lyra_engine::assets::ResourceManager;
|
||||
use lyra_engine::{math::{self, Vec3}, math::Transform, input::{KeyCode, ActionHandler, Action, ActionKind, LayoutId, ActionMapping, ActionSource, ActionMappingId, InputActionPlugin, MouseInput, MouseAxis, CommonActionLabel}, game::Game, render::{window::{CursorGrabMode, WindowOptions}, light::{PointLight, directional::DirectionalLight, SpotLight}}, change_tracker::Ct, ecs::{system::{Criteria, CriteriaSchedule, BatchedSystem, IntoSystem}, World, Component}, DeltaTime, scene::{ModelComponent, CameraComponent}, lua::{LuaScriptingPlugin, LuaScript}, Script, ScriptList};
|
||||
use lyra_engine::assets::{ResourceManager, Model};
|
||||
|
||||
mod free_fly_camera;
|
||||
use free_fly_camera::{FreeFlyCameraPlugin, FreeFlyCamera};
|
||||
|
@ -16,13 +16,6 @@ pub enum ActionLabel {
|
|||
LookRoll,
|
||||
}
|
||||
|
||||
const ACTLBL_MOVE_UP_DOWN: &str = "MoveUpDown";
|
||||
const ACTLBL_MOVE_LEFT_RIGHT: &str = "MoveLeftRight";
|
||||
const ACTLBL_MOVE_FORWARD_BACKWARD: &str = "MoveForwardBackward";
|
||||
const ACTLBL_LOOK_LEFT_RIGHT: &str = "LookLeftRight";
|
||||
const ACTLBL_LOOK_UP_DOWN: &str = "LookUpDown";
|
||||
const ACTLBL_LOOK_ROLL: &str = "LookRoll";
|
||||
|
||||
struct FixedTimestep {
|
||||
max_tps: u32,
|
||||
fixed_time: f32,
|
||||
|
@ -93,45 +86,30 @@ async fn main() {
|
|||
//let diffuse_texture = resman.request::<Texture>("assets/happy-tree.png").unwrap();
|
||||
//let antique_camera_model = resman.request::<Model>("assets/AntiqueCamera.glb").unwrap();
|
||||
//let cube_model = resman.request::<Model>("assets/cube-texture-bin.glb").unwrap();
|
||||
/* let cube_gltf = resman.request::<Gltf>("assets/texture-sep/texture-sep.gltf").unwrap();
|
||||
let crate_gltf = resman.request::<Gltf>("assets/crate/crate.gltf").unwrap();
|
||||
|
||||
let separate_gltf = resman.request::<Gltf>("assets/pos-testing/child-node-cubes.glb").unwrap(); */
|
||||
//drop(resman);
|
||||
|
||||
/* let cube_mesh = &cube_gltf.data_ref()
|
||||
.unwrap().meshes[0];
|
||||
let crate_mesh = &crate_gltf.data_ref()
|
||||
.unwrap().meshes[0];
|
||||
|
||||
let separate_scene = &separate_gltf.data_ref()
|
||||
.unwrap().scenes[0]; */
|
||||
|
||||
let sponza_model = resman.request::<Gltf>("assets/sponza/Sponza.gltf").unwrap();
|
||||
let cube_model = resman.request::<Model>("assets/texture-sep/texture-sep.gltf").unwrap();
|
||||
let crate_model = resman.request::<Model>("assets/crate/crate.gltf").unwrap();
|
||||
//let sponza_model = resman.request::<Model>("assets/sponza/Sponza.gltf").unwrap();
|
||||
drop(resman);
|
||||
|
||||
let sponza_scene = &sponza_model.data_ref()
|
||||
.unwrap().scenes[0];
|
||||
|
||||
world.spawn((
|
||||
sponza_scene.clone(),
|
||||
Transform::from_xyz(0.0, 0.0, 0.0),
|
||||
));
|
||||
|
||||
/* world.spawn((
|
||||
separate_scene.clone(),
|
||||
ModelComponent(antique_camera_model),
|
||||
Transform::from_xyz(0.0, -5.0, -10.0),
|
||||
)); */
|
||||
|
||||
/* {
|
||||
let cube_tran = Transform::from_xyz(-5.9026427, -1.8953488, -10.0);
|
||||
/* world.spawn((
|
||||
ModelComponent(sponza_model),
|
||||
Transform::from_xyz(0.0, 0.0, 0.0),
|
||||
)); */
|
||||
|
||||
{
|
||||
let cube_tran = Transform::from_xyz(-3.5, 0.0, -8.0);
|
||||
//cube_tran.rotate_y(math::Angle::Degrees(180.0));
|
||||
world.spawn((
|
||||
cube_tran,
|
||||
crate_mesh.clone(),
|
||||
ModelComponent(crate_model.clone()),
|
||||
CubeFlag,
|
||||
));
|
||||
} */
|
||||
}
|
||||
|
||||
{
|
||||
let mut light_tran = Transform::from_xyz(1.5, 2.5, 0.0);
|
||||
|
@ -146,11 +124,11 @@ async fn main() {
|
|||
specular: 1.3,
|
||||
},
|
||||
light_tran,
|
||||
//cube_mesh.clone(),
|
||||
ModelComponent(cube_model.clone()),
|
||||
));
|
||||
}
|
||||
|
||||
/* {
|
||||
{
|
||||
let mut light_tran = Transform::from_xyz(-3.5, 0.2, -4.5);
|
||||
light_tran.scale = Vec3::new(0.5, 0.5, 0.5);
|
||||
world.spawn((
|
||||
|
@ -168,31 +146,9 @@ async fn main() {
|
|||
specular: 1.0,
|
||||
},
|
||||
Transform::from(light_tran),
|
||||
cube_mesh.clone(),
|
||||
ModelComponent(cube_model.clone()),
|
||||
));
|
||||
}
|
||||
|
||||
{
|
||||
let mut light_tran = Transform::from_xyz(2.0, 2.5, -9.5);
|
||||
light_tran.scale = Vec3::new(0.5, 0.5, 0.5);
|
||||
world.spawn((
|
||||
PointLight {
|
||||
color: Vec3::new(0.0, 0.0, 1.0),
|
||||
|
||||
intensity: 3.3,
|
||||
|
||||
constant: 1.0,
|
||||
linear: 0.09,
|
||||
quadratic: 0.032,
|
||||
|
||||
ambient: 0.2,
|
||||
diffuse: 1.0,
|
||||
specular: 1.3,
|
||||
},
|
||||
Transform::from(light_tran),
|
||||
cube_mesh.clone(),
|
||||
));
|
||||
} */
|
||||
|
||||
/* {
|
||||
let mut light_tran = Transform::from_xyz(-5.0, 2.5, -9.5);
|
||||
|
@ -216,6 +172,28 @@ async fn main() {
|
|||
));
|
||||
} */
|
||||
|
||||
{
|
||||
let mut light_tran = Transform::from_xyz(2.0, 2.5, -9.5);
|
||||
light_tran.scale = Vec3::new(0.5, 0.5, 0.5);
|
||||
world.spawn((
|
||||
PointLight {
|
||||
color: Vec3::new(0.0, 0.0, 1.0),
|
||||
|
||||
intensity: 3.3,
|
||||
|
||||
constant: 1.0,
|
||||
linear: 0.09,
|
||||
quadratic: 0.032,
|
||||
|
||||
ambient: 0.2,
|
||||
diffuse: 1.0,
|
||||
specular: 1.3,
|
||||
},
|
||||
Transform::from(light_tran),
|
||||
ModelComponent(cube_model),
|
||||
));
|
||||
}
|
||||
|
||||
let mut camera = CameraComponent::new_3d();
|
||||
camera.transform.translation += math::Vec3::new(0.0, 0.0, 5.5);
|
||||
world.spawn(( camera, FreeFlyCamera::default() ));
|
||||
|
@ -233,7 +211,7 @@ async fn main() {
|
|||
|
||||
Ok(())
|
||||
};
|
||||
let _fps_plugin = move |game: &mut Game| {
|
||||
let fps_plugin = move |game: &mut Game| {
|
||||
let world = game.world_mut();
|
||||
world.add_resource(fps_counter::FPSCounter::new());
|
||||
};
|
||||
|
@ -268,54 +246,54 @@ async fn main() {
|
|||
};
|
||||
|
||||
let action_handler_plugin = |game: &mut Game| {
|
||||
let action_handler = ActionHandler::builder()
|
||||
/* let action_handler = ActionHandler::builder()
|
||||
.add_layout(LayoutId::from(0))
|
||||
|
||||
.add_action(ACTLBL_MOVE_FORWARD_BACKWARD, Action::new(ActionKind::Axis))
|
||||
.add_action(ACTLBL_MOVE_LEFT_RIGHT, Action::new(ActionKind::Axis))
|
||||
.add_action(ACTLBL_MOVE_UP_DOWN, Action::new(ActionKind::Axis))
|
||||
.add_action(ACTLBL_LOOK_LEFT_RIGHT, Action::new(ActionKind::Axis))
|
||||
.add_action(ACTLBL_LOOK_UP_DOWN, Action::new(ActionKind::Axis))
|
||||
.add_action(ACTLBL_LOOK_ROLL, Action::new(ActionKind::Axis))
|
||||
.add_action(CommonActionLabel::MoveForwardBackward, Action::new(ActionKind::Axis))
|
||||
.add_action(CommonActionLabel::MoveLeftRight, Action::new(ActionKind::Axis))
|
||||
.add_action(CommonActionLabel::MoveUpDown, Action::new(ActionKind::Axis))
|
||||
.add_action(CommonActionLabel::LookLeftRight, Action::new(ActionKind::Axis))
|
||||
.add_action(CommonActionLabel::LookUpDown, Action::new(ActionKind::Axis))
|
||||
.add_action(CommonActionLabel::LookRoll, Action::new(ActionKind::Axis))
|
||||
|
||||
.add_mapping(ActionMapping::builder(LayoutId::from(0), ActionMappingId::from(0))
|
||||
.bind(ACTLBL_MOVE_FORWARD_BACKWARD, &[
|
||||
.bind(CommonActionLabel::MoveForwardBackward, &[
|
||||
ActionSource::Keyboard(KeyCode::W).into_binding_modifier(1.0),
|
||||
ActionSource::Keyboard(KeyCode::S).into_binding_modifier(-1.0)
|
||||
])
|
||||
.bind(ACTLBL_MOVE_LEFT_RIGHT, &[
|
||||
.bind(CommonActionLabel::MoveLeftRight, &[
|
||||
ActionSource::Keyboard(KeyCode::A).into_binding_modifier(-1.0),
|
||||
ActionSource::Keyboard(KeyCode::D).into_binding_modifier(1.0)
|
||||
])
|
||||
.bind(ACTLBL_MOVE_UP_DOWN, &[
|
||||
.bind(CommonActionLabel::MoveUpDown, &[
|
||||
ActionSource::Keyboard(KeyCode::C).into_binding_modifier(1.0),
|
||||
ActionSource::Keyboard(KeyCode::Z).into_binding_modifier(-1.0)
|
||||
])
|
||||
.bind(ACTLBL_LOOK_LEFT_RIGHT, &[
|
||||
.bind(CommonActionLabel::LookLeftRight, &[
|
||||
ActionSource::Mouse(MouseInput::Axis(MouseAxis::X)).into_binding(),
|
||||
ActionSource::Keyboard(KeyCode::Left).into_binding_modifier(-1.0),
|
||||
ActionSource::Keyboard(KeyCode::Right).into_binding_modifier(1.0),
|
||||
//ActionSource::Gamepad(GamepadFormat::DualAxis, GamepadInput::Axis(GamepadAxis::RThumbstickX)).into_binding(),
|
||||
])
|
||||
.bind(ACTLBL_LOOK_UP_DOWN, &[
|
||||
.bind(CommonActionLabel::LookUpDown, &[
|
||||
ActionSource::Mouse(MouseInput::Axis(MouseAxis::Y)).into_binding(),
|
||||
ActionSource::Keyboard(KeyCode::Up).into_binding_modifier(-1.0),
|
||||
ActionSource::Keyboard(KeyCode::Down).into_binding_modifier(1.0),
|
||||
//ActionSource::Gamepad(GamepadFormat::DualAxis, GamepadInput::Axis(GamepadAxis::RThumbstickY)).into_binding(),
|
||||
])
|
||||
.bind(ACTLBL_LOOK_ROLL, &[
|
||||
.bind(CommonActionLabel::LookRoll, &[
|
||||
ActionSource::Keyboard(KeyCode::E).into_binding_modifier(-1.0),
|
||||
ActionSource::Keyboard(KeyCode::Q).into_binding_modifier(1.0),
|
||||
])
|
||||
.finish()
|
||||
).finish();
|
||||
);
|
||||
|
||||
let world = game.world_mut();
|
||||
world.add_resource(action_handler);
|
||||
world.add_resource(action_handler); */
|
||||
game.with_plugin(InputActionPlugin);
|
||||
};
|
||||
|
||||
/* let script_test_plugin = |game: &mut Game| {
|
||||
let script_test_plugin = |game: &mut Game| {
|
||||
game.with_plugin(LuaScriptingPlugin);
|
||||
|
||||
let world = game.world_mut();
|
||||
|
@ -328,13 +306,13 @@ async fn main() {
|
|||
let scripts = ScriptList::new(vec![script]);
|
||||
world.spawn((scripts,));
|
||||
|
||||
}; */
|
||||
};
|
||||
|
||||
Game::initialize().await
|
||||
.with_plugin(lyra_engine::DefaultPlugins)
|
||||
.with_startup_system(setup_sys.into_system())
|
||||
.with_plugin(action_handler_plugin)
|
||||
//.with_plugin(script_test_plugin)
|
||||
.with_plugin(script_test_plugin)
|
||||
//.with_plugin(fps_plugin)
|
||||
.with_plugin(jiggle_plugin)
|
||||
.with_plugin(FreeFlyCameraPlugin)
|
||||
|
|
|
@ -177,10 +177,10 @@ where
|
|||
unsafe fn fetch<'a>(&self, _world: &'a World, archetype: &'a crate::archetype::Archetype, tick: crate::Tick) -> Self::Fetch<'a> {
|
||||
let col = archetype.get_column(self.type_id)
|
||||
.expect("You ignored 'can_visit_archetype'!");
|
||||
|
||||
let col = NonNull::from(col);
|
||||
|
||||
// TODO: find a way to get the component column mutable with a borrowed archetype so its tick can be updated.
|
||||
// the fetcher needs to tick the entities tick in the archetype
|
||||
let col = NonNull::from(col);
|
||||
|
||||
FetchBorrowMut {
|
||||
col,
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
use std::marker::PhantomData;
|
||||
|
||||
use crate::{query::{AsQuery, Query}, Archetype, Component, DynTypeId, World};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Has<C: Component> {
|
||||
_marker: PhantomData<C>
|
||||
}
|
||||
|
||||
impl<C: Component> Copy for Has<C> {}
|
||||
|
||||
impl<C: Component> Clone for Has<C> {
|
||||
fn clone(&self) -> Self {
|
||||
Self { _marker: self._marker.clone() }
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: Component> Query for Has<C> {
|
||||
type Item<'a> = ();
|
||||
|
||||
type Fetch<'a> = ();
|
||||
|
||||
fn new() -> Self {
|
||||
Has {
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
|
||||
fn can_visit_archetype(&self, archetype: &Archetype) -> bool {
|
||||
archetype.has_column(DynTypeId::of::<C>())
|
||||
}
|
||||
|
||||
unsafe fn fetch<'a>(&self, _world: &'a World, _: &'a Archetype, _: crate::Tick) -> Self::Fetch<'a> {
|
||||
()
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: Component> AsQuery for Has<C> {
|
||||
type Query = Self;
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
pub mod has_component;
|
||||
pub use has_component::*;
|
||||
|
||||
pub mod or;
|
||||
pub use or::*;
|
|
@ -1,40 +0,0 @@
|
|||
use crate::{query::{AsQuery, Query}, Archetype, World};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Or<Q1: AsQuery, Q2: AsQuery> {
|
||||
left: Q1::Query,
|
||||
right: Q2::Query,
|
||||
}
|
||||
|
||||
impl<Q1: AsQuery, Q2: AsQuery> Copy for Or<Q1, Q2> {}
|
||||
|
||||
impl<Q1: AsQuery, Q2: AsQuery> Clone for Or<Q1, Q2> {
|
||||
fn clone(&self) -> Self {
|
||||
Self { left: self.left.clone(), right: self.right.clone() }
|
||||
}
|
||||
}
|
||||
|
||||
impl<Q1: AsQuery, Q2: AsQuery> Query for Or<Q1, Q2> {
|
||||
type Item<'a> = ();
|
||||
|
||||
type Fetch<'a> = ();
|
||||
|
||||
fn new() -> Self {
|
||||
Or {
|
||||
left: Q1::Query::new(),
|
||||
right: Q2::Query::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn can_visit_archetype(&self, archetype: &Archetype) -> bool {
|
||||
self.left.can_visit_archetype(archetype) || self.right.can_visit_archetype(archetype)
|
||||
}
|
||||
|
||||
unsafe fn fetch<'a>(&self, _world: &'a World, _: &'a Archetype, _: crate::Tick) -> Self::Fetch<'a> {
|
||||
()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Q1: AsQuery, Q2: AsQuery> AsQuery for Or<Q1, Q2> {
|
||||
type Query = Self;
|
||||
}
|
|
@ -29,8 +29,6 @@ pub use world::*;
|
|||
|
||||
pub mod dynamic;
|
||||
|
||||
pub mod filter;
|
||||
|
||||
/// A [`Fetch`]er implementation gets data out of an archetype.
|
||||
pub trait Fetch<'a> {
|
||||
/// The type that this Fetch yields
|
||||
|
|
|
@ -90,7 +90,7 @@ where
|
|||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
loop {
|
||||
if Q::ALWAYS_FETCHES {
|
||||
if Q::ALWAYS_FETCHES && F::ALWAYS_FETCHES {
|
||||
// only fetch this query once.
|
||||
// fetcher gets set to Some after this `next` call.
|
||||
if self.fetcher.is_none() {
|
||||
|
|
|
@ -254,13 +254,6 @@ impl World {
|
|||
v.into_iter()
|
||||
}
|
||||
|
||||
/// View into the world for a set of entities that satisfy the queries.
|
||||
pub fn filtered_view_iter<Q: AsQuery, F: AsQuery>(&self) -> ViewIter<Q::Query, F::Query> {
|
||||
let archetypes = self.archetypes.values().collect();
|
||||
let v = ViewState::new(self, Q::Query::new(), F::Query::new(), archetypes);
|
||||
v.into_iter()
|
||||
}
|
||||
|
||||
pub fn dynamic_view(&self) -> DynamicView {
|
||||
DynamicView::new(self)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use lyra_resource::{ResHandle, Texture};
|
||||
|
||||
use super::texture::RenderTexture;
|
||||
|
||||
pub struct MaterialSpecular {
|
||||
|
@ -11,18 +9,13 @@ pub struct MaterialSpecular {
|
|||
pub color_texture: Option<RenderTexture>,
|
||||
}
|
||||
|
||||
fn texture_to_render(device: &wgpu::Device, queue: &wgpu::Queue, bg_layout: &Arc<wgpu::BindGroupLayout>, i: &Option<ResHandle<Texture>>) -> Option<RenderTexture> {
|
||||
if let Some(tex) = i {
|
||||
RenderTexture::from_resource(device, queue, bg_layout.clone(), tex, None).ok()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl MaterialSpecular {
|
||||
pub fn from_resource(device: &wgpu::Device, queue: &wgpu::Queue, bg_layout: Arc<wgpu::BindGroupLayout>, value: &lyra_resource::gltf::Specular) -> Self {
|
||||
let tex = texture_to_render(device, queue, &bg_layout, &value.texture);
|
||||
let color_tex = texture_to_render(device, queue, &bg_layout, &value.color_texture);
|
||||
pub fn from_resource(device: &wgpu::Device, queue: &wgpu::Queue, bg_layout: Arc<wgpu::BindGroupLayout>, value: &lyra_resource::Specular) -> Self {
|
||||
let tex = value.texture.as_ref().map(|t| t.data_ref())
|
||||
.map(|i| RenderTexture::from_image(device, queue, bg_layout.clone(), &i.image, None).unwrap());
|
||||
|
||||
let color_tex = value.color_texture.as_ref().map(|t| t.data_ref())
|
||||
.map(|i| RenderTexture::from_image(device, queue, bg_layout, &i.image, None).unwrap());
|
||||
|
||||
Self {
|
||||
factor: value.factor,
|
||||
|
@ -45,8 +38,9 @@ pub struct Material {
|
|||
}
|
||||
|
||||
impl Material {
|
||||
pub fn from_resource(device: &wgpu::Device, queue: &wgpu::Queue, bg_layout: Arc<wgpu::BindGroupLayout>, value: &lyra_resource::gltf::Material) -> Self {
|
||||
let diffuse_texture = texture_to_render(device, queue, &bg_layout, &value.base_color_texture);
|
||||
pub fn from_resource(device: &wgpu::Device, queue: &wgpu::Queue, bg_layout: Arc<wgpu::BindGroupLayout>, value: &lyra_resource::Material) -> Self {
|
||||
let diffuse_texture = value.base_color_texture.as_ref().map(|t| t.data_ref())
|
||||
.map(|i| RenderTexture::from_image(device, queue, bg_layout.clone(), &i.image, None).unwrap());
|
||||
|
||||
let specular = value.specular.as_ref().map(|s| MaterialSpecular::from_resource(device, queue, bg_layout.clone(), s));
|
||||
|
||||
|
|
|
@ -1,17 +1,21 @@
|
|||
use lyra_ecs::Entity;
|
||||
|
||||
use crate::math::Transform;
|
||||
|
||||
pub struct RenderJob {
|
||||
pub entity: Entity,
|
||||
pub shader_id: u64,
|
||||
pub mesh_uuid: uuid::Uuid,
|
||||
pub mesh_buffer_id: uuid::Uuid,
|
||||
pub transform: Transform,
|
||||
}
|
||||
|
||||
impl RenderJob {
|
||||
pub fn new(entity: Entity, shader_id: u64, mesh_buffer_id: uuid::Uuid) -> Self {
|
||||
pub fn new(entity: Entity, shader_id: u64, mesh_buffer_id: uuid::Uuid, transform: Transform) -> Self {
|
||||
Self {
|
||||
entity,
|
||||
shader_id,
|
||||
mesh_uuid: mesh_buffer_id,
|
||||
mesh_buffer_id,
|
||||
transform,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,13 +5,10 @@ use std::borrow::Cow;
|
|||
use glam::Vec3;
|
||||
use instant::Instant;
|
||||
use itertools::izip;
|
||||
use lyra_ecs::query::filter::{Has, Or};
|
||||
use lyra_ecs::{Entity, Tick};
|
||||
use lyra_ecs::Entity;
|
||||
use lyra_ecs::query::{Entities, TickOf};
|
||||
use lyra_ecs::World;
|
||||
use lyra_resource::gltf::GltfScene;
|
||||
use tracing::{debug, warn};
|
||||
use uuid::Uuid;
|
||||
use wgpu::{BindGroupLayout, Limits};
|
||||
use wgpu::util::DeviceExt;
|
||||
use winit::window::Window;
|
||||
|
@ -19,7 +16,7 @@ use winit::window::Window;
|
|||
use crate::math::Transform;
|
||||
use crate::render::material::MaterialUniform;
|
||||
use crate::render::render_buffer::BufferWrapperBuilder;
|
||||
use crate::scene::CameraComponent;
|
||||
use crate::scene::{ModelComponent, CameraComponent};
|
||||
|
||||
use super::camera::{RenderCamera, CameraUniform};
|
||||
use super::desc_buf_lay::DescVertexBufferLayout;
|
||||
|
@ -31,10 +28,7 @@ use super::transform_buffer_storage::TransformBuffers;
|
|||
use super::vertex::Vertex;
|
||||
use super::{render_pipeline::FullRenderPipeline, render_buffer::BufferStorage, render_job::RenderJob};
|
||||
|
||||
use lyra_resource::{gltf::Mesh, ResHandle};
|
||||
|
||||
type MeshHandle = ResHandle<Mesh>;
|
||||
type SceneHandle = ResHandle<GltfScene>;
|
||||
use lyra_resource::Mesh;
|
||||
|
||||
pub trait Renderer {
|
||||
fn prepare(&mut self, main_world: &mut World);
|
||||
|
@ -271,19 +265,16 @@ impl BasicRenderer {
|
|||
s
|
||||
}
|
||||
|
||||
/// Checks if the mesh buffers in the GPU need to be updated.
|
||||
fn check_mesh_buffers(&mut self, _entity: Entity, meshh: &ResHandle<Mesh>) {
|
||||
let mesh_uuid = meshh.uuid();
|
||||
|
||||
if let (Some(mesh), Some(buffers)) = (meshh.data_ref(), self.mesh_buffers.get_mut(&mesh_uuid)) {
|
||||
fn update_mesh_buffers(&mut self, _entity: Entity, mesh: &Mesh) {
|
||||
if let Some(buffers) = self.mesh_buffers.get_mut(&mesh.uuid) {
|
||||
// check if the buffer sizes dont match. If they dont, completely remake the buffers
|
||||
let vertices = mesh.position().unwrap();
|
||||
if buffers.buffer_vertex.count() != vertices.len() {
|
||||
debug!("Recreating buffers for mesh {}", mesh_uuid.to_string());
|
||||
let (vert, idx) = self.create_vertex_index_buffers(&mesh);
|
||||
debug!("Recreating buffers for mesh {}", mesh.uuid.to_string());
|
||||
let (vert, idx) = self.create_vertex_index_buffers(mesh);
|
||||
|
||||
// have to re-get buffers because of borrow checker
|
||||
let buffers = self.mesh_buffers.get_mut(&mesh_uuid).unwrap();
|
||||
let buffers = self.mesh_buffers.get_mut(&mesh.uuid).unwrap();
|
||||
buffers.buffer_indices = idx;
|
||||
buffers.buffer_vertex = vert;
|
||||
|
||||
|
@ -301,8 +292,8 @@ impl BasicRenderer {
|
|||
if let Some(index_buffer) = buffers.buffer_indices.as_ref() {
|
||||
let aligned_indices = match mesh.indices.as_ref().unwrap() {
|
||||
// U16 indices need to be aligned to u32, for wpgu, which are 4-bytes in size.
|
||||
lyra_resource::gltf::MeshIndices::U16(v) => bytemuck::pod_align_to::<u16, u32>(v).1,
|
||||
lyra_resource::gltf::MeshIndices::U32(v) => bytemuck::pod_align_to::<u32, u32>(v).1,
|
||||
lyra_resource::MeshIndices::U16(v) => bytemuck::pod_align_to::<u16, u32>(v).1,
|
||||
lyra_resource::MeshIndices::U32(v) => bytemuck::pod_align_to::<u32, u32>(v).1,
|
||||
};
|
||||
|
||||
let index_buffer = index_buffer.1.buffer();
|
||||
|
@ -336,8 +327,8 @@ impl BasicRenderer {
|
|||
let indices = match mesh.indices.as_ref() {
|
||||
Some(indices) => {
|
||||
let (idx_type, len, contents) = match indices {
|
||||
lyra_resource::gltf::MeshIndices::U16(v) => (wgpu::IndexFormat::Uint16, v.len(), bytemuck::cast_slice(v)),
|
||||
lyra_resource::gltf::MeshIndices::U32(v) => (wgpu::IndexFormat::Uint32, v.len(), bytemuck::cast_slice(v)),
|
||||
lyra_resource::MeshIndices::U16(v) => (wgpu::IndexFormat::Uint16, v.len(), bytemuck::cast_slice(v)),
|
||||
lyra_resource::MeshIndices::U32(v) => (wgpu::IndexFormat::Uint32, v.len(), bytemuck::cast_slice(v)),
|
||||
};
|
||||
|
||||
let index_buffer = self.device.create_buffer_init(
|
||||
|
@ -363,11 +354,7 @@ impl BasicRenderer {
|
|||
fn create_mesh_buffers(&mut self, mesh: &Mesh) -> MeshBufferStorage {
|
||||
let (vertex_buffer, buffer_indices) = self.create_vertex_index_buffers(mesh);
|
||||
|
||||
let material = mesh.material.as_ref()
|
||||
.expect("Material resource not loaded yet")
|
||||
.data_ref()
|
||||
.unwrap();
|
||||
let material = Material::from_resource(&self.device, &self.queue, self.bgl_texture.clone(), &material);
|
||||
let material = Material::from_resource(&self.device, &self.queue, self.bgl_texture.clone(), &mesh.material());
|
||||
let uni = MaterialUniform::from(&material);
|
||||
self.queue.write_buffer(&self.material_buffer.inner_buf, 0, bytemuck::bytes_of(&uni));
|
||||
debug!("Wrote material to buffer");
|
||||
|
@ -380,57 +367,24 @@ impl BasicRenderer {
|
|||
}
|
||||
|
||||
/// Processes the mesh for the renderer, storing and creating buffers as needed. Returns true if a new mesh was processed.
|
||||
fn process_mesh(&mut self, entity: Entity, transform: Transform, mesh: &Mesh, mesh_uuid: Uuid) -> bool {
|
||||
fn process_mesh(&mut self, entity: Entity, transform: Transform, mesh: &Mesh) -> bool {
|
||||
if self.transform_buffers.should_expand() {
|
||||
self.transform_buffers.expand_buffers(&self.device);
|
||||
}
|
||||
|
||||
self.transform_buffers.update_or_insert(&self.queue, &self.render_limits,
|
||||
mesh_uuid, || ( transform.calculate_mat4(), glam::Mat3::from_quat(transform.rotation) ));
|
||||
entity, || ( transform.calculate_mat4(), glam::Mat3::from_quat(transform.rotation) ));
|
||||
|
||||
#[allow(clippy::map_entry)]
|
||||
if !self.mesh_buffers.contains_key(&mesh_uuid) {
|
||||
if !self.mesh_buffers.contains_key(&mesh.uuid) {
|
||||
// create the mesh's buffers
|
||||
let buffers = self.create_mesh_buffers(mesh);
|
||||
self.mesh_buffers.insert(mesh_uuid, buffers);
|
||||
self.entity_meshes.insert(entity, mesh_uuid);
|
||||
self.mesh_buffers.insert(mesh.uuid, buffers);
|
||||
self.entity_meshes.insert(entity, mesh.uuid);
|
||||
|
||||
true
|
||||
} else { false }
|
||||
}
|
||||
|
||||
fn interpolate_transforms(&mut self, now: Instant, last_epoch: Tick, entity: Entity, transform: &Transform, transform_epoch: Tick) -> Transform {
|
||||
let cached = match self.entity_last_transforms.get_mut(&entity) {
|
||||
Some(last) if transform_epoch == last_epoch => {
|
||||
last.from_transform = last.to_transform;
|
||||
last.to_transform = *transform;
|
||||
last.last_updated_at = Some(last.cached_at);
|
||||
last.cached_at = now;
|
||||
|
||||
last.clone()
|
||||
},
|
||||
Some(last) => last.clone(),
|
||||
None => {
|
||||
let cached = CachedTransform {
|
||||
last_updated_at: None,
|
||||
cached_at: now,
|
||||
from_transform: *transform,
|
||||
to_transform: *transform,
|
||||
};
|
||||
self.entity_last_transforms.insert(entity, cached.clone());
|
||||
cached
|
||||
}
|
||||
};
|
||||
|
||||
let fixed_time = match cached.last_updated_at {
|
||||
Some(last_updated_at) => cached.cached_at - last_updated_at,
|
||||
None => now - cached.cached_at
|
||||
}.as_secs_f32();
|
||||
let accumulator = (now - cached.cached_at).as_secs_f32();
|
||||
let alpha = accumulator / fixed_time;
|
||||
|
||||
cached.from_transform.lerp(cached.to_transform, alpha)
|
||||
}
|
||||
}
|
||||
|
||||
impl Renderer for BasicRenderer {
|
||||
|
@ -441,62 +395,57 @@ impl Renderer for BasicRenderer {
|
|||
|
||||
let now_inst = Instant::now();
|
||||
|
||||
let view = main_world.filtered_view_iter::<(Entities, &Transform, TickOf<Transform>), Or<Has<MeshHandle>, Has<SceneHandle>>>();
|
||||
for (entity, transform, transform_epoch) in view {
|
||||
for (entity, model, model_epoch, transform, transform_epoch) in main_world.view_iter::<(Entities, &ModelComponent, TickOf<ModelComponent>, &Transform, TickOf<Transform>)>() {
|
||||
alive_entities.insert(entity);
|
||||
|
||||
let mesh_view = main_world.view_one::<(&MeshHandle, TickOf<MeshHandle>)>(entity);
|
||||
if let Some((mesh_han, mesh_epoch)) = mesh_view.get() {
|
||||
let interop_pos = self.interpolate_transforms(now_inst, last_epoch, entity, &transform, transform_epoch);
|
||||
let cached = match self.entity_last_transforms.get_mut(&entity) {
|
||||
Some(last) if transform_epoch == last_epoch => {
|
||||
last.from_transform = last.to_transform;
|
||||
last.to_transform = *transform;
|
||||
last.last_updated_at = Some(last.cached_at);
|
||||
last.cached_at = now_inst;
|
||||
|
||||
if let Some(mesh) = mesh_han.data_ref() {
|
||||
// if process mesh did not just create a new mesh, and the epoch
|
||||
// shows that the scene has changed, verify that the mesh buffers
|
||||
// dont need to be resent to the gpu.
|
||||
if !self.process_mesh(entity, interop_pos, &*mesh, mesh_han.uuid())
|
||||
&& mesh_epoch == last_epoch {
|
||||
self.check_mesh_buffers(entity, &mesh_han);
|
||||
}
|
||||
|
||||
let material = mesh.material.as_ref().unwrap()
|
||||
.data_ref().unwrap();
|
||||
let shader = material.shader_uuid.unwrap_or(0);
|
||||
let job = RenderJob::new(entity, shader, mesh_han.uuid());
|
||||
self.render_jobs.push_back(job);
|
||||
last.clone()
|
||||
},
|
||||
Some(last) => last.clone(),
|
||||
None => {
|
||||
let cached = CachedTransform {
|
||||
last_updated_at: None,
|
||||
cached_at: now_inst,
|
||||
from_transform: *transform,
|
||||
to_transform: *transform,
|
||||
};
|
||||
self.entity_last_transforms.insert(entity, cached.clone());
|
||||
cached
|
||||
}
|
||||
}
|
||||
};
|
||||
//debug!("Transform: {:?}, comp: {:?}", cached.to_transform.translation, transform.transform.translation);
|
||||
|
||||
let fixed_time = match cached.last_updated_at {
|
||||
Some(last_updated_at) => cached.cached_at - last_updated_at,
|
||||
None => now_inst - cached.cached_at
|
||||
}.as_secs_f32();
|
||||
let accumulator = (now_inst - cached.cached_at).as_secs_f32();
|
||||
let alpha = accumulator / fixed_time;
|
||||
|
||||
let scene_view = main_world.view_one::<(&SceneHandle, TickOf<SceneHandle>)>(entity);
|
||||
if let Some((scene_han, scene_epoch)) = scene_view.get() {
|
||||
// TODO: Rendering scenes
|
||||
let transform_val = cached.from_transform.lerp(cached.to_transform, alpha);
|
||||
|
||||
if let Some(scene) = scene_han.data_ref() {
|
||||
let interpo_pos = self.interpolate_transforms(now_inst, last_epoch, entity, &transform, transform_epoch);
|
||||
let meshes = scene.collect_world_meshes();
|
||||
|
||||
for (mesh_han, mesh_pos) in meshes.into_iter() {
|
||||
if let Some(mesh) = mesh_han.data_ref() {
|
||||
let mesh_interpo = interpo_pos + mesh_pos;
|
||||
|
||||
// if process mesh did not just create a new mesh, and the epoch
|
||||
// shows that the scene has changed, verify that the mesh buffers
|
||||
// dont need to be resent to the gpu.
|
||||
if !self.process_mesh(entity, mesh_interpo, &*mesh, mesh_han.uuid())
|
||||
&& scene_epoch == last_epoch {
|
||||
self.check_mesh_buffers(entity, &mesh_han);
|
||||
}
|
||||
|
||||
let material = mesh.material.as_ref().unwrap()
|
||||
.data_ref().unwrap();
|
||||
let shader = material.shader_uuid.unwrap_or(0);
|
||||
let job = RenderJob::new(entity, shader, mesh_han.uuid());
|
||||
self.render_jobs.push_back(job);
|
||||
}
|
||||
}
|
||||
let model = model.data_ref();
|
||||
for mesh in model.meshes.iter() {
|
||||
if !self.process_mesh(entity, transform_val, mesh) && model_epoch == last_epoch {
|
||||
self.update_mesh_buffers(entity, mesh);
|
||||
}
|
||||
|
||||
let shader = mesh.material().shader_uuid.unwrap_or(0);
|
||||
let job = RenderJob::new(entity, shader, mesh.uuid, transform_val);
|
||||
self.render_jobs.push_back(job);
|
||||
}
|
||||
}
|
||||
|
||||
/* for (entity, mesh, mesh_epoch, transform) in main_world.query::<(Entities, &MeshComponent, EpochOf<MeshComponent>, &TransformComponent)>().iter() {
|
||||
debug!("TODO: Process MeshComponents"); // TODO: Process MeshComponents
|
||||
} */
|
||||
|
||||
// collect dead entities
|
||||
self.transform_buffers.tick();
|
||||
|
||||
|
@ -564,7 +513,7 @@ impl Renderer for BasicRenderer {
|
|||
render_pass.set_pipeline(pipeline.get_wgpu_pipeline());
|
||||
|
||||
// get the mesh (containing vertices) and the buffers from storage
|
||||
let buffers = self.mesh_buffers.get(&job.mesh_uuid).unwrap();
|
||||
let buffers = self.mesh_buffers.get(&job.mesh_buffer_id).unwrap();
|
||||
|
||||
// Bind the optional texture
|
||||
if let Some(tex) = buffers.material.as_ref()
|
||||
|
@ -583,7 +532,7 @@ impl Renderer for BasicRenderer {
|
|||
}
|
||||
|
||||
// Get the bindgroup for job's transform and bind to it using an offset.
|
||||
let transform_indices = *self.transform_buffers.transform_indices(job.mesh_uuid).unwrap();
|
||||
let transform_indices = *self.transform_buffers.entity_indices(job.entity).unwrap();
|
||||
let bindgroup = self.transform_buffers.bind_group(transform_indices).unwrap();
|
||||
let offset = TransformBuffers::index_offset(&self.render_limits, transform_indices) as u32;
|
||||
render_pass.set_bind_group(1, bindgroup, &[ offset, offset, ]);
|
||||
|
|
|
@ -1,10 +1,29 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use image::GenericImageView;
|
||||
use lyra_resource::{FilterMode, ResHandle, Texture, WrappingMode};
|
||||
use lyra_resource::{ResHandle, Texture};
|
||||
|
||||
use super::render_buffer::BindGroupPair;
|
||||
|
||||
/* #[derive(Clone)]
|
||||
pub struct Texture {
|
||||
texture_id: u32,
|
||||
pub img: image::DynamicImage,
|
||||
}
|
||||
|
||||
impl Texture {
|
||||
pub fn from_bytes(bytes: &[u8]) -> anyhow::Result<Self> {
|
||||
let img = image::load_from_memory(bytes)?;
|
||||
|
||||
Ok(Self {
|
||||
texture_id: 0,
|
||||
img,
|
||||
})
|
||||
}
|
||||
} */
|
||||
|
||||
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct RenderTexture {
|
||||
pub inner_texture: wgpu::Texture,
|
||||
|
@ -134,101 +153,8 @@ impl RenderTexture {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn from_resource(device: &wgpu::Device, queue: &wgpu::Queue, bg_layout: Arc<wgpu::BindGroupLayout>, texture_res: &ResHandle<Texture>, label: Option<&str>) -> anyhow::Result<Self> {
|
||||
let texture_ref = texture_res.data_ref().unwrap();
|
||||
let img = texture_ref.image.data_ref().unwrap();
|
||||
|
||||
let rgba = img.to_rgba8();
|
||||
let dimensions = img.dimensions();
|
||||
|
||||
let size = wgpu::Extent3d {
|
||||
width: dimensions.0,
|
||||
height: dimensions.1,
|
||||
depth_or_array_layers: 1,
|
||||
};
|
||||
let texture = device.create_texture(
|
||||
&wgpu::TextureDescriptor {
|
||||
label,
|
||||
size,
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
|
||||
view_formats: &[],
|
||||
}
|
||||
);
|
||||
|
||||
queue.write_texture(
|
||||
wgpu::ImageCopyTexture {
|
||||
aspect: wgpu::TextureAspect::All,
|
||||
texture: &texture,
|
||||
mip_level: 0,
|
||||
origin: wgpu::Origin3d::ZERO,
|
||||
},
|
||||
&rgba,
|
||||
wgpu::ImageDataLayout {
|
||||
offset: 0,
|
||||
bytes_per_row: std::num::NonZeroU32::new(4 * dimensions.0),
|
||||
rows_per_image: std::num::NonZeroU32::new(dimensions.1),
|
||||
},
|
||||
size,
|
||||
);
|
||||
|
||||
// convert resource sampler into wgpu sampler
|
||||
let sampler_desc = match &texture_ref.sampler {
|
||||
Some(sampler) => {
|
||||
let magf = res_filter_to_wgpu(sampler.mag_filter.unwrap_or(FilterMode::Linear));
|
||||
let minf = res_filter_to_wgpu(sampler.min_filter.unwrap_or(FilterMode::Nearest));
|
||||
let mipf = res_filter_to_wgpu(sampler.mipmap_filter.unwrap_or(FilterMode::Nearest));
|
||||
|
||||
let wrap_u = res_wrap_to_wgpu(sampler.wrap_u);
|
||||
let wrap_v = res_wrap_to_wgpu(sampler.wrap_v);
|
||||
let wrap_w = res_wrap_to_wgpu(sampler.wrap_w);
|
||||
|
||||
wgpu::SamplerDescriptor {
|
||||
address_mode_u: wrap_u,
|
||||
address_mode_v: wrap_v,
|
||||
address_mode_w: wrap_w,
|
||||
mag_filter: magf,
|
||||
min_filter: minf,
|
||||
mipmap_filter: mipf,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
None => wgpu::SamplerDescriptor {
|
||||
address_mode_u: wgpu::AddressMode::ClampToEdge,
|
||||
address_mode_v: wgpu::AddressMode::ClampToEdge,
|
||||
address_mode_w: wgpu::AddressMode::ClampToEdge,
|
||||
mag_filter: wgpu::FilterMode::Linear,
|
||||
min_filter: wgpu::FilterMode::Nearest,
|
||||
mipmap_filter: wgpu::FilterMode::Nearest,
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
|
||||
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
let sampler = device.create_sampler(
|
||||
&sampler_desc
|
||||
);
|
||||
|
||||
let bgp = Self::create_bind_group_pair(device, bg_layout, &view, &sampler);
|
||||
|
||||
Ok(Self {
|
||||
inner_texture: texture,
|
||||
view,
|
||||
sampler,
|
||||
bindgroup_pair: Some(bgp),
|
||||
})
|
||||
}
|
||||
|
||||
/// Updates the texture on the gpu with the provided texture.
|
||||
///
|
||||
/// # Panics
|
||||
/// Panics if `texture` is not loaded
|
||||
pub fn update_texture(&mut self, _device: &wgpu::Device, queue: &wgpu::Queue, texture: &ResHandle<Texture>) {
|
||||
let texture = &texture.data_ref().unwrap().image;
|
||||
let texture = &texture.data_ref().unwrap();
|
||||
pub fn update_texture(&mut self, _device: &wgpu::Device, queue: &wgpu::Queue, texture: &Arc<ResHandle<Texture>>) {
|
||||
let texture = &texture.data_ref().image;
|
||||
let rgba = texture.to_rgba8();
|
||||
let dimensions = texture.dimensions();
|
||||
let size = wgpu::Extent3d {
|
||||
|
@ -304,21 +230,4 @@ impl RenderTexture {
|
|||
pub fn bind_group(&self) -> &wgpu::BindGroup {
|
||||
&self.bindgroup_pair.as_ref().unwrap().bindgroup
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert [`lyra_resource::WrappingMode`] to [`wgpu::AddressMode`]
|
||||
fn res_wrap_to_wgpu(wmode: WrappingMode) -> wgpu::AddressMode {
|
||||
match wmode {
|
||||
WrappingMode::ClampToEdge => wgpu::AddressMode::ClampToEdge,
|
||||
WrappingMode::MirroredRepeat => wgpu::AddressMode::MirrorRepeat,
|
||||
WrappingMode::Repeat => wgpu::AddressMode::Repeat,
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert [`lyra_resource::FilterMode`] to [`wgpu::FilterMode`]
|
||||
fn res_filter_to_wgpu(fmode: FilterMode) -> wgpu::FilterMode {
|
||||
match fmode {
|
||||
FilterMode::Nearest => wgpu::FilterMode::Nearest,
|
||||
FilterMode::Linear => wgpu::FilterMode::Linear,
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
use std::{collections::{VecDeque, HashMap}, num::NonZeroU64};
|
||||
|
||||
use uuid::Uuid;
|
||||
use lyra_ecs::Entity;
|
||||
use wgpu::Limits;
|
||||
|
||||
use std::mem;
|
||||
|
@ -19,20 +19,11 @@ pub(crate) struct TransformBufferEntry {
|
|||
pub bindgroup: wgpu::BindGroup,
|
||||
}
|
||||
|
||||
/// A helper struct for managing the Transform buffers for meshes.
|
||||
///
|
||||
/// This struct manages a "chain" of uniform buffers that store Transform for meshes. When
|
||||
/// first created it only has a single "chain-link" with a buffer that is the maximum length
|
||||
/// the GPU supports. When the first buffer fills up, a new one should be created which will also
|
||||
/// be the maximum length the GPU supports. When the new buffer fills up, a new one will be
|
||||
/// created once again, and so on.
|
||||
///
|
||||
/// `Uuid`'s are used to represent entries (usually Meshes) in the buffer. The Uuid's can be used
|
||||
/// to insert, update, and retrieve the transforms.
|
||||
pub(crate) struct TransformBuffers {
|
||||
pub bindgroup_layout: wgpu::BindGroupLayout,
|
||||
pub just_updated: HashMap<Uuid, TransformBufferIndices>,
|
||||
pub not_updated: HashMap<Uuid, TransformBufferIndices>,
|
||||
/// A vector storing the EntityId and
|
||||
pub just_updated: HashMap<Entity, TransformBufferIndices>,
|
||||
pub not_updated: HashMap<Entity, TransformBufferIndices>,
|
||||
pub dead_indices: VecDeque<TransformBufferIndices>,
|
||||
pub next_indices: TransformBufferIndices,
|
||||
/// (transform count, buffer, bindgroup)
|
||||
|
@ -87,15 +78,12 @@ impl TransformBuffers {
|
|||
s
|
||||
}
|
||||
|
||||
/// Update an transform in the buffer.
|
||||
///
|
||||
/// # Panics
|
||||
/// Panics if the entity isn't stored, you can check if it is before with [`TransformBuffers:contains`].
|
||||
pub fn update_transform(&mut self, queue: &wgpu::Queue, limits: &Limits, uuid: Uuid, transform: glam::Mat4, normal_matrix: glam::Mat3) -> TransformBufferIndices {
|
||||
let indices = self.not_updated.remove(&uuid)
|
||||
.or_else(|| self.just_updated.remove(&uuid))
|
||||
/// Update an entity's buffer with the new transform. Will panic if the entity isn't stored
|
||||
pub fn update_entity(&mut self, queue: &wgpu::Queue, limits: &Limits, entity: Entity, transform: glam::Mat4, normal_matrix: glam::Mat3) -> TransformBufferIndices {
|
||||
let indices = self.not_updated.remove(&entity)
|
||||
.or_else(|| self.just_updated.remove(&entity))
|
||||
.expect("Use 'insert_entity' for new entities");
|
||||
self.just_updated.insert(uuid, indices);
|
||||
self.just_updated.insert(entity, indices);
|
||||
|
||||
let normal_matrix = glam::Mat4::from_mat3(normal_matrix);
|
||||
|
||||
|
@ -106,8 +94,8 @@ impl TransformBuffers {
|
|||
indices
|
||||
}
|
||||
|
||||
/// Insert a new transform into the buffer, returns where in the buffer it was stored.
|
||||
pub fn insert_transform(&mut self, queue: &wgpu::Queue, limits: &Limits, uuid: Uuid, transform: glam::Mat4, normal_matrix: glam::Mat3) -> TransformBufferIndices {
|
||||
/// Insert a new entity into the buffer, returns where it was stored.
|
||||
pub fn insert_entity(&mut self, queue: &wgpu::Queue, limits: &Limits, entity: Entity, transform: glam::Mat4, normal_matrix: glam::Mat3) -> TransformBufferIndices {
|
||||
let indices = match self.dead_indices.pop_front() {
|
||||
Some(indices) => indices,
|
||||
None => {
|
||||
|
@ -125,28 +113,28 @@ impl TransformBuffers {
|
|||
}
|
||||
};
|
||||
|
||||
self.just_updated.insert(uuid, indices);
|
||||
self.update_transform(queue, limits, uuid, transform, normal_matrix)
|
||||
self.just_updated.insert(entity, indices);
|
||||
self.update_entity(queue, limits, entity, transform, normal_matrix)
|
||||
}
|
||||
|
||||
/// Update or insert a transform
|
||||
pub fn update_or_insert<TFn>(&mut self, queue: &wgpu::Queue, limits: &Limits, uuid: Uuid, transform_fn: TFn) -> TransformBufferIndices
|
||||
/// Update or insert an entities transform
|
||||
pub fn update_or_insert<TFn>(&mut self, queue: &wgpu::Queue, limits: &Limits, entity: Entity, transform_fn: TFn) -> TransformBufferIndices
|
||||
where TFn: Fn() -> (glam::Mat4, glam::Mat3)
|
||||
{
|
||||
let (tran, norm) = transform_fn();
|
||||
if self.contains(uuid) {
|
||||
self.update_transform(queue, limits, uuid, tran, norm)
|
||||
if self.contains(entity) {
|
||||
self.update_entity(queue, limits, entity, tran, norm)
|
||||
} else {
|
||||
self.insert_transform(queue, limits, uuid, tran, norm)
|
||||
self.insert_entity(queue, limits, entity, tran, norm)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the transform related to the `uuid` is stored (does not mean its up-to-date).
|
||||
pub fn contains(&self, uuid: Uuid) -> bool {
|
||||
self.not_updated.contains_key(&uuid) || self.just_updated.contains_key(&uuid)
|
||||
/// Returns true if the entity's transform is stored (does not mean its up-to-date).
|
||||
pub fn contains(&self, entity: Entity) -> bool {
|
||||
self.not_updated.contains_key(&entity) || self.just_updated.contains_key(&entity)
|
||||
}
|
||||
|
||||
/// Collect the dead transforms and prepare self to check next time.
|
||||
/// Collect the dead entities, mark entities and not updated for next updates.
|
||||
pub fn tick(&mut self) {
|
||||
// take the dead entities, these were ones that were not updated this tick
|
||||
let dead: VecDeque<TransformBufferIndices> = self.not_updated.values().copied().collect();
|
||||
|
@ -176,10 +164,7 @@ impl TransformBuffers {
|
|||
.map(|entry| &entry.bindgroup)
|
||||
}
|
||||
|
||||
/// Expand the Transform buffers by adding another uniform buffer binding.
|
||||
///
|
||||
/// This object has a chain of uniform buffers, when the buffers are expanded, a new
|
||||
/// "chain-link" is created.
|
||||
/// Expand the Transform buffers by adding another uniform buffer binding
|
||||
pub fn expand_buffers(&mut self, device: &wgpu::Device) {
|
||||
let limits = device.limits();
|
||||
let max_buffer_sizes = (limits.max_uniform_buffer_binding_size as u64) / 2;
|
||||
|
@ -243,8 +228,7 @@ impl TransformBuffers {
|
|||
self.buffer_bindgroups.push(entry);
|
||||
}
|
||||
|
||||
/// Returns the indices of the Transform
|
||||
pub fn transform_indices(&self, uuid: Uuid) -> Option<&TransformBufferIndices> {
|
||||
self.just_updated.get(&uuid).or_else(|| self.not_updated.get(&uuid))
|
||||
pub fn entity_indices(&self, entity: Entity) -> Option<&TransformBufferIndices> {
|
||||
self.just_updated.get(&entity).or_else(|| self.not_updated.get(&entity))
|
||||
}
|
||||
}
|
|
@ -1,37 +1,15 @@
|
|||
use lyra_ecs::Component;
|
||||
use lyra_reflect::Reflect;
|
||||
use lyra_resource::{gltf::Mesh, ResHandle};
|
||||
use lyra_resource::Mesh;
|
||||
|
||||
#[derive(Clone, Component, Reflect)]
|
||||
#[derive(Clone, Component)]
|
||||
pub struct MeshComponent {
|
||||
#[reflect(skip)]
|
||||
pub mesh: ResHandle<Mesh>,
|
||||
}
|
||||
|
||||
impl From<ResHandle<Mesh>> for MeshComponent {
|
||||
fn from(value: ResHandle<Mesh>) -> Self {
|
||||
Self {
|
||||
mesh: value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Deref for MeshComponent {
|
||||
type Target = ResHandle<Mesh>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.mesh
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::DerefMut for MeshComponent {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.mesh
|
||||
}
|
||||
pub mesh: Mesh,
|
||||
}
|
||||
|
||||
impl MeshComponent {
|
||||
pub fn new(mesh: ResHandle<Mesh>) -> Self {
|
||||
Self::from(mesh)
|
||||
pub fn new(mesh: Mesh) -> Self {
|
||||
Self {
|
||||
mesh,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,9 @@
|
|||
pub mod mesh;
|
||||
pub use mesh::*;
|
||||
|
||||
pub mod model;
|
||||
pub use model::*;
|
||||
|
||||
pub mod camera;
|
||||
pub use camera::*;
|
||||
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
use lyra_ecs::Component;
|
||||
use lyra_reflect::Reflect;
|
||||
use lyra_resource::ResHandle;
|
||||
|
||||
use crate::assets::Model;
|
||||
|
||||
#[derive(Clone, Component, Reflect)]
|
||||
pub struct ModelComponent(#[reflect(skip)] pub ResHandle<Model>);
|
||||
|
||||
impl From<ResHandle<Model>> for ModelComponent {
|
||||
fn from(value: ResHandle<Model>) -> Self {
|
||||
ModelComponent(value)
|
||||
}
|
||||
}
|
||||
|
||||
/* impl From<ResHandle<Model> for ModelComponent {
|
||||
|
||||
} */
|
||||
|
||||
impl std::ops::Deref for ModelComponent {
|
||||
type Target = ResHandle<Model>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::DerefMut for ModelComponent {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
/* impl ModelComponent {
|
||||
pub fn new(model, material: Material) -> Self {
|
||||
Self {
|
||||
mesh,
|
||||
material
|
||||
}
|
||||
}
|
||||
} */
|
|
@ -92,13 +92,6 @@ impl Transform {
|
|||
trans.y += y;
|
||||
trans.z += z;
|
||||
}
|
||||
|
||||
/// Combines a transform with another one.
|
||||
///
|
||||
/// The translations are added while the rotations and scales are multiplied.
|
||||
pub fn combine(self, rhs: Transform) -> Transform {
|
||||
self + rhs
|
||||
}
|
||||
|
||||
/// Performs a linear interpolation between `self` and `rhs` based on the value `alpha`.
|
||||
///
|
||||
|
|
|
@ -6,8 +6,7 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
lyra-ecs = { path = "../lyra-ecs", features = [ "math" ] }
|
||||
lyra-math = { path = "../lyra-math" }
|
||||
lyra-ecs = { path = "../lyra-ecs" }
|
||||
anyhow = "1.0.75"
|
||||
base64 = "0.21.4"
|
||||
crossbeam = { version = "0.8.4", features = [ "crossbeam-channel" ] }
|
||||
|
@ -24,4 +23,3 @@ percent-encoding = "2.3.0"
|
|||
thiserror = "1.0.48"
|
||||
tracing = "0.1.37"
|
||||
uuid = { version = "1.4.1", features = ["v4"] }
|
||||
instant = "0.1"
|
|
@ -1,37 +0,0 @@
|
|||
pub mod loader;
|
||||
pub use loader::*;
|
||||
|
||||
pub mod material;
|
||||
use lyra_math::Transform;
|
||||
pub use material::*;
|
||||
|
||||
pub mod mesh;
|
||||
pub use mesh::*;
|
||||
|
||||
pub mod scene;
|
||||
pub use scene::*;
|
||||
|
||||
use crate::ResHandle;
|
||||
|
||||
/// A loaded Gltf file
|
||||
#[derive(Clone, Default)]
|
||||
pub struct Gltf {
|
||||
pub scenes: Vec<ResHandle<GltfScene>>,
|
||||
pub materials: Vec<ResHandle<Material>>,
|
||||
pub meshes: Vec<ResHandle<Mesh>>,
|
||||
}
|
||||
|
||||
impl Gltf {
|
||||
/// Collects all Gltf meshes and gets their world Transform.
|
||||
pub fn collect_world_meshes(&self) -> Vec<(ResHandle<Mesh>, Transform)> {
|
||||
let mut v = vec![];
|
||||
|
||||
for scene in self.scenes.iter() {
|
||||
let mut tmp = scene.data_ref()
|
||||
.unwrap().collect_world_meshes();
|
||||
v.append(&mut tmp);
|
||||
}
|
||||
|
||||
v
|
||||
}
|
||||
}
|
|
@ -1,55 +0,0 @@
|
|||
use lyra_math::Transform;
|
||||
|
||||
use super::Mesh;
|
||||
use crate::ResHandle;
|
||||
|
||||
/// A Node in the Gltf file
|
||||
#[derive(Clone, Default)]
|
||||
pub struct GltfNode {
|
||||
pub name: Option<String>,
|
||||
pub mesh: Option<ResHandle<Mesh>>,
|
||||
pub transform: Transform,
|
||||
pub children: Vec<GltfNode>,
|
||||
}
|
||||
|
||||
/// A Scene in a Gltf file
|
||||
#[derive(Clone)]
|
||||
pub struct GltfScene {
|
||||
pub nodes: Vec<GltfNode>,
|
||||
}
|
||||
|
||||
impl GltfScene {
|
||||
fn collect_node(&self, parent_node: &GltfNode, node: &GltfNode) -> Vec<(ResHandle<Mesh>, Transform)> {
|
||||
let mut v = vec![];
|
||||
|
||||
if let Some(mesh) = &node.mesh {
|
||||
v.push((mesh.clone(), parent_node.transform + node.transform));
|
||||
}
|
||||
|
||||
for child in node.children.iter() {
|
||||
let mut tmp = self.collect_node(node, child);
|
||||
v.append(&mut tmp);
|
||||
}
|
||||
|
||||
v
|
||||
}
|
||||
|
||||
/// Collects all Gltf meshes and gets their world Transform.
|
||||
pub fn collect_world_meshes(&self) -> Vec<(ResHandle<Mesh>, Transform)> {
|
||||
let mut v = vec![];
|
||||
|
||||
// process the root nodes in the scene
|
||||
for parent_node in self.nodes.iter() {
|
||||
if let Some(mesh) = &parent_node.mesh {
|
||||
v.push((mesh.clone(), parent_node.transform));
|
||||
}
|
||||
|
||||
for child in parent_node.children.iter() {
|
||||
let mut tmp = self.collect_node(parent_node, child);
|
||||
v.append(&mut tmp);
|
||||
}
|
||||
}
|
||||
|
||||
v
|
||||
}
|
||||
}
|
|
@ -10,7 +10,11 @@ pub use texture::*;
|
|||
pub mod loader;
|
||||
pub use loader::*;
|
||||
|
||||
pub mod gltf;
|
||||
pub mod model;
|
||||
pub use model::*;
|
||||
|
||||
pub mod material;
|
||||
pub use material::*;
|
||||
|
||||
pub mod world_ext;
|
||||
pub use world_ext::*;
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
use std::{fs::File, sync::Arc, io::Read};
|
||||
|
||||
use image::ImageError;
|
||||
use tracing::{debug, trace};
|
||||
|
||||
use crate::{resource::ResHandle, resource_manager::ResourceStorage, Image, ResourceManager};
|
||||
use crate::{resource_manager::ResourceStorage, texture::Texture, resource::ResHandle, ResourceManager};
|
||||
|
||||
use super::{LoaderError, ResourceLoader};
|
||||
|
||||
|
@ -13,7 +12,7 @@ impl From<ImageError> for LoaderError {
|
|||
}
|
||||
}
|
||||
|
||||
/// A struct that implements the `ResourceLoader` trait used for loading images.
|
||||
/// A struct that implements the `ResourceLoader` trait used for loading textures.
|
||||
#[derive(Default)]
|
||||
pub struct ImageLoader;
|
||||
|
||||
|
@ -60,25 +59,25 @@ impl ResourceLoader for ImageLoader {
|
|||
ImageError::IoError(e) => LoaderError::IoError(e),
|
||||
_ => LoaderError::DecodingError(e.into()),
|
||||
})?;
|
||||
let image = Image::from(image);
|
||||
let res = ResHandle::with_data(path, image);
|
||||
let texture = Texture {
|
||||
image,
|
||||
};
|
||||
let res = ResHandle::with_data(path, texture);
|
||||
|
||||
Ok(Arc::new(res))
|
||||
}
|
||||
|
||||
fn load_bytes(&self, _resource_manager: &mut ResourceManager, bytes: Vec<u8>, offset: usize, length: usize) -> Result<Arc<dyn ResourceStorage>, LoaderError> {
|
||||
trace!("Loading {} bytes as an image", length);
|
||||
|
||||
let image = image::load_from_memory(&bytes[offset..(length-offset)])
|
||||
.map_err(|e| match e {
|
||||
ImageError::IoError(e) => LoaderError::IoError(e),
|
||||
_ => LoaderError::DecodingError(e.into()),
|
||||
})?;
|
||||
let image = Image::from(image);
|
||||
let res = ResHandle::with_data(&uuid::Uuid::new_v4().to_string(), image);
|
||||
let texture = Texture {
|
||||
image,
|
||||
};
|
||||
let res = ResHandle::with_data(&uuid::Uuid::new_v4().to_string(), texture);
|
||||
|
||||
debug!("Finished loading image of {} bytes", length);
|
||||
|
||||
Ok(Arc::new(res))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
pub mod image;
|
||||
pub mod model;
|
||||
|
||||
use std::{io, sync::Arc, path::Path, ffi::OsStr};
|
||||
|
||||
|
|
|
@ -1,12 +1,8 @@
|
|||
use std::{sync::Arc, path::PathBuf};
|
||||
|
||||
use glam::{Quat, Vec3};
|
||||
use instant::Instant;
|
||||
use lyra_math::Transform;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{gltf::GltfScene, util, LoaderError, ResHandle, ResourceLoader, ResourceManager};
|
||||
use super::{GltfNode, Material, Mesh, MeshIndices, MeshVertexAttribute, VertexAttributeData};
|
||||
use crate::{ResourceLoader, LoaderError, Mesh, Model, MeshVertexAttribute, VertexAttributeData, ResHandle, Material, MeshIndices, ResourceManager, util};
|
||||
|
||||
use tracing::debug;
|
||||
|
||||
|
@ -64,23 +60,16 @@ impl ModelLoader {
|
|||
}
|
||||
} */
|
||||
|
||||
fn process_node(ctx: &mut GltfLoadContext, materials: &Vec<ResHandle<Material>>, gnode: gltf::Node<'_>) -> GltfNode {
|
||||
let mut node = GltfNode::default();
|
||||
|
||||
node.transform = {
|
||||
let gt = gnode.transform();
|
||||
let (pos, rot, scale) = gt.decomposed();
|
||||
|
||||
Transform::new(Vec3::from(pos), Quat::from_array(rot), Vec3::from(scale))
|
||||
};
|
||||
node.name = gnode.name().map(str::to_string);
|
||||
|
||||
if let Some(mesh) = gnode.mesh() {
|
||||
let mut new_mesh = Mesh::default();
|
||||
fn process_node(buffers: &Vec<Vec<u8>>, materials: &Vec<Material>, node: gltf::Node<'_>) -> Vec<Mesh> {
|
||||
let mut meshes = vec![];
|
||||
//node.transform()
|
||||
|
||||
if let Some(mesh) = node.mesh() {
|
||||
for prim in mesh.primitives() {
|
||||
let reader = prim.reader(|buf| Some(ctx.buffers[buf.index()].as_slice()));
|
||||
let reader = prim.reader(|buf| Some(buffers[buf.index()].as_slice()));
|
||||
|
||||
let mut new_mesh = Mesh::default();
|
||||
|
||||
// read the positions
|
||||
if let Some(pos) = reader.read_positions() {
|
||||
if prim.mode() != gltf::mesh::Mode::Triangles {
|
||||
|
@ -122,20 +111,18 @@ impl ModelLoader {
|
|||
}
|
||||
|
||||
let mat = materials.get(prim.material().index().unwrap()).unwrap();
|
||||
new_mesh.material = Some(mat.clone());
|
||||
}
|
||||
new_mesh.set_material(mat.clone());
|
||||
|
||||
let handle = ResHandle::with_data("", new_mesh);
|
||||
ctx.resource_manager.store_uuid(handle.clone());
|
||||
node.mesh = Some(handle);
|
||||
meshes.push(new_mesh);
|
||||
}
|
||||
}
|
||||
|
||||
for child in gnode.children() {
|
||||
let cmesh = ModelLoader::process_node(ctx, materials, child);
|
||||
node.children.push(cmesh);
|
||||
for child in node.children() {
|
||||
let mut child_meshes = ModelLoader::process_node(buffers, materials, child);
|
||||
meshes.append(&mut child_meshes);
|
||||
}
|
||||
|
||||
node
|
||||
meshes
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -160,6 +147,18 @@ impl ResourceLoader for ModelLoader {
|
|||
parent_path.pop();
|
||||
let parent_path = parent_path.display().to_string();
|
||||
|
||||
/* let (document, buffers, images) = gltf::import(&path)?;
|
||||
let buffers: Vec<Vec<u8>> = buffers.into_iter().map(|b| b.0).collect();
|
||||
|
||||
let scene = document.scenes().next().unwrap();
|
||||
|
||||
let materials: Vec<Material> = document.materials()
|
||||
.map(|mat| Material::from_gltf(resource_manager, &parent_path, mat)).collect();
|
||||
|
||||
let meshes: Vec<Mesh> = scene.nodes()
|
||||
.map(|node| self.process_node(&buffers, &materials, node))
|
||||
.flatten().collect(); */
|
||||
|
||||
let gltf = gltf::Gltf::open(path)?;
|
||||
|
||||
let mut use_bin = false;
|
||||
|
@ -173,7 +172,8 @@ impl ResourceLoader for ModelLoader {
|
|||
.map_err(ModelLoaderError::UriDecodingError),
|
||||
}).collect();
|
||||
|
||||
let mut gltf_out = super::Gltf::default();
|
||||
// TODO: Read in multiple scenes
|
||||
let scene = gltf.scenes().next().unwrap();
|
||||
|
||||
let mut context = GltfLoadContext {
|
||||
resource_manager,
|
||||
|
@ -183,38 +183,15 @@ impl ResourceLoader for ModelLoader {
|
|||
buffers: &buffers,
|
||||
};
|
||||
|
||||
let start_inst = Instant::now();
|
||||
let materials: Vec<ResHandle<Material>> = gltf.materials()
|
||||
.map(|mat| ResHandle::with_data("", Material::from_gltf(&mut context, mat)))
|
||||
let materials: Vec<Material> = gltf.materials()
|
||||
.map(|mat| Material::from_gltf(&mut context, mat)).collect();
|
||||
|
||||
let meshes: Vec<Mesh> = scene.nodes()
|
||||
.flat_map(|node| ModelLoader::process_node(&buffers, &materials, node))
|
||||
.collect();
|
||||
let mat_time = Instant::now() - start_inst;
|
||||
debug!("Loaded {} materials in {}s", materials.len(), mat_time.as_secs_f32());
|
||||
debug!("Loaded {} meshes, and {} materials from '{}'", meshes.len(), materials.len(), path);
|
||||
|
||||
for (idx, scene) in gltf.scenes().enumerate() {
|
||||
let start_inst = Instant::now();
|
||||
let nodes: Vec<GltfNode> = scene.nodes()
|
||||
.map(|node| ModelLoader::process_node(&mut context, &materials, node))
|
||||
.collect();
|
||||
let node_time = Instant::now() - start_inst;
|
||||
|
||||
debug!("Loaded {} nodes in the scene in {}s", nodes.len(), node_time.as_secs_f32());
|
||||
|
||||
for mesh in nodes.iter().map(|n| &n.mesh) {
|
||||
if let Some(mesh) = mesh {
|
||||
gltf_out.meshes.push(mesh.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let scene = GltfScene {
|
||||
nodes,
|
||||
};
|
||||
let scene = ResHandle::with_data(&format!("{}:Scene{}", path, idx), scene);
|
||||
gltf_out.scenes.push(scene);
|
||||
}
|
||||
|
||||
gltf_out.materials = materials;
|
||||
|
||||
Ok(Arc::new(ResHandle::with_data(path, gltf_out)))
|
||||
Ok(Arc::new(ResHandle::with_data(path, Model::new(meshes))))
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
|
@ -225,7 +202,7 @@ impl ResourceLoader for ModelLoader {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{gltf::Gltf, ResourceLoader};
|
||||
use crate::ResourceLoader;
|
||||
use super::*;
|
||||
|
||||
fn test_file_path(path: &str) -> String {
|
||||
|
@ -240,27 +217,16 @@ mod tests {
|
|||
|
||||
let mut manager = ResourceManager::new();
|
||||
let loader = ModelLoader::default();
|
||||
let gltf = loader.load(&mut manager, &path).unwrap();
|
||||
let gltf = Arc::downcast::<ResHandle<Gltf>>(gltf.as_arc_any()).unwrap();
|
||||
let gltf = gltf.data_ref().unwrap();
|
||||
|
||||
assert_eq!(gltf.scenes.len(), 1);
|
||||
let scene = &gltf.scenes[0]
|
||||
.data_ref().unwrap();
|
||||
|
||||
assert_eq!(scene.nodes.len(), 1);
|
||||
let mnode = &scene.nodes[0];
|
||||
|
||||
assert!(mnode.mesh.is_some());
|
||||
assert_eq!(mnode.transform, Transform::from_xyz(0.0, 0.0, 0.0));
|
||||
assert_eq!(mnode.children.len(), 0);
|
||||
|
||||
let mesh = mnode.mesh.as_ref().unwrap();
|
||||
let mesh = mesh.data_ref().unwrap();
|
||||
let model = loader.load(&mut manager, &path).unwrap();
|
||||
let model = Arc::downcast::<ResHandle<Model>>(model.as_arc_any()).unwrap();
|
||||
let model = model.data_ref();
|
||||
assert_eq!(model.meshes.len(), 1); // There should only be 1 mesh
|
||||
let mesh = &model.meshes[0];
|
||||
assert!(mesh.position().unwrap().len() > 0);
|
||||
assert!(mesh.normals().unwrap().len() > 0);
|
||||
assert!(mesh.tex_coords().unwrap().len() > 0);
|
||||
assert!(mesh.indices.clone().unwrap().len() > 0);
|
||||
assert!(mesh.material.as_ref().unwrap().data_ref().unwrap().base_color_texture.is_some());
|
||||
assert!(mesh.material().base_color_texture.is_some());
|
||||
let _mesh_mat = mesh.material(); // inner panic if material was not loaded
|
||||
}
|
||||
}
|
|
@ -1,9 +1,6 @@
|
|||
use std::{collections::hash_map::DefaultHasher, hash::{Hash, Hasher}};
|
||||
|
||||
use gltf::texture::{MagFilter, MinFilter};
|
||||
|
||||
use crate::{util, FilterMode, Image, ResHandle, Texture, TextureSampler, WrappingMode};
|
||||
use super::loader::GltfLoadContext;
|
||||
use crate::{Texture, ResHandle, util, loader::model::GltfLoadContext};
|
||||
|
||||
/// PBR metallic roughness
|
||||
#[derive(Clone, Debug, Default)]
|
||||
|
@ -250,33 +247,15 @@ impl Material {
|
|||
fn load_texture(context: &mut GltfLoadContext, texture_info: gltf::texture::Info<'_>) -> ResHandle<Texture> {
|
||||
// TODO: texture_info.tex_coord()
|
||||
let tex = texture_info.texture();
|
||||
// TODO: tex.sampler()
|
||||
let img = tex.source();
|
||||
let src = img.source();
|
||||
|
||||
let buf = Material::read_source(context, src).unwrap();
|
||||
let buflen = buf.len();
|
||||
let mime_type = infer::get(&buf).expect("Failed to get image type").mime_type();
|
||||
let mime_type = infer::get(&buf).expect("Failed to get file type").mime_type();
|
||||
|
||||
let tex_img = context.resource_manager.load_bytes::<Image>(&uuid::Uuid::new_v4().to_string(), mime_type,
|
||||
buf, 0, buflen).unwrap();
|
||||
|
||||
let samp = tex.sampler();
|
||||
let samp = TextureSampler {
|
||||
mag_filter: samp.mag_filter().map(FilterMode::from_mag_filter),
|
||||
min_filter: samp.min_filter().map(FilterMode::from_min_filter),
|
||||
mipmap_filter: samp.min_filter().and_then(FilterMode::mipmap_filter),
|
||||
wrap_u: samp.wrap_s().into(),
|
||||
wrap_v: samp.wrap_t().into(),
|
||||
wrap_w: WrappingMode::ClampToEdge,
|
||||
};
|
||||
|
||||
let handler = ResHandle::with_data("", Texture {
|
||||
image: tex_img,
|
||||
sampler: Some(samp),
|
||||
});
|
||||
context.resource_manager.store_uuid(handler.clone());
|
||||
handler
|
||||
context.resource_manager.load_bytes::<Texture>(&uuid::Uuid::new_v4().to_string(), mime_type,
|
||||
buf, 0, buflen).unwrap()
|
||||
}
|
||||
|
||||
/// Load the Material from a gltf::Material.
|
||||
|
@ -316,55 +295,4 @@ impl Material {
|
|||
specular,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<gltf::texture::MagFilter> for FilterMode {
|
||||
fn from(value: gltf::texture::MagFilter) -> Self {
|
||||
match value {
|
||||
gltf::texture::MagFilter::Nearest => Self::Nearest,
|
||||
gltf::texture::MagFilter::Linear => Self::Linear,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FilterMode {
|
||||
/// Get the MinFilter mode and the mipmap filter mode from gltf MinFilter
|
||||
pub fn from_min_filter(minf: MinFilter) -> FilterMode {
|
||||
match minf {
|
||||
MinFilter::Nearest => FilterMode::Nearest,
|
||||
MinFilter::Linear => FilterMode::Linear,
|
||||
MinFilter::NearestMipmapNearest => FilterMode::Nearest,
|
||||
MinFilter::LinearMipmapNearest => FilterMode::Linear,
|
||||
MinFilter::NearestMipmapLinear => FilterMode::Nearest,
|
||||
MinFilter::LinearMipmapLinear => FilterMode::Linear,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_mag_filter(magf: MagFilter) -> FilterMode {
|
||||
match magf {
|
||||
MagFilter::Nearest => FilterMode::Nearest,
|
||||
MagFilter::Linear => FilterMode::Linear,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mipmap_filter(minf: MinFilter) -> Option<FilterMode> {
|
||||
match minf {
|
||||
MinFilter::Nearest => None,
|
||||
MinFilter::Linear => None,
|
||||
MinFilter::NearestMipmapNearest => Some(FilterMode::Nearest),
|
||||
MinFilter::LinearMipmapNearest => Some(FilterMode::Nearest),
|
||||
MinFilter::NearestMipmapLinear => Some(FilterMode::Linear),
|
||||
MinFilter::LinearMipmapLinear => Some(FilterMode::Linear),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<gltf::texture::WrappingMode> for WrappingMode {
|
||||
fn from(value: gltf::texture::WrappingMode) -> Self {
|
||||
match value {
|
||||
gltf::texture::WrappingMode::ClampToEdge => Self::ClampToEdge,
|
||||
gltf::texture::WrappingMode::MirroredRepeat => Self::MirroredRepeat,
|
||||
gltf::texture::WrappingMode::Repeat => Self::Repeat,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,8 +1,7 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use crate::{lyra_engine, ResHandle};
|
||||
|
||||
use super::Material;
|
||||
use crate::Material;
|
||||
use crate::lyra_engine;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
|
@ -83,11 +82,23 @@ pub enum MeshVertexAttribute {
|
|||
Other(String),
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, lyra_ecs::Component)]
|
||||
#[derive(Clone, lyra_ecs::Component)]
|
||||
pub struct Mesh {
|
||||
pub uuid: uuid::Uuid,
|
||||
pub attributes: HashMap<MeshVertexAttribute, VertexAttributeData>,
|
||||
pub indices: Option<MeshIndices>,
|
||||
pub material: Option<ResHandle<Material>>,
|
||||
material: Option<Material>,
|
||||
}
|
||||
|
||||
impl Default for Mesh {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
uuid: uuid::Uuid::new_v4(),
|
||||
attributes: Default::default(),
|
||||
indices: Default::default(),
|
||||
material: Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mesh {
|
||||
|
@ -116,4 +127,26 @@ impl Mesh {
|
|||
self.attributes.get(&MeshVertexAttribute::TexCoords)
|
||||
.map(|p| p.as_vec2())
|
||||
}
|
||||
|
||||
pub fn material(&self) -> Material {
|
||||
self.material.clone().expect("This mesh is missing a material!")
|
||||
}
|
||||
|
||||
pub fn set_material(&mut self, val: Material) {
|
||||
self.material = Some(val);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct Model {
|
||||
pub meshes: Vec<Mesh>,
|
||||
//pub material
|
||||
}
|
||||
|
||||
impl Model {
|
||||
pub fn new(meshes: Vec<Mesh>) -> Self {
|
||||
Self {
|
||||
meshes,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,5 @@
|
|||
use std::{any::Any, sync::{Arc, RwLock}};
|
||||
|
||||
use lyra_ecs::Component;
|
||||
use crate::lyra_engine;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::ResourceStorage;
|
||||
|
@ -40,8 +38,7 @@ pub struct Resource<T> {
|
|||
/// This struct has an inner [`RwLock`] to the resource data, so most methods may be blocking.
|
||||
/// However, the only times it will be blocking is if another thread is reloading the resource
|
||||
/// and has a write lock on the data. This means that most of the time, it is not blocking.
|
||||
#[derive(Component)]
|
||||
pub struct ResHandle<T: 'static> {
|
||||
pub struct ResHandle<T> {
|
||||
pub(crate) data: Arc<RwLock<Resource<T>>>,
|
||||
}
|
||||
|
||||
|
@ -106,7 +103,18 @@ impl<T> ResHandle<T> {
|
|||
}
|
||||
|
||||
/// Get a reference to the data in the resource
|
||||
pub fn data_ref<'a>(&'a self) -> Option<ResourceDataRef<'a, T>> {
|
||||
///
|
||||
/// # Panics
|
||||
/// Panics if the resource was not loaded yet.
|
||||
pub fn data_ref<'a>(&'a self) -> ResourceDataRef<'a, T> {
|
||||
let d = self.data.read().expect("Resource mutex was poisoned!");
|
||||
ResourceDataRef {
|
||||
guard: d
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempt to get a borrow to the resource data. Returns `None` if the resource is not loaded.
|
||||
pub fn try_data_ref<'a>(&'a self) -> Option<ResourceDataRef<'a, T>> {
|
||||
if self.is_loaded() {
|
||||
let d = self.data.read().expect("Resource mutex was poisoned!");
|
||||
Some(ResourceDataRef {
|
||||
|
|
|
@ -6,7 +6,7 @@ use notify_debouncer_full::{DebouncedEvent, FileIdMap};
|
|||
use thiserror::Error;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{gltf::ModelLoader, loader::{image::ImageLoader, LoaderError, ResourceLoader}, resource::ResHandle, ResourceState};
|
||||
use crate::{loader::{image::ImageLoader, model::ModelLoader, LoaderError, ResourceLoader}, resource::ResHandle, ResourceState};
|
||||
|
||||
/// A trait for type erased storage of a resource.
|
||||
/// Implemented for [`ResHandle<T>`]
|
||||
|
@ -56,7 +56,6 @@ pub struct ResourceWatcher {
|
|||
|
||||
pub struct ResourceManager {
|
||||
resources: HashMap<String, Arc<dyn ResourceStorage>>,
|
||||
uuid_resources: HashMap<Uuid, Arc<dyn ResourceStorage>>,
|
||||
loaders: Vec<Arc<dyn ResourceLoader>>,
|
||||
watchers: HashMap<String, ResourceWatcher>,
|
||||
}
|
||||
|
@ -71,7 +70,6 @@ impl ResourceManager {
|
|||
pub fn new() -> Self {
|
||||
Self {
|
||||
resources: HashMap::new(),
|
||||
uuid_resources: HashMap::new(),
|
||||
loaders: vec![ Arc::new(ImageLoader), Arc::new(ModelLoader) ],
|
||||
watchers: HashMap::new(),
|
||||
}
|
||||
|
@ -150,29 +148,6 @@ impl ResourceManager {
|
|||
}
|
||||
}
|
||||
|
||||
/// Store a resource using its uuid.
|
||||
///
|
||||
/// The resource cannot be requested with [`ResourceManager::request`], it can only be
|
||||
/// retrieved with [`ResourceManager::request_uuid`].
|
||||
pub fn store_uuid<T: Send + Sync + 'static>(&mut self, res: ResHandle<T>) {
|
||||
self.uuid_resources.insert(res.uuid(), Arc::new(res));
|
||||
}
|
||||
|
||||
/// Request a resource via its uuid.
|
||||
///
|
||||
/// Returns `None` if the resource was not found. The resource must of had been
|
||||
/// stored with [`ResourceManager::request`] to return `Some`.
|
||||
pub fn request_uuid<T: Send + Sync + 'static>(&mut self, uuid: &Uuid) -> Option<ResHandle<T>> {
|
||||
match self.uuid_resources.get(uuid) {
|
||||
Some(res) => {
|
||||
let res = res.clone().as_arc_any();
|
||||
let res: Arc<ResHandle<T>> = res.downcast::<ResHandle<T>>().expect("Failure to downcast resource");
|
||||
Some(ResHandle::<T>::clone(&res))
|
||||
},
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Store bytes in the manager. If there is already an entry with the same identifier it will be updated.
|
||||
///
|
||||
/// Panics: If there is already an entry with the same `ident`, and the entry is not bytes, this function will panic.
|
||||
|
@ -329,7 +304,7 @@ mod tests {
|
|||
let mut man = ResourceManager::new();
|
||||
let res = man.request::<Texture>(&get_image("squiggles.png")).unwrap();
|
||||
assert_eq!(res.state(), ResourceState::Ready);
|
||||
let img = res.data_ref();
|
||||
let img = res.try_data_ref();
|
||||
img.unwrap();
|
||||
}
|
||||
|
||||
|
@ -365,7 +340,7 @@ mod tests {
|
|||
let mut man = ResourceManager::new();
|
||||
let res = man.request::<Texture>(&get_image("squiggles.png")).unwrap();
|
||||
assert_eq!(res.state(), ResourceState::Ready);
|
||||
let img = res.data_ref();
|
||||
let img = res.try_data_ref();
|
||||
img.unwrap();
|
||||
|
||||
println!("Path = {}", res.path());
|
||||
|
@ -385,7 +360,7 @@ mod tests {
|
|||
let mut man = ResourceManager::new();
|
||||
let res = man.request::<Texture>(&image_path).unwrap();
|
||||
assert_eq!(res.state(), ResourceState::Ready);
|
||||
let img = res.data_ref();
|
||||
let img = res.try_data_ref();
|
||||
img.unwrap();
|
||||
|
||||
let recv = man.watch(&image_path, false).unwrap();
|
||||
|
|
|
@ -1,73 +1,10 @@
|
|||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
//pub use gltf::texture::{MagFilter, MinFilter, WrappingMode};
|
||||
use image::DynamicImage;
|
||||
|
||||
use crate::ResHandle;
|
||||
|
||||
/// The filter mode of the sampler.
|
||||
///
|
||||
/// This is used for minification, magnification, and mipmap filters
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub enum FilterMode {
|
||||
Nearest,
|
||||
Linear,
|
||||
}
|
||||
|
||||
/// The wrapping mode of the Texture coordinates
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub enum WrappingMode {
|
||||
ClampToEdge,
|
||||
MirroredRepeat,
|
||||
Repeat,
|
||||
}
|
||||
|
||||
/// The descriptor of the sampler for a Texture.
|
||||
#[derive(Clone)]
|
||||
pub struct TextureSampler {
|
||||
pub mag_filter: Option<FilterMode>,
|
||||
pub min_filter: Option<FilterMode>,
|
||||
pub mipmap_filter: Option<FilterMode>,
|
||||
pub wrap_u: WrappingMode,
|
||||
pub wrap_v: WrappingMode,
|
||||
pub wrap_w: WrappingMode,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Image(DynamicImage);
|
||||
|
||||
impl Deref for Image {
|
||||
type Target = DynamicImage;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for Image {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DynamicImage> for Image {
|
||||
fn from(value: DynamicImage) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Texture {
|
||||
pub image: ResHandle<Image>,
|
||||
pub sampler: Option<TextureSampler>,
|
||||
pub image: DynamicImage,
|
||||
}
|
||||
|
||||
impl Texture {
|
||||
/// Create a texture from an image.
|
||||
pub fn from_image(image: ResHandle<Image>) -> Self {
|
||||
Self {
|
||||
image,
|
||||
sampler: None,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -51,7 +51,7 @@ impl<'a, T> DerefMut for MutCow<'a, T> {
|
|||
/// This SceneGraph is special in the sense that it is literally just an ECS world with methods
|
||||
/// implemented for it that make it easier to use for a SceneGraph.
|
||||
//#[derive(Default)]
|
||||
pub struct SceneGraph<'a> {
|
||||
pub struct SceneGraph<'a> {
|
||||
pub(crate) world: MutCow<'a, World>,
|
||||
root_node: SceneNode,
|
||||
}
|
||||
|
@ -162,13 +162,6 @@ impl<'a> SceneGraph<'a> {
|
|||
world_add_child_node(&mut self.world, parent, local_transform, bundle)
|
||||
}
|
||||
|
||||
pub fn add_empty_node_under(&mut self, parent: &SceneNode, local_transform: Transform) -> SceneNode {
|
||||
let e = self.world.spawn((SceneNodeFlag, local_transform));
|
||||
self.world.add_relation(e, ChildOf, parent.entity());
|
||||
|
||||
SceneNode::new(Some(parent.entity()), e)
|
||||
}
|
||||
|
||||
/// Traverses down the SceneGraph, calling `callback` with each SceneNode and its world transform.
|
||||
///
|
||||
/// The traversal does not include the root scene node.
|
||||
|
@ -179,7 +172,7 @@ impl<'a> SceneGraph<'a> {
|
|||
self.traverse_down_from(self.root_node.clone(), &mut callback);
|
||||
}
|
||||
|
||||
/// Recursively Traverses down the SceneGraph from a starting node, calling `callback` with each
|
||||
/// Traverses down the SceneGraph from a starting node, calling `callback` with each
|
||||
/// SceneNode and its world transform.
|
||||
fn traverse_down_from<F>(&self, start: SceneNode, callback: &mut F)
|
||||
where
|
||||
|
@ -196,10 +189,6 @@ impl<'a> SceneGraph<'a> {
|
|||
self.traverse_down_from(node, callback);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn root_node(&self) -> SceneNode {
|
||||
self.root_node.clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// Add a node under a parent node.
|
||||
|
@ -267,7 +256,7 @@ pub mod tests {
|
|||
});
|
||||
}
|
||||
|
||||
/* #[test]
|
||||
#[test]
|
||||
fn inserting_and_from_world() {
|
||||
let v2s = vec![Vec3::new(10.0, 10.0, 10.0), Vec3::new(50.0, 50.0, 50.0)];
|
||||
|
||||
|
@ -312,5 +301,5 @@ pub mod tests {
|
|||
|
||||
idx += 1;
|
||||
});
|
||||
} */
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue