[dali_2.3.24] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / loader / gltf2-asset.h
index 19d56b6..16c7cd8 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef DALI_SCENE3D_LOADER_GLTF2_ASSET_H_
 #define DALI_SCENE3D_LOADER_GLTF2_ASSET_H_
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  *
  */
 
-// INTERNAL INCLUDES
-#include "dali-scene3d/internal/loader/json-reader.h"
-#include "dali-scene3d/public-api/loader/index.h"
-
 // EXTERNAL INCLUDES
+#include <dali/devel-api/common/map-wrapper.h>
+#include <dali/public-api/common/vector-wrapper.h>
+#include <dali/public-api/math/matrix3.h>
+#include <dali/public-api/math/quaternion.h>
+#include <dali/public-api/math/vector2.h>
+#include <dali/public-api/math/vector4.h>
 #include <cstdint>
 #include <memory>
-#include "dali/devel-api/common/map-wrapper.h"
-#include "dali/public-api/common/vector-wrapper.h"
-#include "dali/public-api/math/quaternion.h"
-#include "dali/public-api/math/vector4.h"
+
+// INTERNAL INCLUDES
+#include <dali-scene3d/internal/loader/json-reader.h>
+#include <dali-scene3d/public-api/loader/index.h>
+#include <dali-scene3d/public-api/loader/utils.h>
 
 #define ENUM_STRING_MAPPING(t, x) \
   {                               \
@@ -50,7 +53,8 @@
 
 namespace gltf2
 {
-using Index = Dali::Scene3D::Loader::Index;
+using Index                           = Dali::Scene3D::Loader::Index;
+constexpr float UNDEFINED_FLOAT_VALUE = -1.0f; ///< Special marker for some non-negative only float values.
 
 template<typename T>
 class Ref
@@ -178,15 +182,57 @@ struct Attribute
     POSITION,
     NORMAL,
     TANGENT,
-    TEXCOORD_0,
-    TEXCOORD_1,
-    COLOR_0,
-    JOINTS_0,
-    WEIGHTS_0,
+    TEXCOORD_N,
+    COLOR_N,
+    JOINTS_N,
+    WEIGHTS_N,
     INVALID
   };
 
-  static Type FromString(const char* s, size_t len);
+  using HashType = uint32_t;
+
+  // Hash bit layout
+  // +--+--+--+--+--+--+--+
+  // |31|30|29|28|27|..| 0| bit index
+  // +--+--+--+--+--+--+--+
+  //  \_/ - Set is used
+  //     \______/ - Type enum
+  //              \_______/ - Set ID
+  static const HashType SET_SHIFT{31};
+  static const HashType TYPE_SHIFT{28};
+  static const HashType SET_MASK{0x01u << SET_SHIFT};
+  static const HashType TYPE_MASK{0x07 << TYPE_SHIFT};
+  static const HashType SET_ID_MASK{0x0fffffff};
+
+  static HashType ToHash(Type type, bool set, HashType setIndex)
+  {
+    return ((set << SET_SHIFT) & SET_MASK) | ((static_cast<HashType>(type) << TYPE_SHIFT) & TYPE_MASK) | (setIndex & SET_ID_MASK);
+  }
+
+  static Attribute::Type TypeFromHash(HashType hash)
+  {
+    return static_cast<Type>((hash & TYPE_MASK) >> TYPE_SHIFT);
+  }
+
+  static bool SetFromHash(HashType hash)
+  {
+    return (hash & SET_SHIFT) != 0;
+  }
+
+  static HashType SetIdFromHash(HashType hash)
+  {
+    return (hash & SET_ID_MASK);
+  }
+
+  /**
+   * Convert to Type + setIndex, where setIndex is N for that attr, e.g. "JOINTS_1" => {JOINTS_N, 1}
+   */
+  static HashType HashFromString(const char* s, size_t len);
+
+  /**
+   * Convert to type only, there is no set for POSITION, NORMALS or TANGENT.
+   */
+  static Attribute::Type TargetFromString(const char* s, size_t len);
 
   Attribute() = delete;
 };
@@ -319,8 +365,8 @@ struct Sampler
 {
   Filter::Type mMinFilter = Filter::LINEAR;
   Filter::Type mMagFilter = Filter::LINEAR;
-  Wrap::Type   mWrapS     = Wrap::CLAMP_TO_EDGE;
-  Wrap::Type   mWrapT     = Wrap::CLAMP_TO_EDGE;
+  Wrap::Type   mWrapS     = Wrap::REPEAT;
+  Wrap::Type   mWrapT     = Wrap::REPEAT;
   //TODO: extensions
   //TODO: extras
 };
@@ -331,6 +377,39 @@ struct Texture
   Ref<Sampler> mSampler;
 };
 
