Merge "Fix the normalization factor calculation for blendshapes" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / mesh-definition.cpp
index 18093a5..6cc3d6a 100644 (file)
@@ -15,8 +15,8 @@
  *
  */
 
-// INTERNAL INCLUDES
-#include "dali-scene3d/public-api/loader/mesh-definition.h"
+// CLASS HEADER
+#include <dali-scene3d/public-api/loader/mesh-definition.h>
 
 // EXTERNAL INCLUDES
 #include <dali/devel-api/adaptor-framework/file-stream.h>
 #include <fstream>
 #include <type_traits>
 
-namespace Dali
-{
-namespace Scene3D
-{
-namespace Loader
+namespace Dali::Scene3D::Loader
 {
 namespace
 {
@@ -94,11 +90,11 @@ bool ReadBlob(const MeshDefinition::Blob& descriptor, std::istream& source, uint
       uint32_t       readSize  = 0;
       uint32_t       totalSize = (descriptor.mLength / descriptor.mElementSizeHint) * descriptor.mStride;
       while(readSize < totalSize &&
-            source.read(reinterpret_cast<char*>(target), descriptor.mElementSizeHint) &&
-            source.seekg(diff, std::istream::cur))
+            source.read(reinterpret_cast<char*>(target), descriptor.mElementSizeHint))
       {
         readSize += descriptor.mStride;
         target += descriptor.mElementSizeHint;
+        source.seekg(diff, std::istream::cur);
       }
       return readSize == totalSize;
     }
@@ -117,7 +113,7 @@ void ReadValues(const std::vector<uint8_t>& valuesBuffer, const std::vector<uint
   }
 }
 
-bool ReadAccessor(const MeshDefinition::Accessor& accessor, std::istream& source, uint8_t* target)
+bool ReadAccessor(const MeshDefinition::Accessor& accessor, std::istream& source, uint8_t* target, std::vector<uint32_t>* sparseIndices)
 {
   bool success = false;
 
@@ -156,31 +152,63 @@ bool ReadAccessor(const MeshDefinition::Accessor& accessor, std::istream& source
       return false;
     }
 
+    // If non-null sparse indices vector, prepare it for output
+    if(sparseIndices)
+    {
+      sparseIndices->resize(accessor.mSparse->mCount);
+    }
+
     switch(indices.mElementSizeHint)
     {
       case 1u:
       {
         ReadValues<uint8_t>(valuesBuffer, indicesBuffer, target, accessor.mSparse->mCount, values.mElementSizeHint);
+        if(sparseIndices)
+        {
+          // convert 8-bit indices into 32-bit
+          std::transform(indicesBuffer.begin(), indicesBuffer.end(), sparseIndices->begin(), [](const uint8_t& value) { return uint32_t(value); });
+        }
         break;
       }
       case 2u:
       {
         ReadValues<uint16_t>(valuesBuffer, indicesBuffer, target, accessor.mSparse->mCount, values.mElementSizeHint);
+        if(sparseIndices)
+        {
+          // convert 16-bit indices into 32-bit
+          std::transform(reinterpret_cast<uint16_t*>(indicesBuffer.data()),
+                         reinterpret_cast<uint16_t*>(indicesBuffer.data()) + accessor.mSparse->mCount,
+                         sparseIndices->begin(),
+                         [](const uint16_t& value) {
+                           return uint32_t(value);
+                         });
+        }
         break;
       }
       case 4u:
       {
         ReadValues<uint32_t>(valuesBuffer, indicesBuffer, target, accessor.mSparse->mCount, values.mElementSizeHint);
+        if(sparseIndices)
+        {
+          std::copy(indicesBuffer.begin(), indicesBuffer.end(), reinterpret_cast<uint8_t*>(sparseIndices->data()));
+        }
         break;
       }
       default:
+      {
         DALI_ASSERT_DEBUG(!"Unsupported type for an index");
+      }
     }
   }
 
   return success;
 }
 
+bool ReadAccessor(const MeshDefinition::Accessor& accessor, std::istream& source, uint8_t* target)
+{
+  return ReadAccessor(accessor, source, target, nullptr);
+}
+
 template<typename T>
 void ReadJointAccessor(MeshDefinition::RawData& raw, const MeshDefinition::Accessor& accessor, std::istream& source, const std::string& meshPath)
 {
@@ -215,6 +243,41 @@ void ReadJointAccessor(MeshDefinition::RawData& raw, const MeshDefinition::Acces
   raw.mAttribs.push_back({"aJoints", Property::VECTOR4, static_cast<uint32_t>(outBufferSize / sizeof(Vector4)), std::move(buffer)});
 }
 
+template<typename T>
+void ReadWeightAccessor(MeshDefinition::RawData& raw, const MeshDefinition::Accessor& accessor, std::istream& source, const std::string& meshPath)
+{
+  constexpr auto sizeofBlobUnit = sizeof(T) * 4;
+
+  DALI_ASSERT_ALWAYS(((accessor.mBlob.mLength % sizeofBlobUnit == 0) ||
+                      accessor.mBlob.mStride >= sizeofBlobUnit) &&
+                     "weights buffer length not a multiple of element size");
+  const auto inBufferSize  = accessor.mBlob.GetBufferSize();
+  const auto outBufferSize = (sizeof(Vector4) / sizeofBlobUnit) * inBufferSize;
+
+  std::vector<uint8_t> buffer(outBufferSize);
+  auto                 inBuffer = buffer.data() + outBufferSize - inBufferSize;
+  if(!ReadAccessor(accessor, source, inBuffer))
+  {
+    ExceptionFlinger(ASSERT_LOCATION) << "Failed to read weights from '" << meshPath << "'.";
+  }
+
+  if constexpr(sizeofBlobUnit != sizeof(Vector4))
+  {
+    auto       floats = reinterpret_cast<float*>(buffer.data());
+    const auto end    = inBuffer + inBufferSize;
+    while(inBuffer != end)
+    {
+      const auto value = *reinterpret_cast<T*>(inBuffer);
+      // Normalize weight value. value /= 255 for uint8_t weight, and value /= 65535 for uint16_t weight.
+      *floats = static_cast<float>(value) / static_cast<float>((1 << (sizeof(T) * 8)) - 1);
+
+      inBuffer += sizeof(T);
+      ++floats;
+    }
+  }
+  raw.mAttribs.push_back({"aWeights", Property::VECTOR4, static_cast<uint32_t>(outBufferSize / sizeof(Vector4)), std::move(buffer)});
+}
+
 template<bool use32BitsIndices, typename IndexProviderType = IndexProvider<use32BitsIndices>>
 bool GenerateNormals(MeshDefinition::RawData& raw)
 {
@@ -386,7 +449,7 @@ void CalculateTextureSize(uint32_t totalTextureSize, uint32_t& textureWidth, uin
 void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, const std::vector<MeshDefinition::BlendShape>& blendShapes, uint32_t numberOfVertices, float& blendShapeUnnormalizeFactor, BufferDefinition::Vector& buffers)
 {
   uint32_t geometryBufferIndex = 0u;
-  float    maxDistance         = 0.f;
+  float    maxDistanceSquared  = 0.f;
   Vector3* geometryBufferV3    = reinterpret_cast<Vector3*>(geometryBuffer);
   for(const auto& blendShape : blendShapes)
   {
@@ -396,21 +459,41 @@ void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, const std::vector<MeshDe
                           blendShape.deltas.mBlob.mStride >= sizeof(Vector3)) &&
                          "Blend Shape position buffer length not a multiple of element size");
 
-      const auto           bufferSize = blendShape.deltas.mBlob.GetBufferSize();
-      std::vector<uint8_t> buffer(bufferSize);
-      if(ReadAccessor(blendShape.deltas, buffers[blendShape.deltas.mBufferIdx].GetBufferStream(), buffer.data()))
+      const auto            bufferSize = blendShape.deltas.mBlob.GetBufferSize();
+      std::vector<uint8_t>  buffer(bufferSize);
+      std::vector<uint32_t> sparseIndices{};
+
+      if(ReadAccessor(blendShape.deltas, buffers[blendShape.deltas.mBufferIdx].GetBufferStream(), buffer.data(), &sparseIndices))
       {
-        blendShape.deltas.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()));
+        blendShape.deltas.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()), &sparseIndices);
+
         // Calculate the difference with the original mesh.
         // Find the max distance to normalize the deltas.
