1 #ifndef DALI_SCENE3D_LOADER_GLTF2_ASSET_H_
2 #define DALI_SCENE3D_LOADER_GLTF2_ASSET_H_
4 * Copyright (c) 2022 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-scene3d/internal/loader/json-reader.h"
22 #include "dali-scene3d/public-api/loader/index.h"
27 #include "dali/devel-api/common/map-wrapper.h"
28 #include "dali/public-api/common/vector-wrapper.h"
29 #include "dali/public-api/math/quaternion.h"
30 #include "dali/public-api/math/vector4.h"
32 #define ENUM_STRING_MAPPING(t, x) \
37 #define ENUM_TYPE_FROM_STRING(structName, table) \
38 structName::Type structName::FromString(const char* s, size_t len) \
40 std::string target(s, len); \
41 std::transform(target.begin(), target.end(), target.begin(), ::toupper); \
43 auto iFind = table.find(std::string_view(target.c_str(), len)); \
44 if(iFind != table.end()) \
46 return iFind->second; \
48 return structName::INVALID; \
53 using Index = Dali::Scene3D::Loader::Index;
60 Ref(std::vector<T>& v, Index i)
67 * @return The index of the object into the vector.
68 * @note It is client code responsibility to ensure that the vector is unambiguous. It should be in
69 * a glTF document, since there's one vector for each type.
71 Index GetIndex() const
77 * @brief There may be scenarios in which the object, whose vector we're populating, changes, e.g.
78 * when we don't have a final one at the time of reading the references.
80 void UpdateVector(std::vector<T>& v)
87 return mVector != nullptr;
91 return &(*mVector)[mIndex];
95 return (*mVector)[mIndex];
98 bool operator==(const Ref<T>& other) const
100 return mVector == other.mVector && mIndex == other.mIndex;
103 bool operator!=(const Ref<T>& other) const
105 return !operator==(other);
109 std::vector<T>* mVector = nullptr;
110 Index mIndex = Dali::Scene3D::Loader::INVALID_INDEX;
115 std::string_view mGenerator;
116 std::string_view mVersion;
124 UNSIGNED_BYTE = 5121,
126 UNSIGNED_SHORT = 5123,
132 static bool IsUnsigned(Type t);
133 static uint32_t Size(Type t);
135 Component() = delete;
152 static uint32_t ElementCount(Type t);
154 static Type FromString(const char* s, size_t len);
156 AccessorType() = delete;
169 static Type FromString(const char* s, size_t len);
171 AlphaMode() = delete;
189 static Type FromString(const char* s, size_t len);
191 Attribute() = delete;
196 uint32_t mByteLength;
197 std::string_view mUri;
209 ARRAY_BUFFER = 34962,
210 ELEMENT_ARRAY_BUFFER = 34963
217 uint32_t mByteOffset = 0;
218 uint32_t mByteLength;
219 uint32_t mByteStride = 0; // if 0 after reading, it needs to be calculated
225 struct BufferViewClient
227 Ref<BufferView> mBufferView;
228 uint32_t mByteOffset = 0;
231 struct ComponentTypedBufferViewClient : BufferViewClient
233 Component::Type mComponentType = Component::INVALID;
235 uint32_t GetBytesPerComponent() const;
240 std::string_view mName;
246 struct Accessor : ComponentTypedBufferViewClient, Named
251 ComponentTypedBufferViewClient mIndices;
252 BufferViewClient mValues;
258 bool mNormalized = false;
259 AccessorType::Type mType = AccessorType::INVALID;
260 std::vector<float> mMin;
261 std::vector<float> mMax;
262 std::unique_ptr<Sparse> mSparse;
266 uint32_t GetElementSizeBytes() const
268 return GetBytesPerComponent() * AccessorType::ElementCount(mType);
271 uint32_t GetBytesLength() const
273 return GetElementSizeBytes() * mCount;
276 void SetSparse(const Sparse& s)
278 mSparse.reset(new Sparse(s));
284 std::string_view mUri;
285 std::string_view mMimeType;
286 Ref<BufferView> mBufferView;
297 NEAREST_MIPMAP_NEAREST = 9984,
298 NEAREST_MIPMAP_LINEAR = 9985,
299 LINEAR_MIPMAP_NEAREST = 9986,
300 LINEAR_MIPMAP_LINEAR = 9987,
311 CLAMP_TO_EDGE = 33071,
312 MIRRORED_REPEAT = 33648,
320 Filter::Type mMinFilter = Filter::LINEAR;
321 Filter::Type mMagFilter = Filter::LINEAR;
322 Wrap::Type mWrapS = Wrap::REPEAT;
323 Wrap::Type mWrapT = Wrap::REPEAT;
331 Ref<Sampler> mSampler;
336 Ref<gltf2::Texture> mTexture;
337 uint32_t mTexCoord = 0;
339 float mStrength = 1.f;
341 operator bool() const
348 * Material Ior is supported with KHR_materials_ior extension.
349 * https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_ior
353 float mIor = MAXFLOAT;
357 * Material Specular is supported with KHR_materials_ior extension.
358 * https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_specular
360 struct MaterialSpecular
362 float mSpecularFactor = 1.0f;
363 TextureInfo mSpecularTexture;
364 Dali::Vector3 mSpecularColorFactor = Dali::Vector3::ONE;
365 TextureInfo mSpecularColorTexture;
368 struct MaterialExtensions
370 MaterialSpecular mMaterialSpecular;
371 MaterialIor mMaterialIor;
374 struct Material : Named
376 struct Pbr // MetallicRoughness
378 Dali::Vector4 mBaseColorFactor = Dali::Vector4::ONE;
379 TextureInfo mBaseColorTexture;
380 float mMetallicFactor = 1.f;
381 float mRoughnessFactor = 1.f;
382 TextureInfo mMetallicRoughnessTexture;
387 Pbr mPbrMetallicRoughness;
388 TextureInfo mNormalTexture;
389 TextureInfo mOcclusionTexture;
390 TextureInfo mEmissiveTexture;
391 Dali::Vector3 mEmissiveFactor;
392 AlphaMode::Type mAlphaMode = AlphaMode::OPAQUE;
393 float mAlphaCutoff = .5f;
394 bool mDoubleSided = false;
397 MaterialExtensions mMaterialExtensions;
417 std::map<Attribute::Type, Ref<Accessor>> mAttributes;
418 std::vector<std::map<Attribute::Type, Ref<Accessor>>> mTargets;
419 Ref<Accessor> mIndices;
420 Ref<Material> mMaterial;
421 Mode mMode = TRIANGLES;
423 //TODO: [morph] targets
428 std::vector<Primitive> mPrimitives;
429 std::vector<float> mWeights;
438 Ref<Accessor> mInverseBindMatrices;
440 std::vector<Ref<Node>> mJoints;
445 struct Camera : Named
467 std::string_view mType;
468 Perspective mPerspective;
469 Orthographic mOrthographic;
476 Dali::Vector3 mTranslation = Dali::Vector3::ZERO;
477 Dali::Quaternion mRotation = Dali::Quaternion::IDENTITY;
478 Dali::Vector3 mScale = Dali::Vector3::ONE;
481 std::vector<Ref<Node>> mChildren;
485 //TODO: [morph] weights
489 void SetMatrix(const Dali::Matrix& m);
492 struct Animation : Named
505 static Type FromString(const char* s, size_t len);
508 Ref<Accessor> mInput;
509 Ref<Accessor> mOutput;
510 Interpolation::Type mInterpolation;
529 static Type FromString(const char* s, size_t len);
535 Ref<Sampler> mSampler;
541 std::vector<Sampler> mSamplers;
542 std::vector<Channel> mChannels;
547 std::vector<Ref<Node>> mNodes;
554 std::vector<Buffer> mBuffers;
555 std::vector<BufferView> mBufferViews;
556 std::vector<Accessor> mAccessors;
558 std::vector<Image> mImages;
559 std::vector<Sampler> mSamplers;
560 std::vector<Texture> mTextures;
561 std::vector<Material> mMaterials;
563 std::vector<Mesh> mMeshes;
564 std::vector<Skin> mSkins;
566 std::vector<Camera> mCameras;
567 std::vector<Node> mNodes;
569 std::vector<Animation> mAnimations;
571 std::vector<Scene> mScenes;
574 Document() = default;
575 Document(const Document&) = delete;
576 Document(Document&&) = default;
578 Document& operator=(const Document&) = delete;
579 Document& operator=(Document&&) = default;
583 * @brief Provides a json::Property<T>::ReadFn for interpreting unsigned integers
584 * as a Ref<U> into a std::vector<U> data member of a type T.
591 template<typename U, std::vector<U> T::*V>
592 static Ref<U> Read(const json_value_s& j)
594 uint32_t index = json::Read::Number<uint32_t>(j);
595 return Ref<U>(sObject->*V, index);
600 T* RefReader<T>::sObject = nullptr;
603 * @brief Convenience method to set the object for RefReader.
606 void SetRefReaderObject(T& object)
608 RefReader<T>::sObject = &object;
612 * @brief Reads a string and attempts to convert it to an enum.
613 * @note The enum must: 1, be called Type, nested to T, 2, provide a FromString static method taking a const char*
614 * (string data) and a size_t (string length) and returning T::Type.
616 template<typename T> // T must have a nested enum called Type and a static Type FromString(const char*) method.
617 typename T::Type ReadStringEnum(const json_value_s& j)
619 auto str = json::Read::StringView(j);
621 return T::FromString(str.data(), str.size());
625 * @brief Convenience method to attempt to create a Dali vector type T from an array of floats.
626 * @note T must provide an AsFloat() member method returning the non-const array of its
630 inline T ReadDaliVector(const json_value_s& j)
632 std::vector<float> floats = json::Read::Array<float, json::Read::Number<float>>(j);
634 std::copy(floats.begin(), std::min(floats.end(), floats.begin() + sizeof(T) / sizeof(float)), result.AsFloat());
639 * @brief Convenience method to attemt to read a Quaternion, which implicitly converts
640 * to Vector4 but fails to provide an AsFloat() method.
642 Dali::Quaternion ReadQuaternion(const json_value_s& j);
646 #endif //DALI_SCENE3D_LOADER_GLTF2_ASSET_H_