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