-        const Vector3* const deltasBuffer = reinterpret_cast<const Vector3* const>(buffer.data());
+        const auto* const deltasBuffer = reinterpret_cast<const Vector3* const>(buffer.data());
 
-        for(uint32_t index = 0u; index < numberOfVertices; ++index)
-        {
-          Vector3& delta = geometryBufferV3[geometryBufferIndex++];
-          delta          = deltasBuffer[index];
+        auto ProcessVertex = [&geometryBufferV3, &deltasBuffer, &maxDistanceSquared](uint32_t geometryBufferIndex, uint32_t deltaIndex) {
+          Vector3& delta = geometryBufferV3[geometryBufferIndex] = deltasBuffer[deltaIndex];
+          delta                                                  = deltasBuffer[deltaIndex];
+          return std::max(maxDistanceSquared, delta.LengthSquared());
+        };
 
-          maxDistance = std::max(maxDistance, delta.LengthSquared());
+        if(sparseIndices.empty())
+        {
+          for(uint32_t index = 0u; index < numberOfVertices; ++index)
+          {
+            maxDistanceSquared = ProcessVertex(geometryBufferIndex++, index);
+          }
+        }
+        else
+        {
+          // initialize blendshape texture
+          // TODO: there may be a case when sparse accessor uses a base buffer view for initial values.
+          std::fill(geometryBufferV3 + geometryBufferIndex, geometryBufferV3 + geometryBufferIndex + numberOfVertices, Vector3::ZERO);
+          for(auto index : sparseIndices)
+          {
+            maxDistanceSquared = ProcessVertex(geometryBufferIndex + index, index);
+          }
+          geometryBufferIndex += numberOfVertices;
         }
       }
     }
