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