1 #ifndef DALI_SCENE_LOADER_GLTF2_ASSET_H_
2 #define DALI_SCENE_LOADER_GLTF2_ASSET_H_
4 * Copyright (c) 2021 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-scene-loader/internal/json-reader.h"
22 #include "dali-scene-loader/public-api/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"
34 using Index = Dali::SceneLoader::Index;
41 Ref(std::vector<T>& v, Index i)
48 * @return The index of the object into the vector.
49 * @note It is client code responsibility to ensure that the vector is unambiguous. It should be in
50 * a glTF document, since there's one vector for each type.
52 Index GetIndex() const
58 * @brief There may be scenarios in which the object, whose vector we're populating, changes, e.g.
59 * when we don't have a final one at the time of reading the references.
61 void UpdateVector(std::vector<T>& v)
68 return mVector != nullptr;
72 return &(*mVector)[mIndex];
76 return (*mVector)[mIndex];
79 bool operator==(const Ref<T>& other) const
81 return mVector == other.mVector && mIndex == other.mIndex;
84 bool operator!=(const Ref<T>& other) const
86 return !operator==(other);
90 std::vector<T>* mVector = nullptr;
91 Index mIndex = Dali::SceneLoader::INVALID_INDEX;
96 std::string_view mVersion;
104 UNSIGNED_BYTE = 5121,
106 UNSIGNED_SHORT = 5123,
112 static bool IsUnsigned(Type t);
113 static uint32_t Size(Type t);
115 Component() = delete;
132 static uint32_t ElementCount(Type t);
134 static Type FromString(const char* s, size_t len);
136 AccessorType() = delete;
149 static Type FromString(const char* s, size_t len);
151 AlphaMode() = delete;
169 static Type FromString(const char* s, size_t len);
171 Attribute() = delete;
176 uint32_t mByteLength;
177 std::string_view mUri;
189 ARRAY_BUFFER = 34962,
190 ELEMENT_ARRAY_BUFFER = 34963
197 uint32_t mByteOffset = 0;
198 uint32_t mByteLength;
199 uint32_t mByteStride = 0; // if 0 after reading, it needs to be calculated
205 struct BufferViewClient
207 Ref<BufferView> mBufferView;
208 uint32_t mByteOffset = 0;
211 struct ComponentTypedBufferViewClient : BufferViewClient
213 Component::Type mComponentType = Component::INVALID;
215 uint32_t GetBytesPerComponent() const;
220 std::string_view mName;
226 struct Accessor : ComponentTypedBufferViewClient, Named
231 ComponentTypedBufferViewClient mIndices;
232 BufferViewClient mValues;
238 bool mNormalized = false;
239 AccessorType::Type mType = AccessorType::INVALID;
240 std::vector<float> mMin;
241 std::vector<float> mMax;
242 std::unique_ptr<Sparse> mSparse;
246 uint32_t GetElementSizeBytes() const
248 return GetBytesPerComponent() * AccessorType::ElementCount(mType);
251 uint32_t GetBytesLength() const
253 return GetElementSizeBytes() * mCount;
256 void SetSparse(const Sparse& s)
258 mSparse.reset(new Sparse(s));
264 std::string_view mUri;
265 std::string_view mMimeType;
266 Ref<BufferView> mBufferView;
277 NEAREST_MIPMAP_NEAREST = 9984,
278 NEAREST_MIPMAP_LINEAR = 9985,
279 LINEAR_MIPMAP_NEAREST = 9986,
280 LINEAR_MIPMAP_LINEAR = 9987,
291 CLAMP_TO_EDGE = 33071,
292 MIRRORED_REPEAT = 33648,
300 Filter::Type mMinFilter = Filter::LINEAR;
301 Filter::Type mMagFilter = Filter::LINEAR;
302 Wrap::Type mWrapS = Wrap::CLAMP_TO_EDGE;
303 Wrap::Type mWrapT = Wrap::CLAMP_TO_EDGE;
311 Ref<Sampler> mSampler;
316 Ref<gltf2::Texture> mTexture;
317 uint32_t mTexCoord = 0;
320 operator bool() const
326 struct Material : Named
328 struct Pbr //MetallicRoughness
330 Dali::Vector4 mBaseColorFactor = Dali::Vector4::ONE;
331 TextureInfo mBaseColorTexture;
332 float mMetallicFactor = 1.f;
333 float mRoughnessFactor = 1.f;
334 TextureInfo mMetallicRoughnessTexture;
339 Pbr mPbrMetallicRoughness;
340 TextureInfo mNormalTexture;
341 TextureInfo mOcclusionTexture;
342 TextureInfo mEmissiveTexture;
343 Dali::Vector3 mEmissiveFactor;
344 AlphaMode::Type mAlphaMode = AlphaMode::OPAQUE;
345 float mAlphaCutoff = .5f;
346 bool mDoubleSided = false;
367 std::map<Attribute::Type, Ref<Accessor>> mAttributes;
368 std::vector<std::map<Attribute::Type, Ref<Accessor>>> mTargets;
369 Ref<Accessor> mIndices;
370 Ref<Material> mMaterial;
371 Mode mMode = TRIANGLES;
373 //TODO: [morph] targets
378 std::vector<Primitive> mPrimitives;
379 std::vector<float> mWeights;
388 Ref<Accessor> mInverseBindMatrices;
390 std::vector<Ref<Node>> mJoints;
395 struct Camera : Named
417 std::string_view mType;
418 Perspective mPerspective;
419 Orthographic mOrthographic;
426 Dali::Vector3 mTranslation = Dali::Vector3::ZERO;
427 Dali::Quaternion mRotation = Dali::Quaternion::IDENTITY;
428 Dali::Vector3 mScale = Dali::Vector3::ONE;
431 std::vector<Ref<Node>> mChildren;
435 //TODO: [morph] weights
439 void SetMatrix(const Dali::Matrix& m);
442 struct Animation : Named
455 static Type FromString(const char* s, size_t len);
458 Ref<Accessor> mInput;
459 Ref<Accessor> mOutput;
460 Interpolation::Type mInterpolation;
479 static Type FromString(const char* s, size_t len);
485 Ref<Sampler> mSampler;
491 std::vector<Sampler> mSamplers;
492 std::vector<Channel> mChannels;
497 std::vector<Ref<Node>> mNodes;
504 std::vector<Buffer> mBuffers;
505 std::vector<BufferView> mBufferViews;
506 std::vector<Accessor> mAccessors;
508 std::vector<Image> mImages;
509 std::vector<Sampler> mSamplers;
510 std::vector<Texture> mTextures;
511 std::vector<Material> mMaterials;
513 std::vector<Mesh> mMeshes;
514 std::vector<Skin> mSkins;
516 std::vector<Camera> mCameras;
517 std::vector<Node> mNodes;
519 std::vector<Animation> mAnimations;
521 std::vector<Scene> mScenes;
524 Document() = default;
525 Document(const Document&) = delete;
526 Document(Document&&) = default;
528 Document& operator=(const Document&) = delete;
529 Document& operator=(Document&&) = default;
533 * @brief Provides a json::Property<T>::ReadFn for interpreting unsigned integers
534 * as a Ref<U> into a std::vector<U> data member of a type T.
541 template<typename U, std::vector<U> T::*V>
542 static Ref<U> Read(const json_value_s& j)
544 uint32_t index = json::Read::Number<uint32_t>(j);
545 return Ref<U>(sObject->*V, index);
550 T* RefReader<T>::sObject = nullptr;
553 * @brief Convenience method to set the object for RefReader.
556 void SetRefReaderObject(T& object)
558 RefReader<T>::sObject = &object;
562 * @brief Reads a string and attempts to convert it to an enum.
563 * @note The enum must: 1, be called Type, nested to T, 2, provide a FromString static method taking a const char*
564 * (string data) and a size_t (string length) and returning T::Type.
566 template<typename T> // T must have a nested enum called Type and a static Type FromString(const char*) method.
567 typename T::Type ReadStringEnum(const json_value_s& j)
569 auto str = json::Read::StringView(j);
571 return T::FromString(str.data(), str.size());
575 * @brief Convenience method to attempt to create a Dali vector type T from an array of floats.
576 * @note T must provide an AsFloat() member method returning the non-const array of its
580 inline T ReadDaliVector(const json_value_s& j)
582 std::vector<float> floats = json::Read::Array<float, json::Read::Number<float>>(j);
584 std::copy(floats.begin(), std::min(floats.end(), floats.begin() + sizeof(T) / sizeof(float)), result.AsFloat());
589 * @brief Convenience method to attemt to read a Quaternion, which implicitly converts
590 * to Vector4 but fails to provide an AsFloat() method.
592 Dali::Quaternion ReadQuaternion(const json_value_s& j);
596 #endif //DALI_SCENE_LOADER_GLTF2_ASSET_H_