NoJobEngine 2.0.7
Loading...
Searching...
No Matches
SceneSerializer.cpp
Go to the documentation of this file.
2#include "Engine/Core/Log.h"
9
10#include <algorithm>
11#include <fstream>
12#include <iomanip>
13#include <sstream>
14#include <unordered_map>
15
16namespace NoJob
17{
18 namespace
19 {
20 template<typename T>
21 void V3(std::ostream& o, const T& v)
22 {
23 o << v.x << ' '
24 << v.y << ' '
25 << v.z;
26 }
27
28 bool ReadV3(
29 std::istringstream& s,
30 glm::vec3& v)
31 {
32 return bool(
33 s >> v.x
34 >> v.y
35 >> v.z);
36 }
37 }
38
40 const Scene& scene,
41 const std::filesystem::path& path)
42 {
43 std::ofstream out(path);
44
45 if (!out)
46 return false;
47
48 out << "NOJOB_SCENE 1\n";
49 out << std::setprecision(9);
50
51 auto& mutableScene =
52 const_cast<Scene&>(scene);
53
54 for (Entity e :
55 mutableScene.GetEntities())
56 {
57 const auto& tag =
58 e.GetComponent<TagComponent>();
59
60 const auto& tr =
61 e.GetComponent<TransformComponent>();
62
63 const auto& rel =
64 e.GetComponent<RelationshipComponent>();
65
66 out << "ENTITY "
67 << e.GetComponent<IDComponent>().ID
68 << ' '
69 << std::quoted(tag.Tag)
70 << '\n';
71
72 out << "TRANSFORM ";
73
74 V3(out, tr.Position);
75 out << ' ';
76
77 V3(out, tr.Rotation);
78 out << ' ';
79
80 V3(out, tr.Scale);
81 out << '\n';
82
83 out << "PARENT "
84 << rel.Parent
85 << '\n';
86
87 if (e.HasComponent<LayerComponent>())
88 {
89 const auto& layer = e.GetComponent<LayerComponent>();
90 out << "LAYER " << layer.Layer << '\n';
91 }
92
93 if (e.HasComponent<CameraComponent>())
94 {
95 auto& c =
96 e.GetComponent<CameraComponent>();
97
98 out << "CAMERA "
99 << int(c.ProjectionType) << ' '
100 << c.Primary << ' '
101 << c.PerspectiveFOV << ' '
102 << c.PerspectiveNear << ' '
103 << c.PerspectiveFar << ' '
104 << c.OrthographicSize << ' '
105 << c.OrthographicNear << ' '
106 << c.OrthographicFar
107 << '\n';
108 }
109
110 if (e.HasComponent<DirectionalLightComponent>())
111 {
112 auto& c =
113 e.GetComponent<DirectionalLightComponent>();
114
115 out << "DIRECTIONAL ";
116
117 V3(out, c.Color);
118
119 out << ' '
120 << c.Intensity << ' '
121 << c.CastShadows << ' '
122 << c.ShadowBias
123 << '\n';
124 }
125
126 if (e.HasComponent<PointLightComponent>())
127 {
128 auto& c =
129 e.GetComponent<PointLightComponent>();
130
131 out << "POINT ";
132
133 V3(out, c.Color);
134
135 out << ' '
136 << c.Intensity << ' '
137 << c.Range << ' '
138 << c.CastShadows << ' '
139 << c.ShadowBias
140 << '\n';
141 }
142
143 if (e.HasComponent<SpotLightComponent>())
144 {
145 auto& c =
146 e.GetComponent<SpotLightComponent>();
147
148 out << "SPOT ";
149
150 V3(out, c.Color);
151
152 out << ' '
153 << c.Intensity << ' '
154 << c.Range << ' '
155 << c.InnerAngle << ' '
156 << c.OuterAngle << ' '
157 << c.CastShadows << ' '
158 << c.ShadowBias
159 << '\n';
160 }
161
162 if (e.HasComponent<AudioSourceComponent>())
163 {
164 auto& c =
165 e.GetComponent<AudioSourceComponent>();
166
167 out << "AUDIO_SOURCE_V2 "
168 << std::quoted(c.ClipPath) << ' '
169 << c.PlayOnAwake << ' '
170 << c.Loop << ' '
171 << c.Volume << ' '
172 << c.Pitch << ' '
173 << c.SpatialBlend << ' '
174 << c.MinDistance << ' '
175 << c.MaxDistance << ' '
176 << c.DopplerFactor
177 << '\n';
178 }
179
180 if (e.HasComponent<AudioListenerComponent>())
181 {
182 auto& c =
183 e.GetComponent<AudioListenerComponent>();
184
185 out << "AUDIO_LISTENER "
186 << c.Enabled
187 << '\n';
188 }
189
190 if (e.HasComponent<ParticleSystemComponent>())
191 {
192 auto& c =
193 e.GetComponent<ParticleSystemComponent>();
194
195 out << "PARTICLE_SYSTEM_V2 "
196 << c.Playing << ' '
197 << c.Loop << ' '
198 << c.Duration << ' '
199 << c.StartLifetime << ' '
200 << c.LifetimeRandom << ' '
201 << c.StartSpeed << ' '
202 << c.SpeedRandom << ' '
203 << c.StartSize << ' '
204 << c.SizeRandom << ' '
205 << c.StartColor.r << ' '
206 << c.StartColor.g << ' '
207 << c.StartColor.b << ' '
208 << c.StartColor.a << ' '
209 << c.EndColor.r << ' '
210 << c.EndColor.g << ' '
211 << c.EndColor.b << ' '
212 << c.EndColor.a << ' '
213 << c.EndSizeMultiplier << ' '
214 << c.EmissionRate << ' '
215 << c.MaxParticles << ' '
216 << c.Direction.x << ' '
217 << c.Direction.y << ' '
218 << c.Direction.z << ' '
219 << c.Gravity.x << ' '
220 << c.Gravity.y << ' '
221 << c.Gravity.z << ' '
222 << static_cast<int>(c.Shape) << ' '
223 << c.ShapeRadius << ' '
224 << c.ConeAngle << ' '
225 << static_cast<int>(c.BlendMode) << ' '
226 << std::quoted(c.TexturePath)
227 << '\n';
228 }
229
230 if (e.HasComponent<AnimatorComponent>())
231 {
232 auto& c =
233 e.GetComponent<AnimatorComponent>();
234
235 const auto source =
236 c.Animation
238 c.Animation->SourcePath())
239 .generic_string()
240 : std::string();
241
242 out << "ANIMATOR "
243 << std::quoted(source) << ' '
244 << c.ClipIndex << ' '
245 << c.Speed << ' '
246 << c.Playing << ' '
247 << c.Loop
248 << '\n';
249 }
250
251 auto writeScript = [&](const NativeScriptComponent& c)
252 {
253
254 out << "SCRIPT_V2 "
255 << c.Enabled << ' '
256 << std::quoted(c.ScriptName) << ' '
257 << c.Fields.size()
258 << '\n';
259
260 for (const auto& [name, value] :
261 c.Fields)
262 {
263 out << "SCRIPT_FIELD "
264 << std::quoted(name) << ' '
265 << int(value.Type) << ' ';
266
267 if (value.Type ==
269 {
270 out << value.Float;
271 }
272 else if (value.Type ==
274 {
275 out << value.Int;
276 }
277 else if (value.Type ==
279 {
280 out << value.Bool;
281 }
282 else if (value.Type == ScriptFieldType::String)
283 {
284 out << std::quoted(value.String);
285 }
286 else
287 {
288 V3(out, value.Vec3);
289 }
290
291 out << '\n';
292 }
293 };
294 if (e.HasComponent<NativeScriptComponent>())
295 writeScript(e.GetComponent<NativeScriptComponent>());
296 for (const auto& extra : scene.GetAdditionalNativeScripts(e.GetHandle()))
297 writeScript(extra);
298
299 if (e.HasComponent<RigidbodyComponent>())
300 {
301 auto& c =
302 e.GetComponent<RigidbodyComponent>();
303
304 out << "RIGIDBODY "
305 << int(c.Type) << ' '
306 << c.Mass << ' '
307 << c.UseGravity
308 << '\n';
309 }
310
311 // -------------------------------------------------
312 // V1.8 Native Gameplay AI
313 // -------------------------------------------------
314
315 if (e.HasComponent<NavAgentComponent>())
316 {
317 auto& c =
318 e.GetComponent<NavAgentComponent>();
319
320 out << "NAV_AGENT_V2 "
321 << c.Enabled << ' '
322 << c.Speed << ' '
323 << c.StoppingDistance << ' ';
324
325 V3(out, c.Destination);
326
327 out << ' '
328 << c.HasDestination << ' '
329 << c.RepathInterval
330 << '\n';
331 }
332
333 if (e.HasComponent<PerceptionComponent>())
334 {
335 const auto& c = e.GetComponent<PerceptionComponent>();
336
337 out << "PERCEPTION_V1 "
338 << c.Enabled << ' '
339 << c.DetectionMask << ' '
340 << c.DetectionRadius << ' '
341 << c.FieldOfView << ' '
342 << c.EyeHeight << ' '
343 << c.MemoryDuration << ' '
344 << c.UpdateInterval << ' '
345 << c.DebugDraw << '\n';
346 }
347
348 if (e.HasComponent<BoxColliderComponent>())
349 {
350 auto& c =
351 e.GetComponent<BoxColliderComponent>();
352
353 out << "BOX ";
354
355 V3(out, c.Size);
356
357 out << ' '
358 << c.IsTrigger << ' '
359 << c.Material.Friction << ' '
360 << c.Material.Bounciness
361 << '\n';
362 }
363
364 if (e.HasComponent<SphereColliderComponent>())
365 {
366 auto& c =
367 e.GetComponent<SphereColliderComponent>();
368
369 out << "SPHERE "
370 << c.Radius << ' '
371 << c.IsTrigger << ' '
372 << c.Material.Friction << ' '
373 << c.Material.Bounciness
374 << '\n';
375 }
376
377 if (e.HasComponent<CapsuleColliderComponent>())
378 {
379 auto& c =
380 e.GetComponent<CapsuleColliderComponent>();
381
382 out << "CAPSULE "
383 << c.Radius << ' '
384 << c.Height << ' '
385 << c.IsTrigger << ' '
386 << c.Material.Friction << ' '
387 << c.Material.Bounciness
388 << '\n';
389 }
390
391 if (e.HasComponent<MeshComponent>())
392 {
393 auto mp =
395 e.GetComponent<MeshComponent>()
396 .MeshAsset)
397 .generic_string();
398
399 if (mp.empty())
400 {
401 out << "MESH CUBE\n";
402 }
403 else
404 {
405 out << "MESH "
406 << std::quoted(mp)
407 << '\n';
408 }
409 }
410
411 if (e.HasComponent<MeshRendererComponent>() &&
412 e.GetComponent<MeshRendererComponent>()
414 {
415 auto m =
416 e.GetComponent<MeshRendererComponent>()
417 .MaterialAsset;
418
419 auto col =
420 m->GetColor();
421
422 out << "MATERIAL_V2 "
423 << col.r << ' '
424 << col.g << ' '
425 << col.b << ' '
426 << col.a << ' '
427 << m->Metallic() << ' '
428 << m->Roughness() << ' '
429 << m->AmbientOcclusion() << ' '
430 << m->NormalStrength() << ' ';
431
432 V3(
433 out,
434 m->EmissiveColor());
435
436 out << ' '
437 << m->EmissiveStrength() << ' '
438 << m->UseTexture() << ' '
439 << static_cast<int>(
440 m->SurfaceMode())
441 << ' '
442 << m->AlphaCutoff()
443 << '\n';
444
445 const auto albedo =
447 m->GetTexture())
448 .generic_string();
449
450 const auto normal =
452 m->GetNormalTexture())
453 .generic_string();
454
455 const auto metallic =
457 m->GetMetallicTexture())
458 .generic_string();
459
460 const auto roughness =
462 m->GetRoughnessTexture())
463 .generic_string();
464
465 const auto ao =
467 m->GetAOTexture())
468 .generic_string();
469
470 const auto emissive =
472 m->GetEmissiveTexture())
473 .generic_string();
474
475 out << "TEXTURES "
476 << std::quoted(albedo) << ' '
477 << std::quoted(normal) << ' '
478 << std::quoted(metallic) << ' '
479 << std::quoted(roughness) << ' '
480 << std::quoted(ao) << ' '
481 << std::quoted(emissive)
482 << '\n';
483 }
484
485 out << "END\n";
486 }
487
488 return true;
489 }
490
492 Scene& scene,
493 const std::filesystem::path& path,
494 const std::shared_ptr<Mesh>& defaultMesh,
495 const std::shared_ptr<Material>& defaultMaterial)
496 {
497 std::ifstream in(path);
498
499 if (!in)
500 return false;
501
502 std::string line;
503
504 std::getline(
505 in,
506 line);
507
508 if (line.rfind(
509 "NOJOB_SCENE",
510 0) != 0)
511 {
512 return false;
513 }
514
515 for (auto e :
516 scene.GetEntities())
517 {
518 scene.DestroyEntity(e);
519 }
520
521 Entity current{};
522
523 std::uint32_t parentHandle =
524 0;
525
526 bool materialUseTextureExplicit =
527 false;
528
529 struct ParentRequest
530 {
531 Entity child;
532 std::uint32_t oldParent;
533 };
534
535 std::vector<ParentRequest>
536 parents;
537
538 std::unordered_map<
539 std::uint32_t,
540 Entity>
541 handleMap;
542
543 while (std::getline(
544 in,
545 line))
546 {
547 std::istringstream s(line);
548
549 std::string k;
550
551 s >> k;
552
553 if (k == "ENTITY")
554 {
555 std::uint64_t id;
556 std::string name;
557
558 s >> id
559 >> std::quoted(name);
560
561 current =
562 scene.CreateEntity(
563 name);
564
565 materialUseTextureExplicit =
566 false;
567
568 handleMap[
569 static_cast<
570 std::uint32_t>(id)] =
571 current;
572 }
573 else if (!current)
574 {
575 continue;
576 }
577 else if (k == "LAYER")
578 {
579 std::uint32_t layer = EntityLayers::Default;
580 if (s >> layer)
581 {
582 layer = std::min(layer, EntityLayers::MaxLayers - 1);
583 if (current.HasComponent<LayerComponent>())
584 current.GetComponent<LayerComponent>().Layer = layer;
585 else
586 current.AddComponent<LayerComponent>().Layer = layer;
587 }
588 }
589 else if (k == "TRANSFORM")
590 {
591 auto& c =
592 current.GetComponent<
594
595 ReadV3(
596 s,
597 c.Position);
598
599 ReadV3(
600 s,
601 c.Rotation);
602
603 ReadV3(
604 s,
605 c.Scale);
606 }
607 else if (k == "PARENT")
608 {
609 s >> parentHandle;
610
611 if (parentHandle)
612 {
613 parents.push_back(
614 {
615 current,
616 parentHandle
617 });
618 }
619 }
620 else if (k == "CAMERA")
621 {
623 int p;
624
625 s >> p
626 >> c.Primary
627 >> c.PerspectiveFOV
628 >> c.PerspectiveNear
629 >> c.PerspectiveFar
632 >> c.OrthographicFar;
633
636
637 current.AddComponent<
638 CameraComponent>(c);
639 }
640 else if (k == "DIRECTIONAL")
641 {
643
644 ReadV3(
645 s,
646 c.Color);
647
648 s >> c.Intensity
649 >> c.CastShadows
650 >> c.ShadowBias;
651
652 current.AddComponent<
654 }
655 else if (k == "POINT")
656 {
658
659 ReadV3(
660 s,
661 c.Color);
662
663 s >> c.Intensity
664 >> c.Range
665 >> c.CastShadows
666 >> c.ShadowBias;
667
668 current.AddComponent<
670 }
671 else if (k == "SPOT")
672 {
674
675 ReadV3(
676 s,
677 c.Color);
678
679 s >> c.Intensity
680 >> c.Range
681 >> c.InnerAngle
682 >> c.OuterAngle
683 >> c.CastShadows
684 >> c.ShadowBias;
685
686 current.AddComponent<
688 }
689 else if (k == "AUDIO_SOURCE")
690 {
692
693 s >> std::quoted(c.ClipPath)
694 >> c.PlayOnAwake
695 >> c.Loop
696 >> c.Volume
697 >> c.Pitch;
698
699 current.AddComponent<
701 }
702 else if (k == "AUDIO_SOURCE_V2")
703 {
705
706 s >> std::quoted(c.ClipPath)
707 >> c.PlayOnAwake
708 >> c.Loop
709 >> c.Volume
710 >> c.Pitch
711 >> c.SpatialBlend
712 >> c.MinDistance
713 >> c.MaxDistance
714 >> c.DopplerFactor;
715
716 current.AddComponent<
718 }
719 else if (k == "AUDIO_LISTENER")
720 {
722
723 s >> c.Enabled;
724
725 current.AddComponent<
727 }
728 else if (k == "ANIMATOR")
729 {
730 std::string animationPath;
731
732 int clipIndex = 0;
733 int playing = 1;
734 int loop = 1;
735
736 float speed = 1.0f;
737
738 s >> std::quoted(animationPath)
739 >> clipIndex
740 >> speed
741 >> playing
742 >> loop;
743
744 if (!animationPath.empty())
745 {
746 try
747 {
748 auto animation =
751 ResolveProjectPath(
752 animationPath));
753
754 if (animation &&
755 animation->HasAnimations())
756 {
758
759 c.Animation =
760 std::move(
761 animation);
762
763 c.ClipIndex =
764 std::clamp(
765 clipIndex,
766 0,
767 static_cast<int>(
768 c.Animation
769 ->Clips()
770 .size()) -
771 1);
772
773 c.TimeSeconds =
774 0.0f;
775
776 c.Speed =
777 speed;
778
779 c.Playing =
780 playing != 0;
781
782 c.Loop =
783 loop != 0;
784
785 current.AddComponent<
787 std::move(c));
788 }
789 }
790 catch (...)
791 {
792 Log::Warn(
793 "Scene animation load failed: " +
794 animationPath);
795 }
796 }
797 }
798 else if (k == "PARTICLE_SYSTEM")
799 {
801
802 s >> c.Playing
803 >> c.Loop
804 >> c.Duration
805 >> c.StartLifetime
806 >> c.StartSpeed
807 >> c.StartSize
808 >> c.StartColor.r
809 >> c.StartColor.g
810 >> c.StartColor.b
811 >> c.StartColor.a
812 >> c.EmissionRate
813 >> c.MaxParticles
814 >> c.Direction.x
815 >> c.Direction.y
816 >> c.Direction.z
817 >> c.Gravity.x
818 >> c.Gravity.y
819 >> c.Gravity.z;
820
821 current.AddComponent<
823 }
824 else if (k == "PARTICLE_SYSTEM_V2")
825 {
827
828 int shape = 0;
829 int blend = 0;
830
831 s >> c.Playing
832 >> c.Loop
833 >> c.Duration
834 >> c.StartLifetime
835 >> c.LifetimeRandom
836 >> c.StartSpeed
837 >> c.SpeedRandom
838 >> c.StartSize
839 >> c.SizeRandom
840 >> c.StartColor.r
841 >> c.StartColor.g
842 >> c.StartColor.b
843 >> c.StartColor.a
844 >> c.EndColor.r
845 >> c.EndColor.g
846 >> c.EndColor.b
847 >> c.EndColor.a
849 >> c.EmissionRate
850 >> c.MaxParticles
851 >> c.Direction.x
852 >> c.Direction.y
853 >> c.Direction.z
854 >> c.Gravity.x
855 >> c.Gravity.y
856 >> c.Gravity.z
857 >> shape
858 >> c.ShapeRadius
859 >> c.ConeAngle
860 >> blend
861 >> std::quoted(
862 c.TexturePath);
863
864 c.Shape =
865 static_cast<
866 ParticleShape>(shape);
867
868 c.BlendMode =
869 static_cast<
870 ParticleBlendMode>(blend);
871
872 current.AddComponent<
874 }
875 else if (k == "SCRIPT")
876 {
877 // V1.0-V1.3 compatibility:
878 // SCRIPT <enabled> <rotationSpeed>
879
881
882 float legacySpeed =
883 1.0f;
884
885 s >> c.Enabled
886 >> legacySpeed;
887
888 c.ScriptName =
889 "Rotator";
890
891 c.Fields["Speed"] =
894 legacySpeed);
895
896 c.Fields["Axis"] =
899 {
900 0.0f,
901 1.0f,
902 0.0f
903 });
904
905 current.AddComponent<
907 }
908 else if (k == "SCRIPT_V2")
909 {
911
912 std::size_t fieldCount =
913 0;
914
915 s >> c.Enabled
916 >> std::quoted(
917 c.ScriptName)
918 >> fieldCount;
919
920 if (!current.HasComponent<NativeScriptComponent>())
922 else
923 scene.GetAdditionalNativeScripts(current.GetHandle()).push_back(std::move(c));
924 }
925 else if (
926 k == "SCRIPT_FIELD" &&
927 current.HasComponent<
929 {
930 std::string name;
931
932 int type = 0;
933
934 s >> std::quoted(name)
935 >> type;
936
938
939 v.Type =
940 ScriptFieldType(type);
941
942 if (v.Type ==
944 {
945 s >> v.Float;
946 }
947 else if (v.Type ==
949 {
950 s >> v.Int;
951 }
952 else if (v.Type ==
954 {
955 s >> v.Bool;
956 }
957 else if (v.Type == ScriptFieldType::String)
958 {
959 s >> std::quoted(v.String);
960 }
961 else
962 {
963 ReadV3(
964 s,
965 v.Vec3);
966 }
967
968 auto& extras = scene.GetAdditionalNativeScripts(current.GetHandle());
969 auto& script = extras.empty() ? current.GetComponent<NativeScriptComponent>() : extras.back();
970 script.Fields[name] = v;
971 }
972 else if (k == "RIGIDBODY")
973 {
975
976 int ty;
977
978 s >> ty
979 >> c.Mass
980 >> c.UseGravity;
981
982 c.Type =
983 RigidbodyType(ty);
984
985 current.AddComponent<
987 }
988
989 // -------------------------------------------------
990 // V1.8 Native Gameplay AI
991 // -------------------------------------------------
992
993 else if (k == "NAV_AGENT" ||
994 k == "NAV_AGENT_V2")
995 {
997
998 s >> c.Enabled
999 >> c.Speed
1000 >> c.StoppingDistance;
1001
1002 ReadV3(s, c.Destination);
1003
1004 s >> c.HasDestination;
1005
1006 if (k == "NAV_AGENT_V2")
1007 s >> c.RepathInterval;
1008
1009 c.Speed =
1010 std::max(c.Speed, 0.0f);
1011
1013 std::max(c.StoppingDistance, 0.0f);
1014
1015 c.RepathInterval =
1016 std::clamp(c.RepathInterval, 0.05f, 10.0f);
1017
1018 current.AddComponent<NavAgentComponent>(c);
1019 }
1020
1021 else if (k == "PERCEPTION_V1")
1022 {
1024
1025 if (s >> c.Enabled
1026 >> c.DetectionMask
1027 >> c.DetectionRadius
1028 >> c.FieldOfView
1029 >> c.EyeHeight
1030 >> c.MemoryDuration
1031 >> c.UpdateInterval
1032 >> c.DebugDraw)
1033 {
1034 c.DetectionRadius = std::max(0.0f, c.DetectionRadius);
1035 c.FieldOfView = std::clamp(c.FieldOfView, 0.0f, 360.0f);
1036 c.EyeHeight = std::clamp(c.EyeHeight, 0.0f, 100.0f);
1037 c.MemoryDuration = std::max(0.0f, c.MemoryDuration);
1038 c.UpdateInterval = std::clamp(c.UpdateInterval, 0.01f, 60.0f);
1039
1041 }
1042 }
1043
1044 else if (k == "BOX")
1045 {
1047
1048 ReadV3(
1049 s,
1050 c.Size);
1051
1052 s >> c.IsTrigger
1053 >> c.Material.Friction
1054 >> c.Material.Bounciness;
1055
1056 current.AddComponent<
1058 }
1059 else if (k == "SPHERE")
1060 {
1062
1063 s >> c.Radius
1064 >> c.IsTrigger
1065 >> c.Material.Friction
1066 >> c.Material.Bounciness;
1067
1068 current.AddComponent<
1070 }
1071 else if (k == "CAPSULE")
1072 {
1074
1075 s >> c.Radius
1076 >> c.Height
1077 >> c.IsTrigger
1078 >> c.Material.Friction
1079 >> c.Material.Bounciness;
1080
1081 current.AddComponent<
1083 }
1084 else if (k == "MESH")
1085 {
1086 std::string mp;
1087
1088 s >> std::quoted(mp);
1089
1090 if (mp == "CUBE" ||
1091 mp.empty())
1092 {
1093 current.AddComponent<
1095 defaultMesh);
1096 }
1097 else
1098 {
1099 try
1100 {
1101 current.AddComponent<
1104 LoadMesh(mp));
1105 }
1106 catch (...)
1107 {
1108 Log::Warn(
1109 "Scene mesh load failed, using fallback: " +
1110 mp);
1111
1112 current.AddComponent<
1114 defaultMesh);
1115 }
1116 }
1117 }
1118 else if (k == "MATERIAL")
1119 {
1120 auto m =
1121 std::make_shared<
1122 Material>(
1123 *defaultMaterial);
1124
1125 glm::vec4 col;
1126
1127 s >> col.r
1128 >> col.g
1129 >> col.b
1130 >> col.a
1131 >> m->Metallic()
1132 >> m->Roughness()
1133 >> m->AmbientOcclusion()
1134 >> m->NormalStrength();
1135
1136 ReadV3(
1137 s,
1138 m->EmissiveColor());
1139
1140 s >> m->EmissiveStrength();
1141
1142 m->GetColor() =
1143 col;
1144
1145 // The editor default material may contain the
1146 // checker/default texture. A serialized material
1147 // must not inherit it just because it was cloned.
1148 // TEXTURES below explicitly restores a real
1149 // albedo texture when one was saved.
1150 m->UseTexture() =
1151 false;
1152
1153 current.AddComponent<
1155 }
1156 else if (k == "MATERIAL_V2")
1157 {
1158 auto m =
1159 std::make_shared<
1160 Material>(
1161 *defaultMaterial);
1162
1163 glm::vec4 col;
1164
1165 bool useTexture =
1166 false;
1167
1168 int surfaceMode =
1169 0;
1170
1171 s >> col.r
1172 >> col.g
1173 >> col.b
1174 >> col.a
1175 >> m->Metallic()
1176 >> m->Roughness()
1177 >> m->AmbientOcclusion()
1178 >> m->NormalStrength();
1179
1180 ReadV3(
1181 s,
1182 m->EmissiveColor());
1183
1184 s >> m->EmissiveStrength()
1185 >> useTexture
1186 >> surfaceMode
1187 >> m->AlphaCutoff();
1188
1189 m->GetColor() =
1190 col;
1191
1192 m->UseTexture() =
1193 useTexture;
1194
1195 materialUseTextureExplicit =
1196 true;
1197
1198 m->SurfaceMode() =
1199 static_cast<
1201 surfaceMode);
1202
1203 // TEXTURES restores the actual texture objects.
1204 // The saved checkbox state remains authoritative
1205 // instead of being guessed from the path.
1206 current.AddComponent<
1208 }
1209 else if (
1210 k == "TEXTURES" &&
1211 current.HasComponent<
1213 {
1214 std::string albedo;
1215 std::string normal;
1216 std::string metallic;
1217 std::string roughness;
1218 std::string ao;
1219 std::string emissive;
1220
1221 s >> std::quoted(albedo)
1222 >> std::quoted(normal)
1223 >> std::quoted(metallic)
1224 >> std::quoted(roughness)
1225 >> std::quoted(ao)
1226 >> std::quoted(emissive);
1227
1228 auto m =
1229 current
1230 .GetComponent<
1232 .MaterialAsset;
1233
1234 auto load =
1235 [](const std::string& path)
1236 -> std::shared_ptr<
1237 Texture2D>
1238 {
1239 return
1240 path.empty()
1241 ? nullptr
1244 path);
1245 };
1246
1247 if (!albedo.empty())
1248 {
1249 m->SetTexture(
1250 load(albedo));
1251
1252 if (!materialUseTextureExplicit)
1253 {
1254 m->UseTexture() =
1255 true;
1256 }
1257 }
1258
1259 m->SetNormalTexture(
1260 load(normal));
1261
1262 m->SetMetallicTexture(
1263 load(metallic));
1264
1265 m->SetRoughnessTexture(
1266 load(roughness));
1267
1268 m->SetAOTexture(
1269 load(ao));
1270
1271 m->SetEmissiveTexture(
1272 load(emissive));
1273 }
1274 }
1275
1276 // Resolve saved parent IDs after all entities
1277 // have been created.
1278 for (auto& pr :
1279 parents)
1280 {
1281 auto it =
1282 handleMap.find(
1283 pr.oldParent);
1284
1285 if (it !=
1286 handleMap.end())
1287 {
1288 scene.SetParent(
1289 pr.child,
1290 it->second,
1291 false);
1292 }
1293 }
1294
1295 return true;
1296 }
1297}
Animation declarations for the Animation subsystem.
AssetManager declarations for the Asset subsystem.
Components declarations for the Scenes and Native Scripting subsystem.
Log declarations for the Core subsystem.
Material declarations for the Rendering subsystem.
Mesh declarations for the Rendering subsystem.
Scene declarations for the Scenes and Native Scripting subsystem.
Scene serialization and deserialization.
static std::shared_ptr< AnimationAsset > Load(const std::filesystem::path &path)
Definition Animation.cpp:24
static std::filesystem::path ToProjectRelative(const std::filesystem::path &path)
static std::filesystem::path GetTexturePath(const std::shared_ptr< Texture2D > &texture)
static std::shared_ptr< Mesh > LoadMesh(const std::filesystem::path &path)
static std::shared_ptr< Texture2D > LoadTexture(const std::filesystem::path &path)
static std::filesystem::path GetMeshPath(const std::shared_ptr< Mesh > &mesh)
std::uint32_t GetHandle() const
Definition Entity.h:41
T & GetComponent()
Definition Scene.h:851
T & AddComponent(Args &&... args)
Definition Scene.h:836
bool HasComponent() const
Definition Scene.h:865
static void Warn(std::string_view message)
Definition Log.h:18
float & Metallic()
Definition Material.h:49
std::vector< Entity > GetEntities()
Definition Scene.cpp:231
bool SetParent(Entity child, Entity parent, bool keepWorldTransform=true)
Definition Scene.cpp:1610
Entity CreateEntity(const std::string &name="Entity")
Definition Scene.cpp:133
void DestroyEntity(Entity entity)
Definition Scene.cpp:161
std::vector< NativeScriptComponent > & GetAdditionalNativeScripts(std::uint32_t handle)
Definition Scene.h:95
static bool Load(Scene &scene, const std::filesystem::path &path, const std::shared_ptr< Mesh > &defaultMesh, const std::shared_ptr< Material > &defaultMaterial)
static bool Save(const Scene &scene, const std::filesystem::path &path)
constexpr std::uint32_t Default
Definition Components.h:43
constexpr std::uint32_t MaxLayers
Definition Components.h:49
ParticleBlendMode
Definition Components.h:164
MaterialSurfaceMode
Definition Material.h:16
CameraProjectionType
Definition Components.h:106
RigidbodyType
Definition Components.h:223
ParticleShape
Definition Components.h:163
ScriptFieldType
Definition Components.h:297
std::shared_ptr< AnimationAsset > Animation
Definition Components.h:325
CameraProjectionType ProjectionType
Definition Components.h:113
std::uint32_t Layer
Definition Components.h:64
std::shared_ptr< Mesh > MeshAsset
Definition Components.h:341
std::shared_ptr< Material > MaterialAsset
Definition Components.h:346
std::unordered_map< std::string, ScriptFieldValue > Fields
Definition Components.h:319
ParticleBlendMode BlendMode
Definition Components.h:189
static ScriptFieldValue MakeFloat(float value)
Definition Components.h:308
ScriptFieldType Type
Definition Components.h:301
static ScriptFieldValue MakeVec3(glm::vec3 value)
Definition Components.h:312