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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
#![warn(missing_docs)]
//! Contains all structures and methods to create and manage 3D scenes.
//!
//! A `Scene` is a container for graph nodes, animations and physics.
pub mod accel;
pub mod base;
pub mod camera;
pub mod collider;
pub mod debug;
pub mod decal;
pub mod dim2;
pub mod graph;
pub mod joint;
pub mod legacy_physics;
pub mod light;
pub mod mesh;
pub mod node;
pub mod particle_system;
pub mod rigidbody;
pub mod sprite;
pub mod terrain;
pub mod transform;
pub mod variable;
pub mod visibility;
use crate::scene::base::legacy::PhysicsBinding;
use crate::scene::legacy_physics::dim3::RigidBodyHandle;
use crate::{
animation::AnimationContainer,
core::{
algebra::{UnitQuaternion, Vector2},
color::Color,
instant,
pool::{Handle, Pool, PoolIterator, PoolIteratorMut, Ticket},
sstorage::ImmutableString,
visitor::{Visit, VisitError, VisitResult, Visitor},
},
engine::{resource_manager::ResourceManager, PhysicsBinder},
material::{shader::SamplerFallback, PropertyValue},
resource::texture::Texture,
scene::{
base::BaseBuilder,
collider::{ColliderBuilder, ColliderShape, GeometrySource},
debug::SceneDrawingContext,
graph::{
physics::{
collider_shape_from_native_collider, joint_params_from_native,
PhysicsPerformanceStatistics,
},
Graph,
},
joint::JointBuilder,
legacy_physics::LegacyPhysics,
mesh::buffer::{
VertexAttributeDataType, VertexAttributeDescriptor, VertexAttributeUsage,
VertexWriteTrait,
},
node::Node,
rigidbody::{RigidBodyBuilder, RigidBodyType},
transform::TransformBuilder,
},
sound::{context::SoundContext, engine::SoundEngine},
utils::{lightmap::Lightmap, log::Log, log::MessageKind, navmesh::Navmesh},
};
use fxhash::FxHashMap;
use std::{
fmt::{Display, Formatter},
ops::{Index, IndexMut},
path::Path,
sync::{Arc, Mutex},
};
/// A container for navigational meshes.
#[derive(Default, Clone, Debug)]
pub struct NavMeshContainer {
pool: Pool<Navmesh>,
}
impl NavMeshContainer {
/// Adds new navigational mesh to the container and returns its handle.
pub fn add(&mut self, navmesh: Navmesh) -> Handle<Navmesh> {
self.pool.spawn(navmesh)
}
/// Removes navigational mesh by its handle.
pub fn remove(&mut self, handle: Handle<Navmesh>) -> Navmesh {
self.pool.free(handle)
}
/// Creates new immutable iterator.
pub fn iter(&self) -> impl Iterator<Item = &Navmesh> {
self.pool.iter()
}
/// Creates new immutable iterator.
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Navmesh> {
self.pool.iter_mut()
}
/// Creates a handle to navmesh from its index.
pub fn handle_from_index(&self, i: u32) -> Handle<Navmesh> {
self.pool.handle_from_index(i)
}
/// Destroys all navmeshes. All handles will become invalid.
pub fn clear(&mut self) {
self.pool.clear()
}
/// Checks if given handle is valid.
pub fn is_valid_handle(&self, handle: Handle<Navmesh>) -> bool {
self.pool.is_valid_handle(handle)
}
}
impl Index<Handle<Navmesh>> for NavMeshContainer {
type Output = Navmesh;
fn index(&self, index: Handle<Navmesh>) -> &Self::Output {
&self.pool[index]
}
}
impl IndexMut<Handle<Navmesh>> for NavMeshContainer {
fn index_mut(&mut self, index: Handle<Navmesh>) -> &mut Self::Output {
&mut self.pool[index]
}
}
impl Visit for NavMeshContainer {
fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
visitor.enter_region(name)?;
self.pool.visit("Pool", visitor)?;
visitor.leave_region()
}
}
/// See module docs.
#[derive(Debug)]
pub struct Scene {
/// Graph is main container for all scene nodes. It calculates global transforms for nodes,
/// updates them and performs all other important work. See `graph` module docs for more
/// info.
pub graph: Graph,
/// Animations container controls all animation on scene. Each animation can have tracks which
/// has handles to graph nodes. See `animation` module docs for more info.
pub animations: AnimationContainer,
/// Texture to draw scene to. If empty, scene will be drawn on screen directly.
/// It is useful to "embed" some scene into other by drawing a quad with this
/// texture. This can be used to make in-game video conference - you can make
/// separate scene with your characters and draw scene into texture, then in
/// main scene you can attach this texture to some quad which will be used as
/// monitor. Other usage could be previewer of models, like pictogram of character
/// in real-time strategies, in other words there are plenty of possible uses.
pub render_target: Option<Texture>,
/// Drawing context for simple graphics.
pub drawing_context: SceneDrawingContext,
/// A sound context that holds all sound sources, effects, etc. belonging to the scene.
pub sound_context: SoundContext,
/// A container for navigational meshes.
pub navmeshes: NavMeshContainer,
/// Current lightmap.
lightmap: Option<Lightmap>,
/// Performance statistics from last `update` call.
pub performance_statistics: PerformanceStatistics,
/// Color of ambient lighting.
pub ambient_lighting_color: Color,
/// Whether the scene will be updated and rendered or not. Default is true.
/// This flag allowing you to build a scene manager for your game. For example,
/// you may have a scene for menu and one per level. Menu's scene is persistent,
/// however you don't want it to be updated and renderer while you have a level
/// loaded and playing a game. When you're start playing, just set `enabled` flag
/// to false for menu's scene and when you need to open a menu - set it to true and
/// set `enabled` flag to false for level's scene.
pub enabled: bool,
// Legacy physics world.
legacy_physics: LegacyPhysics,
// Legacy physics binder.
legacy_physics_binder: PhysicsBinder<Node, RigidBodyHandle>,
}
impl Default for Scene {
fn default() -> Self {
Self {
graph: Default::default(),
animations: Default::default(),
legacy_physics: Default::default(),
legacy_physics_binder: Default::default(),
render_target: None,
lightmap: None,
drawing_context: Default::default(),
sound_context: Default::default(),
navmeshes: Default::default(),
performance_statistics: Default::default(),
ambient_lighting_color: Color::opaque(100, 100, 100),
enabled: true,
}
}
}
fn map_texture(tex: Option<Texture>, rm: ResourceManager) -> Option<Texture> {
if let Some(shallow_texture) = tex {
let shallow_texture = shallow_texture.state();
Some(rm.request_texture(shallow_texture.path()))
} else {
None
}
}
/// A structure that holds times that specific update step took.
#[derive(Clone, Default, Debug)]
pub struct PerformanceStatistics {
/// Physics performance statistics.
pub physics: PhysicsPerformanceStatistics,
/// 2D Physics performance statistics.
pub physics2d: PhysicsPerformanceStatistics,
/// A time (in seconds) which was required to update graph.
pub graph_update_time: f32,
/// A time (in seconds) which was required to update animations.
pub animations_update_time: f32,
/// A time (in seconds) which was required to render sounds.
pub sound_update_time: f32,
}
impl Display for PerformanceStatistics {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}\nGraph: {} ms\nAnimations: {} ms\nSounds: {} ms",
self.physics,
self.graph_update_time * 1000.0,
self.animations_update_time * 1000.0,
self.sound_update_time * 1000.0
)
}
}
impl Scene {
/// Creates new scene with single root node.
///
/// # Notes
///
/// This method differs from Default trait implementation! Scene::default() creates
/// empty graph with no nodes.
#[inline]
pub fn new() -> Self {
Self {
// Graph must be created with `new` method because it differs from `default`
graph: Graph::new(),
legacy_physics: Default::default(),
animations: Default::default(),
legacy_physics_binder: Default::default(),
render_target: None,
lightmap: None,
drawing_context: Default::default(),
sound_context: SoundContext::new(),
navmeshes: Default::default(),
performance_statistics: Default::default(),
ambient_lighting_color: Color::opaque(100, 100, 100),
enabled: true,
}
}
/// Tries to load scene from given file. File can contain any scene in native engine format.
/// Such scenes can be made in rusty editor.
pub async fn from_file<P: AsRef<Path>>(
path: P,
resource_manager: ResourceManager,
) -> Result<Self, VisitError> {
let mut scene = Scene::default();
{
let mut visitor = Visitor::load_binary(path.as_ref()).await?;
scene.visit("Scene", &mut visitor)?;
}
// Collect all used resources and wait for them.
let mut resources = Vec::new();
for node in scene.graph.linear_iter_mut() {
if let Some(shallow_resource) = node.resource.clone() {
let resource = resource_manager
.clone()
.request_model(&shallow_resource.state().path());
node.resource = Some(resource.clone());
resources.push(resource);
}
}
let _ = crate::core::futures::future::join_all(resources).await;
// Restore pointers to resources. Scene saves only paths to resources, here we must
// find real resources instead.
for node in scene.graph.linear_iter_mut() {
match node {
Node::Mesh(mesh) => {
for surface in mesh.surfaces_mut() {
surface.material().lock().resolve(resource_manager.clone());
}
}
Node::Sprite(sprite) => {
sprite.set_texture(map_texture(sprite.texture(), resource_manager.clone()));
}
Node::ParticleSystem(particle_system) => {
particle_system.set_texture(map_texture(
particle_system.texture(),
resource_manager.clone(),
));
}
Node::Camera(camera) => {
camera.set_environment(map_texture(
camera.environment_map(),
resource_manager.clone(),
));
if let Some(skybox) = camera.skybox_mut() {
skybox.bottom =
map_texture(skybox.bottom.clone(), resource_manager.clone());
skybox.top = map_texture(skybox.top.clone(), resource_manager.clone());
skybox.left = map_texture(skybox.left.clone(), resource_manager.clone());
skybox.right = map_texture(skybox.right.clone(), resource_manager.clone());
skybox.front = map_texture(skybox.front.clone(), resource_manager.clone());
skybox.back = map_texture(skybox.back.clone(), resource_manager.clone());
}
}
Node::Terrain(terrain) => {
for layer in terrain.layers() {
layer.material.lock().resolve(resource_manager.clone());
}
}
Node::Rectangle(rectangle) => {
rectangle.set_texture(map_texture(
rectangle.texture_value(),
resource_manager.clone(),
));
}
Node::Decal(decal) => {
decal.set_diffuse_texture(map_texture(
decal.diffuse_texture_value(),
resource_manager.clone(),
));
decal.set_normal_texture(map_texture(
decal.normal_texture_value(),
resource_manager.clone(),
));
}
_ => (),
}
}
if let Some(lightmap) = scene.lightmap.as_mut() {
for entries in lightmap.map.values_mut() {
for entry in entries.iter_mut() {
entry.texture = map_texture(entry.texture.clone(), resource_manager.clone());
}
}
}
// We have to wait until skybox textures are all loaded, because we need to read their data
// to re-create cube map.
let mut skybox_textures = Vec::new();
for node in scene.graph.linear_iter() {
if let Node::Camera(camera) = node {
if let Some(skybox) = camera.skybox_ref() {
skybox_textures.extend(skybox.textures().iter().filter_map(|t| t.clone()));
}
}
}
crate::core::futures::future::join_all(skybox_textures).await;
// And do resolve to extract correct graphical data and so on.
scene.resolve();
Ok(scene)
}
/// Removes node from scene with all associated entities, like animations etc. This method
/// should be used all times instead of [Graph::remove_node](crate::scene::graph::Graph::remove_node).
///
/// # Panics
///
/// Panics if handle is invalid.
pub fn remove_node(&mut self, handle: Handle<Node>) {
for descendant in self.graph.traverse_handle_iter(handle) {
// Remove all associated animations.
self.animations.retain(|animation| {
for track in animation.get_tracks() {
if track.get_node() == descendant {
return false;
}
}
true
});
}
self.graph.remove_node(handle)
}
fn convert_legacy_physics(&mut self) {
// Convert rigid bodies and colliders.
let mut body_map = FxHashMap::default();
for (node, body_handle) in self.legacy_physics_binder.forward_map() {
let body_ref = if let Some(body_ref) = self.legacy_physics.bodies.get(body_handle) {
body_ref
} else {
continue;
};
let [x_rotation_locked, y_rotation_locked, z_rotation_locked] =
body_ref.is_rotation_locked();
let body_node_handle = RigidBodyBuilder::new(
BaseBuilder::new()
.with_name("Rigid Body")
.with_local_transform(
TransformBuilder::new()
.with_local_position(body_ref.position().translation.vector)
.with_local_rotation(body_ref.position().rotation)
.build(),
),
)
.with_body_type(RigidBodyType::from(body_ref.body_type()))
.with_mass(body_ref.mass())
.with_ang_vel(*body_ref.angvel())
.with_lin_vel(*body_ref.linvel())
.with_lin_damping(body_ref.linear_damping())
.with_ang_damping(body_ref.angular_damping())
.with_x_rotation_locked(x_rotation_locked)
.with_y_rotation_locked(y_rotation_locked)
.with_z_rotation_locked(z_rotation_locked)
.with_translation_locked(body_ref.is_translation_locked())
.with_ccd_enabled(body_ref.is_ccd_enabled())
.build(&mut self.graph);
body_map.insert(body_handle, body_node_handle);
for c in body_ref.colliders() {
let collider_ref =
if let Some(collider_ref) = self.legacy_physics.colliders.native_ref(*c) {
collider_ref
} else {
continue;
};
let mut shape = collider_shape_from_native_collider(collider_ref.shape());
let name = match shape {
ColliderShape::Ball(_) => "Ball Collider",
ColliderShape::Cylinder(_) => "Cylinder Collider",
ColliderShape::Cone(_) => "Cone Collider",
ColliderShape::Cuboid(_) => "Cuboid Collider",
ColliderShape::Capsule(_) => "Capsule Collider",
ColliderShape::Segment(_) => "Segment Collider",
ColliderShape::Triangle(_) => "Triangle Collider",
ColliderShape::Trimesh(_) => "Trimesh Collider",
ColliderShape::Heightfield(_) => "Heightfield Collider",
ColliderShape::Polyhedron(_) => "Convex Polyhedron",
};
// Trimesh and heightfield needs extra care.
match shape {
ColliderShape::Trimesh(ref mut trimesh) => {
trimesh.sources = self
.graph
.traverse_handle_iter(*node)
.filter(|h| self.graph[*h].is_mesh())
.map(GeometrySource)
.collect::<Vec<_>>();
}
ColliderShape::Heightfield(ref mut heightfield) => {
heightfield.geometry_source = GeometrySource(*node);
}
_ => (),
}
let collider_handle = ColliderBuilder::new(
BaseBuilder::new().with_name(name).with_local_transform(
TransformBuilder::new()
.with_local_position(
collider_ref
.position_wrt_parent()
.map(|p| p.translation.vector)
.unwrap_or_default(),
)
.with_local_rotation(
collider_ref
.position_wrt_parent()
.map(|p| p.rotation)
.unwrap_or_default(),
)
.build(),
),
)
.with_friction_combine_rule(collider_ref.friction_combine_rule().into())
.with_restitution_combine_rule(collider_ref.restitution_combine_rule().into())
.with_shape(shape)
.with_sensor(collider_ref.is_sensor())
.with_restitution(collider_ref.restitution())
.with_density(collider_ref.density())
.with_collision_groups(collider_ref.collision_groups().into())
.with_solver_groups(collider_ref.solver_groups().into())
.with_friction(collider_ref.friction())
.build(&mut self.graph);
self.graph.link_nodes(collider_handle, body_node_handle);
}
let node_ref = &mut self.graph[*node];
node_ref
.local_transform_mut()
.set_position(Default::default())
.set_rotation(UnitQuaternion::default());
let parent = node_ref.parent();
match node_ref.physics_binding {
PhysicsBinding::NodeWithBody => {
self.graph.link_nodes(*node, body_node_handle);
self.graph.link_nodes(body_node_handle, parent);
}
PhysicsBinding::BodyWithNode => {
self.graph.link_nodes(body_node_handle, *node);
}
}
}
// Convert joints.
for joint in self.legacy_physics.joints.iter() {
let body1 = if let Some(body1) = self
.legacy_physics
.bodies
.handle_map()
.key_of(&joint.body1)
.and_then(|h| body_map.get(h))
{
*body1
} else {
continue;
};
let body2 = if let Some(body2) = self
.legacy_physics
.bodies
.handle_map()
.key_of(&joint.body2)
.and_then(|h| body_map.get(h))
{
*body2
} else {
continue;
};
let joint_handle = JointBuilder::new(BaseBuilder::new())
.with_params(joint_params_from_native(&joint.params))
.with_body1(body1)
.with_body2(body2)
.build(&mut self.graph);
self.graph.link_nodes(joint_handle, body1);
}
}
pub(in crate) fn resolve(&mut self) {
Log::writeln(MessageKind::Information, "Starting resolve...".to_owned());
self.graph.resolve();
self.animations.resolve(&self.graph);
self.graph.update_hierarchical_data();
self.legacy_physics
.resolve(&self.legacy_physics_binder, &self.graph, None);
self.convert_legacy_physics();
// Re-apply lightmap if any. This has to be done after resolve because we must patch surface
// data at this stage, but if we'd do this before we wouldn't be able to do this because
// meshes contains invalid surface data.
if let Some(lightmap) = self.lightmap.as_mut() {
// Patch surface data first. To do this we gather all surface data instances and
// look in patch data if we have patch for data.
let mut unique_data_set = FxHashMap::default();
for &handle in lightmap.map.keys() {
if let Node::Mesh(mesh) = &mut self.graph[handle] {
for surface in mesh.surfaces() {
let data = surface.data();
let key = &*data as *const _ as u64;
unique_data_set.entry(key).or_insert(data);
}
}
}
for (_, data) in unique_data_set.into_iter() {
let mut data = data.lock();
if let Some(patch) = lightmap.patches.get(&data.content_hash()) {
if !data
.vertex_buffer
.has_attribute(VertexAttributeUsage::TexCoord1)
{
data.vertex_buffer
.modify()
.add_attribute(
VertexAttributeDescriptor {
usage: VertexAttributeUsage::TexCoord1,
data_type: VertexAttributeDataType::F32,
size: 2,
divisor: 0,
shader_location: 6, // HACK: GBuffer renderer expects it to be at 6
},
Vector2::<f32>::default(),
)
.unwrap();
}
data.geometry_buffer.set_triangles(patch.triangles.clone());
let mut vertex_buffer_mut = data.vertex_buffer.modify();
for &v in patch.additional_vertices.iter() {
vertex_buffer_mut.duplicate(v as usize);
}
assert_eq!(
vertex_buffer_mut.vertex_count() as usize,
patch.second_tex_coords.len()
);
for (mut view, &tex_coord) in vertex_buffer_mut
.iter_mut()
.zip(patch.second_tex_coords.iter())
{
view.write_2_f32(VertexAttributeUsage::TexCoord1, tex_coord)
.unwrap();
}
} else {
Log::writeln(
MessageKind::Warning,
"Failed to get surface data patch while resolving lightmap!\
This means that surface has changed and lightmap must be regenerated!"
.to_owned(),
);
}
}
// Apply textures.
for (&handle, entries) in lightmap.map.iter_mut() {
if let Node::Mesh(mesh) = &mut self.graph[handle] {
for (entry, surface) in entries.iter_mut().zip(mesh.surfaces_mut()) {
if let Err(e) = surface.material().lock().set_property(
&ImmutableString::new("lightmapTexture"),
PropertyValue::Sampler {
value: entry.texture.clone(),
fallback: SamplerFallback::Black,
},
) {
Log::writeln(
MessageKind::Error,
format!(
"Failed to apply light map texture to material. Reason {:?}",
e
),
)
}
}
}
}
}
Log::writeln(MessageKind::Information, "Resolve succeeded!".to_owned());
}
/// Tries to set new lightmap to scene.
pub fn set_lightmap(&mut self, lightmap: Lightmap) -> Result<Option<Lightmap>, &'static str> {
// Assign textures to surfaces.
for (handle, lightmaps) in lightmap.map.iter() {
if let Node::Mesh(mesh) = &mut self.graph[*handle] {
if mesh.surfaces().len() != lightmaps.len() {
return Err("failed to set lightmap, surface count mismatch");
}
for (surface, entry) in mesh.surfaces_mut().iter_mut().zip(lightmaps) {
// This unwrap() call must never panic in normal conditions, because texture wrapped in Option
// only to implement Default trait to be serializable.
let texture = entry.texture.clone().unwrap();
if let Err(e) = surface.material().lock().set_property(
&ImmutableString::new("lightmapTexture"),
PropertyValue::Sampler {
value: Some(texture),
fallback: SamplerFallback::Black,
},
) {
Log::writeln(
MessageKind::Error,
format!(
"Failed to apply light map texture to material. Reason {:?}",
e
),
)
}
}
}
}
Ok(std::mem::replace(&mut self.lightmap, Some(lightmap)))
}
/// Performs single update tick with given delta time from last frame. Internally
/// it updates physics, animations, and each graph node. In most cases there is
/// no need to call it directly, engine automatically updates all available scenes.
pub fn update(&mut self, frame_size: Vector2<f32>, dt: f32) {
let last = instant::Instant::now();
self.animations.update_animations(dt);
self.performance_statistics.animations_update_time =
(instant::Instant::now() - last).as_secs_f32();
let last = instant::Instant::now();
self.graph.update(frame_size, dt);
self.performance_statistics.physics = self.graph.physics.performance_statistics.clone();
self.performance_statistics.physics2d = self.graph.physics2d.performance_statistics.clone();
self.performance_statistics.graph_update_time =
(instant::Instant::now() - last).as_secs_f32();
self.performance_statistics.sound_update_time = self
.sound_context
.state()
.full_render_duration()
.as_secs_f32();
}
/// Creates deep copy of a scene, filter predicate allows you to filter out nodes
/// by your criteria.
pub fn clone<F>(&self, filter: &mut F) -> (Self, FxHashMap<Handle<Node>, Handle<Node>>)
where
F: FnMut(Handle<Node>, &Node) -> bool,
{
let (graph, old_new_map) = self.graph.clone(filter);
let mut animations = self.animations.clone();
for animation in animations.iter_mut() {
// Remove all tracks for nodes that were filtered out.
animation.retain_tracks(|track| old_new_map.contains_key(&track.get_node()));
// Remap track nodes.
for track in animation.get_tracks_mut() {
track.set_node(old_new_map[&track.get_node()]);
}
}
(
Self {
graph,
animations,
legacy_physics: Default::default(),
legacy_physics_binder: Default::default(),
// Render target is intentionally not copied, because it does not makes sense - a copy
// will redraw frame completely.
render_target: Default::default(),
lightmap: self.lightmap.clone(),
drawing_context: self.drawing_context.clone(),
sound_context: self.sound_context.deep_clone(),
navmeshes: self.navmeshes.clone(),
performance_statistics: Default::default(),
ambient_lighting_color: self.ambient_lighting_color,
enabled: self.enabled,
},
old_new_map,
)
}
}
impl Visit for Scene {
fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
visitor.enter_region(name)?;
self.graph.visit("Graph", visitor)?;
self.animations.visit("Animations", visitor)?;
self.lightmap.visit("Lightmap", visitor)?;
self.sound_context.visit("SoundContext", visitor)?;
self.navmeshes.visit("NavMeshes", visitor)?;
self.ambient_lighting_color
.visit("AmbientLightingColor", visitor)?;
self.enabled.visit("Enabled", visitor)?;
// Load legacy stuff for backward compatibility.
if visitor.is_reading() {
let _ = self.legacy_physics.visit("Physics", visitor);
let _ = self.legacy_physics_binder.visit("PhysicsBinder", visitor);
}
visitor.leave_region()
}
}
/// Container for scenes in the engine.
#[derive(Default)]
pub struct SceneContainer {
pool: Pool<Scene>,
sound_engine: Arc<Mutex<SoundEngine>>,
}
impl SceneContainer {
pub(in crate) fn new(sound_engine: Arc<Mutex<SoundEngine>>) -> Self {
Self {
pool: Pool::new(),
sound_engine,
}
}
/// Return true if given handle is valid and "points" to "alive" scene.
pub fn is_valid_handle(&self, handle: Handle<Scene>) -> bool {
self.pool.is_valid_handle(handle)
}
/// Returns pair iterator which yields (handle, scene_ref) pairs.
pub fn pair_iter(&self) -> impl Iterator<Item = (Handle<Scene>, &Scene)> {
self.pool.pair_iter()
}
/// Creates new iterator over scenes in container.
#[inline]
pub fn iter(&self) -> PoolIterator<Scene> {
self.pool.iter()
}
/// Creates new mutable iterator over scenes in container.
#[inline]
pub fn iter_mut(&mut self) -> PoolIteratorMut<Scene> {
self.pool.iter_mut()
}
/// Adds new scene into container.
#[inline]
pub fn add(&mut self, scene: Scene) -> Handle<Scene> {
self.sound_engine
.lock()
.unwrap()
.add_context(scene.sound_context.clone());
self.pool.spawn(scene)
}
/// Removes all scenes from container.
#[inline]
pub fn clear(&mut self) {
self.pool.clear()
}
/// Removes given scene from container.
#[inline]
pub fn remove(&mut self, handle: Handle<Scene>) {
self.sound_engine
.lock()
.unwrap()
.remove_context(self.pool[handle].sound_context.clone());
self.pool.free(handle);
}
/// Takes scene from the container and transfers ownership to caller. You must either
/// put scene back using ticket or call `forget_ticket` to make memory used by scene
/// vacant again.
pub fn take_reserve(&mut self, handle: Handle<Scene>) -> (Ticket<Scene>, Scene) {
self.pool.take_reserve(handle)
}
/// Puts scene back using its ticket.
pub fn put_back(&mut self, ticket: Ticket<Scene>, scene: Scene) -> Handle<Scene> {
self.pool.put_back(ticket, scene)
}
/// Forgets ticket of a scene, making place at which ticket points, vacant again.
pub fn forget_ticket(&mut self, ticket: Ticket<Scene>) {
self.pool.forget_ticket(ticket)
}
}
impl Index<Handle<Scene>> for SceneContainer {
type Output = Scene;
#[inline]
fn index(&self, index: Handle<Scene>) -> &Self::Output {
&self.pool[index]
}
}
impl IndexMut<Handle<Scene>> for SceneContainer {
#[inline]
fn index_mut(&mut self, index: Handle<Scene>) -> &mut Self::Output {
&mut self.pool[index]
}
}
impl Visit for SceneContainer {
fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
visitor.enter_region(name)?;
self.pool.visit("Pool", visitor)?;
self.sound_engine.visit("SoundEngine", visitor)?;
visitor.leave_region()
}
}