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>
32 #include <dali-scene3d/internal/common/environment-map-load-task.h>
33 #include <dali-scene3d/internal/common/light-observer.h>
34 #include <dali-scene3d/internal/common/model-load-task.h>
35 #include <dali-scene3d/public-api/controls/model/model.h>
36 #include <dali-scene3d/public-api/controls/scene-view/scene-view.h>
37 #include <dali-scene3d/public-api/light/light.h>
38 #include <dali-scene3d/public-api/loader/load-result.h>
39 #include <dali-scene3d/public-api/model-components/model-node.h>
50 * @brief Impl class for Model.
52 class Model : public Dali::Toolkit::Internal::Control, public LightObserver
55 using AnimationData = std::pair<std::string, Dali::Animation>;
56 using CameraData = Loader::CameraParameters;
57 using BlendShapeModelNodeMap = std::map<std::string, std::vector<Scene3D::ModelNode>>;
60 * @copydoc Model::New()
62 static Dali::Scene3D::Model New(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
65 * @copydoc Model::GetModelRoot()
67 const Scene3D::ModelNode GetModelRoot() const;
70 * @copydoc Model::AddModelNode()
72 void AddModelNode(Scene3D::ModelNode modelNode);
75 * @copydoc Model::RemoveModelNode()
77 void RemoveModelNode(Scene3D::ModelNode modelNode);
80 * @copydoc Model::SetChildrenSensitive()
82 void SetChildrenSensitive(bool enable);
85 * @copydoc Model::GetChildrenSensitive()
87 bool GetChildrenSensitive() const;
90 * @copydoc Model::SetChildrenFocusable()
92 void SetChildrenFocusable(bool enable);
95 * @copydoc Model::GetChildrenFocusable()
97 bool GetChildrenFocusable() const;
100 * @copydoc Model::SetImageBasedLightSource()
102 void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
105 * @copydoc Model::SetImageBasedLightScaleFactor()
107 void SetImageBasedLightScaleFactor(float scaleFactor);
110 * @copydoc Model::GetImageBasedLightScaleFactor()
112 float GetImageBasedLightScaleFactor() const;
115 * @copydoc Model::GetAnimationCount()
117 uint32_t GetAnimationCount() const;
120 * @copydoc Model::GetAnimation()
122 Dali::Animation GetAnimation(uint32_t index) const;
125 * @copydoc Model::GetAnimation()
127 Dali::Animation GetAnimation(const std::string& name) const;
130 * @copydoc Model::GetCameraCount()
132 uint32_t GetCameraCount() const;
135 * @copydoc Model::GenerateCamera()
137 Dali::CameraActor GenerateCamera(uint32_t index) const;
140 * @copydoc Model::ApplyCamera()
142 bool ApplyCamera(uint32_t index, Dali::CameraActor camera) const;
145 * @copydoc Model::FindChildModelNodeByName()
147 Scene3D::ModelNode FindChildModelNodeByName(std::string_view nodeName);
150 * @copydoc Model::RetrieveBlendShapeNames()
152 void RetrieveBlendShapeNames(std::vector<std::string>& blendShapeNames) const;
155 * @copydoc Model::RetrieveModelNodesByBlendShapeName()
157 void RetrieveModelNodesByBlendShapeName(std::string_view blendShapeName, std::vector<Scene3D::ModelNode>& modelNodes) const;
160 * @copydoc Model::GenerateMotionDataAnimation()
162 Dali::Animation GenerateMotionDataAnimation(Scene3D::MotionData motionData);
165 * @copydoc Model::SetMotionData()
167 void SetMotionData(Scene3D::MotionData motionData);
171 * @brief Constructs a new Model.
172 * @param[in] modelUrl model file path.(e.g., glTF, and DLI).
173 * @param[in] resourceDirectoryUrl resource file path that includes binary, image etc.
175 Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
178 * A reference counted object may only be deleted by calling Unreference()
184 * @copydoc Toolkit::Control::OnInitialize
189 * @copydoc CustomActorImpl::OnSceneConnection()
191 void OnSceneConnection(int depth) override;
194 * @copydoc CustomActorImpl::OnSceneDisconnection()
196 void OnSceneDisconnection() override;
199 * @copydoc CustomActorImpl::OnSizeSet( const Vector3& size )
201 void OnSizeSet(const Vector3& size) override;
204 * @copydoc Toolkit::Control::GetNaturalSize
206 Vector3 GetNaturalSize() override;
209 * @copydoc Toolkit::Control::GetHeightForWidth()
211 float GetHeightForWidth(float width) override;
214 * @copydoc Toolkit::Control::GetWidthForHeight()
216 float GetWidthForHeight(float height) override;
219 * @copydoc Toolkit::Control::OnRelayout()
221 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
224 * @copydoc Toolkit::Control::IsResourceReady()
226 bool IsResourceReady() const override;
230 * @brief Create Model Root of this Model class.
232 void CreateModelRoot();
235 * @brief Scales the model to fit the control or to return to original size.
237 void ScaleModel(bool useCurrentSize);
240 * @brief Changes model anchor point to set the model at center or returns to the original model pivot.
242 void FitModelPosition();
245 * @brief Changes IBL information of the input node.
247 void UpdateImageBasedLightTextureRecursively(Scene3D::ModelNode node, Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels);
250 * @brief Changes IBL factor of the input node.
252 void UpdateImageBasedLightScaleFactorRecursively(Scene3D::ModelNode node, float iblScaleFactor);
255 * @brief Changes IBL textures of the input node.
257 void UpdateImageBasedLightTexture();
260 * @brief Changes IBL scale factor of the input node.
262 void UpdateImageBasedLightScaleFactor();
265 * @brief Apply self transform into inputed camera.
266 * Inputed camera must be configured by CameraParameter. Mean, inputed camera coordinate depend on Model.
267 * After this API finished, CameraActor coordinate system converted as DALi coordinate system.
269 * @param[in,out] camera CameraActor who need to apply model itself's transform
271 void ApplyCameraTransform(Dali::CameraActor camera) const;
273 public: // Overrides LightObserver Methods.
275 * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyImageBasedLightTexture()
277 void NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor, uint32_t specularMipmapLevels) override;
280 * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyImageBasedLightScaleFactor()
282 void NotifyImageBasedLightScaleFactor(float scaleFactor) override;
285 * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyLightAdded()
287 void NotifyLightAdded(uint32_t lightIndex, Scene3D::Light light) override;
290 * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyLightRemoved()
292 void NotifyLightRemoved(uint32_t lightIndex) override;
296 * @brief Asynchronously model loading finished.
298 void OnModelLoadComplete();
301 * @brief Asynchronously ibl diffusel image loading finished.
303 void OnIblDiffuseLoadComplete();
306 * @brief Asynchronously ibl specular image loading finished.
308 void OnIblSpecularLoadComplete();
311 * @brief Asynchronously ibl loading finished.
313 void OnIblLoadComplete();
316 * @brief Update model root scale when Model size property is updated.
318 void OnSizeNotification(Dali::PropertyNotification& source);
321 * @brief Reset Resource loading tasks.
323 void ResetResourceTasks();
326 * @brief Reset a Resource loading task.
328 void ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask);
331 * @brief Notify Resource Ready signal.
333 void NotifyResourceReady();
336 * @brief Create Model from loaded SceneDefinition.
341 * @brief Create Dali::Animation from loaded AnimationDefinitions.
343 void CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene);
346 * @brief Reset CameraData from loaded CameraParameters.
348 void ResetCameraParameters();
351 * @brief Collect ModelNode list by blendshape name
353 void UpdateBlendShapeNodeMap();
356 std::string mModelUrl;
357 std::string mResourceDirectoryUrl;
358 Scene3D::ModelNode mModelRoot;
359 std::vector<AnimationData> mAnimations;
360 std::vector<CameraData> mCameraParameters;
361 WeakHandle<Scene3D::SceneView> mParentSceneView;
362 Dali::PropertyNotification mSizeNotification;
365 std::vector<Scene3D::Light> mLights;
367 // List of ModelNode for name of blend shape.
368 BlendShapeModelNodeMap mBlendShapeModelNodeMap;
370 // Asynchronous loading variable
371 ModelLoadTaskPtr mModelLoadTask;
372 EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
373 EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
375 std::string mDiffuseIblUrl;
376 std::string mSpecularIblUrl;
378 Dali::Texture mDefaultSpecularTexture;
379 Dali::Texture mDefaultDiffuseTexture;
380 Dali::Texture mSceneSpecularTexture;
381 Dali::Texture mSceneDiffuseTexture;
382 Dali::Texture mSpecularTexture;
383 Dali::Texture mDiffuseTexture;
384 Vector3 mNaturalSize;
386 float mSceneIblScaleFactor;
387 float mIblScaleFactor;
388 uint32_t mSceneSpecularMipmapLevels;
389 uint32_t mSpecularMipmapLevels;
390 bool mModelChildrenSensitive;
391 bool mModelChildrenFocusable;
392 bool mModelResourceReady;
393 bool mIblDiffuseResourceReady;
394 bool mIblSpecularResourceReady;
395 bool mIblDiffuseDirty;
396 bool mIblSpecularDirty;
399 } // namespace Internal
401 // Helpers for public-api forwarding methods
402 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
404 DALI_ASSERT_ALWAYS(obj);
405 Dali::RefObject& handle = obj.GetImplementation();
406 return static_cast<Dali::Scene3D::Internal::Model&>(handle);
409 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
411 DALI_ASSERT_ALWAYS(obj);
412 const Dali::RefObject& handle = obj.GetImplementation();
413 return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
416 } // namespace Scene3D
420 #endif // DALI_SCENE3D_INTERNAL_MODEL_H