Compare commits
7 Commits
e376241aae
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
339351c207 | ||
| b989146249 | |||
| b67a180341 | |||
| 14b0948b9c | |||
| 54e89a28ba | |||
| ebf5aa0ac4 | |||
| 4a041cc833 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
|||||||
/target
|
/target
|
||||||
|
Cargo.lock
|
||||||
|
.idea/*
|
||||||
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
@@ -5,7 +5,8 @@ use cgmath::{Decomposed, InnerSpace, Matrix4, Point3, Rad, Vector3};
|
|||||||
use winit_24::dpi::{LogicalPosition, PhysicalPosition};
|
use winit_24::dpi::{LogicalPosition, PhysicalPosition};
|
||||||
use winit_24::event::{ElementState, MouseScrollDelta, VirtualKeyCode};
|
use winit_24::event::{ElementState, MouseScrollDelta, VirtualKeyCode};
|
||||||
use crate::render::OPENGL_TO_WGPU_MATRIX;
|
use crate::render::OPENGL_TO_WGPU_MATRIX;
|
||||||
|
use imgui::Condition;
|
||||||
|
use imgui::*;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
pub struct Camera {
|
pub struct Camera {
|
||||||
@@ -150,6 +151,7 @@ impl CameraController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_camera(&mut self, camera: &mut Camera, dt: f32) {
|
pub fn update_camera(&mut self, camera: &mut Camera, dt: f32) {
|
||||||
|
|
||||||
// Move forward/backward and left/right
|
// Move forward/backward and left/right
|
||||||
let view_vector = Vector3::new(
|
let view_vector = Vector3::new(
|
||||||
(1.0 * camera.pitch.0.sin() * camera.yaw.0.sin()),
|
(1.0 * camera.pitch.0.sin() * camera.yaw.0.sin()),
|
||||||
|
|||||||
@@ -8,11 +8,13 @@ use rapier3d::geometry::ColliderHandle;
|
|||||||
use wgpu::{BindGroup, Buffer, TextureView};
|
use wgpu::{BindGroup, Buffer, TextureView};
|
||||||
|
|
||||||
use crate::runtime::state::{TomlPositionDescription, TomlRotationDescription};
|
use crate::runtime::state::{TomlPositionDescription, TomlRotationDescription};
|
||||||
|
use imgui::Ui;
|
||||||
|
|
||||||
// a component is any type that is 'static, sized, send and sync
|
// a component is any type that is 'static, sized, send and sync
|
||||||
|
|
||||||
pub struct ImguiWindow<'a> {
|
pub struct ImguiWindow<'a, T> {
|
||||||
pub window: imgui::Window<'a>,
|
pub window: fn() -> imgui::Window<'a>,
|
||||||
|
pub func: fn(&Ui, Vec<&T>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
@@ -30,6 +32,21 @@ pub struct Position {
|
|||||||
pub rot: cgmath::Euler<Deg<f32>>,
|
pub rot: cgmath::Euler<Deg<f32>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Position {
|
||||||
|
fn default() -> Self {
|
||||||
|
Position {
|
||||||
|
x: 0.0,
|
||||||
|
y: 0.0,
|
||||||
|
z: 0.0,
|
||||||
|
rot: Euler {
|
||||||
|
x: Deg(0.0),
|
||||||
|
y: Deg(0.0),
|
||||||
|
z: Deg(0.0),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<TomlPositionDescription> for Position {
|
impl From<TomlPositionDescription> for Position {
|
||||||
fn from(pos: TomlPositionDescription) -> Self {
|
fn from(pos: TomlPositionDescription) -> Self {
|
||||||
let euler = match pos.rot {
|
let euler = match pos.rot {
|
||||||
|
|||||||
70
src/main.rs
70
src/main.rs
@@ -1,16 +1,17 @@
|
|||||||
|
extern crate env_logger;
|
||||||
extern crate imgui;
|
extern crate imgui;
|
||||||
extern crate imgui_wgpu;
|
extern crate imgui_wgpu;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
extern crate tobj;
|
|
||||||
extern crate winit_24;
|
|
||||||
extern crate env_logger;
|
|
||||||
extern crate toml;
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
|
extern crate tobj;
|
||||||
|
extern crate toml;
|
||||||
|
extern crate winit_24;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::f32::consts::PI;
|
use std::f32::consts::PI;
|
||||||
|
use std::fs;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
@@ -20,6 +21,7 @@ use cgmath::{
|
|||||||
};
|
};
|
||||||
use futures::executor::block_on;
|
use futures::executor::block_on;
|
||||||
use futures::task::LocalSpawn;
|
use futures::task::LocalSpawn;
|
||||||
|
use futures::FutureExt;
|
||||||
use gilrs::Event as GilEvent;
|
use gilrs::Event as GilEvent;
|
||||||
use gilrs::{Gamepad, Gilrs};
|
use gilrs::{Gamepad, Gilrs};
|
||||||
use imgui::FontSource;
|
use imgui::FontSource;
|
||||||
@@ -28,6 +30,7 @@ use imgui::*;
|
|||||||
use imgui_wgpu::{Renderer as ImguiRenderer, RendererConfig as ImguiRendererConfig};
|
use imgui_wgpu::{Renderer as ImguiRenderer, RendererConfig as ImguiRendererConfig};
|
||||||
use legion::systems::{SyncResources, UnsafeResources};
|
use legion::systems::{SyncResources, UnsafeResources};
|
||||||
use legion::*;
|
use legion::*;
|
||||||
|
use log::LevelFilter;
|
||||||
use rapier3d::counters::Timer;
|
use rapier3d::counters::Timer;
|
||||||
use rapier3d::dynamics::{
|
use rapier3d::dynamics::{
|
||||||
IntegrationParameters, JointSet, RigidBody, RigidBodyBuilder, RigidBodyHandle, RigidBodySet,
|
IntegrationParameters, JointSet, RigidBody, RigidBodyBuilder, RigidBodyHandle, RigidBodySet,
|
||||||
@@ -40,7 +43,8 @@ use rapier3d::pipeline::PhysicsPipeline;
|
|||||||
use wgpu::{BindGroup, Buffer, TextureView};
|
use wgpu::{BindGroup, Buffer, TextureView};
|
||||||
use wgpu_subscriber;
|
use wgpu_subscriber;
|
||||||
use winit_24::event::DeviceEvent::MouseMotion;
|
use winit_24::event::DeviceEvent::MouseMotion;
|
||||||
use winit_24::platform::unix::x11::ffi::Time;
|
use winit_24::event::{ElementState, VirtualKeyCode};
|
||||||
|
use winit_24::event_loop::EventLoopProxy;
|
||||||
use winit_24::window::Window;
|
use winit_24::window::Window;
|
||||||
use winit_24::{
|
use winit_24::{
|
||||||
event::{self, WindowEvent},
|
event::{self, WindowEvent},
|
||||||
@@ -54,13 +58,9 @@ use crate::imgui_supp::extended_winit_imgui_support;
|
|||||||
use crate::imgui_supp::imgui_support::{ImguiContext, ImguiPlatform};
|
use crate::imgui_supp::imgui_support::{ImguiContext, ImguiPlatform};
|
||||||
use crate::owned_event::{OwnedEvent, OwnedEventExtension};
|
use crate::owned_event::{OwnedEvent, OwnedEventExtension};
|
||||||
use crate::physics::state::PhysicsState;
|
use crate::physics::state::PhysicsState;
|
||||||
use std::fs;
|
use crate::render::system::ImguiPerformanceProfilerLine;
|
||||||
use winit_24::event::{VirtualKeyCode, ElementState};
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use futures::FutureExt;
|
|
||||||
use log::LevelFilter;
|
|
||||||
use crate::runtime::state::RuntimeState;
|
use crate::runtime::state::RuntimeState;
|
||||||
use winit_24::event_loop::EventLoopProxy;
|
use winit_24::dpi::PhysicalSize;
|
||||||
|
|
||||||
mod camera;
|
mod camera;
|
||||||
mod components;
|
mod components;
|
||||||
@@ -112,15 +112,18 @@ Todo:
|
|||||||
|
|
||||||
//log::info!("");
|
//log::info!("");
|
||||||
|
|
||||||
// ImGUI works on more or less a global state. which is MegaLame
|
// ImGUI works on more or less an unsafe global state. which is MegaLame
|
||||||
static mut CURRENT_UI: Option<imgui::Ui<'static>> = None;
|
static mut CURRENT_UI: Option<imgui::Ui<'static>> = None;
|
||||||
pub unsafe fn current_ui<'a>() -> Option<&'a imgui::Ui<'a>> {
|
pub unsafe fn current_ui<'a>() -> Option<&'a imgui::Ui<'a>> {
|
||||||
CURRENT_UI.as_ref()
|
CURRENT_UI.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
|
||||||
|
|
||||||
let logger = env_logger::builder().filter(Some("minimal_viable_game_engine"), LevelFilter::Info).init();
|
|
||||||
|
fn main() {
|
||||||
|
let logger = env_logger::builder()
|
||||||
|
.filter(Some("minimal_viable_game_engine"), LevelFilter::Info)
|
||||||
|
.init();
|
||||||
|
|
||||||
let mut world = World::default();
|
let mut world = World::default();
|
||||||
|
|
||||||
@@ -131,10 +134,13 @@ fn main() {
|
|||||||
let mut load_schedule = Schedule::builder()
|
let mut load_schedule = Schedule::builder()
|
||||||
.add_system(runtime::system::runtime_load_system())
|
.add_system(runtime::system::runtime_load_system())
|
||||||
.add_system(runtime::system::runtime_spawn_system())
|
.add_system(runtime::system::runtime_spawn_system())
|
||||||
|
.flush()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let mut render_schedule = Schedule::builder()
|
let mut render_schedule = Schedule::builder()
|
||||||
|
.add_system(render::system::render_imgui_system())
|
||||||
.add_system(render::system::render_test_system())
|
.add_system(render::system::render_test_system())
|
||||||
|
.add_system(render::system::render_performance_flag_system())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let mut update_schedule = Schedule::builder()
|
let mut update_schedule = Schedule::builder()
|
||||||
@@ -151,11 +157,14 @@ fn main() {
|
|||||||
let event_loop = EventLoop::<OwnedEventExtension>::with_user_event();
|
let event_loop = EventLoop::<OwnedEventExtension>::with_user_event();
|
||||||
let mut builder = winit_24::window::WindowBuilder::new();
|
let mut builder = winit_24::window::WindowBuilder::new();
|
||||||
builder = builder.with_title("MVGE");
|
builder = builder.with_title("MVGE");
|
||||||
|
builder = builder.with_inner_size(PhysicalSize::new(1200,900));
|
||||||
|
|
||||||
let window = builder.build(&event_loop).unwrap();
|
let window = builder.build(&event_loop).unwrap();
|
||||||
|
|
||||||
let mut resources = Resources::default();
|
let mut resources = Resources::default();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Load up all the resources
|
// Load up all the resources
|
||||||
{
|
{
|
||||||
let mut imgui_context = imgui::Context::create();
|
let mut imgui_context = imgui::Context::create();
|
||||||
@@ -242,21 +251,26 @@ fn main() {
|
|||||||
// conditionally, and run the fps locked renderer
|
// conditionally, and run the fps locked renderer
|
||||||
event::Event::MainEventsCleared => {
|
event::Event::MainEventsCleared => {
|
||||||
event_schedule.execute(&mut world, &mut resources);
|
event_schedule.execute(&mut world, &mut resources);
|
||||||
imgui_prepare_schedule.execute(&mut world, &mut resources);
|
|
||||||
resources
|
resources
|
||||||
.get_mut::<Vec<OwnedEvent<OwnedEventExtension>>>()
|
.get_mut::<Vec<OwnedEvent<OwnedEventExtension>>>()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.clear();
|
.clear();
|
||||||
|
|
||||||
|
imgui_prepare_schedule.execute(&mut world, &mut resources);
|
||||||
|
|
||||||
let (step_size, elapsed_time) = {
|
let (step_size, elapsed_time) = {
|
||||||
// deltatime since last frame
|
let mut loop_state = resources.get_mut::<LoopState>().unwrap();
|
||||||
let loop_state = resources.get::<LoopState>().unwrap();
|
|
||||||
(
|
(
|
||||||
loop_state.step_size,
|
loop_state.step_size,
|
||||||
loop_state.start_time.elapsed().as_secs_f32(),
|
loop_state.start_time.elapsed().as_secs_f32(),
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
delta_time = elapsed_time - current_time;
|
delta_time = elapsed_time - current_time;
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut loop_state = resources.get_mut::<LoopState>().unwrap();
|
||||||
|
loop_state.delta_time = Duration::from_secs_f32(delta_time);
|
||||||
|
}
|
||||||
current_time = elapsed_time;
|
current_time = elapsed_time;
|
||||||
if delta_time > 0.02 {
|
if delta_time > 0.02 {
|
||||||
delta_time = 0.02;
|
delta_time = 0.02;
|
||||||
@@ -265,10 +279,10 @@ fn main() {
|
|||||||
|
|
||||||
while accumulator_time - step_size >= step_size {
|
while accumulator_time - step_size >= step_size {
|
||||||
accumulator_time -= step_size;
|
accumulator_time -= step_size;
|
||||||
|
|
||||||
// ==== DELTA TIME LOCKED ====
|
// ==== DELTA TIME LOCKED ====
|
||||||
update_schedule.execute(&mut world, &mut resources);
|
update_schedule.execute(&mut world, &mut resources);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==== FPS LOCKED ====
|
// ==== FPS LOCKED ====
|
||||||
render_schedule.execute(&mut world, &mut resources);
|
render_schedule.execute(&mut world, &mut resources);
|
||||||
}
|
}
|
||||||
@@ -289,15 +303,17 @@ fn main() {
|
|||||||
event: winit_24::event::DeviceEvent::Key(keyboard_input),
|
event: winit_24::event::DeviceEvent::Key(keyboard_input),
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
match keyboard_input.virtual_keycode.unwrap() {
|
if keyboard_input.virtual_keycode.is_some() {
|
||||||
VirtualKeyCode::Escape => {
|
match keyboard_input.virtual_keycode.unwrap() {
|
||||||
if keyboard_input.state == ElementState::Pressed {
|
VirtualKeyCode::Escape => {
|
||||||
*control_flow = ControlFlow::Exit;
|
if keyboard_input.state == ElementState::Pressed {
|
||||||
} else {
|
*control_flow = ControlFlow::Exit;
|
||||||
//d
|
} else {
|
||||||
|
//d
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
_ => (),
|
||||||
}
|
}
|
||||||
_ => ()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
event::Event::WindowEvent {
|
event::Event::WindowEvent {
|
||||||
@@ -320,7 +336,6 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn setup_gamepad(event_loop: &EventLoop<OwnedEventExtension>) {
|
pub fn setup_gamepad(event_loop: &EventLoop<OwnedEventExtension>) {
|
||||||
|
|
||||||
let event_loop_proxy = event_loop.create_proxy();
|
let event_loop_proxy = event_loop.create_proxy();
|
||||||
|
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
@@ -362,4 +377,3 @@ pub fn setup_gamepad(event_loop: &EventLoop<OwnedEventExtension>) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,11 @@ use rapier3d::geometry::{BroadPhase, ColliderSet, NarrowPhase};
|
|||||||
use rapier3d::pipeline::{PhysicsPipeline, ChannelEventCollector};
|
use rapier3d::pipeline::{PhysicsPipeline, ChannelEventCollector};
|
||||||
|
|
||||||
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, ImguiWindow};
|
||||||
use imgui::FontSource;
|
use imgui::{FontSource, Condition};
|
||||||
|
use imgui::*;
|
||||||
use crate::physics::state::PhysicsState;
|
use crate::physics::state::PhysicsState;
|
||||||
|
use crate::render::system::ImguiGenericOutputLine;
|
||||||
|
|
||||||
|
|
||||||
#[system]
|
#[system]
|
||||||
@@ -73,14 +75,22 @@ pub fn run_physics(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[system]
|
#[system]
|
||||||
#[write_component(Camera)]
|
#[write_component(Camera)]
|
||||||
#[write_component(CameraController)]
|
#[write_component(CameraController)]
|
||||||
|
#[write_component(ImguiGenericOutputLine)]
|
||||||
pub fn update_camera(world: &mut SubWorld, #[resource] loop_state: &mut LoopState) {
|
pub fn update_camera(world: &mut SubWorld, #[resource] loop_state: &mut LoopState) {
|
||||||
|
|
||||||
let mut query = <(&mut Camera, &mut CameraController)>::query();
|
let mut query = <(&mut Camera, &mut CameraController)>::query();
|
||||||
for (mut camera, controller) in query.iter_mut(world) {
|
for (mut camera, controller) in query.iter_mut(world) {
|
||||||
controller.update_camera(&mut camera, loop_state.step_size)
|
controller.update_camera(&mut camera, loop_state.step_size)
|
||||||
}
|
}
|
||||||
|
let mut query = <(&mut Camera, &mut CameraController, &mut ImguiGenericOutputLine)>::query();
|
||||||
|
for (mut camera, controller, ui_entry) in query.iter_mut(world) {
|
||||||
|
ui_entry.label = format!("Camera: {:.2}, {:.2}, {:.2}", camera.position.x, camera.position.y, camera.position.z);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[system]
|
#[system]
|
||||||
@@ -93,6 +103,7 @@ pub fn update_models(
|
|||||||
#[resource] physics_state: &mut PhysicsState,
|
#[resource] physics_state: &mut PhysicsState,
|
||||||
#[resource] physics_pipeline: &mut PhysicsPipeline,
|
#[resource] physics_pipeline: &mut PhysicsPipeline,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
// Make sure all the entities we care about are added to the system
|
// Make sure all the entities we care about are added to the system
|
||||||
let mut query = <(&mut Collider, &mut Physics, &mut Mesh, &mut Position)>::query();
|
let mut query = <(&mut Collider, &mut Physics, &mut Mesh, &mut Position)>::query();
|
||||||
for (collider, physics, mesh, position) in query.iter_mut(world) {
|
for (collider, physics, mesh, position) in query.iter_mut(world) {
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ use rapier3d::parry::motion::RigidMotionComposition;
|
|||||||
use wgpu::util::DeviceExt;
|
use wgpu::util::DeviceExt;
|
||||||
use wgpu::{BackendBit, BindGroup, BindGroupLayout, Buffer, BufferBindingType, Device, FragmentState, Instance, Queue, Surface, SwapChain, SwapChainDescriptor, SwapChainFrame, TextureView, VertexState, CommandEncoder};
|
use wgpu::{BackendBit, BindGroup, BindGroupLayout, Buffer, BufferBindingType, Device, FragmentState, Instance, Queue, Surface, SwapChain, SwapChainDescriptor, SwapChainFrame, TextureView, VertexState, CommandEncoder};
|
||||||
use winit_24::dpi::PhysicalSize;
|
use winit_24::dpi::PhysicalSize;
|
||||||
use winit_24::platform::unix::x11::ffi::Time;
|
|
||||||
use winit_24::window::Window;
|
use winit_24::window::Window;
|
||||||
|
|
||||||
use crate::camera::{Camera, CameraController};
|
use crate::camera::{Camera, CameraController};
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
use std::iter::Chain;
|
||||||
|
use std::slice::Iter;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::thread::current;
|
use std::thread::current;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
@@ -18,20 +20,24 @@ use legion::world::SubWorld;
|
|||||||
use legion::*;
|
use legion::*;
|
||||||
use rapier3d::parry::motion::RigidMotionComposition;
|
use rapier3d::parry::motion::RigidMotionComposition;
|
||||||
use wgpu::util::DeviceExt;
|
use wgpu::util::DeviceExt;
|
||||||
use wgpu::{BackendBit, BindGroup, BindGroupLayout, Buffer, BufferBindingType, Device, FragmentState, Instance, Queue, Surface, SwapChain, SwapChainDescriptor, SwapChainFrame, TextureView, VertexState, CommandEncoder};
|
use wgpu::{
|
||||||
|
BackendBit, BindGroup, BindGroupLayout, Buffer, BufferBindingType, CommandEncoder, Device,
|
||||||
|
FragmentState, Instance, Queue, Surface, SwapChain, SwapChainDescriptor, SwapChainFrame,
|
||||||
|
TextureView, VertexState,
|
||||||
|
};
|
||||||
use winit_24::dpi::PhysicalSize;
|
use winit_24::dpi::PhysicalSize;
|
||||||
use winit_24::platform::unix::x11::ffi::Time;
|
|
||||||
use winit_24::window::Window;
|
use winit_24::window::Window;
|
||||||
|
|
||||||
use crate::camera::{Camera, CameraController};
|
use crate::camera::{Camera, CameraController};
|
||||||
use crate::components::{Mesh, Position, RangeCopy};
|
use crate::components::{ImguiWindow, LoopState, Mesh, Position, RangeCopy};
|
||||||
use crate::current_ui;
|
use crate::current_ui;
|
||||||
use crate::geometry::{load_obj, Vertex};
|
use crate::geometry::{load_obj, Vertex};
|
||||||
use crate::imgui_supp::imgui_support::{ImguiContext, ImguiPlatform};
|
use crate::imgui_supp::imgui_support::{ImguiContext, ImguiPlatform};
|
||||||
use crate::light::{DirectionalLight, LightRaw};
|
use crate::light::{DirectionalLight, LightRaw};
|
||||||
use crate::render::state::{RenderState};
|
use crate::render::state::RenderState;
|
||||||
use crate::render::{push_debug_group_checked, insert_debug_marker_checked, pop_debug_group_checked, EntityUniforms};
|
use crate::render::{
|
||||||
|
insert_debug_marker_checked, pop_debug_group_checked, push_debug_group_checked, EntityUniforms,
|
||||||
|
};
|
||||||
|
|
||||||
#[system]
|
#[system]
|
||||||
#[write_component(Camera)]
|
#[write_component(Camera)]
|
||||||
@@ -53,14 +59,128 @@ pub fn imgui_prepare(
|
|||||||
unsafe { crate::CURRENT_UI = Some(std::mem::transmute(imgui_context.frame())) }
|
unsafe { crate::CURRENT_UI = Some(std::mem::transmute(imgui_context.frame())) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn run_imgui_render_step<G: 'static + Sized + Send + Sync>(world: &mut SubWorld, ui: &Ui) {
|
||||||
|
let mut component_query = <(&G)>::query();
|
||||||
|
let mut window_query = <(&ImguiWindow<G>)>::query();
|
||||||
|
|
||||||
|
let mut window_data = None;
|
||||||
|
let mut window_func = None;
|
||||||
|
for (window) in window_query.iter(world) {
|
||||||
|
window_data = Some((window.window)());
|
||||||
|
window_func = Some(window.func);
|
||||||
|
}
|
||||||
|
|
||||||
|
if window_data.is_some() {
|
||||||
|
let mut v = Vec::new();
|
||||||
|
for (component_state) in component_query.iter(world) {
|
||||||
|
v.push(component_state)
|
||||||
|
}
|
||||||
|
window_data
|
||||||
|
.unwrap()
|
||||||
|
.build(&ui, || (window_func.unwrap())(ui, v));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Go through each "global" window-data component and render it's data
|
||||||
|
#[system]
|
||||||
|
#[write_component(ImguiWindow<ImguiPerformanceProfilerLine>)]
|
||||||
|
#[write_component(ImguiPerformanceProfilerLine)]
|
||||||
|
#[write_component(ImguiWindow<ImguiGenericOutputLine>)]
|
||||||
|
#[write_component(ImguiGenericOutputLine)]
|
||||||
|
pub fn render_imgui(world: &mut SubWorld, #[resource] loop_state: &mut LoopState) {
|
||||||
|
let ui = unsafe { crate::current_ui().unwrap() };
|
||||||
|
|
||||||
|
// Pull out the window associated with this type, and render each of the components in the sytem
|
||||||
|
run_imgui_render_step::<ImguiGenericOutputLine>(world, &ui);
|
||||||
|
run_imgui_render_step::<ImguiPerformanceProfilerLine>(world, &ui);
|
||||||
|
}
|
||||||
|
|
||||||
|
// This would be the shared state for all imgui generic output things
|
||||||
|
pub struct ImguiGenericOutputLine {
|
||||||
|
pub label: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ImguiGenericOutputLine {
|
||||||
|
pub fn new(label: String) -> ImguiGenericOutputLine {
|
||||||
|
ImguiGenericOutputLine { label }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This would be the shared state for all imgui performance window things
|
||||||
|
pub struct ImguiPerformanceProfilerLine {
|
||||||
|
pub label: String,
|
||||||
|
list_of_fps: [f32; 400],
|
||||||
|
index: usize,
|
||||||
|
scale_min: f32,
|
||||||
|
pub scale_max: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ImguiPerformanceProfilerLine {
|
||||||
|
fn add_sample(&mut self, sample: f32) {
|
||||||
|
self.list_of_fps[self.index] = sample;
|
||||||
|
|
||||||
|
if self.index >= 399 {
|
||||||
|
self.scale_max = self.list_of_fps.iter().cloned().fold(0. / 0., f32::max);
|
||||||
|
self.index = 0;
|
||||||
|
} else {
|
||||||
|
self.index += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.scale_max < sample {
|
||||||
|
self.scale_max = sample;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn iter_data_from_head(&self) -> Chain<Iter<f32>, Iter<f32>> {
|
||||||
|
let (left, right) = self.list_of_fps.split_at(self.index);
|
||||||
|
right.iter().chain(left.iter())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn current_average_label(&self) -> (f32, String) {
|
||||||
|
let (left, right) = self.list_of_fps.split_at(self.index);
|
||||||
|
(
|
||||||
|
(left
|
||||||
|
.iter()
|
||||||
|
.rev()
|
||||||
|
.chain(right.iter().rev())
|
||||||
|
.take(50)
|
||||||
|
.sum::<f32>()
|
||||||
|
/ 50.0),
|
||||||
|
"FPS".to_string(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new(label: String) -> ImguiPerformanceProfilerLine {
|
||||||
|
ImguiPerformanceProfilerLine {
|
||||||
|
label,
|
||||||
|
list_of_fps: [0.0; 400],
|
||||||
|
index: 0,
|
||||||
|
scale_min: 0.0,
|
||||||
|
scale_max: 0.0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[system]
|
||||||
|
#[write_component(ImguiPerformanceProfilerLine)]
|
||||||
|
pub fn render_performance_flag(world: &mut SubWorld, #[resource] loop_state: &mut LoopState) {
|
||||||
|
let delta_time = loop_state.delta_time.as_secs_f32();
|
||||||
|
|
||||||
|
let mut query = <(&mut ImguiPerformanceProfilerLine)>::query();
|
||||||
|
for (mut profiler) in query.iter_mut(world) {
|
||||||
|
profiler.add_sample(delta_time);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[system]
|
#[system]
|
||||||
#[write_component(Camera)]
|
#[write_component(Camera)]
|
||||||
#[write_component(Position)]
|
#[write_component(Position)]
|
||||||
|
#[write_component(ImguiPerformanceProfilerLine)]
|
||||||
#[write_component(Point3<f32>)]
|
#[write_component(Point3<f32>)]
|
||||||
#[write_component(Mesh)]
|
#[write_component(Mesh)]
|
||||||
#[write_component(DirectionalLight)]
|
#[write_component(DirectionalLight)]
|
||||||
pub fn render_test(
|
pub fn render_test(
|
||||||
world: &mut SubWorld,
|
world: &mut SubWorld,
|
||||||
|
#[resource] loop_state: &mut LoopState,
|
||||||
#[resource] renderer: &mut RenderState,
|
#[resource] renderer: &mut RenderState,
|
||||||
#[resource] winit_window: &mut Window,
|
#[resource] winit_window: &mut Window,
|
||||||
#[resource] imgui_context: &mut Arc<Mutex<ImguiContext>>,
|
#[resource] imgui_context: &mut Arc<Mutex<ImguiContext>>,
|
||||||
@@ -72,7 +192,6 @@ pub fn render_test(
|
|||||||
|
|
||||||
push_debug_group_checked("start render function", &mut encoder);
|
push_debug_group_checked("start render function", &mut encoder);
|
||||||
|
|
||||||
|
|
||||||
let frame = renderer.get_current_frame();
|
let frame = renderer.get_current_frame();
|
||||||
|
|
||||||
// Update the camera uniform buffers, need to make it support selection of
|
// Update the camera uniform buffers, need to make it support selection of
|
||||||
@@ -132,8 +251,10 @@ pub fn render_test(
|
|||||||
let mut query = <(&mut DirectionalLight, &mut Position)>::query();
|
let mut query = <(&mut DirectionalLight, &mut Position)>::query();
|
||||||
|
|
||||||
for (i, (light, pos)) in query.iter_mut(world).enumerate() {
|
for (i, (light, pos)) in query.iter_mut(world).enumerate() {
|
||||||
insert_debug_marker_checked(&format!("shadow pass {} (light at position {:?})", i, pos), &mut encoder);
|
insert_debug_marker_checked(
|
||||||
|
&format!("shadow pass {} (light at position {:?})", i, pos),
|
||||||
|
&mut encoder,
|
||||||
|
);
|
||||||
|
|
||||||
// The light uniform buffer already has the projection,
|
// The light uniform buffer already has the projection,
|
||||||
// let's just copy it over to the shadow uniform buffer.
|
// let's just copy it over to the shadow uniform buffer.
|
||||||
@@ -147,7 +268,6 @@ pub fn render_test(
|
|||||||
|
|
||||||
insert_debug_marker_checked("render entities", &mut encoder);
|
insert_debug_marker_checked("render entities", &mut encoder);
|
||||||
|
|
||||||
|
|
||||||
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||||
label: Some("render pass"),
|
label: Some("render pass"),
|
||||||
color_attachments: &[],
|
color_attachments: &[],
|
||||||
@@ -221,17 +341,8 @@ pub fn render_test(
|
|||||||
let mut imgui_context = &mut imgui_context.lock().unwrap().context;
|
let mut imgui_context = &mut imgui_context.lock().unwrap().context;
|
||||||
let mut imgui_platform = &mut imgui_platform.lock().unwrap().platform;
|
let mut imgui_platform = &mut imgui_platform.lock().unwrap().platform;
|
||||||
|
|
||||||
//imgui_state.context.io_mut().update_delta_time(Duration::new(0,160));
|
|
||||||
let ui = unsafe { crate::current_ui().unwrap() };
|
let ui = unsafe { crate::current_ui().unwrap() };
|
||||||
|
|
||||||
let window = imgui::Window::new(im_str!("Hello too"));
|
|
||||||
window
|
|
||||||
.size([400.0, 100.0], Condition::FirstUseEver)
|
|
||||||
.position([50.0, 50.0], Condition::FirstUseEver)
|
|
||||||
.build(&ui, || {
|
|
||||||
ui.text(im_str!("Frametime: {:?}", 10.0));
|
|
||||||
});
|
|
||||||
|
|
||||||
// ui.show_demo_window(&mut true);
|
// ui.show_demo_window(&mut true);
|
||||||
imgui_platform.prepare_render(&ui, &winit_window);
|
imgui_platform.prepare_render(&ui, &winit_window);
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ use std::path::PathBuf;
|
|||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
use cgmath::{Deg, Euler, Point3, Quaternion, Rad};
|
use cgmath::{Deg, Euler, Point3, Quaternion, Rad};
|
||||||
use imgui::FontSource;
|
use imgui::*;
|
||||||
|
use imgui::{Condition, FontSource, Ui};
|
||||||
use legion::systems::CommandBuffer;
|
use legion::systems::CommandBuffer;
|
||||||
use legion::world::SubWorld;
|
use legion::world::SubWorld;
|
||||||
use legion::IntoQuery;
|
use legion::IntoQuery;
|
||||||
@@ -17,16 +18,102 @@ use rapier3d::na::{Isometry3, Vector, Vector3};
|
|||||||
use rapier3d::pipeline::{ChannelEventCollector, PhysicsPipeline};
|
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, ImguiWindow, LoopState, Mesh, Physics, Position};
|
||||||
use crate::geometry::RawMesh;
|
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::render::system::{ImguiGenericOutputLine, ImguiPerformanceProfilerLine};
|
||||||
use crate::runtime::state::RuntimeState;
|
use crate::runtime::state::RuntimeState;
|
||||||
|
|
||||||
|
pub fn quad_color(color: [f32; 4]) -> [[f32; 4]; 4] {
|
||||||
|
[color, color, color, color]
|
||||||
|
}
|
||||||
|
|
||||||
#[system]
|
#[system]
|
||||||
#[write_component(Mesh)]
|
#[write_component(Mesh)]
|
||||||
pub fn runtime_load(world: &mut SubWorld, #[resource] runtime_state: &mut RuntimeState) {
|
pub fn runtime_load(
|
||||||
|
cmd: &mut CommandBuffer,
|
||||||
|
world: &mut SubWorld,
|
||||||
|
#[resource] runtime_state: &mut RuntimeState,
|
||||||
|
) {
|
||||||
runtime_state.preload_meshes(PathBuf::from("./resources"));
|
runtime_state.preload_meshes(PathBuf::from("./resources"));
|
||||||
|
|
||||||
|
let entity: Entity = cmd.push((ImguiWindow {
|
||||||
|
// a window that does everything for the performance profiler
|
||||||
|
window: || {
|
||||||
|
imgui::Window::new(im_str!("Performance Profiler"))
|
||||||
|
.size([400.0, 200.0], Condition::FirstUseEver)
|
||||||
|
.position([10.0, 10.0], Condition::FirstUseEver)
|
||||||
|
},
|
||||||
|
func: |ui: &Ui, a: Vec<&ImguiPerformanceProfilerLine>| {
|
||||||
|
// ui.text(im_str!("Performance Graph"));
|
||||||
|
|
||||||
|
let draw_list = ui.get_window_draw_list();
|
||||||
|
let top_left = ui.cursor_screen_pos();
|
||||||
|
let region_size = ui.content_region_avail();
|
||||||
|
|
||||||
|
let region_size = [region_size[0] * 0.80, region_size[1]];
|
||||||
|
|
||||||
|
// Fill rect
|
||||||
|
let qcolor = quad_color([0.5, 0.5, 1.0, 0.1]);
|
||||||
|
draw_list.add_rect_filled_multicolor(
|
||||||
|
top_left,
|
||||||
|
[top_left[0] + (region_size[0]), top_left[1] + region_size[1]],
|
||||||
|
qcolor[0],
|
||||||
|
qcolor[1],
|
||||||
|
qcolor[2],
|
||||||
|
qcolor[3],
|
||||||
|
);
|
||||||
|
|
||||||
|
for profiler_line in a {
|
||||||
|
let x_scale = (region_size[0]) / 400.0;
|
||||||
|
let y_scale = region_size[1] / profiler_line.scale_max;
|
||||||
|
profiler_line
|
||||||
|
.iter_data_from_head()
|
||||||
|
.fold((0, 0.0f32), |accum, &fps_val| {
|
||||||
|
let x1 = accum.0 as f32 * x_scale + top_left[0];
|
||||||
|
let y1 = top_left[1] + region_size[1] - accum.1 * y_scale;
|
||||||
|
let x2 = (accum.0 as f32 + 1.0) * x_scale + top_left[0];
|
||||||
|
let y2 = top_left[1] + region_size[1] - fps_val * y_scale;
|
||||||
|
let p1 = [x1, y1];
|
||||||
|
let p2 = [x2, y2];
|
||||||
|
draw_list
|
||||||
|
.add_line(p1, p2, [1.0, 1.0, 0.0, 0.8])
|
||||||
|
.thickness(1.0)
|
||||||
|
.build();
|
||||||
|
(accum.0 + 1, fps_val)
|
||||||
|
});
|
||||||
|
|
||||||
|
let text_x = (region_size[0] + top_left[0]);
|
||||||
|
let text_y = top_left[1] + region_size[1]
|
||||||
|
- profiler_line.current_average_label().0 * y_scale;
|
||||||
|
draw_list.add_text(
|
||||||
|
[text_x, text_y],
|
||||||
|
[1.0, 1.0, 0.0, 1.0],
|
||||||
|
format!(
|
||||||
|
"{} {:.0}",
|
||||||
|
profiler_line.current_average_label().1,
|
||||||
|
1.0 / profiler_line.current_average_label().0
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},));
|
||||||
|
let entity: Entity = cmd.push((ImguiPerformanceProfilerLine::new("RenderFPS".to_string()),));
|
||||||
|
|
||||||
|
let entity: Entity = cmd.push((ImguiWindow {
|
||||||
|
// a window that does everything for the performance profiler
|
||||||
|
window: || {
|
||||||
|
imgui::Window::new(im_str!("Generic Output"))
|
||||||
|
.size([400.0, 500.0], Condition::FirstUseEver)
|
||||||
|
.position([50.0, 250.0], Condition::FirstUseEver)
|
||||||
|
},
|
||||||
|
func: |ui: &Ui, a: Vec<&ImguiGenericOutputLine>| {
|
||||||
|
for label in a {
|
||||||
|
ui.text(im_str!("{}", label.label));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[system]
|
#[system]
|
||||||
@@ -88,6 +175,7 @@ pub fn runtime_spawn(
|
|||||||
collider: collider,
|
collider: collider,
|
||||||
collider_handle: None,
|
collider_handle: None,
|
||||||
},
|
},
|
||||||
|
ImguiGenericOutputLine::new("wahoo! from a physics entity".to_string()),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
"Terrain" => {
|
"Terrain" => {
|
||||||
@@ -153,6 +241,7 @@ pub fn runtime_spawn(
|
|||||||
pitch: Rad(PI / 2.0 + 25.0),
|
pitch: Rad(PI / 2.0 + 25.0),
|
||||||
},
|
},
|
||||||
CameraController::new(5.0, 1.0),
|
CameraController::new(5.0, 1.0),
|
||||||
|
ImguiGenericOutputLine::new("wahoo! from a camera".to_string()),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
"Light" => {
|
"Light" => {
|
||||||
|
|||||||
Reference in New Issue
Block a user