2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-scene3d/internal/controls/scene-view/scene-view-impl.h>
22 #include <dali-toolkit/dali-toolkit.h>
23 #include <dali-toolkit/devel-api/controls/control-devel.h>
24 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
25 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
26 #include <dali-toolkit/public-api/image-loader/image-url.h>
27 #include <dali-toolkit/public-api/image-loader/image.h>
28 #include <dali/devel-api/actors/camera-actor-devel.h>
29 #include <dali/devel-api/adaptor-framework/window-devel.h>
30 #include <dali/devel-api/common/stage.h>
31 #include <dali/devel-api/rendering/frame-buffer-devel.h>
32 #include <dali/integration-api/adaptor-framework/adaptor.h>
33 #include <dali/integration-api/debug.h>
34 #include <dali/public-api/math/math-utils.h>
35 #include <dali/public-api/object/type-registry-helper.h>
36 #include <dali/public-api/object/type-registry.h>
37 #include <string_view>
40 #include <dali-scene3d/internal/controls/model/model-impl.h>
41 #include <dali-scene3d/internal/graphics/builtin-shader-extern-gen.h>
42 #include <dali-scene3d/internal/light/light-impl.h>
44 #include <dali/integration-api/debug.h>
58 return Scene3D::SceneView::New();
61 // Setup properties, signals and actions using the type-registry.
62 DALI_TYPE_REGISTRATION_BEGIN(Scene3D::SceneView, Toolkit::Control, Create);
63 DALI_TYPE_REGISTRATION_END()
65 Property::Index RENDERING_BUFFER = Dali::Toolkit::Control::CONTROL_PROPERTY_END_INDEX + 1;
66 constexpr int32_t DEFAULT_ORIENTATION = 0;
67 constexpr int32_t INVALID_INDEX = -1;
68 constexpr uint32_t MAXIMUM_SIZE_SHADOW_MAP = 2048;
70 static constexpr std::string_view SKYBOX_INTENSITY_STRING = "uIntensity";
72 Dali::Actor CreateSkybox()
79 Vertex skyboxVertices[] = {
81 {Vector3(-1.0f, 1.0f, -1.0f)},
82 {Vector3(-1.0f, -1.0f, -1.0f)},
83 {Vector3(1.0f, -1.0f, -1.0f)},
84 {Vector3(1.0f, -1.0f, -1.0f)},
85 {Vector3(1.0f, 1.0f, -1.0f)},
86 {Vector3(-1.0f, 1.0f, -1.0f)},
89 {Vector3(-1.0f, -1.0f, 1.0f)},
90 {Vector3(-1.0f, -1.0f, -1.0f)},
91 {Vector3(-1.0f, 1.0f, -1.0f)},
92 {Vector3(-1.0f, 1.0f, -1.0f)},
93 {Vector3(-1.0f, 1.0f, 1.0f)},
94 {Vector3(-1.0f, -1.0f, 1.0f)},
97 {Vector3(1.0f, -1.0f, -1.0f)},
98 {Vector3(1.0f, -1.0f, 1.0f)},
99 {Vector3(1.0f, 1.0f, 1.0f)},
100 {Vector3(1.0f, 1.0f, 1.0f)},
101 {Vector3(1.0f, 1.0f, -1.0f)},
102 {Vector3(1.0f, -1.0f, -1.0f)},
105 {Vector3(-1.0f, -1.0f, 1.0f)},
106 {Vector3(-1.0f, 1.0f, 1.0f)},
107 {Vector3(1.0f, 1.0f, 1.0f)},
108 {Vector3(1.0f, 1.0f, 1.0f)},
109 {Vector3(1.0f, -1.0f, 1.0f)},
110 {Vector3(-1.0f, -1.0f, 1.0f)},
113 {Vector3(-1.0f, 1.0f, -1.0f)},
114 {Vector3(1.0f, 1.0f, -1.0f)},
115 {Vector3(1.0f, 1.0f, 1.0f)},
116 {Vector3(1.0f, 1.0f, 1.0f)},
117 {Vector3(-1.0f, 1.0f, 1.0f)},
118 {Vector3(-1.0f, 1.0f, -1.0f)},
121 {Vector3(-1.0f, -1.0f, -1.0f)},
122 {Vector3(-1.0f, -1.0f, 1.0f)},
123 {Vector3(1.0f, -1.0f, -1.0f)},
124 {Vector3(1.0f, -1.0f, -1.0f)},
125 {Vector3(-1.0f, -1.0f, 1.0f)},
126 {Vector3(1.0f, -1.0f, 1.0f)}};
128 Dali::VertexBuffer vertexBuffer = Dali::VertexBuffer::New(Property::Map().Add("aPosition", Property::VECTOR3));
129 vertexBuffer.SetData(skyboxVertices, sizeof(skyboxVertices) / sizeof(Vertex));
131 Dali::Geometry skyboxGeometry = Geometry::New();
132 skyboxGeometry.AddVertexBuffer(vertexBuffer);
133 skyboxGeometry.SetType(Geometry::TRIANGLES);
135 Dali::Shader shaderSkybox = Shader::New(SHADER_SKYBOX_SHADER_VERT.data(), SHADER_SKYBOX_SHADER_FRAG.data());
136 Dali::Renderer skyboxRenderer;
137 skyboxRenderer = Renderer::New(skyboxGeometry, shaderSkybox);
138 skyboxRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, 2.0f);
139 // Enables the depth test.
140 skyboxRenderer.SetProperty(Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON);
141 // The fragment shader will run only is those pixels that have the max depth value.
142 skyboxRenderer.SetProperty(Renderer::Property::DEPTH_FUNCTION, DepthFunction::LESS_EQUAL);
144 Dali::Actor skyboxActor = Actor::New();
145 skyboxActor.SetProperty(Dali::Actor::Property::NAME, "SkyBox");
146 skyboxActor.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
147 skyboxActor.SetProperty(Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
148 skyboxActor.AddRenderer(skyboxRenderer);
152 void SetShadowLightConstraint(Dali::CameraActor selectedCamera, Dali::CameraActor shadowLightCamera)
154 shadowLightCamera.SetProperty(Dali::CameraActor::Property::ASPECT_RATIO, 1.0f);
155 shadowLightCamera.SetProperty(Dali::DevelCameraActor::Property::ORTHOGRAPHIC_SIZE, 1.0f);
156 shadowLightCamera.SetProperty(Dali::CameraActor::Property::NEAR_PLANE_DISTANCE, 0.5f);
157 shadowLightCamera.SetProperty(Dali::CameraActor::Property::FAR_PLANE_DISTANCE, 3.5f);
159 //< Make constraint for above properties.
160 shadowLightCamera.RemoveConstraints();
162 // Compute ViewProjectionMatrix and store it to "tempViewProjectionMatrix" property
163 auto tempViewProjectionMatrixIndex = shadowLightCamera.RegisterProperty("tempViewProjectionMatrix", Matrix::IDENTITY);
164 Constraint projectionMatrixConstraint = Constraint::New<Matrix>(shadowLightCamera, tempViewProjectionMatrixIndex, [](Matrix& output, const PropertyInputContainer& inputs)
166 Matrix worldMatrix = inputs[0]->GetMatrix();
167 float tangentFov_2 = tanf(inputs[4]->GetFloat());
168 float nearDistance = inputs[5]->GetFloat();
169 float farDistance = inputs[6]->GetFloat();
170 float aspectRatio = inputs[7]->GetFloat();
175 if(inputs[1]->GetInteger() == Dali::Camera::ProjectionMode::PERSPECTIVE_PROJECTION)
177 if(inputs[2]->GetInteger() == Dali::DevelCameraActor::ProjectionDirection::VERTICAL)
179 nearY = tangentFov_2 * nearDistance;
180 nearX = nearY * aspectRatio;
181 farY = tangentFov_2 * farDistance;
182 farX = farY * aspectRatio;
186 nearX = tangentFov_2 * nearDistance;
187 nearY = nearX / aspectRatio;
188 farX = tangentFov_2 * farDistance;
189 farY = farX / aspectRatio;
194 if(inputs[2]->GetInteger() == Dali::DevelCameraActor::ProjectionDirection::VERTICAL)
196 nearY = inputs[3]->GetFloat();
197 nearX = nearY * aspectRatio;
201 nearX = inputs[3]->GetFloat();
202 nearY = nearX / aspectRatio;
208 std::vector<Vector4> points;
209 points.push_back(Vector4(nearX, nearY, nearDistance, 1.0f));
210 points.push_back(Vector4(-nearX, nearY, nearDistance, 1.0f));
211 points.push_back(Vector4(-nearX, -nearY, nearDistance, 1.0f));
212 points.push_back(Vector4(nearX, -nearY, nearDistance, 1.0f));
213 points.push_back(Vector4(farX, farY, farDistance, 1.0f));
214 points.push_back(Vector4(-farX, farY, farDistance, 1.0f));
215 points.push_back(Vector4(-farX, -farY, farDistance, 1.0f));
216 points.push_back(Vector4(farX, -farY, farDistance, 1.0f));
218 Matrix shadowCameraWorldMatrix = inputs[8]->GetMatrix();
220 for(auto&& point : points)
222 point = worldMatrix * point;
223 worldCenter += point;
226 shadowCameraWorldMatrix.SetTranslation(Vector3(worldCenter));
227 Matrix shadowCameraViewMatrix = shadowCameraWorldMatrix;
228 shadowCameraViewMatrix.Invert();
230 Vector3 areaMin = Vector3::ONE * MAXFLOAT, areaMax = Vector3::ONE * -MAXFLOAT;
231 for(auto&& point : points)
233 Vector4 pointV = shadowCameraViewMatrix * point;
234 areaMin.x = std::min(areaMin.x, pointV.x);
235 areaMin.y = std::min(areaMin.y, pointV.y);
236 areaMin.z = std::min(areaMin.z, pointV.z);
237 areaMax.x = std::max(areaMax.x, pointV.x);
238 areaMax.y = std::max(areaMax.y, pointV.y);
239 areaMax.z = std::max(areaMax.z, pointV.z);
242 Vector2 center = Vector2(areaMax + areaMin) * 0.5;
243 float delta = std::max(std::abs(areaMax.x - areaMin.x), std::abs(areaMax.y - areaMin.y));
244 float delta_2 = delta * 0.5f;
245 Vector2 squareAreaMin = center - Vector2::ONE * delta_2;
246 Vector2 squareAreaMax = center + Vector2::ONE * delta_2;
247 float deltaZ = areaMax.z - areaMin.z;
249 float right = -squareAreaMin.x;
250 float left = -squareAreaMax.x;
251 float top = squareAreaMin.y;
252 float bottom = squareAreaMax.y;
253 float near = areaMin.z;
254 float far = areaMax.z;
256 float* projMatrix = output.AsFloat();
258 projMatrix[0] = -2.0f / delta;
259 projMatrix[1] = 0.0f;
260 projMatrix[2] = 0.0f;
261 projMatrix[3] = 0.0f;
263 projMatrix[4] = 0.0f;
264 projMatrix[5] = -2.0f / delta;
265 projMatrix[6] = 0.0f;
266 projMatrix[7] = 0.0f;
268 projMatrix[8] = 0.0f;
269 projMatrix[9] = 0.0f;
270 projMatrix[10] = 2.0f / deltaZ;
271 projMatrix[11] = 0.0f;
273 projMatrix[12] = -(right + left) / delta;
274 projMatrix[13] = -(top + bottom) / delta;
275 projMatrix[14] = -(near + far) / deltaZ;
276 projMatrix[15] = 1.0f;
278 output = output * shadowCameraViewMatrix;
280 projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::Actor::Property::WORLD_MATRIX});
281 projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::CameraActor::Property::PROJECTION_MODE});
282 projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::DevelCameraActor::Property::PROJECTION_DIRECTION});
283 projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::DevelCameraActor::Property::ORTHOGRAPHIC_SIZE});
284 projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::CameraActor::Property::FIELD_OF_VIEW});
285 projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::CameraActor::Property::NEAR_PLANE_DISTANCE});
286 projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::CameraActor::Property::FAR_PLANE_DISTANCE});
287 projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::CameraActor::Property::ASPECT_RATIO});
288 projectionMatrixConstraint.AddSource(Source{shadowLightCamera, Dali::Actor::Property::WORLD_MATRIX});
289 projectionMatrixConstraint.ApplyPost();
292 } // anonymous namespace
294 SceneView::SceneView()
295 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
296 mWindowOrientation(DEFAULT_ORIENTATION),
298 mSkyboxOrientation(Quaternion()),
299 mSkyboxIntensity(1.0f),
300 mShaderManager(new Scene3D::Loader::ShaderManager())
304 SceneView::~SceneView()
306 if(Dali::Adaptor::IsAvailable())
308 if(mIblDiffuseLoadTask)
310 Dali::AsyncTaskManager::Get().RemoveTask(mIblDiffuseLoadTask);
311 mIblDiffuseLoadTask.Reset();
313 if(mIblSpecularLoadTask)
315 Dali::AsyncTaskManager::Get().RemoveTask(mIblSpecularLoadTask);
316 mIblSpecularLoadTask.Reset();
320 Dali::AsyncTaskManager::Get().RemoveTask(mSkyboxLoadTask);
321 mSkyboxLoadTask.Reset();
326 Dali::Scene3D::SceneView SceneView::New()
328 SceneView* impl = new SceneView();
330 Dali::Scene3D::SceneView handle = Dali::Scene3D::SceneView(*impl);
332 // Second-phase init of the implementation
333 // This can only be done after the CustomActor connection has been made...
339 void SceneView::AddCamera(CameraActor camera)
345 UpdateCamera(camera);
347 mCameras.push_back(camera);
351 void SceneView::RemoveCamera(CameraActor camera)
353 if(camera == mDefaultCamera)
355 DALI_LOG_ERROR("Default Camera cannot removed.\n");
361 for(uint32_t i = 0; i < mCameras.size(); ++i)
363 if(mCameras[i] == camera)
365 mCameras.erase(mCameras.begin() + i);
370 if(mSelectedCamera == camera)
372 CameraActor newCurrentCamera = *mCameras.begin();
373 UpdateCamera(newCurrentCamera);
378 uint32_t SceneView::GetCameraCount() const
380 return mCameras.size();
383 CameraActor SceneView::GetSelectedCamera() const
385 return mSelectedCamera;
388 CameraActor SceneView::GetCamera(uint32_t index) const
390 if(index < mCameras.size())
392 return mCameras[index];
394 DALI_LOG_ERROR("Input index is out of bounds\n");
395 return CameraActor();
398 CameraActor SceneView::GetCamera(const std::string& name) const
400 CameraActor returnCamera;
401 for(auto&& camera : mCameras)
403 if(camera.GetProperty<std::string>(Actor::Property::NAME) == name)
405 returnCamera = camera;
412 void SceneView::SelectCamera(uint32_t index)
414 UpdateCamera(GetCamera(index));
417 void SceneView::SelectCamera(const std::string& name)
419 UpdateCamera(GetCamera(name));
422 void SceneView::RegisterSceneItem(Scene3D::Internal::LightObserver* item)
426 item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor, mSpecularMipmapLevels);
427 item->NotifyShadowMapTexture(mShadowTexture);
428 mItems.push_back(item);
432 void SceneView::UnregisterSceneItem(Scene3D::Internal::LightObserver* item)
436 for(uint32_t i = 0; i < mItems.size(); ++i)
438 if(mItems[i] == item)
440 mItems.erase(mItems.begin() + i);
447 void SceneView::SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor)
449 bool needIblReset = false;
450 bool isOnScene = Self().GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE);
451 if(mDiffuseIblUrl != diffuseUrl)
453 mDiffuseIblUrl = diffuseUrl;
454 if(mDiffuseIblUrl.empty())
460 mIblDiffuseDirty = true;
461 mIblDiffuseResourceReady = false;
465 if(mSpecularIblUrl != specularUrl)
467 mSpecularIblUrl = specularUrl;
468 if(mSpecularIblUrl.empty())
474 mIblSpecularDirty = true;
475 mIblSpecularResourceReady = false;
479 // If one or both of diffuse url and specular url are empty,
480 // we don't need to request to load texture.
483 if(mIblDiffuseLoadTask)
485 Dali::AsyncTaskManager::Get().RemoveTask(mIblDiffuseLoadTask);
486 mIblDiffuseLoadTask.Reset();
489 if(mIblSpecularLoadTask)
491 Dali::AsyncTaskManager::Get().RemoveTask(mIblSpecularLoadTask);
492 mIblSpecularLoadTask.Reset();
495 mIblDiffuseDirty = false;
496 mIblSpecularDirty = false;
497 mIblDiffuseResourceReady = true;
498 mIblSpecularResourceReady = true;
500 mDiffuseTexture.Reset();
501 mSpecularTexture.Reset();
503 mSpecularMipmapLevels = 1u;
504 NotifyImageBasedLightTextureChange();
508 if(isOnScene && mIblDiffuseDirty)
510 if(mIblDiffuseLoadTask)
512 Dali::AsyncTaskManager::Get().RemoveTask(mIblDiffuseLoadTask);
513 mIblDiffuseLoadTask.Reset();
515 mIblDiffuseLoadTask = new EnvironmentMapLoadTask(mDiffuseIblUrl, Scene3D::EnvironmentMapType::CUBEMAP, MakeCallback(this, &SceneView::OnIblDiffuseLoadComplete));
516 Dali::AsyncTaskManager::Get().AddTask(mIblDiffuseLoadTask);
517 mIblDiffuseDirty = false;
520 if(isOnScene && mIblSpecularDirty)
522 if(mIblSpecularLoadTask)
524 Dali::AsyncTaskManager::Get().RemoveTask(mIblSpecularLoadTask);
525 mIblSpecularLoadTask.Reset();
527 mIblSpecularLoadTask = new EnvironmentMapLoadTask(mSpecularIblUrl, Scene3D::EnvironmentMapType::CUBEMAP, MakeCallback(this, &SceneView::OnIblSpecularLoadComplete));
528 Dali::AsyncTaskManager::Get().AddTask(mIblSpecularLoadTask);
529 mIblSpecularDirty = false;
533 if(!Dali::Equals(mIblScaleFactor, scaleFactor))
535 SetImageBasedLightScaleFactor(scaleFactor);
538 // If diffuse and specular textures are already loaded, emits resource ready signal here.
539 if(IsResourceReady())
541 Control::SetResourceReady();
545 void SceneView::SetImageBasedLightScaleFactor(float scaleFactor)
547 mIblScaleFactor = scaleFactor;
548 for(auto&& item : mItems)
552 item->NotifyImageBasedLightScaleFactor(scaleFactor);
557 float SceneView::GetImageBasedLightScaleFactor() const
559 return mIblScaleFactor;
562 void SceneView::AddLight(Scene3D::Light light)
564 bool enabled = mShaderManager->AddLight(light);
565 mLights.push_back(std::make_pair(light, enabled));
567 if(light.IsShadowEnabled())
573 void SceneView::RemoveLight(Scene3D::Light light)
575 mShaderManager->RemoveLight(light);
576 for(uint32_t i = 0; i < mLights.size(); ++i)
578 if(mLights[i].first == light)
580 mLights.erase(mLights.begin() + i);
585 if(mLights.size() > mShaderManager->GetLightCount())
587 for(auto&& waitingLight : mLights)
589 if(waitingLight.second)
594 waitingLight.second = mShaderManager->AddLight(waitingLight.first);
599 if(light == mShadowLight)
606 for(auto&& lightEntity : mLights)
608 if(!lightEntity.second || !lightEntity.first.IsShadowEnabled())
612 SetShadow(lightEntity.first);
618 void SceneView::SetShadow(Scene3D::Light light)
625 auto foundLight = std::find_if(mLights.begin(), mLights.end(), [light](std::pair<Scene3D::Light, bool> lightEntity) -> bool
626 { return (lightEntity.second && lightEntity.first == light); });
628 if(foundLight == mLights.end())
633 mShadowLight = light;
635 // Directional Light setting.
636 CameraActor lightCamera = GetImplementation(light).GetCamera();
637 CameraActor selectedCamera = GetSelectedCamera();
638 SetShadowLightConstraint(selectedCamera, lightCamera);
640 // make framebuffer for depth map and set it to render task.
641 Vector3 size = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
642 uint32_t shadowMapBufferSize = std::min(static_cast<uint32_t>(std::max(size.width, size.height)), MAXIMUM_SIZE_SHADOW_MAP);
643 UpdateShadowMapBuffer(shadowMapBufferSize);
645 // use lightCamera as a camera of shadow render task.
646 mShadowMapRenderTask.SetCameraActor(lightCamera);
648 mShaderManager->SetShadow(light);
651 void SceneView::RemoveShadow(Scene3D::Light light)
653 if(mShadowLight != light)
658 // remove all constraint from light camera
659 CameraActor lightCamera = GetImplementation(mShadowLight).GetCamera();
660 lightCamera.RemoveConstraints();
662 // reset framebuffer and remove it from render task.
663 mShadowFrameBuffer.Reset();
664 mShaderManager->RemoveShadow();
665 mShadowMapRenderTask.SetCameraActor(CameraActor());
667 mShadowLight.Reset();
669 mShadowTexture.Reset();
670 for(auto&& item : mItems)
674 item->NotifyShadowMapTexture(mShadowTexture);
678 for(auto&& lightEntity : mLights)
680 if(!lightEntity.second || !lightEntity.first.IsShadowEnabled())
684 SetShadow(lightEntity.first);
689 uint32_t SceneView::GetActivatedLightCount() const
691 return mShaderManager->GetLightCount();
694 void SceneView::UseFramebuffer(bool useFramebuffer)
696 if(mUseFrameBuffer != useFramebuffer)
698 mUseFrameBuffer = useFramebuffer;
703 bool SceneView::IsUsingFramebuffer() const
705 return mUseFrameBuffer;
708 void SceneView::SetFramebufferMultiSamplingLevel(uint8_t multiSamplingLevel)
710 if(mFrameBufferMultiSamplingLevel != multiSamplingLevel)
712 mFrameBufferMultiSamplingLevel = multiSamplingLevel;
714 // Create new framebuffer with changed multiSamplingLevel.
715 if(mRenderTask && mFrameBuffer && mTexture)
717 Vector3 size = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
719 mFrameBuffer = FrameBuffer::New(size.width, size.height, FrameBuffer::Attachment::DEPTH_STENCIL);
720 mFrameBuffer.AttachColorTexture(mTexture);
721 DevelFrameBuffer::SetMultiSamplingLevel(mFrameBuffer, mFrameBufferMultiSamplingLevel);
722 mRenderTask.SetFrameBuffer(mFrameBuffer);
724 // Note : we don't need to create new visual since visual's url is depend on mTexture.
729 uint8_t SceneView::GetFramebufferMultiSamplingLevel() const
731 return mFrameBufferMultiSamplingLevel;
734 void SceneView::SetSkybox(const std::string& skyboxUrl)
736 if(mSkyboxUrl != skyboxUrl)
738 UpdateSkybox(skyboxUrl, mSkyboxEnvironmentMapType);
742 void SceneView::SetSkyboxEnvironmentMapType(Scene3D::EnvironmentMapType skyboxEnvironmentMapType)
744 if(mSkyboxEnvironmentMapType != skyboxEnvironmentMapType)
746 UpdateSkybox(mSkyboxUrl, skyboxEnvironmentMapType);
750 void SceneView::SetSkyboxIntensity(float intensity)
752 mSkyboxIntensity = intensity;
755 DALI_LOG_ERROR("Intensity should be greater than or equal to 0.\n");
756 mSkyboxIntensity = 0.0f;
761 mSkybox.RegisterProperty(SKYBOX_INTENSITY_STRING.data(), mSkyboxIntensity);
765 float SceneView::GetSkyboxIntensity() const
767 return mSkyboxIntensity;
770 void SceneView::SetSkyboxOrientation(const Quaternion& orientation)
772 mSkyboxOrientation = orientation;
775 mSkybox.SetProperty(Dali::Actor::Property::ORIENTATION, orientation);
779 Quaternion SceneView::GetSkyboxOrientation() const
781 return mSkyboxOrientation;
784 Dali::Scene3D::Loader::ShaderManagerPtr SceneView::GetShaderManager() const
786 return mShaderManager;
789 void SceneView::UpdateShadowUniform(Scene3D::Light light)
791 mShaderManager->UpdateShadowUniform(light);
794 ///////////////////////////////////////////////////////////
799 void SceneView::OnSceneConnection(int depth)
801 // If diffuse and specular url is not valid, IBL does not need to be loaded.
802 if(!mDiffuseIblUrl.empty() && !mSpecularIblUrl.empty())
804 SetImageBasedLightSource(mDiffuseIblUrl, mSpecularIblUrl, mIblScaleFactor);
807 if(!mSkyboxUrl.empty())
809 UpdateSkybox(mSkyboxUrl, mSkyboxEnvironmentMapType);
812 Window window = DevelWindow::Get(Self());
815 // Only for on-screen window
816 window.ResizeSignal().Connect(this, &SceneView::OnWindowResized);
819 mWindowOrientation = DevelWindow::GetPhysicalOrientation(window);
822 // On-screen / Off-screen window
823 mSceneHolder = Integration::SceneHolder::Get(Self());
826 RenderTaskList taskList = mSceneHolder.GetRenderTaskList();
827 mShadowMapRenderTask = taskList.CreateTask();
828 mShadowMapRenderTask.SetSourceActor(mRootLayer);
829 mShadowMapRenderTask.SetExclusive(true);
830 mShadowMapRenderTask.SetInputEnabled(false);
831 mShadowMapRenderTask.SetCullMode(false);
832 mShadowMapRenderTask.SetClearEnabled(true);
833 mShadowMapRenderTask.SetClearColor(Color::WHITE);
834 mShadowMapRenderTask.SetRenderPassTag(10);
835 mShadowMapRenderTask.SetCameraActor(CameraActor());
837 mRenderTask = taskList.CreateTask();
838 mRenderTask.SetSourceActor(mRootLayer);
839 mRenderTask.SetExclusive(true);
840 mRenderTask.SetInputEnabled(true);
841 mRenderTask.SetCullMode(false);
842 mRenderTask.SetScreenToFrameBufferMappingActor(Self());
847 Control::OnSceneConnection(depth);
850 void SceneView::OnSceneDisconnection()
854 Window window = mWindow.GetHandle();
857 window.ResizeSignal().Disconnect(this, &SceneView::OnWindowResized);
865 RenderTaskList taskList = mSceneHolder.GetRenderTaskList();
866 taskList.RemoveTask(mRenderTask);
869 if(mShadowMapRenderTask)
871 RenderTaskList taskList = mSceneHolder.GetRenderTaskList();
872 taskList.RemoveTask(mShadowMapRenderTask);
873 mShadowMapRenderTask.Reset();
875 mSceneHolder.Reset();
877 mFrameBuffer.Reset();
878 mShadowFrameBuffer.Reset();
880 Control::OnSceneDisconnection();
883 void SceneView::OnInitialize()
886 mRootLayer = Layer::New();
887 mRootLayer.SetProperty(Layer::Property::BEHAVIOR, Layer::LAYER_3D);
888 mRootLayer.SetProperty(Layer::Property::DEPTH_TEST, true);
889 // The models in the SceneView should be have independent coordinate with DALi default coordinate.
890 mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_POSITION, false);
891 mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_ORIENTATION, false);
892 mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_SCALE, false);
893 self.Add(mRootLayer);
895 mDefaultCamera = Dali::CameraActor::New3DCamera();
896 mDefaultCamera.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
897 mDefaultCamera.SetProperty(Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
898 AddCamera(mDefaultCamera);
899 UpdateCamera(mDefaultCamera);
902 void SceneView::OnChildAdd(Actor& child)
904 if(child != mRootLayer)
906 mRootLayer.Add(child);
908 Control::OnChildAdd(child);
911 void SceneView::OnChildRemove(Actor& child)
913 mRootLayer.Remove(child);
914 Control::OnChildRemove(child);
917 float SceneView::GetHeightForWidth(float width)
920 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
921 return Control::GetHeightForWidth(width) + padding.top + padding.bottom;
924 float SceneView::GetWidthForHeight(float height)
927 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
928 return Control::GetWidthForHeight(height) + padding.start + padding.end;
931 void SceneView::OnRelayout(const Vector2& size, RelayoutContainer& container)
933 Control::OnRelayout(size, container);
934 // Change canvas size of camera actor.
938 bool SceneView::IsResourceReady() const
940 return mIblDiffuseResourceReady && mIblSpecularResourceReady && mSkyboxResourceReady;
943 void SceneView::UpdateCamera(CameraActor camera)
947 if(mSelectedCamera && mSelectedCamera.GetParent())
949 mSelectedCamera.Unparent();
951 mRootLayer.Add(camera);
954 mSelectedCamera = camera;
957 SetShadowLightConstraint(mSelectedCamera, GetImplementation(mShadowLight).GetCamera());
962 void SceneView::UpdateRenderTask()
966 if(mSelectedCamera != mRenderTask.GetCameraActor())
968 mRenderTask.SetCameraActor(mSelectedCamera);
971 Vector3 size = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
972 const float aspectRatio = size.width / size.height;
973 mSelectedCamera.SetAspectRatio(aspectRatio);
975 uint32_t shadowMapBufferSize = std::min(static_cast<uint32_t>(std::max(size.width, size.height)), MAXIMUM_SIZE_SHADOW_MAP);
976 UpdateShadowMapBuffer(shadowMapBufferSize);
980 Dali::FrameBuffer currentFrameBuffer = mRenderTask.GetFrameBuffer();
981 if(!currentFrameBuffer ||
982 !Dali::Equals(currentFrameBuffer.GetColorTexture().GetWidth(), size.width) ||
983 !Dali::Equals(currentFrameBuffer.GetColorTexture().GetHeight(), size.height))
985 mRootLayer.SetProperty(Dali::Actor::Property::COLOR_MODE, ColorMode::USE_OWN_COLOR);
986 mRenderTask.ResetViewportGuideActor();
987 mRenderTask.SetViewport(Dali::Viewport(Vector4::ZERO));
989 // create offscreen buffer of new size to render our child actors to
990 mTexture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, unsigned(size.width), unsigned(size.height));
991 mFrameBuffer = FrameBuffer::New(size.width, size.height, FrameBuffer::Attachment::DEPTH_STENCIL);
992 mFrameBuffer.AttachColorTexture(mTexture);
993 DevelFrameBuffer::SetMultiSamplingLevel(mFrameBuffer, mFrameBufferMultiSamplingLevel);
994 Dali::Toolkit::ImageUrl imageUrl = Dali::Toolkit::Image::GenerateUrl(mFrameBuffer, 0u);
996 Property::Map imagePropertyMap;
997 imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
998 imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, imageUrl.GetUrl());
999 // To flip rendered scene without CameraActor::SetInvertYAxis() to avoid backface culling.
1000 imagePropertyMap.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, Vector4(0.0f, 1.0f, 1.0f, -1.0f));
1001 mVisual = Toolkit::VisualFactory::Get().CreateVisual(imagePropertyMap);
1003 Toolkit::DevelControl::RegisterVisual(*this, RENDERING_BUFFER, mVisual);
1005 mRenderTask.SetFrameBuffer(mFrameBuffer);
1006 mRenderTask.SetClearEnabled(true);
1007 mRenderTask.SetClearColor(Color::TRANSPARENT);
1012 mRenderTask.SetViewportGuideActor(Self());
1013 if(mRenderTask.GetFrameBuffer())
1015 mRootLayer.SetProperty(Dali::Actor::Property::COLOR_MODE, ColorMode::USE_OWN_MULTIPLY_PARENT_ALPHA);
1016 FrameBuffer framebuffer;
1017 mRenderTask.SetFrameBuffer(framebuffer);
1018 mRenderTask.SetClearEnabled(false);
1020 Toolkit::DevelControl::UnregisterVisual(*this, RENDERING_BUFFER);
1023 mFrameBuffer.Reset();
1032 void SceneView::OnWindowResized(Window window, Window::WindowSize size)
1034 mWindowOrientation = DevelWindow::GetPhysicalOrientation(window);
1038 void SceneView::RotateCamera()
1042 DevelCameraActor::RotateProjection(mSelectedCamera, DEFAULT_ORIENTATION);
1046 DevelCameraActor::RotateProjection(mSelectedCamera, mWindowOrientation);
1050 void SceneView::UpdateSkybox(const std::string& skyboxUrl, Scene3D::EnvironmentMapType skyboxEnvironmentMapType)
1052 bool isOnScene = Self().GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE);
1053 if(mSkyboxUrl != skyboxUrl || mSkyboxEnvironmentMapType != skyboxEnvironmentMapType)
1055 mSkyboxDirty = true;
1056 mSkyboxResourceReady = false;
1057 mSkyboxUrl = skyboxUrl;
1058 mSkyboxEnvironmentMapType = skyboxEnvironmentMapType;
1061 if(mSkyboxUrl.empty())
1065 Dali::AsyncTaskManager::Get().RemoveTask(mSkyboxLoadTask);
1066 mSkyboxLoadTask.Reset();
1073 mSkyboxTexture.Reset();
1076 mSkyboxDirty = false;
1077 mSkyboxResourceReady = true;
1081 if(isOnScene && mSkyboxDirty)
1085 Dali::AsyncTaskManager::Get().RemoveTask(mSkyboxLoadTask);
1086 mSkyboxLoadTask.Reset();
1089 mSkyboxLoadTask = new EnvironmentMapLoadTask(mSkyboxUrl, mSkyboxEnvironmentMapType, MakeCallback(this, &SceneView::OnSkyboxLoadComplete));
1090 Dali::AsyncTaskManager::Get().AddTask(mSkyboxLoadTask);
1091 mSkyboxDirty = false;
1095 if(IsResourceReady())
1097 Control::SetResourceReady();
1101 void SceneView::OnSkyboxLoadComplete()
1105 mSkybox = CreateSkybox();
1106 SetSkyboxIntensity(mSkyboxIntensity);
1107 SetSkyboxOrientation(mSkyboxOrientation);
1110 mRootLayer.Add(mSkybox);
1114 mSkyboxResourceReady = true;
1115 if(IsResourceReady())
1117 Control::SetResourceReady();
1120 mSkyboxTexture = mSkyboxLoadTask->GetLoadedTexture();
1121 Shader skyboxShader;
1122 if(mSkyboxLoadTask->GetEnvironmentMapType() == Scene3D::EnvironmentMapType::CUBEMAP)
1124 skyboxShader = Shader::New(SHADER_SKYBOX_SHADER_VERT.data(), SHADER_SKYBOX_SHADER_FRAG.data());
1128 skyboxShader = Shader::New(SHADER_SKYBOX_SHADER_VERT.data(), SHADER_SKYBOX_EQUIRECTANGULAR_SHADER_FRAG.data());
1131 Renderer skyboxRenderer = (mSkybox.GetRendererCount() > 0u) ? mSkybox.GetRendererAt(0u) : Renderer();
1134 Dali::TextureSet skyboxTextures = TextureSet::New();
1135 skyboxTextures.SetTexture(0, mSkyboxTexture);
1136 skyboxRenderer.SetTextures(skyboxTextures);
1137 skyboxRenderer.SetShader(skyboxShader);
1140 mSkyboxLoadTask.Reset();
1143 void SceneView::OnIblDiffuseLoadComplete()
1145 mDiffuseTexture = mIblDiffuseLoadTask->GetLoadedTexture();
1146 mIblDiffuseResourceReady = true;
1147 if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
1149 OnIblLoadComplete();
1151 mIblDiffuseLoadTask.Reset();
1154 void SceneView::OnIblSpecularLoadComplete()
1156 mSpecularTexture = mIblSpecularLoadTask->GetLoadedTexture();
1157 mSpecularMipmapLevels = mIblSpecularLoadTask->GetMipmapLevels();
1158 mIblSpecularResourceReady = true;
1159 if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
1161 OnIblLoadComplete();
1163 mIblSpecularLoadTask.Reset();
1166 void SceneView::OnIblLoadComplete()
1168 NotifyImageBasedLightTextureChange();
1169 if(IsResourceReady())
1171 Control::SetResourceReady();
1175 void SceneView::NotifyImageBasedLightTextureChange()
1177 for(auto&& item : mItems)
1181 item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor, mSpecularMipmapLevels);
1186 void SceneView::UpdateShadowMapBuffer(uint32_t shadowMapSize)
1188 Dali::FrameBuffer currentShadowFrameBuffer = mShadowMapRenderTask.GetFrameBuffer();
1190 (!currentShadowFrameBuffer ||
1192 !Dali::Equals(DevelFrameBuffer::GetDepthTexture(currentShadowFrameBuffer).GetWidth(), shadowMapSize)))
1194 mShadowFrameBuffer.Reset();
1195 mShadowTexture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::DEPTH_UNSIGNED_INT, shadowMapSize, shadowMapSize);
1196 mShadowFrameBuffer = FrameBuffer::New(shadowMapSize, shadowMapSize, FrameBuffer::Attachment::NONE);
1197 DevelFrameBuffer::AttachDepthTexture(mShadowFrameBuffer, mShadowTexture);
1198 mShadowMapRenderTask.SetFrameBuffer(mShadowFrameBuffer);
1200 for(auto&& item : mItems)
1204 item->NotifyShadowMapTexture(mShadowTexture);
1210 } // namespace Internal
1211 } // namespace Scene3D