[Tizen] Add MotionData class and generate Animation by this
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / model-components / model-node-impl.h
1 #ifndef DALI_SCENE3D_MODEL_COMPONENTS_MODEL_NODE_IMPL_H
2 #define DALI_SCENE3D_MODEL_COMPONENTS_MODEL_NODE_IMPL_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/devel-api/common/map-wrapper.h>
23 #include <dali/public-api/actors/custom-actor-impl.h>
24 #include <dali/public-api/common/dali-common.h>
25 #include <memory> // for std::unique_ptr
26 #include <string>
27
28 // INTERNAL INCLUDES
29 #include <dali-scene3d/internal/model-components/model-primitive-modify-observer.h>
30 #include <dali-scene3d/public-api/light/light.h>
31 #include <dali-scene3d/public-api/loader/mesh-definition.h>
32 #include <dali-scene3d/public-api/loader/skinning-details.h>
33 #include <dali-scene3d/public-api/model-components/model-node.h>
34 #include <dali-scene3d/public-api/model-components/model-primitive.h>
35
36 namespace Dali
37 {
38 namespace Scene3D
39 {
40 /**
41  * @addtogroup dali_toolkit_controls_model
42  * @{
43  */
44
45 namespace Internal
46 {
47 /**
48  * @brief This is the internal base class for custom node of model.
49  *
50  * @SINCE_2_2.99
51  */
52 class DALI_SCENE3D_API ModelNode : public CustomActorImpl, public ModelPrimitiveModifyObserver
53 {
54 public:
55   using ModelPrimitiveContainer = std::vector<Scene3D::ModelPrimitive>;
56   using BoneDataContainer       = std::vector<Dali::Scene3D::Loader::Skinning::BoneData>;
57   using BlendShapeIndexMap      = std::map<std::string, Loader::BlendShapes::Index>;
58
59   // Creation & Destruction
60   /**
61    * @brief Creates a new ModelNodeImpl instance that does not require touch by default.
62    *
63    * If touch is required, then the user can connect to this class' touch signal.
64    * @SINCE_2_2.99
65    * @return A handle to the ModelNode instance
66    */
67   static Scene3D::ModelNode New();
68
69 protected:
70   /**
71    * @brief Virtual destructor.
72    * @SINCE_2_2.99
73    */
74   virtual ~ModelNode();
75
76 protected: // From CustomActorImpl
77   /**
78    * @copydoc CustomActorImpl::OnSceneConnection()
79    * @note If overridden, then an up-call to ModelNode::OnSceneConnection MUST be made at the end.
80    */
81   void OnSceneConnection(int depth) override;
82
83   /**
84    * @copydoc CustomActorImpl::OnSceneDisconnection()
85    * @note If overridden, then an up-call to ModelNode::OnSceneDisconnection MUST be made at the end.
86    */
87   void OnSceneDisconnection() override;
88
89   /**
90    * @copydoc CustomActorImpl::OnChildAdd()
91    * @note If overridden, then an up-call to ModelNode::OnChildAdd MUST be made at the end.
92    */
93   void OnChildAdd(Actor& child) override;
94
95   /**
96    * @copydoc CustomActorImpl::OnChildRemove()
97    * @note If overridden, then an up-call to ModelNode::OnChildRemove MUST be made at the end.
98    */
99   void OnChildRemove(Actor& child) override;
100
101   /**
102    * @copydoc CustomActorImpl::OnPropertySet()
103    * @note If overridden, then an up-call to ModelNode::OnChildRemove MUST be made at the end.
104    */
105   void OnPropertySet(Property::Index index, const Property::Value& propertyValue) override;
106
107   /**
108    * @copydoc CustomActorImpl::OnSizeSet()
109    * @note If overridden, then an up-call to ModelNode::OnSizeSet MUST be made at the end.
110    */
111   void OnSizeSet(const Vector3& targetSize) override;
112
113   /**
114    * @copydoc CustomActorImpl::OnSizeAnimation()
115    * @note If overridden, then an up-call to ModelNode::OnSizeAnimation MUST be made at the end.
116    */
117   void OnSizeAnimation(Animation& animation, const Vector3& targetSize) override;
118
119   /**
120    * @copydoc CustomActorImpl::OnRelayout()
121    */
122   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
123
124   /**
125    * @copydoc CustomActorImpl::OnSetResizePolicy()
126    */
127   void OnSetResizePolicy(ResizePolicy::Type policy, Dimension::Type dimension) override;
128
129   /**
130    * @copydoc CustomActorImpl::GetNaturalSize()
131    */
132   Vector3 GetNaturalSize() override;
133
134   /**
135    * @copydoc CustomActorImpl::CalculateChildSize()
136    */
137   float CalculateChildSize(const Dali::Actor& child, Dimension::Type dimension) override;
138
139   /**
140    * @copydoc CustomActorImpl::GetHeightForWidth()
141    */
142   float GetHeightForWidth(float width) override;
143
144   /**
145    * @copydoc CustomActorImpl::GetWidthForHeight()
146    */
147   float GetWidthForHeight(float height) override;
148
149   /**
150    * @copydoc CustomActorImpl::RelayoutDependentOnChildren()
151    */
152   bool RelayoutDependentOnChildren(Dimension::Type dimension = Dimension::ALL_DIMENSIONS) override;
153
154   /**
155    * @copydoc CustomActorImpl::OnCalculateRelayoutSize()
156    */
157   void OnCalculateRelayoutSize(Dimension::Type dimension) override;
158
159   /**
160    * @copydoc CustomActorImpl::OnLayoutNegotiated()
161    */
162   void OnLayoutNegotiated(float size, Dimension::Type dimension) override;
163
164 protected:
165   // Construction
166
167   /**
168    * @brief ModelNode constructor.
169    *
170    * @SINCE_2_2.99
171    */
172   ModelNode();
173
174   /**
175    * @brief Second phase initialization.
176    * @SINCE_2_2.99
177    */
178   void Initialize();
179
180 public: // API for derived classes to override
181   // Lifecycle
182
183   /**
184    * @brief This method is called after the Node has been initialized.
185    *
186    * Derived classes should do any second phase initialization by overriding this method.
187    * @SINCE_2_2.99
188    */
189   virtual void OnInitialize();
190
191 public: // Public Method
192   /**
193    * @copydoc Dali::Scene3D::ModelNode::GetModelPrimitiveCount()
194    */
195   uint32_t GetModelPrimitiveCount() const;
196
197   /**
198    * @copydoc Dali::Scene3D::ModelNode::AddModelPrimitive()
199    */
200   void AddModelPrimitive(Dali::Scene3D::ModelPrimitive modelPrimitive);
201
202   /**
203    * @copydoc Dali::Scene3D::ModelNode::RemoveModelPrimitive(Dali::Scene3D::ModelPrimitive modelPrimitive)
204    */
205   void RemoveModelPrimitive(Dali::Scene3D::ModelPrimitive modelPrimitive);
206
207   /**
208    * @copydoc Dali::Scene3D::ModelNode::RemoveModelPrimitive(uint32_t index)
209    */
210   void RemoveModelPrimitive(uint32_t index);
211
212   /**
213    * @copydoc Dali::Scene3D::ModelNode::GetModelPrimitive()
214    */
215   Dali::Scene3D::ModelPrimitive GetModelPrimitive(uint32_t index) const;
216
217   /**
218    * @copydoc Dali::Scene3D::ModelNode::FindChildModelNodeByName()
219    */
220   Scene3D::ModelNode FindChildModelNodeByName(std::string_view nodeName);
221
222   /**
223    * @copydoc Dali::Scene3D::ModelNode::RetrieveBlendShapeNames()
224    */
225   void RetrieveBlendShapeNames(std::vector<std::string>& blendShapeNames) const;
226
227   /**
228    * @copydoc Dali::Scene3D::ModelNode::GetBlendShapeIndexByName()
229    */
230   Loader::BlendShapes::Index GetBlendShapeIndexByName(std::string_view blendShapeName) const;
231
232   /**
233    * @brief Sets the diffuse and specular image-based lighting textures for a ModelPrimitive.
234    *
235    * @param[in] diffuseTexture The diffuse texture.
236    * @param[in] specularTexture The specular texture.
237    * @param[in] iblScaleFactor The scale factor for the image-based lighting.
238    * @param[in] specularMipmapLevels The number of mipmap levels for the specular texture.
239    */
240   void SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels);
241
242   /**
243    * @brief Sets the scale factor for image-based lighting.
244    *
245    * @param[in] iblScaleFactor The scale factor for image-based lighting.
246    */
247   void SetImageBasedLightScaleFactor(float iblScaleFactor);
248
249   void AddLight(Scene3D::Light light, uint32_t lightIndex);
250   void RemoveLight(uint32_t lightIndex);
251
252   /**
253    * @brief Sets the blend shape data for a ModelPrimitive.
254    *
255    * @param[in] data The blend shape data.
256    * @param[in] primitive The ModelPrimitive to set the blend shape data for.
257    */
258   void SetBlendShapeData(Scene3D::Loader::BlendShapes::BlendShapeData& data, Scene3D::ModelPrimitive primitive);
259
260   /**
261    * @brief Sets the bone matrix for a ModelPrimitive and bone index.
262    *
263    * @param[in] inverseMatrix The inverse matrix of the bone.
264    * @param[in] primitive The ModelPrimitive to set the bone matrix for.
265    * @param[in] boneIndex The index of the bone to set the matrix for.
266    */
267   void SetBoneMatrix(const Matrix& inverseMatrix, Scene3D::ModelPrimitive primitive, Scene3D::Loader::Index& boneIndex);
268
269   /**
270    * @brief Called when a Renderer of ModelPrimitive is created.
271    *
272    * @param[in] renderer The Renderer that is created.
273    */
274   void OnRendererCreated(Renderer renderer) override;
275
276 private:
277   /**
278    * @brief Updates the bone matrix for a ModelPrimitive.
279    *
280    * @param[in] primitive The ModelPrimitive to set the bone matrix for.
281    */
282   void UpdateBoneMatrix(Scene3D::ModelPrimitive primitive);
283
284 private:
285   /// @cond internal
286
287   // Not copyable or movable
288   DALI_INTERNAL ModelNode(const ModelNode&) = delete;            ///< Deleted copy constructor.
289   DALI_INTERNAL ModelNode(ModelNode&&)      = delete;            ///< Deleted move constructor.
290   DALI_INTERNAL ModelNode& operator=(const ModelNode&) = delete; ///< Deleted copy assignment operator.
291   DALI_INTERNAL ModelNode& operator=(ModelNode&&) = delete;      ///< Deleted move assignment operator.
292
293 private:
294   ModelPrimitiveContainer mModelPrimitiveContainer; ///< List of model primitives
295   BoneDataContainer       mBoneDataContainer;
296   BlendShapeIndexMap      mBlendShapeIndexMap; ///< Index of blend shape by name
297   Dali::Texture           mSpecularTexture;
298   Dali::Texture           mDiffuseTexture;
299   float                   mIblScaleFactor{1.0f};
300   uint32_t                mSpecularMipmapLevels{1u};
301
302   // Light
303   std::vector<Scene3D::Light> mLights;
304   /// @endcond
305 };
306
307 /**
308  * @brief Gets implementation from the handle.
309  *
310  * @SINCE_2_2.99
311  * @param handle
312  * @return Implementation
313  * @pre handle is initialized and points to a node
314  */
315 DALI_SCENE3D_API Internal::ModelNode& GetImplementation(Dali::Scene3D::ModelNode& handle);
316
317 /**
318  * @brief Gets implementation from the handle.
319  *
320  * @SINCE_2_2.99
321  * @param handle
322  * @return Implementation
323  * @pre Handle is initialized and points to a node.
324  */
325 DALI_SCENE3D_API const Internal::ModelNode& GetImplementation(const Dali::Scene3D::ModelNode& handle);
326
327 } // namespace Internal
328
329 /**
330  * @}
331  */
332 } // namespace Scene3D
333
334 } // namespace Dali
335
336 #endif // DALI_SCENE3D_MODEL_COMPONENTS_MODEL_NODE_IMPL_H