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::SetImageBasedLightScaleFactor()
92 void SetImageBasedLightScaleFactor(float scaleFactor);
95 * @copydoc Model::GetImageBasedLightScaleFactor()
97 float GetImageBasedLightScaleFactor() const;
100 * @copydoc Model::GetAnimationCount()
102 uint32_t GetAnimationCount() const;
105 * @copydoc Model::GetAnimation()
107 Dali::Animation GetAnimation(uint32_t index) const;
110 * @copydoc Model::GetAnimation()
112 Dali::Animation GetAnimation(const std::string& name) const;
115 * @copydoc Model::GetCameraCount()
117 uint32_t GetCameraCount() const;
120 * @copydoc Model::GenerateCamera()
122 Dali::CameraActor GenerateCamera(uint32_t index) const;
125 * @copydoc Model::ApplyCamera()
127 bool ApplyCamera(uint32_t index, Dali::CameraActor camera) const;
131 * @brief Constructs a new Model.
132 * @param[in] modelUrl model file path.(e.g., glTF, and DLI).
133 * @param[in] resourceDirectoryUrl resource file path that includes binary, image etc.
135 Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
138 * A reference counted object may only be deleted by calling Unreference()
144 * @copydoc Toolkit::Control::OnInitialize
149 * @copydoc CustomActorImpl::OnSceneConnection()
151 void OnSceneConnection(int depth) override;
154 * @copydoc CustomActorImpl::OnSceneDisconnection()
156 void OnSceneDisconnection() override;
159 * @copydoc Toolkit::Control::GetNaturalSize
161 Vector3 GetNaturalSize() override;
164 * @copydoc Toolkit::Control::GetHeightForWidth()
166 float GetHeightForWidth(float width) override;
169 * @copydoc Toolkit::Control::GetWidthForHeight()
171 float GetWidthForHeight(float height) override;
174 * @copydoc Toolkit::Control::OnRelayout()
176 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
179 * @copydoc Toolkit::Control::IsResourceReady()
181 bool IsResourceReady() const override;
185 * @brief Scales the model to fit the control or to return to original size.
190 * @brief Changes model anchor point to set the model at center or returns to the original model pivot.
192 void FitModelPosition();
195 * @brief Changes IBL information of the input node.
197 void CollectRenderableActor(Actor actor);
200 * @brief Changes IBL textures of the input node.
202 void UpdateImageBasedLightTexture();
205 * @brief Changes IBL scale factor of the input node.
207 void UpdateImageBasedLightScaleFactor();
210 * @brief Apply self transform into inputed camera.
211 * Inputed camera must be configured by CameraParameter. Mean, inputed camera coordinate depend on Model.
212 * After this API finished, CameraActor coordinate system converted as DALi coordinate system.
214 * @param[in,out] camera CameraActor who need to apply model itself's transform
216 void ApplyCameraTransform(Dali::CameraActor camera) const;
218 public: // Overrides ImageBasedLightObserver Methods.
220 * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightTexture()
222 void NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor, uint32_t specularMipmapLevels) override;
225 * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightScaleFactor()
227 void NotifyImageBasedLightScaleFactor(float scaleFactor) override;
231 * @brief Asynchronously model loading finished.
233 void OnModelLoadComplete();
236 * @brief Asynchronously ibl diffusel image loading finished.
238 void OnIblDiffuseLoadComplete();
241 * @brief Asynchronously ibl specular image loading finished.
243 void OnIblSpecularLoadComplete();
246 * @brief Asynchronously ibl loading finished.
248 void OnIblLoadComplete();
251 * @brief Reset Resource loading tasks.
253 void ResetResourceTasks();
256 * @brief Reset a Resource loading task.
258 void ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask);
261 * @brief Notify Resource Ready signal.
263 void NotifyResourceReady();
266 * @brief Create Model from loaded SceneDefinition.
271 * @brief Create Dali::Animation from loaded AnimationDefinitions.
273 void CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene);
276 * @brief Reset CameraData from loaded CameraParameters.
278 void ResetCameraParameters();
281 std::string mModelUrl;
282 std::string mResourceDirectoryUrl;
283 Dali::Actor mModelRoot;
284 std::vector<AnimationData> mAnimations;
285 std::vector<CameraData> mCameraParameters;
286 std::vector<WeakHandle<Actor>> mRenderableActors;
287 WeakHandle<Scene3D::SceneView> mParentSceneView;
289 // Asynchronous loading variable
290 ModelLoadTaskPtr mModelLoadTask;
291 EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
292 EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
294 std::string mDiffuseIblUrl;
295 std::string mSpecularIblUrl;
297 // TODO: This default texture can be removed after 3D Resource Cache is added.
298 Dali::Texture mDefaultSpecularTexture;
299 Dali::Texture mDefaultDiffuseTexture;
300 Dali::Texture mSceneSpecularTexture;
301 Dali::Texture mSceneDiffuseTexture;
302 Dali::Texture mSpecularTexture;
303 Dali::Texture mDiffuseTexture;
304 Vector3 mNaturalSize;
306 float mSceneIblScaleFactor;
307 float mIblScaleFactor;
308 uint32_t mSceneSpecularMipmapLevels;
309 uint32_t mSpecularMipmapLevels;
310 bool mModelChildrenSensitive;
311 bool mModelChildrenFocusable;
312 bool mModelResourceReady;
313 bool mIblDiffuseResourceReady;
314 bool mIblSpecularResourceReady;
315 bool mIblDiffuseDirty;
316 bool mIblSpecularDirty;
319 } // namespace Internal
321 // Helpers for public-api forwarding methods
322 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
324 DALI_ASSERT_ALWAYS(obj);
325 Dali::RefObject& handle = obj.GetImplementation();
326 return static_cast<Dali::Scene3D::Internal::Model&>(handle);
329 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
331 DALI_ASSERT_ALWAYS(obj);
332 const Dali::RefObject& handle = obj.GetImplementation();
333 return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
336 } // namespace Scene3D
340 #endif // DALI_SCENE3D_INTERNAL_MODEL_H