1 #ifndef DALI_SCENE3D_INTERNAL_MODEL_H
2 #define DALI_SCENE3D_INTERNAL_MODEL_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-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
213 * @param[in] mesh Collider mesh to associate with model node
215 void RegisterColliderMesh(Scene3D::ModelNode& node, const Dali::Scene3D::Algorithm::ColliderMesh& mesh);
218 * @brief Removes node/collider mesh from the register
219 * @param[in] node Child node to remove from the register
221 void RemoveColliderMesh(Scene3D::ModelNode& node);
225 * @brief Constructs a new Model.
226 * @param[in] modelUrl model file path.(e.g., glTF, and DLI).
227 * @param[in] resourceDirectoryUrl resource file path that includes binary, image etc.
229 Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
232 * A reference counted object may only be deleted by calling Unreference()
238 * @copydoc Toolkit::Control::OnInitialize
243 * @copydoc CustomActorImpl::OnSceneConnection()
245 void OnSceneConnection(int depth) override;
248 * @copydoc CustomActorImpl::OnSceneDisconnection()
250 void OnSceneDisconnection() override;
253 * @copydoc CustomActorImpl::OnSizeSet( const Vector3& size )
255 void OnSizeSet(const Vector3& size) override;
258 * @copydoc Toolkit::Control::GetNaturalSize
260 Vector3 GetNaturalSize() override;
263 * @copydoc Toolkit::Control::GetHeightForWidth()
265 float GetHeightForWidth(float width) override;
268 * @copydoc Toolkit::Control::GetWidthForHeight()
270 float GetWidthForHeight(float height) override;
273 * @copydoc Toolkit::Control::OnRelayout()
275 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
278 * @copydoc Toolkit::Control::IsResourceReady()
280 bool IsResourceReady() const override;
284 * @brief Create Model Root of this Model class.
286 void CreateModelRoot();
289 * @brief Scales the model to fit the control or to return to original size.
291 void ScaleModel(bool useCurrentSize);
294 * @brief Changes model anchor point to set the model at center or returns to the original model pivot.
296 void FitModelPosition();
299 * @brief Changes IBL information of the input node.
301 void UpdateImageBasedLightTextureRecursively(Scene3D::ModelNode node, Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels);
304 * @brief Changes IBL factor of the input node.
306 void UpdateImageBasedLightScaleFactorRecursively(Scene3D::ModelNode node, float iblScaleFactor);
309 * @brief Changes IBL textures of the input node.
311 void UpdateImageBasedLightTexture();
314 * @brief Changes IBL scale factor of the input node.
316 void UpdateImageBasedLightScaleFactor();
319 * @brief Apply self transform into inputed camera.
320 * Inputed camera must be configured by CameraParameter. Mean, inputed camera coordinate depend on Model.
321 * After this API finished, CameraActor coordinate system converted as DALi coordinate system.
323 * @param[in,out] camera CameraActor who need to apply model itself's transform
325 void ApplyCameraTransform(Dali::CameraActor camera) const;
327 public: // Overrides LightObserver Methods.
329 * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyShadowMapTexture()
331 void NotifyShadowMapTexture(Dali::Texture shadowMapTexture) override;
334 * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyImageBasedLightTexture()
336 void NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor, uint32_t specularMipmapLevels) override;
339 * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyImageBasedLightScaleFactor()
341 void NotifyImageBasedLightScaleFactor(float scaleFactor) override;
345 * @brief Asynchronously model loading finished.
347 void OnModelLoadComplete();
350 * @brief Asynchronously ibl diffusel image loading finished.
352 void OnIblDiffuseLoadComplete();
355 * @brief Asynchronously ibl specular image loading finished.
357 void OnIblSpecularLoadComplete();
360 * @brief Asynchronously ibl loading finished.
362 void OnIblLoadComplete();
365 * @brief Update model root scale when Model size property is updated.
367 void OnSizeNotification(Dali::PropertyNotification& source);
370 * @brief Reset Resource loading tasks.
372 void ResetResourceTasks();
375 * @brief Reset a Resource loading task.
377 void ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask);
380 * @brief Notify Resource Ready signal.
382 void NotifyResourceReady();
385 * @brief Create Model from loaded SceneDefinition.
390 * @brief Create Dali::Animation from loaded AnimationDefinitions.
392 void CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene);
395 * @brief Reset CameraData from loaded CameraParameters.
397 void ResetCameraParameters();
400 * @brief Collect ModelNode list by blendshape name
402 void UpdateBlendShapeNodeMap();
405 std::string mModelUrl;
406 std::string mResourceDirectoryUrl;
407 Scene3D::ModelNode mModelRoot;
408 std::vector<AnimationData> mAnimations;
409 std::vector<CameraData> mCameraParameters;
410 WeakHandle<Scene3D::SceneView> mParentSceneView;
411 Dali::PropertyNotification mSizeNotification;
414 Scene3D::Model::MeshHitSignalType mMeshHitSignal;
416 Dali::Scene3D::Loader::ShaderManagerPtr mShaderManager;
418 ColliderMeshContainer mColliderMeshes;
420 // List of ModelNode for name of blend shape.
421 BlendShapeModelNodeMap mBlendShapeModelNodeMap;
423 // Asynchronous loading variable
424 ModelLoadTaskPtr mModelLoadTask;
425 EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
426 EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
429 Dali::Texture mShadowMapTexture;
431 std::string mDiffuseIblUrl;
432 std::string mSpecularIblUrl;
434 Dali::Texture mDefaultSpecularTexture;
435 Dali::Texture mDefaultDiffuseTexture;
436 Dali::Texture mSceneSpecularTexture;
437 Dali::Texture mSceneDiffuseTexture;
438 Dali::Texture mSpecularTexture;
439 Dali::Texture mDiffuseTexture;
440 Vector3 mNaturalSize;
442 float mSceneIblScaleFactor;
443 float mIblScaleFactor;
444 uint32_t mSceneSpecularMipmapLevels;
445 uint32_t mSpecularMipmapLevels;
446 bool mModelChildrenSensitive;
447 bool mModelChildrenFocusable;
448 bool mModelResourceReady;
449 bool mIblDiffuseResourceReady;
450 bool mIblSpecularResourceReady;
451 bool mIblDiffuseDirty;
452 bool mIblSpecularDirty;
455 } // namespace Internal
457 // Helpers for public-api forwarding methods
458 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
460 DALI_ASSERT_ALWAYS(obj);
461 Dali::RefObject& handle = obj.GetImplementation();
462 return static_cast<Dali::Scene3D::Internal::Model&>(handle);
465 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
467 DALI_ASSERT_ALWAYS(obj);
468 const Dali::RefObject& handle = obj.GetImplementation();
469 return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
472 } // namespace Scene3D
476 #endif // DALI_SCENE3D_INTERNAL_MODEL_H