Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
GltfSpawner.cpp
Go to the documentation of this file.
8#include <glm/gtc/type_ptr.hpp>
9
10namespace EngineCore {
11
12 namespace {
13 // Extract transform from tinygltf node
14 Ecs::LocalTransform extractNodeTransform(const tinygltf::Node& node) {
15 Transform transform;
16
17 if (!node.translation.empty()) {
18 transform.setLocation(glm::vec3(
19 node.translation[0],
20 node.translation[1],
21 node.translation[2]
22 ));
23 }
24
25 if (!node.rotation.empty()) {
26 // tinygltf stores quaternion as [x, y, z, w]
27 transform.setRotationQuat(glm::quat(
28 static_cast<float>(node.rotation[3]), // w
29 static_cast<float>(node.rotation[0]), // x
30 static_cast<float>(node.rotation[1]), // y
31 static_cast<float>(node.rotation[2]) // z
32 ));
33 }
34
35 if (!node.scale.empty()) {
36 transform.setScale(glm::vec3(
37 node.scale[0],
38 node.scale[1],
39 node.scale[2]
40 ));
41 }
42
43 return Ecs::LocalTransform{ transform.toMatrix() };
44 }
45 }
46
47 std::vector<StaticMeshActor*> GltfSpawner::spawnAll(
48 Scene* scene,
49 const std::filesystem::path& gltfPath)
50 {
51 std::vector<StaticMeshActor*> spawnedActors;
52
53 // 1. Quick metadata-only parse - skips ALL buffer/image loading
54 // Only parses JSON structure to get node names and transforms
55 GltfLoader loader;
56 tinygltf::Model model = loader.loadMetadataOnly(gltfPath);
57
58 // 2. Submit to async pipeline for heavy processing (vertex data, meshlets, etc.)
59 // This happens on background threads
60 AssetManager* assetManager = scene->getAssetManager();
61 if (assetManager) {
62 assetManager->loadEcsModel(gltfPath);
63 }
64
65 // 3. Create actors immediately using the parsed metadata
66 // The actual mesh data will arrive later through the async pipeline
67 spawnedActors.reserve(model.nodes.size());
68
69 for (const tinygltf::Node& node : model.nodes) {
70 // Skip nodes without meshes (cameras, lights, empties, armatures)
71 if (node.mesh < 0) {
72 continue;
73 }
74
75 Asset::Path meshPath(gltfPath, node.name);
76 Ecs::LocalTransform spawnTransform = extractNodeTransform(node);
77
78 auto* actor = scene->spawnActor<StaticMeshActor>(spawnTransform, meshPath);
79 spawnedActors.push_back(actor);
80 }
81
82 return spawnedActors;
83 }
84
85}
void loadEcsModel(const std::filesystem::path &path)
Submits a gltf model for loading. You provide the path of the model to load. This can include 3D data...
tinygltf::Model loadMetadataOnly(const std::filesystem::path &path) const
Loads only the GLTF metadata (nodes, names, transforms) without loading buffer data.
static std::vector< StaticMeshActor * > spawnAll(Scene *scene, const std::filesystem::path &gltfPath)
A scene is the overarching structure which can spawn actors.
Definition Scene.h:17
T * spawnActor(const Ecs::LocalTransform &spawnTransform, Args &&... args)
Spawns an actor from a specific class in somewhere into the scene.
Definition Scene.h:33
AssetManager * getAssetManager() const
Gets the asset manager for this scene.
Definition Scene.cpp:53
Wrapper for entt component creation which ensures types safety and attaches the correct component wit...
Log category system implementation.