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/adaptor-framework/adaptor.h>
32 #include <dali/devel-api/rendering/frame-buffer-devel.h>
33 #include <dali/integration-api/debug.h>
34 #include <dali/public-api/object/type-registry-helper.h>
35 #include <dali/public-api/object/type-registry.h>
38 #include <dali-scene3d/internal/controls/model/model-impl.h>
39 #include <dali-scene3d/public-api/loader/cube-map-loader.h>
53 return Scene3D::SceneView::New();
56 // Setup properties, signals and actions using the type-registry.
57 DALI_TYPE_REGISTRATION_BEGIN(Scene3D::SceneView, Toolkit::Control, Create);
58 DALI_TYPE_REGISTRATION_END()
60 Property::Index RENDERING_BUFFER = Dali::Toolkit::Control::CONTROL_PROPERTY_END_INDEX + 1;
61 constexpr int32_t DEFAULT_ORIENTATION = 0;
63 constexpr uint8_t DEFAULT_FRAME_BUFFER_MULTI_SAMPLING_LEVEL = 4u;
64 } // anonymous namespace
66 SceneView::SceneView()
67 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
68 mWindowOrientation(DEFAULT_ORIENTATION),
69 mIblLoadedCallback(nullptr)
73 SceneView::~SceneView()
75 if(mIblLoadedCallback && Adaptor::IsAvailable())
77 // Removes the callback from the callback manager in case the control is destroyed before the callback is executed.
78 Adaptor::Get().RemoveIdle(mIblLoadedCallback);
81 if(Stage::IsInstalled())
85 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
86 taskList.RemoveTask(mRenderTask);
92 Dali::Scene3D::SceneView SceneView::New()
94 SceneView* impl = new SceneView();
96 Dali::Scene3D::SceneView handle = Dali::Scene3D::SceneView(*impl);
98 // Second-phase init of the implementation
99 // This can only be done after the CustomActor connection has been made...
105 void SceneView::AddCamera(CameraActor camera)
111 UpdateCamera(camera);
113 mCameras.push_back(camera);
117 void SceneView::RemoveCamera(CameraActor camera)
119 if(camera == mDefaultCamera)
121 DALI_LOG_ERROR("Default Camera cannot removed.\n");
127 for(uint32_t i = 0; i < mCameras.size(); ++i)
129 if(mCameras[i] == camera)
131 mCameras.erase(mCameras.begin() + i);
136 if(mSelectedCamera == camera)
138 CameraActor newCurrentCamera = *mCameras.begin();
139 UpdateCamera(newCurrentCamera);
144 uint32_t SceneView::GetCameraCount() const
146 return mCameras.size();
149 CameraActor SceneView::GetSelectedCamera() const
151 return mSelectedCamera;
154 CameraActor SceneView::GetCamera(uint32_t index) const
156 if(index < mCameras.size())
158 return mCameras[index];
160 DALI_LOG_ERROR("Input index is out of bounds\n");
161 return CameraActor();
164 CameraActor SceneView::GetCamera(const std::string& name) const
166 CameraActor returnCamera;
167 for(auto&& camera : mCameras)
169 if(camera.GetProperty<std::string>(Actor::Property::NAME) == name)
171 returnCamera = camera;
178 void SceneView::SelectCamera(uint32_t index)
180 UpdateCamera(GetCamera(index));
183 void SceneView::SelectCamera(const std::string& name)
185 UpdateCamera(GetCamera(name));
188 void SceneView::RegisterSceneItem(Scene3D::Internal::ImageBasedLightObserver* item)
192 item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
193 mItems.push_back(item);
197 void SceneView::UnregisterSceneItem(Scene3D::Internal::ImageBasedLightObserver* item)
201 for(uint32_t i = 0; i < mItems.size(); ++i)
203 if(mItems[i] == item)
205 mItems.erase(mItems.begin() + i);
212 void SceneView::SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor)
214 // Request asynchronous model loading
215 if(!mIblLoadedCallback)
217 mIBLResourceReady = false;
218 mDiffuseIblUrl = diffuseUrl;
219 mSpecularIblUrl = specularUrl;
220 mIblScaleFactor = scaleFactor;
221 // The callback manager takes the ownership of the callback object.
222 mIblLoadedCallback = MakeCallback(this, &SceneView::OnLoadComplete);
223 Adaptor::Get().AddIdle(mIblLoadedCallback, false);
227 void SceneView::SetImageBasedLightScaleFactor(float scaleFactor)
229 mIblScaleFactor = scaleFactor;
230 for(auto&& item : mItems)
234 item->NotifyImageBasedLightScaleFactor(scaleFactor);
239 float SceneView::GetImageBasedLightScaleFactor() const
241 return mIblScaleFactor;
244 void SceneView::UseFramebuffer(bool useFramebuffer)
246 if(mUseFrameBuffer != useFramebuffer)
248 mUseFrameBuffer = useFramebuffer;
253 bool SceneView::IsUsingFramebuffer() const
255 return mUseFrameBuffer;
258 ///////////////////////////////////////////////////////////
263 void SceneView::OnSceneConnection(int depth)
267 Window window = DevelWindow::Get(Self());
270 window.ResizeSignal().Connect(this, &SceneView::OnWindowResized);
273 Control::OnSceneConnection(depth);
276 void SceneView::OnSceneDisconnection()
280 Window window = DevelWindow::Get(Self());
283 window.ResizeSignal().Disconnect(this, &SceneView::OnWindowResized);
286 Control::OnSceneDisconnection();
289 void SceneView::OnInitialize()
292 mRootLayer = Layer::New();
293 mRootLayer.SetProperty(Layer::Property::BEHAVIOR, Layer::LAYER_3D);
294 mRootLayer.SetProperty(Layer::Property::DEPTH_TEST, true);
295 // The models in the SceneView should be have independent coordinate with DALi default coordinate.
296 mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_POSITION, false);
297 mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_ORIENTATION, false);
298 mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_SCALE, false);
299 self.Add(mRootLayer);
301 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
302 mRenderTask = taskList.CreateTask();
303 mRenderTask.SetSourceActor(mRootLayer);
304 mRenderTask.SetExclusive(true);
305 mRenderTask.SetInputEnabled(true);
306 mRenderTask.SetCullMode(false);
307 mRenderTask.SetScreenToFrameBufferMappingActor(Self());
309 mDefaultCamera = Dali::CameraActor::New();
310 mDefaultCamera.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
311 mDefaultCamera.SetProperty(Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
312 mDefaultCamera.SetNearClippingPlane(1.0f);
313 AddCamera(mDefaultCamera);
314 UpdateCamera(mDefaultCamera);
317 void SceneView::OnChildAdd(Actor& child)
319 if(child != mRootLayer)
321 mRootLayer.Add(child);
323 Control::OnChildAdd(child);
326 void SceneView::OnChildRemove(Actor& child)
328 mRootLayer.Remove(child);
329 Control::OnChildRemove(child);
332 float SceneView::GetHeightForWidth(float width)
335 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
336 return Control::GetHeightForWidth(width) + padding.top + padding.bottom;
339 float SceneView::GetWidthForHeight(float height)
342 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
343 return Control::GetWidthForHeight(height) + padding.start + padding.end;
346 void SceneView::OnRelayout(const Vector2& size, RelayoutContainer& container)
348 Control::OnRelayout(size, container);
349 // Change canvas size of camera actor.
353 bool SceneView::IsResourceReady() const
355 return mIBLResourceReady;
358 void SceneView::UpdateCamera(CameraActor camera)
362 if(mSelectedCamera && mSelectedCamera.GetParent())
364 mSelectedCamera.Unparent();
366 mRootLayer.Add(camera);
369 mSelectedCamera = camera;
373 void SceneView::UpdateRenderTask()
377 if(mSelectedCamera != mRenderTask.GetCameraActor())
379 mRenderTask.SetCameraActor(mSelectedCamera);
382 Vector3 size = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
383 const float aspectRatio = size.width / size.height;
384 mSelectedCamera.SetAspectRatio(aspectRatio);
385 const bool projectionVertical = mSelectedCamera.GetProperty<int>(Dali::DevelCameraActor::Property::PROJECTION_DIRECTION) == Dali::DevelCameraActor::VERTICAL;
387 // if projectionVertical, Top / Bottom is +-ve to keep consistency with orthographic values
388 // else, Left / Right is +-ve to keep consistency with orthographic values
389 const float orthographicSize = DALI_LIKELY(projectionVertical) ? mSelectedCamera[Dali::CameraActor::Property::TOP_PLANE_DISTANCE] : mSelectedCamera[Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE];
390 const float halfHeight = DALI_LIKELY(projectionVertical) ? orthographicSize : orthographicSize / aspectRatio;
391 const float halfWidth = DALI_LIKELY(projectionVertical) ? orthographicSize * aspectRatio : orthographicSize;
393 mSelectedCamera[Dali::CameraActor::Property::LEFT_PLANE_DISTANCE] = -halfWidth;
394 mSelectedCamera[Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE] = halfWidth;
395 mSelectedCamera[Dali::CameraActor::Property::TOP_PLANE_DISTANCE] = halfHeight;
396 mSelectedCamera[Dali::CameraActor::Property::BOTTOM_PLANE_DISTANCE] = -halfHeight;
400 Dali::FrameBuffer currentFrameBuffer = mRenderTask.GetFrameBuffer();
401 if(!currentFrameBuffer ||
402 currentFrameBuffer.GetColorTexture().GetWidth() != size.width ||
403 currentFrameBuffer.GetColorTexture().GetHeight() != size.height)
405 mRenderTask.ResetViewportGuideActor();
406 mRenderTask.SetViewport(Dali::Viewport(Vector4::ZERO));
408 // create offscreen buffer of new size to render our child actors to
409 mTexture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, unsigned(size.width), unsigned(size.height));
410 mFrameBuffer = FrameBuffer::New(size.width, size.height, FrameBuffer::Attachment::DEPTH_STENCIL);
411 mFrameBuffer.AttachColorTexture(mTexture);
412 DevelFrameBuffer::SetMultiSamplingLevel(mFrameBuffer, DEFAULT_FRAME_BUFFER_MULTI_SAMPLING_LEVEL);
413 Dali::Toolkit::ImageUrl imageUrl = Dali::Toolkit::Image::GenerateUrl(mFrameBuffer, 0u);
415 Property::Map imagePropertyMap;
416 imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
417 imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, imageUrl.GetUrl());
418 // To flip rendered scene without CameraActor::SetInvertYAxis() to avoid backface culling.
419 imagePropertyMap.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, Vector4(0.0f, 1.0f, 1.0f, -1.0f));
420 mVisual = Toolkit::VisualFactory::Get().CreateVisual(imagePropertyMap);
422 Toolkit::DevelControl::RegisterVisual(*this, RENDERING_BUFFER, mVisual);
424 mRenderTask.SetFrameBuffer(mFrameBuffer);
425 mRenderTask.SetClearEnabled(true);
426 mRenderTask.SetClearColor(Color::TRANSPARENT);
431 mRenderTask.SetViewportGuideActor(Self());
432 if(mRenderTask.GetFrameBuffer())
434 FrameBuffer framebuffer;
435 mRenderTask.SetFrameBuffer(framebuffer);
436 mRenderTask.SetClearEnabled(false);
438 Toolkit::DevelControl::UnregisterVisual(*this, RENDERING_BUFFER);
441 mFrameBuffer.Reset();
450 void SceneView::OnWindowResized(Window window, Window::WindowSize size)
452 mWindowOrientation = DevelWindow::GetPhysicalOrientation(window);
456 void SceneView::RotateCamera()
460 DevelCameraActor::RotateProjection(mSelectedCamera, DEFAULT_ORIENTATION);
464 DevelCameraActor::RotateProjection(mSelectedCamera, mWindowOrientation);
468 void SceneView::LoadImageBasedLight()
470 Texture diffuseTexture = Dali::Scene3D::Loader::LoadCubeMap(mDiffuseIblUrl);
471 Texture specularTexture = Dali::Scene3D::Loader::LoadCubeMap(mSpecularIblUrl);
473 if(diffuseTexture && specularTexture)
475 mDiffuseTexture = diffuseTexture;
476 mSpecularTexture = specularTexture;
477 for(auto&& item : mItems)
481 item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
487 void SceneView::OnLoadComplete()
489 // TODO: In this implementation, we cannot know which request occurs this OnLoadComplete Callback.
490 // Currently it is no problem because the all loading is processed in this method.
492 // Prevent to emit unnecessary resource ready signal.
493 if(IsResourceReady())
498 if(!mIBLResourceReady)
500 LoadImageBasedLight();
501 mIBLResourceReady = true;
502 mIblLoadedCallback = nullptr;
505 if(IsResourceReady())
507 Control::SetResourceReady(false);
511 } // namespace Internal
512 } // namespace Scene3D