Equirectangular projection support for Skybox in SceneView 60/285560/3
authorRichard <r.huang@samsung.com>
Wed, 14 Dec 2022 15:37:13 +0000 (15:37 +0000)
committerRichard <r.huang@samsung.com>
Wed, 14 Dec 2022 16:52:46 +0000 (16:52 +0000)
Change-Id: Ife95e49a7e5a59c2e44f874ccea1bc330ab88ef1

dali-scene3d/internal/controls/scene-view/scene-view-impl.cpp
dali-scene3d/internal/controls/scene-view/scene-view-impl.h
dali-scene3d/internal/graphics/shaders/skybox-equirectangular-shader.frag [new file with mode: 0644]
dali-scene3d/public-api/controls/scene-view/scene-view.cpp
dali-scene3d/public-api/controls/scene-view/scene-view.h

index e8fb48ee5a93445644a37c870c9ab66e7cbda4fd..f4d549823e21efb642883ba293c763590381e969 100644 (file)
@@ -25,6 +25,7 @@
 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
 #include <dali-toolkit/public-api/image-loader/image-url.h>
 #include <dali-toolkit/public-api/image-loader/image.h>
+#include <dali-toolkit/public-api/image-loader/sync-image-loader.h>
 #include <dali/devel-api/actors/camera-actor-devel.h>
 #include <dali/devel-api/adaptor-framework/window-devel.h>
 #include <dali/devel-api/common/stage.h>
@@ -65,7 +66,7 @@ constexpr uint8_t DEFAULT_FRAME_BUFFER_MULTI_SAMPLING_LEVEL = 4u;
 
 static constexpr std::string_view SKYBOX_INTENSITY_STRING = "uIntensity";
 
-Dali::Actor CreateSkybox(const std::string& skyboxUrl)
+Dali::Actor CreateSkybox(const std::string& skyboxUrl, Scene3D::SceneView::SkyboxType skyboxType)
 {
   struct Vertex
   {
@@ -121,7 +122,6 @@ Dali::Actor CreateSkybox(const std::string& skyboxUrl)
     {Vector3(-1.0f, -1.0f, 1.0f)},
     {Vector3(1.0f, -1.0f, 1.0f)}};
 
-  Dali::Shader       shaderSkybox = Shader::New(SHADER_SKYBOX_SHADER_VERT.data(), SHADER_SKYBOX_SHADER_FRAG.data());
   Dali::VertexBuffer vertexBuffer = Dali::VertexBuffer::New(Property::Map().Add("aPosition", Property::VECTOR3));
   vertexBuffer.SetData(skyboxVertices, sizeof(skyboxVertices) / sizeof(Vertex));
 
@@ -129,11 +129,29 @@ Dali::Actor CreateSkybox(const std::string& skyboxUrl)
   skyboxGeometry.AddVertexBuffer(vertexBuffer);
   skyboxGeometry.SetType(Geometry::TRIANGLES);
 
-  Dali::Texture    skyboxTexture  = Dali::Scene3D::Loader::LoadCubeMap(skyboxUrl);
+  Dali::Texture  skyboxTexture;
+  Dali::Shader   shaderSkybox;
+  Dali::Renderer skyboxRenderer;
+
+  if(skyboxType == Scene3D::SceneView::SkyboxType::CUBEMAP)
+  {
+    skyboxTexture = Dali::Scene3D::Loader::LoadCubeMap(skyboxUrl);
+    shaderSkybox  = Shader::New(SHADER_SKYBOX_SHADER_VERT.data(), SHADER_SKYBOX_SHADER_FRAG.data());
+  }
+  else // Scene3D::SceneView::SkyboxType::EQUIRECTANGULAR
+  {
+    // Load image from file
+    PixelData pixels = Dali::Toolkit::SyncImageLoader::Load(skyboxUrl);
+
+    skyboxTexture = Texture::New(TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight());
+    skyboxTexture.Upload(pixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight());
+    shaderSkybox = Shader::New(SHADER_SKYBOX_SHADER_VERT.data(), SHADER_SKYBOX_EQUIRECTANGULAR_SHADER_FRAG.data());
+  }
+
   Dali::TextureSet skyboxTextures = TextureSet::New();
   skyboxTextures.SetTexture(0, skyboxTexture);
 
-  Dali::Renderer skyboxRenderer = Renderer::New(skyboxGeometry, shaderSkybox);
+  skyboxRenderer = Renderer::New(skyboxGeometry, shaderSkybox);
   skyboxRenderer.SetTextures(skyboxTextures);
   skyboxRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, 2.0f);
   // Enables the depth test.
@@ -338,7 +356,7 @@ bool SceneView::IsUsingFramebuffer() const
   return mUseFrameBuffer;
 }
 
