1 /*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef CORE__GLTF__GLTF2_DATA_STRUCTURES_H
17 #define CORE__GLTF__GLTF2_DATA_STRUCTURES_H
18
19 #include <cstdint>
20
21 #include <base/containers/string.h>
22 #include <base/containers/unique_ptr.h>
23 #include <base/containers/vector.h>
24 #include <base/math/matrix.h>
25 #include <base/math/quaternion.h>
26 #include <base/math/vector.h>
27 #include <core/namespace.h>
28
29 #define GLTF2_EXTENSION_IGFX_COMPRESSED
30 #define GLTF2_EXTENSION_KHR_LIGHTS
31 #define GLTF2_EXTENSION_KHR_LIGHTS_PBR
32 #define GLTF2_EXTENSION_KHR_MATERIALS_CLEARCOAT
33 #define GLTF2_EXTENSION_KHR_MATERIALS_IOR
34 #define GLTF2_EXTENSION_KHR_MATERIALS_PBRSPECULARGLOSSINESS
35 #define GLTF2_EXTENSION_KHR_MATERIALS_SHEEN
36 #define GLTF2_EXTENSION_KHR_MATERIALS_SPECULAR
37 #define GLTF2_EXTENSION_KHR_MATERIALS_TRANSMISSION
38 #define GLTF2_EXTENSION_KHR_MATERIALS_UNLIT
39 #define GLTF2_EXTENSION_KHR_MESH_QUANTIZATION
40 #define GLTF2_EXTENSION_KHR_TEXTURE_BASISU
41 #define GLTF2_EXTENSION_KHR_TEXTURE_TRANSFORM
42 #define GLTF2_EXTENSION_EXT_LIGHTS_IMAGE_BASED
43 #define GLTF2_EXTRAS_CLEAR_COAT_MATERIAL
44 #define GLTF2_EXTENSION_HW_XR_EXT
45 #define GLTF2_EXTRAS_RSDZ
46
47 #ifdef OPAQUE
48 // hrpm win32 gdi..
49 #undef OPAQUE
50 #endif
51
CORE3D_BEGIN_NAMESPACE()52 CORE3D_BEGIN_NAMESPACE()
53 namespace GLTF2 {
54 constexpr const uint32_t GLTF_INVALID_INDEX = 0xFFFFFFFF;
55 constexpr const uint32_t GLTF_MAGIC = 0x46546C67; // ASCII string "glTF"
56
57 struct Skin;
58 struct AttributeBase;
59 struct Node;
60
61 // extensions
62 #if defined(GLTF2_EXTENSION_KHR_LIGHTS) || defined(GLTF2_EXTENSION_KHR_LIGHTS_PBR)
63 struct KHRLight;
64 #endif
65
66 enum class BufferTarget : int {
67 NOT_DEFINED = 0,
68 ARRAY_BUFFER = 34962,
69 ELEMENT_ARRAY_BUFFER = 34963,
70 };
71
72 enum class ChunkType : int {
73 JSON = 0x4E4F534A, // JSON
74 BIN = 0x004E4942, // BIN Binary buffer
75 };
76
77 enum class AttributeType : int {
78 NORMAL = 0,
79 POSITION = 1,
80 TANGENT = 2,
81 TEXCOORD = 3,
82 COLOR = 4,
83 JOINTS = 5,
84 WEIGHTS = 6,
85 INVALID = 0xff
86 };
87
88 enum class RenderMode : int {
89 // WebGL enums
90 BEGIN = 0,
91 POINTS = 0,
92 LINES = 1,
93 LINE_LOOP = 2,
94 LINE_STRIP = 3,
95 TRIANGLES = 4,
96 TRIANGLE_STRIP = 5,
97 TRIANGLE_FAN = 6,
98 COUNT = 7,
99 INVALID = 0xff
100 };
101
102 enum class ComponentType : int {
103 INVALID,
104 BYTE = 5120,
105 UNSIGNED_BYTE = 5121,
106 SHORT = 5122,
107 UNSIGNED_SHORT = 5123,
108 INT = 5124, // not used in GLTF2
109 UNSIGNED_INT = 5125,
110 FLOAT = 5126,
111 };
112
113 enum class DataType : int { INVALID, SCALAR, VEC2, VEC3, VEC4, MAT2, MAT3, MAT4 };
114
115 enum class AlphaMode : int {
116 // The alpha value is ignored and the rendered output is fully opaque.
117 OPAQUE,
118 // The rendered output is either fully opaque or fully transparent
119 // depending on the alpha value and the specified alpha cutoff value.
120 MASK,
121 // The alpha value is used to composite the source and destination areas.
122 // The rendered output is combined with the background using the normal
123 // painting operation (i.e. the Porter and Duff over operator).
124 BLEND,
125 };
126
127 enum class BlendMode : int {
128 TRANSPARENT_ALPHA, // transparentAlpha
129 TRANSPARENT_COLOR, // transparentColor
130 ADD, // add
131 MODULATE, // modulate
132 REPLACE, // replace
133 NONE // use alphaMode if blendMode not mentioned, (default)
134 };
135
136 enum class MimeType : int { INVALID, JPEG, PNG, KTX, DDS, KTX2 };
137
138 enum class CameraType : int { INVALID, PERSPECTIVE, ORTHOGRAPHIC };
139
140 enum class LightType : int { INVALID, DIRECTIONAL, POINT, SPOT, AMBIENT };
141
142 enum class FilterMode : int {
143 NEAREST = 9728,
144 LINEAR = 9729,
145 NEAREST_MIPMAP_NEAREST = 9984,
146 LINEAR_MIPMAP_NEAREST = 9985,
147 NEAREST_MIPMAP_LINEAR = 9986,
148 LINEAR_MIPMAP_LINEAR = 9987
149 };
150
151 enum class WrapMode : int { CLAMP_TO_EDGE = 33071, MIRRORED_REPEAT = 33648, REPEAT = 10497 };
152
153 enum class AnimationInterpolation : int { INVALID, STEP, LINEAR, SPLINE };
154
155 enum class AnimationPath : int {
156 INVALID,
157 TRANSLATION,
158 ROTATION,
159 SCALE,
160 WEIGHTS,
161 // RDSZ specific
162 VISIBLE,
163 OPACITY,
164 };
165
166 struct GLBHeader {
167 uint32_t magic = 0;
168 uint32_t version = 0;
169 uint32_t length = 0;
170 };
171
172 struct GLBChunk {
173 uint32_t chunkLength = 0;
174 uint32_t chunkType = 0;
175 };
176
177 struct Buffer {
178 // [required field]
179 size_t byteLength = 0;
180
181 // either empty (indicating GLB buffer)
182 // or path to file
183 // or data:[<mediatype>][;base64],<data> as defined
184 // in https://tools.ietf.org/html/rfc2397
185 BASE_NS::string uri;
186
187 // Data for this buffer.
188 BASE_NS::vector<uint8_t> data;
189 };
190
191 struct BufferView {
192 // [required field], with the index to the buffer.
193 // Note: referenced buffers needs to be loaded first.
194 Buffer* buffer { nullptr };
195
196 // required, "minimum": 1
197 size_t byteLength = 0;
198
199 // "minimum": 0, "default": 0
200 size_t byteOffset = 0;
201
202 // "minimum": 4, "maximum": 252, "multipleOf": 4
203 // The stride, in bytes, between vertex attributes.
204 // When this is not defined (0), data is tightly packed.
205 // When two or more accessors use the same bufferView, this field must be defined.
206 size_t byteStride = 0;
207
208 BufferTarget target = BufferTarget::NOT_DEFINED; // ARRAY_BUFFER, ELEMENT_ARRAY_BUFFER
209
210 // Data for this buffer view.
211 const uint8_t* data { nullptr };
212 };
213
214 struct SparseIndices {
215 // The bufferView with sparse indices.
216 // Referenced bufferView can't have ARRAY_BUFFER or
217 // ELEMENT_ARRAY_BUFFER target.
218 BufferView* bufferView { nullptr };
219
220 // The offset relative to the start of the bufferView in bytes.
221 // Must be aligned.
222 // "minimum": 0,
223 // "default": 0
224 uint32_t byteOffset = 0;
225
226 // The indices data type.
227 // Valid values correspond to WebGL enums:
228 // `5121` means UNSIGNED_BYTE, `5123` means UNSIGNED_SHORT
229 // `5125` means UNSIGNED_INT.
230 ComponentType componentType = ComponentType::UNSIGNED_INT;
231 };
232
233 struct SparseValues {
234 // The bufferView with sparse values.
235 // Referenced bufferView can't have ARRAY_BUFFER or
236 // ELEMENT_ARRAY_BUFFER target."
237 BufferView* bufferView { nullptr };
238
239 // The offset relative to the start of the bufferView in bytes.
240 // Must be aligned.
241 uint32_t byteOffset = 0;
242 };
243
244 struct Sparse {
245 // The number of attributes encoded in this sparse accessor.
246 uint32_t count = 0;
247
248 // Index array of size `count` that points to those accessor attributes
249 // that deviate from their initialization value. Indices must strictly increase.
250 SparseIndices indices;
251
252 // Array of size `count` times number of components,
253 // storing the displaced accessor attributes pointed by `indices`.
254 // Substituted values must have the same `componentType` and
255 // number of components as the base accessor."
256 SparseValues values;
257 };
258
259 struct Accessor {
260 // The bufferView.
261 // When not defined, accessor must be initialized with zeros;
262 // `sparse` property or extensions could override zeros with actual values.
263 BufferView* bufferView { nullptr };
264
265 // [required] The datatype of components in the attribute.
266 // All valid values correspond to WebGL enums.
267 // The corresponding typed arrays are
268 // `Int8Array`, `Uint8Array`, `Int16Array`, `Uint16Array`,
269 // `Uint32Array`, and `Float32Array`, respectively.
270 // 5125 (UNSIGNED_INT) is only allowed when the accessor contains indices,
271 // i.e., the accessor is only referenced by `primitive.indices`.
272 ComponentType componentType = ComponentType::UNSIGNED_INT;
273
274 // [required] The number of attributes referenced by this accessor,
275 // not to be confused with the number of bytes or number of components.
276 // "minimum": 1
277 uint32_t count = 0;
278
279 // [required] Specifies if the attribute is a scalar, vector, or matrix.
280 DataType type = DataType::INVALID;
281
282 // The offset relative to the start of the bufferView in bytes.
283 // This must be a multiple of the size of the component datatype.
284 // minimum: 0
285 // default: 0
286 uint32_t byteOffset = 0;
287
288 // Specifies whether integer data values should be normalized
289 // (`true`) to [0, 1] (for unsigned types) or [-1, 1] (for signed types),
290 // or converted directly (`false`) when they are accessed.
291 // This property is defined only for accessors that contain vertex attributes
292 // or animation output data.
293 // "default": false
294 bool normalized = false;
295
296 // Maximum value of each component in this attribute.
297 // Array elements must be treated as having the same data type as
298 // accessor's `componentType`. Both min and max arrays have the same length.
299 // The length is determined by the value of the type property;
300 // it can be 1, 2, 3, 4, 9, or 16.
301 // `normalized` property has no effect on array values:
302 // they always correspond to the actual values stored in the buffer.
303 // When accessor is sparse, this property must contain max values of
304 // accessor data with sparse substitution applied.
305 // "minItems": 1,
306 // "maxItems": 16,
307 BASE_NS::vector<float> max;
308
309 // Minimum value of each component in this attribute.
310 // Array elements must be treated as having the same data type as
311 // accessor's `componentType`. Both min and max arrays have the same length.
312 // The length is determined by the value of the type property;
313 // it can be 1, 2, 3, 4, 9, or 16.
314 // `normalized` property has no effect on array values:
315 // they always correspond to the actual values stored in the buffer.
316 // When accessor is sparse, this property must contain min values of
317 // accessor data with sparse substitution applied.
318 // "minItems": 1,
319 // "maxItems": 16,
320 BASE_NS::vector<float> min;
321
322 // Sparse storage of attributes that deviate from their initialization value.
323 Sparse sparse;
324 };
325
326 struct AttributeBase {
327 AttributeType type;
328 uint32_t index; // for example texcoord 0,1,2...
329 };
330
331 struct Attribute {
332 AttributeBase attribute;
333 Accessor* accessor { nullptr };
334 };
335
336 struct Image {
337 // The uri of the image.
338 // Relative paths are relative to the .gltf file.
339 // Instead of referencing an external file,
340 // the uri can also be a data-uri.
341 // The image format must be jpg or png.
342 BASE_NS::string uri;
343
344 // The bufferView that contains the image.
345 // Use this instead of the image's uri property.
346 BufferView* bufferView { nullptr };
347
348 // The image's MIME type. Needed when BufferView is used.
349 MimeType type;
350 };
351
352 struct Sampler {
353 FilterMode magFilter = FilterMode::LINEAR;
354 FilterMode minFilter = FilterMode::LINEAR;
355
356 WrapMode wrapS = WrapMode::REPEAT;
357 WrapMode wrapT = WrapMode::REPEAT;
358 };
359
360 struct Texture {
361 // The sampler used by this texture.
362 // When nullptr, a sampler with repeat wrapping
363 // and auto filtering should be used.
364 Sampler* sampler { nullptr };
365
366 // The image used by this texture.
367 Image* image { nullptr };
368 };
369
370 struct TextureInfo {
371 // The texture.
372 Texture* texture { nullptr };
373
374 // index defined in gltf.
375 uint32_t index = GLTF_INVALID_INDEX;
376
377 // The set index of texture's TEXCOORD attribute
378 // used for texture coordinate mapping.
379 // "default": 0
380 uint32_t texCoordIndex = 0;
381
382 #if defined(GLTF2_EXTENSION_KHR_TEXTURE_TRANSFORM)
383 struct TextureTransform {
384 BASE_NS::Math::Vec2 offset { 0.0f, 0.0f };
385 BASE_NS::Math::Vec2 scale { 1.f, 1.f };
386 float rotation = 0.0f;
387 uint32_t texCoordIndex = GLTF_INVALID_INDEX;
388 } transform;
389 #endif
390 };
391
392 struct MetallicRoughness {
393 // The RGBA components of the base color of the material.
394 // The fourth component (A) is the alpha coverage of the material.
395 // The `alphaMode` property specifies how alpha is interpreted.
396 // These values are linear. If a baseColorTexture is specified,
397 // this value is multiplied with the texel values.
398 // "default": [ 1.0, 1.0, 1.0, 1.0 ]
399 BASE_NS::Math::Vec4 baseColorFactor { 1.f, 1.f, 1.f, 1.f };
400
401 // The base color texture.
402 // This texture contains RGB(A) components in sRGB color space.
403 // The first three components (RGB) specify the base color of the
404 // material. If the fourth component (A) is present, it represents
405 // the alpha coverage of the material. Otherwise, an alpha of 1.0 is
406 // assumed. The `alphaMode` property specifies how alpha is
407 // interpreted. The stored texels must not be premultiplied.
408 // "default": 1.0
409 TextureInfo baseColorTexture;
410
411 // The metalness of the material.
412 // A value of 1.0 means the material is a metal.
413 // A value of 0.0 means the material is a dielectric.
414 // Values in between are for blending between metals
415 // and dielectrics such as dirty metallic surfaces.
416 // This value is linear. If a metallicRoughnessTexture is specified,
417 // this value is multiplied with the metallic texel values.
418 float metallicFactor { 1.f };
419
420 // The roughness of the material.
421 // A value of 1.0 means the material is completely rough.
422 // A value of 0.0 means the material is completely smooth.
423 // This value is linear. If a metallicRoughnessTexture is specified,
424 // this value is multiplied with the roughness texel values.
425 // "default": 1.0
426 float roughnessFactor { 1.f };
427
428 // The metallic-roughness texture.
429 // The metalness values are sampled from the B channel.
430 // The roughness values are sampled from the G channel.
431 // These values are linear. If other channels are present (R or A),
432 // they are ignored for metallic-roughness calculations.
433 TextureInfo metallicRoughnessTexture;
434 };
435
436 struct NormalTexture {
437 TextureInfo textureInfo;
438 float scale = 1.0f;
439 };
440
441 struct OcclusionTexture {
442 TextureInfo textureInfo;
443 float strength = 1.0f;
444 };
445
446 struct Material {
447 enum class Type { MetallicRoughness, SpecularGlossiness, Unlit, TextureSheetAnimation };
448
449 Type type { Type::MetallicRoughness };
450
451 BASE_NS::string name; // name
452 MetallicRoughness metallicRoughness;
453
454 // "The scalar multiplier applied to each normal vector of the texture.
455 // This value scales the normal vector using the formula:
456 // `scaledNormal = normalize((normalize(<sampled normal texture value>) * 2.0
457 // - 1.0) * vec3(<normal scale>, <normal scale>, 1.0))`. This value is ignored if
458 // normalTexture is not specified. This value is linear."
459 NormalTexture normalTexture;
460
461 // A scalar multiplier controlling the amount of occlusion applied.
462 // A value of 0.0 means no occlusion. A value of 1.0 means full occlusion.
463 // This value affects the resulting color using the formula:
464 // `occludedColor = lerp(color, color * <sampled occlusion texture value>,
465 // <occlusion strength>)`. This value is ignored if the corresponding texture
466 // is not specified. This value is linear. "default": 1.0, "minimum": 0.0,
467 // "maximum": 1.0,
468 OcclusionTexture occlusionTexture;
469
470 TextureInfo emissiveTexture;
471
472 BASE_NS::Math::Vec3 emissiveFactor; // "default": [ 0.0, 0.0, 0.0 ],
473
474 AlphaMode alphaMode = AlphaMode::OPAQUE;
475
476 BlendMode blendMode = BlendMode::NONE;
477
478 float alphaCutoff = 0.5f; // "minimum": 0.0,
479
480 // default": false,
481 // Specifies whether the material is double sided.
482 // When this value is false, back-face culling is enabled.
483 // When this value is true, back-face culling is disabled
484 // and double sided lighting is enabled.
485 // The back-face must have its normals reversed before
486 // the lighting equation is evaluated.
487 bool doubleSided = false;
488
489 #if defined(GLTF2_EXTENSION_KHR_MATERIALS_CLEARCOAT) || defined(GLTF2_EXTRAS_CLEAR_COAT_MATERIAL)
490 struct Clearcoat {
491 // The clearcoat layer intensity.
492 float factor = 0.0f;
493 // The clearcoat layer intensity texture.
494 TextureInfo texture;
495 // The clearcoat layer roughness.
496 float roughness = 0.0f;
497 // The clearcoat layer roughness texture.
498 TextureInfo roughnessTexture;
499 // The clearcoat normal map texture.
500 NormalTexture normalTexture;
501 } clearcoat;
502 #endif
503
504 #if defined(GLTF2_EXTENSION_KHR_MATERIALS_IOR)
505 struct Ior {
506 // Material's index of refraction.
507 float ior = 1.5f;
508 } ior;
509 #endif
510
511 #if defined(GLTF2_EXTENSION_KHR_MATERIALS_PBRSPECULARGLOSSINESS)
512 struct SpecularGlossiness {
513 // The RGBA components of the reflected diffuse color of the material.
514 // Metals have a diffuse value of `[0.0, 0.0, 0.0]`. The fourth component (A) is the alpha coverage of the
515 // material. The 'alphaMode' property specifies how alpha is interpreted. The values are linear.
516 BASE_NS::Math::Vec4 diffuseFactor { 1.f, 1.f, 1.f, 1.f }; // "default": [ 1.0, 1.0, 1.0, 1.0 ]
517
518 // The diffuse texture. This texture contains RGB(A) components of the reflected diffuse color of the material
519 // in sRGB color space. If the fourth component (A) is present, it represents the alpha coverage of the
520 // material. Otherwise, an alpha of 1.0 is assumed. The `alphaMode` property specifies how alpha is interpreted.
521 // The stored texels must not be premultiplied.
522 TextureInfo diffuseTexture;
523
524 // The specular RGB color of the material. This value is linear.
525 BASE_NS::Math::Vec3 specularFactor { 1.f, 1.f, 1.f }; // "default": [ 1.0, 1.0, 1.0 ]
526
527 // The glossiness or smoothness of the material.A value of 1.0 means the material has full glossiness
528 // or is perfectly smooth.A value of 0.0 means the material has no glossiness or is completely rough.This value
529 // is linear.
530 float glossinessFactor = 1.0f;
531
532 // The specular-glossiness texture is RGBA texture, containing the specular color of the material (RGB
533 // components) and its glossiness (A component). The values are in sRGB space.
534 TextureInfo specularGlossinessTexture;
535 } specularGlossiness;
536 #endif
537
538 #if defined(GLTF2_EXTENSION_KHR_MATERIALS_SHEEN)
539 struct Sheen {
540 // The sheen color in linear space
541 BASE_NS::Math::Vec3 factor;
542 // The sheen color (sRGB)
543 TextureInfo texture;
544 // The sheen roughness.
545 float roughness = 0.0f;
546 // The sheen roughness texture, stored in the alpha channel.
547 TextureInfo roughnessTexture;
548 } sheen;
549 #endif
550
551 #if defined(GLTF2_EXTENSION_KHR_MATERIALS_SPECULAR)
552 struct Specular {
553 // The specular reflection strength.
554 float factor = 1.f;
555 // The specular reflection strength texture, stored in the alpha channel.
556 TextureInfo texture;
557 // The specular color in linear space.
558 BASE_NS::Math::Vec3 color { 1.f, 1.f, 1.f };
559 // The specular color texture. The values are in sRGB space.
560 TextureInfo colorTexture;
561 } specular;
562 #endif
563
564 #if defined(GLTF2_EXTENSION_KHR_MATERIALS_TRANSMISSION)
565 struct Transmission {
566 // Percentage of light that is transmitted through the surface
567 float factor = 0.0f;
568 // Transmission percentage of the surface, stored in the R channel. This will be multiplied by
569 // transmissionFactor.
570 TextureInfo texture;
571 } transmission;
572 #endif
573 };
574
575 struct MorphTarget {
576 // extension of spec
577 // see https://github.com/KhronosGroup/glTF-Blender-Exporter/pull/153)
578 // https://github.com/KhronosGroup/glTF/issues/1036
579 BASE_NS::string name;
580 BASE_NS::vector<Attribute> target;
581 #if defined(GLTF2_EXTENSION_IGFX_COMPRESSED)
582 // true when morph target is using IGFX_compressed extension.
583 bool iGfxCompressed = false;
584 #endif
585 };
586
587 struct MeshPrimitive {
588 // [required fields]
589 // A dictionary object, where each key corresponds
590 // to mesh attribute semantic and each value is the index
591 // of the accessor containing attribute's data.
592 BASE_NS::vector<Attribute> attributes;
593
594 // "The index of the accessor that contains mesh indices.
595 // When this is not defined, the primitives should be rendered
596 // without indices using drawArrays.
597 // When defined, the accessor must contain indices:
598 // the `bufferView` referenced by the accessor should have
599 // a `target` equal to 34963 (ELEMENT_ARRAY_BUFFER)
600 // `componentType` must be 5121 (UNSIGNED_BYTE),
601 // 5123 (UNSIGNED_SHORT) or 5125 (UNSIGNED_INT),
602 // the latter may require enabling additional hardware support;
603 // `type` must be `\"SCALAR\"`.
604 // For triangle primitives, the front face has
605 // a counter-clockwise (CCW) winding order."
606 Accessor* indices { nullptr };
607
608 // "The index of the material to apply to this primitive when rendering.
609 Material* material { nullptr };
610
611 // index defined in gltf.
612 uint32_t materialIndex = GLTF_INVALID_INDEX;
613
614 // The type of primitives to render. All valid values correspond to WebGL enums.
615 RenderMode mode = RenderMode::TRIANGLES;
616
617 // An array of Morph Targets,
618 // each Morph Target is a dictionary mapping attributes
619 // (only `POSITION`, `NORMAL`, and `TANGENT` supported)
620 // to their deviations in the Morph Target.
621 BASE_NS::vector<MorphTarget> targets;
622 };
623
624 struct Mesh {
625 // Name.
626 BASE_NS::string name;
627 // [required field], primitives
628 BASE_NS::vector<MeshPrimitive> primitives;
629 // Array of weights to be applied to the Morph Targets.
630 BASE_NS::vector<float> weights;
631 };
632
633 struct Camera {
634 // Name.
635 BASE_NS::string name;
636
637 CameraType type;
638
639 union Attributes {
640 struct Perspective {
641 // PERSPECTIVE
642 // minimum value for each is 0
643 // => in this implementation negative is used to disable parameter
644 float aspect;
645 float yfov; // required
646 float zfar;
647 float znear; // required
648 } perspective;
649
650 struct Ortho {
651 // ORTHOGRAPHIC
652 // xmag, ymag cant't be zero
653 // zfar, znear : minimum is zero
654 // all are required
655 float xmag;
656 float ymag;
657 float zfar;
658 float znear;
659 } ortho;
660 } attributes;
661 };
662
663 struct Skin {
664 BASE_NS::string name;
665
666 // The accessor containing the floating-point 4X4 inverse-bind matrices.
667 // The default is that each matrix is a 4X4 identity matrix,
668 // which implies that inverse-bind matrices were pre-applied.
669 Accessor* inverseBindMatrices { nullptr };
670
671 // The node used as a skeleton root. When undefined, joints transforms resolve to scene root.
672 Node* skeleton { nullptr };
673
674 // The skeleton nodes, used as joints in this skin.
675 BASE_NS::vector<Node*> joints;
676 };
677
678 struct Node {
679 BASE_NS::string name;
680
681 Mesh* mesh { nullptr };
682
683 Camera* camera { nullptr };
684
685 #if defined(GLTF2_EXTENSION_KHR_LIGHTS) || defined(GLTF2_EXTENSION_KHR_LIGHTS_PBR)
686 KHRLight* light { nullptr };
687 #endif
688
689 #if defined(GLTF2_EXTRAS_RSDZ)
690 BASE_NS::string modelIdRSDZ;
691 #endif
692
693 // Helpers mostly for skeleton support
694 Node* parent { nullptr };
695 bool isJoint = false;
696
697 BASE_NS::vector<Node*> children;
698 BASE_NS::vector<size_t> tmpChildren; // indices, used when gltf is parsed. (NOTE: move outside of node)
699
700 Skin* skin { nullptr };
701 uint32_t tmpSkin; // index to skin (NOTE: move outside of node)
702
703 bool usesTRS = true;
704
705 BASE_NS::Math::Vec3 translation { 0.f, 0.f, 0.f };
706 BASE_NS::Math::Quat rotation { 0.f, 0.f, 0.f, 1.f };
707 BASE_NS::Math::Vec3 scale { 1.f, 1.f, 1.f };
708
709 BASE_NS::Math::Mat4X4 matrix;
710
711 BASE_NS::vector<float> weights;
712 };
713
714 struct Scene {
715 BASE_NS::string name;
716 BASE_NS::vector<Node*> nodes;
717
718 #if defined(GLTF2_EXTENSION_KHR_LIGHTS) || defined(GLTF2_EXTENSION_KHR_LIGHTS_PBR)
719 KHRLight* light { nullptr }; // Ambient light
720 #endif
721
722 #if defined(GLTF2_EXTENSION_EXT_LIGHTS_IMAGE_BASED)
723 size_t imageBasedLightIndex = GLTF_INVALID_INDEX;
724 #endif
725 };
726
727 struct AnimationSampler {
728 Accessor* input { nullptr };
729 Accessor* output { nullptr };
730 AnimationInterpolation interpolation;
731 };
732
733 struct AnimationChannel // = animation.channel.target
734 {
735 Node* node { nullptr };
736 AnimationPath path;
737 };
738
739 struct AnimationTrack // = animation.channel
740 {
741 AnimationChannel channel;
742 AnimationSampler* sampler { nullptr };
743 };
744
745 struct Animation {
746 BASE_NS::string name;
747 BASE_NS::vector<AnimationTrack> tracks;
748 BASE_NS::vector<BASE_NS::unique_ptr<AnimationSampler>> samplers;
749 };
750
751 // extensions
752 #if defined(GLTF2_EXTENSION_KHR_LIGHTS) || defined(GLTF2_EXTENSION_KHR_LIGHTS_PBR)
753 struct KHRLight {
754 BASE_NS::string name;
755 LightType type = LightType::AMBIENT;
756 BASE_NS::Math::Vec3 color = BASE_NS::Math::Vec3(1.f, 1.f, 1.f); // RGB
757 float intensity = 1.0f; // Intensity of the light source in lumens. default 1.0
758
759 struct {
760 float range = .0f;
761
762 struct {
763 // SPOT
764 float innerAngle = 0.f;
765 float outerAngle = 0.785398163397448f; // PI / 4
766 } spot;
767
768 } positional;
769
770 struct {
771 bool shadowCaster = false;
772 float nearClipDistance = 100.f;
773 float farClipDistance = 10000.f;
774 } shadow;
775 };
776 #endif
777
778 #if defined(GLTF2_EXTENSION_EXT_LIGHTS_IMAGE_BASED)
779 struct ImageBasedLight {
780 // Represents one mip level of a cube map.
781 using CubemapMipLevel = BASE_NS::vector<size_t>;
782 // Represents one set of irrandiance coefficients.
783 using LightingCoeff = BASE_NS::vector<float>;
784
785 // Name of the light.
786 BASE_NS::string name;
787 // Quaternion that represents the rotation of the IBL environment.
788 BASE_NS::Math::Quat rotation { 0.0f, 0.0f, 0.0f, 1.0f };
789 // Brightness multiplier for environment.
790 float intensity { 1.0f };
791 // Declares spherical harmonic coefficients for irradiance up to l=2. This is a 9x3 array.
792 BASE_NS::vector<LightingCoeff> irradianceCoefficients;
793 // Declares an array of the first N mips of the prefiltered cubemap.
794 // Each mip is, in turn, defined with an array of 6 images, one for each cube face. i.e. this is an Nx6 array.
795 BASE_NS::vector<CubemapMipLevel> specularImages;
796 // The dimension (in pixels) of the first specular mip. This is needed to determine, pre-load, the total number of
797 // mips needed.
798 uint32_t specularImageSize { 0 };
799 // Specular cubemap image, optional.
800 size_t specularCubeImage { GLTF_INVALID_INDEX };
801 // Skymap cubemap image, optional.
802 size_t skymapImage { GLTF_INVALID_INDEX };
803 // Skymap image lod level, optional.
804 float skymapImageLodLevel { 0.0f };
805 };
806 #endif
807
808 } // namespace GLTF2
809 CORE3D_END_NAMESPACE()
810
811 #endif // CORE__GLTF__GLTF2_DATA_STRUCTURES_H