Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
GltfLoader.h
Go to the documentation of this file.
1#pragma once
2#include "BS_tracy_thread_pool.hpp"
10
11#include <Engine/Mesh/Vertex.h>
12#include <filesystem>
13#include <glm/gtc/quaternion.hpp>
14#include <limits>
15#include <tiny_gltf.h>
16#include <vector>
17
18namespace Engine::Assets
19{
20 class RuntimeTexture;
21 class Texture;
22} // namespace Engine::Assets
23
24namespace Engine::Core
25{
26 class AssetManager;
27} // namespace Engine::Core
28
30
31 class GltfLoader {
32 public:
34
36
37
38
40 GltfBufferDataView(const std::vector<tinygltf::Buffer>& buffers,
41 const std::vector<tinygltf::BufferView>& bufferViews,
42 const tinygltf::Accessor& accessor);
43
44 const tinygltf::Accessor& accessor;
45 const tinygltf::BufferView& bufferView;
46 const tinygltf::Buffer& buffer;
47 const float *data;
48 };
49
51 GltfVertexData(const std::vector<tinygltf::Buffer> &buffers,
52 const std::vector<tinygltf::BufferView> &bufferViews,
53 const std::vector<tinygltf::Accessor> &accessors, const tinygltf::Primitive &primitive);
54 std::optional<GltfBufferDataView> positionDataView;
55 std::optional<GltfBufferDataView> normalDataView;
56 std::optional<GltfBufferDataView> tangentDataView; // TANGENT attribute (vec4: xyz=tangent, w=handedness)
57 std::optional<GltfBufferDataView> uvDataView;
58 std::optional<GltfBufferDataView> lightmapDataView;
59 std::optional<GltfBufferDataView> indexDataView;
60 std::optional<GltfBufferDataView> jointsDataView;
61 std::optional<GltfBufferDataView> weightsDataView;
62 };
63
64 std::vector<Vertex> getPrimitiveVertices(const tinygltf::Primitive& primitive);
65
67
69 GltfTextureData() = default;
70
71 GltfTextureData(const tinygltf::Image &image) : image(image) {
72 }
73
74 [[nodiscard]] tinygltf::Image getImage() const { return image; }
75
76 [[nodiscard]] bool hasTextureData() const;
77
78 private:
79 tinygltf::Image image = tinygltf::Image();
80
81 };
82
84 StaticMeshSettings() = default;
85
86 StaticMeshSettings(const tinygltf::Value &extras);
87
88 bool bUsesLightMaps = false;
89 std::filesystem::path lightMapPath = std::filesystem::path();
90 int lightMapUvIndex = std::numeric_limits<int>::max();
91 };
92
101
102 explicit VulkanSchneeExtension(const tinygltf::Value& vs_extension,
103 const tinygltf::Model* model = nullptr);
104
112 explicit MeshProperties(const tinygltf::Value& extension);
115 };
116
124 explicit LightProperties(const tinygltf::Value& extension);
125
127 explicit LightmapProperties(const tinygltf::Value& extension);
128 std::filesystem::path lightmapPath;
129 std::filesystem::path lightmapKtxPath;
131
133 [[nodiscard]] const std::filesystem::path& getRuntimeLightmapPath() const;
134 };
135
136 bool getCastShadows() const;
137 bool getReceiveShadows() const;
138 bool getHasLightmaps() const;
139
140 private:
141
142 std::optional<LightmapProperties> lightmapProperties;
143
147 };
148
149 struct metadata {
150
151 std::string blenderObjectName;
152 };
153
162
164 explicit CollisionProperties(const tinygltf::Value& extension,
165 const tinygltf::Model* model = nullptr);
166
168 bool generateConvexHull = false;
169 bool isTrigger = false;
170 glm::vec3 offset{0.0f};
171
172 // Primitive params
173 glm::vec3 boxHalfExtents{0.5f};
174 float sphereRadius = 0.5f;
175 float capsuleRadius = 0.5f;
176 float capsuleHeight = 1.0f;
177
178 // Convex/mesh data
179 std::vector<glm::vec3> vertices;
180 std::vector<uint32_t> indices;
181
182 // Compound children
184 std::string name;
185 std::vector<glm::vec3> vertices;
186 std::vector<uint32_t> indices;
187 std::vector<uint32_t> edgeIndices;
188 glm::vec3 offset{0.0f};
189 glm::quat rotation{glm::identity<glm::quat>()};
190 };
191 std::vector<CompoundChild> compoundChildren;
192
193 [[nodiscard]] bool hasCollision() const { return shapeType != ShapeType::NONE; }
194
195 private:
196 static ShapeType parseShapeType(const std::string& typeStr);
197 };
198
200 explicit PointLightProperties(const tinygltf::Value& extension);
201
202 glm::vec3 color{1.0f};
203 float intensity = 1.0f;
204 float radius = 10.0f;
205 float cullDistance = 100.0f;
206 };
207
209 explicit DirectionalLightProperties(const tinygltf::Value& extension);
210
211 glm::vec3 color{1.0f};
212 glm::vec3 direction{0.0f, -1.0f, 0.0f};
213 float intensity = 1.0f;
214 bool enabled = true;
215 bool castsShadows = true;
216 bool hasDirection = false;
218 };
219
220 std::optional<LightProperties> lightProperties;
221 std::optional<MeshProperties> meshProperties;
222 std::optional<LightProperties::LightmapProperties> lightmapProperties;
223 std::optional<CollisionProperties> collisionProperties;
224 std::optional<PointLightProperties> pointLightProperties;
225 std::optional<DirectionalLightProperties> directionalLightProperties;
226
227 [[nodiscard]] bool hasLightProperties() const;
228 [[nodiscard]] bool hasLightmapProperties() const;
229 [[nodiscard]] bool hasMeshProperties() const;
230 [[nodiscard]] bool hasCollisionProperties() const;
231 [[nodiscard]] bool hasPointLightProperties() const;
232 [[nodiscard]] bool hasDirectionalLightProperties() const;
233
234 private:
245 static std::string extractVersion(const tinygltf::Value& vs_extension);
246 };
247
256
257 explicit VulkanSchneeMaterialExtension(const tinygltf::Value& vs_extension);
258
266 explicit ShaderParameter(const tinygltf::Value& param);
267
268 std::string type; // "connected" or "value"
269 std::string socketType; // Socket type from Blender
270 tinygltf::Value value; // Only valid if type == "value"
271 };
272
273 std::string shaderType;
275
276 std::map<std::string, ShaderParameter> shaderParameters;
277 std::map<std::string, double> nodeProperties;
278 std::map<std::string, tinygltf::Value> customProperties;
279 bool normalMapBC5 = false; // True when normal map uses BC5 (2-channel RG)
280 };
281
290
291 explicit MaterialExtensions(const tinygltf::ExtensionMap &material);
292
293 std::optional<VulkanSchneeMaterialExtension> vulkanSchneeMaterialExtension;
294 };
295
296
305 explicit StaticMeshExtensions(const tinygltf::ExtensionMap &extensions);
306 StaticMeshExtensions(const tinygltf::Model &model, const tinygltf::ExtensionMap &extensions);
307
308 std::optional<VulkanSchneeExtension> vulkanSchneeExtension;
309 };
310
320 GltfMaterialData() = default;
321
322 GltfMaterialData(const tinygltf::Model &model, const tinygltf::Material &material,
323 const StaticMeshExtensions &meshSettings);
324
325 [[nodiscard]] bool hasTexture() const;
326
327 [[nodiscard]] uint32_t getTextureIndex() const;
328
329 [[nodiscard]] bool hasVulkanSchneeExtension() const;
330
332
333 [[nodiscard]] std::string getShaderName() const;
334
335 [[nodiscard]] const Materials::MaterialData& getMaterialData() const { return materialData; }
337
338 // PBR texture accessors (0xFFFFFFFF = not present)
339 [[nodiscard]] uint32_t getNormalTextureIndex() const { return normalTextureIndex; }
340 [[nodiscard]] uint32_t getRoughnessMetallicTextureIndex() const { return roughnessMetallicTextureIndex; }
341 [[nodiscard]] uint32_t getEmissiveTextureIndex() const { return emissiveTextureIndex; }
342
343 // PBR factor accessors
344 [[nodiscard]] float getRoughnessFactor() const { return roughnessFactor; }
345 [[nodiscard]] float getMetallicFactor() const { return metallicFactor; }
346 [[nodiscard]] float getNormalScale() const { return normalScale; }
347 [[nodiscard]] glm::vec3 getEmissiveFactor() const { return emissiveFactor; }
348
349 // PBR texture data accessors
350 [[nodiscard]] bool hasNormalTexture() const { return gltfNormalTextureIndex >= 0; }
351 [[nodiscard]] bool hasMetallicRoughnessTexture() const { return gltfMetallicRoughnessTextureIndex >= 0; }
352 [[nodiscard]] bool hasEmissiveTexture() const { return gltfEmissiveTextureIndex >= 0; }
353 [[nodiscard]] int32_t getGltfNormalTextureIndex() const { return gltfNormalTextureIndex; }
355 [[nodiscard]] int32_t getGltfEmissiveTextureIndex() const { return gltfEmissiveTextureIndex; }
356
357 // Lightmap texture accessors
358 [[nodiscard]] bool hasLightmapTexture() const { return !lightmapPath.empty(); }
359 [[nodiscard]] const std::filesystem::path& getLightmapPath() const { return lightmapPath; }
360
361 private:
363 void parsePbrData(const tinygltf::Model &model, const tinygltf::Material &material);
364
366
367 std::optional<MaterialExtensions> materialExtensions;
368 glm::vec4 baseColorFactor = glm::vec4(0.0f, 0.0f, 0.0f, 0.0f);
369 uint32_t textureIndex = std::numeric_limits<uint32_t>::max();
370
371 // PBR texture indices (0xFFFFFFFF = not present)
372 uint32_t normalTextureIndex = 0xFFFFFFFF;
373 uint32_t roughnessMetallicTextureIndex = 0xFFFFFFFF;
374 uint32_t emissiveTextureIndex = 0xFFFFFFFF;
375
376 // PBR material factors
377 float roughnessFactor = 1.0f;
378 float metallicFactor = 0.0f;
379 float normalScale = 1.0f;
380 glm::vec3 emissiveFactor = glm::vec3(0.0f);
381
382 // GLTF indices for mapping to texture paths (-1 = not present)
386
387 // Lightmap path (from VulkanSchnee extension)
388 std::filesystem::path lightmapPath;
389 };
390
396 friend class Core::AssetManager;
397
399
407 GltfMeshPrimitiveData(tinygltf::Model model, tinygltf::Primitive primitive,
408 StaticMeshExtensions meshSettings);
409
415 [[nodiscard]] std::vector<Vertex> getVertices() const;
416
422 [[nodiscard]] std::vector<uint32_t> getIndices() const;
423
424 [[nodiscard]] GltfMaterialData getMaterialData() const;
425
432 [[nodiscard]] RuntimeTexture *getColorTexturePtr() const;
433
434 [[nodiscard]] uint32_t getColorTextureIndex() const;
435
436 [[nodiscard]] const std::vector<BoneVertexData>& getBoneVertexData() const { return boneVertexData; }
437 [[nodiscard]] bool hasSkinData() const { return !boneVertexData.empty(); }
438
439 private:
440 uint32_t textureIndex = std::numeric_limits<uint32_t>::max();
442
443 tinygltf::Primitive primitive;
445 std::vector<Vertex> vertices;
446 std::vector<uint32_t> indices;
447 std::vector<BoneVertexData> boneVertexData;
448 };
449
451 GltfMeshData(const tinygltf::Model &model, const tinygltf::Mesh &mesh, const tinygltf::Node &node,
452 const glm::mat4 &transform);
453
454 [[nodiscard]] std::vector<GltfMeshPrimitiveData> getPrimitives() const { return primitives; }
455 [[nodiscard]] std::vector<GltfMeshPrimitiveData> &getPrimitivesRef() { return primitives; }
456 [[nodiscard]] std::string getName() const { return name; }
457
466 [[nodiscard]] glm::mat4 getTransform() const { return transform; }
467
468 [[nodiscard]] int getSkinIndex() const { return skinIndex; }
469
470 private:
471 std::string name;
472 tinygltf::Mesh mesh;
473 std::vector<GltfMeshPrimitiveData> primitives;
474 glm::mat4 transform;
475 int skinIndex = -1;
476 };
477
488 static Assets::Skin loadSkinData(const tinygltf::Model& model, int skinIndex);
489
501 static Animation loadAnimationData(const tinygltf::Model& model, int animIndex, const Assets::Skin& skin);
502
513 void load(const std::filesystem::path &path, std::vector<GltfMeshData> &meshes) const;
514
515 std::optional<tinygltf::Model> loadModel(const std::filesystem::path &path) const;
516
517 tinygltf::Model loadAsync(const std::filesystem::path &path) const;
518
529 tinygltf::Model loadMetadataOnly(const std::filesystem::path &path) const;
530
531 private:
533 };
534} // namespace Engine::Assets::Loaders
Animation clip loaded from glTF animation data.
std::vector< Vertex > getPrimitiveVertices(const tinygltf::Primitive &primitive)
std::optional< tinygltf::Model > loadModel(const std::filesystem::path &path) const
static Assets::Skin loadSkinData(const tinygltf::Model &model, int skinIndex)
Load skin (skeleton) data from a glTF model.
GltfLoader(NamedThreadPool *threadPool=nullptr)
tinygltf::Model loadMetadataOnly(const std::filesystem::path &path) const
Loads only the GLTF metadata (nodes, names, transforms) without loading buffer data.
void load(const std::filesystem::path &path, std::vector< GltfMeshData > &meshes) const
Loads a vector of meshes from a gltf file.
Ecs::PrimitiveData loadPrimitiveData(GltfVertexData vertexData)
static Animation loadAnimationData(const tinygltf::Model &model, int animIndex, const Assets::Skin &skin)
Load animation data from a glTF model.
tinygltf::Model loadAsync(const std::filesystem::path &path) const
Skeleton definition loaded from glTF skin data.
Definition SkinAsset.h:27
Wrapper for texture data.
Tracy-named thread pool using BS_tracy::tracy_thread_pool.
Engine::Core::MaterialData MaterialData
Core audio subsystem owning the miniaudio engine and managing playback.
Definition AudioConfig.h:9
GltfBufferDataView(const std::vector< tinygltf::Buffer > &buffers, const std::vector< tinygltf::BufferView > &bufferViews, const tinygltf::Accessor &accessor)
Stores the material data which is relevant for our current set of shaders.
Definition GltfLoader.h:319
void parsePbrData(const tinygltf::Model &model, const tinygltf::Material &material)
const Materials::MaterialData & getMaterialData() const
Definition GltfLoader.h:335
GltfMaterialData(const tinygltf::Model &model, const tinygltf::Material &material, const StaticMeshExtensions &meshSettings)
const VulkanSchneeMaterialExtension & getVulkanSchneeExtension() const
const std::filesystem::path & getLightmapPath() const
Definition GltfLoader.h:359
std::optional< MaterialExtensions > materialExtensions
Definition GltfLoader.h:367
std::vector< GltfMeshPrimitiveData > getPrimitives() const
Definition GltfLoader.h:454
int skinIndex
glTF skin index (-1 = no skin)
Definition GltfLoader.h:475
GltfMeshData(const tinygltf::Model &model, const tinygltf::Mesh &mesh, const tinygltf::Node &node, const glm::mat4 &transform)
std::vector< GltfMeshPrimitiveData > & getPrimitivesRef()
Definition GltfLoader.h:455
std::vector< GltfMeshPrimitiveData > primitives
Definition GltfLoader.h:473
glm::mat4 getTransform() const
Gets the model matrix for the object from the file it was imported from.
Definition GltfLoader.h:466
const std::vector< BoneVertexData > & getBoneVertexData() const
Definition GltfLoader.h:436
std::vector< Vertex > getVertices() const
Gets a list of all unprocessed vertices of this primtitive.
RuntimeTexture * getColorTexturePtr() const
Gets the pointer to the primitive of the color texture. This is only a valid pointer after it has bee...
GltfMeshPrimitiveData(tinygltf::Model model, tinygltf::Primitive primitive, StaticMeshExtensions meshSettings)
Extracts all relevant data from the tinygltf importer objects.
std::vector< BoneVertexData > boneVertexData
Empty for static meshes.
Definition GltfLoader.h:447
std::vector< uint32_t > getIndices() const
Gets the raw unprocessed list of all indices.
GltfTextureData(const tinygltf::Image &image)
Definition GltfLoader.h:71
std::optional< GltfBufferDataView > uvDataView
Definition GltfLoader.h:57
std::optional< GltfBufferDataView > positionDataView
Definition GltfLoader.h:54
std::optional< GltfBufferDataView > normalDataView
Definition GltfLoader.h:55
std::optional< GltfBufferDataView > indexDataView
Definition GltfLoader.h:59
GltfVertexData(const std::vector< tinygltf::Buffer > &buffers, const std::vector< tinygltf::BufferView > &bufferViews, const std::vector< tinygltf::Accessor > &accessors, const tinygltf::Primitive &primitive)
std::optional< GltfBufferDataView > jointsDataView
JOINTS_0 (typically uint16 x4)
Definition GltfLoader.h:60
std::optional< GltfBufferDataView > tangentDataView
Definition GltfLoader.h:56
std::optional< GltfBufferDataView > lightmapDataView
Definition GltfLoader.h:58
std::optional< GltfBufferDataView > weightsDataView
WEIGHTS_0 (float x4)
Definition GltfLoader.h:61
std::optional< VulkanSchneeMaterialExtension > vulkanSchneeMaterialExtension
Definition GltfLoader.h:293
MaterialExtensions(const tinygltf::ExtensionMap &material)
Parses all extensions and extracts recognized extensions.
Definition GltfLoader.h:303
StaticMeshExtensions(const tinygltf::Model &model, const tinygltf::ExtensionMap &extensions)
StaticMeshExtensions(const tinygltf::ExtensionMap &extensions)
std::optional< VulkanSchneeExtension > vulkanSchneeExtension
Definition GltfLoader.h:308
CollisionProperties(const tinygltf::Value &extension, const tinygltf::Model *model=nullptr)
VulkanSchneeExtension(const tinygltf::Value &vs_extension, const tinygltf::Model *model=nullptr)
std::optional< DirectionalLightProperties > directionalLightProperties
Definition GltfLoader.h:225
static std::string extractVersion(const tinygltf::Value &vs_extension)
Extracts the version from the material extension.
std::optional< PointLightProperties > pointLightProperties
Definition GltfLoader.h:224
std::optional< LightProperties::LightmapProperties > lightmapProperties
Definition GltfLoader.h:222
std::optional< CollisionProperties > collisionProperties
Definition GltfLoader.h:223
Extracts all material data from the gltf file. All data during runtime will be read from an object of...
Definition GltfLoader.h:254
std::map< std::string, tinygltf::Value > customProperties
Definition GltfLoader.h:278
VulkanSchneeMaterialExtension(const tinygltf::Value &vs_extension)
std::map< std::string, ShaderParameter > shaderParameters
Definition GltfLoader.h:276