@@ -421,20 +504,18 @@ void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, const std::vector<MeshDe
                           blendShape.normals.mBlob.mStride >= sizeof(Vector3)) &&
                          "Blend Shape normals buffer length not a multiple of element size");
 
-      const auto           bufferSize = blendShape.normals.mBlob.GetBufferSize();
-      std::vector<uint8_t> buffer(bufferSize);
-      if(ReadAccessor(blendShape.normals, buffers[blendShape.normals.mBufferIdx].GetBufferStream(), buffer.data()))
+      const auto            bufferSize = blendShape.normals.mBlob.GetBufferSize();
+      std::vector<uint8_t>  buffer(bufferSize);
+      std::vector<uint32_t> sparseIndices;
+
+      if(ReadAccessor(blendShape.normals, buffers[blendShape.normals.mBufferIdx].GetBufferStream(), buffer.data(), &sparseIndices))
       {
-        blendShape.normals.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()));
+        blendShape.normals.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()), &sparseIndices);
 
         // Calculate the difference with the original mesh, and translate to make all values positive.
-        const Vector3* const deltasBuffer = reinterpret_cast<const Vector3* const>(buffer.data());
-
-        for(uint32_t index = 0u; index < numberOfVertices; ++index)
-        {
-          Vector3& delta = geometryBufferV3[geometryBufferIndex++];
-          delta          = deltasBuffer[index];
-
+        const Vector3* const deltasBuffer  = reinterpret_cast<const Vector3* const>(buffer.data());
+        auto                 ProcessVertex = [&geometryBufferV3, &deltasBuffer, &maxDistanceSquared](uint32_t geometryBufferIndex, uint32_t deltaIndex) {
+          Vector3& delta = geometryBufferV3[geometryBufferIndex] = deltasBuffer[deltaIndex];
           delta.x *= 0.5f;
           delta.y *= 0.5f;
           delta.z *= 0.5f;
@@ -442,6 +523,23 @@ void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, const std::vector<MeshDe
           delta.x += 0.5f;
           delta.y += 0.5f;
           delta.z += 0.5f;
+        };
+
+        if(sparseIndices.empty())
+        {
+          for(uint32_t index = 0u; index < numberOfVertices; ++index)
+          {
+            ProcessVertex(geometryBufferIndex++, index);
+          }
+        }
+        else
+        {
+          std::fill(geometryBufferV3 + geometryBufferIndex, geometryBufferV3 + geometryBufferIndex + numberOfVertices, Vector3(0.5, 0.5, 0.5));
+          for(auto index : sparseIndices)
+          {
+            ProcessVertex(geometryBufferIndex + index, index);
+          }
+          geometryBufferIndex += numberOfVertices;
         }
       }
     }
@@ -452,20 +550,18 @@ void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, const std::vector<MeshDe
                           blendShape.tangents.mBlob.mStride >= sizeof(Vector3)) &&
                          "Blend Shape tangents buffer length not a multiple of element size");
 
