Vulkan Schnee 0.0.1 (22840dc8)
High-performance rendering engine
Loading...
Searching...
No Matches
Scene.h
1#pragma once
2
3#include "Actor.h"
4#include "Engine/Asset/AssetRegistry.h"
5#include "Engine/Ecs/EcsData.h"
6#include "Engine/Entity/EntityContainer.h"
7#include "Engine/Entity/SpawnContext.h"
8#include "Engine/Files/AssetPath.h"
9#include "Engine/Logging/TracyMacros.hpp"
10#include "Entity.h"
11
12#include <filesystem>
13#include <memory>
14#include <optional>
15#include <stdexcept>
16#include <type_traits>
17#include <utility>
18#include <vector>
19
20namespace Engine
21{
22 class EngineKern;
23} // namespace Engine
24
25namespace Engine::Core
26{
27 class AssetManager;
28 class AssetRegistry;
29} // namespace Engine::Core
30
31namespace Engine::Entities {
32 class Scene;
33 class StaticMeshActor;
34
39 {
40 float sunBrightness = 2.65f;
41 float shInfluence = 0.40f;
42 float lightmapSunRemoval = 0.35f;
43 float lightmapBrightness = 1.0f;
44 };
45
46 namespace detail {
47 struct GltfSpawnPlanData;
48
59
64 std::shared_ptr<const GltfSpawnPlanData> data;
65 std::vector<GltfSpawnNode> meshNodes;
66 };
67
68 GltfSpawnPlan prepareGltfSpawnPlan( Scene * scene, const std::filesystem::path & gltfPath );
70
72 Actor * actor,
73 const GltfSpawnPlan & plan,
74 const GltfSpawnNode & spawnNode
75 );
76 } // namespace detail
77
84 class Scene : public Core::ITickable {
85 public:
90
94 ~Scene() override;
95
102 template <class T, typename... Args>
103 T* spawnEntity(Args&&... args)
104 {
105 static_assert(std::is_base_of_v<Entities::Entity, T>, "T must derive from Entity");
106 TRACY_ZONE_SCOPED_NAMED("Spawn entity of class");
107
108 return entities_.spawnEntity<T>(std::forward<Args>(args)...);
109 }
110
115 void destroyEntity(Entity* entity );
116
124 template<class T, typename... Args>
125 T* spawnActor(const Ecs::Transform& spawnTransform, Args&&... args) {
126 static_assert(std::is_base_of_v<Entities::Actor, T>, "T must derive from Actor");
127 static_assert(
128 std::is_constructible_v<T, const SpawnContext&, Args...>,
129 "Actor types spawned with Scene::spawnActor must be constructible as "
130 "T(const SpawnContext&, ...actorSpecificArgs)."
131 );
132 TRACY_ZONE_SCOPED_NAMED("Spawned actor of class");
133
134 try {
135 if (!sceneGraph) {
136 throw std::runtime_error("Failed to spawn Actor with world node");
137 }
138 std::shared_ptr<Entities::SceneNode> node = sceneGraph->createNode(spawnTransform, sceneGraph->getRoot(), false);
139 const SpawnContext context{node, this};
140
141 auto actor = std::make_unique<T>(context, std::forward<Args>(args)...);
142 T* rawPtr = actor.get();
143 rawPtr->sceneNode = node;
144 return entities_.adoptEntity(std::move(actor));
145 } catch (const std::exception &e) {
146 throw std::runtime_error("Failed to spawn actor: " + std::string(e.what()));
147 }
148 }
149
158 template<typename T, typename... Args>
160 const Ecs::Transform& localTransform,
161 Actor& parent,
162 Args&&... args
163 ) {
164 return spawnActorAttached<T>( localTransform, parent.getSceneNode(), std::forward<Args>( args )... );
165 }
166
175 template<typename T, typename... Args>
177 const Ecs::Transform& localTransform,
178 const std::shared_ptr<SceneNode>& parentNode,
179 Args&&... args
180 ) {
181 static_assert(std::is_base_of_v<Entities::Actor, T>, "T must derive from Actor");
182 static_assert(
183 std::is_constructible_v<T, const SpawnContext&, Args...>,
184 "Actor types spawned with Scene::spawnActorAttached must be constructible as "
185 "T(const SpawnContext&, ...actorSpecificArgs)."
186 );
187 TRACY_ZONE_SCOPED_NAMED("Spawned attached actor of class");
188
189 try {
190 if (!sceneGraph || !parentNode) {
191 throw std::runtime_error("Failed to spawn attached Actor with parent node");
192 }
193
194 std::shared_ptr<Entities::SceneNode> node = NodeFactory::createNode(parentNode, false);
195 node->setLocalModelMatrix(localTransform.matrix);
196 const SpawnContext context{node, this};
197
198 auto actor = std::make_unique<T>(context, std::forward<Args>(args)...);
199 T* rawPtr = actor.get();
200 rawPtr->sceneNode = node;
201 return entities_.adoptEntity(std::move(actor));
202 } catch (const std::exception &e) {
203 throw std::runtime_error("Failed to spawn attached actor: " + std::string(e.what()));
204 }
205 }
206
218 template<typename ActorT = StaticMeshActor>
219 static std::vector<ActorT*> spawnAll(
220 Scene* scene,
221 const std::filesystem::path& gltfPath)
222 {
223 Core::AssetRegistry* registry = scene != nullptr ? scene->getAssetRegistry() : nullptr;
224 if (registry != nullptr)
225 {
226 return spawnAll<ActorT>(scene, registry->gltfs().load(gltfPath));
227 }
228
229 detail::GltfSpawnPlan spawnPlan = detail::prepareGltfSpawnPlan(scene, gltfPath);
230
231 std::vector<ActorT*> spawnedActors;
232 spawnedActors.reserve( spawnPlan.meshNodes.size() );
233
234 for ( detail::GltfSpawnNode & spawnNode : spawnPlan.meshNodes ) {
235 auto* actor = scene->spawnActor<ActorT>(spawnNode.transform, std::move(spawnNode.meshPath));
236 spawnedActors.push_back(actor);
237 detail::applyGltfNodeExtensions( actor, spawnPlan, spawnNode );
238 }
239
240 return spawnedActors;
241 }
242
250 template<typename ActorT = StaticMeshActor>
251 static std::vector<ActorT*> spawnAll(
252 Scene* scene,
254 {
256
257 std::vector<ActorT*> spawnedActors;
258 spawnedActors.reserve(spawnPlan.meshNodes.size());
259
260 for (detail::GltfSpawnNode& spawnNode : spawnPlan.meshNodes) {
261 ActorT* actor = nullptr;
262
263 if constexpr (std::is_constructible_v<ActorT, const SpawnContext&, Assets::GltfRenderableHandle>)
264 {
265 if (spawnNode.renderable)
266 {
267 actor = scene->spawnActor<ActorT>(spawnNode.transform, spawnNode.renderable);
268 }
269 }
270
271 if (actor == nullptr)
272 {
273 if constexpr (std::is_constructible_v<ActorT, const SpawnContext&, Assets::GeometryMeshHandle>)
274 {
275 if (spawnNode.geometryMesh)
276 {
277 actor = scene->spawnActor<ActorT>(spawnNode.transform, spawnNode.geometryMesh);
278 }
279 }
280 }
281
282 if (actor == nullptr)
283 {
284 actor = scene->spawnActor<ActorT>(spawnNode.transform, std::move(spawnNode.meshPath));
285 }
286
287 spawnedActors.push_back(actor);
288 detail::applyGltfNodeExtensions(actor, spawnPlan, spawnNode);
289 }
290
291 return spawnedActors;
292 }
293
299
305
310
315
319 virtual void loadContent();
320
324 virtual void unloadContent();
325
329 [[nodiscard]] std::vector<Entities::Entity*> getEntities() const;
330
336 template<typename Func>
337 void forEachEntity(Func&& func) const
338 {
339 entities_.forEachEntity(std::forward<Func>(func));
340 }
341
346
350 [[nodiscard]] const EntityContainer& getEntityContainer() const;
351
356 void reserveActorCapacity(size_t additionalActors);
357
361 void cleanup();
362
366 [[nodiscard]] Core::SceneManager* getSceneManager() const;
367
371 [[nodiscard]] Core::AssetManager* getAssetManager() const;
372
376 [[nodiscard]] Core::AssetRegistry* getAssetRegistry() const;
377
383
389
393 [[nodiscard]] SceneGraph* getSceneGraph() const { return sceneGraph.get(); }
394
398 void setLightingOverride( const SceneLightingSettings & settings ) { lightingOverride_ = settings; }
399
404
405 [[nodiscard]] bool hasLightingOverride() const { return lightingOverride_.has_value(); }
407 {
408 return lightingOverride_ ? &*lightingOverride_ : nullptr;
409 }
410 [[nodiscard]] const SceneLightingSettings * getLightingOverride() const
411 {
412 return lightingOverride_ ? &*lightingOverride_ : nullptr;
413 }
414
415 protected:
418 private:
419
420 std::unique_ptr<SceneGraph> sceneGraph;
421
423 std::optional<SceneLightingSettings> lightingOverride_;
424 };
425}
Coordinates engine asset loading pipelines and runtime texture descriptor ownership.
Groups typed asset registry stores and resolves authored asset references.
GltfStore & gltfs()
Returns the mutable glTF store.
Assets::GltfHandle load(const std::filesystem::path &source)
Gets or creates a stable handle for a glTF source file and imports its renderable metadata.
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...
Owns the engine runtime and provides access to engine subsystems.
Definition Engine.h:67
An Actor is similar to an Engine::Entities::Entity. An actor is an Entity with a transform.
Definition Actor.h:71
std::shared_ptr< SceneNode > getSceneNode() const
Getter for the scene graph node for this object.
Owns entity lifetime and deferred destruction for a scene or subsystem.
The entity class is the base class of everything which is attached to a scene. If it is part of a sce...
Definition Entity.h:18
static std::shared_ptr< SceneNode > createNode(const std::shared_ptr< SceneNode > &parent, bool disableCaching=false)
Creates a new SceneNode with the specified parent.
Represents the entire scene as a graph (specifically, a tree) of SceneNode objects....
Definition SceneGraph.h:237
Owns scene entities, actor lifetime, and the scene graph root.
Definition Scene.h:84
static std::vector< ActorT * > spawnAll(Scene *scene, const std::filesystem::path &gltfPath)
Spawns one actor for each mesh node in a GLTF file.
Definition Scene.h:219
void destroyActor(Entities::Actor *actor)
Queues an actor for destruction after the current tick.
EntityContainer & getEntityContainer()
Returns the mutable scene entity container.
void forEachEntity(Func &&func) const
Invokes a callback for each scene-owned entity.
Definition Scene.h:337
Core::SceneManager * getSceneManager() const
Returns the scene manager from the active engine.
SceneGraph * getSceneGraph() const
Returns the scene graph owned by this scene.
Definition Scene.h:393
std::vector< Entities::Entity * > getEntities() const
Returns the scene-owned entities.
Core::AssetManager * getAssetManager() const
Returns the asset manager assigned to this scene.
virtual void unloadContent()
Releases scene content. Override in derived scenes.
void destroyActorImmediate(Entities::Actor *actor)
Destroys an actor now and calls endPlay() if needed.
void reserveActorCapacity(size_t additionalActors)
Reserves storage for upcoming actor spawns.
SceneLightingSettings * getLightingOverride()
Definition Scene.h:406
bool hasLightingOverride() const
Definition Scene.h:405
void setLightingOverride(const SceneLightingSettings &settings)
Enables scene-specific lighting values.
Definition Scene.h:398
void destroyEntity(Entity *entity)
Queues an entity for destruction.
Core::AssetRegistry * assetRegistry
Asset registry assigned to this scene.
Definition Scene.h:417
Scene()
Creates a scene with an empty scene graph.
std::optional< SceneLightingSettings > lightingOverride_
Definition Scene.h:423
Core::AssetRegistry * getAssetRegistry() const
Returns the asset registry assigned to this scene.
const EntityContainer & getEntityContainer() const
Returns the read-only scene entity container.
void flushPendingActorDestruction()
Destroys all actors queued by destroyActor().
T * spawnActorAttached(const Ecs::Transform &localTransform, const std::shared_ptr< SceneNode > &parentNode, Args &&... args)
Creates an actor under a scene node.
Definition Scene.h:176
void flushPendingEntityDestruction()
Destroys all entities queued for destruction.
std::unique_ptr< SceneGraph > sceneGraph
Definition Scene.h:420
void setAssetRegistry(Core::AssetRegistry *registry)
Sets the asset registry for this scene.
void setAssetManager(Core::AssetManager *manager)
Sets the asset manager for this scene.
virtual void loadContent()
Loads scene content. Override in derived scenes.
EntityContainer entities_
Definition Scene.h:422
T * spawnEntity(Args &&... args)
Creates an entity owned by this scene.
Definition Scene.h:103
T * spawnActorAttached(const Ecs::Transform &localTransform, Actor &parent, Args &&... args)
Creates an actor under another actor's scene node.
Definition Scene.h:159
const SceneLightingSettings * getLightingOverride() const
Definition Scene.h:410
static std::vector< ActorT * > spawnAll(Scene *scene, Assets::GltfHandle gltf)
Spawns one actor for each mesh node in a loaded GLTF asset.
Definition Scene.h:251
void clearLightingOverride()
Disables scene-specific lighting and restores the global defaults.
Definition Scene.h:403
T * spawnActor(const Ecs::Transform &spawnTransform, Args &&... args)
Creates an actor at a world transform and calls beginPlay().
Definition Scene.h:125
~Scene() override
Cleans up all scene-owned entities and actors.
Core::AssetManager * assetManager
Asset manager assigned to this scene.
Definition Scene.h:416
void cleanup()
Destroys all scene-owned entities and clears pending actor destruction.
Wrapper for entt component creation which ensures types safety and attaches the correct component wit...
AssetHandle< GltfRenderableHandleTag > GltfRenderableHandle
Handle to one mesh-bearing node inside a glTF file.
AssetHandle< GltfHandleTag > GltfHandle
Handle to a glTF container file.
AssetHandle< GeometryMeshHandleTag > GeometryMeshHandle
Handle to non-skinned mesh geometry.
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:89
Local transform matrix and helpers for translation, rotation, and scale.
Definition EcsData.h:476
glm::mat4 matrix
Column-major local transform matrix.
Definition EcsData.h:513
Scene-specific multipliers for direct and baked environment lighting.
Definition Scene.h:39
Scene ownership data passed to actor and component constructors during spawning.
Planned scene actor for one renderable glTF node.
Definition Scene.h:52
Ecs::Transform transform
Authored local transform for the node.
Definition Scene.h:56
Asset::MeshPath meshPath
Authored mesh path and node identity.
Definition Scene.h:55
Assets::GeometryMeshHandle geometryMesh
Geometry mesh handle used by the renderable.
Definition Scene.h:54
size_t modelNodeIndex
Index of the source model node.
Definition Scene.h:57
Assets::GltfRenderableHandle renderable
Renderable asset handle for this node.
Definition Scene.h:53
Prepared glTF spawn data reused while creating actors from one glTF asset.
Definition Scene.h:63
std::shared_ptr< const GltfSpawnPlanData > data
Shared prepared glTF data.
Definition Scene.h:64
std::vector< GltfSpawnNode > meshNodes
Mesh nodes to spawn as actors.
Definition Scene.h:65