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/integration-api/debug.h>
33 #include <dali/public-api/object/type-registry-helper.h>
34 #include <dali/public-api/object/type-registry.h>
37 #include <dali-scene3d/internal/controls/model/model-impl.h>
38 #include <dali-scene3d/public-api/loader/cube-map-loader.h>
52 return Scene3D::SceneView::New();
55 // Setup properties, signals and actions using the type-registry.
56 DALI_TYPE_REGISTRATION_BEGIN(Scene3D::SceneView, Toolkit::Control, Create);
57 DALI_TYPE_REGISTRATION_END()
59 Property::Index RENDERING_BUFFER = Dali::Toolkit::Control::CONTROL_PROPERTY_END_INDEX + 1;
60 constexpr int32_t DEFAULT_ORIENTATION = 0;
62 } // anonymous namespace
64 SceneView::SceneView()
65 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
66 mWindowOrientation(DEFAULT_ORIENTATION),
67 mIblLoadedCallback(nullptr)
71 SceneView::~SceneView()
73 if(mIblLoadedCallback && Adaptor::IsAvailable())
75 // Removes the callback from the callback manager in case the control is destroyed before the callback is executed.
76 Adaptor::Get().RemoveIdle(mIblLoadedCallback);
80 Dali::Scene3D::SceneView SceneView::New()
82 SceneView* impl = new SceneView();
84 Dali::Scene3D::SceneView handle = Dali::Scene3D::SceneView(*impl);
86 // Second-phase init of the implementation
87 // This can only be done after the CustomActor connection has been made...
93 void SceneView::AddCamera(CameraActor camera)
101 mCameras.push_back(camera);
105 void SceneView::RemoveCamera(CameraActor camera)
107 if(camera == mDefaultCamera)
109 DALI_LOG_ERROR("Default Camera cannot removed.\n");
115 for(uint32_t i = 0; i < mCameras.size(); ++i)
117 if(mCameras[i] == camera)
119 mCameras.erase(mCameras.begin() + i);
124 if(mSelectedCamera == camera)
126 CameraActor newCurrentCamera = *mCameras.begin();
127 UpdateCamera(newCurrentCamera);
132 uint32_t SceneView::GetCameraCount() const
134 return mCameras.size();
137 CameraActor SceneView::GetSelectedCamera() const
139 return mSelectedCamera;
142 CameraActor SceneView::GetCamera(uint32_t index) const
144 if(index < mCameras.size())
146 return mCameras[index];
148 DALI_LOG_ERROR("Input index is out of bounds\n");
149 return CameraActor();
152 CameraActor SceneView::GetCamera(const std::string& name) const
154 CameraActor returnCamera;
155 for(auto&& camera : mCameras)
157 if(camera.GetProperty<std::string>(Actor::Property::NAME) == name)
159 returnCamera = camera;
166 void SceneView::SelectCamera(uint32_t index)
168 UpdateCamera(GetCamera(index));
171 void SceneView::SelectCamera(const std::string& name)
173 UpdateCamera(GetCamera(name));
176 void SceneView::RegisterSceneItem(Scene3D::Internal::ImageBasedLightObserver *item)
180 item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
181 mItems.push_back(item);
185 void SceneView::UnregisterSceneItem(Scene3D::Internal::ImageBasedLightObserver *item)
189 for(uint32_t i = 0; i < mItems.size(); ++i)
191 if(mItems[i] == item)
193 mItems.erase(mItems.begin() + i);
200 void SceneView::SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor)
202 // Request asynchronous model loading
203 if(!mIblLoadedCallback)
205 mIBLResourceReady = false;
206 mDiffuseIblUrl = diffuseUrl;
207 mSpecularIblUrl = specularUrl;
208 mIblScaleFactor = scaleFactor;
209 // The callback manager takes the ownership of the callback object.
210 mIblLoadedCallback = MakeCallback(this, &SceneView::OnLoadComplete);
211 Adaptor::Get().AddIdle(mIblLoadedCallback, false);
215 void SceneView::SetImageBasedLightScaleFactor(float scaleFactor)
217 mIblScaleFactor = scaleFactor;
218 for(auto&& item : mItems)
222 item->NotifyImageBasedLightScaleFactor(scaleFactor);
227 float SceneView::GetImageBasedLightScaleFactor() const
229 return mIblScaleFactor;
232 void SceneView::UseFramebuffer(bool useFramebuffer)
234 if(mUseFrameBuffer != useFramebuffer)
236 mUseFrameBuffer = useFramebuffer;
241 bool SceneView::IsUsingFramebuffer() const
243 return mUseFrameBuffer;
246 ///////////////////////////////////////////////////////////
251 void SceneView::OnSceneConnection(int depth)
255 Window window = DevelWindow::Get(Self());
258 window.ResizeSignal().Connect(this, &SceneView::OnWindowResized);
261 Control::OnSceneConnection(depth);
264 void SceneView::OnSceneDisconnection()
268 Window window = DevelWindow::Get(Self());
271 window.ResizeSignal().Disconnect(this, &SceneView::OnWindowResized);
274 Control::OnSceneDisconnection();
277 void SceneView::OnInitialize()
280 mRootLayer = Layer::New();
281 mRootLayer.SetProperty(Layer::Property::BEHAVIOR, Layer::LAYER_3D);
282 mRootLayer.SetProperty(Layer::Property::DEPTH_TEST, true);
283 // The models in the SceneView should be have independent coordinate with DALi default coordinate.
284 mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_POSITION, false);
285 mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_ORIENTATION, false);
286 mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_SCALE, false);
287 self.Add(mRootLayer);
289 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
290 mRenderTask = taskList.CreateTask();
291 mRenderTask.SetSourceActor(mRootLayer);
292 mRenderTask.SetExclusive(true);
293 mRenderTask.SetInputEnabled(true);
294 mRenderTask.SetCullMode(false);
295 mRenderTask.SetScreenToFrameBufferMappingActor(Self());
297 mDefaultCamera = Dali::CameraActor::New();
298 mDefaultCamera.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
299 mDefaultCamera.SetProperty(Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
300 mDefaultCamera.SetNearClippingPlane(1.0f);
301 AddCamera(mDefaultCamera);
302 UpdateCamera(mDefaultCamera);
305 void SceneView::OnChildAdd(Actor& child)
307 if(child != mRootLayer)
309 mRootLayer.Add(child);
311 Control::OnChildAdd(child);
314 void SceneView::OnChildRemove(Actor& child)
316 mRootLayer.Remove(child);
317 Control::OnChildRemove(child);
320 float SceneView::GetHeightForWidth(float width)
323 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
324 return Control::GetHeightForWidth(width) + padding.top + padding.bottom;
327 float SceneView::GetWidthForHeight(float height)
330 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
331 return Control::GetWidthForHeight(height) + padding.start + padding.end;
334 void SceneView::OnRelayout(const Vector2& size, RelayoutContainer& container)
336 Control::OnRelayout(size, container);
337 // Change canvas size of camera actor.
341 bool SceneView::IsResourceReady() const
343 return mIBLResourceReady;
346 void SceneView::UpdateCamera(CameraActor camera)
350 if(mSelectedCamera && mSelectedCamera.GetParent())
352 mSelectedCamera.Unparent();
354 mRootLayer.Add(camera);
357 mSelectedCamera = camera;
361 void SceneView::UpdateRenderTask()
365 if(mSelectedCamera != mRenderTask.GetCameraActor())
367 mRenderTask.SetCameraActor(mSelectedCamera);
370 Vector3 size = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
371 const float aspectRatio = size.width / size.height;
372 mSelectedCamera.SetAspectRatio(aspectRatio);
373 const bool projectionVertical = mSelectedCamera.GetProperty<int>(Dali::DevelCameraActor::Property::PROJECTION_DIRECTION) == Dali::DevelCameraActor::VERTICAL;
375 // if projectionVertical, Top / Bottom is +-ve to keep consistency with orthographic values
376 // else, Left / Right is +-ve to keep consistency with orthographic values
377 const float orthographicSize = DALI_LIKELY(projectionVertical) ? mSelectedCamera[Dali::CameraActor::Property::TOP_PLANE_DISTANCE] : mSelectedCamera[Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE];
378 const float halfHeight = DALI_LIKELY(projectionVertical) ? orthographicSize : orthographicSize / aspectRatio;
379 const float halfWidth = DALI_LIKELY(projectionVertical) ? orthographicSize * aspectRatio : orthographicSize;
381 mSelectedCamera[Dali::CameraActor::Property::LEFT_PLANE_DISTANCE] = -halfWidth;
382 mSelectedCamera[Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE] = halfWidth;
383 mSelectedCamera[Dali::CameraActor::Property::TOP_PLANE_DISTANCE] = halfHeight;
384 mSelectedCamera[Dali::CameraActor::Property::BOTTOM_PLANE_DISTANCE] = -halfHeight;
388 Dali::FrameBuffer currentFrameBuffer = mRenderTask.GetFrameBuffer();
389 if(!currentFrameBuffer ||
390 currentFrameBuffer.GetColorTexture().GetWidth() != size.width ||
391 currentFrameBuffer.GetColorTexture().GetHeight() != size.height)
393 mRenderTask.ResetViewportGuideActor();
394 mRenderTask.SetViewport(Dali::Viewport(Vector4::ZERO));
396 // create offscreen buffer of new size to render our child actors to
397 mTexture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, unsigned(size.width), unsigned(size.height));
398 mRenderTarget = FrameBuffer::New(size.width, size.height, FrameBuffer::Attachment::DEPTH_STENCIL);
399 mRenderTarget.AttachColorTexture(mTexture);
400 Dali::Toolkit::ImageUrl imageUrl = Dali::Toolkit::Image::GenerateUrl(mRenderTarget, 0u);
402 Property::Map imagePropertyMap;
403 imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
404 imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, imageUrl.GetUrl());
405 // To flip rendered scene without CameraActor::SetInvertYAxis() to avoid backface culling.
406 imagePropertyMap.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, Vector4(0.0f, 1.0f, 1.0f, -1.0f));
407 mVisual = Toolkit::VisualFactory::Get().CreateVisual(imagePropertyMap);
409 Toolkit::DevelControl::RegisterVisual(*this, RENDERING_BUFFER, mVisual);
411 mRenderTask.SetFrameBuffer(mRenderTarget);
412 mRenderTask.SetClearEnabled(true);
413 mRenderTask.SetClearColor(Color::TRANSPARENT);
418 mRenderTask.SetViewportGuideActor(Self());
419 if(mRenderTask.GetFrameBuffer())
421 FrameBuffer framebuffer;
422 mRenderTask.SetFrameBuffer(framebuffer);
423 mRenderTask.SetClearEnabled(false);
425 Toolkit::DevelControl::UnregisterVisual(*this, RENDERING_BUFFER);
428 mRenderTarget.Reset();
437 void SceneView::OnWindowResized(Window window, Window::WindowSize size)
439 mWindowOrientation = DevelWindow::GetPhysicalOrientation(window);
443 void SceneView::RotateCamera()
447 DevelCameraActor::RotateProjection(mSelectedCamera, DEFAULT_ORIENTATION);
451 DevelCameraActor::RotateProjection(mSelectedCamera, mWindowOrientation);
455 void SceneView::LoadImageBasedLight()
457 Texture diffuseTexture = Dali::Scene3D::Loader::LoadCubeMap(mDiffuseIblUrl);
458 Texture specularTexture = Dali::Scene3D::Loader::LoadCubeMap(mSpecularIblUrl);
460 if(diffuseTexture && specularTexture)
462 mDiffuseTexture = diffuseTexture;
463 mSpecularTexture = specularTexture;
464 for(auto&& item : mItems)
468 item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
474 void SceneView::OnLoadComplete()
476 // TODO: In this implementation, we cannot know which request occurs this OnLoadComplete Callback.
477 // Currently it is no problem because the all loading is processed in this method.
479 // Prevent to emit unnecessary resource ready signal.
480 if(IsResourceReady())
485 if(!mIBLResourceReady)
487 LoadImageBasedLight();
488 mIBLResourceReady = true;
489 mIblLoadedCallback = nullptr;
492 if(IsResourceReady())
494 Control::SetResourceReady(false);
498 } // namespace Internal
499 } // namespace Scene3D