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) {
165 Matrix worldMatrix = inputs[0]->GetMatrix();
166 float tangentFov_2 = tanf(inputs[4]->GetFloat());
167 float nearDistance = inputs[5]->GetFloat();
168 float farDistance = inputs[6]->GetFloat();
169 float aspectRatio = inputs[7]->GetFloat();
174 if(inputs[1]->GetInteger() == Dali::Camera::ProjectionMode::PERSPECTIVE_PROJECTION)
176 if(inputs[2]->GetInteger() == Dali::DevelCameraActor::ProjectionDirection::VERTICAL)
178 nearY = tangentFov_2 * nearDistance;
179 nearX = nearY * aspectRatio;
180 farY = tangentFov_2 * farDistance;
181 farX = farY * aspectRatio;
185 nearX = tangentFov_2 * nearDistance;
186 nearY = nearX / aspectRatio;
187 farX = tangentFov_2 * farDistance;
188 farY = farX / aspectRatio;
193 if(inputs[2]->GetInteger() == Dali::DevelCameraActor::ProjectionDirection::VERTICAL)
195 nearY = inputs[3]->GetFloat();
196 nearX = nearY * aspectRatio;
200 nearX = inputs[3]->GetFloat();
201 nearY = nearX / aspectRatio;
207 std::vector<Vector4> points;
208 points.push_back(Vector4(nearX, nearY, nearDistance, 1.0f));
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(farX, farY, farDistance, 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));
217 Matrix shadowCameraWorldMatrix = inputs[8]->GetMatrix();
219 for(auto&& point : points)
221 point = worldMatrix * point;
222 worldCenter += point;
225 shadowCameraWorldMatrix.SetTranslation(Vector3(worldCenter));
226 Matrix shadowCameraViewMatrix = shadowCameraWorldMatrix;
227 shadowCameraViewMatrix.Invert();
229 Vector3 areaMin = Vector3::ONE * MAXFLOAT, areaMax = Vector3::ONE * -MAXFLOAT;
230 for(auto&& point : points)
232 Vector4 pointV = shadowCameraViewMatrix * point;
233 areaMin.x = std::min(areaMin.x, pointV.x);
234 areaMin.y = std::min(areaMin.y, pointV.y);
235 areaMin.z = std::min(areaMin.z, pointV.z);
236 areaMax.x = std::max(areaMax.x, pointV.x);
237 areaMax.y = std::max(areaMax.y, pointV.y);
238 areaMax.z = std::max(areaMax.z, pointV.z);
241 Vector2 center = Vector2(areaMax + areaMin) * 0.5;
242 float delta = std::max(std::abs(areaMax.x - areaMin.x), std::abs(areaMax.y - areaMin.y));
243 float delta_2 = delta * 0.5f;
244 Vector2 squareAreaMin = center - Vector2::ONE * delta_2;
245 Vector2 squareAreaMax = center + Vector2::ONE * delta_2;
246 float deltaZ = areaMax.z - areaMin.z;
248 float right = -squareAreaMin.x;
249 float left = -squareAreaMax.x;
250 float top = squareAreaMin.y;
251 float bottom = squareAreaMax.y;
252 float near = areaMin.z;
253 float far = areaMax.z;
255 float* projMatrix = output.AsFloat();
257 projMatrix[0] = -2.0f / delta;
258 projMatrix[1] = 0.0f;
259 projMatrix[2] = 0.0f;
260 projMatrix[3] = 0.0f;
262 projMatrix[4] = 0.0f;
263 projMatrix[5] = -2.0f / delta;
264 projMatrix[6] = 0.0f;
265 projMatrix[7] = 0.0f;
267 projMatrix[8] = 0.0f;
268 projMatrix[9] = 0.0f;
269 projMatrix[10] = 2.0f / deltaZ;
270 projMatrix[11] = 0.0f;
272 projMatrix[12] = -(right + left) / delta;
273 projMatrix[13] = -(top + bottom) / delta;
274 projMatrix[14] = -(near + far) / deltaZ;
275 projMatrix[15] = 1.0f;
277 output = output * shadowCameraViewMatrix;
279 projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::Actor::Property::WORLD_MATRIX});
280 projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::CameraActor::Property::PROJECTION_MODE});
281 projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::DevelCameraActor::Property::PROJECTION_DIRECTION});
282 projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::DevelCameraActor::Property::ORTHOGRAPHIC_SIZE});
283 projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::CameraActor::Property::FIELD_OF_VIEW});
284 projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::CameraActor::Property::NEAR_PLANE_DISTANCE});
285 projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::CameraActor::Property::FAR_PLANE_DISTANCE});
286 projectionMatrixConstraint.AddSource(Source{selectedCamera, Dali::CameraActor::Property::ASPECT_RATIO});
287 projectionMatrixConstraint.AddSource(Source{shadowLightCamera, Dali::Actor::Property::WORLD_MATRIX});
288 projectionMatrixConstraint.ApplyPost();
291 } // anonymous namespace
293 SceneView::SceneView()
294 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
295 mWindowOrientation(DEFAULT_ORIENTATION),
297 mSkyboxOrientation(Quaternion()),
298 mSkyboxIntensity(1.0f),
299 mShaderManager(new Scene3D::Loader::ShaderManager())
303 SceneView::~SceneView()
305 if(Dali::Adaptor::IsAvailable())
307 if(mIblDiffuseLoadTask)
309 Dali::AsyncTaskManager::Get().RemoveTask(mIblDiffuseLoadTask);
310 mIblDiffuseLoadTask.Reset();
312 if(mIblSpecularLoadTask)
314 Dali::AsyncTaskManager::Get().RemoveTask(mIblSpecularLoadTask);
315 mIblSpecularLoadTask.Reset();
319 Dali::AsyncTaskManager::Get().RemoveTask(mSkyboxLoadTask);
320 mSkyboxLoadTask.Reset();
325 Dali::Scene3D::SceneView SceneView::New()
327 SceneView* impl = new SceneView();
329 Dali::Scene3D::SceneView handle = Dali::Scene3D::SceneView(*impl);
331 // Second-phase init of the implementation
332 // This can only be done after the CustomActor connection has been made...
338 void SceneView::AddCamera(CameraActor camera)
344 UpdateCamera(camera);
346 mCameras.push_back(camera);
350 void SceneView::RemoveCamera(CameraActor camera)
352 if(camera == mDefaultCamera)
354 DALI_LOG_ERROR("Default Camera cannot removed.\n");
360 for(uint32_t i = 0; i < mCameras.size(); ++i)
362 if(mCameras[i] == camera)
364 mCameras.erase(mCameras.begin() + i);
369 if(mSelectedCamera == camera)
371 CameraActor newCurrentCamera = *mCameras.begin();
372 UpdateCamera(newCurrentCamera);
377 uint32_t SceneView::GetCameraCount() const
379 return mCameras.size();
382 CameraActor SceneView::GetSelectedCamera() const
384 return mSelectedCamera;
387 CameraActor SceneView::GetCamera(uint32_t index) const
389 if(index < mCameras.size())
391 return mCameras[index];
393 DALI_LOG_ERROR("Input index is out of bounds\n");
394 return CameraActor();
397 CameraActor SceneView::GetCamera(const std::string& name) const
399 CameraActor returnCamera;
400 for(auto&& camera : mCameras)
402 if(camera.GetProperty<std::string>(Actor::Property::NAME) == name)
404 returnCamera = camera;
411 void SceneView::SelectCamera(uint32_t index)
413 UpdateCamera(GetCamera(index));
416 void SceneView::SelectCamera(const std::string& name)
418 UpdateCamera(GetCamera(name));
421 void SceneView::RegisterSceneItem(Scene3D::Internal::LightObserver* item)
425 item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor, mSpecularMipmapLevels);
426 item->NotifyShadowMapTexture(mShadowTexture);
427 mItems.push_back(item);
431 void SceneView::UnregisterSceneItem(Scene3D::Internal::LightObserver* item)
435 for(uint32_t i = 0; i < mItems.size(); ++i)
437 if(mItems[i] == item)
439 mItems.erase(mItems.begin() + i);
446 void SceneView::SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor)
448 bool needIblReset = false;
449 bool isOnScene = Self().GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE);
450 if(mDiffuseIblUrl != diffuseUrl)
452 mDiffuseIblUrl = diffuseUrl;
453 if(mDiffuseIblUrl.empty())
459 mIblDiffuseDirty = true;
460 mIblDiffuseResourceReady = false;
464 if(mSpecularIblUrl != specularUrl)
466 mSpecularIblUrl = specularUrl;
467 if(mSpecularIblUrl.empty())
473 mIblSpecularDirty = true;
474 mIblSpecularResourceReady = false;
478 // If one or both of diffuse url and specular url are empty,
479 // we don't need to request to load texture.
482 if(mIblDiffuseLoadTask)
484 Dali::AsyncTaskManager::Get().RemoveTask(mIblDiffuseLoadTask);
485 mIblDiffuseLoadTask.Reset();
488 if(mIblSpecularLoadTask)
490 Dali::AsyncTaskManager::Get().RemoveTask(mIblSpecularLoadTask);
491 mIblSpecularLoadTask.Reset();
494 mIblDiffuseDirty = false;
495 mIblSpecularDirty = false;
496 mIblDiffuseResourceReady = true;
497 mIblSpecularResourceReady = true;
499 mDiffuseTexture.Reset();
500 mSpecularTexture.Reset();
502 mSpecularMipmapLevels = 1u;
503 NotifyImageBasedLightTextureChange();
507 if(isOnScene && mIblDiffuseDirty)
509 if(mIblDiffuseLoadTask)
511 Dali::AsyncTaskManager::Get().RemoveTask(mIblDiffuseLoadTask);
512 mIblDiffuseLoadTask.Reset();
514 mIblDiffuseLoadTask = new EnvironmentMapLoadTask(mDiffuseIblUrl, Scene3D::EnvironmentMapType::CUBEMAP, MakeCallback(this, &SceneView::OnIblDiffuseLoadComplete));
515 Dali::AsyncTaskManager::Get().AddTask(mIblDiffuseLoadTask);
516 mIblDiffuseDirty = false;
519 if(isOnScene && mIblSpecularDirty)
521 if(mIblSpecularLoadTask)
523 Dali::AsyncTaskManager::Get().RemoveTask(mIblSpecularLoadTask);
524 mIblSpecularLoadTask.Reset();
526 mIblSpecularLoadTask = new EnvironmentMapLoadTask(mSpecularIblUrl, Scene3D::EnvironmentMapType::CUBEMAP, MakeCallback(this, &SceneView::OnIblSpecularLoadComplete));
527 Dali::AsyncTaskManager::Get().AddTask(mIblSpecularLoadTask);
528 mIblSpecularDirty = false;
532 if(!Dali::Equals(mIblScaleFactor, scaleFactor))
534 SetImageBasedLightScaleFactor(scaleFactor);
537 // If diffuse and specular textures are already loaded, emits resource ready signal here.
538 if(IsResourceReady())
540 Control::SetResourceReady();
544 void SceneView::SetImageBasedLightScaleFactor(float scaleFactor)
546 mIblScaleFactor = scaleFactor;
547 for(auto&& item : mItems)
551 item->NotifyImageBasedLightScaleFactor(scaleFactor);
556 float SceneView::GetImageBasedLightScaleFactor() const
558 return mIblScaleFactor;
561 void SceneView::AddLight(Scene3D::Light light)
563 bool enabled = mShaderManager->AddLight(light);
564 mLights.push_back(std::make_pair(light, enabled));
566 if(light.IsShadowEnabled())
572 void SceneView::RemoveLight(Scene3D::Light light)
574 mShaderManager->RemoveLight(light);
575 for(uint32_t i = 0; i < mLights.size(); ++i)
577 if(mLights[i].first == light)
579 mLights.erase(mLights.begin() + i);
584 if(mLights.size() > mShaderManager->GetLightCount())
586 for(auto&& waitingLight : mLights)
588 if(waitingLight.second)
593 waitingLight.second = mShaderManager->AddLight(waitingLight.first);
598 if(light == mShadowLight)
605 for(auto&& lightEntity : mLights)
607 if(!lightEntity.second || !lightEntity.first.IsShadowEnabled())
611 SetShadow(lightEntity.first);
617 void SceneView::SetShadow(Scene3D::Light light)
624 auto foundLight = std::find_if(mLights.begin(), mLights.end(), [light](std::pair<Scene3D::Light, bool> lightEntity) -> bool { return (lightEntity.second && lightEntity.first == light); });
626 if(foundLight == mLights.end())
631 mShadowLight = light;
633 // Directional Light setting.
634 CameraActor lightCamera = GetImplementation(light).GetCamera();
635 CameraActor selectedCamera = GetSelectedCamera();
636 SetShadowLightConstraint(selectedCamera, lightCamera);
638 // make framebuffer for depth map and set it to render task.
639 Vector3 size = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
640 uint32_t shadowMapBufferSize = std::min(static_cast<uint32_t>(std::max(size.width, size.height)), MAXIMUM_SIZE_SHADOW_MAP);
641 UpdateShadowMapBuffer(shadowMapBufferSize);
643 // use lightCamera as a camera of shadow render task.
644 mShadowMapRenderTask.SetCameraActor(lightCamera);
646 mShaderManager->SetShadow(light);
649 void SceneView::RemoveShadow(Scene3D::Light light)
651 if(mShadowLight != light)
656 // remove all constraint from light camera
657 CameraActor lightCamera = GetImplementation(mShadowLight).GetCamera();
658 lightCamera.RemoveConstraints();
660 // reset framebuffer and remove it from render task.
661 mShadowFrameBuffer.Reset();
662 mShaderManager->RemoveShadow();
663 mShadowMapRenderTask.SetCameraActor(CameraActor());
665 mShadowLight.Reset();
667 mShadowTexture.Reset();
668 for(auto&& item : mItems)
672 item->NotifyShadowMapTexture(mShadowTexture);
676 for(auto&& lightEntity : mLights)
678 if(!lightEntity.second || !lightEntity.first.IsShadowEnabled())
682 SetShadow(lightEntity.first);
687 uint32_t SceneView::GetActivatedLightCount() const
689 return mShaderManager->GetLightCount();
692 void SceneView::UseFramebuffer(bool useFramebuffer)
694 if(mUseFrameBuffer != useFramebuffer)
696 mUseFrameBuffer = useFramebuffer;
701 bool SceneView::IsUsingFramebuffer() const
703 return mUseFrameBuffer;
706 void SceneView::SetFramebufferMultiSamplingLevel(uint8_t multiSamplingLevel)
708 if(mFrameBufferMultiSamplingLevel != multiSamplingLevel)
710 mFrameBufferMultiSamplingLevel = multiSamplingLevel;
712 // Create new framebuffer with changed multiSamplingLevel.
713 if(mRenderTask && mFrameBuffer && mTexture)
715 Vector3 size = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
717 mFrameBuffer = FrameBuffer::New(size.width, size.height, FrameBuffer::Attachment::DEPTH_STENCIL);
718 mFrameBuffer.AttachColorTexture(mTexture);
719 DevelFrameBuffer::SetMultiSamplingLevel(mFrameBuffer, mFrameBufferMultiSamplingLevel);
720 mRenderTask.SetFrameBuffer(mFrameBuffer);
722 // Note : we don't need to create new visual since visual's url is depend on mTexture.
727 uint8_t SceneView::GetFramebufferMultiSamplingLevel() const
729 return mFrameBufferMultiSamplingLevel;
732 void SceneView::SetSkybox(const std::string& skyboxUrl)
734 if(mSkyboxUrl != skyboxUrl)
736 UpdateSkybox(skyboxUrl, mSkyboxEnvironmentMapType);
740 void SceneView::SetSkyboxEnvironmentMapType(Scene3D::EnvironmentMapType skyboxEnvironmentMapType)
742 if(mSkyboxEnvironmentMapType != skyboxEnvironmentMapType)
744 UpdateSkybox(mSkyboxUrl, skyboxEnvironmentMapType);
748 void SceneView::SetSkyboxIntensity(float intensity)
750 mSkyboxIntensity = intensity;
753 DALI_LOG_ERROR("Intensity should be greater than or equal to 0.\n");
754 mSkyboxIntensity = 0.0f;
759 mSkybox.RegisterProperty(SKYBOX_INTENSITY_STRING.data(), mSkyboxIntensity);
763 float SceneView::GetSkyboxIntensity() const
765 return mSkyboxIntensity;
768 void SceneView::SetSkyboxOrientation(const Quaternion& orientation)
770 mSkyboxOrientation = orientation;
773 mSkybox.SetProperty(Dali::Actor::Property::ORIENTATION, orientation);
777 Quaternion SceneView::GetSkyboxOrientation() const
779 return mSkyboxOrientation;
782 Dali::Scene3D::Loader::ShaderManagerPtr SceneView::GetShaderManager() const
784 return mShaderManager;
787 void SceneView::UpdateShadowUniform(Scene3D::Light light)
789 mShaderManager->UpdateShadowUniform(light);
792 ///////////////////////////////////////////////////////////
797 void SceneView::OnSceneConnection(int depth)
799 // If diffuse and specular url is not valid, IBL does not need to be loaded.
800 if(!mDiffuseIblUrl.empty() && !mSpecularIblUrl.empty())
802 SetImageBasedLightSource(mDiffuseIblUrl, mSpecularIblUrl, mIblScaleFactor);
805 if(!mSkyboxUrl.empty())
807 UpdateSkybox(mSkyboxUrl, mSkyboxEnvironmentMapType);
810 Window window = DevelWindow::Get(Self());
813 // Only for on-screen window
814 window.ResizeSignal().Connect(this, &SceneView::OnWindowResized);
817 mWindowOrientation = DevelWindow::GetPhysicalOrientation(window);
820 // On-screen / Off-screen window
821 mSceneHolder = Integration::SceneHolder::Get(Self());
824 RenderTaskList taskList = mSceneHolder.GetRenderTaskList();
825 mShadowMapRenderTask = taskList.CreateTask();
826 mShadowMapRenderTask.SetSourceActor(mRootLayer);
827 mShadowMapRenderTask.SetExclusive(true);
828 mShadowMapRenderTask.SetInputEnabled(false);
829 mShadowMapRenderTask.SetCullMode(false);
830 mShadowMapRenderTask.SetClearEnabled(true);
831 mShadowMapRenderTask.SetClearColor(Color::WHITE);
832 mShadowMapRenderTask.SetRenderPassTag(10);
833 mShadowMapRenderTask.SetCameraActor(CameraActor());
835 mRenderTask = taskList.CreateTask();
836 mRenderTask.SetSourceActor(mRootLayer);
837 mRenderTask.SetExclusive(true);
838 mRenderTask.SetInputEnabled(true);
839 mRenderTask.SetCullMode(false);
840 mRenderTask.SetScreenToFrameBufferMappingActor(Self());
845 Control::OnSceneConnection(depth);
848 void SceneView::OnSceneDisconnection()
852 Window window = mWindow.GetHandle();
855 window.ResizeSignal().Disconnect(this, &SceneView::OnWindowResized);
863 RenderTaskList taskList = mSceneHolder.GetRenderTaskList();
864 taskList.RemoveTask(mRenderTask);
867 if(mShadowMapRenderTask)
869 RenderTaskList taskList = mSceneHolder.GetRenderTaskList();
870 taskList.RemoveTask(mShadowMapRenderTask);
871 mShadowMapRenderTask.Reset();
873 mSceneHolder.Reset();
875 mFrameBuffer.Reset();
876 mShadowFrameBuffer.Reset();
878 Control::OnSceneDisconnection();
881 void SceneView::OnInitialize()
884 mRootLayer = Layer::New();
885 mRootLayer.SetProperty(Layer::Property::BEHAVIOR, Layer::LAYER_3D);
886 mRootLayer.SetProperty(Layer::Property::DEPTH_TEST, true);
887 // The models in the SceneView should be have independent coordinate with DALi default coordinate.
888 mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_POSITION, false);
889 mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_ORIENTATION, false);
890 mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_SCALE, false);
891 self.Add(mRootLayer);
893 mDefaultCamera = Dali::CameraActor::New3DCamera();
894 mDefaultCamera.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
895 mDefaultCamera.SetProperty(Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
896 AddCamera(mDefaultCamera);
897 UpdateCamera(mDefaultCamera);
900 void SceneView::OnChildAdd(Actor& child)
902 if(child != mRootLayer)
904 mRootLayer.Add(child);
906 Control::OnChildAdd(child);
909 void SceneView::OnChildRemove(Actor& child)
911 mRootLayer.Remove(child);
912 Control::OnChildRemove(child);
915 float SceneView::GetHeightForWidth(float width)
918 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
919 return Control::GetHeightForWidth(width) + padding.top + padding.bottom;
922 float SceneView::GetWidthForHeight(float height)
925 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
926 return Control::GetWidthForHeight(height) + padding.start + padding.end;
929 void SceneView::OnRelayout(const Vector2& size, RelayoutContainer& container)
931 Control::OnRelayout(size, container);
932 // Change canvas size of camera actor.
936 bool SceneView::IsResourceReady() const
938 return mIblDiffuseResourceReady && mIblSpecularResourceReady && mSkyboxResourceReady;
941 void SceneView::UpdateCamera(CameraActor camera)
945 if(mSelectedCamera && mSelectedCamera.GetParent())
947 mSelectedCamera.Unparent();
949 mRootLayer.Add(camera);
952 mSelectedCamera = camera;
955 SetShadowLightConstraint(mSelectedCamera, GetImplementation(mShadowLight).GetCamera());
960 void SceneView::UpdateRenderTask()
964 if(mSelectedCamera != mRenderTask.GetCameraActor())
966 mRenderTask.SetCameraActor(mSelectedCamera);
969 Vector3 size = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
970 const float aspectRatio = size.width / size.height;
971 mSelectedCamera.SetAspectRatio(aspectRatio);
973 uint32_t shadowMapBufferSize = std::min(static_cast<uint32_t>(std::max(size.width, size.height)), MAXIMUM_SIZE_SHADOW_MAP);
974 UpdateShadowMapBuffer(shadowMapBufferSize);
978 Dali::FrameBuffer currentFrameBuffer = mRenderTask.GetFrameBuffer();
979 if(!currentFrameBuffer ||
980 !Dali::Equals(currentFrameBuffer.GetColorTexture().GetWidth(), size.width) ||
981 !Dali::Equals(currentFrameBuffer.GetColorTexture().GetHeight(), size.height))
983 mRootLayer.SetProperty(Dali::Actor::Property::COLOR_MODE, ColorMode::USE_OWN_COLOR);
984 mRenderTask.ResetViewportGuideActor();
985 mRenderTask.SetViewport(Dali::Viewport(Vector4::ZERO));
987 // create offscreen buffer of new size to render our child actors to
988 mTexture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, unsigned(size.width), unsigned(size.height));
989 mFrameBuffer = FrameBuffer::New(size.width, size.height, FrameBuffer::Attachment::DEPTH_STENCIL);
990 mFrameBuffer.AttachColorTexture(mTexture);
991 DevelFrameBuffer::SetMultiSamplingLevel(mFrameBuffer, mFrameBufferMultiSamplingLevel);
992 Dali::Toolkit::ImageUrl imageUrl = Dali::Toolkit::Image::GenerateUrl(mFrameBuffer, 0u);
994 Property::Map imagePropertyMap;
995 imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
996 imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, imageUrl.GetUrl());
997 // To flip rendered scene without CameraActor::SetInvertYAxis() to avoid backface culling.
998 imagePropertyMap.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, Vector4(0.0f, 1.0f, 1.0f, -1.0f));
999 mVisual = Toolkit::VisualFactory::Get().CreateVisual(imagePropertyMap);
1001 Toolkit::DevelControl::RegisterVisual(*this, RENDERING_BUFFER, mVisual);
1003 mRenderTask.SetFrameBuffer(mFrameBuffer);
1004 mRenderTask.SetClearEnabled(true);
1005 mRenderTask.SetClearColor(Color::TRANSPARENT);
1010 mRenderTask.SetViewportGuideActor(Self());
1011 if(mRenderTask.GetFrameBuffer())
1013 mRootLayer.SetProperty(Dali::Actor::Property::COLOR_MODE, ColorMode::USE_OWN_MULTIPLY_PARENT_ALPHA);
1014 FrameBuffer framebuffer;
1015 mRenderTask.SetFrameBuffer(framebuffer);
1016 mRenderTask.SetClearEnabled(false);
1018 Toolkit::DevelControl::UnregisterVisual(*this, RENDERING_BUFFER);
1021 mFrameBuffer.Reset();
1030 void SceneView::OnWindowResized(Window window, Window::WindowSize size)
1032 mWindowOrientation = DevelWindow::GetPhysicalOrientation(window);
1036 void SceneView::RotateCamera()
1040 DevelCameraActor::RotateProjection(mSelectedCamera, DEFAULT_ORIENTATION);
1044 DevelCameraActor::RotateProjection(mSelectedCamera, mWindowOrientation);
1048 void SceneView::UpdateSkybox(const std::string& skyboxUrl, Scene3D::EnvironmentMapType skyboxEnvironmentMapType)
1050 bool isOnScene = Self().GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE);
1051 if(mSkyboxUrl != skyboxUrl || mSkyboxEnvironmentMapType != skyboxEnvironmentMapType)
1053 mSkyboxDirty = true;
1054 mSkyboxResourceReady = false;
1055 mSkyboxUrl = skyboxUrl;
1056 mSkyboxEnvironmentMapType = skyboxEnvironmentMapType;
1059 if(mSkyboxUrl.empty())
1063 Dali::AsyncTaskManager::Get().RemoveTask(mSkyboxLoadTask);
1064 mSkyboxLoadTask.Reset();
1071 mSkyboxTexture.Reset();
1074 mSkyboxDirty = false;
1075 mSkyboxResourceReady = true;
1079 if(isOnScene && mSkyboxDirty)
1083 Dali::AsyncTaskManager::Get().RemoveTask(mSkyboxLoadTask);
1084 mSkyboxLoadTask.Reset();
1087 mSkyboxLoadTask = new EnvironmentMapLoadTask(mSkyboxUrl, mSkyboxEnvironmentMapType, MakeCallback(this, &SceneView::OnSkyboxLoadComplete));
1088 Dali::AsyncTaskManager::Get().AddTask(mSkyboxLoadTask);
1089 mSkyboxDirty = false;
1093 if(IsResourceReady())
1095 Control::SetResourceReady();
1099 void SceneView::OnSkyboxLoadComplete()
1103 mSkybox = CreateSkybox();
1104 SetSkyboxIntensity(mSkyboxIntensity);
1105 SetSkyboxOrientation(mSkyboxOrientation);
1108 mRootLayer.Add(mSkybox);
1112 mSkyboxResourceReady = true;
1113 mSkyboxTexture = mSkyboxLoadTask->GetLoadedTexture();
1114 Shader skyboxShader;
1115 if(mSkyboxLoadTask->GetEnvironmentMapType() == Scene3D::EnvironmentMapType::CUBEMAP)
1117 skyboxShader = Shader::New(SHADER_SKYBOX_SHADER_VERT.data(), SHADER_SKYBOX_SHADER_FRAG.data());
1121 skyboxShader = Shader::New(SHADER_SKYBOX_SHADER_VERT.data(), SHADER_SKYBOX_EQUIRECTANGULAR_SHADER_FRAG.data());
1124 Renderer skyboxRenderer = (mSkybox.GetRendererCount() > 0u) ? mSkybox.GetRendererAt(0u) : Renderer();
1127 Dali::TextureSet skyboxTextures = TextureSet::New();
1128 skyboxTextures.SetTexture(0, mSkyboxTexture);
1129 skyboxRenderer.SetTextures(skyboxTextures);
1130 skyboxRenderer.SetShader(skyboxShader);
1133 mSkyboxLoadTask.Reset();
1135 if(IsResourceReady())
1137 Control::SetResourceReady();
1141 void SceneView::OnIblDiffuseLoadComplete()
1143 mDiffuseTexture = mIblDiffuseLoadTask->GetLoadedTexture();
1144 mIblDiffuseResourceReady = true;
1145 mIblDiffuseLoadTask.Reset();
1146 if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
1148 OnIblLoadComplete();
1152 void SceneView::OnIblSpecularLoadComplete()
1154 mSpecularTexture = mIblSpecularLoadTask->GetLoadedTexture();
1155 mSpecularMipmapLevels = mIblSpecularLoadTask->GetMipmapLevels();
1156 mIblSpecularResourceReady = true;
1157 mIblSpecularLoadTask.Reset();
1158 if(mIblDiffuseResourceReady && mIblSpecularResourceReady)
1160 OnIblLoadComplete();
1164 void SceneView::OnIblLoadComplete()
1166 NotifyImageBasedLightTextureChange();
1167 if(IsResourceReady())
1169 Control::SetResourceReady();
1173 void SceneView::NotifyImageBasedLightTextureChange()
1175 for(auto&& item : mItems)
1179 item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor, mSpecularMipmapLevels);
1184 void SceneView::UpdateShadowMapBuffer(uint32_t shadowMapSize)
1186 Dali::FrameBuffer currentShadowFrameBuffer = mShadowMapRenderTask.GetFrameBuffer();
1188 (!currentShadowFrameBuffer ||
1190 !Dali::Equals(DevelFrameBuffer::GetDepthTexture(currentShadowFrameBuffer).GetWidth(), shadowMapSize)))
1192 mShadowFrameBuffer.Reset();
1193 mShadowTexture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::DEPTH_UNSIGNED_INT, shadowMapSize, shadowMapSize);
1194 mShadowFrameBuffer = FrameBuffer::New(shadowMapSize, shadowMapSize, FrameBuffer::Attachment::NONE);
1195 DevelFrameBuffer::AttachDepthTexture(mShadowFrameBuffer, mShadowTexture);
1196 mShadowMapRenderTask.SetFrameBuffer(mShadowFrameBuffer);
1198 for(auto&& item : mItems)
1202 item->NotifyShadowMapTexture(mShadowTexture);
1208 } // namespace Internal
1209 } // namespace Scene3D