[dali_2.3.24] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / loader / gltf2-asset.h
index 6576803..16c7cd8 100644 (file)
  *
  */
 
-// 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>
 
+// 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) \
   {                               \
 #x, t::x                      \
@@ -179,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;
 };
@@ -332,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;
@@ -339,9 +417,11 @@ struct TextureInfo
   float               mScale    = 1.f;
   float               mStrength = 1.f;
 
+  TextureExtensions mTextureExtensions;
+
   operator bool() const
   {
-    return !!mTexture;
+    return mTexture;
   }
 };
 
@@ -415,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;
@@ -426,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;
@@ -548,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;
@@ -569,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;