1 #ifndef DALI_SCENE3D_MODEL_COMPONENTS_MODEL_NODE_IMPL_H
2 #define DALI_SCENE3D_MODEL_COMPONENTS_MODEL_NODE_IMPL_H
5 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
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"
45 * @addtogroup dali_toolkit_controls_model
51 using ColliderMeshUniquePtr = std::unique_ptr<Dali::Scene3D::Algorithm::ColliderMesh>;
53 * @brief This is the internal base class for custom node of model.
57 class DALI_SCENE3D_API ModelNode : public CustomActorImpl, public ModelPrimitiveModifyObserver
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>;
64 // Creation & Destruction
66 * @brief Creates a new ModelNodeImpl instance that does not require touch by default.
68 * If touch is required, then the user can connect to this class' touch signal.
70 * @return A handle to the ModelNode instance
72 static Scene3D::ModelNode New();
76 * @brief Virtual destructor.
81 protected: // From CustomActorImpl
83 * @copydoc CustomActorImpl::OnSceneConnection()
84 * @note If overridden, then an up-call to ModelNode::OnSceneConnection MUST be made at the end.
86 void OnSceneConnection(int depth) override;
89 * @copydoc CustomActorImpl::OnSceneDisconnection()
90 * @note If overridden, then an up-call to ModelNode::OnSceneDisconnection MUST be made at the end.
92 void OnSceneDisconnection() override;
95 * @copydoc CustomActorImpl::OnChildAdd()
96 * @note If overridden, then an up-call to ModelNode::OnChildAdd MUST be made at the end.
98 void OnChildAdd(Actor& child) override;
101 * @copydoc CustomActorImpl::OnChildRemove()
102 * @note If overridden, then an up-call to ModelNode::OnChildRemove MUST be made at the end.
104 void OnChildRemove(Actor& child) override;
107 * @copydoc CustomActorImpl::OnPropertySet()
108 * @note If overridden, then an up-call to ModelNode::OnChildRemove MUST be made at the end.
110 void OnPropertySet(Property::Index index, const Property::Value& propertyValue) override;
113 * @copydoc CustomActorImpl::OnSizeSet()
114 * @note If overridden, then an up-call to ModelNode::OnSizeSet MUST be made at the end.
116 void OnSizeSet(const Vector3& targetSize) override;
119 * @copydoc CustomActorImpl::OnSizeAnimation()
120 * @note If overridden, then an up-call to ModelNode::OnSizeAnimation MUST be made at the end.
122 void OnSizeAnimation(Animation& animation, const Vector3& targetSize) override;
125 * @copydoc CustomActorImpl::OnRelayout()
127 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
130 * @copydoc CustomActorImpl::OnSetResizePolicy()
132 void OnSetResizePolicy(ResizePolicy::Type policy, Dimension::Type dimension) override;
135 * @copydoc CustomActorImpl::GetNaturalSize()
137 Vector3 GetNaturalSize() override;
140 * @copydoc CustomActorImpl::CalculateChildSize()
142 float CalculateChildSize(const Dali::Actor& child, Dimension::Type dimension) override;
145 * @copydoc CustomActorImpl::GetHeightForWidth()
147 float GetHeightForWidth(float width) override;
150 * @copydoc CustomActorImpl::GetWidthForHeight()
152 float GetWidthForHeight(float height) override;
155 * @copydoc CustomActorImpl::RelayoutDependentOnChildren()
157 bool RelayoutDependentOnChildren(Dimension::Type dimension = Dimension::ALL_DIMENSIONS) override;
160 * @copydoc CustomActorImpl::OnCalculateRelayoutSize()
162 void OnCalculateRelayoutSize(Dimension::Type dimension) override;
165 * @copydoc CustomActorImpl::OnLayoutNegotiated()
167 void OnLayoutNegotiated(float size, Dimension::Type dimension) override;
173 * @brief ModelNode constructor.
180 * @brief Second phase initialization.
185 public: // API for derived classes to override
189 * @brief This method is called after the Node has been initialized.
191 * Derived classes should do any second phase initialization by overriding this method.
194 virtual void OnInitialize();
196 public: // Public Method
198 * @copydoc Dali::Scene3D::ModelNode::GetModelPrimitiveCount()
200 uint32_t GetModelPrimitiveCount() const;
203 * @copydoc Dali::Scene3D::ModelNode::AddModelPrimitive()
205 void AddModelPrimitive(Dali::Scene3D::ModelPrimitive modelPrimitive, Loader::ShaderOption::HashType hash);
208 * @copydoc Dali::Scene3D::ModelNode::RemoveModelPrimitive(Dali::Scene3D::ModelPrimitive modelPrimitive)
210 void RemoveModelPrimitive(Dali::Scene3D::ModelPrimitive modelPrimitive);
213 * @copydoc Dali::Scene3D::ModelNode::RemoveModelPrimitive(uint32_t index)
215 void RemoveModelPrimitive(uint32_t index);
218 * @copydoc Dali::Scene3D::ModelNode::GetModelPrimitive()
220 Dali::Scene3D::ModelPrimitive GetModelPrimitive(uint32_t index) const;
223 * @copydoc Dali::Scene3D::ModelNode::FindChildModelNodeByName()
225 Scene3D::ModelNode FindChildModelNodeByName(std::string_view nodeName);
228 * @copydoc Dali::Scene3D::ModelNode::RetrieveBlendShapeNames()
230 void RetrieveBlendShapeNames(std::vector<std::string>& blendShapeNames) const;
233 * @copydoc Dali::Scene3D::ModelNode::GetBlendShapeIndexByName()
235 Loader::BlendShapes::Index GetBlendShapeIndexByName(std::string_view blendShapeName) const;
238 * @brief Sets the shadow map textures for a ModelNode.
240 * @param[in] shadowMapTexture The shadow map texture.
242 void SetShadowMapTexture(Dali::Texture shadowMapTexture);
245 * @brief Sets the diffuse and specular image-based lighting textures for a ModelNode.
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.
252 void SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels);
255 * @brief Sets the scale factor for image-based lighting.
257 * @param[in] iblScaleFactor The scale factor for image-based lighting.
259 void SetImageBasedLightScaleFactor(float iblScaleFactor);
262 * @brief Updates shaders by using current material
264 * @param[in] shaderManager Shader manager to create shader.
266 void UpdateShader(Scene3D::Loader::ShaderManagerPtr shaderManager);
269 * @brief Sets the blend shape data for a ModelPrimitive.
271 * @param[in] data The blend shape data.
272 * @param[in] primitive The ModelPrimitive to set the blend shape data for.
274 void SetBlendShapeData(Scene3D::Loader::BlendShapes::BlendShapeData& data, Scene3D::ModelPrimitive primitive);
277 * @brief Sets the bone matrix for a ModelPrimitive and bone index.
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.
283 void SetBoneMatrix(const Matrix& inverseMatrix, Scene3D::ModelPrimitive primitive, Scene3D::Loader::Index& boneIndex);
286 * @brief Called when a Renderer of ModelPrimitive is created.
288 * @param[in] renderer The Renderer that is created.
290 void OnRendererCreated(Renderer renderer) override;
293 * @copydoc Dali::Scene3D::ModelNode::SetColliderMesh()
295 void SetColliderMesh(ColliderMeshUniquePtr&& colliderMesh);
298 * @copydoc Dali::Scene3D::ModelNode::HasColliderMesh()
300 [[nodiscard]] bool HasColliderMesh() const;
303 * @copydoc Dali::Scene3D::ModelNode::GetColliderMesh()
305 [[nodiscard]] const Scene3D::Algorithm::ColliderMesh& GetColliderMesh() const;
308 * @brief Sets a root model for the ModelNode
309 * @param[in] model Valid Model or nullptr to disconnect node from tree
311 void SetRootModel(Dali::Scene3D::Internal::Model* model)
313 mParentModel = model;
318 * @brief Updates the bone matrix for a ModelPrimitive.
320 * @param[in] primitive The ModelPrimitive to set the bone matrix for.
322 void UpdateBoneMatrix(Scene3D::ModelPrimitive primitive);
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.
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;
341 Internal::Model* mParentModel{nullptr};
344 std::unique_ptr<Dali::Scene3D::Algorithm::ColliderMesh> mColliderMesh;
346 float mIblScaleFactor{1.0f};
347 uint32_t mSpecularMipmapLevels{1u};
352 * @brief Gets implementation from the handle.
356 * @return Implementation
357 * @pre handle is initialized and points to a node
359 DALI_SCENE3D_API Internal::ModelNode& GetImplementation(Dali::Scene3D::ModelNode& handle);
362 * @brief Gets implementation from the handle.
366 * @return Implementation
367 * @pre Handle is initialized and points to a node.
369 DALI_SCENE3D_API const Internal::ModelNode& GetImplementation(const Dali::Scene3D::ModelNode& handle);
371 } // namespace Internal
376 } // namespace Scene3D
380 #endif // DALI_SCENE3D_MODEL_COMPONENTS_MODEL_NODE_IMPL_H