-      const auto           bufferSize = blendShape.tangents.mBlob.GetBufferSize();
-      std::vector<uint8_t> buffer(bufferSize);
-      if(ReadAccessor(blendShape.tangents, buffers[blendShape.tangents.mBufferIdx].GetBufferStream(), buffer.data()))
+      const auto            bufferSize = blendShape.tangents.mBlob.GetBufferSize();
+      std::vector<uint8_t>  buffer(bufferSize);
+      std::vector<uint32_t> sparseIndices;
+
+      if(ReadAccessor(blendShape.tangents, buffers[blendShape.tangents.mBufferIdx].GetBufferStream(), buffer.data(), &sparseIndices))
       {
-        blendShape.tangents.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()));
+        blendShape.tangents.mBlob.ApplyMinMax(static_cast<uint32_t>(bufferSize / sizeof(Vector3)), reinterpret_cast<float*>(buffer.data()), &sparseIndices);
 
         // Calculate the difference with the original mesh, and translate to make all values positive.
-        const Vector3* const deltasBuffer = reinterpret_cast<const Vector3* const>(buffer.data());
-
-        for(uint32_t index = 0u; index < numberOfVertices; ++index)
-        {
-          Vector3& delta = geometryBufferV3[geometryBufferIndex++];
-          delta          = deltasBuffer[index];
-
+        const Vector3* const deltasBuffer  = reinterpret_cast<const Vector3* const>(buffer.data());
+        auto                 ProcessVertex = [&geometryBufferV3, &deltasBuffer, &maxDistanceSquared](uint32_t geometryBufferIndex, uint32_t deltaIndex) {
+          Vector3& delta = geometryBufferV3[geometryBufferIndex] = deltasBuffer[deltaIndex];
           delta.x *= 0.5f;
           delta.y *= 0.5f;
           delta.z *= 0.5f;
@@ -473,12 +569,37 @@ void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, const std::vector<MeshDe
           delta.x += 0.5f;
           delta.y += 0.5f;
           delta.z += 0.5f;
+        };
+
+        if(sparseIndices.empty())
+        {
+          for(uint32_t index = 0u; index < numberOfVertices; ++index)
+          {
+            ProcessVertex(geometryBufferIndex++, index);
+          }
+        }
+        else
+        {
+          std::fill(geometryBufferV3 + geometryBufferIndex, geometryBufferV3 + geometryBufferIndex + numberOfVertices, Vector3(0.5, 0.5, 0.5));
+          for(auto index : sparseIndices)
+          {
+            ProcessVertex(geometryBufferIndex + index, index);
+          }
+          geometryBufferIndex += numberOfVertices;
         }
       }
     }
   }
 
   geometryBufferIndex = 0u;
+
+  const float maxDistance = sqrtf(maxDistanceSquared);
+
+  const float normalizeFactor = (maxDistanceSquared < Math::MACHINE_EPSILON_100) ? 1.f : (0.5f / maxDistance);
+
+  // Calculate and store the unnormalize factor.
+  blendShapeUnnormalizeFactor = maxDistance * 2.0f;
+
   for(const auto& blendShape : blendShapes)
   {
     // Normalize all the deltas and translate to a possitive value.
@@ -486,8 +607,6 @@ void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, const std::vector<MeshDe
     // whose values that are less than zero are clamped.
     if(blendShape.deltas.IsDefined())
     {
-      const float normalizeFactor = (fabsf(maxDistance) < Math::MACHINE_EPSILON_1000) ? 1.f : (0.5f / sqrtf(maxDistance));
-
       for(uint32_t index = 0u; index < numberOfVertices; ++index)
       {
         Vector3& delta = geometryBufferV3[geometryBufferIndex++];
@@ -495,9 +614,6 @@ void CalculateGltf2BlendShapes(uint8_t* geometryBuffer, const std::vector<MeshDe
         delta.y        = Clamp(((delta.y * normalizeFactor) + 0.5f), 0.f, 1.f);
         delta.z        = Clamp(((delta.z * normalizeFactor) + 0.5f), 0.f, 1.f);
       }
-
-      // Calculate and store the unnormalize factor.
-      blendShapeUnnormalizeFactor = 1.f / normalizeFactor;
     }
 
     if(blendShape.normals.IsDefined())
@@ -568,7 +684,7 @@ void MeshDefinition::Blob::ComputeMinMax(std::vector<float>& min, std::vector<fl
   }
 }
 
-void MeshDefinition::Blob::ApplyMinMax(const std::vector<float>& min, const std::vector<float>& max, uint32_t count, float* values)
+void MeshDefinition::Blob::ApplyMinMax(const std::vector<float>& min, const std::vector<float>& max, uint32_t count, float* values, std::vector<uint32_t>* sparseIndices)
 {
   DALI_ASSERT_DEBUG(max.size() == min.size() || max.size() * min.size() == 0);
   const auto numComponents = std::max(min.size(), max.size());
@@ -617,9 +733,9 @@ void MeshDefinition::Blob::ComputeMinMax(uint32_t numComponents, uint32_t count,
   ComputeMinMax(mMin, mMax, numComponents, count, values);
 }
 
-void MeshDefinition::Blob::ApplyMinMax(uint32_t count, float* values) const
+void MeshDefinition::Blob::ApplyMinMax(uint32_t count, float* values, std::vector<uint32_t>* sparseIndices) const
 {
-  ApplyMinMax(mMin, mMax, count, values);
+  ApplyMinMax(mMin, mMax, count, values, sparseIndices);
 }
 
 void MeshDefinition::RawData::Attrib::AttachBuffer(Geometry& g) const
@@ -918,6 +1034,18 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector&
       raw.mAttribs.push_back({"aVertexColor", propertyType, static_cast<uint32_t>(bufferSize / propertySize), std::move(buffer)});
     }
   }
