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>
36 #include <dali-scene3d/public-api/model-components/model-node.h>
47 * @brief Impl class for Model.
49 class Model : public Dali::Toolkit::Internal::Control, public ImageBasedLightObserver
52 using AnimationData = std::pair<std::string, Dali::Animation>;
53 using CameraData = Loader::CameraParameters;
56 * @copydoc Model::New()
58 static Dali::Scene3D::Model New(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
61 * @copydoc Model::GetModelRoot()
63 const Scene3D::ModelNode GetModelRoot() const;
66 * @copydoc Model::AddModelNode()
68 void AddModelNode(Scene3D::ModelNode modelNode);
71 * @copydoc Model::RemoveModelNode()
73 void RemoveModelNode(Scene3D::ModelNode modelNode);
76 * @copydoc Model::SetChildrenSensitive()
78 void SetChildrenSensitive(bool enable);
81 * @copydoc Model::GetChildrenSensitive()
83 bool GetChildrenSensitive() const;
86 * @copydoc Model::SetChildrenFocusable()
88 void SetChildrenFocusable(bool enable);
91 * @copydoc Model::GetChildrenFocusable()
93 bool GetChildrenFocusable() const;
96 * @copydoc Model::SetImageBasedLightSource()
98 void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
101 * @copydoc Model::SetImageBasedLightScaleFactor()
103 void SetImageBasedLightScaleFactor(float scaleFactor);
106 * @copydoc Model::GetImageBasedLightScaleFactor()
108 float GetImageBasedLightScaleFactor() const;
111 * @copydoc Model::GetAnimationCount()
113 uint32_t GetAnimationCount() const;
116 * @copydoc Model::GetAnimation()
118 Dali::Animation GetAnimation(uint32_t index) const;
121 * @copydoc Model::GetAnimation()
123 Dali::Animation GetAnimation(const std::string& name) const;
126 * @copydoc Model::GetCameraCount()
128 uint32_t GetCameraCount() const;
131 * @copydoc Model::GenerateCamera()
133 Dali::CameraActor GenerateCamera(uint32_t index) const;
136 * @copydoc Model::ApplyCamera()
138 bool ApplyCamera(uint32_t index, Dali::CameraActor camera) const;
141 * @copydoc Model::FindChildModelNodeByName()
143 Scene3D::ModelNode FindChildModelNodeByName(std::string_view nodeName);
147 * @brief Constructs a new Model.
148 * @param[in] modelUrl model file path.(e.g., glTF, and DLI).
149 * @param[in] resourceDirectoryUrl resource file path that includes binary, image etc.
151 Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
154 * A reference counted object may only be deleted by calling Unreference()
160 * @copydoc Toolkit::Control::OnInitialize
165 * @copydoc CustomActorImpl::OnSceneConnection()
167 void OnSceneConnection(int depth) override;
170 * @copydoc CustomActorImpl::OnSceneDisconnection()
172 void OnSceneDisconnection() override;
175 * @copydoc Toolkit::Control::GetNaturalSize
177 Vector3 GetNaturalSize() override;
180 * @copydoc Toolkit::Control::GetHeightForWidth()
182 float GetHeightForWidth(float width) override;
185 * @copydoc Toolkit::Control::GetWidthForHeight()
187 float GetWidthForHeight(float height) override;
190 * @copydoc Toolkit::Control::OnRelayout()
192 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
195 * @copydoc Toolkit::Control::IsResourceReady()
197 bool IsResourceReady() const override;
201 * @brief Create Model Root of this Model class.
203 void CreateModelRoot();
206 * @brief Scales the model to fit the control or to return to original size.
211 * @brief Changes model anchor point to set the model at center or returns to the original model pivot.
213 void FitModelPosition();
216 * @brief Changes IBL information of the input node.
218 void UpdateImageBasedLightTextureRecursively(Scene3D::ModelNode node, Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels);
221 * @brief Changes IBL factor of the input node.
223 void UpdateImageBasedLightScaleFactorRecursively(Scene3D::ModelNode node, float iblScaleFactor);
226 * @brief Changes IBL textures of the input node.
228 void UpdateImageBasedLightTexture();
231 * @brief Changes IBL scale factor of the input node.
233 void UpdateImageBasedLightScaleFactor();
236 * @brief Apply self transform into inputed camera.
237 * Inputed camera must be configured by CameraParameter. Mean, inputed camera coordinate depend on Model.
238 * After this API finished, CameraActor coordinate system converted as DALi coordinate system.
240 * @param[in,out] camera CameraActor who need to apply model itself's transform
242 void ApplyCameraTransform(Dali::CameraActor camera) const;
244 public: // Overrides ImageBasedLightObserver Methods.
246 * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightTexture()
248 void NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor, uint32_t specularMipmapLevels) override;
251 * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightScaleFactor()
253 void NotifyImageBasedLightScaleFactor(float scaleFactor) override;
257 * @brief Asynchronously model loading finished.
259 void OnModelLoadComplete();
262 * @brief Asynchronously ibl diffusel image loading finished.
264 void OnIblDiffuseLoadComplete();
267 * @brief Asynchronously ibl specular image loading finished.
269 void OnIblSpecularLoadComplete();
272 * @brief Asynchronously ibl loading finished.
274 void OnIblLoadComplete();
277 * @brief Reset Resource loading tasks.
279 void ResetResourceTasks();
282 * @brief Reset a Resource loading task.
284 void ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask);
287 * @brief Notify Resource Ready signal.
289 void NotifyResourceReady();
292 * @brief Create Model from loaded SceneDefinition.
297 * @brief Create Dali::Animation from loaded AnimationDefinitions.
299 void CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene);
302 * @brief Reset CameraData from loaded CameraParameters.
304 void ResetCameraParameters();
307 std::string mModelUrl;
308 std::string mResourceDirectoryUrl;
309 Scene3D::ModelNode mModelRoot;
310 std::vector<AnimationData> mAnimations;
311 std::vector<CameraData> mCameraParameters;
312 WeakHandle<Scene3D::SceneView> mParentSceneView;
314 // Asynchronous loading variable
315 ModelLoadTaskPtr mModelLoadTask;
316 EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
317 EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
319 std::string mDiffuseIblUrl;
320 std::string mSpecularIblUrl;
322 Dali::Texture mDefaultSpecularTexture;
323 Dali::Texture mDefaultDiffuseTexture;
324 Dali::Texture mSceneSpecularTexture;
325 Dali::Texture mSceneDiffuseTexture;
326 Dali::Texture mSpecularTexture;
327 Dali::Texture mDiffuseTexture;
328 Vector3 mNaturalSize;
330 float mSceneIblScaleFactor;
331 float mIblScaleFactor;
332 uint32_t mSceneSpecularMipmapLevels;
333 uint32_t mSpecularMipmapLevels;
334 bool mModelChildrenSensitive;
335 bool mModelChildrenFocusable;
336 bool mModelResourceReady;
337 bool mIblDiffuseResourceReady;
338 bool mIblSpecularResourceReady;
339 bool mIblDiffuseDirty;
340 bool mIblSpecularDirty;
343 } // namespace Internal
345 // Helpers for public-api forwarding methods
346 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
348 DALI_ASSERT_ALWAYS(obj);
349 Dali::RefObject& handle = obj.GetImplementation();
350 return static_cast<Dali::Scene3D::Internal::Model&>(handle);
353 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
355 DALI_ASSERT_ALWAYS(obj);
356 const Dali::RefObject& handle = obj.GetImplementation();
357 return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
360 } // namespace Scene3D
364 #endif // DALI_SCENE3D_INTERNAL_MODEL_H