/*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 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.
#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>
#include <dali/devel-api/rendering/frame-buffer-devel.h>
+#include <dali/integration-api/adaptor-framework/adaptor.h>
#include <dali/integration-api/debug.h>
+#include <dali/public-api/math/math-utils.h>
#include <dali/public-api/object/type-registry-helper.h>
#include <dali/public-api/object/type-registry.h>
#include <string_view>
// INTERNAL INCLUDES
#include <dali-scene3d/internal/controls/model/model-impl.h>
#include <dali-scene3d/internal/graphics/builtin-shader-extern-gen.h>
-#include <dali-scene3d/public-api/loader/cube-map-loader.h>
+#include <dali-scene3d/internal/light/light-impl.h>
+
+#include <dali/integration-api/debug.h>
using namespace Dali;
DALI_TYPE_REGISTRATION_BEGIN(Scene3D::SceneView, Toolkit::Control, Create);
DALI_TYPE_REGISTRATION_END()
-Property::Index RENDERING_BUFFER = Dali::Toolkit::Control::CONTROL_PROPERTY_END_INDEX + 1;
-constexpr int32_t DEFAULT_ORIENTATION = 0;
-
-constexpr uint8_t DEFAULT_FRAME_BUFFER_MULTI_SAMPLING_LEVEL = 4u;
+Property::Index RENDERING_BUFFER = Dali::Toolkit::Control::CONTROL_PROPERTY_END_INDEX + 1;
+constexpr int32_t DEFAULT_ORIENTATION = 0;
+constexpr int32_t INVALID_INDEX = -1;
+constexpr uint32_t MAXIMUM_SIZE_SHADOW_MAP = 2048;
static constexpr std::string_view SKYBOX_INTENSITY_STRING = "uIntensity";
-Dali::Actor CreateSkybox(const std::string& skyboxUrl, Scene3D::SceneView::SkyboxType skyboxType)
+Dali::Actor CreateSkybox()
{
struct Vertex
{
skyboxGeometry.AddVertexBuffer(vertexBuffer);
skyboxGeometry.SetType(Geometry::TRIANGLES);
- Dali::Texture skyboxTexture;
- Dali::Shader shaderSkybox;
+ Dali::Shader shaderSkybox = Shader::New(SHADER_SKYBOX_SHADER_VERT.data(), SHADER_SKYBOX_SHADER_FRAG.data());
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);
-
skyboxRenderer = Renderer::New(skyboxGeometry, shaderSkybox);
- skyboxRenderer.SetTextures(skyboxTextures);
skyboxRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, 2.0f);
// Enables the depth test.
skyboxRenderer.SetProperty(Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON);
return skyboxActor;
}
+void SetShadowLightConstraint(Dali::CameraActor selectedCamera, Dali::CameraActor shadowLightCamera)
+{
+ shadowLightCamera.SetProperty(Dali::CameraActor::Property::ASPECT_RATIO, 1.0f);
+
+ shadowLightCamera.SetProperty(Dali::DevelCameraActor::Property::ORTHOGRAPHIC_SIZE, 1.0f);
+ shadowLightCamera.SetProperty(Dali::CameraActor::Property::NEAR_PLANE_DISTANCE, 0.5f);
+ shadowLightCamera.SetProperty(Dali::CameraActor::Property::FAR_PLANE_DISTANCE, 3.5f);
+
+ //< Make constraint for above properties.
+ shadowLightCamera.RemoveConstraints();
+
+ // Compute View Matrix of ShadowLightCamera
+ // Input : ShadowLightCamera's world position, world orientation
+ auto tempViewMatrixIndex = shadowLightCamera.RegisterProperty("tempViewMatrix", Matrix::IDENTITY);
+ Constraint viewMatrixConstraint = Constraint::New<Matrix>(shadowLightCamera, tempViewMatrixIndex, [](Matrix& output, const PropertyInputContainer& inputs)
+ {
+ output = inputs[0]->GetMatrix();
+ output.Invert(); });
+ viewMatrixConstraint.AddSource(Source{shadowLightCamera, Dali::Actor::Property::WORLD_MATRIX});
+ viewMatrixConstraint.ApplyPost();
+
+ // Compute Orthographic Size / Near / Far and store it to "TempCameraProperty" property that is a Vector3 property
+ auto tempProjectionMatrixIndex = shadowLightCamera.RegisterProperty("tempProjectionMatrix", Matrix::IDENTITY);
+ Constraint projectionMatrixConstraint = Constraint::New<Matrix>(shadowLightCamera, tempProjectionMatrixIndex, [](Matrix& output, const PropertyInputContainer& inputs)
+ {
+ Matrix worldMatrix = inputs[0]->GetMatrix();
+ float tangentFov_2 = tanf(inputs[4]->GetFloat());
+ float nearDistance = inputs[5]->GetFloat();
+ float farDistance = inputs[6]->GetFloat();
+ float aspectRatio = inputs[7]->GetFloat();
+ float nearY = 0.0f;
+ float nearX = 0.0f;
+ float farY = 0.0f;
+ float farX = 0.0f;
+ if(inputs[1]->GetInteger() == Dali::Camera::ProjectionMode::PERSPECTIVE_PROJECTION)
+ {
+ if(inputs[2]->GetInteger() == Dali::DevelCameraActor::ProjectionDirection::VERTICAL)
+ {
+ nearY = tangentFov_2 * nearDistance;
+ nearX = nearY * aspectRatio;
+ farY = tangentFov_2 * farDistance;
+ farX = farY * aspectRatio;
+ }
+ else
+ {
+ nearX = tangentFov_2 * nearDistance;
+ nearY = nearX / aspectRatio;
+ farX = tangentFov_2 * farDistance;
+ farY = farX / aspectRatio;
+ }
+ }
+ else
+ {
+ if(inputs[2]->GetInteger() == Dali::DevelCameraActor::ProjectionDirection::VERTICAL)
+ {
+ nearY = inputs[3]->GetFloat();
+ nearX = nearY * aspectRatio;
+ }
+ else
+ {
+ nearX = inputs[3]->GetFloat();
+ nearY = nearX / aspectRatio;
+ }
+ farX = nearX;
+ farY = nearY;
+ }
+
+ std::vector<Vector4> points;
+ points.push_back(Vector4(nearX, nearY, nearDistance, 1.0f));
+ points.push_back(Vector4(-nearX, nearY, nearDistance, 1.0f));
+ points.push_back(Vector4(-nearX, -nearY, nearDistance, 1.0f));
+ points.push_back(Vector4(nearX, -nearY, nearDistance, 1.0f));
+ points.push_back(Vector4(farX, farY, farDistance, 1.0f));
+ points.push_back(Vector4(-farX, farY, farDistance, 1.0f));
+ points.push_back(Vector4(-farX, -farY, farDistance, 1.0f));
+ points.push_back(Vector4(farX, -farY, farDistance, 1.0f));
+
+ Vector3 areaMin = Vector3::ONE * MAXFLOAT, areaMax = Vector3::ONE * -MAXFLOAT;
+ for(auto&& point : points)
+ {
+ Vector4 pointW = worldMatrix * point;
+ Vector4 pointV = inputs[8]->GetMatrix() * pointW;
+ areaMin.x = std::min(areaMin.x, pointV.x);
+ areaMin.y = std::min(areaMin.y, pointV.y);
+ areaMin.z = std::min(areaMin.z, pointV.z);
+ areaMax.x = std::max(areaMax.x, pointV.x);
+ areaMax.y = std::max(areaMax.y, pointV.y);
+ areaMax.z = std::max(areaMax.z, pointV.z);
+ }
+
+ Vector2 center = Vector2(areaMax + areaMin) / 2.0f;
+ float delta = std::max(std::abs(areaMax.x - areaMin.x), std::abs(areaMax.y - areaMin.y));
+ float delta_2 = delta * 0.5f;
+ Vector2 squareAreaMin = center - Vector2::ONE * delta_2;
+ Vector2 squareAreaMax = center + Vector2::ONE * delta_2;
+ float deltaZ = areaMax.z - areaMin.z;
+
+ float right = -squareAreaMin.x;
+ float left = -squareAreaMax.x;
+ float top = squareAreaMin.y;
+ float bottom = squareAreaMax.y;
+ float near = areaMin.z;
+ float far = areaMax.z;
+
+ float* projMatrix = output.AsFloat();
+
+ projMatrix[0] = -2.0f / delta;
+ projMatrix[1] = 0.0f;
+ projMatrix[2] = 0.0f;
+ projMatrix[3] = 0.0f;
+
+ projMatrix[4] = 0.0f;
+ projMatrix[5] = -2.0f / delta;
+ projMatrix[6] = 0.0f;
+ projMatrix[7] = 0.0f;
+
+ projMatrix[8] = 0.0f;
+ projMatrix[9] = 0.0f;
+ projMatrix[10] = 2.0f / deltaZ;
+ projMatrix[11] = 0.0f;
+
+ projMatrix[12] = -(right + left) / delta;
+ projMatrix[13] = -(top + bottom) / delta;
+ projMatrix[14] = -(near + far) / deltaZ;
+ projMatrix[15] = 1.0f; });
+ projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::Actor::Property::WORLD_MATRIX});
+ projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::CameraActor::Property::PROJECTION_MODE});
+ projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::DevelCameraActor::Property::PROJECTION_DIRECTION});
+ projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::DevelCameraActor::Property::ORTHOGRAPHIC_SIZE});
+ projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::CameraActor::Property::FIELD_OF_VIEW});
+ projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::CameraActor::Property::NEAR_PLANE_DISTANCE});
+ projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::CameraActor::Property::FAR_PLANE_DISTANCE});
+ projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::CameraActor::Property::ASPECT_RATIO});
+ projectionMatrixConstraint.AddSource(Source{shadowLightCamera, tempViewMatrixIndex});
+ projectionMatrixConstraint.ApplyPost();
+}
+
} // anonymous namespace
SceneView::SceneView()
mWindowOrientation(DEFAULT_ORIENTATION),
mSkybox(),
mSkyboxOrientation(Quaternion()),
- mSkyboxIntensity(1.0f)
+ mSkyboxIntensity(1.0f),
+ mShaderManager(new Scene3D::Loader::ShaderManager())
{
}
-SceneView::~SceneView() = default;
+SceneView::~SceneView()
+{
+ if(Dali::Adaptor::IsAvailable())
+ {
+ if(mIblDiffuseLoadTask)
+ {
+ Dali::AsyncTaskManager::Get().RemoveTask(mIblDiffuseLoadTask);
+ mIblDiffuseLoadTask.Reset();
+ }
+ if(mIblSpecularLoadTask)
+ {
+ Dali::AsyncTaskManager::Get().RemoveTask(mIblSpecularLoadTask);
+ mIblSpecularLoadTask.Reset();
+ }
+ if(mSkyboxLoadTask)
+ {
+ Dali::AsyncTaskManager::Get().RemoveTask(mSkyboxLoadTask);
+ mSkyboxLoadTask.Reset();
+ }
+ }
+}
Dali::Scene3D::SceneView SceneView::New()
{
UpdateCamera(GetCamera(name));
}
-void SceneView::RegisterSceneItem(Scene3D::Internal::ImageBasedLightObserver* item)
+void SceneView::RegisterSceneItem(Scene3D::Internal::LightObserver* item)
{
if(item)
{
- item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
+ item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor, mSpecularMipmapLevels);
mItems.push_back(item);
}
}
-void SceneView::UnregisterSceneItem(Scene3D::Internal::ImageBasedLightObserver* item)
+void SceneView::UnregisterSceneItem(Scene3D::Internal::LightObserver* item)
{
if(item)
{
void SceneView::SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor)
{
- mIBLResourceReady = false;
+ bool needIblReset = false;
+ bool isOnScene = Self().GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE);
+ if(mDiffuseIblUrl != diffuseUrl)
+ {
+ mDiffuseIblUrl = diffuseUrl;
+ if(mDiffuseIblUrl.empty())
+ {
+ needIblReset = true;
+ }
+ else
+ {
+ mIblDiffuseDirty = true;
+ mIblDiffuseResourceReady = false;
+ }
+ }
+
+ if(mSpecularIblUrl != specularUrl)
+ {
+ mSpecularIblUrl = specularUrl;
+ if(mSpecularIblUrl.empty())
+ {
+ needIblReset = true;
+ }
+ else
+ {
+ mIblSpecularDirty = true;
+ mIblSpecularResourceReady = false;
+ }
+ }
+
+ // If one or both of diffuse url and specular url are empty,
+ // we don't need to request to load texture.
+ if(needIblReset)
+ {
+ if(mIblDiffuseLoadTask)
+ {
+ Dali::AsyncTaskManager::Get().RemoveTask(mIblDiffuseLoadTask);
+ mIblDiffuseLoadTask.Reset();
+ }
+
+ if(mIblSpecularLoadTask)
+ {
+ Dali::AsyncTaskManager::Get().RemoveTask(mIblSpecularLoadTask);
+ mIblSpecularLoadTask.Reset();
+ }
- // If url is empty or invalid, reset IBL.
- mDiffuseTexture = (!diffuseUrl.empty()) ? Dali::Scene3D::Loader::LoadCubeMap(diffuseUrl) : Texture();
- mSpecularTexture = (!specularUrl.empty()) ? Dali::Scene3D::Loader::LoadCubeMap(specularUrl) : Texture();
+ mIblDiffuseDirty = false;
+ mIblSpecularDirty = false;
+ mIblDiffuseResourceReady = true;
+ mIblSpecularResourceReady = true;
- mIblScaleFactor = scaleFactor;
+ mDiffuseTexture.Reset();
+ mSpecularTexture.Reset();
- for(auto&& item : mItems)
+ mSpecularMipmapLevels = 1u;
+ NotifyImageBasedLightTextureChange();
+ }
+ else
{
- if(item)
+ if(isOnScene && mIblDiffuseDirty)
+ {
+ if(mIblDiffuseLoadTask)
+ {
+ Dali::AsyncTaskManager::Get().RemoveTask(mIblDiffuseLoadTask);
+ mIblDiffuseLoadTask.Reset();
+ }
+ mIblDiffuseLoadTask = new EnvironmentMapLoadTask(mDiffuseIblUrl, Scene3D::EnvironmentMapType::CUBEMAP, MakeCallback(this, &SceneView::OnIblDiffuseLoadComplete));
+ Dali::AsyncTaskManager::Get().AddTask(mIblDiffuseLoadTask);
+ mIblDiffuseDirty = false;
+ }
+
+ if(isOnScene && mIblSpecularDirty)
{
- item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
+ if(mIblSpecularLoadTask)
+ {
+ Dali::AsyncTaskManager::Get().RemoveTask(mIblSpecularLoadTask);
+ mIblSpecularLoadTask.Reset();
+ }
+ mIblSpecularLoadTask = new EnvironmentMapLoadTask(mSpecularIblUrl, Scene3D::EnvironmentMapType::CUBEMAP, MakeCallback(this, &SceneView::OnIblSpecularLoadComplete));
+ Dali::AsyncTaskManager::Get().AddTask(mIblSpecularLoadTask);
+ mIblSpecularDirty = false;
}
}
- mIBLResourceReady = true;
+ if(!Dali::Equals(mIblScaleFactor, scaleFactor))
+ {
+ SetImageBasedLightScaleFactor(scaleFactor);
+ }
+
+ // If diffuse and specular textures are already loaded, emits resource ready signal here.
if(IsResourceReady())
{
- Control::SetResourceReady(false);
+ Control::SetResourceReady();
}
}
return mIblScaleFactor;
}
+void SceneView::AddLight(Scene3D::Light light)
+{
+ bool enabled = mShaderManager->AddLight(light);
+ mLights.push_back(std::make_pair(light, enabled));
+
+ if(light.IsShadowEnabled())
+ {
+ SetShadow(light);
+ }
+}
+
+void SceneView::RemoveLight(Scene3D::Light light)
+{
+ mShaderManager->RemoveLight(light);
+ for(uint32_t i = 0; i < mLights.size(); ++i)
+ {
+ if(mLights[i].first == light)
+ {
+ mLights.erase(mLights.begin() + i);
+ break;
+ }
+ }
+
+ if(mLights.size() > mShaderManager->GetLightCount())
+ {
+ for(auto&& waitingLight : mLights)
+ {
+ if(waitingLight.second)
+ {
+ continue;
+ }
+
+ waitingLight.second = mShaderManager->AddLight(waitingLight.first);
+ break;
+ }
+ }
+
+ if(light == mShadowLight)
+ {
+ RemoveShadow(light);
+ }
+
+ if(!mShadowLight)
+ {
+ for(auto&& lightEntity : mLights)
+ {
+ if(!lightEntity.second || !lightEntity.first.IsShadowEnabled())
+ {
+ continue;
+ }
+ SetShadow(lightEntity.first);
+ break;
+ }
+ }
+}
+
+void SceneView::SetShadow(Scene3D::Light light)
+{
+ if(!!mShadowLight)
+ {
+ return;
+ }
+
+ auto foundLight = std::find_if(mLights.begin(), mLights.end(), [light](std::pair<Scene3D::Light, bool> lightEntity) -> bool
+ { return (lightEntity.second && lightEntity.first == light); });
+
+ if(foundLight == mLights.end())
+ {
+ return;
+ }
+
+ mShadowLight = light;
+
+ // Directional Light setting.
+ CameraActor lightCamera = GetImplementation(light).GetCamera();
+ CameraActor selectedCamera = GetSelectedCamera();
+ SetShadowLightConstraint(selectedCamera, lightCamera);
+
+ // make framebuffer for depth map and set it to render task.
+ Vector3 size = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
+ uint32_t shadowMapBufferSize = std::min(static_cast<uint32_t>(std::max(size.width, size.height)), MAXIMUM_SIZE_SHADOW_MAP);
+ UpdateShadowMapBuffer(shadowMapBufferSize);
+
+ // use lightCamera as a camera of shadow render task.
+ mShadowMapRenderTask.SetCameraActor(lightCamera);
+
+ mShaderManager->SetShadow(light);
+}
+
+void SceneView::RemoveShadow(Scene3D::Light light)
+{
+ if(mShadowLight != light)
+ {
+ return;
+ }
+
+ // remove all constraint from light camera
+ CameraActor lightCamera = GetImplementation(mShadowLight).GetCamera();
+ lightCamera.RemoveConstraints();
+
+ // reset framebuffer and remove it from render task.
+ mShadowFrameBuffer.Reset();
+ mShaderManager->RemoveShadow();
+ mShadowMapRenderTask.SetCameraActor(CameraActor());
+
+ mShadowLight.Reset();
+
+ for(auto&& lightEntity : mLights)
+ {
+ if(!lightEntity.second || !lightEntity.first.IsShadowEnabled())
+ {
+ continue;
+ }
+ SetShadow(lightEntity.first);
+ break;
+ }
+}
+
+uint32_t SceneView::GetActivatedLightCount() const
+{
+ return mShaderManager->GetLightCount();
+}
+
void SceneView::UseFramebuffer(bool useFramebuffer)
{
if(mUseFrameBuffer != useFramebuffer)
return mUseFrameBuffer;
}
-void SceneView::SetSkybox(const std::string& skyboxUrl, Scene3D::SceneView::SkyboxType skyboxType)
+void SceneView::SetFramebufferMultiSamplingLevel(uint8_t multiSamplingLevel)
{
- mSkyboxResourceReady = false;
- if(mSkybox)
+ if(mFrameBufferMultiSamplingLevel != multiSamplingLevel)
{
- mSkybox.Unparent();
- mSkybox.Reset();
+ mFrameBufferMultiSamplingLevel = multiSamplingLevel;
+
+ // Create new framebuffer with changed multiSamplingLevel.
+ if(mRenderTask && mFrameBuffer && mTexture)
+ {
+ Vector3 size = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
+
+ mFrameBuffer = FrameBuffer::New(size.width, size.height, FrameBuffer::Attachment::DEPTH_STENCIL);
+ mFrameBuffer.AttachColorTexture(mTexture);
+ DevelFrameBuffer::SetMultiSamplingLevel(mFrameBuffer, mFrameBufferMultiSamplingLevel);
+ mRenderTask.SetFrameBuffer(mFrameBuffer);
+
+ // Note : we don't need to create new visual since visual's url is depend on mTexture.
+ }
}
- mSkybox = CreateSkybox(skyboxUrl, skyboxType);
- SetSkyboxIntensity(mSkyboxIntensity);
- SetSkyboxOrientation(mSkyboxOrientation);
- if(mRootLayer)
+}
+
+uint8_t SceneView::GetFramebufferMultiSamplingLevel() const
+{
+ return mFrameBufferMultiSamplingLevel;
+}
+
+void SceneView::SetSkybox(const std::string& skyboxUrl)
+{
+ if(mSkyboxUrl != skyboxUrl)
{
- mRootLayer.Add(mSkybox);
+ UpdateSkybox(skyboxUrl, mSkyboxEnvironmentMapType);
}
+}
- mSkyboxResourceReady = true;
- if(IsResourceReady())
+void SceneView::SetSkyboxEnvironmentMapType(Scene3D::EnvironmentMapType skyboxEnvironmentMapType)
+{
+ if(mSkyboxEnvironmentMapType != skyboxEnvironmentMapType)
{
- Control::SetResourceReady(false);
+ UpdateSkybox(mSkyboxUrl, skyboxEnvironmentMapType);
}
}
return mSkyboxOrientation;
}
+Dali::Scene3D::Loader::ShaderManagerPtr SceneView::GetShaderManager() const
+{
+ return mShaderManager;
+}
+
+void SceneView::UpdateShadowUniform(Scene3D::Light light)
+{
+ mShaderManager->UpdateShadowUniform(light);
+}
+
///////////////////////////////////////////////////////////
//
// Private methods
void SceneView::OnSceneConnection(int depth)
{
+ // If diffuse and specular url is not valid, IBL does not need to be loaded.
+ if(!mDiffuseIblUrl.empty() && !mSpecularIblUrl.empty())
+ {
+ SetImageBasedLightSource(mDiffuseIblUrl, mSpecularIblUrl, mIblScaleFactor);
+ }
+
+ if(!mSkyboxUrl.empty())
+ {
+ UpdateSkybox(mSkyboxUrl, mSkyboxEnvironmentMapType);
+ }
+
Window window = DevelWindow::Get(Self());
if(window)
{
+ // Only for on-screen window
window.ResizeSignal().Connect(this, &SceneView::OnWindowResized);
- RenderTaskList taskList = window.GetRenderTaskList();
- mRenderTask = taskList.CreateTask();
+
+ mWindow = window;
+ mWindowOrientation = DevelWindow::GetPhysicalOrientation(window);
+ }
+
+ // On-screen / Off-screen window
+ mSceneHolder = Integration::SceneHolder::Get(Self());
+ if(mSceneHolder)
+ {
+ RenderTaskList taskList = mSceneHolder.GetRenderTaskList();
+ mShadowMapRenderTask = taskList.CreateTask();
+ mShadowMapRenderTask.SetSourceActor(mRootLayer);
+ mShadowMapRenderTask.SetExclusive(true);
+ mShadowMapRenderTask.SetInputEnabled(false);
+ mShadowMapRenderTask.SetCullMode(false);
+ mShadowMapRenderTask.SetClearEnabled(true);
+ mShadowMapRenderTask.SetClearColor(Color::WHITE);
+ mShadowMapRenderTask.SetRenderPassTag(10);
+ mShadowMapRenderTask.SetCameraActor(CameraActor());
+
+ mRenderTask = taskList.CreateTask();
mRenderTask.SetSourceActor(mRootLayer);
mRenderTask.SetExclusive(true);
mRenderTask.SetInputEnabled(true);
mRenderTask.SetScreenToFrameBufferMappingActor(Self());
UpdateRenderTask();
- mWindow = window;
}
Control::OnSceneConnection(depth);
if(window)
{
window.ResizeSignal().Disconnect(this, &SceneView::OnWindowResized);
- RenderTaskList taskList = window.GetRenderTaskList();
+ }
+ mWindow.Reset();
+
+ if(mSceneHolder)
+ {
if(mRenderTask)
{
+ RenderTaskList taskList = mSceneHolder.GetRenderTaskList();
taskList.RemoveTask(mRenderTask);
- mFrameBuffer.Reset();
+ mRenderTask.Reset();
+ }
+ if(mShadowMapRenderTask)
+ {
+ RenderTaskList taskList = mSceneHolder.GetRenderTaskList();
+ taskList.RemoveTask(mShadowMapRenderTask);
+ mShadowMapRenderTask.Reset();
}
+ mSceneHolder.Reset();
}
- mWindow.Reset();
+ mFrameBuffer.Reset();
+ mShadowFrameBuffer.Reset();
Control::OnSceneDisconnection();
}
mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_SCALE, false);
self.Add(mRootLayer);
- mDefaultCamera = Dali::CameraActor::New();
+ mDefaultCamera = Dali::CameraActor::New3DCamera();
mDefaultCamera.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
mDefaultCamera.SetProperty(Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
- mDefaultCamera.SetNearClippingPlane(1.0f);
AddCamera(mDefaultCamera);
UpdateCamera(mDefaultCamera);
}
bool SceneView::IsResourceReady() const
{
- return mIBLResourceReady & mSkyboxResourceReady;
+ return mIblDiffuseResourceReady && mIblSpecularResourceReady && mSkyboxResourceReady;
}
void SceneView::UpdateCamera(CameraActor camera)
}
mSelectedCamera = camera;
+ if(mShadowLight)
+ {
+ SetShadowLightConstraint(mSelectedCamera, GetImplementation(mShadowLight).GetCamera());
+ }
UpdateRenderTask();
}
const float aspectRatio = size.width / size.height;
mSelectedCamera.SetAspectRatio(aspectRatio);
+ uint32_t shadowMapBufferSize = std::min(static_cast<uint32_t>(std::max(size.width, size.height)), MAXIMUM_SIZE_SHADOW_MAP);
+ UpdateShadowMapBuffer(shadowMapBufferSize);
+
if(mUseFrameBuffer)
{
Dali::FrameBuffer currentFrameBuffer = mRenderTask.GetFrameBuffer();
if(!currentFrameBuffer ||
- currentFrameBuffer.GetColorTexture().GetWidth() != size.width ||
- currentFrameBuffer.GetColorTexture().GetHeight() != size.height)
+ !Dali::Equals(currentFrameBuffer.GetColorTexture().GetWidth(), size.width) ||
+ !Dali::Equals(currentFrameBuffer.GetColorTexture().GetHeight(), size.height))
{
+ mRootLayer.SetProperty(Dali::Actor::Property::COLOR_MODE, ColorMode::USE_OWN_COLOR);
mRenderTask.ResetViewportGuideActor();
mRenderTask.SetViewport(Dali::Viewport(Vector4::ZERO));
mTexture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, unsigned(size.width), unsigned(size.height));
mFrameBuffer = FrameBuffer::New(size.width, size.height, FrameBuffer::Attachment::DEPTH_STENCIL);
mFrameBuffer.AttachColorTexture(mTexture);
- DevelFrameBuffer::SetMultiSamplingLevel(mFrameBuffer, DEFAULT_FRAME_BUFFER_MULTI_SAMPLING_LEVEL);
+ DevelFrameBuffer::SetMultiSamplingLevel(mFrameBuffer, mFrameBufferMultiSamplingLevel);
Dali::Toolkit::ImageUrl imageUrl = Dali::Toolkit::Image::GenerateUrl(mFrameBuffer, 0u);
Property::Map imagePropertyMap;
imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, imageUrl.GetUrl());
- // To make sure this visual call LoadTexture API immediate.
- imagePropertyMap.Insert(Toolkit::ImageVisual::Property::LOAD_POLICY, Toolkit::ImageVisual::LoadPolicy::IMMEDIATE);
- imagePropertyMap.Insert(Toolkit::ImageVisual::Property::RELEASE_POLICY, Toolkit::ImageVisual::ReleasePolicy::DESTROYED);
// To flip rendered scene without CameraActor::SetInvertYAxis() to avoid backface culling.
imagePropertyMap.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, Vector4(0.0f, 1.0f, 1.0f, -1.0f));
mVisual = Toolkit::VisualFactory::Get().CreateVisual(imagePropertyMap);
- // Use premultiplied alpha when we use FBO
- if(mVisual)
- {
- Toolkit::GetImplementation(mVisual).EnablePreMultipliedAlpha(true);
- }
-
Toolkit::DevelControl::RegisterVisual(*this, RENDERING_BUFFER, mVisual);
mRenderTask.SetFrameBuffer(mFrameBuffer);
mRenderTask.SetViewportGuideActor(Self());
if(mRenderTask.GetFrameBuffer())
{
+ mRootLayer.SetProperty(Dali::Actor::Property::COLOR_MODE, ColorMode::USE_OWN_MULTIPLY_PARENT_ALPHA);
FrameBuffer framebuffer;
mRenderTask.SetFrameBuffer(framebuffer);
mRenderTask.SetClearEnabled(false);
}
}
+void SceneView::UpdateSkybox(const std::string& skyboxUrl, Scene3D::EnvironmentMapType skyboxEnvironmentMapType)
+{
+ bool isOnScene = Self().GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE);
+ if(mSkyboxUrl != skyboxUrl || mSkyboxEnvironmentMapType != skyboxEnvironmentMapType)
+ {
+ mSkyboxDirty = true;
+ mSkyboxResourceReady = false;
+ mSkyboxUrl = skyboxUrl;
+ mSkyboxEnvironmentMapType = skyboxEnvironmentMapType;
+ }
+
+ if(mSkyboxUrl.empty())
+ {
+ if(mSkyboxLoadTask)
+ {
+ Dali::AsyncTaskManager::Get().RemoveTask(mSkyboxLoadTask);
+ mSkyboxLoadTask.Reset();
+ }
+
+ if(mSkybox)
+ {
+ mSkybox.Unparent();
+ mSkybox.Reset();
+ mSkyboxTexture.Reset();
+ }
+
+ mSkyboxDirty = false;
+ mSkyboxResourceReady = true;
+ }
+ else
+ {
+ if(isOnScene && mSkyboxDirty)
+ {
+ if(mSkyboxLoadTask)
+ {
+ Dali::AsyncTaskManager::Get().RemoveTask(mSkyboxLoadTask);
+ mSkyboxLoadTask.Reset();
+ }
+
+ mSkyboxLoadTask = new EnvironmentMapLoadTask(mSkyboxUrl, mSkyboxEnvironmentMapType, MakeCallback(this, &SceneView::OnSkyboxLoadComplete));
+ Dali::AsyncTaskManager::Get().AddTask(mSkyboxLoadTask);
+ mSkyboxDirty = false;
+ }
+ }
+
+ if(IsResourceReady())
+ {
+ Control::SetResourceReady();
+ }
+}
+
+void SceneView::OnSkyboxLoadComplete()
+{
+ if(!mSkybox)
+ {
+ mSkybox = CreateSkybox();
+ SetSkyboxIntensity(mSkyboxIntensity);
+ SetSkyboxOrientation(mSkyboxOrientation);
+ if(mRootLayer)
+ {
+ mRootLayer.Add(mSkybox);
+ }
+ }
+
+ mSkyboxResourceReady = true;
+ if(IsResourceReady())
+ {
+ Control::SetResourceReady();
+ }
+
+ mSkyboxTexture = mSkyboxLoadTask->GetLoadedTexture();
+ Shader skyboxShader;
+ if(mSkyboxLoadTask->GetEnvironmentMapType() == Scene3D::EnvironmentMapType::CUBEMAP)
+ {
+ skyboxShader = Shader::New(SHADER_SKYBOX_SHADER_VERT.data(), SHADER_SKYBOX_SHADER_FRAG.data());
+ }
+ else
+ {
+ skyboxShader = Shader::New(SHADER_SKYBOX_SHADER_VERT.data(), SHADER_SKYBOX_EQUIRECTANGULAR_SHADER_FRAG.data());
+ }
+
+ Renderer skyboxRenderer = (mSkybox.GetRendererCount() > 0u) ? mSkybox.GetRendererAt(0u) : Renderer();
+ if(skyboxRenderer)
+ {
+ Dali::TextureSet skyboxTextures = TextureSet::New();
+ skyboxTextures.SetTexture(0, mSkyboxTexture);
+ skyboxRenderer.SetTextures(skyboxTextures);
+ skyboxRenderer.SetShader(skyboxShader);
+ }
+
+ mSkyboxLoadTask.Reset();
+}
+
+void SceneView::OnIblDiffuseLoadComplete()
+{
+ mDiffuseTexture = mIblDiffuseLoadTask->GetLoadedTexture();
+ mIblDiffuseResourceReady = true;
+ if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
+ {
+ OnIblLoadComplete();
+ }
+ mIblDiffuseLoadTask.Reset();
+}
+
+void SceneView::OnIblSpecularLoadComplete()
+{
+ mSpecularTexture = mIblSpecularLoadTask->GetLoadedTexture();
+ mSpecularMipmapLevels = mIblSpecularLoadTask->GetMipmapLevels();
+ mIblSpecularResourceReady = true;
+ if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
+ {
+ OnIblLoadComplete();
+ }
+ mIblSpecularLoadTask.Reset();
+}
+
+void SceneView::OnIblLoadComplete()
+{
+ NotifyImageBasedLightTextureChange();
+ if(IsResourceReady())
+ {
+ Control::SetResourceReady();
+ }
+}
+
+void SceneView::NotifyImageBasedLightTextureChange()
+{
+ for(auto&& item : mItems)
+ {
+ if(item)
+ {
+ item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor, mSpecularMipmapLevels);
+ }
+ }
+}
+
+void SceneView::UpdateShadowMapBuffer(uint32_t shadowMapSize)
+{
+ Dali::FrameBuffer currentShadowFrameBuffer = mShadowMapRenderTask.GetFrameBuffer();
+ if(mShadowLight &&
+ (!currentShadowFrameBuffer ||
+ !Dali::Equals(DevelFrameBuffer::GetDepthTexture(currentShadowFrameBuffer).GetWidth(), shadowMapSize)))
+ {
+ mShadowFrameBuffer.Reset();
+ Dali::Texture shadowTexture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::DEPTH_UNSIGNED_INT, shadowMapSize, shadowMapSize);
+ mShadowFrameBuffer = FrameBuffer::New(shadowMapSize, shadowMapSize, FrameBuffer::Attachment::NONE);
+ DevelFrameBuffer::AttachDepthTexture(mShadowFrameBuffer, shadowTexture);
+ mShadowMapRenderTask.SetFrameBuffer(mShadowFrameBuffer);
+
+ for(auto&& item : mItems)
+ {
+ if(item)
+ {
+ item->NotifyShadowMapTexture(shadowTexture);
+ }
+ }
+ }
+}
+
} // namespace Internal
} // namespace Scene3D
} // namespace Dali