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>
35 #include <dali-scene3d/public-api/loader/load-result.h>
46 * @brief Impl class for Model.
48 class Model : public Dali::Toolkit::Internal::Control, public ImageBasedLightObserver
51 using AnimationData = std::pair<std::string, Dali::Animation>;
52 using CameraData = Loader::CameraParameters;
55 * @copydoc Model::New()
57 static Dali::Scene3D::Model New(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
60 * @copydoc Model::GetModelRoot()
62 const Actor GetModelRoot() const;
65 * @copydoc Model::SetChildrenSensitive()
67 void SetChildrenSensitive(bool enable);
70 * @copydoc Model::GetChildrenSensitive()
72 bool GetChildrenSensitive() const;
75 * @copydoc Model::SetChildrenFocusable()
77 void SetChildrenFocusable(bool enable);
80 * @copydoc Model::GetChildrenFocusable()
82 bool GetChildrenFocusable() const;
85 * @copydoc Model::SetImageBasedLightSource()
87 void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
90 * @copydoc Model::SetImageBasedLightTexture()
92 void SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor);
95 * @copydoc Model::SetImageBasedLightScaleFactor()
97 void SetImageBasedLightScaleFactor(float scaleFactor);
100 * @copydoc Model::GetImageBasedLightScaleFactor()
102 float GetImageBasedLightScaleFactor() const;
105 * @copydoc Model::GetAnimationCount()
107 uint32_t GetAnimationCount() const;
110 * @copydoc Model::GetAnimation()
112 Dali::Animation GetAnimation(uint32_t index) const;
115 * @copydoc Model::GetAnimation()
117 Dali::Animation GetAnimation(const std::string& name) const;
120 * @copydoc Model::GetCameraCount()
122 uint32_t GetCameraCount() const;
125 * @copydoc Model::GenerateCamera()
127 Dali::CameraActor GenerateCamera(uint32_t index) const;
130 * @copydoc Model::ApplyCamera()
132 bool ApplyCamera(uint32_t index, Dali::CameraActor camera) const;
136 * @brief Constructs a new Model.
137 * @param[in] modelUrl model file path.(e.g., glTF, and DLI).
138 * @param[in] resourceDirectoryUrl resource file path that includes binary, image etc.
140 Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
143 * A reference counted object may only be deleted by calling Unreference()
149 * @copydoc Toolkit::Control::OnInitialize
154 * @copydoc CustomActorImpl::OnSceneConnection()
156 void OnSceneConnection(int depth) override;
159 * @copydoc CustomActorImpl::OnSceneDisconnection()
161 void OnSceneDisconnection() override;
164 * @copydoc Toolkit::Control::GetNaturalSize
166 Vector3 GetNaturalSize() override;
169 * @copydoc Toolkit::Control::GetHeightForWidth()
171 float GetHeightForWidth(float width) override;
174 * @copydoc Toolkit::Control::GetWidthForHeight()
176 float GetWidthForHeight(float height) override;
179 * @copydoc Toolkit::Control::OnRelayout()
181 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
184 * @copydoc Toolkit::Control::IsResourceReady()
186 bool IsResourceReady() const override;
190 * @brief Scales the model to fit the control or to return to original size.
195 * @brief Changes model anchor point to set the model at center or returns to the original model pivot.
197 void FitModelPosition();
200 * @brief Changes IBL information of the input node.
202 void CollectRenderableActor(Actor actor);
205 * @brief Changes IBL textures of the input node.
207 void UpdateImageBasedLightTexture();
210 * @brief Changes IBL scale factor of the input node.
212 void UpdateImageBasedLightScaleFactor();
215 * @brief Apply self transform into inputed camera.
216 * Inputed camera must be configured by CameraParameter. Mean, inputed camera coordinate depend on Model.
217 * After this API finished, CameraActor coordinate system converted as DALi coordinate system.
219 * @param[in,out] camera CameraActor who need to apply model itself's transform
221 void ApplyCameraTransform(Dali::CameraActor camera) const;
223 public: // Overrides ImageBasedLightObserver Methods.
225 * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightTexture()
227 void NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor) override;
230 * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightScaleFactor()
232 void NotifyImageBasedLightScaleFactor(float scaleFactor) override;
236 * @brief Asynchronously model loading finished.
238 void OnModelLoadComplete();
241 * @brief Asynchronously ibl diffusel image loading finished.
243 void OnIblDiffuseLoadComplete();
246 * @brief Asynchronously ibl specular image loading finished.
248 void OnIblSpecularLoadComplete();
251 * @brief Asynchronously ibl loading finished.
253 void OnIblLoadComplete();
256 * @brief Reset Resource loading tasks.
258 void ResetResourceTasks();
261 * @brief Reset a Resource loading task.
263 void ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask);
266 * @brief Notify Resource Ready signal.
268 void NotifyResourceReady();
271 * @brief Create Model from loaded SceneDefinition.
276 * @brief Create Dali::Animation from loaded AnimationDefinitions.
278 void CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene);
281 * @brief Reset CameraData from loaded CameraParameters.
283 void ResetCameraParameters();
286 std::string mModelUrl;
287 std::string mResourceDirectoryUrl;
288 Dali::Actor mModelRoot;
289 std::vector<AnimationData> mAnimations;
290 std::vector<CameraData> mCameraParameters;
291 std::vector<WeakHandle<Actor>> mRenderableActors;
292 WeakHandle<Scene3D::SceneView> mParentSceneView;
294 // Asynchronous loading variable
295 ModelLoadTaskPtr mModelLoadTask;
296 EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
297 EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
299 std::string mDiffuseIblUrl;
300 std::string mSpecularIblUrl;
302 // TODO: This default texture can be removed after 3D Resource Cache is added.
303 Dali::Texture mDefaultSpecularTexture;
304 Dali::Texture mDefaultDiffuseTexture;
305 Dali::Texture mSceneSpecularTexture;
306 Dali::Texture mSceneDiffuseTexture;
307 Dali::Texture mSpecularTexture;
308 Dali::Texture mDiffuseTexture;
309 Vector3 mNaturalSize;
311 float mSceneIblScaleFactor;
312 float mIblScaleFactor;
313 bool mModelChildrenSensitive;
314 bool mModelChildrenFocusable;
315 bool mModelResourceReady;
316 bool mIblDiffuseResourceReady;
317 bool mIblSpecularResourceReady;
318 bool mIblDiffuseDirty;
319 bool mIblSpecularDirty;
322 } // namespace Internal
324 // Helpers for public-api forwarding methods
325 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
327 DALI_ASSERT_ALWAYS(obj);
328 Dali::RefObject& handle = obj.GetImplementation();
329 return static_cast<Dali::Scene3D::Internal::Model&>(handle);
332 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
334 DALI_ASSERT_ALWAYS(obj);
335 const Dali::RefObject& handle = obj.GetImplementation();
336 return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
339 } // namespace Scene3D
343 #endif // DALI_SCENE3D_INTERNAL_MODEL_H