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