Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
Scene.h
Go to the documentation of this file.
1#pragma once
2
3#include "Actor.h"
9#include "Entity.h"
10
11#include <filesystem>
12#include <memory>
13#include <stdexcept>
14#include <type_traits>
15#include <utility>
16#include <vector>
17
18namespace Engine
19{
20 class EngineKern;
21} // namespace Engine
22
23namespace Engine::Core
24{
25 class AssetManager;
26 class AssetRegistry;
27} // namespace Engine::Core
28
29namespace Engine::Entities {
30 class Scene;
31 class StaticMeshActor;
32
33 namespace detail {
34 struct GltfSpawnPlanData;
35
43
45 std::shared_ptr<const GltfSpawnPlanData> data;
46 std::vector<GltfSpawnNode> meshNodes;
47 };
48
49 GltfSpawnPlan prepareGltfSpawnPlan( Scene * scene, const std::filesystem::path & gltfPath );
51
53 Actor * actor,
54 const GltfSpawnPlan & plan,
55 const GltfSpawnNode & spawnNode
56 );
57 } // namespace detail
58
62 class Scene : public Core::ITickable {
63 public:
65 ~Scene() override;
66
77 template<typename T, typename... Args>
78 T* spawnActor(const Ecs::Transform& spawnTransform, Args&&... args) {
79 static_assert(std::is_base_of_v<Entities::Actor, T>, "T must derive from Actor");
80 static_assert(
81 std::is_constructible_v<T, const SpawnContext&, Args...>,
82 "Actor types spawned with Scene::spawnActor must be constructible as "
83 "T(const SpawnContext&, ...actorSpecificArgs)."
84 );
85 TRACY_ZONE_SCOPED_NAMED("Spawned actor of class");
86
87 try {
88 if (!sceneGraph) {
89 throw std::runtime_error("Failed to spawn Actor with world node");
90 }
91 std::shared_ptr<Entities::SceneNode> node = sceneGraph->createNode(spawnTransform, sceneGraph->getRoot(), false);
92 const SpawnContext context{node, this};
93
94 T* rawPtr = new T(context, std::forward<Args>(args)...);
95 entities.push_back(rawPtr);
96
97 rawPtr->sceneNode = node;
98 rawPtr->beginPlay();
99
100 return rawPtr;
101 } catch (const std::exception &e) {
102 throw std::runtime_error("Failed to spawn actor: " + std::string(e.what()));
103 }
104 }
105
117 template<typename ActorT = StaticMeshActor>
118 static std::vector<ActorT*> spawnAll(
119 Scene* scene,
120 const std::filesystem::path& gltfPath)
121 {
122 Core::AssetRegistry* registry = scene != nullptr ? scene->getAssetRegistry() : nullptr;
123 if (registry != nullptr)
124 {
125 return spawnAll<ActorT>(scene, registry->gltfs().load(gltfPath));
126 }
127
128 detail::GltfSpawnPlan spawnPlan = detail::prepareGltfSpawnPlan(scene, gltfPath);
129
130 std::vector<ActorT*> spawnedActors;
131 spawnedActors.reserve( spawnPlan.meshNodes.size() );
132
133 for ( detail::GltfSpawnNode & spawnNode : spawnPlan.meshNodes ) {
134 auto* actor = scene->spawnActor<ActorT>(spawnNode.transform, std::move(spawnNode.meshPath));
135 spawnedActors.push_back(actor);
136 detail::applyGltfNodeExtensions( actor, spawnPlan, spawnNode );
137 }
138
139 return spawnedActors;
140 }
141
142 template<typename ActorT = StaticMeshActor>
143 static std::vector<ActorT*> spawnAll(
144 Scene* scene,
146 {
148
149 std::vector<ActorT*> spawnedActors;
150 spawnedActors.reserve(spawnPlan.meshNodes.size());
151
152 for (detail::GltfSpawnNode& spawnNode : spawnPlan.meshNodes) {
153 ActorT* actor = nullptr;
154
155 if constexpr (std::is_constructible_v<ActorT, const SpawnContext&, Assets::GltfRenderableHandle>)
156 {
157 if (spawnNode.renderable)
158 {
159 actor = scene->spawnActor<ActorT>(spawnNode.transform, spawnNode.renderable);
160 }
161 }
162
163 if (actor == nullptr)
164 {
165 if constexpr (std::is_constructible_v<ActorT, const SpawnContext&, Assets::GeometryMeshHandle>)
166 {
167 if (spawnNode.geometryMesh)
168 {
169 actor = scene->spawnActor<ActorT>(spawnNode.transform, spawnNode.geometryMesh);
170 }
171 }
172 }
173
174 if (actor == nullptr)
175 {
176 actor = scene->spawnActor<ActorT>(spawnNode.transform, std::move(spawnNode.meshPath));
177 }
178
179 spawnedActors.push_back(actor);
180 detail::applyGltfNodeExtensions(actor, spawnPlan, spawnNode);
181 }
182
183 return spawnedActors;
184 }
185
189
190 virtual void loadContent();
191 virtual void unloadContent();
192
193 [[nodiscard]] std::vector<Entities::Entity*> getEntities() const;
194
198 void reserveActorCapacity(size_t additionalActors);
199
200 void cleanup();
201 [[nodiscard]] Core::SceneManager* getSceneManager() const;
202
207 [[nodiscard]] Core::AssetManager* getAssetManager() const;
208 [[nodiscard]] Core::AssetRegistry* getAssetRegistry() const;
209
216
221 [[nodiscard]] EngineKern* getEngine() const { return engine_; }
222 [[nodiscard]] SceneGraph* getSceneGraph() const { return sceneGraph.get(); }
223
224 protected:
225 EngineKern* engine_ = nullptr;
228 private:
229
230 std::unique_ptr<SceneGraph> sceneGraph;
231
232 std::vector<Entity*> entities;
233 std::vector<Actor*> pendingDestroyActors_;
234 };
235}
#define TRACY_ZONE_SCOPED_NAMED(name)
Assets::GltfHandle load(const std::filesystem::path &source)
This is the interface which is used to call a tick function on an object. Everything which should be ...
Definition Tick.h:24
Manages game objects within a scene, handling registration, ID allocation, and GPU buffer synchroniza...
An Actor is similar to an Engine::Entities::Entity. An actor is an Entity with a transform.
Definition Actor.h:65
Represents the entire scene as a graph (specifically, a tree) of SceneNode objects....
Definition SceneGraph.h:199
A scene is the overarching structure which can spawn, contain and destroy actors or entities.
Definition Scene.h:62
static std::vector< ActorT * > spawnAll(Scene *scene, const std::filesystem::path &gltfPath)
Spawns all meshes from a GLTF file into the scene.
Definition Scene.h:118
void destroyActor(Entities::Actor *actor)
Core::SceneManager * getSceneManager() const
SceneGraph * getSceneGraph() const
Definition Scene.h:222
std::vector< Entities::Entity * > getEntities() const
Core::AssetManager * getAssetManager() const
Gets the asset manager for this scene.
virtual void unloadContent()
void destroyActorImmediate(Entities::Actor *actor)
void reserveActorCapacity(size_t additionalActors)
Reserves storage for a known number of upcoming actor spawns.
Core::AssetRegistry * assetRegistry
Definition Scene.h:227
std::vector< Actor * > pendingDestroyActors_
Definition Scene.h:233
Core::AssetRegistry * getAssetRegistry() const
EngineKern * engine_
Definition Scene.h:225
std::unique_ptr< SceneGraph > sceneGraph
Definition Scene.h:230
void setAssetRegistry(Core::AssetRegistry *registry)
void setAssetManager(Core::AssetManager *manager)
Sets the asset manager for this scene (used for dependency injection in tests)
virtual void loadContent()
static std::vector< ActorT * > spawnAll(Scene *scene, Assets::GltfHandle gltf)
Definition Scene.h:143
T * spawnActor(const Ecs::Transform &spawnTransform, Args &&... args)
Spawns an actor from a specific class in somewhere into the scene.
Definition Scene.h:78
std::vector< Entity * > entities
Definition Scene.h:232
Core::AssetManager * assetManager
Definition Scene.h:226
EngineKern * getEngine() const
Gets the engine instance for this scene.
Definition Scene.h:221
Wrapper for entt component creation which ensures types safety and attaches the correct component wit...
AssetHandle< GltfRenderableHandleTag > GltfRenderableHandle
AssetHandle< GltfHandleTag > GltfHandle
AssetHandle< GeometryMeshHandleTag > GeometryMeshHandle
Manages IBL (Image-Based Lighting) resources for specular reflections.
GltfSpawnPlan prepareGltfSpawnPlan(Scene *scene, const std::filesystem::path &gltfPath)
void applyGltfNodeExtensions(Actor *actor, const GltfSpawnPlan &plan, const GltfSpawnNode &spawnNode)
Identifies a named mesh-like asset inside an asset file.
Definition AssetPath.h:40
Scene ownership data passed to actor and component constructors during spawning.
Assets::GeometryMeshHandle geometryMesh
Definition Scene.h:38
Assets::GltfRenderableHandle renderable
Definition Scene.h:37
std::shared_ptr< const GltfSpawnPlanData > data
Definition Scene.h:45
std::vector< GltfSpawnNode > meshNodes
Definition Scene.h:46