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