Change ModelView to Model
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / controls / scene-view / scene-view-impl.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-scene3d/internal/controls/scene-view/scene-view-impl.h>
20
21 // EXTERNAL INCLUDES
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/common/stage.h>
29 #include <dali/integration-api/debug.h>
30 #include <dali/public-api/object/type-registry-helper.h>
31 #include <dali/public-api/object/type-registry.h>
32
33 // INTERNAL INCLUDES
34 #include <dali-scene3d/internal/controls/model/model-impl.h>
35 #include <dali-scene3d/public-api/loader/cube-map-loader.h>
36
37 #include <dali/integration-api/debug.h>
38
39 using namespace Dali;
40
41 namespace Dali
42 {
43 namespace Scene3D
44 {
45 namespace Internal
46 {
47 namespace
48 {
49 BaseHandle Create()
50 {
51   return Scene3D::SceneView::New();
52 }
53
54 // Setup properties, signals and actions using the type-registry.
55 DALI_TYPE_REGISTRATION_BEGIN(Scene3D::SceneView, Toolkit::Control, Create);
56 DALI_TYPE_REGISTRATION_END()
57
58 Property::Index RENDERING_BUFFER = Dali::Toolkit::Control::CONTROL_PROPERTY_END_INDEX + 1;
59
60 } // anonymous namespace
61
62 SceneView::SceneView()
63 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT))
64 {
65 }
66
67 SceneView::~SceneView() = default;
68
69 Dali::Scene3D::SceneView SceneView::New()
70 {
71   SceneView* impl = new SceneView();
72
73   Dali::Scene3D::SceneView handle = Dali::Scene3D::SceneView(*impl);
74
75   // Second-phase init of the implementation
76   // This can only be done after the CustomActor connection has been made...
77   impl->Initialize();
78
79   return handle;
80 }
81
82 void SceneView::AddCamera(CameraActor camera)
83 {
84   if(camera)
85   {
86     if(mCameras.empty())
87     {
88       UpdateCamera(camera);
89     }
90     mCameras.push_back(camera);
91   }
92 }
93
94 void SceneView::RemoveCamera(CameraActor camera)
95 {
96   if(camera == mDefaultCamera)
97   {
98     DALI_LOG_ERROR("Default Camera cannot removed.\n");
99     return;
100   }
101
102   if(camera)
103   {
104     for(uint32_t i = 0; i < mCameras.size(); ++i)
105     {
106       if(mCameras[i] == camera)
107       {
108         mCameras.erase(mCameras.begin() + i);
109         break;
110       }
111     }
112
113     if(mSelectedCamera == camera)
114     {
115       CameraActor newCurrentCamera = *mCameras.begin();
116       UpdateCamera(newCurrentCamera);
117     }
118   }
119 }
120
121 uint32_t SceneView::GetCameraCount() const
122 {
123   return mCameras.size();
124 }
125
126 CameraActor SceneView::GetSelectedCamera() const
127 {
128   return mSelectedCamera;
129 }
130
131 CameraActor SceneView::GetCamera(uint32_t index) const
132 {
133   if(index < mCameras.size())
134   {
135     return mCameras[index];
136   }
137   DALI_LOG_ERROR("Input index is out of bounds\n");
138   return CameraActor();
139 }
140
141 CameraActor SceneView::GetCamera(const std::string& name) const
142 {
143   CameraActor returnCamera;
144   for(auto&& camera : mCameras)
145   {
146     if(camera.GetProperty<std::string>(Actor::Property::NAME) == name)
147     {
148       returnCamera = camera;
149       break;
150     }
151   }
152   return returnCamera;
153 }
154
155 void SceneView::SelectCamera(uint32_t index)
156 {
157   UpdateCamera(GetCamera(index));
158 }
159
160 void SceneView::SelectCamera(const std::string& name)
161 {
162   UpdateCamera(GetCamera(name));
163 }
164
165 void SceneView::RegisterModel(Scene3D::Model model)
166 {
167   if(model)
168   {
169     model.SetImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
170     mModels.push_back(model);
171   }
172 }
173
174 void SceneView::UnregisterModel(Scene3D::Model model)
175 {
176   if(model)
177   {
178     for(uint32_t i = 0; i < mModels.size(); ++i)
179     {
180       if(mModels[i] == model)
181       {
182         mModels.erase(mModels.begin() + i);
183         break;
184       }
185     }
186   }
187 }
188
189 void SceneView::SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor)
190 {
191   mIBLResourceReady = false;
192   Texture diffuseTexture = Dali::Scene3D::Loader::LoadCubeMap(diffuseUrl);
193   if(diffuseTexture)
194   {
195     Texture specularTexture = Dali::Scene3D::Loader::LoadCubeMap(specularUrl);
196     if(specularTexture)
197     {
198       mDiffuseTexture  = diffuseTexture;
199       mSpecularTexture = specularTexture;
200       mIblScaleFactor  = scaleFactor;
201
202       for(auto&& model : mModels)
203       {
204         if(model)
205         {
206           model.SetImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
207         }
208       }
209     }
210   }
211   mIBLResourceReady = true;
212   Control::SetResourceReady(false);
213 }
214
215 void SceneView::SetImageBasedLightScaleFactor(float scaleFactor)
216 {
217   mIblScaleFactor = scaleFactor;
218   for(auto&& model : mModels)
219   {
220     if(model)
221     {
222       model.SetImageBasedLightScaleFactor(scaleFactor);
223     }
224   }
225 }
226
227 float SceneView::GetImageBasedLightScaleFactor() const
228 {
229   return mIblScaleFactor;
230 }
231
232 void SceneView::UseFramebuffer(bool useFramebuffer)
233 {
234   if(mUseFrameBuffer != useFramebuffer)
235   {
236     mUseFrameBuffer = useFramebuffer;
237     UpdateRenderTask();
238   }
239 }
240
241 bool SceneView::IsUsingFramebuffer() const
242 {
243   return mUseFrameBuffer;
244 }
245
246 ///////////////////////////////////////////////////////////
247 //
248 // Private methods
249 //
250
251 void SceneView::OnSceneConnection(int depth)
252 {
253   UpdateRenderTask();
254
255   Control::OnSceneConnection(depth);
256 }
257
258 void SceneView::OnSceneDisconnection()
259 {
260   mModels.clear();
261   Control::OnSceneDisconnection();
262 }
263
264 void SceneView::OnInitialize()
265 {
266   Actor self = Self();
267   mRootLayer = Layer::New();
268   mRootLayer.SetProperty(Layer::Property::BEHAVIOR, Layer::LAYER_3D);
269   mRootLayer.SetProperty(Layer::Property::DEPTH_TEST, true);
270   // The models in the SceneView should be have independent coordinate with DALi default coordinate.
271   mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_POSITION, false);
272   mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_ORIENTATION, false);
273   mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_SCALE, false);
274   self.Add(mRootLayer);
275
276   RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
277   mRenderTask             = taskList.CreateTask();
278   mRenderTask.SetSourceActor(mRootLayer);
279   mRenderTask.SetExclusive(true);
280   mRenderTask.SetInputEnabled(true);
281   mRenderTask.SetCullMode(false);
282   mRenderTask.SetScreenToFrameBufferMappingActor(Self());
283
284   mDefaultCamera = Dali::CameraActor::New();
285   mDefaultCamera.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
286   mDefaultCamera.SetProperty(Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
287   mDefaultCamera.SetNearClippingPlane(1.0f);
288   AddCamera(mDefaultCamera);
289   UpdateCamera(mDefaultCamera);
290 }
291
292 void SceneView::OnChildAdd(Actor& child)
293 {
294   if(child != mRootLayer)
295   {
296     mRootLayer.Add(child);
297   }
298   Control::OnChildAdd(child);
299 }
300
301 void SceneView::OnChildRemove(Actor& child)
302 {
303   mRootLayer.Remove(child);
304   Control::OnChildRemove(child);
305 }
306
307 float SceneView::GetHeightForWidth(float width)
308 {
309   Extents padding;
310   padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
311   return Control::GetHeightForWidth(width) + padding.top + padding.bottom;
312 }
313
314 float SceneView::GetWidthForHeight(float height)
315 {
316   Extents padding;
317   padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
318   return Control::GetWidthForHeight(height) + padding.start + padding.end;
319 }
320
321 void SceneView::OnRelayout(const Vector2& size, RelayoutContainer& container)
322 {
323   Control::OnRelayout(size, container);
324   // Change canvas size of camera actor.
325   UpdateRenderTask();
326 }
327
328 bool SceneView::IsResourceReady() const
329 {
330   return mIBLResourceReady;
331 }
332
333 void SceneView::UpdateCamera(CameraActor camera)
334 {
335   if(camera)
336   {
337     if(mSelectedCamera && mSelectedCamera.GetParent())
338     {
339       mSelectedCamera.Unparent();
340     }
341     mRootLayer.Add(camera);
342   }
343
344   mSelectedCamera = camera;
345   UpdateRenderTask();
346 }
347
348 void SceneView::UpdateRenderTask()
349 {
350   if(mRenderTask)
351   {
352     if(mSelectedCamera != mRenderTask.GetCameraActor())
353     {
354       mRenderTask.SetCameraActor(mSelectedCamera);
355     }
356
357     Vector3 size = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
358     const float aspectRatio = size.width / size.height;
359     mSelectedCamera.SetAspectRatio(aspectRatio);
360     const float halfHeight = mSelectedCamera[Dali::CameraActor::Property::TOP_PLANE_DISTANCE];
361     const float halfWidth = aspectRatio * halfHeight;
362     mSelectedCamera[Dali::CameraActor::Property::LEFT_PLANE_DISTANCE]   = -halfWidth;
363     mSelectedCamera[Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE]  = halfWidth;
364     mSelectedCamera[Dali::CameraActor::Property::TOP_PLANE_DISTANCE]    = halfHeight; // Top is +ve to keep consistency with orthographic values
365     mSelectedCamera[Dali::CameraActor::Property::BOTTOM_PLANE_DISTANCE] = -halfHeight; // Bottom is -ve to keep consistency with orthographic values
366     if(mUseFrameBuffer)
367     {
368       Dali::FrameBuffer currentFrameBuffer = mRenderTask.GetFrameBuffer();
369       if(!currentFrameBuffer ||
370          currentFrameBuffer.GetColorTexture().GetWidth() != size.width ||
371          currentFrameBuffer.GetColorTexture().GetHeight() != size.height)
372       {
373         mRenderTask.ResetViewportGuideActor();
374         mRenderTask.SetViewport(Dali::Viewport(Vector4::ZERO));
375
376         // create offscreen buffer of new size to render our child actors to
377         mTexture      = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, unsigned(size.width), unsigned(size.height));
378         mRenderTarget = FrameBuffer::New(size.width, size.height, FrameBuffer::Attachment::DEPTH_STENCIL);
379         mRenderTarget.AttachColorTexture(mTexture);
380         Dali::Toolkit::ImageUrl imageUrl = Dali::Toolkit::Image::GenerateUrl(mRenderTarget, 0u);
381
382         Property::Map imagePropertyMap;
383         imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
384         imagePropertyMap.Insert(Toolkit::ImageVisual::Property::URL, imageUrl.GetUrl());
385         // To flip rendered scene without CameraActor::SetInvertYAxis() to avoid backface culling.
386         imagePropertyMap.Insert(Toolkit::ImageVisual::Property::PIXEL_AREA, Vector4(0.0f, 1.0f, 1.0f, -1.0f));
387         mVisual = Toolkit::VisualFactory::Get().CreateVisual(imagePropertyMap);
388
389         Toolkit::DevelControl::RegisterVisual(*this, RENDERING_BUFFER, mVisual);
390
391         mRenderTask.SetFrameBuffer(mRenderTarget);
392         mRenderTask.SetClearEnabled(true);
393         mRenderTask.SetClearColor(Color::TRANSPARENT);
394       }
395     }
396     else
397     {
398       mRenderTask.SetViewportGuideActor(Self());
399       if(mRenderTask.GetFrameBuffer())
400       {
401         FrameBuffer framebuffer;
402         mRenderTask.SetFrameBuffer(framebuffer);
403         mRenderTask.SetClearEnabled(false);
404       }
405     }
406   }
407 }
408
409 } // namespace Internal
410 } // namespace Scene3D
411 } // namespace Dali