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/public-api/actors/camera-actor.h>
24 #include <dali/public-api/actors/layer.h>
25 #include <dali/public-api/animation/animation.h>
26 #include <dali/public-api/object/property-notification.h>
27 #include <dali/public-api/object/weak-handle.h>
28 #include <dali/public-api/rendering/texture.h>
31 #include <dali-scene3d/internal/common/environment-map-load-task.h>
32 #include <dali-scene3d/internal/common/image-based-light-observer.h>
33 #include <dali-scene3d/internal/common/model-load-task.h>
34 #include <dali-scene3d/public-api/controls/model/model.h>
35 #include <dali-scene3d/public-api/controls/scene-view/scene-view.h>
36 #include <dali-scene3d/public-api/loader/load-result.h>
37 #include <dali-scene3d/public-api/model-components/model-node.h>
48 * @brief Impl class for Model.
50 class Model : public Dali::Toolkit::Internal::Control, public ImageBasedLightObserver
53 using AnimationData = std::pair<std::string, Dali::Animation>;
54 using CameraData = Loader::CameraParameters;
57 * @copydoc Model::New()
59 static Dali::Scene3D::Model New(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
62 * @copydoc Model::GetModelRoot()
64 const Scene3D::ModelNode GetModelRoot() const;
67 * @copydoc Model::AddModelNode()
69 void AddModelNode(Scene3D::ModelNode modelNode);
72 * @copydoc Model::RemoveModelNode()
74 void RemoveModelNode(Scene3D::ModelNode modelNode);
77 * @copydoc Model::SetChildrenSensitive()
79 void SetChildrenSensitive(bool enable);
82 * @copydoc Model::GetChildrenSensitive()
84 bool GetChildrenSensitive() const;
87 * @copydoc Model::SetChildrenFocusable()
89 void SetChildrenFocusable(bool enable);
92 * @copydoc Model::GetChildrenFocusable()
94 bool GetChildrenFocusable() const;
97 * @copydoc Model::SetImageBasedLightSource()
99 void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
102 * @copydoc Model::SetImageBasedLightScaleFactor()
104 void SetImageBasedLightScaleFactor(float scaleFactor);
107 * @copydoc Model::GetImageBasedLightScaleFactor()
109 float GetImageBasedLightScaleFactor() const;
112 * @copydoc Model::GetAnimationCount()
114 uint32_t GetAnimationCount() const;
117 * @copydoc Model::GetAnimation()
119 Dali::Animation GetAnimation(uint32_t index) const;
122 * @copydoc Model::GetAnimation()
124 Dali::Animation GetAnimation(const std::string& name) const;
127 * @copydoc Model::GetCameraCount()
129 uint32_t GetCameraCount() const;
132 * @copydoc Model::GenerateCamera()
134 Dali::CameraActor GenerateCamera(uint32_t index) const;
137 * @copydoc Model::ApplyCamera()
139 bool ApplyCamera(uint32_t index, Dali::CameraActor camera) const;
142 * @copydoc Model::FindChildModelNodeByName()
144 Scene3D::ModelNode FindChildModelNodeByName(std::string_view nodeName);
148 * @brief Constructs a new Model.
149 * @param[in] modelUrl model file path.(e.g., glTF, and DLI).
150 * @param[in] resourceDirectoryUrl resource file path that includes binary, image etc.
152 Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
155 * A reference counted object may only be deleted by calling Unreference()
161 * @copydoc Toolkit::Control::OnInitialize
166 * @copydoc CustomActorImpl::OnSceneConnection()
168 void OnSceneConnection(int depth) override;
171 * @copydoc CustomActorImpl::OnSceneDisconnection()
173 void OnSceneDisconnection() override;
176 * @copydoc CustomActorImpl::OnSizeSet( const Vector3& size )
178 void OnSizeSet(const Vector3& size) override;
181 * @copydoc Toolkit::Control::GetNaturalSize
183 Vector3 GetNaturalSize() override;
186 * @copydoc Toolkit::Control::GetHeightForWidth()
188 float GetHeightForWidth(float width) override;
191 * @copydoc Toolkit::Control::GetWidthForHeight()
193 float GetWidthForHeight(float height) override;
196 * @copydoc Toolkit::Control::OnRelayout()
198 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
201 * @copydoc Toolkit::Control::IsResourceReady()
203 bool IsResourceReady() const override;
207 * @brief Create Model Root of this Model class.
209 void CreateModelRoot();
212 * @brief Scales the model to fit the control or to return to original size.
214 void ScaleModel(bool useCurrentSize);
217 * @brief Changes model anchor point to set the model at center or returns to the original model pivot.
219 void FitModelPosition();
222 * @brief Changes IBL information of the input node.
224 void UpdateImageBasedLightTextureRecursively(Scene3D::ModelNode node, Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels);
227 * @brief Changes IBL factor of the input node.
229 void UpdateImageBasedLightScaleFactorRecursively(Scene3D::ModelNode node, float iblScaleFactor);
232 * @brief Changes IBL textures of the input node.
234 void UpdateImageBasedLightTexture();
237 * @brief Changes IBL scale factor of the input node.
239 void UpdateImageBasedLightScaleFactor();
242 * @brief Apply self transform into inputed camera.
243 * Inputed camera must be configured by CameraParameter. Mean, inputed camera coordinate depend on Model.
244 * After this API finished, CameraActor coordinate system converted as DALi coordinate system.
246 * @param[in,out] camera CameraActor who need to apply model itself's transform
248 void ApplyCameraTransform(Dali::CameraActor camera) const;
250 public: // Overrides ImageBasedLightObserver Methods.
252 * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightTexture()
254 void NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor, uint32_t specularMipmapLevels) override;
257 * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightScaleFactor()
259 void NotifyImageBasedLightScaleFactor(float scaleFactor) override;
263 * @brief Asynchronously model loading finished.
265 void OnModelLoadComplete();
268 * @brief Asynchronously ibl diffusel image loading finished.
270 void OnIblDiffuseLoadComplete();
273 * @brief Asynchronously ibl specular image loading finished.
275 void OnIblSpecularLoadComplete();
278 * @brief Asynchronously ibl loading finished.
280 void OnIblLoadComplete();
283 * @brief Update model root scale when Model size property is updated.
285 void OnSizeNotification(Dali::PropertyNotification& source);
288 * @brief Reset Resource loading tasks.
290 void ResetResourceTasks();
293 * @brief Reset a Resource loading task.
295 void ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask);
298 * @brief Notify Resource Ready signal.
300 void NotifyResourceReady();
303 * @brief Create Model from loaded SceneDefinition.
308 * @brief Create Dali::Animation from loaded AnimationDefinitions.
310 void CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene);
313 * @brief Reset CameraData from loaded CameraParameters.
315 void ResetCameraParameters();
318 std::string mModelUrl;
319 std::string mResourceDirectoryUrl;
320 Scene3D::ModelNode mModelRoot;
321 std::vector<AnimationData> mAnimations;
322 std::vector<CameraData> mCameraParameters;
323 WeakHandle<Scene3D::SceneView> mParentSceneView;
324 Dali::PropertyNotification mSizeNotification;
326 // Asynchronous loading variable
327 ModelLoadTaskPtr mModelLoadTask;
328 EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
329 EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
331 std::string mDiffuseIblUrl;
332 std::string mSpecularIblUrl;
334 Dali::Texture mDefaultSpecularTexture;
335 Dali::Texture mDefaultDiffuseTexture;
336 Dali::Texture mSceneSpecularTexture;
337 Dali::Texture mSceneDiffuseTexture;
338 Dali::Texture mSpecularTexture;
339 Dali::Texture mDiffuseTexture;
340 Vector3 mNaturalSize;
342 float mSceneIblScaleFactor;
343 float mIblScaleFactor;
344 uint32_t mSceneSpecularMipmapLevels;
345 uint32_t mSpecularMipmapLevels;
346 bool mModelChildrenSensitive;
347 bool mModelChildrenFocusable;
348 bool mModelResourceReady;
349 bool mIblDiffuseResourceReady;
350 bool mIblSpecularResourceReady;
351 bool mIblDiffuseDirty;
352 bool mIblSpecularDirty;
355 } // namespace Internal
357 // Helpers for public-api forwarding methods
358 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
360 DALI_ASSERT_ALWAYS(obj);
361 Dali::RefObject& handle = obj.GetImplementation();
362 return static_cast<Dali::Scene3D::Internal::Model&>(handle);
365 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
367 DALI_ASSERT_ALWAYS(obj);
368 const Dali::RefObject& handle = obj.GetImplementation();
369 return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
372 } // namespace Scene3D
376 #endif // DALI_SCENE3D_INTERNAL_MODEL_H