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