Generate Camera from loaded model
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / controls / model / model-impl.h
1 #ifndef DALI_SCENE3D_INTERNAL_MODEL_H
2 #define DALI_SCENE3D_INTERNAL_MODEL_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/public-api/controls/control-impl.h>
23 #include <dali/public-api/actors/camera-actor.h>
24 #include <dali/public-api/actors/layer.h>
25 #include <dali/public-api/animation/animation.h>
26 #include <dali/public-api/object/weak-handle.h>
27 #include <dali/public-api/rendering/texture.h>
28
29 // INTERNAL INCLUDES
30 #include <dali-scene3d/internal/common/environment-map-load-task.h>
31 #include <dali-scene3d/internal/common/image-based-light-observer.h>
32 #include <dali-scene3d/internal/common/model-load-task.h>
33 #include <dali-scene3d/public-api/controls/model/model.h>
34 #include <dali-scene3d/public-api/controls/scene-view/scene-view.h>
35
36 namespace Dali
37 {
38 namespace Scene3D
39 {
40 class Model;
41
42 namespace Internal
43 {
44 /**
45  * @brief Impl class for Model.
46  */
47 class Model : public Dali::Toolkit::Internal::Control, public ImageBasedLightObserver
48 {
49 public:
50   using AnimationData = std::pair<std::string, Dali::Animation>;
51   using CameraData    = Loader::CameraParameters;
52
53   /**
54    * @brief Creates a new Model.
55    *
56    * @return A public handle to the newly allocated Model.
57    */
58   static Dali::Scene3D::Model New(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
59
60   /**
61    * @copydoc Model::GetModelRoot()
62    */
63   const Actor GetModelRoot() const;
64
65   /**
66    * @copydoc Model::SetChildrenSensitive()
67    */
68   void SetChildrenSensitive(bool enable);
69
70   /**
71    * @copydoc Model::GetChildrenSensitive()
72    */
73   bool GetChildrenSensitive() const;
74
75   /**
76    * @copydoc Model::SetChildrenFocusable()
77    */
78   void SetChildrenFocusable(bool enable);
79
80   /**
81    * @copydoc Model::GetChildrenFocusable()
82    */
83   bool GetChildrenFocusable() const;
84
85   /**
86    * @copydoc Model::SetImageBasedLightSource()
87    */
88   void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
89
90   /**
91    * @copydoc Model::SetImageBasedLightTexture()
92    */
93   void SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor);
94
95   /**
96    * @copydoc Model::SetImageBasedLightScaleFactor()
97    */
98   void SetImageBasedLightScaleFactor(float scaleFactor);
99
100   /**
101    * @copydoc Model::GetImageBasedLightScaleFactor()
102    */
103   float GetImageBasedLightScaleFactor() const;
104
105   /**
106    * @copydoc Model::GetAnimationCount()
107    */
108   uint32_t GetAnimationCount() const;
109
110   /**
111    * @copydoc Model::GetAnimation()
112    */
113   Dali::Animation GetAnimation(uint32_t index) const;
114
115   /**
116    * @copydoc Model::GetAnimation()
117    */
118   Dali::Animation GetAnimation(const std::string& name) const;
119
120   /**
121    * @copydoc Model::GetCameraCount()
122    */
123   uint32_t GetCameraCount() const;
124
125   /**
126    * @copydoc Model::GenerateCamera()
127    */
128   Dali::CameraActor GenerateCamera(uint32_t index) const;
129
130   /**
131    * @copydoc Model::ApplyCamera()
132    */
133   bool ApplyCamera(uint32_t index, Dali::CameraActor camera) const;
134
135 protected:
136   /**
137    * @brief Constructs a new Model.
138    */
139   Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
140
141   /**
142    * A reference counted object may only be deleted by calling Unreference()
143    */
144   virtual ~Model();
145
146 private:
147   /**
148    * @copydoc Toolkit::Control::OnInitialize
149    */
150   void OnInitialize();
151
152   /**
153    * @copydoc CustomActorImpl::OnSceneConnection()
154    */
155   void OnSceneConnection(int depth) override;
156
157   /**
158    * @copydoc CustomActorImpl::OnSceneDisconnection()
159    */
160   void OnSceneDisconnection() override;
161
162   /**
163    * @copydoc Toolkit::Control::GetNaturalSize
164    */
165   Vector3 GetNaturalSize() override;
166
167   /**
168    * @copydoc Toolkit::Control::GetHeightForWidth()
169    */
170   float GetHeightForWidth(float width) override;
171
172   /**
173    * @copydoc Toolkit::Control::GetWidthForHeight()
174    */
175   float GetWidthForHeight(float height) override;
176
177   /**
178    * @copydoc Toolkit::Control::OnRelayout()
179    */
180   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
181
182   /**
183    * @copydoc Toolkit::Control::IsResourceReady()
184    */
185   bool IsResourceReady() const override;
186
187 private:
188   /**
189    * @brief Scales the model to fit the control or to return to original size.
190    */
191   void ScaleModel();
192
193   /**
194    * @brief Changes model anchor point to set the model at center or returns to the original model pivot.
195    */
196   void FitModelPosition();
197
198   /**
199    * @brief Changes IBL information of the input node.
200    */
201   void CollectRenderableActor(Actor actor);
202
203   /**
204    * @brief Changes IBL textures of the input node.
205    */
206   void UpdateImageBasedLightTexture();
207
208   /**
209    * @brief Changes IBL scale factor of the input node.
210    */
211   void UpdateImageBasedLightScaleFactor();
212
213   /**
214    * @brief Apply self transform into inputed camera.
215    * Inputed camera must be configured by CameraParameter. Mean, inputed camera coordinate depend on Model.
216    * After this API finished, CameraActor coordinate system converted as DALi coordinate system.
217    *
218    * @param[in,out] camera CameraActor who need to apply model itself's transform
219    */
220   void ApplyCameraTransform(Dali::CameraActor camera) const;
221
222 public: // Overrides ImageBasedLightObserver Methods.
223   /**
224    * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightTexture()
225    */
226   void NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor) override;
227
228   /**
229    * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightScaleFactor()
230    */
231   void NotifyImageBasedLightScaleFactor(float scaleFactor) override;
232
233 private:
234   /**
235    * @brief Asynchronously model loading finished.
236    */
237   void OnModelLoadComplete();
238
239   /**
240    * @brief Asynchronously ibl diffusel image loading finished.
241    */
242   void OnIblDiffuseLoadComplete();
243
244   /**
245    * @brief Asynchronously ibl specular image loading finished.
246    */
247   void OnIblSpecularLoadComplete();
248
249   /**
250    * @brief Asynchronously ibl loading finished.
251    */
252   void OnIblLoadComplete();
253
254   /**
255    * @brief Reset Resource loading tasks.
256    */
257   void ResetResourceTasks();
258
259   /**
260    * @brief Reset a Resource loading task.
261    */
262   void ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask);
263
264   /**
265    * @brief Request to load a Ibl texture asynchronously
266    */
267   void RequestLoadIblTexture(EnvironmentMapLoadTaskPtr asyncLoadTask, const std::string& url);
268
269   /**
270    * @brief Notify Resource Ready signal.
271    */
272   void NotifyResourceReady();
273
274   /**
275    * @brief Create Model from loaded SceneDefinition.
276    */
277   void CreateModel();
278
279   /**
280    * @brief Create Dali::Animation from loaded AnimationDefinitions.
281    */
282   void CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene);
283
284   /**
285    * @brief Reset CameraData from loaded CameraParameters.
286    */
287   void ResetCameraParameters();
288
289 private:
290   std::string                    mModelUrl;
291   std::string                    mResourceDirectoryUrl;
292   Dali::Actor                    mModelRoot;
293   std::vector<AnimationData>     mAnimations;
294   std::vector<CameraData>        mCameraParameters;
295   std::vector<WeakHandle<Actor>> mRenderableActors;
296   WeakHandle<Scene3D::SceneView> mParentSceneView;
297
298   // Asynchronous loading variable
299   ModelLoadTaskPtr          mModelLoadTask;
300   EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
301   EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
302
303   std::string mDiffuseIblUrl;
304   std::string mSpecularIblUrl;
305
306   // TODO: This default texture can be removed after 3D Resource Cache is added.
307   Dali::Texture mDefaultSpecularTexture;
308   Dali::Texture mDefaultDiffuseTexture;
309   Dali::Texture mSceneSpecularTexture;
310   Dali::Texture mSceneDiffuseTexture;
311   Dali::Texture mSpecularTexture;
312   Dali::Texture mDiffuseTexture;
313   Vector3       mNaturalSize;
314   Vector3       mModelPivot;
315   float         mSceneIblScaleFactor;
316   float         mIblScaleFactor;
317   bool          mModelChildrenSensitive;
318   bool          mModelChildrenFocusable;
319   bool          mModelResourceReady;
320   bool          mIblDiffuseResourceReady;
321   bool          mIblSpecularResourceReady;
322   bool          mIblDiffuseDirty;
323   bool          mIblSpecularDirty;
324 };
325
326 } // namespace Internal
327
328 // Helpers for public-api forwarding methods
329 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
330 {
331   DALI_ASSERT_ALWAYS(obj);
332   Dali::RefObject& handle = obj.GetImplementation();
333   return static_cast<Dali::Scene3D::Internal::Model&>(handle);
334 }
335
336 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
337 {
338   DALI_ASSERT_ALWAYS(obj);
339   const Dali::RefObject& handle = obj.GetImplementation();
340   return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
341 }
342
343 } // namespace Scene3D
344
345 } // namespace Dali
346
347 #endif // DALI_SCENE3D_INTERNAL_MODEL_H