1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
pub mod game_object;
use std::any::Any;
use vulkano::{swapchain::SwapchainAcquireFuture, sync::GpuFuture};
use winit::window::Window;
use crate::graphics::{draw_pass_manager::DrawPassManager, frame_system::FrameSystem, lighting_pass_manager::LightingPassManager};
use game_object::camera::Camera;
use {
crate::{
scene::{
game_object::GameObject,
},
registration::{
relation::{Parent, ParentWrapper},
named::Named
},
scripting::executor::Spawner,
components::{
triangle_mesh::TriangleMesh
},
event::UserEvent,
graphics::{
Drawable,
},
},
feo_math::{
linear_algebra::matrix4::Matrix4,
utils::space::Space
},
std::{
mem,
sync::{Arc, RwLock}
},
winit::event::Event
};
#[derive(Clone, Parent, Drawable, Debug)]
pub struct Scene {
pub worldspace: Space,
pub children: Vec<Arc<RwLock<dyn GameObject>>>,
pub main_camera: Option<Arc<RwLock<dyn Camera>>>,
}
impl Scene {
const NAME: &'static str = "Scene";
pub fn new(worldspace: Option<Space>) -> Arc<RwLock<Self>>{
Arc::new(RwLock::new(Scene{
worldspace: worldspace.unwrap_or_else(|| Space::new(None, None, None)),
children: Vec::new(),
main_camera: None,
}))
}
pub fn set_main_camera(&mut self, main_camera: Arc<RwLock<impl Camera>>){
self.main_camera = Some(main_camera as Arc<RwLock<dyn Camera>>);
}
pub fn spawn_script_cores(&self, spawner: Spawner){
self.children.clone().into_iter().for_each(|game_object| {
let game_object_template = game_object.clone();
game_object_template.write().unwrap().spawn_script_core(game_object, spawner.clone());
});
}
pub fn spawn_script_handlers(&self, spawner: Spawner, event: Event<'static, UserEvent<Arc<dyn Any + Send + Sync>>>){
self.children.clone().into_iter().for_each(|game_object| {
game_object.clone().write().unwrap().spawn_script_handler(game_object, spawner.clone(), event.clone());
});
}
#[inline]
pub fn render(&self,
this: Arc<RwLock<Scene>>,
frame_system: &mut FrameSystem,
image_num: usize,
acquire_future: SwapchainAcquireFuture<Window>,
previous_frame_end: &mut Option<Box<dyn GpuFuture>> ) -> Box<dyn GpuFuture> {
frame_system.draw_pass_manager.clear();
frame_system.lighting_pass_manager.clear();
self.load_into_managers(ParentWrapper::Scene(this), &mut frame_system.draw_pass_manager, &mut frame_system.lighting_pass_manager);
let main_camera = self.main_camera.clone().expect("No camera defined");
frame_system.draw_pass_manager.recreate_camera_set(main_camera.clone());
let main_camera_read = main_camera.read().unwrap();
let future = previous_frame_end.take().unwrap().join(acquire_future);
let mut builder = frame_system.pass_builder(
future,
image_num,
main_camera_read.build_projection().inverse().transpose(),
main_camera_read.get_inversed_subspace()
);
builder.build()
}
#[inline]
pub fn build_space(&mut self) -> Matrix4<f32>{
self.worldspace.build()
}
}
impl Named for Scene {
fn get_name(&self) -> &str { Scene::NAME }
}