1 #ifndef DALI_SCENE_LOADER_GLTF2_ASSET_H_
2 #define DALI_SCENE_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-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 mGenerator;
97 std::string_view mVersion;
105 UNSIGNED_BYTE = 5121,
107 UNSIGNED_SHORT = 5123,
113 static bool IsUnsigned(Type t);
114 static uint32_t Size(Type t);
116 Component() = delete;
133 static uint32_t ElementCount(Type t);
135 static Type FromString(const char* s, size_t len);
137 AccessorType() = delete;
150 static Type FromString(const char* s, size_t len);
152 AlphaMode() = delete;
170 static Type FromString(const char* s, size_t len);
172 Attribute() = delete;
177 uint32_t mByteLength;
178 std::string_view mUri;
190 ARRAY_BUFFER = 34962,
191 ELEMENT_ARRAY_BUFFER = 34963
198 uint32_t mByteOffset = 0;
199 uint32_t mByteLength;
200 uint32_t mByteStride = 0; // if 0 after reading, it needs to be calculated
206 struct BufferViewClient
208 Ref<BufferView> mBufferView;
209 uint32_t mByteOffset = 0;
212 struct ComponentTypedBufferViewClient : BufferViewClient
214 Component::Type mComponentType = Component::INVALID;
216 uint32_t GetBytesPerComponent() const;
221 std::string_view mName;
227 struct Accessor : ComponentTypedBufferViewClient, Named
232 ComponentTypedBufferViewClient mIndices;
233 BufferViewClient mValues;
239 bool mNormalized = false;
240 AccessorType::Type mType = AccessorType::INVALID;
241 std::vector<float> mMin;
242 std::vector<float> mMax;
243 std::unique_ptr<Sparse> mSparse;
247 uint32_t GetElementSizeBytes() const
249 return GetBytesPerComponent() * AccessorType::ElementCount(mType);
252 uint32_t GetBytesLength() const
254 return GetElementSizeBytes() * mCount;
257 void SetSparse(const Sparse& s)
259 mSparse.reset(new Sparse(s));
265 std::string_view mUri;
266 std::string_view mMimeType;
267 Ref<BufferView> mBufferView;
278 NEAREST_MIPMAP_NEAREST = 9984,
279 NEAREST_MIPMAP_LINEAR = 9985,
280 LINEAR_MIPMAP_NEAREST = 9986,
281 LINEAR_MIPMAP_LINEAR = 9987,
292 CLAMP_TO_EDGE = 33071,
293 MIRRORED_REPEAT = 33648,
301 Filter::Type mMinFilter = Filter::LINEAR;
302 Filter::Type mMagFilter = Filter::LINEAR;
303 Wrap::Type mWrapS = Wrap::CLAMP_TO_EDGE;
304 Wrap::Type mWrapT = Wrap::CLAMP_TO_EDGE;
312 Ref<Sampler> mSampler;
317 Ref<gltf2::Texture> mTexture;
318 uint32_t mTexCoord = 0;
320 float mStrength = 1.f;
322 operator bool() const
328 struct Material : Named
330 struct Pbr //MetallicRoughness
332 Dali::Vector4 mBaseColorFactor = Dali::Vector4::ONE;
333 TextureInfo mBaseColorTexture;
334 float mMetallicFactor = 1.f;
335 float mRoughnessFactor = 1.f;
336 TextureInfo mMetallicRoughnessTexture;
341 Pbr mPbrMetallicRoughness;
342 TextureInfo mNormalTexture;
343 TextureInfo mOcclusionTexture;
344 TextureInfo mEmissiveTexture;
345 Dali::Vector3 mEmissiveFactor;
346 AlphaMode::Type mAlphaMode = AlphaMode::OPAQUE;
347 float mAlphaCutoff = .5f;
348 bool mDoubleSided = false;
369 std::map<Attribute::Type, Ref<Accessor>> mAttributes;
370 std::vector<std::map<Attribute::Type, Ref<Accessor>>> mTargets;
371 Ref<Accessor> mIndices;
372 Ref<Material> mMaterial;
373 Mode mMode = TRIANGLES;
375 //TODO: [morph] targets
380 std::vector<Primitive> mPrimitives;
381 std::vector<float> mWeights;
390 Ref<Accessor> mInverseBindMatrices;
392 std::vector<Ref<Node>> mJoints;
397 struct Camera : Named
419 std::string_view mType;
420 Perspective mPerspective;
421 Orthographic mOrthographic;
428 Dali::Vector3 mTranslation = Dali::Vector3::ZERO;
429 Dali::Quaternion mRotation = Dali::Quaternion::IDENTITY;
430 Dali::Vector3 mScale = Dali::Vector3::ONE;
433 std::vector<Ref<Node>> mChildren;
437 //TODO: [morph] weights
441 void SetMatrix(const Dali::Matrix& m);
444 struct Animation : Named
457 static Type FromString(const char* s, size_t len);
460 Ref<Accessor> mInput;
461 Ref<Accessor> mOutput;
462 Interpolation::Type mInterpolation;
481 static Type FromString(const char* s, size_t len);
487 Ref<Sampler> mSampler;
493 std::vector<Sampler> mSamplers;
494 std::vector<Channel> mChannels;
499 std::vector<Ref<Node>> mNodes;
506 std::vector<Buffer> mBuffers;
507 std::vector<BufferView> mBufferViews;
508 std::vector<Accessor> mAccessors;
510 std::vector<Image> mImages;
511 std::vector<Sampler> mSamplers;
512 std::vector<Texture> mTextures;
513 std::vector<Material> mMaterials;
515 std::vector<Mesh> mMeshes;
516 std::vector<Skin> mSkins;
518 std::vector<Camera> mCameras;
519 std::vector<Node> mNodes;
521 std::vector<Animation> mAnimations;
523 std::vector<Scene> mScenes;
526 Document() = default;
527 Document(const Document&) = delete;
528 Document(Document&&) = default;
530 Document& operator=(const Document&) = delete;
531 Document& operator=(Document&&) = default;
535 * @brief Provides a json::Property<T>::ReadFn for interpreting unsigned integers
536 * as a Ref<U> into a std::vector<U> data member of a type T.
543 template<typename U, std::vector<U> T::*V>
544 static Ref<U> Read(const json_value_s& j)
546 uint32_t index = json::Read::Number<uint32_t>(j);
547 return Ref<U>(sObject->*V, index);
552 T* RefReader<T>::sObject = nullptr;
555 * @brief Convenience method to set the object for RefReader.
558 void SetRefReaderObject(T& object)
560 RefReader<T>::sObject = &object;
564 * @brief Reads a string and attempts to convert it to an enum.
565 * @note The enum must: 1, be called Type, nested to T, 2, provide a FromString static method taking a const char*
566 * (string data) and a size_t (string length) and returning T::Type.
568 template<typename T> // T must have a nested enum called Type and a static Type FromString(const char*) method.
569 typename T::Type ReadStringEnum(const json_value_s& j)
571 auto str = json::Read::StringView(j);
573 return T::FromString(str.data(), str.size());
577 * @brief Convenience method to attempt to create a Dali vector type T from an array of floats.
578 * @note T must provide an AsFloat() member method returning the non-const array of its
582 inline T ReadDaliVector(const json_value_s& j)
584 std::vector<float> floats = json::Read::Array<float, json::Read::Number<float>>(j);
586 std::copy(floats.begin(), std::min(floats.end(), floats.begin() + sizeof(T) / sizeof(float)), result.AsFloat());
591 * @brief Convenience method to attemt to read a Quaternion, which implicitly converts
592 * to Vector4 but fails to provide an AsFloat() method.
594 Dali::Quaternion ReadQuaternion(const json_value_s& j);
598 #endif //DALI_SCENE_LOADER_GLTF2_ASSET_H_