2 * Copyright (c) 2022 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/integration-api/debug.h>
32 #include <dali/public-api/object/type-registry-helper.h>
33 #include <dali/public-api/object/type-registry.h>
34 #include <string_view>
37 #include <dali-scene3d/internal/controls/model/model-impl.h>
38 #include <dali-scene3d/internal/graphics/builtin-shader-extern-gen.h>
39 #include <dali-scene3d/public-api/loader/cube-map-loader.h>
41 #include <dali/integration-api/debug.h>
55 return Scene3D::SceneView::New();
58 // Setup properties, signals and actions using the type-registry.
59 DALI_TYPE_REGISTRATION_BEGIN(Scene3D::SceneView, Toolkit::Control, Create);
60 DALI_TYPE_REGISTRATION_END()
62 Property::Index RENDERING_BUFFER = Dali::Toolkit::Control::CONTROL_PROPERTY_END_INDEX + 1;
63 constexpr int32_t DEFAULT_ORIENTATION = 0;
65 static constexpr std::string_view SKYBOX_INTENSITY_STRING = "uIntensity";
67 Dali::Actor CreateSkybox(const std::string& skyboxUrl)
74 Vertex skyboxVertices[] = {
76 {Vector3(-1.0f, 1.0f, -1.0f)},
77 {Vector3(-1.0f, -1.0f, -1.0f)},
78 {Vector3(1.0f, -1.0f, -1.0f)},
79 {Vector3(1.0f, -1.0f, -1.0f)},
80 {Vector3(1.0f, 1.0f, -1.0f)},
81 {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)},
87 {Vector3(-1.0f, 1.0f, -1.0f)},
88 {Vector3(-1.0f, 1.0f, 1.0f)},
89 {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)},
95 {Vector3(1.0f, 1.0f, 1.0f)},
96 {Vector3(1.0f, 1.0f, -1.0f)},
97 {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)},
103 {Vector3(1.0f, 1.0f, 1.0f)},
104 {Vector3(1.0f, -1.0f, 1.0f)},
105 {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)},
111 {Vector3(1.0f, 1.0f, 1.0f)},
112 {Vector3(-1.0f, 1.0f, 1.0f)},
113 {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)},
119 {Vector3(1.0f, -1.0f, -1.0f)},
120 {Vector3(-1.0f, -1.0f, 1.0f)},
121 {Vector3(1.0f, -1.0f, 1.0f)}};
123 Dali::Shader shaderSkybox = Shader::New(SHADER_SKYBOX_SHADER_VERT.data(), SHADER_SKYBOX_SHADER_FRAG.data());
124 Dali::VertexBuffer vertexBuffer = Dali::VertexBuffer::New(Property::Map().Add("aPosition", Property::VECTOR3));
125 vertexBuffer.SetData(skyboxVertices, sizeof(skyboxVertices) / sizeof(Vertex));
127 Dali::Geometry skyboxGeometry = Geometry::New();
128 skyboxGeometry.AddVertexBuffer(vertexBuffer);
129 skyboxGeometry.SetType(Geometry::TRIANGLES);
131 Dali::Texture skyboxTexture = Dali::Scene3D::Loader::LoadCubeMap(skyboxUrl);
132 Dali::TextureSet skyboxTextures = TextureSet::New();
133 skyboxTextures.SetTexture(0, skyboxTexture);
135 Dali::Renderer skyboxRenderer = Renderer::New(skyboxGeometry, shaderSkybox);
136 skyboxRenderer.SetTextures(skyboxTextures);
137 skyboxRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, 2.0f);
138 // Enables the depth test.
139 skyboxRenderer.SetProperty(Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON);
140 // The fragment shader will run only is those pixels that have the max depth value.
141 skyboxRenderer.SetProperty(Renderer::Property::DEPTH_FUNCTION, DepthFunction::LESS_EQUAL);
143 Dali::Actor skyboxActor = Actor::New();
144 skyboxActor.SetProperty(Dali::Actor::Property::NAME, "SkyBox");
145 skyboxActor.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
146 skyboxActor.SetProperty(Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
147 skyboxActor.AddRenderer(skyboxRenderer);
151 } // anonymous namespace
153 SceneView::SceneView()
154 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
155 mWindowOrientation(DEFAULT_ORIENTATION),
157 mSkyboxOrientation(Quaternion()),
158 mSkyboxIntensity(1.0f)
162 SceneView::~SceneView() = default;
164 Dali::Scene3D::SceneView SceneView::New()
166 SceneView* impl = new SceneView();
168 Dali::Scene3D::SceneView handle = Dali::Scene3D::SceneView(*impl);
170 // Second-phase init of the implementation
171 // This can only be done after the CustomActor connection has been made...
177 void SceneView::AddCamera(CameraActor camera)
183 UpdateCamera(camera);
185 mCameras.push_back(camera);
189 void SceneView::RemoveCamera(CameraActor camera)
191 if(camera == mDefaultCamera)
193 DALI_LOG_ERROR("Default Camera cannot removed.\n");
199 for(uint32_t i = 0; i < mCameras.size(); ++i)
201 if(mCameras[i] == camera)
203 mCameras.erase(mCameras.begin() + i);
208 if(mSelectedCamera == camera)
210 CameraActor newCurrentCamera = *mCameras.begin();
211 UpdateCamera(newCurrentCamera);
216 uint32_t SceneView::GetCameraCount() const
218 return mCameras.size();
221 CameraActor SceneView::GetSelectedCamera() const
223 return mSelectedCamera;
226 CameraActor SceneView::GetCamera(uint32_t index) const
228 if(index < mCameras.size())
230 return mCameras[index];
232 DALI_LOG_ERROR("Input index is out of bounds\n");
233 return CameraActor();
236 CameraActor SceneView::GetCamera(const std::string& name) const
238 CameraActor returnCamera;
239 for(auto&& camera : mCameras)
241 if(camera.GetProperty<std::string>(Actor::Property::NAME) == name)
243 returnCamera = camera;
250 void SceneView::SelectCamera(uint32_t index)
252 UpdateCamera(GetCamera(index));
255 void SceneView::SelectCamera(const std::string& name)
257 UpdateCamera(GetCamera(name));
260 void SceneView::RegisterModel(Scene3D::Model model)
264 model.SetImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
265 mModels.push_back(model);
269 void SceneView::UnregisterModel(Scene3D::Model model)
273 for(uint32_t i = 0; i < mModels.size(); ++i)
275 if(mModels[i] == model)
277 mModels.erase(mModels.begin() + i);
284 void SceneView::SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor)
286 mIBLResourceReady = false;
287 Texture diffuseTexture = Dali::Scene3D::Loader::LoadCubeMap(diffuseUrl);
290 Texture specularTexture = Dali::Scene3D::Loader::LoadCubeMap(specularUrl);
293 mDiffuseTexture = diffuseTexture;
294 mSpecularTexture = specularTexture;
295 mIblScaleFactor = scaleFactor;
297 for(auto&& model : mModels)
301 model.SetImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
306 mIBLResourceReady = true;
307 if(IsResourceReady())
309 Control::SetResourceReady(false);
313 void SceneView::SetImageBasedLightScaleFactor(float scaleFactor)
315 mIblScaleFactor = scaleFactor;
316 for(auto&& model : mModels)
320 model.SetImageBasedLightScaleFactor(scaleFactor);
325 float SceneView::GetImageBasedLightScaleFactor() const
327 return mIblScaleFactor;
330 void SceneView::UseFramebuffer(bool useFramebuffer)
332 if(mUseFrameBuffer != useFramebuffer)
334 mUseFrameBuffer = useFramebuffer;
339 bool SceneView::IsUsingFramebuffer() const
341 return mUseFrameBuffer;
344 void SceneView::SetSkybox(const std::string& skyboxUrl)
346 mSkyboxResourceReady = false;
352 mSkybox = CreateSkybox(skyboxUrl);
353 SetSkyboxIntensity(mSkyboxIntensity);
354 SetSkyboxOrientation(mSkyboxOrientation);
357 mRootLayer.Add(mSkybox);
360 mSkyboxResourceReady = true;
361 if(IsResourceReady())
363 Control::SetResourceReady(false);
367 void SceneView::SetSkyboxIntensity(float intensity)
369 mSkyboxIntensity = intensity;
372 DALI_LOG_ERROR("Intensity should be greater than or equal to 0.\n");
373 mSkyboxIntensity = 0.0f;
378 mSkybox.RegisterProperty(SKYBOX_INTENSITY_STRING.data(), mSkyboxIntensity);
382 float SceneView::GetSkyboxIntensity() const
384 return mSkyboxIntensity;
387 void SceneView::SetSkyboxOrientation(const Quaternion& orientation)
389 mSkyboxOrientation = orientation;
392 mSkybox.SetProperty(Dali::Actor::Property::ORIENTATION, orientation);
396 Quaternion SceneView::GetSkyboxOrientation() const
398 return mSkyboxOrientation;
401 ///////////////////////////////////////////////////////////
406 void SceneView::OnSceneConnection(int depth)
410 Window window = DevelWindow::Get(Self());
413 window.ResizeSignal().Connect(this, &SceneView::OnWindowResized);
416 Control::OnSceneConnection(depth);
419 void SceneView::OnSceneDisconnection()
423 Window window = DevelWindow::Get(Self());
426 window.ResizeSignal().Disconnect(this, &SceneView::OnWindowResized);
429 Control::OnSceneDisconnection();
432 void SceneView::OnInitialize()
435 mRootLayer = Layer::New();
436 mRootLayer.SetProperty(Layer::Property::BEHAVIOR, Layer::LAYER_3D);
437 mRootLayer.SetProperty(Layer::Property::DEPTH_TEST, true);
438 // The models in the SceneView should be have independent coordinate with DALi default coordinate.
439 mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_POSITION, false);
440 mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_ORIENTATION, false);
441 mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_SCALE, false);
442 self.Add(mRootLayer);
444 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
445 mRenderTask = taskList.CreateTask();
446 mRenderTask.SetSourceActor(mRootLayer);
447 mRenderTask.SetExclusive(true);
448 mRenderTask.SetInputEnabled(true);
449 mRenderTask.SetCullMode(false);
450 mRenderTask.SetScreenToFrameBufferMappingActor(Self());
452 mDefaultCamera = Dali::CameraActor::New();
453 mDefaultCamera.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
454 mDefaultCamera.SetProperty(Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
455 mDefaultCamera.SetNearClippingPlane(1.0f);
456 AddCamera(mDefaultCamera);
457 UpdateCamera(mDefaultCamera);
460 void SceneView::OnChildAdd(Actor& child)
462 if(child != mRootLayer)
464 mRootLayer.Add(child);
466 Control::OnChildAdd(child);
469 void SceneView::OnChildRemove(Actor& child)
471 mRootLayer.Remove(child);
472 Control::OnChildRemove(child);
475 float SceneView::GetHeightForWidth(float width)
478 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
479 return Control::GetHeightForWidth(width) + padding.top + padding.bottom;
482 float SceneView::GetWidthForHeight(float height)
485 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
486 return Control::GetWidthForHeight(height) + padding.start + padding.end;
489 void SceneView::OnRelayout(const Vector2& size, RelayoutContainer& container)
491 Control::OnRelayout(size, container);
492 // Change canvas size of camera actor.
496 bool SceneView::IsResourceReady() const
498 return mIBLResourceReady & mSkyboxResourceReady;
501 void SceneView::UpdateCamera(CameraActor camera)
505 if(mSelectedCamera && mSelectedCamera.GetParent())
507 mSelectedCamera.Unparent();
509 mRootLayer.Add(camera);
512 mSelectedCamera = camera;
516 void SceneView::UpdateRenderTask()
520 if(mSelectedCamera != mRenderTask.GetCameraActor())
522 mRenderTask.SetCameraActor(mSelectedCamera);
525 Vector3 size = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
526 const float aspectRatio = size.width / size.height;
527 mSelectedCamera.SetAspectRatio(aspectRatio);
528 const bool projectionVertical = mSelectedCamera.GetProperty<int>(Dali::DevelCameraActor::Property::PROJECTION_DIRECTION) == Dali::DevelCameraActor::VERTICAL;
530 // if projectionVertical, Top / Bottom is +-ve to keep consistency with orthographic values
531 // else, Left / Right is +-ve to keep consistency with orthographic values
532 const float orthographicSize = DALI_LIKELY(projectionVertical) ? mSelectedCamera[Dali::CameraActor::Property::TOP_PLANE_DISTANCE] : mSelectedCamera[Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE];
533 const float halfHeight = DALI_LIKELY(projectionVertical) ? orthographicSize : orthographicSize / aspectRatio;
534 const float halfWidth = DALI_LIKELY(projectionVertical) ? orthographicSize * aspectRatio : orthographicSize;
536 mSelectedCamera[Dali::CameraActor::Property::LEFT_PLANE_DISTANCE] = -halfWidth;
537 mSelectedCamera[Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE] = halfWidth;
538 mSelectedCamera[Dali::CameraActor::Property::TOP_PLANE_DISTANCE] = halfHeight;
539 mSelectedCamera[Dali::CameraActor::Property::BOTTOM_PLANE_DISTANCE] = -halfHeight;
543 Dali::FrameBuffer currentFrameBuffer = mRenderTask.GetFrameBuffer();
544 if(!currentFrameBuffer ||
545 currentFrameBuffer.GetColorTexture().GetWidth() != size.width ||
546 currentFrameBuffer.GetColorTexture().GetHeight() != size.height)
548 mRenderTask.ResetViewportGuideActor();
549 mRenderTask.SetViewport(Dali::Viewport(Vector4::ZERO));
551 // create offscreen buffer of new size to render our child actors to
552 mTexture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, unsigned(size.width), unsigned(size.height));
553 mRenderTarget = FrameBuffer::New(size.width, size.height, FrameBuffer::Attachment::DEPTH_STENCIL);
554 mRenderTarget.AttachColorTexture(mTexture);
555 Dali::Toolkit::ImageUrl imageUrl = Dali::Toolkit::Image::GenerateUrl(mRenderTarget, 0u);
557 Property::Map imagePropertyMap;
558 imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
559 imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, imageUrl.GetUrl());
560 // To flip rendered scene without CameraActor::SetInvertYAxis() to avoid backface culling.
561 imagePropertyMap.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, Vector4(0.0f, 1.0f, 1.0f, -1.0f));
562 mVisual = Toolkit::VisualFactory::Get().CreateVisual(imagePropertyMap);
564 Toolkit::DevelControl::RegisterVisual(*this, RENDERING_BUFFER, mVisual);
566 mRenderTask.SetFrameBuffer(mRenderTarget);
567 mRenderTask.SetClearEnabled(true);
568 mRenderTask.SetClearColor(Color::TRANSPARENT);
573 mRenderTask.SetViewportGuideActor(Self());
574 if(mRenderTask.GetFrameBuffer())
576 FrameBuffer framebuffer;
577 mRenderTask.SetFrameBuffer(framebuffer);
578 mRenderTask.SetClearEnabled(false);
580 Toolkit::DevelControl::UnregisterVisual(*this, RENDERING_BUFFER);
583 mRenderTarget.Reset();
592 void SceneView::OnWindowResized(Window window, Window::WindowSize size)
594 mWindowOrientation = DevelWindow::GetPhysicalOrientation(window);
598 void SceneView::RotateCamera()
602 DevelCameraActor::RotateProjection(mSelectedCamera, DEFAULT_ORIENTATION);
606 DevelCameraActor::RotateProjection(mSelectedCamera, mWindowOrientation);
610 } // namespace Internal
611 } // namespace Scene3D