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
8
10#include <filesystem>
11#include <glm/gtc/quaternion.hpp>
12#include <limits>
13#include <tiny_gltf.h>
14#include <vector>
15
16namespace Engine::Assets
17{
18 class RuntimeTexture;
19 class Texture;
20} // namespace Engine::Assets
21
22namespace Engine::Core
23{
24 class AssetManager;
25} // namespace Engine::Core
26
28{
29
38 {
39 public:
45
50
58 {
66 const std::vector<tinygltf::Buffer> & buffers,
67 const std::vector<tinygltf::BufferView> & bufferViews,
68 const tinygltf::Accessor & accessor
69 );
70
74 const tinygltf::Accessor & accessor;
75
79 const tinygltf::BufferView & bufferView;
80
84 const tinygltf::Buffer & buffer;
85
89 const float * data;
90 };
91
96 {
105 const std::vector<tinygltf::Buffer> & buffers,
106 const std::vector<tinygltf::BufferView> & bufferViews,
107 const std::vector<tinygltf::Accessor> & accessors,
108 const tinygltf::Primitive & primitive
109 );
110
114 std::optional<GltfBufferDataView> positionDataView;
115
119 std::optional<GltfBufferDataView> normalDataView;
120
125 std::optional<GltfBufferDataView>
126 tangentDataView; // TANGENT attribute (vec4: xyz=tangent, w=handedness)
127
131 std::optional<GltfBufferDataView> uvDataView;
132
136 std::optional<GltfBufferDataView> lightmapDataView;
137
141 std::optional<GltfBufferDataView> indexDataView;
142
146 std::optional<GltfBufferDataView> jointsDataView;
147
151 std::optional<GltfBufferDataView> weightsDataView;
152 };
153
160 std::vector<Vertex> getPrimitiveVertices( const tinygltf::Primitive & primitive );
161
168
173 {
177 GltfTextureData() = default;
178
183 GltfTextureData( const tinygltf::Image & image ) : image( image ) {}
184
189 [[nodiscard]] tinygltf::Image getImage() const
190 {
191 return image;
192 }
193
198 [[nodiscard]] bool hasTextureData() const;
199
200 private:
201 tinygltf::Image image = tinygltf::Image();
202 };
203
208 {
213
218 StaticMeshSettings( const tinygltf::Value & extras );
219
223 bool bUsesLightMaps = false;
224
228 std::filesystem::path lightMapPath = std::filesystem::path();
229
233 int lightMapUvIndex = std::numeric_limits<int>::max();
234 };
235
241 {
243
245 const tinygltf::Value & vs_extension,
246 const tinygltf::Model * model = nullptr
247 );
248
253 {
254 explicit MeshProperties( const tinygltf::Value & extension );
257 };
258
263 {
264 explicit LightProperties( const tinygltf::Value & extension );
265
267 {
268 explicit LightmapProperties( const tinygltf::Value & extension );
269 std::filesystem::path lightmapPath;
270 std::filesystem::path lightmapKtxPath;
272
274 [[nodiscard]] const std::filesystem::path & getRuntimeLightmapPath() const;
275 };
276
277 bool getCastShadows() const;
278 bool getReceiveShadows() const;
279 bool getHasLightmaps() const;
280
281 private:
282 std::optional<LightmapProperties> lightmapProperties;
283
287 };
288
289 struct metadata
290 {
291 std::string blenderObjectName;
292 };
293
301 {
312
315 const tinygltf::Value & extension,
316 const tinygltf::Model * model = nullptr
317 );
318
320 bool generateConvexHull = false;
321 bool isTrigger = false;
322 glm::vec3 offset{ 0.0f };
323
324 // Primitive params
325 glm::vec3 boxHalfExtents{ 0.5f };
326 float sphereRadius = 0.5f;
327 float capsuleRadius = 0.5f;
328 float capsuleHeight = 1.0f;
329
330 // Convex/mesh data
331 std::vector<glm::vec3> vertices;
332 std::vector<uint32_t> indices;
333
334 // Compound children
336 {
337 std::string name;
338 std::vector<glm::vec3> vertices;
339 std::vector<uint32_t> indices;
340 std::vector<uint32_t> edgeIndices;
341 glm::vec3 offset{ 0.0f };
342 glm::quat rotation{ glm::identity<glm::quat>() };
343 };
344
345 std::vector<CompoundChild> compoundChildren;
346
347 [[nodiscard]] bool hasCollision() const
348 {
349 return shapeType != ShapeType::NONE;
350 }
351
352 private:
353 static ShapeType parseShapeType( const std::string & typeStr );
354 };
355
357 {
358 explicit PointLightProperties( const tinygltf::Value & extension );
359
360 glm::vec3 color{ 1.0f };
361 float intensity = 1.0f;
362 float radius = 10.0f;
363 float cullDistance = 100.0f;
364 };
365
367 {
368 explicit DirectionalLightProperties( const tinygltf::Value & extension );
369
370 glm::vec3 color{ 1.0f };
371 glm::vec3 direction{ 0.0f, -1.0f, 0.0f };
372 float intensity = 1.0f;
373 bool enabled = true;
374 bool castsShadows = true;
375 bool hasDirection = false;
377 };
378
379 std::optional<LightProperties> lightProperties;
380 std::optional<MeshProperties> meshProperties;
381 std::optional<LightProperties::LightmapProperties> lightmapProperties;
382 std::optional<CollisionProperties> collisionProperties;
383 std::optional<PointLightProperties> pointLightProperties;
384 std::optional<DirectionalLightProperties> directionalLightProperties;
385
386 [[nodiscard]] bool hasLightProperties() const;
387 [[nodiscard]] bool hasLightmapProperties() const;
388 [[nodiscard]] bool hasMeshProperties() const;
389 [[nodiscard]] bool hasCollisionProperties() const;
390 [[nodiscard]] bool hasPointLightProperties() const;
391 [[nodiscard]] bool hasDirectionalLightProperties() const;
392
393 private:
404 static std::string extractVersion( const tinygltf::Value & vs_extension );
405 };
406
415 {
417
418 explicit VulkanSchneeMaterialExtension( const tinygltf::Value & vs_extension );
419
427 {
428 explicit ShaderParameter( const tinygltf::Value & param );
429
430 std::string type; // "connected" or "value"
431 std::string socketType; // Socket type from Blender
432 tinygltf::Value value; // Only valid if type == "value"
433 };
434
435 std::string shaderType;
437
438 std::map<std::string, ShaderParameter> shaderParameters;
439 std::map<std::string, double> nodeProperties;
440 std::map<std::string, tinygltf::Value> customProperties;
441 bool normalMapBC5 = false; // True when normal map uses BC5 (2-channel RG)
442 };
443
452 {
454
459 explicit MaterialExtensions( const tinygltf::ExtensionMap & material );
460
464 std::optional<VulkanSchneeMaterialExtension> vulkanSchneeMaterialExtension;
465 };
466
474 {
476
481 explicit StaticMeshExtensions( const tinygltf::ExtensionMap & extensions );
482
488 StaticMeshExtensions( const tinygltf::Model & model, const tinygltf::ExtensionMap & extensions );
489
493 std::optional<VulkanSchneeExtension> vulkanSchneeExtension;
494 };
495
505 {
506 GltfMaterialData() = default;
507
509 const tinygltf::Model & model,
510 const tinygltf::Material & material,
511 const StaticMeshExtensions & meshSettings
512 );
513
518 [[nodiscard]] bool hasTexture() const;
519
524 [[nodiscard]] uint32_t getTextureIndex() const;
525
530 [[nodiscard]] bool hasVulkanSchneeExtension() const;
531
537
542 [[nodiscard]] std::string getShaderName() const;
543
548 [[nodiscard]] const Materials::MaterialData & getMaterialData() const
549 {
550 return materialData;
551 }
552
558 {
559 return materialData;
560 }
561
566 [[nodiscard]] uint32_t getNormalTextureIndex() const
567 {
568 return normalTextureIndex;
569 }
570
575 [[nodiscard]] uint32_t getRoughnessMetallicTextureIndex() const
576 {
578 }
579
584 [[nodiscard]] uint32_t getEmissiveTextureIndex() const
585 {
587 }
588
593 [[nodiscard]] float getRoughnessFactor() const
594 {
595 return roughnessFactor;
596 }
597
602 [[nodiscard]] float getMetallicFactor() const
603 {
604 return metallicFactor;
605 }
606
611 [[nodiscard]] float getNormalScale() const
612 {
613 return normalScale;
614 }
615
620 [[nodiscard]] glm::vec3 getEmissiveFactor() const
621 {
622 return emissiveFactor;
623 }
624
629 [[nodiscard]] bool hasNormalTexture() const
630 {
631 return gltfNormalTextureIndex >= 0;
632 }
633
638 [[nodiscard]] bool hasMetallicRoughnessTexture() const
639 {
641 }
642
647 [[nodiscard]] bool hasEmissiveTexture() const
648 {
649 return gltfEmissiveTextureIndex >= 0;
650 }
651
656 [[nodiscard]] int32_t getGltfNormalTextureIndex() const
657 {
659 }
660
665 [[nodiscard]] int32_t getGltfMetallicRoughnessTextureIndex() const
666 {
668 }
669
674 [[nodiscard]] int32_t getGltfEmissiveTextureIndex() const
675 {
677 }
678
683 [[nodiscard]] bool hasLightmapTexture() const
684 {
685 return !lightmapPath.empty();
686 }
687
692 [[nodiscard]] const std::filesystem::path & getLightmapPath() const
693 {
694 return lightmapPath;
695 }
696
697 private:
699 void parsePbrData( const tinygltf::Model & model, const tinygltf::Material & material );
700
702
703 std::optional<MaterialExtensions> materialExtensions;
704 glm::vec4 baseColorFactor = glm::vec4( 0.0f, 0.0f, 0.0f, 0.0f );
705 uint32_t textureIndex = std::numeric_limits<uint32_t>::max();
706
707 // PBR texture indices (0xFFFFFFFF = not present)
708 uint32_t normalTextureIndex = 0xFFFFFFFF;
709 uint32_t roughnessMetallicTextureIndex = 0xFFFFFFFF;
710 uint32_t emissiveTextureIndex = 0xFFFFFFFF;
711
712 // PBR material factors
713 float roughnessFactor = 1.0f;
714 float metallicFactor = 0.0f;
715 float normalScale = 1.0f;
716 glm::vec3 emissiveFactor = glm::vec3( 0.0f );
717
718 // GLTF indices for mapping to texture paths (-1 = not present)
722
723 // Lightmap path (from VulkanSchnee extension)
724 std::filesystem::path lightmapPath;
725 };
726
733 {
734 friend class Core::AssetManager;
735
740
749 tinygltf::Model model,
750 tinygltf::Primitive primitive,
751 StaticMeshExtensions meshSettings
752 );
753
759 [[nodiscard]] std::vector<Vertex> getVertices() const;
760
766 [[nodiscard]] std::vector<uint32_t> getIndices() const;
767
772 [[nodiscard]] GltfMaterialData getMaterialData() const;
773
781 [[nodiscard]] RuntimeTexture * getColorTexturePtr() const;
782
787 [[nodiscard]] uint32_t getColorTextureIndex() const;
788
793 [[nodiscard]] const std::vector<BoneVertexData> & getBoneVertexData() const
794 {
795 return boneVertexData;
796 }
797
802 [[nodiscard]] bool hasSkinData() const
803 {
804 return !boneVertexData.empty();
805 }
806
807 private:
808 uint32_t textureIndex = std::numeric_limits<uint32_t>::max();
810
811 tinygltf::Primitive primitive;
813 std::vector<Vertex> vertices;
814 std::vector<uint32_t> indices;
815 std::vector<BoneVertexData> boneVertexData;
816 };
817
822 {
831 const tinygltf::Model & model,
832 const tinygltf::Mesh & mesh,
833 const tinygltf::Node & node,
834 const glm::mat4 & transform
835 );
836
841 [[nodiscard]] std::vector<GltfMeshPrimitiveData> getPrimitives() const
842 {
843 return primitives;
844 }
845
850 [[nodiscard]] std::vector<GltfMeshPrimitiveData> & getPrimitivesRef()
851 {
852 return primitives;
853 }
854
859 [[nodiscard]] std::string getName() const
860 {
861 return name;
862 }
863
872 [[nodiscard]] glm::mat4 getTransform() const
873 {
874 return transform;
875 }
876
881 [[nodiscard]] int getSkinIndex() const
882 {
883 return skinIndex;
884 }
885
886 private:
887 std::string name;
888 tinygltf::Mesh mesh;
889 std::vector<GltfMeshPrimitiveData> primitives;
890 glm::mat4 transform;
891 int skinIndex = -1;
892 };
893
904 static Assets::Skin loadSkinData( const tinygltf::Model & model, int skinIndex );
905
917 static Animation
918 loadAnimationData( const tinygltf::Model & model, int animIndex, const Assets::Skin & skin );
919
930 void load( const std::filesystem::path & path, std::vector<GltfMeshData> & meshes ) const;
931
942 std::optional<tinygltf::Model> loadModel( const std::filesystem::path & path ) const;
943
953 tinygltf::Model loadAsync( const std::filesystem::path & path ) const;
954
965 tinygltf::Model loadMetadataOnly( const std::filesystem::path & path ) const;
966
967 private:
969 };
970} // namespace Engine::Assets::Loaders
Animation clip loaded from glTF animation data.
std::vector< Vertex > getPrimitiveVertices(const tinygltf::Primitive &primitive)
Legacy primitive vertex extraction entry point.
std::optional< tinygltf::Model > loadModel(const std::filesystem::path &path) const
Loads a full ASCII glTF model through tinygltf.
static Assets::Skin loadSkinData(const tinygltf::Model &model, int skinIndex)
Load skin (skeleton) data from a glTF model.
GltfLoader(NamedThreadPool *threadPool=nullptr)
Creates a loader that can use an optional thread pool for deferred work.
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)
Converts resolved glTF vertex and index views into engine primitive data.
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
Loads a glTF model while skipping image payload loading.
~GltfLoader()
Waits for queued work on the supplied thread pool before destruction.
Runtime-owned Vulkan texture resource.
Definition Texture.h:33
Skeleton definition loaded from glTF skin data.
Definition SkinAsset.h:30
Wrapper for texture data.
Tracy-named thread pool using BS_tracy::tracy_thread_pool.
Engine::Core::MaterialData MaterialData
Manages IBL (Image-Based Lighting) resources for specular reflections.
PipelineNames
Named graphics pipelines used by material selection and shader lookup.
const tinygltf::Buffer & buffer
Buffer referenced by bufferView.buffer.
Definition GltfLoader.h:84
GltfBufferDataView(const std::vector< tinygltf::Buffer > &buffers, const std::vector< tinygltf::BufferView > &bufferViews, const tinygltf::Accessor &accessor)
Resolves an accessor to its buffer view, buffer, and raw data pointer.
const tinygltf::Accessor & accessor
Accessor that describes the element count, type, and component type.
Definition GltfLoader.h:74
const tinygltf::BufferView & bufferView
Buffer view referenced by accessor.bufferView.
Definition GltfLoader.h:79
const float * data
Raw pointer to the first accessor byte, interpreted as float data by callers.
Definition GltfLoader.h:89
Stores the material data which is relevant for our current set of shaders.
Definition GltfLoader.h:505
float getMetallicFactor() const
Gets the glTF metallic factor.
Definition GltfLoader.h:602
glm::vec3 getEmissiveFactor() const
Gets the emissive factor.
Definition GltfLoader.h:620
bool hasVulkanSchneeExtension() const
Checks whether the material has a Vulkan Schnee material extension.
void parsePbrData(const tinygltf::Model &model, const tinygltf::Material &material)
const Materials::MaterialData & getMaterialData() const
Gets immutable engine material data generated from glTF and extension data.
Definition GltfLoader.h:548
uint32_t getNormalTextureIndex() const
Gets the engine normal texture index.
Definition GltfLoader.h:566
bool hasTexture() const
Checks whether the material references a base color texture.
GltfMaterialData(const tinygltf::Model &model, const tinygltf::Material &material, const StaticMeshExtensions &meshSettings)
int32_t getGltfMetallicRoughnessTextureIndex() const
Gets the original glTF metallic-roughness texture index.
Definition GltfLoader.h:665
bool hasEmissiveTexture() const
Checks whether the glTF material references an emissive texture.
Definition GltfLoader.h:647
bool hasNormalTexture() const
Checks whether the glTF material references a normal texture.
Definition GltfLoader.h:629
uint32_t getTextureIndex() const
Gets the engine texture index for the base color texture.
const VulkanSchneeMaterialExtension & getVulkanSchneeExtension() const
Gets the parsed Vulkan Schnee material extension.
const std::filesystem::path & getLightmapPath() const
Gets the lightmap texture path from the engine extension.
Definition GltfLoader.h:692
int32_t getGltfNormalTextureIndex() const
Gets the original glTF normal texture index.
Definition GltfLoader.h:656
int32_t getGltfEmissiveTextureIndex() const
Gets the original glTF emissive texture index.
Definition GltfLoader.h:674
float getRoughnessFactor() const
Gets the glTF roughness factor.
Definition GltfLoader.h:593
uint32_t getEmissiveTextureIndex() const
Gets the engine emissive texture index.
Definition GltfLoader.h:584
std::optional< MaterialExtensions > materialExtensions
Definition GltfLoader.h:703
float getNormalScale() const
Gets the normal map scale.
Definition GltfLoader.h:611
uint32_t getRoughnessMetallicTextureIndex() const
Gets the engine metallic-roughness texture index.
Definition GltfLoader.h:575
bool hasMetallicRoughnessTexture() const
Checks whether the glTF material references a metallic-roughness texture.
Definition GltfLoader.h:638
Materials::MaterialData & getMaterialDataRef()
Gets mutable engine material data generated from glTF and extension data.
Definition GltfLoader.h:557
bool hasLightmapTexture() const
Checks whether this material uses a lightmap texture path.
Definition GltfLoader.h:683
std::string getShaderName() const
Gets the shader name selected for this material.
std::vector< GltfMeshPrimitiveData > getPrimitives() const
Gets a copy of parsed primitives.
Definition GltfLoader.h:841
std::string getName() const
Gets the mesh node name.
Definition GltfLoader.h:859
int skinIndex
glTF skin index (-1 = no skin)
Definition GltfLoader.h:891
GltfMeshData(const tinygltf::Model &model, const tinygltf::Mesh &mesh, const tinygltf::Node &node, const glm::mat4 &transform)
Parses one mesh node into engine primitive data.
std::vector< GltfMeshPrimitiveData > & getPrimitivesRef()
Gets mutable parsed primitives.
Definition GltfLoader.h:850
std::vector< GltfMeshPrimitiveData > primitives
Definition GltfLoader.h:889
glm::mat4 getTransform() const
Gets the model matrix for the object from the file it was imported from.
Definition GltfLoader.h:872
int getSkinIndex() const
Gets the glTF skin index referenced by the mesh node.
Definition GltfLoader.h:881
GltfMaterialData getMaterialData() const
Gets material data parsed for this primitive.
const std::vector< BoneVertexData > & getBoneVertexData() const
Gets per-vertex bone weights and joint indices.
Definition GltfLoader.h:793
GltfMeshPrimitiveData()=default
Creates empty primitive data.
bool hasSkinData() const
Checks whether this primitive has skinning data.
Definition GltfLoader.h:802
uint32_t getColorTextureIndex() const
Gets the glTF color texture index referenced by this primitive.
std::vector< Vertex > getVertices() const
Gets the vertices parsed for this primitive.
RuntimeTexture * getColorTexturePtr() const
Gets the pointer to the primitive of the color texture.
GltfMeshPrimitiveData(tinygltf::Model model, tinygltf::Primitive primitive, StaticMeshExtensions meshSettings)
Extracts primitive geometry and material data from tinygltf objects.
std::vector< BoneVertexData > boneVertexData
Empty for static meshes.
Definition GltfLoader.h:815
std::vector< uint32_t > getIndices() const
Gets the raw unprocessed list of all indices.
GltfTextureData()=default
Creates an empty texture payload.
bool hasTextureData() const
Checks whether an image payload is stored.
GltfTextureData(const tinygltf::Image &image)
Stores an image payload from tinygltf.
Definition GltfLoader.h:183
tinygltf::Image getImage() const
Gets the stored tinygltf image.
Definition GltfLoader.h:189
Resolved vertex attribute and index views for a glTF primitive.
Definition GltfLoader.h:96
std::optional< GltfBufferDataView > uvDataView
TEXCOORD_0 attribute view when present.
Definition GltfLoader.h:131
std::optional< GltfBufferDataView > positionDataView
POSITION attribute view when present.
Definition GltfLoader.h:114
std::optional< GltfBufferDataView > normalDataView
NORMAL attribute view when present.
Definition GltfLoader.h:119
std::optional< GltfBufferDataView > indexDataView
Primitive index accessor view.
Definition GltfLoader.h:141
GltfVertexData(const std::vector< tinygltf::Buffer > &buffers, const std::vector< tinygltf::BufferView > &bufferViews, const std::vector< tinygltf::Accessor > &accessors, const tinygltf::Primitive &primitive)
Extracts known attribute views from a primitive.
std::optional< GltfBufferDataView > jointsDataView
JOINTS_0 attribute view when present.
Definition GltfLoader.h:146
std::optional< GltfBufferDataView > tangentDataView
TANGENT attribute view when present. The xyz values store tangent direction and w stores handedness.
Definition GltfLoader.h:126
std::optional< GltfBufferDataView > lightmapDataView
TEXCOORD_1 attribute view when present.
Definition GltfLoader.h:136
std::optional< GltfBufferDataView > weightsDataView
WEIGHTS_0 attribute view when present.
Definition GltfLoader.h:151
std::optional< VulkanSchneeMaterialExtension > vulkanSchneeMaterialExtension
Parsed Vulkan Schnee material extension when present.
Definition GltfLoader.h:464
MaterialExtensions(const tinygltf::ExtensionMap &material)
Parses material-level glTF extensions recognized by the engine.
Parses all extensions and extracts recognized extensions.
Definition GltfLoader.h:474
StaticMeshExtensions(const tinygltf::Model &model, const tinygltf::ExtensionMap &extensions)
Parses mesh or node extensions with access to model-dependent data.
StaticMeshExtensions(const tinygltf::ExtensionMap &extensions)
Parses mesh or node extensions without model-dependent data.
std::optional< VulkanSchneeExtension > vulkanSchneeExtension
Parsed Vulkan Schnee engine extension when present.
Definition GltfLoader.h:493
StaticMeshSettings(const tinygltf::Value &extras)
Parses lightmap settings from a tinygltf extras object.
StaticMeshSettings()=default
Creates settings with lightmaps disabled.
std::filesystem::path lightMapPath
Path to the baked lightmap texture.
Definition GltfLoader.h:228
bool bUsesLightMaps
True when the mesh uses baked lightmaps.
Definition GltfLoader.h:223
int lightMapUvIndex
UV channel index used for lightmap sampling.
Definition GltfLoader.h:233
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:384
static std::string extractVersion(const tinygltf::Value &vs_extension)
Extracts the version from the material extension.
std::optional< PointLightProperties > pointLightProperties
Definition GltfLoader.h:383
std::optional< LightProperties::LightmapProperties > lightmapProperties
Definition GltfLoader.h:381
std::optional< CollisionProperties > collisionProperties
Definition GltfLoader.h:382
Extracts all material data from the gltf file. All data during runtime will be read from an object of...
Definition GltfLoader.h:415
std::map< std::string, tinygltf::Value > customProperties
Definition GltfLoader.h:440
VulkanSchneeMaterialExtension(const tinygltf::Value &vs_extension)
std::map< std::string, ShaderParameter > shaderParameters
Definition GltfLoader.h:438