1 #ifndef DALI_SCENE3D_LOADER_GLTF2_ASSET_H_
2 #define DALI_SCENE3D_LOADER_GLTF2_ASSET_H_
4 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <dali/devel-api/common/map-wrapper.h>
22 #include <dali/public-api/common/vector-wrapper.h>
23 #include <dali/public-api/math/quaternion.h>
24 #include <dali/public-api/math/vector4.h>
29 #include <dali-scene3d/internal/loader/json-reader.h>
30 #include <dali-scene3d/public-api/loader/index.h>
31 #include <dali-scene3d/public-api/loader/utils.h>
33 #define ENUM_STRING_MAPPING(t, x) \
38 #define ENUM_TYPE_FROM_STRING(structName, table) \
39 structName::Type structName::FromString(const char* s, size_t len) \
41 std::string target(s, len); \
42 std::transform(target.begin(), target.end(), target.begin(), ::toupper); \
44 auto iFind = table.find(std::string_view(target.c_str(), len)); \
45 if(iFind != table.end()) \
47 return iFind->second; \
49 return structName::INVALID; \
54 using Index = Dali::Scene3D::Loader::Index;
55 constexpr float UNDEFINED_FLOAT_VALUE = -1.0f; ///< Special marker for some non-negative only float values.
62 Ref(std::vector<T>& v, Index i)
69 * @return The index of the object into the vector.
70 * @note It is client code responsibility to ensure that the vector is unambiguous. It should be in
71 * a glTF document, since there's one vector for each type.
73 Index GetIndex() const
79 * @brief There may be scenarios in which the object, whose vector we're populating, changes, e.g.
80 * when we don't have a final one at the time of reading the references.
82 void UpdateVector(std::vector<T>& v)
89 return mVector != nullptr;
93 return &(*mVector)[mIndex];
97 return (*mVector)[mIndex];
100 bool operator==(const Ref<T>& other) const
102 return mVector == other.mVector && mIndex == other.mIndex;
105 bool operator!=(const Ref<T>& other) const
107 return !operator==(other);
111 std::vector<T>* mVector = nullptr;
112 Index mIndex = Dali::Scene3D::Loader::INVALID_INDEX;
117 std::string_view mGenerator;
118 std::string_view mVersion;
126 UNSIGNED_BYTE = 5121,
128 UNSIGNED_SHORT = 5123,
134 static bool IsUnsigned(Type t);
135 static uint32_t Size(Type t);
137 Component() = delete;
154 static uint32_t ElementCount(Type t);
156 static Type FromString(const char* s, size_t len);
158 AccessorType() = delete;
171 static Type FromString(const char* s, size_t len);
173 AlphaMode() = delete;
190 using HashType = uint32_t;
193 // +--+--+--+--+--+--+--+
194 // |31|30|29|28|27|..| 0| bit index
195 // +--+--+--+--+--+--+--+
197 // \______/ - Type enum
198 // \_______/ - Set ID
199 static const HashType SET_SHIFT{31};
200 static const HashType TYPE_SHIFT{28};
201 static const HashType SET_MASK{0x01u << SET_SHIFT};
202 static const HashType TYPE_MASK{0x07 << TYPE_SHIFT};
203 static const HashType SET_ID_MASK{0x0fffffff};
205 static HashType ToHash(Type type, bool set, HashType setIndex)
207 return ((set << SET_SHIFT) & SET_MASK) | ((static_cast<HashType>(type) << TYPE_SHIFT) & TYPE_MASK) | (setIndex & SET_ID_MASK);
210 static Attribute::Type TypeFromHash(HashType hash)
212 return static_cast<Type>((hash & TYPE_MASK) >> TYPE_SHIFT);
215 static bool SetFromHash(HashType hash)
217 return (hash & SET_SHIFT) != 0;
220 static HashType SetIdFromHash(HashType hash)
222 return (hash & SET_ID_MASK);
226 * Convert to Type + setIndex, where setIndex is N for that attr, e.g. "JOINTS_1" => {JOINTS_N, 1}
228 static HashType HashFromString(const char* s, size_t len);
231 * Convert to type only, there is no set for POSITION, NORMALS or TANGENT.
233 static Attribute::Type TargetFromString(const char* s, size_t len);
235 Attribute() = delete;
240 uint32_t mByteLength;
241 std::string_view mUri;
253 ARRAY_BUFFER = 34962,
254 ELEMENT_ARRAY_BUFFER = 34963
261 uint32_t mByteOffset = 0;
262 uint32_t mByteLength;
263 uint32_t mByteStride = 0; // if 0 after reading, it needs to be calculated
269 struct BufferViewClient
271 Ref<BufferView> mBufferView;
272 uint32_t mByteOffset = 0;
275 struct ComponentTypedBufferViewClient : BufferViewClient
277 Component::Type mComponentType = Component::INVALID;
279 uint32_t GetBytesPerComponent() const;
284 std::string_view mName;
290 struct Accessor : ComponentTypedBufferViewClient, Named
295 ComponentTypedBufferViewClient mIndices;
296 BufferViewClient mValues;
302 bool mNormalized = false;
303 AccessorType::Type mType = AccessorType::INVALID;
304 std::vector<float> mMin;
305 std::vector<float> mMax;
306 std::unique_ptr<Sparse> mSparse;
310 uint32_t GetElementSizeBytes() const
312 return GetBytesPerComponent() * AccessorType::ElementCount(mType);
315 uint32_t GetBytesLength() const
317 return GetElementSizeBytes() * mCount;
320 void SetSparse(const Sparse& s)
322 mSparse.reset(new Sparse(s));
328 std::string_view mUri;
329 std::string_view mMimeType;
330 Ref<BufferView> mBufferView;
341 NEAREST_MIPMAP_NEAREST = 9984,
342 NEAREST_MIPMAP_LINEAR = 9985,
343 LINEAR_MIPMAP_NEAREST = 9986,
344 LINEAR_MIPMAP_LINEAR = 9987,
355 CLAMP_TO_EDGE = 33071,
356 MIRRORED_REPEAT = 33648,
364 Filter::Type mMinFilter = Filter::LINEAR;
365 Filter::Type mMagFilter = Filter::LINEAR;
366 Wrap::Type mWrapS = Wrap::REPEAT;
367 Wrap::Type mWrapT = Wrap::REPEAT;
375 Ref<Sampler> mSampler;
380 Ref<gltf2::Texture> mTexture;
381 uint32_t mTexCoord = 0;
383 float mStrength = 1.f;
385 operator bool() const
392 * Material Ior is supported with KHR_materials_ior extension.
393 * https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_ior
397 float mIor = UNDEFINED_FLOAT_VALUE;
401 * Material Specular is supported with KHR_materials_ior extension.
402 * https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_specular
404 struct MaterialSpecular
406 float mSpecularFactor = 1.0f;
407 TextureInfo mSpecularTexture;
408 Dali::Vector3 mSpecularColorFactor = Dali::Vector3::ONE;
409 TextureInfo mSpecularColorTexture;
412 struct MaterialExtensions
414 MaterialSpecular mMaterialSpecular;
415 MaterialIor mMaterialIor;
418 struct Material : Named
420 struct Pbr // MetallicRoughness
422 Dali::Vector4 mBaseColorFactor = Dali::Vector4::ONE;
423 TextureInfo mBaseColorTexture;
424 float mMetallicFactor = 1.f;
425 float mRoughnessFactor = 1.f;
426 TextureInfo mMetallicRoughnessTexture;
431 Pbr mPbrMetallicRoughness;
432 TextureInfo mNormalTexture;
433 TextureInfo mOcclusionTexture;
434 TextureInfo mEmissiveTexture;
435 Dali::Vector3 mEmissiveFactor;
436 AlphaMode::Type mAlphaMode = AlphaMode::OPAQUE;
437 float mAlphaCutoff = .5f;
438 bool mDoubleSided = false;
441 MaterialExtensions mMaterialExtensions;
461 std::map<Attribute::HashType, Ref<Accessor>> mAttributes;
462 std::vector<std::map<Attribute::Type, Ref<Accessor>>> mTargets;
463 Ref<Accessor> mIndices;
464 Ref<Material> mMaterial;
465 Mode mMode = TRIANGLES;
467 //TODO: [morph] targets
474 std::vector<std::string_view> mTargetNames;
481 std::vector<std::string_view> mSXRTargetsNames;
482 std::vector<std::string_view> mAvatarShapeNames;
487 std::vector<Primitive> mPrimitives;
488 std::vector<float> mWeights;
490 Extensions mExtensions;
497 Ref<Accessor> mInverseBindMatrices;
499 std::vector<Ref<Node>> mJoints;
504 struct Camera : Named
508 float mAspectRatio = UNDEFINED_FLOAT_VALUE;
509 float mYFov = UNDEFINED_FLOAT_VALUE;
510 float mZFar = UNDEFINED_FLOAT_VALUE;
511 float mZNear = UNDEFINED_FLOAT_VALUE;
518 float mXMag = UNDEFINED_FLOAT_VALUE;
519 float mYMag = UNDEFINED_FLOAT_VALUE;
520 float mZFar = UNDEFINED_FLOAT_VALUE;
521 float mZNear = UNDEFINED_FLOAT_VALUE;
526 std::string_view mType;
527 Perspective mPerspective;
528 Orthographic mOrthographic;
535 Dali::Vector3 mTranslation = Dali::Vector3::ZERO;
536 Dali::Quaternion mRotation = Dali::Quaternion::IDENTITY;
537 Dali::Vector3 mScale = Dali::Vector3::ONE;
540 std::vector<Ref<Node>> mChildren;
544 //TODO: [morph] weights
548 void SetMatrix(const Dali::Matrix& m);
551 struct Animation : Named
564 static Type FromString(const char* s, size_t len);
567 Ref<Accessor> mInput;
568 Ref<Accessor> mOutput;
569 Interpolation::Type mInterpolation;
588 static Type FromString(const char* s, size_t len);
594 Ref<Sampler> mSampler;
600 std::vector<Sampler> mSamplers;
601 std::vector<Channel> mChannels;
606 std::vector<Ref<Node>> mNodes;
609 enum ExtensionFlags : uint32_t
611 NONE = Dali::Scene3D::Loader::NthBit(0),
612 KHR_MESH_QUANTIZATION = Dali::Scene3D::Loader::NthBit(1), // See https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_texture_transform
613 KHR_TEXTURE_TRANSFORM = Dali::Scene3D::Loader::NthBit(2), // See https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_mesh_quantization
614 KHR_MATERIALS_IOR = Dali::Scene3D::Loader::NthBit(3), // See https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_ior
615 KHR_MATERIALS_SPECULAR = Dali::Scene3D::Loader::NthBit(4), // See https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_specular
623 std::vector<Buffer> mBuffers;
624 std::vector<BufferView> mBufferViews;
625 std::vector<Accessor> mAccessors;
627 std::vector<Image> mImages;
628 std::vector<Sampler> mSamplers;
629 std::vector<Texture> mTextures;
630 std::vector<Material> mMaterials;
632 std::vector<Mesh> mMeshes;
633 std::vector<Skin> mSkins;
635 std::vector<Camera> mCameras;
636 std::vector<Node> mNodes;
638 std::vector<Animation> mAnimations;
640 std::vector<std::string_view> mExtensionsUsed;
641 std::vector<std::string_view> mExtensionsRequired;
643 uint32_t mExtensionFlags{0};
645 std::vector<Scene> mScenes;
648 Document() = default;
649 Document(const Document&) = delete;
650 Document(Document&&) = default;
652 Document& operator=(const Document&) = delete;
653 Document& operator=(Document&&) = default;
657 * @brief Provides a json::Property<T>::ReadFn for interpreting unsigned integers
658 * as a Ref<U> into a std::vector<U> data member of a type T.
665 template<typename U, std::vector<U> T::*V>
666 static Ref<U> Read(const json_value_s& j)
668 uint32_t index = json::Read::Number<uint32_t>(j);
669 return Ref<U>(sObject->*V, index);
674 T* RefReader<T>::sObject = nullptr;
677 * @brief Convenience method to set the object for RefReader.
680 void SetRefReaderObject(T& object)
682 RefReader<T>::sObject = &object;
686 * @brief Reads a string and attempts to convert it to an enum.
687 * @note The enum must: 1, be called Type, nested to T, 2, provide a FromString static method taking a const char*
688 * (string data) and a size_t (string length) and returning T::Type.
690 template<typename T> // T must have a nested enum called Type and a static Type FromString(const char*) method.
691 typename T::Type ReadStringEnum(const json_value_s& j)
693 auto str = json::Read::StringView(j);
695 return T::FromString(str.data(), str.size());
699 * @brief Convenience method to attempt to create a Dali vector type T from an array of floats.
700 * @note T must provide an AsFloat() member method returning the non-const array of its
704 inline T ReadDaliVector(const json_value_s& j)
706 std::vector<float> floats = json::Read::Array<float, json::Read::Number<float>>(j);
708 std::copy(floats.begin(), std::min(floats.end(), floats.begin() + sizeof(T) / sizeof(float)), result.AsFloat());
713 * @brief Convenience method to attemt to read a Quaternion, which implicitly converts
714 * to Vector4 but fails to provide an AsFloat() method.
716 Dali::Quaternion ReadQuaternion(const json_value_s& j);
720 #endif //DALI_SCENE3D_LOADER_GLTF2_ASSET_H_