-void SceneView::SetSkybox(const std::string& skyboxUrl)
+void SceneView::SetSkybox(const std::string& skyboxUrl, Scene3D::SceneView::SkyboxType skyboxType)
 {
   mSkyboxResourceReady = false;
   if(mSkybox)
@@ -346,7 +364,7 @@ void SceneView::SetSkybox(const std::string& skyboxUrl)
     mSkybox.Unparent();
     mSkybox.Reset();
   }
-  mSkybox = CreateSkybox(skyboxUrl);
+  mSkybox = CreateSkybox(skyboxUrl, skyboxType);
   SetSkyboxIntensity(mSkyboxIntensity);
   SetSkyboxOrientation(mSkyboxOrientation);
   if(mRootLayer)
index f5e4f8256e17adfe0085a44322e6122efa1f33a3..def2b83723dcbc65a041a01461d0017c6ad4dfa6 100644 (file)
@@ -140,7 +140,7 @@ public:
   /**
    * @copydoc SceneView::SetSkybox()
    */
-  void SetSkybox(const std::string& skyboxUrl);
+  void SetSkybox(const std::string& skyboxUrl, Scene3D::SceneView::SkyboxType skyboxType);
 
   /**
    * @copydoc SceneView::SetSkyboxIntensity()
diff --git a/dali-scene3d/internal/graphics/shaders/skybox-equirectangular-shader.frag b/dali-scene3d/internal/graphics/shaders/skybox-equirectangular-shader.frag
new file mode 100644 (file)
index 0000000..c57c608
--- /dev/null
@@ -0,0 +1,38 @@
+// Fragment shader for a skybox in equirectangular projection
+precision mediump float;
+
+uniform sampler2D uSkyBoxEquirectangularTexture;
+
+uniform vec4  uColor;
+uniform float uIntensity;
+
+varying vec3 vTexCoord;
+
+// Take the sample direction as interpolated from the cube's local position,
+// and use this direction vector and the spherical to cartesian coordinate
+// conversion to sample the equirectangular map as if it's a cube map.
+
+const float M_1_PI = 0.3183; // The reciprocal of pi in radians
+const float M_1_2PI = 0.1591; // The reciprocal of 2*pi in radians
+
+const vec2 inverseAtan = vec2(M_1_2PI, M_1_PI);
+
+vec2 SampleEquirectangularMapAsCubeMap(vec3 v)
+{
+    vec2 uv = vec2(atan(v.z, v.x), asin(v.y));
+    uv *= inverseAtan;
+    uv += 0.5;
+    return uv;
+}
+
+void main()
+{
+  // Project the equirectangular map to a cube's faces
+  vec2 uv = SampleEquirectangularMapAsCubeMap(normalize(vTexCoord));
+
+  // Flip the texture UVs vertically
+  vec2 uvFlippped = vec2(uv.x, 1.0 - uv.y);
+
+  vec4 texColor = texture2D( uSkyBoxEquirectangularTexture, uvFlippped ) * uIntensity;
+  gl_FragColor = texColor * uColor;
+}
index bc41050f4a9e636a36cdc5c025d3a0416951abfb..732c1d6be5884c669a9c5d5b8b2f0638d99c64b2 100644 (file)
@@ -127,9 +127,9 @@ bool SceneView::IsUsingFramebuffer() const
   return GetImpl(*this).IsUsingFramebuffer();
 }
 
-void SceneView::SetSkybox(const std::string& skyboxUrl)
+void SceneView::SetSkybox(const std::string& skyboxUrl, SkyboxType skyboxType)
 {
-  GetImpl(*this).SetSkybox(skyboxUrl);
+  GetImpl(*this).SetSkybox(skyboxUrl, skyboxType);
 }
 
 void SceneView::SetSkyboxIntensity(float intensity)
index 9c72d9ce0b12d5ba8aae9b912abae469ae3871ba..7c4f67b1fae328b3a7085d2fd5987aea6b1873db 100644 (file)
@@ -103,6 +103,16 @@ class SceneView;
 class DALI_SCENE3D_API SceneView : public Dali::Toolkit::Control
 {
 public:
+  /**
+   * @brief The skybox types
+   * @SINCE_2_2.6
+   */
+  enum class SkyboxType
+  {
+    CUBEMAP,        ///< Skybox in cubemap
+    EQUIRECTANGULAR ///< Skybox in equirectangular projection
+  };
+
   /**
    * @brief Create an initialized SceneView.
    *
@@ -320,9 +330,10 @@ public:
    * Skybox texture is asynchronously loaded. When loading is finished, ResourceReady is emitted.
    *
    * @SINCE_2_2.0
-   * @param[in] skyboxUrl Cube map image url for skybox.
+   * @param[in] skyboxUrl image url for skybox.
+   * @param[in] skyboxType The skybox type (by default it is cubemap).
    */
-  void SetSkybox(const std::string& skyboxUrl);
+  void SetSkybox(const std::string& skyboxUrl, SkyboxType skyboxType = SkyboxType::CUBEMAP);
 
   /**
    * @brief Sets Skybox intensity.