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"
4#include <filesystem>
5#include <tiny_gltf.h>
6#include <vector>
7
11
12namespace EngineCore {
13 enum PipelineNames : int;
14}
15
16namespace EngineCore {
17 class AssetManager;
18}
19
20namespace EngineCore {
21 class Texture;
22
23 class GltfLoader {
24 public:
26
28
29
30
32 GltfBufferDataView(const std::vector<tinygltf::Buffer>& buffers,
33 const std::vector<tinygltf::BufferView>& bufferViews,
34 const tinygltf::Accessor& accessor);
35
36 const tinygltf::Accessor& accessor;
37 const tinygltf::BufferView& bufferView;
38 const tinygltf::Buffer& buffer;
39 const float *data;
40 };
41
43 GltfVertexData(const std::vector<tinygltf::Buffer> &buffers,
44 const std::vector<tinygltf::BufferView> &bufferViews,
45 const std::vector<tinygltf::Accessor> &accessors, const tinygltf::Primitive &primitive);
46 std::optional<GltfBufferDataView> positionDataView;
47 std::optional<GltfBufferDataView> normalDataView;
48 std::optional<GltfBufferDataView> tangentDataView; // TANGENT attribute (vec4: xyz=tangent, w=handedness)
49 std::optional<GltfBufferDataView> uvDataView;
50 std::optional<GltfBufferDataView> lightmapDataView;
51 std::optional<GltfBufferDataView> indexDataView;
52 };
53
54 std::vector<Vertex> getPrimitiveVertices(const tinygltf::Primitive& primitive);
55
57
59 GltfTextureData() = default;
60
61 GltfTextureData(const tinygltf::Image &image) : image(image) {
62 }
63
64 [[nodiscard]] tinygltf::Image getImage() const { return image; }
65
66 [[nodiscard]] bool hasTextureData() const;
67
68 private:
69 tinygltf::Image image = tinygltf::Image();
70
71 };
72
74 StaticMeshSettings() = default;
75
76 StaticMeshSettings(const tinygltf::Value &extras);
77
78 bool bUsesLightMaps = false;
79 std::filesystem::path lightMapPath = std::filesystem::path();
80 int lightMapUvIndex = std::numeric_limits<int>::max();
81 };
82
91
92 explicit VulkanSchneeExtension(const tinygltf::Value& vs_extension);
93
101 explicit MeshProperties(const tinygltf::Value& extension);
104 };
105
113 explicit LightProperties(const tinygltf::Value& extension);
114
116 explicit LightmapProperties(const tinygltf::Value& extension);
117 std::filesystem::path lightmapPath;
119
120 bool isLightmapPathValid() const;
121 };
122
123 bool getCastShadows() const;
124 bool getReceiveShadows() const;
125 bool getHasLightmaps() const;
126
127 private:
128
129 std::optional<LightmapProperties> lightmapProperties;
130
134 };
135
136 struct metadata {
137
138 std::string blenderObjectName;
139 };
140
141 std::optional<LightProperties> lightProperties;
142 std::optional<MeshProperties> meshProperties;
143 std::optional<LightProperties::LightmapProperties> lightmapProperties;
144
145 [[nodiscard]] bool hasLightProperties() const;
146 [[nodiscard]] bool hasLightmapProperties() const;
147 [[nodiscard]] bool hasMeshProperties() const;
148
149 private:
160 static std::string extractVersion(const tinygltf::Value& vs_extension);
161 };
162
171
172 explicit VulkanSchneeMaterialExtension(const tinygltf::Value& vs_extension);
173
181 explicit ShaderParameter(const tinygltf::Value& param);
182
183 std::string type; // "connected" or "value"
184 std::string socketType; // Socket type from Blender
185 tinygltf::Value value; // Only valid if type == "value"
186 };
187
188 std::string shaderType;
190
191 std::map<std::string, ShaderParameter> shaderParameters;
192 std::map<std::string, double> nodeProperties;
193 std::map<std::string, tinygltf::Value> customProperties;
194 };
195
204
205 explicit MaterialExtensions(const tinygltf::ExtensionMap &material);
206
207 std::optional<VulkanSchneeMaterialExtension> vulkanSchneeMaterialExtension;
208 };
209
210
219 explicit StaticMeshExtensions(const tinygltf::ExtensionMap &extensions);
220
221 std::optional<VulkanSchneeExtension> vulkanSchneeExtension;
222 };
223
233 GltfMaterialData() = default;
234
235 GltfMaterialData(const tinygltf::Model &model, const tinygltf::Material &material,
236 const StaticMeshExtensions &meshSettings);
237
238 [[nodiscard]] tinygltf::Material getMaterial() const { return material; }
239
240 [[nodiscard]] bool hasTexture() const;
241
242 [[nodiscard]] uint32_t getTextureIndex() const;
243
244 [[nodiscard]] GltfTextureData getTextureData() const;
245
246 [[nodiscard]] bool hasVulkanSchneeExtension() const;
247
248 [[nodiscard]] const VulkanSchneeMaterialExtension& getVulkanSchneeExtension() const;
249
250 [[nodiscard]] std::string getShaderName() const;
251
252 template<typename T>
253 T getMaterialData() const;
254
255 std::variant<
265 > getMaterialDataRaw() const;
266
267 // PBR texture accessors (0xFFFFFFFF = not present)
268 [[nodiscard]] uint32_t getNormalTextureIndex() const { return normalTextureIndex; }
269 [[nodiscard]] uint32_t getRoughnessMetallicTextureIndex() const { return roughnessMetallicTextureIndex; }
270 [[nodiscard]] uint32_t getEmissiveTextureIndex() const { return emissiveTextureIndex; }
271
272 // PBR factor accessors
273 [[nodiscard]] float getRoughnessFactor() const { return roughnessFactor; }
274 [[nodiscard]] float getMetallicFactor() const { return metallicFactor; }
275 [[nodiscard]] float getNormalScale() const { return normalScale; }
276 [[nodiscard]] glm::vec3 getEmissiveFactor() const { return emissiveFactor; }
277
278 // PBR texture data accessors
279 [[nodiscard]] bool hasNormalTexture() const { return gltfNormalTextureIndex >= 0; }
280 [[nodiscard]] bool hasMetallicRoughnessTexture() const { return gltfMetallicRoughnessTextureIndex >= 0; }
281 [[nodiscard]] bool hasEmissiveTexture() const { return gltfEmissiveTextureIndex >= 0; }
282 [[nodiscard]] const GltfTextureData& getNormalTextureData() const { return normalTextureData; }
284 [[nodiscard]] const GltfTextureData& getEmissiveTextureData() const { return emissiveTextureData; }
285 [[nodiscard]] int32_t getGltfNormalTextureIndex() const { return gltfNormalTextureIndex; }
287 [[nodiscard]] int32_t getGltfEmissiveTextureIndex() const { return gltfEmissiveTextureIndex; }
288
289 // Lightmap texture accessors
290 [[nodiscard]] bool hasLightmapTexture() const { return !lightmapPath.empty(); }
291 [[nodiscard]] const std::filesystem::path& getLightmapPath() const { return lightmapPath; }
292 [[nodiscard]] const GltfTextureData& getLightmapTextureData() const { return lightmapTextureData; }
293
294 private:
295 void setupMaterialData();
296 void parsePbrData(const tinygltf::Model &model, const tinygltf::Material &material);
297
298 std::variant<
309
310 std::optional<MaterialExtensions> materialExtensions;
311 tinygltf::Material material = tinygltf::Material();
312 glm::vec4 baseColorFactor = glm::vec4(0.0f, 0.0f, 0.0f, 0.0f);
313 uint32_t textureIndex = std::numeric_limits<uint32_t>::max();
315
316 // PBR texture indices (0xFFFFFFFF = not present)
317 uint32_t normalTextureIndex = 0xFFFFFFFF;
318 uint32_t roughnessMetallicTextureIndex = 0xFFFFFFFF;
319 uint32_t emissiveTextureIndex = 0xFFFFFFFF;
320
321 // PBR material factors
322 float roughnessFactor = 1.0f;
323 float metallicFactor = 0.0f;
324 float normalScale = 1.0f;
325 glm::vec3 emissiveFactor = glm::vec3(0.0f);
326
327 // PBR texture data (image data for loading into descriptor array)
331
332 // GLTF indices for mapping to texture paths (-1 = not present)
336
337 // Lightmap texture data (from VulkanSchnee extension, loaded from EXR file)
338 std::filesystem::path lightmapPath;
340 };
341
352
354
365 GltfMeshPrimitiveData(tinygltf::Model model, tinygltf::Primitive primitive,
366 StaticMeshExtensions meshSettings);
367
376 [[nodiscard]] std::vector<Vertex> getVertices() const;
377
386 [[nodiscard]] std::vector<uint32_t> getIndices() const;
387
388 [[nodiscard]] GltfMaterialData getMaterialData() const;
389
399 [[nodiscard]] Texture *getColorTexturePtr() const;
400
401 [[nodiscard]] uint32_t getColorTextureIndex() const;
402
403 private:
404 uint32_t textureIndex = 0;
406
407 tinygltf::Primitive primitive;
409 std::vector<Vertex> vertices;
410 std::vector<uint32_t> indices;
411 };
412
414 GltfMeshData(const tinygltf::Model &model, const tinygltf::Mesh &mesh, const tinygltf::Node &node,
415 const glm::mat4 &transform);
416
417 [[nodiscard]] std::vector<GltfMeshPrimitiveData> getPrimitives() const { return primitives; }
418 [[nodiscard]] std::vector<GltfMeshPrimitiveData> &getPrimitivesRef() { return primitives; }
419 [[nodiscard]] std::string getName() const { return name; }
420
429 [[nodiscard]] glm::mat4 getTransform() const { return transform; }
430
431 private:
432 std::string name;
433 tinygltf::Mesh mesh;
434 std::vector<GltfMeshPrimitiveData> primitives;
435 glm::mat4 transform;
436 };
437
452 void load(const std::filesystem::path &path, std::vector<GltfMeshData> &meshes) const;
453
454 std::optional<tinygltf::Model> loadModel(const std::filesystem::path &path) const;
455
456 tinygltf::Model loadAsync(const std::filesystem::path &path) const;
457
468 tinygltf::Model loadMetadataOnly(const std::filesystem::path &path) const;
469
470 private:
472 };
473
474 template<typename T>
476 return std::get<T>(materialData);
477 }
478} // namespace EngineCore
Material data for an Object which displays a flat color.
Dynamic textures material with PBR and lightmap support.
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.
std::optional< tinygltf::Model > loadModel(const std::filesystem::path &path) const
tinygltf::Model loadAsync(const std::filesystem::path &path) const
NamedThreadPool * threadPool
Definition GltfLoader.h:471
Ecs::PrimitiveData loadPrimitiveData(GltfVertexData vertexData)
GltfLoader(NamedThreadPool *threadPool=nullptr)
std::vector< Vertex > getPrimitiveVertices(const tinygltf::Primitive &primitive)
L1 Spherical Harmoics shader.
Moveable diffuse shader with PBR and lightmap support.
Material data for an object which displays its normals.
Static lightmap material with PBR support.
Tracy-named thread pool using BS_tracy::tracy_thread_pool.
Log category system implementation.
const tinygltf::Accessor & accessor
Definition GltfLoader.h:36
const tinygltf::BufferView & bufferView
Definition GltfLoader.h:37
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:232
int32_t getGltfMetallicRoughnessTextureIndex() const
Definition GltfLoader.h:286
void parsePbrData(const tinygltf::Model &model, const tinygltf::Material &material)
tinygltf::Material getMaterial() const
Definition GltfLoader.h:238
uint32_t getRoughnessMetallicTextureIndex() const
Definition GltfLoader.h:269
const GltfTextureData & getEmissiveTextureData() const
Definition GltfLoader.h:284
const VulkanSchneeMaterialExtension & getVulkanSchneeExtension() const
std::variant< NormalMaterialData, DiffuseFlatColorMaterialData, DiffuseShaderMaterialData, MovableDiffuseShaderMaterialData, L0ShaderMaterialData, L1ShaderMaterialData, L2ShaderMaterialData, DynamicTexturesMaterialData, StaticLightmapMaterialData > materialData
Definition GltfLoader.h:308
const GltfTextureData & getLightmapTextureData() const
Definition GltfLoader.h:292
const GltfTextureData & getMetallicRoughnessTextureData() const
Definition GltfLoader.h:283
const GltfTextureData & getNormalTextureData() const
Definition GltfLoader.h:282
std::variant< NormalMaterialData, DiffuseFlatColorMaterialData, DiffuseShaderMaterialData, MovableDiffuseShaderMaterialData, L0ShaderMaterialData, L1ShaderMaterialData, L2ShaderMaterialData, DynamicTexturesMaterialData, StaticLightmapMaterialData > getMaterialDataRaw() const
const std::filesystem::path & getLightmapPath() const
Definition GltfLoader.h:291
std::optional< MaterialExtensions > materialExtensions
Definition GltfLoader.h:310
GltfMeshData(const tinygltf::Model &model, const tinygltf::Mesh &mesh, const tinygltf::Node &node, const glm::mat4 &transform)
std::vector< GltfMeshPrimitiveData > & getPrimitivesRef()
Definition GltfLoader.h:418
glm::mat4 getTransform() const
Gets the model matrix for the object from the file it was imported from.
Definition GltfLoader.h:429
std::vector< GltfMeshPrimitiveData > getPrimitives() const
Definition GltfLoader.h:417
std::vector< GltfMeshPrimitiveData > primitives
Definition GltfLoader.h:434
std::vector< uint32_t > getIndices() const
Gets the raw unprocessed list of all indices.
std::vector< Vertex > getVertices() const
Gets a list of all unprocessed vertices of this primtitive.
Texture * getColorTexturePtr() const
Gets the pointer to the primitive of the color texture. This is only a valid pointer after it has bee...
tinygltf::Image getImage() const
Definition GltfLoader.h:64
GltfTextureData(const tinygltf::Image &image)
Definition GltfLoader.h:61
std::optional< GltfBufferDataView > positionDataView
Definition GltfLoader.h:46
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 > normalDataView
Definition GltfLoader.h:47
std::optional< GltfBufferDataView > indexDataView
Definition GltfLoader.h:51
std::optional< GltfBufferDataView > lightmapDataView
Definition GltfLoader.h:50
std::optional< GltfBufferDataView > uvDataView
Definition GltfLoader.h:49
std::optional< GltfBufferDataView > tangentDataView
Definition GltfLoader.h:48
std::optional< VulkanSchneeMaterialExtension > vulkanSchneeMaterialExtension
Definition GltfLoader.h:207
Parses all extensions and extracts recognized extensions.
Definition GltfLoader.h:217
std::optional< VulkanSchneeExtension > vulkanSchneeExtension
Definition GltfLoader.h:221
std::optional< LightProperties > lightProperties
Definition GltfLoader.h:141
std::optional< MeshProperties > meshProperties
Definition GltfLoader.h:142
static std::string extractVersion(const tinygltf::Value &vs_extension)
Extracts the version from the material extension.
std::optional< LightProperties::LightmapProperties > lightmapProperties
Definition GltfLoader.h:143
Extracts all material data from the gltf file. All data during runtime will be read from an object of...
Definition GltfLoader.h:169
std::map< std::string, double > nodeProperties
Definition GltfLoader.h:192
std::map< std::string, tinygltf::Value > customProperties
Definition GltfLoader.h:193
std::map< std::string, ShaderParameter > shaderParameters
Definition GltfLoader.h:191