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"
47 struct GltfSpawnPlanData;
64 std::shared_ptr<const GltfSpawnPlanData>
data;
102 template <
class T,
typename... Args>
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");
108 return entities_.spawnEntity<T>(std::forward<Args>(args)...);
124 template<
class T,
typename... Args>
126 static_assert(std::is_base_of_v<Entities::Actor, T>,
"T must derive from Actor");
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)."
132 TRACY_ZONE_SCOPED_NAMED(
"Spawned actor of class");
136 throw std::runtime_error(
"Failed to spawn Actor with world node");
138 std::shared_ptr<Entities::SceneNode> node =
sceneGraph->createNode(spawnTransform,
sceneGraph->getRoot(),
false);
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()));
158 template<
typename T,
typename... Args>
175 template<
typename T,
typename... Args>
178 const std::shared_ptr<SceneNode>& parentNode,
181 static_assert(std::is_base_of_v<Entities::Actor, T>,
"T must derive from Actor");
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)."
187 TRACY_ZONE_SCOPED_NAMED(
"Spawned attached actor of class");
191 throw std::runtime_error(
"Failed to spawn attached Actor with parent node");
195 node->setLocalModelMatrix(localTransform.
matrix);
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()));
218 template<
typename ActorT = StaticMeshActor>
221 const std::filesystem::path& gltfPath)
224 if (registry !=
nullptr)
231 std::vector<ActorT*> spawnedActors;
232 spawnedActors.reserve( spawnPlan.
meshNodes.size() );
236 spawnedActors.push_back(actor);
240 return spawnedActors;
250 template<
typename ActorT = StaticMeshActor>
257 std::vector<ActorT*> spawnedActors;
258 spawnedActors.reserve(spawnPlan.
meshNodes.size());
261 ActorT* actor =
nullptr;
263 if constexpr (std::is_constructible_v<ActorT, const SpawnContext&, Assets::GltfRenderableHandle>)
271 if (actor ==
nullptr)
273 if constexpr (std::is_constructible_v<ActorT, const SpawnContext&, Assets::GeometryMeshHandle>)
282 if (actor ==
nullptr)
287 spawnedActors.push_back(actor);
291 return spawnedActors;
336 template<
typename Func>
339 entities_.forEachEntity(std::forward<Func>(func));
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 ...
Manages game objects within a scene, handling registration, ID allocation, and GPU buffer synchroniza...
Owns the engine runtime and provides access to engine subsystems.
An Actor is similar to an Engine::Entities::Entity. An actor is an Entity with a transform.
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...
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....
Owns scene entities, actor lifetime, and the scene graph root.
static std::vector< ActorT * > spawnAll(Scene *scene, const std::filesystem::path &gltfPath)
Spawns one actor for each mesh node in a GLTF file.
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.
Core::SceneManager * getSceneManager() const
Returns the scene manager from the active engine.
SceneGraph * getSceneGraph() const
Returns the scene graph owned by this scene.
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()
bool hasLightingOverride() const
void setLightingOverride(const SceneLightingSettings &settings)
Enables scene-specific lighting values.
void destroyEntity(Entity *entity)
Queues an entity for destruction.
Core::AssetRegistry * assetRegistry
Asset registry assigned to this scene.
Scene()
Creates a scene with an empty scene graph.
std::optional< SceneLightingSettings > lightingOverride_
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.
void flushPendingEntityDestruction()
Destroys all entities queued for destruction.
std::unique_ptr< SceneGraph > sceneGraph
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_
T * spawnEntity(Args &&... args)
Creates an entity owned by this scene.
T * spawnActorAttached(const Ecs::Transform &localTransform, Actor &parent, Args &&... args)
Creates an actor under another actor's scene node.
const SceneLightingSettings * getLightingOverride() const
static std::vector< ActorT * > spawnAll(Scene *scene, Assets::GltfHandle gltf)
Spawns one actor for each mesh node in a loaded GLTF asset.
void clearLightingOverride()
Disables scene-specific lighting and restores the global defaults.
T * spawnActor(const Ecs::Transform &spawnTransform, Args &&... args)
Creates an actor at a world transform and calls beginPlay().
~Scene() override
Cleans up all scene-owned entities and actors.
Core::AssetManager * assetManager
Asset manager assigned to this scene.
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.
Scene-specific multipliers for direct and baked environment lighting.
Scene ownership data passed to actor and component constructors during spawning.
Planned scene actor for one renderable glTF node.
Ecs::Transform transform
Authored local transform for the node.
Asset::MeshPath meshPath
Authored mesh path and node identity.
Assets::GeometryMeshHandle geometryMesh
Geometry mesh handle used by the renderable.
size_t modelNodeIndex
Index of the source model node.
Assets::GltfRenderableHandle renderable
Renderable asset handle for this node.
Prepared glTF spawn data reused while creating actors from one glTF asset.
std::shared_ptr< const GltfSpawnPlanData > data
Shared prepared glTF data.
std::vector< GltfSpawnNode > meshNodes
Mesh nodes to spawn as actors.