[Tizen] Parse gltf mesh extra and extensions + Get BlendShape index by name
[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 protected:
160   /**
161    * @brief Constructs a new Model.
162    * @param[in] modelUrl model file path.(e.g., glTF, and DLI).
163    * @param[in] resourceDirectoryUrl resource file path that includes binary, image etc.
164    */
165   Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
166
167   /**
168    * A reference counted object may only be deleted by calling Unreference()
169    */
170   virtual ~Model();
171
172 private:
173   /**
174    * @copydoc Toolkit::Control::OnInitialize
175    */
176   void OnInitialize();
177
178   /**
179    * @copydoc CustomActorImpl::OnSceneConnection()
180    */
181   void OnSceneConnection(int depth) override;
182
183   /**
184    * @copydoc CustomActorImpl::OnSceneDisconnection()
185    */
186   void OnSceneDisconnection() override;
187
188   /**
189    * @copydoc CustomActorImpl::OnSizeSet( const Vector3& size )
190    */
191   void OnSizeSet(const Vector3& size) override;
192
193   /**
194    * @copydoc Toolkit::Control::GetNaturalSize
195    */
196   Vector3 GetNaturalSize() override;
197
198   /**
199    * @copydoc Toolkit::Control::GetHeightForWidth()
200    */
201   float GetHeightForWidth(float width) override;
202
203   /**
204    * @copydoc Toolkit::Control::GetWidthForHeight()
205    */
206   float GetWidthForHeight(float height) override;
207
208   /**
209    * @copydoc Toolkit::Control::OnRelayout()
210    */
211   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
212
213   /**
214    * @copydoc Toolkit::Control::IsResourceReady()
215    */
216   bool IsResourceReady() const override;
217
218 private:
219   /**
220    * @brief Create Model Root of this Model class.
221    */
222   void CreateModelRoot();
223
224   /**
225    * @brief Scales the model to fit the control or to return to original size.
226    */
227   void ScaleModel(bool useCurrentSize);
228
229   /**
230    * @brief Changes model anchor point to set the model at center or returns to the original model pivot.
231    */
232   void FitModelPosition();
233
234   /**
235    * @brief Changes IBL information of the input node.
236    */
237   void UpdateImageBasedLightTextureRecursively(Scene3D::ModelNode node, Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels);
238
239   /**
240    * @brief Changes IBL factor of the input node.
241    */
242   void UpdateImageBasedLightScaleFactorRecursively(Scene3D::ModelNode node, float iblScaleFactor);
243
244   /**
245    * @brief Changes IBL textures of the input node.
246    */
247   void UpdateImageBasedLightTexture();
248
249   /**
250    * @brief Changes IBL scale factor of the input node.
251    */
252   void UpdateImageBasedLightScaleFactor();
253
254   /**
255    * @brief Apply self transform into inputed camera.
256    * Inputed camera must be configured by CameraParameter. Mean, inputed camera coordinate depend on Model.
257    * After this API finished, CameraActor coordinate system converted as DALi coordinate system.
258    *
259    * @param[in,out] camera CameraActor who need to apply model itself's transform
260    */
261   void ApplyCameraTransform(Dali::CameraActor camera) const;
262
263 public: // Overrides LightObserver Methods.
264   /**
265    * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyImageBasedLightTexture()
266    */
267   void NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor, uint32_t specularMipmapLevels) override;
268
269   /**
270    * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyImageBasedLightScaleFactor()
271    */
272   void NotifyImageBasedLightScaleFactor(float scaleFactor) override;
273
274   /**
275    * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyLightAdded()
276    */
277   void NotifyLightAdded(uint32_t lightIndex, Scene3D::Light light) override;
278
279   /**
280    * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyLightRemoved()
281    */
282   void NotifyLightRemoved(uint32_t lightIndex) override;
283
284 private:
285   /**
286    * @brief Asynchronously model loading finished.
287    */
288   void OnModelLoadComplete();
289
290   /**
291    * @brief Asynchronously ibl diffusel image loading finished.
292    */
293   void OnIblDiffuseLoadComplete();
294
295   /**
296    * @brief Asynchronously ibl specular image loading finished.
297    */
298   void OnIblSpecularLoadComplete();
299
300   /**
301    * @brief Asynchronously ibl loading finished.
302    */
303   void OnIblLoadComplete();
304
305   /**
306    * @brief Update model root scale when Model size property is updated.
307    */
308   void OnSizeNotification(Dali::PropertyNotification& source);
309
310   /**
311    * @brief Reset Resource loading tasks.
312    */
313   void ResetResourceTasks();
314
315   /**
316    * @brief Reset a Resource loading task.
317    */
318   void ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask);
319
320   /**
321    * @brief Notify Resource Ready signal.
322    */
323   void NotifyResourceReady();
324
325   /**
326    * @brief Create Model from loaded SceneDefinition.
327    */
328   void CreateModel();
329
330   /**
331    * @brief Create Dali::Animation from loaded AnimationDefinitions.
332    */
333   void CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene);
334
335   /**
336    * @brief Reset CameraData from loaded CameraParameters.
337    */
338   void ResetCameraParameters();
339
340   /**
341    * @brief Collect ModelNode list by blendshape name
342    */
343   void UpdateBlendShapeNodeMap();
344
345 private:
346   std::string                    mModelUrl;
347   std::string                    mResourceDirectoryUrl;
348   Scene3D::ModelNode             mModelRoot;
349   std::vector<AnimationData>     mAnimations;
350   std::vector<CameraData>        mCameraParameters;
351   WeakHandle<Scene3D::SceneView> mParentSceneView;
352   Dali::PropertyNotification     mSizeNotification;
353
354   // Light
355   std::vector<Scene3D::Light> mLights;
356
357   // List of ModelNode for name of blend shape.
358   BlendShapeModelNodeMap mBlendShapeModelNodeMap;
359
360   // Asynchronous loading variable
361   ModelLoadTaskPtr          mModelLoadTask;
362   EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
363   EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
364
365   std::string mDiffuseIblUrl;
366   std::string mSpecularIblUrl;
367
368   Dali::Texture mDefaultSpecularTexture;
369   Dali::Texture mDefaultDiffuseTexture;
370   Dali::Texture mSceneSpecularTexture;
371   Dali::Texture mSceneDiffuseTexture;
372   Dali::Texture mSpecularTexture;
373   Dali::Texture mDiffuseTexture;
374   Vector3       mNaturalSize;
375   Vector3       mModelPivot;
376   float         mSceneIblScaleFactor;
377   float         mIblScaleFactor;
378   uint32_t      mSceneSpecularMipmapLevels;
379   uint32_t      mSpecularMipmapLevels;
380   bool          mModelChildrenSensitive;
381   bool          mModelChildrenFocusable;
382   bool          mModelResourceReady;
383   bool          mIblDiffuseResourceReady;
384   bool          mIblSpecularResourceReady;
385   bool          mIblDiffuseDirty;
386   bool          mIblSpecularDirty;
387 };
388
389 } // namespace Internal
390
391 // Helpers for public-api forwarding methods
392 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
393 {
394   DALI_ASSERT_ALWAYS(obj);
395   Dali::RefObject& handle = obj.GetImplementation();
396   return static_cast<Dali::Scene3D::Internal::Model&>(handle);
397 }
398
399 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
400 {
401   DALI_ASSERT_ALWAYS(obj);
402   const Dali::RefObject& handle = obj.GetImplementation();
403   return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
404 }
405
406 } // namespace Scene3D
407
408 } // namespace Dali
409
410 #endif // DALI_SCENE3D_INTERNAL_MODEL_H