#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>
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
{
{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));
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.
return mUseFrameBuffer;
}
-void SceneView::SetSkybox(const std::string& skyboxUrl)
+void SceneView::SetSkybox(const std::string& skyboxUrl, Scene3D::SceneView::SkyboxType skyboxType)
{
mSkyboxResourceReady = false;
if(mSkybox)
mSkybox.Unparent();
mSkybox.Reset();
}
- mSkybox = CreateSkybox(skyboxUrl);
+ mSkybox = CreateSkybox(skyboxUrl, skyboxType);
SetSkyboxIntensity(mSkyboxIntensity);
SetSkyboxOrientation(mSkyboxOrientation);
if(mRootLayer)
--- /dev/null
+// 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;
+}
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.
*
* 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.