Apply Light using Shader Manager
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / controls / scene-view / scene-view-impl.h
1 #ifndef DALI_SCENE3D_INTERNAL_SCENE_VIEW_H
2 #define DALI_SCENE3D_INTERNAL_SCENE_VIEW_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali-toolkit/internal/visuals/image/image-visual.h>
23 #include <dali-toolkit/public-api/controls/control-impl.h>
24 #include <dali/integration-api/adaptor-framework/scene-holder.h>
25 #include <dali/public-api/actors/camera-actor.h>
26 #include <dali/public-api/actors/layer.h>
27 #include <dali/public-api/adaptor-framework/window.h>
28 #include <dali/public-api/animation/animation.h>
29 #include <dali/public-api/object/weak-handle.h>
30 #include <dali/public-api/render-tasks/render-task.h>
31 #include <dali/public-api/rendering/frame-buffer.h>
32 #include <dali/public-api/rendering/texture.h>
33
34 // INTERNAL INCLUDES
35 #include <dali-scene3d/internal/common/environment-map-load-task.h>
36 #include <dali-scene3d/internal/common/light-observer.h>
37 #include <dali-scene3d/public-api/controls/scene-view/scene-view.h>
38 #include <dali-scene3d/public-api/loader/shader-manager.h>
39
40 namespace Dali
41 {
42 namespace Scene3D
43 {
44 class SceneView;
45
46 namespace Internal
47 {
48 /**
49  * @brief Impl class for SceneView.
50  */
51 class SceneView : public Dali::Toolkit::Internal::Control
52 {
53 public:
54   /**
55    * @brief Creates a new SceneView.
56    *
57    * @return A public handle to the newly allocated SceneView.
58    */
59   static Dali::Scene3D::SceneView New();
60
61   /**
62    * @copydoc SceneView::AddCamera()
63    */
64   void AddCamera(Dali::CameraActor camera);
65
66   /**
67    * @copydoc SceneView::RemoveCamera()
68    */
69   void RemoveCamera(CameraActor camera);
70
71   /**
72    * @copydoc SceneView::GetCameraCount()
73    */
74   uint32_t GetCameraCount() const;
75
76   /**
77    * @copydoc SceneView::GetSelectedCamera()
78    */
79   CameraActor GetSelectedCamera() const;
80
81   /**
82    * @copydoc SceneView::GetCamera()
83    */
84   CameraActor GetCamera(uint32_t index) const;
85
86   /**
87    * @copydoc SceneView::GetCamera()
88    */
89   CameraActor GetCamera(const std::string& name) const;
90
91   /**
92    * @copydoc SceneView::SelectCamera()
93    */
94   void SelectCamera(uint32_t index);
95
96   /**
97    * @copydoc SceneView::SelectCamera()
98    */
99   void SelectCamera(const std::string& name);
100
101   /**
102    * @brief Register an item.
103    *
104    * Some works(e.g, lighting) of SceneView should be propagated to the child 3D items.
105    * SceneView can avoid unnecessary tree traversal to find 3D items by storing only 3D items as a list.
106    *
107    * @param[in] item scene observer to be registered.
108    */
109   void RegisterSceneItem(Scene3D::Internal::LightObserver* item);
110
111   /**
112    * @brief Unregister an item
113    *
114    * @param[in] item scene observer to be unregistered.
115    */
116   void UnregisterSceneItem(Scene3D::Internal::LightObserver* item);
117
118   /**
119    * @copydoc SceneView::SetImageBasedLightSource()
120    */
121   void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
122
123   /**
124    * @copydoc SceneView::SetImageBasedLightScaleFactor()
125    */
126   void SetImageBasedLightScaleFactor(float scaleFactor);
127
128   /**
129    * @copydoc SceneView::GetImageBasedLightScaleFactor()
130    */
131   float GetImageBasedLightScaleFactor() const;
132
133   /**
134    * @brief Adds a Light object to this SceneView.
135    * Multiple light object can be added on this SceneView
136    * But the number of lights those actually turned on has limitation.
137    * Scene3D::Light::GetMaximumEnabledLightCount() method can be used to know the maximum namber.
138    *
139    * @param[in] light Light object to be added.
140    */
141   void AddLight(Scene3D::Light light);
142
143   /**
144    * @brief Removes a Light object to this SceneView.
145    * If the light is currently turned on, and SceneView has lights more than maximum number of enabled light.
146    * One of light in queue is turned on after this light is removed.
147    *
148    * @param[in] light Light object to be removed.
149    */
150   void RemoveLight(Scene3D::Light light);
151
152   /**
153    * @copydoc SceneView::GetActivatedLightCount()
154    */
155   uint32_t GetActivatedLightCount() const;
156
157   /**
158    * @copydoc SceneView::UseFramebuffer()
159    */
160   void UseFramebuffer(bool useFramebuffer);
161
162   /**
163    * @copydoc SceneView::IsUsingFramebuffer()
164    */
165   bool IsUsingFramebuffer() const;
166
167   /**
168    * @copydoc SceneView::SetFramebufferMultiSamplingLevel()
169    */
170   void SetFramebufferMultiSamplingLevel(uint8_t multiSamplingLevel);
171
172   /**
173    * @copydoc SceneView::GetFramebufferMultiSamplingLevel()
174    */
175   uint8_t GetFramebufferMultiSamplingLevel() const;
176
177   /**
178    * @copydoc SceneView::SetSkybox()
179    */
180   void SetSkybox(const std::string& skyboxUrl);
181
182   /**
183    * @copydoc SceneView::SetSkyboxEnvironmentMapType()
184    */
185   void SetSkyboxEnvironmentMapType(Scene3D::EnvironmentMapType skyboxEnvironmentMapType);
186
187   /**
188    * @copydoc SceneView::SetSkyboxIntensity()
189    */
190   void SetSkyboxIntensity(float intensity);
191
192   /**
193    * @copydoc SceneView::GetSkyboxIntensity()
194    */
195   float GetSkyboxIntensity() const;
196
197   /**
198    * @copydoc SceneView::SetSkyboxOrientation()
199    */
200   void SetSkyboxOrientation(const Quaternion& orientation);
201
202   /**
203    * @copydoc SceneView::GetSkyboxOrientation()
204    */
205   Quaternion GetSkyboxOrientation() const;
206
207   /**
208    * @brief Retrieves ShaderManager of this SceneView.
209    * @return ShaderManager of this SceneView.
210    */
211   Dali::Scene3D::Loader::ShaderManagerPtr GetShaderManager() const;
212
213 protected:
214   /**
215    * @brief Constructs a new SceneView.
216    */
217   SceneView();
218
219   /**
220    * A reference counted object may only be deleted by calling Unreference()
221    */
222   virtual ~SceneView();
223
224 private:
225   /**
226    * @copydoc CustomActorImpl::OnSceneConnection()
227    */
228   void OnSceneConnection(int depth) override;
229
230   /**
231    * @copydoc CustomActorImpl::OnSceneDisconnection()
232    */
233   void OnSceneDisconnection() override;
234
235   /**
236    * @copydoc Toolkit::Control::OnInitialize()
237    */
238   void OnInitialize() override;
239
240   /**
241    * @copydoc Toolkit::Control::OnChildAdd()
242    */
243   void OnChildAdd(Actor& child) override;
244
245   /**
246    * @copydoc Toolkit::Control::OnChildRemove()
247    */
248   void OnChildRemove(Actor& child) override;
249
250   /**
251    * @copydoc Toolkit::Control::GetHeightForWidth()
252    */
253   float GetHeightForWidth(float width) override;
254
255   /**
256    * @copydoc Toolkit::Control::GetWidthForHeight()
257    */
258   float GetWidthForHeight(float height) override;
259
260   /**
261    * @copydoc Toolkit::Control::OnRelayout()
262    */
263   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
264
265   /**
266    * @copydoc Toolkit::Control::IsResourceReady()
267    */
268   bool IsResourceReady() const override;
269
270   /**
271    * @brief Changes main camera as a input camera
272    *
273    * @param camera CameraActor that will be a main camera of the SceneView
274    */
275   void UpdateCamera(CameraActor camera);
276
277   /**
278    * @brief Updates RenderTask to use selected camera and to make framebuffer
279    */
280   void UpdateRenderTask();
281
282   /**
283    * @brief Callback that will be called when window is resized.
284    */
285   void OnWindowResized(Window window, Window::WindowSize size);
286
287   /**
288    * @brief Updates camera's projection orientation according to the screen orientation.
289    */
290   void RotateCamera();
291
292   /**
293    * @brief UpdateSkybox with skybox url and skybox environment map type.
294    *
295    * @param[in] skyboxUrl image url for skybox.
296    * @param[in] skyboxEnvironmentMapType The environment type of skybox.
297    */
298   void UpdateSkybox(const std::string& skyboxUrl, Scene3D::EnvironmentMapType skyboxEnvironmentMapType);
299
300   /**
301    * @brief Asynchronously skybox loading finished.
302    */
303   void OnSkyboxLoadComplete();
304
305   /**
306    * @brief Asynchronously ibl diffusel image loading finished.
307    */
308   void OnIblDiffuseLoadComplete();
309
310   /**
311    * @brief Asynchronously ibl specular image loading finished.
312    */
313   void OnIblSpecularLoadComplete();
314
315   /**
316    * @brief Asynchronously ibl loading finished.
317    */
318   void OnIblLoadComplete();
319
320   /**
321    * @brief Notify the changes of Ibl textures to the child items.
322    */
323   void NotifyImageBasedLightTextureChange();
324
325 private:
326   Toolkit::Visual::Base mVisual;
327
328   /////////////////////////////////////////////////////////////
329   // FrameBuffer and Rendertask to render child objects as a 3D Scene
330   Dali::WeakHandle<Dali::Window>                 mWindow;
331   Integration::SceneHolder                       mSceneHolder;
332   CameraActor                                    mDefaultCamera;
333   CameraActor                                    mSelectedCamera;
334   std::vector<CameraActor>                       mCameras;
335   std::vector<Scene3D::Internal::LightObserver*> mItems;
336   Dali::FrameBuffer                              mFrameBuffer;
337   Dali::Texture                                  mTexture;
338   Dali::RenderTask                               mRenderTask;
339   Layer                                          mRootLayer;
340   int32_t                                        mWindowOrientation;
341   Dali::Actor                                    mSkybox;
342   Quaternion                                     mSkyboxOrientation;
343   float                                          mSkyboxIntensity{1.0f};
344   uint8_t                                        mFrameBufferMultiSamplingLevel{0u};
345
346   // Shader Factory
347   Dali::Scene3D::Loader::ShaderManagerPtr mShaderManager;
348
349   // Light
350   std::vector<std::pair<Scene3D::Light, bool>> mLights; // Pair of Light object and flag that denotes the light is currently activated or not.
351
352   // Asynchronous Loading.
353   EnvironmentMapLoadTaskPtr mSkyboxLoadTask;
354   EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
355   EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
356   std::string               mSkyboxUrl;
357   std::string               mDiffuseIblUrl;
358   std::string               mSpecularIblUrl;
359
360   Scene3D::EnvironmentMapType mSkyboxEnvironmentMapType{Scene3D::EnvironmentMapType::AUTO};
361   Dali::Texture               mSkyboxTexture;
362   Dali::Texture               mDiffuseTexture;
363   Dali::Texture               mSpecularTexture;
364   float                       mIblScaleFactor{1.0f};
365   uint32_t                    mSpecularMipmapLevels{1u};
366   bool                        mUseFrameBuffer{false};
367   bool                        mSkyboxResourceReady{true};
368   bool                        mIblDiffuseResourceReady{true};
369   bool                        mIblSpecularResourceReady{true};
370   bool                        mSkyboxDirty{false};
371   bool                        mIblDiffuseDirty{false};
372   bool                        mIblSpecularDirty{false};
373 };
374
375 } // namespace Internal
376
377 // Helpers for public-api forwarding methods
378 inline Dali::Scene3D::Internal::SceneView& GetImpl(Dali::Scene3D::SceneView& obj)
379 {
380   DALI_ASSERT_ALWAYS(obj);
381   Dali::RefObject& handle = obj.GetImplementation();
382   return static_cast<Dali::Scene3D::Internal::SceneView&>(handle);
383 }
384
385 inline const Dali::Scene3D::Internal::SceneView& GetImpl(const Dali::Scene3D::SceneView& obj)
386 {
387   DALI_ASSERT_ALWAYS(obj);
388   const Dali::RefObject& handle = obj.GetImplementation();
389   return static_cast<const Dali::Scene3D::Internal::SceneView&>(handle);
390 }
391
392 } // namespace Scene3D
393
394 } // namespace Dali
395
396 #endif // DALI_SCENE3D_INTERNAL_SCENE_VIEW_H