1 #ifndef DALI_SCENE3D_INTERNAL_MODEL_H
2 #define DALI_SCENE3D_INTERNAL_MODEL_H
5 * Copyright (c) 2022 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/layer.h>
24 #include <dali/public-api/animation/animation.h>
25 #include <dali/public-api/object/weak-handle.h>
26 #include <dali/public-api/rendering/texture.h>
29 #include <dali-scene3d/internal/common/environment-map-load-task.h>
30 #include <dali-scene3d/internal/common/image-based-light-observer.h>
31 #include <dali-scene3d/internal/common/model-load-task.h>
32 #include <dali-scene3d/public-api/controls/model/model.h>
33 #include <dali-scene3d/public-api/controls/scene-view/scene-view.h>
44 * @brief Impl class for Model.
46 class Model : public Dali::Toolkit::Internal::Control, public ImageBasedLightObserver
49 using AnimationData = std::pair<std::string, Dali::Animation>;
52 * @brief Creates a new Model.
54 * @return A public handle to the newly allocated Model.
56 static Dali::Scene3D::Model New(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
59 * @copydoc Model::GetModelRoot()
61 const Actor GetModelRoot() const;
64 * @copydoc Model::SetChildrenSensitive()
66 void SetChildrenSensitive(bool enable);
69 * @copydoc Model::GetChildrenSensitive()
71 bool GetChildrenSensitive() const;
74 * @copydoc Model::SetChildrenFocusable()
76 void SetChildrenFocusable(bool enable);
79 * @copydoc Model::GetChildrenFocusable()
81 bool GetChildrenFocusable() const;
84 * @copydoc Model::SetImageBasedLightSource()
86 void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
89 * @copydoc Model::SetImageBasedLightTexture()
91 void SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor);
94 * @copydoc Model::SetImageBasedLightScaleFactor()
96 void SetImageBasedLightScaleFactor(float scaleFactor);
99 * @copydoc Model::GetImageBasedLightScaleFactor()
101 float GetImageBasedLightScaleFactor() const;
104 * @copydoc Model::GetAnimationCount()
106 uint32_t GetAnimationCount() const;
109 * @copydoc Model::GetAnimation()
111 Dali::Animation GetAnimation(uint32_t index) const;
114 * @copydoc Model::GetAnimation()
116 Dali::Animation GetAnimation(const std::string& name) const;
120 * @brief Constructs a new Model.
122 Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
125 * A reference counted object may only be deleted by calling Unreference()
131 * @copydoc Toolkit::Control::OnInitialize
136 * @copydoc CustomActorImpl::OnSceneConnection()
138 void OnSceneConnection(int depth) override;
141 * @copydoc CustomActorImpl::OnSceneDisconnection()
143 void OnSceneDisconnection() override;
146 * @copydoc Toolkit::Control::GetNaturalSize
148 Vector3 GetNaturalSize() override;
151 * @copydoc Toolkit::Control::GetHeightForWidth()
153 float GetHeightForWidth(float width) override;
156 * @copydoc Toolkit::Control::GetWidthForHeight()
158 float GetWidthForHeight(float height) override;
161 * @copydoc Toolkit::Control::OnRelayout()
163 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
166 * @copydoc Toolkit::Control::IsResourceReady()
168 bool IsResourceReady() const override;
171 * @brief Scales the model to fit the control or to return to original size.
176 * @brief Changes model anchor point to set the model at center or returns to the original model pivot.
178 void FitModelPosition();
181 * @brief Changes IBL information of the input node.
183 void CollectRenderableActor(Actor actor);
186 * @brief Changes IBL textures of the input node.
188 void UpdateImageBasedLightTexture();
191 * @brief Changes IBL scale factor of the input node.
193 void UpdateImageBasedLightScaleFactor();
195 public: // Overrides ImageBasedLightObserver Methods.
197 * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightTexture()
199 void NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor) override;
202 * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightScaleFactor()
204 void NotifyImageBasedLightScaleFactor(float scaleFactor) override;
208 * @brief Asynchronously model loading finished.
210 void OnModelLoadComplete();
213 * @brief Asynchronously ibl diffusel image loading finished.
215 void OnIblDiffuseLoadComplete();
218 * @brief Asynchronously ibl specular image loading finished.
220 void OnIblSpecularLoadComplete();
223 * @brief Asynchronously ibl loading finished.
225 void OnIblLoadComplete();
228 * @brief Reset Resource loading tasks.
230 void ResetResourceTasks();
233 * @brief Reset a Resource loading task.
235 void ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask);
238 * @brief Request to load a Ibl texture asynchronously
240 void RequestLoadIblTexture(EnvironmentMapLoadTaskPtr asyncLoadTask, const std::string& url);
243 * @brief Notify Resource Ready signal.
245 void NotifyResourceReady();
248 * @brief Create Model from loaded SceneDefinition.
253 * @brief Create Dali::Animation from loaded AnimationDefinitions.
255 void CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene);
258 std::string mModelUrl;
259 std::string mResourceDirectoryUrl;
260 Dali::Actor mModelRoot;
261 std::vector<AnimationData> mAnimations;
262 std::vector<WeakHandle<Actor>> mRenderableActors;
263 WeakHandle<Scene3D::SceneView> mParentSceneView;
265 // Asynchronous loading variable
266 ModelLoadTaskPtr mModelLoadTask;
267 EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
268 EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
270 std::string mDiffuseIblUrl;
271 std::string mSpecularIblUrl;
273 // TODO: This default texture can be removed after 3D Resource Cache is added.
274 Dali::Texture mDefaultSpecularTexture;
275 Dali::Texture mDefaultDiffuseTexture;
276 Dali::Texture mSceneSpecularTexture;
277 Dali::Texture mSceneDiffuseTexture;
278 Dali::Texture mSpecularTexture;
279 Dali::Texture mDiffuseTexture;
280 Vector3 mNaturalSize;
282 float mSceneIblScaleFactor;
283 float mIblScaleFactor;
284 bool mModelChildrenSensitive;
285 bool mModelChildrenFocusable;
286 bool mModelResourceReady;
287 bool mIblDiffuseResourceReady;
288 bool mIblSpecularResourceReady;
289 bool mIblDiffuseDirty;
290 bool mIblSpecularDirty;
293 } // namespace Internal
295 // Helpers for public-api forwarding methods
296 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
298 DALI_ASSERT_ALWAYS(obj);
299 Dali::RefObject& handle = obj.GetImplementation();
300 return static_cast<Dali::Scene3D::Internal::Model&>(handle);
303 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
305 DALI_ASSERT_ALWAYS(obj);
306 const Dali::RefObject& handle = obj.GetImplementation();
307 return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
310 } // namespace Scene3D
314 #endif // DALI_SCENE3D_INTERNAL_MODEL_H