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 Scene3D::Model::MeshHitSignal()
179 Scene3D::Model::MeshHitSignalType& MeshHitSignal()
181 return mMeshHitSignal;
185 * @brief Emits MeshHitSignal
186 * @param[in] modelNode ModelNode that has been hit
187 * @return Result of hit handling.
189 bool EmitMeshHitSignal(Scene3D::ModelNode modelNode)
192 if(!mMeshHitSignal.Empty())
194 Scene3D::Model handle(GetOwner());
195 retVal = mMeshHitSignal.Emit(handle, modelNode);
201 * @brief Returns collider mesh container
202 * @return Returns valid container
204 const ColliderMeshContainer& GetNodeColliderMeshContainer() const
206 return mColliderMeshes;
210 * @brief Registers child node with collidier mesh
212 * @param[in] node ModelNode to register
214 void RegisterColliderMesh(Scene3D::ModelNode& node);
217 * @brief Removes node/collider mesh from the register
218 * @param[in] node Child node to remove from the register
220 void RemoveColliderMesh(Scene3D::ModelNode& node);
224 * @brief Constructs a new Model.
225 * @param[in] modelUrl model file path.(e.g., glTF, and DLI).
226 * @param[in] resourceDirectoryUrl resource file path that includes binary, image etc.
228 Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
231 * A reference counted object may only be deleted by calling Unreference()
237 * @copydoc Toolkit::Control::OnInitialize
242 * @copydoc CustomActorImpl::OnSceneConnection()
244 void OnSceneConnection(int depth) override;
247 * @copydoc CustomActorImpl::OnSceneDisconnection()
249 void OnSceneDisconnection() override;
252 * @copydoc CustomActorImpl::OnSizeSet( const Vector3& size )
254 void OnSizeSet(const Vector3& size) override;
257 * @copydoc Toolkit::Control::GetNaturalSize
259 Vector3 GetNaturalSize() override;
262 * @copydoc Toolkit::Control::GetHeightForWidth()
264 float GetHeightForWidth(float width) override;
267 * @copydoc Toolkit::Control::GetWidthForHeight()
269 float GetWidthForHeight(float height) override;
272 * @copydoc Toolkit::Control::OnRelayout()
274 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
277 * @copydoc Toolkit::Control::IsResourceReady()
279 bool IsResourceReady() const override;
283 * @brief Create Model Root of this Model class.
285 void CreateModelRoot();
288 * @brief Scales the model to fit the control or to return to original size.
290 void ScaleModel(bool useCurrentSize);
293 * @brief Changes model anchor point to set the model at center or returns to the original model pivot.
295 void FitModelPosition();
298 * @brief Changes IBL information of the input node.
300 void UpdateImageBasedLightTextureRecursively(Scene3D::ModelNode node, Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels);
303 * @brief Changes IBL factor of the input node.
305 void UpdateImageBasedLightScaleFactorRecursively(Scene3D::ModelNode node, float iblScaleFactor);
308 * @brief Changes IBL textures of the input node.
310 void UpdateImageBasedLightTexture();
313 * @brief Changes IBL scale factor of the input node.
315 void UpdateImageBasedLightScaleFactor();
318 * @brief Apply self transform into inputed camera.
319 * Inputed camera must be configured by CameraParameter. Mean, inputed camera coordinate depend on Model.
320 * After this API finished, CameraActor coordinate system converted as DALi coordinate system.
322 * @param[in,out] camera CameraActor who need to apply model itself's transform
324 void ApplyCameraTransform(Dali::CameraActor camera) const;
326 public: // Overrides LightObserver Methods.
328 * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyShadowMapTexture()
330 void NotifyShadowMapTexture(Dali::Texture shadowMapTexture) override;
333 * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyImageBasedLightTexture()
335 void NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor, uint32_t specularMipmapLevels) override;
338 * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyImageBasedLightScaleFactor()
340 void NotifyImageBasedLightScaleFactor(float scaleFactor) override;
344 * @brief Asynchronously model loading finished.
346 void OnModelLoadComplete();
349 * @brief Asynchronously ibl diffusel image loading finished.
351 void OnIblDiffuseLoadComplete();
354 * @brief Asynchronously ibl specular image loading finished.
356 void OnIblSpecularLoadComplete();
359 * @brief Asynchronously ibl loading finished.
361 void OnIblLoadComplete();
364 * @brief Update model root scale when Model size property is updated.
366 void OnSizeNotification(Dali::PropertyNotification& source);
369 * @brief Reset Resource loading tasks.
371 void ResetResourceTasks();
374 * @brief Notify Resource Ready signal.
376 void NotifyResourceReady();
379 * @brief Create Model from loaded SceneDefinition.
384 * @brief Create Dali::Animation from loaded AnimationDefinitions.
386 void CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene);
389 * @brief Reset CameraData from loaded CameraParameters.
391 void ResetCameraParameters();
394 * @brief Collect ModelNode list by blendshape name
396 void UpdateBlendShapeNodeMap();
399 std::string mModelUrl;
400 std::string mResourceDirectoryUrl;
401 Scene3D::ModelNode mModelRoot;
402 std::vector<AnimationData> mAnimations;
403 std::vector<CameraData> mCameraParameters;
404 WeakHandle<Scene3D::SceneView> mParentSceneView;
405 Dali::PropertyNotification mSizeNotification;
408 Scene3D::Model::MeshHitSignalType mMeshHitSignal;
410 Dali::Scene3D::Loader::ShaderManagerPtr mShaderManager;
412 ColliderMeshContainer mColliderMeshes;
414 // List of ModelNode for name of blend shape.
415 BlendShapeModelNodeMap mBlendShapeModelNodeMap;
417 // Asynchronous loading variable
418 ModelLoadTaskPtr mModelLoadTask;
419 EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
420 EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
423 Dali::Texture mShadowMapTexture;
425 std::string mDiffuseIblUrl;
426 std::string mSpecularIblUrl;
428 Dali::Texture mDefaultSpecularTexture;
429 Dali::Texture mDefaultDiffuseTexture;
430 Dali::Texture mSceneSpecularTexture;
431 Dali::Texture mSceneDiffuseTexture;
432 Dali::Texture mSpecularTexture;
433 Dali::Texture mDiffuseTexture;
434 Vector3 mNaturalSize;
436 float mSceneIblScaleFactor;
437 float mIblScaleFactor;
438 uint32_t mSceneSpecularMipmapLevels;
439 uint32_t mSpecularMipmapLevels;
440 bool mModelChildrenSensitive;
441 bool mModelChildrenFocusable;
442 bool mModelResourceReady;
443 bool mIblDiffuseResourceReady;
444 bool mIblSpecularResourceReady;
445 bool mIblDiffuseDirty;
446 bool mIblSpecularDirty;
449 } // namespace Internal
451 // Helpers for public-api forwarding methods
452 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
454 DALI_ASSERT_ALWAYS(obj);
455 Dali::RefObject& handle = obj.GetImplementation();
456 return static_cast<Dali::Scene3D::Internal::Model&>(handle);
459 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
461 DALI_ASSERT_ALWAYS(obj);
462 const Dali::RefObject& handle = obj.GetImplementation();
463 return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
466 } // namespace Scene3D
470 #endif // DALI_SCENE3D_INTERNAL_MODEL_H