[dali_2.3.21] Merge branch 'devel/master'
[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) 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/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 Toolkit::Internal::Control, 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 public: // From Internal::Control.
82   /**
83    * @copydoc Dali::Toolkit::Internal::Control::OnInitialize()
84    */
85   void OnInitialize() override;
86
87 protected: // From CustomActorImpl. Note that it will make we can ignore many codes what Internal::Control using now.
88   /**
89    * @copydoc CustomActorImpl::OnSceneConnection()
90    * @note If overridden, then an up-call to ModelNode::OnSceneConnection MUST be made at the end.
91    */
92   void OnSceneConnection(int depth) override;
93
94   /**
95    * @copydoc CustomActorImpl::OnSceneDisconnection()
96    * @note If overridden, then an up-call to ModelNode::OnSceneDisconnection MUST be made at the end.
97    */
98   void OnSceneDisconnection() override;
99
100   /**
101    * @copydoc CustomActorImpl::OnChildAdd()
102    * @note If overridden, then an up-call to ModelNode::OnChildAdd MUST be made at the end.
103    */
104   void OnChildAdd(Actor& child) override;
105
106   /**
107    * @copydoc CustomActorImpl::OnChildRemove()
108    * @note If overridden, then an up-call to ModelNode::OnChildRemove MUST be made at the end.
109    */
110   void OnChildRemove(Actor& child) override;
111
112   /**
113    * @copydoc CustomActorImpl::OnPropertySet()
114    * @note If overridden, then an up-call to ModelNode::OnChildRemove MUST be made at the end.
115    */
116   void OnPropertySet(Property::Index index, const Property::Value& propertyValue) override;
117
118   /**
119    * @copydoc CustomActorImpl::OnSizeSet()
120    * @note If overridden, then an up-call to ModelNode::OnSizeSet MUST be made at the end.
121    */
122   void OnSizeSet(const Vector3& targetSize) override;
123
124   /**
125    * @copydoc CustomActorImpl::OnSizeAnimation()
126    * @note If overridden, then an up-call to ModelNode::OnSizeAnimation MUST be made at the end.
127    */
128   void OnSizeAnimation(Animation& animation, const Vector3& targetSize) override;
129
130   /**
131    * @copydoc CustomActorImpl::OnRelayout()
132    */
133   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
134
135   /**
136    * @copydoc CustomActorImpl::OnSetResizePolicy()
137    */
138   void OnSetResizePolicy(ResizePolicy::Type policy, Dimension::Type dimension) override;
139
140   /**
141    * @copydoc CustomActorImpl::GetNaturalSize()
142    */
143   Vector3 GetNaturalSize() override;
144
145   /**
146    * @copydoc CustomActorImpl::CalculateChildSize()
147    */
148   float CalculateChildSize(const Dali::Actor& child, Dimension::Type dimension) override;
149
150   /**
151    * @copydoc CustomActorImpl::GetHeightForWidth()
152    */
153   float GetHeightForWidth(float width) override;
154
155   /**
156    * @copydoc CustomActorImpl::GetWidthForHeight()
157    */
158   float GetWidthForHeight(float height) override;
159
160   /**
161    * @copydoc CustomActorImpl::RelayoutDependentOnChildren()
162    */
163   bool RelayoutDependentOnChildren(Dimension::Type dimension = Dimension::ALL_DIMENSIONS) override;
164
165   /**
166    * @copydoc CustomActorImpl::OnCalculateRelayoutSize()
167    */
168   void OnCalculateRelayoutSize(Dimension::Type dimension) override;
169
170   /**
171    * @copydoc CustomActorImpl::OnLayoutNegotiated()
172    */
173   void OnLayoutNegotiated(float size, Dimension::Type dimension) override;
174
175 protected:
176   // Construction
177
178   /**
179    * @brief ModelNode constructor.
180    *
181    * @SINCE_2_2.99
182    */
183   ModelNode();
184
185 public: // Public Method
186   /**
187    * @copydoc Dali::Scene3D::ModelNode::GetModelPrimitiveCount()
188    */
189   uint32_t GetModelPrimitiveCount() const;
190
191   /**
192    * @copydoc Dali::Scene3D::ModelNode::AddModelPrimitive()
193    */
194   void AddModelPrimitive(Dali::Scene3D::ModelPrimitive modelPrimitive, Loader::ShaderOption::HashType hash);
195
196   /**
197    * @copydoc Dali::Scene3D::ModelNode::RemoveModelPrimitive(Dali::Scene3D::ModelPrimitive modelPrimitive)
198    */
199   void RemoveModelPrimitive(Dali::Scene3D::ModelPrimitive modelPrimitive);
200
201   /**
202    * @copydoc Dali::Scene3D::ModelNode::RemoveModelPrimitive(uint32_t index)
203    */
204   void RemoveModelPrimitive(uint32_t index);
205
206   /**
207    * @copydoc Dali::Scene3D::ModelNode::GetModelPrimitive()
208    */
209   Dali::Scene3D::ModelPrimitive GetModelPrimitive(uint32_t index) const;
210
211   /**
212    * @copydoc Dali::Scene3D::ModelNode::FindChildModelNodeByName()
213    */
214   Scene3D::ModelNode FindChildModelNodeByName(std::string_view nodeName);
215
216   /**
217    * @copydoc Dali::Scene3D::ModelNode::RetrieveBlendShapeNames()
218    */
219   void RetrieveBlendShapeNames(std::vector<std::string>& blendShapeNames) const;
220
221   /**
222    * @copydoc Dali::Scene3D::ModelNode::GetBlendShapeIndexByName()
223    */
224   Loader::BlendShapes::Index GetBlendShapeIndexByName(std::string_view blendShapeName) const;
225
226   /**
227    * @brief Sets the shadow map textures for a ModelNode.
228    *
229    * @param[in] shadowMapTexture The shadow map texture.
230    */
231   void SetShadowMapTexture(Dali::Texture shadowMapTexture);
232
233   /**
234    * @copydoc Dali::Scene3D::ModelNode::CastShadow()
235    */
236   void CastShadow(bool castShadow);
237
238   /**
239    * @copydoc Dali::Scene3D::ModelNode::IsShadowCasting()
240    */
241   bool IsShadowCasting() const;
242
243   /**
244    * @copydoc Dali::Scene3D::ModelNode::ReceiveShadow()
245    */
246   void ReceiveShadow(bool receiveShadow);
247
248   /**
249    * @copydoc Dali::Scene3D::ModelNode::IsShadowReceiving()
250    */
251   bool IsShadowReceiving() const;
252
253   /**
254    * @brief Sets the diffuse and specular image-based lighting textures for a ModelNode.
255    *
256    * @param[in] diffuseTexture The diffuse texture.
257    * @param[in] specularTexture The specular texture.
258    * @param[in] iblScaleFactor The scale factor for the image-based lighting.
259    * @param[in] specularMipmapLevels The number of mipmap levels for the specular texture.
260    */
261   void SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels);
262
263   /**
264    * @brief Sets the scale factor for image-based lighting.
265    *
266    * @param[in] iblScaleFactor The scale factor for image-based lighting.
267    */
268   void SetImageBasedLightScaleFactor(float iblScaleFactor);
269
270   /**
271    * @brief Updates shaders by using current material
272    *
273    * @param[in] shaderManager Shader manager to create shader.
274    */
275   void UpdateShader(Scene3D::Loader::ShaderManagerPtr shaderManager);
276
277   /**
278    * @brief Sets the blend shape data for a ModelPrimitive.
279    *
280    * @param[in] data The blend shape data.
281    * @param[in] primitive The ModelPrimitive to set the blend shape data for.
282    */
283   void SetBlendShapeData(Scene3D::Loader::BlendShapes::BlendShapeData& data, Scene3D::ModelPrimitive primitive);
284
285   /**
286    * @brief Sets the bone matrix for a ModelPrimitive and bone index.
287    *
288    * @param[in] inverseMatrix The inverse matrix of the bone.
289    * @param[in] primitive The ModelPrimitive to set the bone matrix for.
290    * @param[in] boneIndex The index of the bone to set the matrix for.
291    */
292   void SetBoneMatrix(const Matrix& inverseMatrix, Scene3D::ModelPrimitive primitive, Scene3D::Loader::Index& boneIndex);
293
294   /**
295    * @brief Called when a Renderer of ModelPrimitive is created.
296    *
297    * @param[in] renderer The Renderer that is created.
298    */
299   void OnRendererCreated(Renderer renderer) override;
300
301   /**
302    * @copydoc Dali::Scene3D::ModelNode::SetColliderMesh()
303    */
304   void SetColliderMesh(ColliderMeshUniquePtr&& colliderMesh);
305
306   /**
307    * @copydoc Dali::Scene3D::ModelNode::HasColliderMesh()
308    */
309   [[nodiscard]] bool HasColliderMesh() const;
310
311   /**
312    * @copydoc Dali::Scene3D::ModelNode::GetColliderMesh()
313    */
314   [[nodiscard]] const Scene3D::Algorithm::ColliderMesh& GetColliderMesh() const;
315
316   /**
317   * @brief Sets a root model for the ModelNode
318   * @param[in] model Valid Model or nullptr to disconnect node from tree
319   */
320   void SetRootModel(Dali::Scene3D::Internal::Model* model)
321   {
322     mParentModel = model;
323   }
324
325 private:
326   /**
327    * @brief Updates the bone matrix for a ModelPrimitive.
328    *
329    * @param[in] primitive The ModelPrimitive to set the bone matrix for.
330    */
331   void UpdateBoneMatrix(Scene3D::ModelPrimitive primitive);
332
333   /// @cond internal
334
335   // Not copyable or movable
336   DALI_INTERNAL ModelNode(const ModelNode&) = delete;            ///< Deleted copy constructor.
337   DALI_INTERNAL ModelNode(ModelNode&&)      = delete;            ///< Deleted move constructor.
338   DALI_INTERNAL ModelNode& operator=(const ModelNode&) = delete; ///< Deleted copy assignment operator.
339   DALI_INTERNAL ModelNode& operator=(ModelNode&&) = delete;      ///< Deleted move assignment operator.
340
341 private:
342   Scene3D::Loader::ShaderManagerPtr mShaderManager;
343   ModelPrimitiveContainer           mModelPrimitiveContainer; ///< List of model primitives
344   BoneDataContainer                 mBoneDataContainer;
345   BlendShapeIndexMap                mBlendShapeIndexMap; ///< Index of blend shape by name
346   Dali::Texture                     mShadowMapTexture;
347   Dali::Texture                     mSpecularTexture;
348   Dali::Texture                     mDiffuseTexture;
349
350   Internal::Model* mParentModel{nullptr};
351
352   // Collider mesh
353   std::unique_ptr<Dali::Scene3D::Algorithm::ColliderMesh> mColliderMesh;
354
355   float    mIblScaleFactor{1.0f};
356   uint32_t mSpecularMipmapLevels{1u};
357   bool     mIsShadowCasting{true};
358   bool     mIsShadowReceiving{true};
359   /// @endcond
360 };
361
362 /**
363  * @brief Gets implementation from the handle.
364  *
365  * @SINCE_2_2.99
366  * @param handle
367  * @return Implementation
368  * @pre handle is initialized and points to a node
369  */
370 DALI_SCENE3D_API Internal::ModelNode& GetImplementation(Dali::Scene3D::ModelNode& handle);
371
372 /**
373  * @brief Gets implementation from the handle.
374  *
375  * @SINCE_2_2.99
376  * @param handle
377  * @return Implementation
378  * @pre Handle is initialized and points to a node.
379  */
380 DALI_SCENE3D_API const Internal::ModelNode& GetImplementation(const Dali::Scene3D::ModelNode& handle);
381
382 } // namespace Internal
383
384 /**
385  * @}
386  */
387 } // namespace Scene3D
388
389 } // namespace Dali
390
391 #endif // DALI_SCENE3D_MODEL_COMPONENTS_MODEL_NODE_IMPL_H