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/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/light/light.h>
37 #include <dali-scene3d/public-api/loader/load-result.h>
38 #include <dali-scene3d/public-api/model-components/model-node.h>
49 * @brief Impl class for Model.
51 class Model : public Dali::Toolkit::Internal::Control, public LightObserver
54 using AnimationData = std::pair<std::string, Dali::Animation>;
55 using CameraData = Loader::CameraParameters;
58 * @copydoc Model::New()
60 static Dali::Scene3D::Model New(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
63 * @copydoc Model::GetModelRoot()
65 const Scene3D::ModelNode GetModelRoot() const;
68 * @copydoc Model::AddModelNode()
70 void AddModelNode(Scene3D::ModelNode modelNode);
73 * @copydoc Model::RemoveModelNode()
75 void RemoveModelNode(Scene3D::ModelNode modelNode);
78 * @copydoc Model::SetChildrenSensitive()
80 void SetChildrenSensitive(bool enable);
83 * @copydoc Model::GetChildrenSensitive()
85 bool GetChildrenSensitive() const;
88 * @copydoc Model::SetChildrenFocusable()
90 void SetChildrenFocusable(bool enable);
93 * @copydoc Model::GetChildrenFocusable()
95 bool GetChildrenFocusable() const;
98 * @copydoc Model::SetImageBasedLightSource()
100 void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
103 * @copydoc Model::SetImageBasedLightScaleFactor()
105 void SetImageBasedLightScaleFactor(float scaleFactor);
108 * @copydoc Model::GetImageBasedLightScaleFactor()
110 float GetImageBasedLightScaleFactor() const;
113 * @copydoc Model::GetAnimationCount()
115 uint32_t GetAnimationCount() const;
118 * @copydoc Model::GetAnimation()
120 Dali::Animation GetAnimation(uint32_t index) const;
123 * @copydoc Model::GetAnimation()
125 Dali::Animation GetAnimation(const std::string& name) const;
128 * @copydoc Model::GetCameraCount()
130 uint32_t GetCameraCount() const;
133 * @copydoc Model::GenerateCamera()
135 Dali::CameraActor GenerateCamera(uint32_t index) const;
138 * @copydoc Model::ApplyCamera()
140 bool ApplyCamera(uint32_t index, Dali::CameraActor camera) const;
143 * @copydoc Model::FindChildModelNodeByName()
145 Scene3D::ModelNode FindChildModelNodeByName(std::string_view nodeName);
149 * @brief Constructs a new Model.
150 * @param[in] modelUrl model file path.(e.g., glTF, and DLI).
151 * @param[in] resourceDirectoryUrl resource file path that includes binary, image etc.
153 Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
156 * A reference counted object may only be deleted by calling Unreference()
162 * @copydoc Toolkit::Control::OnInitialize
167 * @copydoc CustomActorImpl::OnSceneConnection()
169 void OnSceneConnection(int depth) override;
172 * @copydoc CustomActorImpl::OnSceneDisconnection()
174 void OnSceneDisconnection() override;
177 * @copydoc CustomActorImpl::OnSizeSet( const Vector3& size )
179 void OnSizeSet(const Vector3& size) override;
182 * @copydoc Toolkit::Control::GetNaturalSize
184 Vector3 GetNaturalSize() override;
187 * @copydoc Toolkit::Control::GetHeightForWidth()
189 float GetHeightForWidth(float width) override;
192 * @copydoc Toolkit::Control::GetWidthForHeight()
194 float GetWidthForHeight(float height) override;
197 * @copydoc Toolkit::Control::OnRelayout()
199 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
202 * @copydoc Toolkit::Control::IsResourceReady()
204 bool IsResourceReady() const override;
208 * @brief Create Model Root of this Model class.
210 void CreateModelRoot();
213 * @brief Scales the model to fit the control or to return to original size.
215 void ScaleModel(bool useCurrentSize);
218 * @brief Changes model anchor point to set the model at center or returns to the original model pivot.
220 void FitModelPosition();
223 * @brief Changes IBL information of the input node.
225 void UpdateImageBasedLightTextureRecursively(Scene3D::ModelNode node, Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels);
228 * @brief Changes IBL factor of the input node.
230 void UpdateImageBasedLightScaleFactorRecursively(Scene3D::ModelNode node, float iblScaleFactor);
233 * @brief Changes IBL textures of the input node.
235 void UpdateImageBasedLightTexture();
238 * @brief Changes IBL scale factor of the input node.
240 void UpdateImageBasedLightScaleFactor();
243 * @brief Apply self transform into inputed camera.
244 * Inputed camera must be configured by CameraParameter. Mean, inputed camera coordinate depend on Model.
245 * After this API finished, CameraActor coordinate system converted as DALi coordinate system.
247 * @param[in,out] camera CameraActor who need to apply model itself's transform
249 void ApplyCameraTransform(Dali::CameraActor camera) const;
251 public: // Overrides LightObserver Methods.
253 * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyImageBasedLightTexture()
255 void NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor, uint32_t specularMipmapLevels) override;
258 * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyImageBasedLightScaleFactor()
260 void NotifyImageBasedLightScaleFactor(float scaleFactor) override;
263 * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyLightAdded()
265 void NotifyLightAdded(uint32_t lightIndex, Scene3D::Light light) override;
268 * @copydoc Dali::Scene3D::Internal::LightObserver::NotifyLightRemoved()
270 void NotifyLightRemoved(uint32_t lightIndex) override;
274 * @brief Asynchronously model loading finished.
276 void OnModelLoadComplete();
279 * @brief Asynchronously ibl diffusel image loading finished.
281 void OnIblDiffuseLoadComplete();
284 * @brief Asynchronously ibl specular image loading finished.
286 void OnIblSpecularLoadComplete();
289 * @brief Asynchronously ibl loading finished.
291 void OnIblLoadComplete();
294 * @brief Update model root scale when Model size property is updated.
296 void OnSizeNotification(Dali::PropertyNotification& source);
299 * @brief Reset Resource loading tasks.
301 void ResetResourceTasks();
304 * @brief Reset a Resource loading task.
306 void ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask);
309 * @brief Notify Resource Ready signal.
311 void NotifyResourceReady();
314 * @brief Create Model from loaded SceneDefinition.
319 * @brief Create Dali::Animation from loaded AnimationDefinitions.
321 void CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene);
324 * @brief Reset CameraData from loaded CameraParameters.
326 void ResetCameraParameters();
329 std::string mModelUrl;
330 std::string mResourceDirectoryUrl;
331 Scene3D::ModelNode mModelRoot;
332 std::vector<AnimationData> mAnimations;
333 std::vector<CameraData> mCameraParameters;
334 WeakHandle<Scene3D::SceneView> mParentSceneView;
335 Dali::PropertyNotification mSizeNotification;
338 std::vector<Scene3D::Light> mLights;
340 // Asynchronous loading variable
341 ModelLoadTaskPtr mModelLoadTask;
342 EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
343 EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
345 std::string mDiffuseIblUrl;
346 std::string mSpecularIblUrl;
348 Dali::Texture mDefaultSpecularTexture;
349 Dali::Texture mDefaultDiffuseTexture;
350 Dali::Texture mSceneSpecularTexture;
351 Dali::Texture mSceneDiffuseTexture;
352 Dali::Texture mSpecularTexture;
353 Dali::Texture mDiffuseTexture;
354 Vector3 mNaturalSize;
356 float mSceneIblScaleFactor;
357 float mIblScaleFactor;
358 uint32_t mSceneSpecularMipmapLevels;
359 uint32_t mSpecularMipmapLevels;
360 bool mModelChildrenSensitive;
361 bool mModelChildrenFocusable;
362 bool mModelResourceReady;
363 bool mIblDiffuseResourceReady;
364 bool mIblSpecularResourceReady;
365 bool mIblDiffuseDirty;
366 bool mIblSpecularDirty;
369 } // namespace Internal
371 // Helpers for public-api forwarding methods
372 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
374 DALI_ASSERT_ALWAYS(obj);
375 Dali::RefObject& handle = obj.GetImplementation();
376 return static_cast<Dali::Scene3D::Internal::Model&>(handle);
379 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
381 DALI_ASSERT_ALWAYS(obj);
382 const Dali::RefObject& handle = obj.GetImplementation();
383 return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
386 } // namespace Scene3D
390 #endif // DALI_SCENE3D_INTERNAL_MODEL_H