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/weak-handle.h>
27 #include <dali/public-api/rendering/texture.h>
30 #include <dali-scene3d/internal/common/environment-map-load-task.h>
31 #include <dali-scene3d/internal/common/image-based-light-observer.h>
32 #include <dali-scene3d/internal/common/model-load-task.h>
33 #include <dali-scene3d/public-api/controls/model/model.h>
34 #include <dali-scene3d/public-api/controls/scene-view/scene-view.h>
45 * @brief Impl class for Model.
47 class Model : public Dali::Toolkit::Internal::Control, public ImageBasedLightObserver
50 using AnimationData = std::pair<std::string, Dali::Animation>;
51 using CameraData = Loader::CameraParameters;
54 * @brief Creates a new Model.
56 * @return A public handle to the newly allocated Model.
58 static Dali::Scene3D::Model New(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
61 * @copydoc Model::GetModelRoot()
63 const Actor GetModelRoot() const;
66 * @copydoc Model::SetChildrenSensitive()
68 void SetChildrenSensitive(bool enable);
71 * @copydoc Model::GetChildrenSensitive()
73 bool GetChildrenSensitive() const;
76 * @copydoc Model::SetChildrenFocusable()
78 void SetChildrenFocusable(bool enable);
81 * @copydoc Model::GetChildrenFocusable()
83 bool GetChildrenFocusable() const;
86 * @copydoc Model::SetImageBasedLightSource()
88 void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
91 * @copydoc Model::SetImageBasedLightTexture()
93 void SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor);
96 * @copydoc Model::SetImageBasedLightScaleFactor()
98 void SetImageBasedLightScaleFactor(float scaleFactor);
101 * @copydoc Model::GetImageBasedLightScaleFactor()
103 float GetImageBasedLightScaleFactor() const;
106 * @copydoc Model::GetAnimationCount()
108 uint32_t GetAnimationCount() const;
111 * @copydoc Model::GetAnimation()
113 Dali::Animation GetAnimation(uint32_t index) const;
116 * @copydoc Model::GetAnimation()
118 Dali::Animation GetAnimation(const std::string& name) const;
121 * @copydoc Model::GetCameraCount()
123 uint32_t GetCameraCount() const;
126 * @copydoc Model::GenerateCamera()
128 Dali::CameraActor GenerateCamera(uint32_t index) const;
131 * @copydoc Model::ApplyCamera()
133 bool ApplyCamera(uint32_t index, Dali::CameraActor camera) const;
137 * @brief Constructs a new Model.
139 Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
142 * A reference counted object may only be deleted by calling Unreference()
148 * @copydoc Toolkit::Control::OnInitialize
153 * @copydoc CustomActorImpl::OnSceneConnection()
155 void OnSceneConnection(int depth) override;
158 * @copydoc CustomActorImpl::OnSceneDisconnection()
160 void OnSceneDisconnection() override;
163 * @copydoc Toolkit::Control::GetNaturalSize
165 Vector3 GetNaturalSize() override;
168 * @copydoc Toolkit::Control::GetHeightForWidth()
170 float GetHeightForWidth(float width) override;
173 * @copydoc Toolkit::Control::GetWidthForHeight()
175 float GetWidthForHeight(float height) override;
178 * @copydoc Toolkit::Control::OnRelayout()
180 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
183 * @copydoc Toolkit::Control::IsResourceReady()
185 bool IsResourceReady() const override;
189 * @brief Scales the model to fit the control or to return to original size.
194 * @brief Changes model anchor point to set the model at center or returns to the original model pivot.
196 void FitModelPosition();
199 * @brief Changes IBL information of the input node.
201 void CollectRenderableActor(Actor actor);
204 * @brief Changes IBL textures of the input node.
206 void UpdateImageBasedLightTexture();
209 * @brief Changes IBL scale factor of the input node.
211 void UpdateImageBasedLightScaleFactor();
214 * @brief Apply self transform into inputed camera.
215 * Inputed camera must be configured by CameraParameter. Mean, inputed camera coordinate depend on Model.
216 * After this API finished, CameraActor coordinate system converted as DALi coordinate system.
218 * @param[in,out] camera CameraActor who need to apply model itself's transform
220 void ApplyCameraTransform(Dali::CameraActor camera) const;
222 public: // Overrides ImageBasedLightObserver Methods.
224 * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightTexture()
226 void NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor) override;
229 * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightScaleFactor()
231 void NotifyImageBasedLightScaleFactor(float scaleFactor) override;
235 * @brief Asynchronously model loading finished.
237 void OnModelLoadComplete();
240 * @brief Asynchronously ibl diffusel image loading finished.
242 void OnIblDiffuseLoadComplete();
245 * @brief Asynchronously ibl specular image loading finished.
247 void OnIblSpecularLoadComplete();
250 * @brief Asynchronously ibl loading finished.
252 void OnIblLoadComplete();
255 * @brief Reset Resource loading tasks.
257 void ResetResourceTasks();
260 * @brief Reset a Resource loading task.
262 void ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask);
265 * @brief Request to load a Ibl texture asynchronously
267 void RequestLoadIblTexture(EnvironmentMapLoadTaskPtr asyncLoadTask, const std::string& url);
270 * @brief Notify Resource Ready signal.
272 void NotifyResourceReady();
275 * @brief Create Model from loaded SceneDefinition.
280 * @brief Create Dali::Animation from loaded AnimationDefinitions.
282 void CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene);
285 * @brief Reset CameraData from loaded CameraParameters.
287 void ResetCameraParameters();
290 std::string mModelUrl;
291 std::string mResourceDirectoryUrl;
292 Dali::Actor mModelRoot;
293 std::vector<AnimationData> mAnimations;
294 std::vector<CameraData> mCameraParameters;
295 std::vector<WeakHandle<Actor>> mRenderableActors;
296 WeakHandle<Scene3D::SceneView> mParentSceneView;
298 // Asynchronous loading variable
299 ModelLoadTaskPtr mModelLoadTask;
300 EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
301 EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
303 std::string mDiffuseIblUrl;
304 std::string mSpecularIblUrl;
306 // TODO: This default texture can be removed after 3D Resource Cache is added.
307 Dali::Texture mDefaultSpecularTexture;
308 Dali::Texture mDefaultDiffuseTexture;
309 Dali::Texture mSceneSpecularTexture;
310 Dali::Texture mSceneDiffuseTexture;
311 Dali::Texture mSpecularTexture;
312 Dali::Texture mDiffuseTexture;
313 Vector3 mNaturalSize;
315 float mSceneIblScaleFactor;
316 float mIblScaleFactor;
317 bool mModelChildrenSensitive;
318 bool mModelChildrenFocusable;
319 bool mModelResourceReady;
320 bool mIblDiffuseResourceReady;
321 bool mIblSpecularResourceReady;
322 bool mIblDiffuseDirty;
323 bool mIblSpecularDirty;
326 } // namespace Internal
328 // Helpers for public-api forwarding methods
329 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
331 DALI_ASSERT_ALWAYS(obj);
332 Dali::RefObject& handle = obj.GetImplementation();
333 return static_cast<Dali::Scene3D::Internal::Model&>(handle);
336 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
338 DALI_ASSERT_ALWAYS(obj);
339 const Dali::RefObject& handle = obj.GetImplementation();
340 return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
343 } // namespace Scene3D
347 #endif // DALI_SCENE3D_INTERNAL_MODEL_H