[dali_2.3.22] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scene3d-view / gltf-loader.cpp
index 55dfd8d..b1cd70d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 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.
 
 // EXTERNAL INCLUDES
 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
+#include <dali/devel-api/actors/camera-actor-devel.h>
 #include <dali/devel-api/adaptor-framework/file-stream.h>
 #include <dali/devel-api/adaptor-framework/image-loading.h>
 #include <dali/integration-api/debug.h>
 
+// INTERNAL INCLUDES
+#include <dali-toolkit/internal/controls/scene3d-view/scene3d-view-impl.h>
+
 namespace Dali
 {
 namespace Toolkit
@@ -34,6 +38,8 @@ namespace Gltf
 {
 namespace
 {
+constexpr float MIN_DURATION_SECONDS = 1e-2f;
+
 // Utility functions
 const TreeNode* Tidx(const TreeNode* node, uint32_t index)
 {
@@ -231,7 +237,7 @@ void FitBuffer(Dali::Vector<Vector4>& bufferDestination, Dali::Vector<T>& buffer
 template<typename T>
 bool ReadBinFile(Vector<T>& dataBuffer, std::string url, int32_t offset, int32_t count)
 {
-  size_t readCount = 0;
+  size_t           readCount = 0;
   Dali::FileStream fileStream(url, FileStream::READ | FileStream::BINARY);
   FILE*            fp = fileStream.GetFile();
   if(fp)
@@ -392,10 +398,10 @@ void SetVertexBufferData(MeshInfo& meshInfo, std::string path, std::vector<Acces
   if(accessorIdx >= 0)
   {
     Dali::Vector<Vector3> bufferData;
-    LoadDataFromAccessor(accessorIdx, bufferData, path, accessorArray, bufferViewArray, bufferArray);
+    LoadDataFromAccessor(accessorIdx, bufferData, std::move(path), accessorArray, bufferViewArray, bufferArray);
     SetMeshInfoAndCanonize(meshInfo, bufferData);
 
-    VertexBuffer vertexBuffer = CreateVertexBuffer<Vector3>(bufferData, map, type);
+    VertexBuffer vertexBuffer = CreateVertexBuffer<Vector3>(bufferData, std::move(map), type);
     meshInfo.geometry.AddVertexBuffer(vertexBuffer);
   }
 }
@@ -406,9 +412,9 @@ void SetAttributeBufferData(MeshInfo& meshInfo, std::string path, std::vector<Ac
   if(accessorIdx >= 0)
   {
     Dali::Vector<T> bufferData;
-    LoadDataFromAccessor(accessorIdx, bufferData, path, accessorArray, bufferViewArray, bufferArray);
+    LoadDataFromAccessor(accessorIdx, bufferData, std::move(path), accessorArray, bufferViewArray, bufferArray);
 
-    VertexBuffer vertexBuffer = CreateVertexBuffer<T>(bufferData, map, type);
+    VertexBuffer vertexBuffer = CreateVertexBuffer<T>(bufferData, std::move(map), type);
     meshInfo.geometry.AddVertexBuffer(vertexBuffer);
   }
 }
@@ -416,7 +422,7 @@ void SetAttributeBufferData(MeshInfo& meshInfo, std::string path, std::vector<Ac
 void SetIndexBuffersData(MeshInfo& meshInfo, std::string path, std::vector<AccessorInfo>& accessorArray, std::vector<BufferViewInfo>& bufferViewArray, std::vector<BufferInfo>& bufferArray, int32_t indexIdx)
 {
   Dali::Vector<uint16_t> indexBufferData;
-  LoadDataFromAccessor(indexIdx, indexBufferData, path, accessorArray, bufferViewArray, bufferArray);
+  LoadDataFromAccessor(indexIdx, indexBufferData, std::move(path), accessorArray, bufferViewArray, bufferArray);
   meshInfo.geometry.SetIndexBuffer(&indexBufferData[0], indexBufferData.Size());
 }
 
@@ -430,7 +436,7 @@ float LoadKeyFrames(const AnimationSamplerInfo& currentSampler, const Property::
   LoadDataFromAccessor<T>(currentSampler.output, outputBufferData, path, accessorArray, bufferViewArray, bufferArray);
 
   uint32_t keyframeNum     = inputBufferData.Size();
-  float    lengthAnimation = inputBufferData[inputBufferData.Size() - 1];
+  float    lengthAnimation = std::max((inputBufferData.Size() > 0u ? inputBufferData[inputBufferData.Size() - 1] : MIN_DURATION_SECONDS), MIN_DURATION_SECONDS);
   for(uint32_t i = 0; i < keyframeNum; i++)
   {
     if(propIndex == Dali::Actor::Property::ORIENTATION)
@@ -1300,9 +1306,22 @@ void Loader::LoadCamera(Scene3dView& scene3dView)
     if(cameraInfo.type == "orthographic")
     {
       LoadOrthoGraphic((*cameraIter).second, cameraInfo);
-      float xMag_2 = cameraInfo.orthographic.xmag / 2.0;
-      float yMag_2 = cameraInfo.orthographic.ymag / 2.0;
-      cameraActor.SetOrthographicProjection(-xMag_2, xMag_2, yMag_2, -yMag_2, cameraInfo.orthographic.znear, cameraInfo.orthographic.zfar);
+      float       xMag_2 = cameraInfo.orthographic.xmag / 2.0;
+      float       yMag_2 = cameraInfo.orthographic.ymag / 2.0;
+      const float aspect = xMag_2 / yMag_2;
+
+      cameraActor.SetProjectionMode(Dali::Camera::ORTHOGRAPHIC_PROJECTION);
+      cameraActor.SetProperty(Dali::DevelCameraActor::Property::ORTHOGRAPHIC_SIZE, yMag_2);
+
+      cameraActor.SetNearClippingPlane(cameraInfo.orthographic.znear);
+      if(cameraInfo.orthographic.zfar > 0.0)
+      {
+        cameraActor.SetFarClippingPlane(cameraInfo.orthographic.zfar);
+      }
+      if(aspect > 0.0f) // Avoid divide-by-zero logic
+      {
+        cameraActor.SetAspectRatio(aspect);
+      }
     }
     else if(cameraInfo.type == "perspective")
     {
@@ -1572,7 +1591,9 @@ Actor Loader::AddNode(Scene3dView& scene3dView, uint32_t index)
     FRAGMENT_SHADER += SHADER_GLTF_PHYSICALLY_BASED_SHADER_FRAG.data();
     if(!mShaderCache[shaderTypeIndex])
     {
-      mShaderCache[shaderTypeIndex] = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER);
+      std::ostringstream oss;
+      oss << "GLTF_VIEW_" << shaderTypeIndex;
+      mShaderCache[shaderTypeIndex] = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER, Shader::Hint::NONE, oss.str());
       scene3dView.AddShader(mShaderCache[shaderTypeIndex]);
     }
     Shader shader = mShaderCache[shaderTypeIndex];
@@ -1593,7 +1614,7 @@ Actor Loader::AddNode(Scene3dView& scene3dView, uint32_t index)
     actor.SetProperty(Actor::Property::POSITION, translation);
 
     float hasLightSource = static_cast<float>(!!(scene3dView.GetLightType() & (Toolkit::Scene3dView::LightType::POINT_LIGHT | Toolkit::Scene3dView::LightType::DIRECTIONAL_LIGHT)));
-    float isPointLight = static_cast<float>(!!(scene3dView.GetLightType() & Toolkit::Scene3dView::LightType::POINT_LIGHT));
+    float isPointLight   = static_cast<float>(!!(scene3dView.GetLightType() & Toolkit::Scene3dView::LightType::POINT_LIGHT));
     shader.RegisterProperty("uHasLightSource", hasLightSource);
     shader.RegisterProperty("uIsPointLight", isPointLight);
     shader.RegisterProperty("uLightVector", scene3dView.GetLightVector());