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>
36 #include <dali-scene3d/internal/controls/model/model-impl.h>
37 #include <dali-scene3d/public-api/loader/cube-map-loader.h>
39 #include <dali/integration-api/debug.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 } // anonymous namespace
65 SceneView::SceneView()
66 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
67 mWindowOrientation(DEFAULT_ORIENTATION)
71 SceneView::~SceneView() = default;
73 Dali::Scene3D::SceneView SceneView::New()
75 SceneView* impl = new SceneView();
77 Dali::Scene3D::SceneView handle = Dali::Scene3D::SceneView(*impl);
79 // Second-phase init of the implementation
80 // This can only be done after the CustomActor connection has been made...
86 void SceneView::AddCamera(CameraActor camera)
94 mCameras.push_back(camera);
98 void SceneView::RemoveCamera(CameraActor camera)
100 if(camera == mDefaultCamera)
102 DALI_LOG_ERROR("Default Camera cannot removed.\n");
108 for(uint32_t i = 0; i < mCameras.size(); ++i)
110 if(mCameras[i] == camera)
112 mCameras.erase(mCameras.begin() + i);
117 if(mSelectedCamera == camera)
119 CameraActor newCurrentCamera = *mCameras.begin();
120 UpdateCamera(newCurrentCamera);
125 uint32_t SceneView::GetCameraCount() const
127 return mCameras.size();
130 CameraActor SceneView::GetSelectedCamera() const
132 return mSelectedCamera;
135 CameraActor SceneView::GetCamera(uint32_t index) const
137 if(index < mCameras.size())
139 return mCameras[index];
141 DALI_LOG_ERROR("Input index is out of bounds\n");
142 return CameraActor();
145 CameraActor SceneView::GetCamera(const std::string& name) const
147 CameraActor returnCamera;
148 for(auto&& camera : mCameras)
150 if(camera.GetProperty<std::string>(Actor::Property::NAME) == name)
152 returnCamera = camera;
159 void SceneView::SelectCamera(uint32_t index)
161 UpdateCamera(GetCamera(index));
164 void SceneView::SelectCamera(const std::string& name)
166 UpdateCamera(GetCamera(name));
169 void SceneView::RegisterModel(Scene3D::Model model)
173 model.SetImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
174 mModels.push_back(model);
178 void SceneView::UnregisterModel(Scene3D::Model model)
182 for(uint32_t i = 0; i < mModels.size(); ++i)
184 if(mModels[i] == model)
186 mModels.erase(mModels.begin() + i);
193 void SceneView::SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor)
195 mIBLResourceReady = false;
196 Texture diffuseTexture = Dali::Scene3D::Loader::LoadCubeMap(diffuseUrl);
199 Texture specularTexture = Dali::Scene3D::Loader::LoadCubeMap(specularUrl);
202 mDiffuseTexture = diffuseTexture;
203 mSpecularTexture = specularTexture;
204 mIblScaleFactor = scaleFactor;
206 for(auto&& model : mModels)
210 model.SetImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
215 mIBLResourceReady = true;
216 Control::SetResourceReady(false);
219 void SceneView::SetImageBasedLightScaleFactor(float scaleFactor)
221 mIblScaleFactor = scaleFactor;
222 for(auto&& model : mModels)
226 model.SetImageBasedLightScaleFactor(scaleFactor);
231 float SceneView::GetImageBasedLightScaleFactor() const
233 return mIblScaleFactor;
236 void SceneView::UseFramebuffer(bool useFramebuffer)
238 if(mUseFrameBuffer != useFramebuffer)
240 mUseFrameBuffer = useFramebuffer;
245 bool SceneView::IsUsingFramebuffer() const
247 return mUseFrameBuffer;
250 ///////////////////////////////////////////////////////////
255 void SceneView::OnSceneConnection(int depth)
259 Window window = DevelWindow::Get(Self());
262 window.ResizeSignal().Connect(this, &SceneView::OnWindowResized);
265 Control::OnSceneConnection(depth);
268 void SceneView::OnSceneDisconnection()
272 Window window = DevelWindow::Get(Self());
275 window.ResizeSignal().Disconnect(this, &SceneView::OnWindowResized);
278 Control::OnSceneDisconnection();
281 void SceneView::OnInitialize()
284 mRootLayer = Layer::New();
285 mRootLayer.SetProperty(Layer::Property::BEHAVIOR, Layer::LAYER_3D);
286 mRootLayer.SetProperty(Layer::Property::DEPTH_TEST, true);
287 // The models in the SceneView should be have independent coordinate with DALi default coordinate.
288 mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_POSITION, false);
289 mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_ORIENTATION, false);
290 mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_SCALE, false);
291 self.Add(mRootLayer);
293 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
294 mRenderTask = taskList.CreateTask();
295 mRenderTask.SetSourceActor(mRootLayer);
296 mRenderTask.SetExclusive(true);
297 mRenderTask.SetInputEnabled(true);
298 mRenderTask.SetCullMode(false);
299 mRenderTask.SetScreenToFrameBufferMappingActor(Self());
301 mDefaultCamera = Dali::CameraActor::New();
302 mDefaultCamera.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
303 mDefaultCamera.SetProperty(Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
304 mDefaultCamera.SetNearClippingPlane(1.0f);
305 AddCamera(mDefaultCamera);
306 UpdateCamera(mDefaultCamera);
309 void SceneView::OnChildAdd(Actor& child)
311 if(child != mRootLayer)
313 mRootLayer.Add(child);
315 Control::OnChildAdd(child);
318 void SceneView::OnChildRemove(Actor& child)
320 mRootLayer.Remove(child);
321 Control::OnChildRemove(child);
324 float SceneView::GetHeightForWidth(float width)
327 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
328 return Control::GetHeightForWidth(width) + padding.top + padding.bottom;
331 float SceneView::GetWidthForHeight(float height)
334 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
335 return Control::GetWidthForHeight(height) + padding.start + padding.end;
338 void SceneView::OnRelayout(const Vector2& size, RelayoutContainer& container)
340 Control::OnRelayout(size, container);
341 // Change canvas size of camera actor.
345 bool SceneView::IsResourceReady() const
347 return mIBLResourceReady;
350 void SceneView::UpdateCamera(CameraActor camera)
354 if(mSelectedCamera && mSelectedCamera.GetParent())
356 mSelectedCamera.Unparent();
358 mRootLayer.Add(camera);
361 mSelectedCamera = camera;
365 void SceneView::UpdateRenderTask()
369 if(mSelectedCamera != mRenderTask.GetCameraActor())
371 mRenderTask.SetCameraActor(mSelectedCamera);
374 Vector3 size = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
375 const float aspectRatio = size.width / size.height;
376 mSelectedCamera.SetAspectRatio(aspectRatio);
377 const float halfHeight = mSelectedCamera[Dali::CameraActor::Property::TOP_PLANE_DISTANCE];
378 const float halfWidth = aspectRatio * halfHeight;
379 mSelectedCamera[Dali::CameraActor::Property::LEFT_PLANE_DISTANCE] = -halfWidth;
380 mSelectedCamera[Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE] = halfWidth;
381 mSelectedCamera[Dali::CameraActor::Property::TOP_PLANE_DISTANCE] = halfHeight; // Top is +ve to keep consistency with orthographic values
382 mSelectedCamera[Dali::CameraActor::Property::BOTTOM_PLANE_DISTANCE] = -halfHeight; // Bottom is -ve to keep consistency with orthographic values
385 Dali::FrameBuffer currentFrameBuffer = mRenderTask.GetFrameBuffer();
386 if(!currentFrameBuffer ||
387 currentFrameBuffer.GetColorTexture().GetWidth() != size.width ||
388 currentFrameBuffer.GetColorTexture().GetHeight() != size.height)
390 mRenderTask.ResetViewportGuideActor();
391 mRenderTask.SetViewport(Dali::Viewport(Vector4::ZERO));
393 // create offscreen buffer of new size to render our child actors to
394 mTexture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, unsigned(size.width), unsigned(size.height));
395 mRenderTarget = FrameBuffer::New(size.width, size.height, FrameBuffer::Attachment::DEPTH_STENCIL);
396 mRenderTarget.AttachColorTexture(mTexture);
397 Dali::Toolkit::ImageUrl imageUrl = Dali::Toolkit::Image::GenerateUrl(mRenderTarget, 0u);
399 Property::Map imagePropertyMap;
400 imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
401 imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, imageUrl.GetUrl());
402 // To flip rendered scene without CameraActor::SetInvertYAxis() to avoid backface culling.
403 imagePropertyMap.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, Vector4(0.0f, 1.0f, 1.0f, -1.0f));
404 mVisual = Toolkit::VisualFactory::Get().CreateVisual(imagePropertyMap);
406 Toolkit::DevelControl::RegisterVisual(*this, RENDERING_BUFFER, mVisual);
408 mRenderTask.SetFrameBuffer(mRenderTarget);
409 mRenderTask.SetClearEnabled(true);
410 mRenderTask.SetClearColor(Color::TRANSPARENT);
415 mRenderTask.SetViewportGuideActor(Self());
416 if(mRenderTask.GetFrameBuffer())
418 FrameBuffer framebuffer;
419 mRenderTask.SetFrameBuffer(framebuffer);
420 mRenderTask.SetClearEnabled(false);
422 Toolkit::DevelControl::UnregisterVisual(*this, RENDERING_BUFFER);
425 mRenderTarget.Reset();
434 void SceneView::OnWindowResized(Window window, Window::WindowSize size)
436 mWindowOrientation = DevelWindow::GetPhysicalOrientation(window);
440 void SceneView::RotateCamera()
444 DevelCameraActor::RotateProjection(mSelectedCamera, DEFAULT_ORIENTATION);
448 DevelCameraActor::RotateProjection(mSelectedCamera, mWindowOrientation);
452 } // namespace Internal
453 } // namespace Scene3D