+  else
+  {
+    std::vector<uint8_t> buffer(raw.mAttribs[0].mNumElements * sizeof(Vector4));
+    auto                 colors = reinterpret_cast<Vector4*>(buffer.data());
+
+    for(uint32_t i = 0; i < raw.mAttribs[0].mNumElements; i++)
+    {
+      colors[i] = Vector4::ONE;
+    }
+
+    raw.mAttribs.push_back({"aVertexColor", Property::VECTOR4, raw.mAttribs[0].mNumElements, std::move(buffer)});
+  }
 
   if(IsSkinned())
   {
@@ -936,20 +1064,20 @@ MeshDefinition::LoadRaw(const std::string& modelsPath, BufferDefinition::Vector&
       ReadJointAccessor<float>(raw, mJoints0, streamJoint, pathJoint);
     }
 
-    DALI_ASSERT_ALWAYS(((mWeights0.mBlob.mLength % sizeof(Vector4) == 0) ||
-                        mWeights0.mBlob.mStride >= sizeof(Vector4)) &&
-                       "Weights buffer length not a multiple of element size");
-    const auto           bufferSize = mWeights0.mBlob.GetBufferSize();
-    std::vector<uint8_t> buffer(bufferSize);
-
     std::string pathWeight;
     auto&       streamWeight = GetAvailableData(fileStream, meshPath, buffers[mWeights0.mBufferIdx], pathWeight);
-    if(!ReadAccessor(mWeights0, streamWeight, buffer.data()))
+    if(MaskMatch(mFlags, U16_WEIGHT))
     {
-      ExceptionFlinger(ASSERT_LOCATION) << "Failed to read weights from '" << pathWeight << "'.";
+      ReadWeightAccessor<uint16_t>(raw, mWeights0, streamWeight, pathWeight);
+    }
+    else if(MaskMatch(mFlags, U8_WEIGHT))
+    {
+      ReadWeightAccessor<uint8_t>(raw, mWeights0, streamWeight, pathWeight);
+    }
+    else
+    {
+      ReadWeightAccessor<float>(raw, mWeights0, streamWeight, pathWeight);
     }
-
-    raw.mAttribs.push_back({"aWeights", Property::VECTOR4, static_cast<uint32_t>(bufferSize / sizeof(Vector4)), std::move(buffer)});
   }
 
   // Calculate the Blob for the blend shapes.
@@ -1075,6 +1203,14 @@ MeshGeometry MeshDefinition::Load(RawData&& raw) const
   return meshGeometry;
 }
 
-} // namespace Loader
-} // namespace Scene3D
-} // namespace Dali
+void MeshDefinition::RetrieveBlendShapeComponents(bool& hasPositions, bool& hasNormals, bool& hasTangents) const
+{
+  for(const auto& blendShape : mBlendShapes)
+  {
+    hasPositions = hasPositions || blendShape.deltas.IsDefined();
+    hasNormals   = hasNormals || blendShape.normals.IsDefined();
+    hasTangents  = hasTangents || blendShape.tangents.IsDefined();
+  }
+}
+
+} // namespace Dali::Scene3D::Loader