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;
286 * @brief Asynchronously model loading finished.
288 void OnModelLoadComplete();
291 * @brief Asynchronously ibl diffusel image loading finished.
293 void OnIblDiffuseLoadComplete();
296 * @brief Asynchronously ibl specular image loading finished.
298 void OnIblSpecularLoadComplete();
301 * @brief Asynchronously ibl loading finished.
303 void OnIblLoadComplete();
306 * @brief Update model root scale when Model size property is updated.
308 void OnSizeNotification(Dali::PropertyNotification& source);
311 * @brief Reset Resource loading tasks.
313 void ResetResourceTasks();
316 * @brief Reset a Resource loading task.
318 void ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask);
321 * @brief Notify Resource Ready signal.
323 void NotifyResourceReady();
326 * @brief Create Model from loaded SceneDefinition.
331 * @brief Create Dali::Animation from loaded AnimationDefinitions.
333 void CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene);
336 * @brief Reset CameraData from loaded CameraParameters.
338 void ResetCameraParameters();
341 * @brief Collect ModelNode list by blendshape name
343 void UpdateBlendShapeNodeMap();
346 std::string mModelUrl;
347 std::string mResourceDirectoryUrl;
348 Scene3D::ModelNode mModelRoot;
349 std::vector<AnimationData> mAnimations;
350 std::vector<CameraData> mCameraParameters;
351 WeakHandle<Scene3D::SceneView> mParentSceneView;
352 Dali::PropertyNotification mSizeNotification;
354 Dali::Scene3D::Loader::ShaderManagerPtr mShaderManager;
356 // List of ModelNode for name of blend shape.
357 BlendShapeModelNodeMap mBlendShapeModelNodeMap;
359 // Asynchronous loading variable
360 ModelLoadTaskPtr mModelLoadTask;
361 EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
362 EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
364 std::string mDiffuseIblUrl;
365 std::string mSpecularIblUrl;
367 Dali::Texture mDefaultSpecularTexture;
368 Dali::Texture mDefaultDiffuseTexture;
369 Dali::Texture mSceneSpecularTexture;
370 Dali::Texture mSceneDiffuseTexture;
371 Dali::Texture mSpecularTexture;
372 Dali::Texture mDiffuseTexture;
373 Vector3 mNaturalSize;
375 float mSceneIblScaleFactor;
376 float mIblScaleFactor;
377 uint32_t mSceneSpecularMipmapLevels;
378 uint32_t mSpecularMipmapLevels;
379 bool mModelChildrenSensitive;
380 bool mModelChildrenFocusable;
381 bool mModelResourceReady;
382 bool mIblDiffuseResourceReady;
383 bool mIblSpecularResourceReady;
384 bool mIblDiffuseDirty;
385 bool mIblSpecularDirty;
388 } // namespace Internal
390 // Helpers for public-api forwarding methods
391 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
393 DALI_ASSERT_ALWAYS(obj);
394 Dali::RefObject& handle = obj.GetImplementation();
395 return static_cast<Dali::Scene3D::Internal::Model&>(handle);
398 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
400 DALI_ASSERT_ALWAYS(obj);
401 const Dali::RefObject& handle = obj.GetImplementation();
402 return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
405 } // namespace Scene3D
409 #endif // DALI_SCENE3D_INTERNAL_MODEL_H