+struct TextureTransform
+{
+  float         mRotation = 0.0f;
+  Dali::Vector2 mUvOffset = Dali::Vector2::ZERO;
+  Dali::Vector2 mUvScale  = Dali::Vector2::ONE;
+  uint32_t      mTexCoord = 0u;
+
+  operator bool() const
+  {
+    return !Dali::EqualsZero(mRotation) || mUvOffset != Dali::Vector2::ZERO || mUvScale != Dali::Vector2::ONE || mTexCoord != 0u;
+  }
+
+  const Dali::Matrix3 GetTransform() const
+  {
+    // See: https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_texture_transform
+    Dali::Matrix3 translation = Dali::Matrix3(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, mUvOffset.x, mUvOffset.y, 1.0f);
+    Dali::Matrix3 rotation    = Dali::Matrix3(cos(-mRotation), sin(-mRotation), 0.0f, -sin(-mRotation), cos(-mRotation), 0.0f, 0.0f, 0.0f, 1.0f);
+    Dali::Matrix3 scale       = Dali::Matrix3(mUvScale.x, 0.0f, 0.0f, 0.0f, mUvScale.y, 0.0f, 0.0f, 0.0f, 1.0f);
+
+    return translation * rotation * scale;
+  }
+};
+
+struct TextureExtensions
+{
+  TextureTransform mTextureTransform;
+
+  operator bool() const
+  {
+    return mTextureTransform;
+  }
+};
+
 struct TextureInfo
 {
   Ref<gltf2::Texture> mTexture;
@@ -338,9 +417,11 @@ struct TextureInfo
   float               mScale    = 1.f;
   float               mStrength = 1.f;
 
+  TextureExtensions mTextureExtensions;
+
   operator bool() const
   {
-    return !!mTexture;
+    return mTexture;
   }
 };
 
@@ -350,7 +431,7 @@ struct TextureInfo
  */
 struct MaterialIor
 {
-  float mIor = MAXFLOAT;
+  float mIor = UNDEFINED_FLOAT_VALUE;
 };
 
 /**
@@ -414,7 +495,7 @@ struct Mesh : Named
       INVALID
     };
 
-    std::map<Attribute::Type, Ref<Accessor>>              mAttributes;
+    std::map<Attribute::HashType, Ref<Accessor>>          mAttributes;
     std::vector<std::map<Attribute::Type, Ref<Accessor>>> mTargets;
     Ref<Accessor>                                         mIndices;
     Ref<Material>                                         mMaterial;
@@ -425,10 +506,25 @@ struct Mesh : Named
     //TODO: extensions
   };
 
+  struct Extras
+  {
+    std::vector<std::string_view> mTargetNames;
+
+    //TODO: extras
+  };
+
+  struct Extensions
+  {
+    std::vector<std::string_view> mSXRTargetsNames;
+    std::vector<std::string_view> mAvatarShapeNames;
+
+    //TODO: extensions
+  };
+
   std::vector<Primitive> mPrimitives;
   std::vector<float>     mWeights;
-  //TODO: extras
-  //TODO: extensions
+  Extras                 mExtras;
+  Extensions             mExtensions;
 };
 
 struct Node;
@@ -446,20 +542,20 @@ struct Camera : Named
 {
   struct Perspective
   {
-    float mAspectRatio;
-    float mYFov;
-    float mZFar;
-    float mZNear;
+    float mAspectRatio = UNDEFINED_FLOAT_VALUE;
+    float mYFov        = UNDEFINED_FLOAT_VALUE;
+    float mZFar        = UNDEFINED_FLOAT_VALUE;
+    float mZNear       = UNDEFINED_FLOAT_VALUE;
     //TODO: extras
     //TODO: extensions
   };
 
   struct Orthographic
   {
-    float mXMag;
-    float mYMag;
-    float mZFar;
-    float mZNear;
+    float mXMag  = UNDEFINED_FLOAT_VALUE;
+    float mYMag  = UNDEFINED_FLOAT_VALUE;
+    float mZFar  = UNDEFINED_FLOAT_VALUE;
+    float mZNear = UNDEFINED_FLOAT_VALUE;
     //TODO: extras
     //TODO: extensions
   };
@@ -547,6 +643,16 @@ struct Scene : Named
   std::vector<Ref<Node>> mNodes;
 };
 
+enum ExtensionFlags : uint32_t
+{
+  NONE                   = Dali::Scene3D::Loader::NthBit(0),
+  KHR_MESH_QUANTIZATION  = Dali::Scene3D::Loader::NthBit(1), // See https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_texture_transform
+  KHR_TEXTURE_TRANSFORM  = Dali::Scene3D::Loader::NthBit(2), // See https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_mesh_quantization
+  KHR_MATERIALS_IOR      = Dali::Scene3D::Loader::NthBit(3), // See https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_ior
+  KHR_MATERIALS_SPECULAR = Dali::Scene3D::Loader::NthBit(4), // See https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_specular
+
+};
+
 struct Document
 {
   Asset mAsset;
@@ -568,6 +674,11 @@ struct Document
 
   std::vector<Animation> mAnimations;
 
+  std::vector<std::string_view> mExtensionsUsed;
+  std::vector<std::string_view> mExtensionsRequired;
+
+  uint32_t mExtensionFlags{0};
+
   std::vector<Scene> mScenes;
   Ref<Scene>         mScene;