1 #ifndef DALI_SCENE3D_INTERNAL_SCENE_VIEW_H
2 #define DALI_SCENE3D_INTERNAL_SCENE_VIEW_H
5 * Copyright (c) 2024 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/internal/visuals/image/image-visual.h>
23 #include <dali-toolkit/public-api/controls/control-impl.h>
24 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
25 #include <dali/integration-api/adaptor-framework/scene-holder.h>
26 #include <dali/integration-api/ordered-set.h>
27 #include <dali/public-api/actors/camera-actor.h>
28 #include <dali/public-api/actors/layer.h>
29 #include <dali/public-api/adaptor-framework/timer.h>
30 #include <dali/public-api/adaptor-framework/window.h>
31 #include <dali/public-api/animation/animation.h>
32 #include <dali/public-api/capture/capture.h>
33 #include <dali/public-api/object/weak-handle.h>
34 #include <dali/public-api/render-tasks/render-task.h>
35 #include <dali/public-api/rendering/frame-buffer.h>
36 #include <dali/public-api/rendering/texture.h>
39 #include <dali-scene3d/internal/common/environment-map-load-task.h>
40 #include <dali-scene3d/internal/common/light-observer.h>
41 #include <dali-scene3d/public-api/controls/scene-view/scene-view.h>
42 #include <dali-scene3d/public-api/loader/shader-manager.h>
53 * @brief Impl class for SceneView.
55 class SceneView : public Dali::Toolkit::Internal::Control
59 * @brief Creates a new SceneView.
61 * @return A public handle to the newly allocated SceneView.
63 static Dali::Scene3D::SceneView New();
66 * @copydoc SceneView::AddCamera()
68 void AddCamera(Dali::CameraActor camera);
71 * @copydoc SceneView::RemoveCamera()
73 void RemoveCamera(CameraActor camera);
76 * @copydoc SceneView::GetCameraCount()
78 uint32_t GetCameraCount() const;
81 * @copydoc SceneView::GetSelectedCamera()
83 CameraActor GetSelectedCamera() const;
86 * @copydoc SceneView::GetCamera()
88 CameraActor GetCamera(uint32_t index) const;
91 * @copydoc SceneView::GetCamera()
93 CameraActor GetCamera(const std::string& name) const;
96 * @copydoc SceneView::SelectCamera()
98 void SelectCamera(uint32_t index);
101 * @copydoc SceneView::SelectCamera()
103 void SelectCamera(const std::string& name);
106 * @brief Register an item.
108 * Some works(e.g, lighting) of SceneView should be propagated to the child 3D items.
109 * SceneView can avoid unnecessary tree traversal to find 3D items by storing only 3D items as a list.
111 * @param[in] item scene observer to be registered.
113 void RegisterSceneItem(Scene3D::Internal::LightObserver* item);
116 * @brief Unregister an item
118 * @param[in] item scene observer to be unregistered.
120 void UnregisterSceneItem(Scene3D::Internal::LightObserver* item);
123 * @copydoc SceneView::SetImageBasedLightSource()
125 void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
128 * @copydoc SceneView::SetImageBasedLightScaleFactor()
130 void SetImageBasedLightScaleFactor(float scaleFactor);
133 * @copydoc SceneView::GetImageBasedLightScaleFactor()
135 float GetImageBasedLightScaleFactor() const;
138 * @brief Adds a Light object to this SceneView.
139 * Multiple light object can be added on this SceneView
140 * But the number of lights those actually turned on has limitation.
141 * Scene3D::Light::GetMaximumEnabledLightCount() method can be used to know the maximum namber.
143 * @param[in] light Light object to be added.
145 void AddLight(Scene3D::Light light);
148 * @brief Removes a Light object to this SceneView.
149 * If the light is currently turned on, and SceneView has lights more than maximum number of enabled light.
150 * One of light in queue is turned on after this light is removed.
152 * @param[in] light Light object to be removed.
154 void RemoveLight(Scene3D::Light light);
157 * @brief Set a shadow to this scene by input light.
158 * Currently, SceneView supports only one shadow.
160 * @param[in] light Light object to make shadow.
161 * @note The shadow will be drawn if the input light is turn on in current scene.
163 void SetShadow(Scene3D::Light light);
166 * @brief Removes Shadow from this SceneView.
168 * @param[in] light Light object to be removed.
170 void RemoveShadow(Scene3D::Light light);
173 * @copydoc SceneView::GetActivatedLightCount()
175 uint32_t GetActivatedLightCount() const;
178 * @copydoc SceneView::UseFramebuffer()
180 void UseFramebuffer(bool useFramebuffer);
183 * @copydoc SceneView::IsUsingFramebuffer()
185 bool IsUsingFramebuffer() const;
188 * @copydoc SceneView::SetResolution()
190 void SetResolution(uint32_t width, uint32_t height);
193 * @copydoc SceneView::GetResolutionWidth()
195 uint32_t GetResolutionWidth();
198 * @copydoc SceneView::GetResolutionHeight()
200 uint32_t GetResolutionHeight();
203 * @copydoc SceneView::ResetResolution()
205 void ResetResolution();
208 * @copydoc SceneView::SetFramebufferMultiSamplingLevel()
210 void SetFramebufferMultiSamplingLevel(uint8_t multiSamplingLevel);
213 * @copydoc SceneView::GetFramebufferMultiSamplingLevel()
215 uint8_t GetFramebufferMultiSamplingLevel() const;
218 * @copydoc SceneView::SetSkybox()
220 void SetSkybox(const std::string& skyboxUrl);
223 * @copydoc SceneView::SetSkyboxEnvironmentMapType()
225 void SetSkyboxEnvironmentMapType(Scene3D::EnvironmentMapType skyboxEnvironmentMapType);
228 * @copydoc SceneView::SetSkyboxIntensity()
230 void SetSkyboxIntensity(float intensity);
233 * @copydoc SceneView::GetSkyboxIntensity()
235 float GetSkyboxIntensity() const;
238 * @copydoc SceneView::SetSkyboxOrientation()
240 void SetSkyboxOrientation(const Quaternion& orientation);
243 * @copydoc SceneView::GetSkyboxOrientation()
245 Quaternion GetSkyboxOrientation() const;
248 * @copydoc SceneView::Capture()
250 int32_t Capture(Dali::CameraActor camera, const Vector2& size);
253 * @copydoc SceneView::FinishedSignal
255 Dali::Scene3D::SceneView::CaptureFinishedSignalType& CaptureFinishedSignal();
258 * @brief Retrieves ShaderManager of this SceneView.
259 * @return ShaderManager of this SceneView.
261 Dali::Scene3D::Loader::ShaderManagerPtr GetShaderManager() const;
264 * @brief Updates shader uniforms about shadow.
265 * @param[in] light Light that makes shadow.
267 void UpdateShadowUniform(Scene3D::Light light);
270 * @brief Sets alpha mask url
271 * @param[in] alphaMaskUrl Url for alpha mask.
273 void SetAlphaMaskUrl(std::string& alphaMaskUrl);
276 * @brief Retrieves alpha mask url
277 * @return Alpha mask url.
279 std::string GetAlphaMaskUrl();
282 * @brief Sets mask content scale factor
283 * @param[in] maskContentScaleFactor Scale factor for mask content.
285 void SetMaskContentScaleFactor(float maskContentScaleFactor);
288 * @brief Retrieves mask content scale factor
289 * @return Scale factor for mask content.
291 float GetMaskContentScaleFactor();
294 * @brief Sets whether the rendered result will be crop to mask or not.
295 * @param[in] enableCropToMask True for crop rendered result to mask.
297 void EnableCropToMask(bool enableCropToMask);
300 * @brief Retrieves whether the crop to mask is enabled or not.
301 * @return True when rendered result is cropped to mask.
303 bool IsEnabledCropToMask();
306 * @brief Gets current RenderTask
308 Dali::RenderTask GetRenderTask();
313 * Called when a property of an object of this type is set.
314 * @param[in] object The object whose property is set.
315 * @param[in] index The property index.
316 * @param[in] value The new property value.
318 static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
321 * Called to retrieve a property of an object of this type.
322 * @param[in] object The object whose property is to be retrieved.
323 * @param[in] index The property index.
324 * @return The current value of the property.
326 static Property::Value GetProperty(BaseObject* object, Property::Index index);
330 * @brief Constructs a new SceneView.
335 * A reference counted object may only be deleted by calling Unreference()
337 virtual ~SceneView();
341 * @copydoc CustomActorImpl::OnSceneConnection()
343 void OnSceneConnection(int depth) override;
346 * @copydoc CustomActorImpl::OnSceneDisconnection()
348 void OnSceneDisconnection() override;
351 * @copydoc Toolkit::Control::OnInitialize()
353 void OnInitialize() override;
356 * @copydoc Toolkit::Control::OnChildAdd()
358 void OnChildAdd(Actor& child) override;
361 * @copydoc Toolkit::Control::OnChildRemove()
363 void OnChildRemove(Actor& child) override;
366 * @copydoc Toolkit::Control::GetHeightForWidth()
368 float GetHeightForWidth(float width) override;
371 * @copydoc Toolkit::Control::GetWidthForHeight()
373 float GetWidthForHeight(float height) override;
376 * @copydoc Toolkit::Control::OnRelayout()
378 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
381 * @copydoc Toolkit::Control::IsResourceReady()
383 bool IsResourceReady() const override;
386 * @brief Changes main camera as a input camera
388 * @param camera CameraActor that will be a main camera of the SceneView
390 void UpdateCamera(CameraActor camera);
393 * @brief Updates RenderTask to use selected camera and to make framebuffer
395 void UpdateRenderTask();
398 * @brief Callback that will be called when window is resized.
400 void OnWindowResized(Window window, Window::WindowSize size);
403 * @brief Updates camera's projection orientation according to the screen orientation.
408 * @brief UpdateSkybox with skybox url and skybox environment map type.
410 * @param[in] skyboxUrl image url for skybox.
411 * @param[in] skyboxEnvironmentMapType The environment type of skybox.
413 void UpdateSkybox(const std::string& skyboxUrl, Scene3D::EnvironmentMapType skyboxEnvironmentMapType);
416 * @brief Asynchronously skybox loading finished.
418 void OnSkyboxLoadComplete();
421 * @brief Asynchronously ibl diffusel image loading finished.
423 void OnIblDiffuseLoadComplete();
426 * @brief Asynchronously ibl specular image loading finished.
428 void OnIblSpecularLoadComplete();
431 * @brief Asynchronously ibl loading finished.
433 void OnIblLoadComplete();
436 * @brief Notify the changes of Ibl textures to the child items.
438 void NotifyImageBasedLightTextureChange();
441 * @brief Update shadowMap framebuffer when the size should be changed.
442 * @param[in] shadowMapSize The size of shadowMap texture. The texture's width and hight is equal.
444 void UpdateShadowMapBuffer(uint32_t shadowMapSize);
447 * @brief CaptureFinished Callback that is called the capture rendering is finished.
448 * @param[in] task RenderTask that draws requested capture scene.
450 void OnCaptureFinished(Dali::RenderTask& task);
453 * @brief Time out Callback to handle the case each capture request is not finished for long time.
454 * @return True if the timer needs to go on.
460 * Data to store Capture related objects.
465 int32_t mCaptureId; // Unique Key to distinguish requested Captures.
466 Dali::Toolkit::ImageUrl mCaptureUrl; // URL for first captured buffer, but it is Y-inverted.
467 Dali::Toolkit::ImageView mCaptureImageView; // ImageView to draw first capture buffer to be transfered as input for invert.
468 Dali::RenderTask mCaptureTask; // RenderTask that is used to capture first buffer.
469 Dali::Texture mCaptureTexture; // First Captured texture, but it is Y-inverted.
470 Dali::FrameBuffer mCaptureFrameBuffer; // First Captured FBO, but it is Y-inverted.
471 Dali::CameraActor mCaptureInvertCamera; // CameraActor to invert first captured buffer by second pass.
472 Dali::RenderTask mCaptureInvertTask; // RenderTask to invert first captured buffer.
473 Dali::Texture mCaptureInvertTexture; // Result texture of second pass. This is final Texture result.
474 Dali::FrameBuffer mCaptureInvertFrameBuffer; // FBO for firnal Texture result
477 Toolkit::Visual::Base mVisual;
479 /////////////////////////////////////////////////////////////
480 // FrameBuffer and Rendertask to render child objects as a 3D Scene
481 Dali::WeakHandle<Dali::Window> mWindow;
482 Integration::SceneHolder mSceneHolder;
483 CameraActor mDefaultCamera;
484 CameraActor mSelectedCamera;
485 std::vector<CameraActor> mCameras;
486 Dali::FrameBuffer mFrameBuffer;
487 Dali::Texture mTexture;
488 Dali::RenderTask mRenderTask;
490 int32_t mWindowOrientation;
492 Quaternion mSkyboxOrientation;
493 float mSkyboxIntensity{1.0f};
494 uint8_t mFrameBufferMultiSamplingLevel{0u};
495 Dali::Scene3D::SceneView::CaptureFinishedSignalType mCaptureFinishedSignal;
497 int32_t mCaptureId{0}; // Capture ID for requested capture, this is incrementally increasing.
498 std::vector<std::pair<Dali::RenderTask, std::shared_ptr<CaptureData>>> mCaptureContainer; // Container that stores CaptureData until the Capture is finished.
499 Dali::Timer mCaptureTimer; // Timer to check the capture is time out or not.
500 int32_t mTimerTickCount{0};
502 Dali::Integration::OrderedSet<Scene3D::Internal::LightObserver, false> mLightObservers; ///< The set of items to be notified when light properties change. (not owned)
504 bool mWindowSizeChanged{false};
505 uint32_t mWindowWidth{0};
506 uint32_t mWindowHeight{0};
509 std::string mAlphaMaskUrl;
510 float mMaskContentScaleFactor{1.0f};
511 bool mCropToMask{true};
512 bool mMaskingPropertyChanged{false};
515 Dali::Scene3D::Loader::ShaderManagerPtr mShaderManager;
518 std::vector<std::pair<Scene3D::Light, bool>> mLights; // Pair of Light object and flag that denotes the light is currently activated or not.
519 Dali::FrameBuffer mShadowFrameBuffer;
520 Dali::RenderTask mShadowMapRenderTask;
521 Scene3D::Light mShadowLight;
522 Dali::Texture mShadowTexture;
524 // Asynchronous Loading.
525 EnvironmentMapLoadTaskPtr mSkyboxLoadTask;
526 EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
527 EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
528 std::string mSkyboxUrl;
529 std::string mDiffuseIblUrl;
530 std::string mSpecularIblUrl;
532 Scene3D::EnvironmentMapType mSkyboxEnvironmentMapType{Scene3D::EnvironmentMapType::AUTO};
533 Dali::Texture mSkyboxTexture;
534 Dali::Texture mDiffuseTexture;
535 Dali::Texture mSpecularTexture;
536 float mIblScaleFactor{1.0f};
537 uint32_t mSpecularMipmapLevels{1u};
538 bool mUseFrameBuffer{false};
539 bool mSkyboxResourceReady{true};
540 bool mIblDiffuseResourceReady{true};
541 bool mIblSpecularResourceReady{true};
542 bool mSkyboxDirty{false};
543 bool mIblDiffuseDirty{false};
544 bool mIblSpecularDirty{false};
547 } // namespace Internal
549 // Helpers for public-api forwarding methods
550 inline Dali::Scene3D::Internal::SceneView& GetImpl(Dali::Scene3D::SceneView& obj)
552 DALI_ASSERT_ALWAYS(obj);
553 Dali::RefObject& handle = obj.GetImplementation();
554 return static_cast<Dali::Scene3D::Internal::SceneView&>(handle);
557 inline const Dali::Scene3D::Internal::SceneView& GetImpl(const Dali::Scene3D::SceneView& obj)
559 DALI_ASSERT_ALWAYS(obj);
560 const Dali::RefObject& handle = obj.GetImplementation();
561 return static_cast<const Dali::Scene3D::Internal::SceneView&>(handle);
564 } // namespace Scene3D
568 #endif // DALI_SCENE3D_INTERNAL_SCENE_VIEW_H