1 #ifndef DALI_SCENE3D_INTERNAL_MODEL_H
2 #define DALI_SCENE3D_INTERNAL_MODEL_H
5 * Copyright (c) 2024 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-toolkit/public-api/controls/control-impl.h>
23 #include <dali/devel-api/common/map-wrapper.h>
24 #include <dali/public-api/actors/camera-actor.h>
25 #include <dali/public-api/actors/layer.h>
26 #include <dali/public-api/animation/animation.h>
27 #include <dali/public-api/object/property-notification.h>
28 #include <dali/public-api/object/weak-handle.h>
29 #include <dali/public-api/rendering/texture.h>
30 #include <unordered_map>
33 #include <dali-scene3d/internal/common/environment-map-load-task.h>
34 #include <dali-scene3d/internal/common/light-observer.h>
35 #include <dali-scene3d/internal/common/model-load-task.h>
36 #include <dali-scene3d/internal/model-components/model-node-impl.h>
37 #include <dali-scene3d/public-api/controls/model/model.h>
38 #include <dali-scene3d/public-api/controls/scene-view/scene-view.h>
39 #include <dali-scene3d/public-api/light/light.h>
40 #include <dali-scene3d/public-api/loader/load-result.h>
41 #include <dali-scene3d/public-api/model-components/model-node.h>
52 * @brief Impl class for Model.
54 class Model : public Dali::Toolkit::Internal::Control, public LightObserver
57 using AnimationData = std::pair<std::string, Dali::Animation>;
58 using CameraData = Loader::CameraParameters;
59 using BlendShapeModelNodeMap = std::map<std::string, std::vector<Scene3D::ModelNode>>;
61 // ColliderMeshContainer doesn't hold actual collider meshes
62 // but pairs unique ModelNode id with ModelNode for lookup purposes.
63 // All model nodes in the container have collider mesh attached.
64 using ColliderMeshContainer = std::unordered_map<int, Scene3D::ModelNode>;
67 * @copydoc Model::New()
69 static Dali::Scene3D::Model New(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
72 * @copydoc Model::GetModelRoot()
74 const Scene3D::ModelNode GetModelRoot() const;
77 * @copydoc Model::AddModelNode()
79 void AddModelNode(Scene3D::ModelNode modelNode);
82 * @copydoc Model::RemoveModelNode()
84 void RemoveModelNode(Scene3D::ModelNode modelNode);
87 * @copydoc Model::SetChildrenSensitive()
89 void SetChildrenSensitive(bool enable);
92 * @copydoc Model::GetChildrenSensitive()
94 bool GetChildrenSensitive() const;
97 * @copydoc Model::SetChildrenFocusable()
99 void SetChildrenFocusable(bool enable);
102 * @copydoc Model::GetChildrenFocusable()
104 bool GetChildrenFocusable() const;
107 * @copydoc Model::SetImageBasedLightSource()
109 void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
112 * @copydoc Model::SetImageBasedLightScaleFactor()
114 void SetImageBasedLightScaleFactor(float scaleFactor);
117 * @copydoc Model::GetImageBasedLightScaleFactor()
119 float GetImageBasedLightScaleFactor() const;
122 * @copydoc Model::GetAnimationCount()
124 uint32_t GetAnimationCount() const;
127 * @copydoc Model::GetAnimation()
129 Dali::Animation GetAnimation(uint32_t index) const;
132 * @copydoc Model::GetAnimation()
134 Dali::Animation GetAnimation(const std::string& name) const;
137 * @copydoc Model::GetCameraCount()
139 uint32_t GetCameraCount() const;
142 * @copydoc Model::GenerateCamera()
144 Dali::CameraActor GenerateCamera(uint32_t index) const;
147 * @copydoc Model::ApplyCamera()
149 bool ApplyCamera(uint32_t index, Dali::CameraActor camera) const;
152 * @copydoc Model::FindChildModelNodeByName()
154 Scene3D::ModelNode FindChildModelNodeByName(std::string_view nodeName);
157 * @copydoc Model::RetrieveBlendShapeNames()
159 void RetrieveBlendShapeNames(std::vector<std::string>& blendShapeNames) const;
162 * @copydoc Model::RetrieveModelNodesByBlendShapeName()
164 void RetrieveModelNodesByBlendShapeName(std::string_view blendShapeName, std::vector<Scene3D::ModelNode>& modelNodes) const;
167 * @copydoc Model::GenerateMotionDataAnimation()
169 Dali::Animation GenerateMotionDataAnimation(Scene3D::MotionData motionData);
172 * @copydoc Model::SetMotionData()
174 void SetMotionData(Scene3D::MotionData motionData);
177 * @copydoc Model::CastShadow()
179 void CastShadow(bool castShadow);
182 * @copydoc Model::IsShadowCasting()
184 bool IsShadowCasting() const;
187 * @copydoc Model::ReceiveShadow()
189 void ReceiveShadow(bool receiveShadow);
192 * @copydoc Model::IsShadowReceiving()
194 bool IsShadowReceiving() const;
197 * @copydoc Scene3D::Model::MeshHitSignal()
199 Scene3D::Model::MeshHitSignalType& MeshHitSignal()
201 return mMeshHitSignal;
205 * @brief Emits MeshHitSignal
206 * @param[in] modelNode ModelNode that has been hit
207 * @return Result of hit handling.
209 bool EmitMeshHitSignal(Scene3D::ModelNode modelNode)
212 if(!mMeshHitSignal.Empty())
214 Scene3D::Model handle(GetOwner());
215 retVal = mMeshHitSignal.Emit(handle, modelNode);
221 * @brief Returns collider mesh container
222 * @return Returns valid container
224 const ColliderMeshContainer& GetNodeColliderMeshContainer() const
226 return mColliderMeshes;
230 * @brief Registers child node with collidier mesh
232 * @param[in] node ModelNode to register
234 void RegisterColliderMesh(Scene3D::ModelNode& node);
237 * @brief Removes node/collider mesh from the register
238 * @param[in] node Child node to remove from the register
240 void RemoveColliderMesh(Scene3D::ModelNode& node);
244 * @brief Constructs a new Model.
245 * @param[in] modelUrl model file path.(e.g., glTF, and DLI).
246 * @param[in] resourceDirectoryUrl resource file path that includes binary, image etc.
248 Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
251 * A reference counted object may only be deleted by calling Unreference()
257 * @copydoc Toolkit::Control::OnInitialize
262 * @copydoc CustomActorImpl::OnSceneConnection()
264 void OnSceneConnection(int depth) override;
267 * @copydoc CustomActorImpl::OnSceneDisconnection()
269 void OnSceneDisconnection() override;
272 * @copydoc CustomActorImpl::OnSizeSet( const Vector3& size )
274 void OnSizeSet(const Vector3& size) override;
277 * @copydoc Toolkit::Control::GetNaturalSize
279 Vector3 GetNaturalSize() override;
282 * @copydoc Toolkit::Control::GetHeightForWidth()
284 float GetHeightForWidth(float width) override;
287 * @copydoc Toolkit::Control::GetWidthForHeight()
289 float GetWidthForHeight(float height) override;
292 * @copydoc Toolkit::Control::OnRelayout()
294 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
297 * @copydoc Toolkit::Control::IsResourceReady()
299 bool IsResourceReady() const override;
303 * @brief Create Model Root of this Model class.
305 void CreateModelRoot();
308 * @brief Scales the model to fit the control or to return to original size.
310 void ScaleModel(bool useCurrentSize);
313 * @brief Changes model anchor point to set the model at center or returns to the original model pivot.
315 void FitModelPosition();
318 * @brief Makes the input node cast shadow or not.
320 void UpdateCastShadowRecursively(Scene3D::ModelNode node, bool castShadow);
323 * @brief Makes the input node receive shadow or not.
325 void UpdateReceiveShadowRecursively(Scene3D::ModelNode node, bool receiveShadow);
328 * @brief Changes IBL information of the input node.
330 void UpdateImageBasedLightTextureRecursively(Scene3D::ModelNode node, Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels);
333 * @brief Changes IBL factor of the input node.
335 void UpdateImageBasedLightScaleFactorRecursively(Scene3D::ModelNode node, float iblScaleFactor);
338 * @brief Changes IBL textures of the input node.
340 void UpdateImageBasedLightTexture();
343 * @brief Changes IBL scale factor of the input node.
345 void UpdateImageBasedLightScaleFactor();
348 * @brief Apply self transform into inputed camera.
349 * Inputed camera must be configured by CameraParameter. Mean, inputed camera coordinate depend on Model.
350 * After this API finished, CameraActor coordinate system converted as DALi coordinate system.
352 * @param[in,out] camera CameraActor who need to apply model itself's transform
354 void ApplyCameraTransform(Dali::CameraActor camera) const;
356 public: // Overrides LightObserver Methods.
358 * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyShadowMapTexture()
360 void NotifyShadowMapTexture(Dali::Texture shadowMapTexture) override;
363 * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyImageBasedLightTexture()
365 void NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor, uint32_t specularMipmapLevels) override;
368 * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyImageBasedLightScaleFactor()
370 void NotifyImageBasedLightScaleFactor(float scaleFactor) override;
374 * @brief Asynchronously model loading finished.
376 void OnModelLoadComplete();
379 * @brief Asynchronously ibl diffusel image loading finished.
381 void OnIblDiffuseLoadComplete();
384 * @brief Asynchronously ibl specular image loading finished.
386 void OnIblSpecularLoadComplete();
389 * @brief Asynchronously ibl loading finished.
391 void OnIblLoadComplete();
394 * @brief Update model root scale when Model size property is updated.
396 void OnSizeNotification(Dali::PropertyNotification& source);
399 * @brief Reset Resource loading tasks.
401 void ResetResourceTasks();
404 * @brief Notify Resource Ready signal.
406 void NotifyResourceReady();
409 * @brief Create Model from loaded SceneDefinition.
414 * @brief Create Dali::Animation from loaded AnimationDefinitions.
416 void CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene);
419 * @brief Reset CameraData from loaded CameraParameters.
421 void ResetCameraParameters();
424 * @brief Collect ModelNode list by blendshape name
426 void UpdateBlendShapeNodeMap();
429 std::string mModelUrl;
430 std::string mResourceDirectoryUrl;
431 Scene3D::ModelNode mModelRoot;
432 std::vector<AnimationData> mAnimations;
433 std::vector<CameraData> mCameraParameters;
434 WeakHandle<Scene3D::SceneView> mParentSceneView;
435 Dali::PropertyNotification mSizeNotification;
438 Scene3D::Model::MeshHitSignalType mMeshHitSignal;
440 Dali::Scene3D::Loader::ShaderManagerPtr mShaderManager;
442 ColliderMeshContainer mColliderMeshes;
444 // List of ModelNode for name of blend shape.
445 BlendShapeModelNodeMap mBlendShapeModelNodeMap;
447 // Asynchronous loading variable
448 ModelLoadTaskPtr mModelLoadTask;
449 EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
450 EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
453 Dali::Texture mShadowMapTexture;
455 std::string mDiffuseIblUrl;
456 std::string mSpecularIblUrl;
458 Dali::Texture mDefaultSpecularTexture;
459 Dali::Texture mDefaultDiffuseTexture;
460 Dali::Texture mSceneSpecularTexture;
461 Dali::Texture mSceneDiffuseTexture;
462 Dali::Texture mSpecularTexture;
463 Dali::Texture mDiffuseTexture;
464 Vector3 mNaturalSize;
466 float mSceneIblScaleFactor;
467 float mIblScaleFactor;
468 uint32_t mSceneSpecularMipmapLevels;
469 uint32_t mSpecularMipmapLevels;
470 bool mModelChildrenSensitive;
471 bool mModelChildrenFocusable;
472 bool mModelResourceReady;
473 bool mIblDiffuseResourceReady;
474 bool mIblSpecularResourceReady;
475 bool mIblDiffuseDirty;
476 bool mIblSpecularDirty;
477 bool mIsShadowCasting;
478 bool mIsShadowReceiving;
481 } // namespace Internal
483 // Helpers for public-api forwarding methods
484 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
486 DALI_ASSERT_ALWAYS(obj);
487 Dali::RefObject& handle = obj.GetImplementation();
488 return static_cast<Dali::Scene3D::Internal::Model&>(handle);
491 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
493 DALI_ASSERT_ALWAYS(obj);
494 const Dali::RefObject& handle = obj.GetImplementation();
495 return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
498 } // namespace Scene3D
502 #endif // DALI_SCENE3D_INTERNAL_MODEL_H