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