1 #ifndef DALI_SCENE_LOADER_GLTF2_ASSET_H_
2 #define DALI_SCENE_LOADER_GLTF2_ASSET_H_
4 * Copyright (c) 2020 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/public-api/index.h"
22 #include "dali-scene-loader/internal/json-reader.h"
25 #include "dali/public-api/math/vector4.h"
26 #include "dali/public-api/math/quaternion.h"
27 #include "dali/public-api/common/vector-wrapper.h"
28 #include "dali/devel-api/common/map-wrapper.h"
35 using Index = Dali::SceneLoader::Index;
42 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 { return mIndex; }
55 * @brief There may be scenarios in which the object, whose vector we're populating, changes, e.g.
56 * when we don't have a final one at the time of reading the references.
58 void UpdateVector(std::vector<T>& v)
63 operator bool() const { return mVector != nullptr; }
64 T* operator->() const { return &(*mVector)[mIndex]; }
65 T& operator*() const { return (*mVector)[mIndex]; }
67 bool operator==(const Ref<T>& other) const
69 return mVector == other.mVector && mIndex == other.mIndex;
72 bool operator!=(const Ref<T>& other) const
74 return !operator==(other);
78 std::vector<T>* mVector = nullptr;
79 Index mIndex = Dali::SceneLoader::INVALID_INDEX;
84 std::string_view mVersion;
94 UNSIGNED_SHORT = 5123,
100 static bool IsUnsigned(Type t);
101 static uint32_t Size(Type t);
103 Component() = delete;
120 static uint32_t ElementCount(Type t);
122 static Type FromString(const char* s, size_t len);
124 AccessorType() = delete;
137 static Type FromString(const char* s, size_t len);
139 AlphaMode() = delete;
157 static Type FromString(const char* s, size_t len);
159 Attribute() = delete;
164 uint32_t mByteLength;
165 std::string_view mUri;
177 ARRAY_BUFFER = 34962,
178 ELEMENT_ARRAY_BUFFER = 34963
185 uint32_t mByteOffset = 0;
186 uint32_t mByteLength;
187 uint32_t mByteStride = 0; // if 0 after reading, it needs to be calculated
193 struct BufferViewClient
195 Ref<BufferView> mBufferView;
196 uint32_t mByteOffset = 0;
199 struct ComponentTypedBufferViewClient : BufferViewClient
201 Component::Type mComponentType = Component::INVALID;
203 uint32_t GetBytesPerComponent() const;
208 std::string_view mName;
214 struct Accessor : ComponentTypedBufferViewClient, Named
219 ComponentTypedBufferViewClient mIndices;
220 BufferViewClient mValues;
226 bool mNormalized = false;
227 AccessorType::Type mType = AccessorType::INVALID;
228 std::vector<float> mMin;
229 std::vector<float> mMax;
230 std::unique_ptr<Sparse> mSparse;
234 uint32_t GetElementSizeBytes() const
236 return GetBytesPerComponent() * AccessorType::ElementCount(mType);
239 uint32_t GetBytesLength() const
241 return GetElementSizeBytes() * mCount;
244 void SetSparse(const Sparse& s)
246 mSparse.reset(new Sparse(s));
252 std::string_view mUri;
253 std::string_view mMimeType;
254 Ref<BufferView> mBufferView;
265 NEAREST_MIPMAP_NEAREST = 9984,
266 NEAREST_MIPMAP_LINEAR = 9985,
267 LINEAR_MIPMAP_NEAREST = 9986,
268 LINEAR_MIPMAP_LINEAR = 9987,
279 CLAMP_TO_EDGE = 33071,
280 MIRRORED_REPEAT = 33648,
288 Filter::Type mMinFilter = Filter::LINEAR;
289 Filter::Type mMagFilter = Filter::LINEAR;
290 Wrap::Type mWrapS = Wrap::CLAMP_TO_EDGE;
291 Wrap::Type mWrapT = Wrap::CLAMP_TO_EDGE;
299 Ref<Sampler> mSampler;
304 Ref<gltf2::Texture> mTexture;
305 uint32_t mTexCoord = 0;
308 operator bool() const
314 struct Material: Named
316 struct Pbr//MetallicRoughness
318 Dali::Vector4 mBaseColorFactor = Dali::Vector4::ONE;
319 TextureInfo mBaseColorTexture;
320 float mMetallicFactor = 1.f;
321 float mRoughnessFactor = 1.f;
322 TextureInfo mMetallicRoughnessTexture;
327 Pbr mPbrMetallicRoughness;
328 TextureInfo mNormalTexture;
329 TextureInfo mOcclusionTexture;
330 TextureInfo mEmissiveTexture;
331 Dali::Vector3 mEmissiveFactor;
332 AlphaMode::Type mAlphaMode = AlphaMode::OPAQUE;
333 float mAlphaCutoff = .5f;
334 bool mDoubleSided = false;
355 std::map<Attribute::Type, Ref<Accessor>> mAttributes;
356 std::vector<std::map<Attribute::Type, Ref<Accessor>>> mTargets;
357 Ref<Accessor> mIndices;
358 Ref<Material> mMaterial;
359 Mode mMode = TRIANGLES;
361 //TODO: [morph] targets
366 std::vector<Primitive> mPrimitives;
367 std::vector<float> mWeights;
376 Ref<Accessor> mInverseBindMatrices;
378 std::vector<Ref<Node>> mJoints;
405 std::string_view mType;
406 Perspective mPerspective;
407 Orthographic mOrthographic;
414 Dali::Vector3 mTranslation = Dali::Vector3::ZERO;
415 Dali::Quaternion mRotation = Dali::Quaternion::IDENTITY;
416 Dali::Vector3 mScale = Dali::Vector3::ONE;
419 std::vector<Ref<Node>> mChildren;
423 //TODO: [morph] weights
427 void SetMatrix(const Dali::Matrix& m);
430 struct Animation : Named
443 static Type FromString(const char* s, size_t len);
446 Ref<Accessor> mInput;
447 Ref<Accessor> mOutput;
448 Interpolation::Type mInterpolation;
467 static Type FromString(const char* s, size_t len);
473 Ref<Sampler> mSampler;
479 std::vector<Sampler> mSamplers;
480 std::vector<Channel> mChannels;
485 std::vector<Ref<Node>> mNodes;
492 std::vector<Buffer> mBuffers;
493 std::vector<BufferView> mBufferViews;
494 std::vector<Accessor> mAccessors;
496 std::vector<Image> mImages;
497 std::vector<Sampler> mSamplers;
498 std::vector<Texture> mTextures;
499 std::vector<Material> mMaterials;
501 std::vector<Mesh> mMeshes;
502 std::vector<Skin> mSkins;
504 std::vector<Camera> mCameras;
505 std::vector<Node> mNodes;
507 std::vector<Animation> mAnimations;
509 std::vector<Scene> mScenes;
512 Document() = default;
513 Document(const Document&) = delete;
514 Document(Document&&) = default;
516 Document& operator=(const Document&) = delete;
517 Document& operator=(Document&&) = default;
521 * @brief Provides a json::Property<T>::ReadFn for interpreting unsigned integers
522 * as a Ref<U> into a std::vector<U> data member of a type T.
524 template <typename T>
529 template <typename U, std::vector<U> T::* V>
530 static Ref<U> Read(const json_value_s& j)
532 uint32_t index = json::Read::Number<uint32_t>(j);
533 return Ref<U>(sObject->*V, index);
537 template <typename T>
538 T* RefReader<T>::sObject = nullptr;
541 * @brief Convenience method to set the object for RefReader.
543 template <typename T>
544 void SetRefReaderObject(T& object)
546 RefReader<T>::sObject = &object;
550 * @brief Reads a string and attempts to convert it to an enum.
551 * @note The enum must: 1, be called Type, nested to T, 2, provide a FromString static method taking a const char*
552 * (string data) and a size_t (string length) and returning T::Type.
554 template <typename T> // T must have a nested enum called Type and a static Type FromString(const char*) method.
555 typename T::Type ReadStringEnum(const json_value_s& j)
557 auto str = json::Read::StringView(j);
559 return T::FromString(str.data(), str.size());
563 * @brief Convenience method to attempt to create a Dali vector type T from an array of floats.
564 * @note T must provide an AsFloat() member method returning the non-const array of its
567 template <typename T>
569 T ReadDaliVector(const json_value_s& j)
571 std::vector<float> floats = json::Read::Array<float, json::Read::Number<float>>(j);
573 std::copy(floats.begin(), std::min(floats.end(), floats.begin() + sizeof(T) / sizeof(float)), result.AsFloat());
578 * @brief Convenience method to attemt to read a Quaternion, which implicitly converts
579 * to Vector4 but fails to provide an AsFloat() method.
581 Dali::Quaternion ReadQuaternion(const json_value_s& j);
585 #endif //DALI_SCENE_LOADER_GLTF2_ASSET_H_