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