2 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-scene3d/internal/light/light-impl.h>
22 #include <dali-toolkit/devel-api/controls/control-devel.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/object/type-registry-helper.h>
25 #include <dali/public-api/object/type-registry.h>
28 #include <dali-scene3d/internal/controls/scene-view/scene-view-impl.h>
38 static constexpr uint32_t MAX_NUMBER_OF_LIGHT = 5;
39 static constexpr std::string_view LIGHT_COUNT_STRING("uLightCount");
40 static constexpr std::string_view LIGHT_DIRECTION_STRING("uLightDirection");
41 static constexpr std::string_view LIGHT_COLOR_STRING("uLightColor");
42 static constexpr std::string_view SHADOW_ENABLED_STRING("uIsShadowEnabled");
43 static constexpr std::string_view SHADOW_VIEW_PROJECTION_MATRIX_STRING("uShadowLightViewProjectionMatrix");
46 * Creates control through type registry
50 return Scene3D::Light::New();
53 // Setup properties, signals and actions using the type-registry.
54 DALI_TYPE_REGISTRATION_BEGIN(Scene3D::Light, Dali::CustomActor, Create);
55 DALI_TYPE_REGISTRATION_END()
56 } // unnamed namespace
58 Dali::Scene3D::Light Light::New()
60 // Create the implementation, temporarily owned on stack
61 IntrusivePtr<Light> nodeImpl = new Light();
63 // Pass ownership to handle
64 Scene3D::Light handle(*nodeImpl);
66 // Second-phase init of the implementation
67 // This can only be done after the CustomActor connection has been made...
68 nodeImpl->Initialize();
74 : Control(static_cast<ControlBehaviour>(ControlBehaviour::DISABLE_STYLE_CHANGE_SIGNALS | ActorFlags::DISABLE_SIZE_NEGOTIATION))
82 // From Internal::Control.
84 void Light::OnInitialize()
88 // TODO : We need to check this is enough.
89 Toolkit::DevelControl::EnableCreateAccessible(Toolkit::Control::DownCast(self), false);
91 self.SetProperty(Dali::Actor::Property::COLOR, Color::WHITE);
93 // Directional Light setting
94 mLightSourceActor = Dali::CameraActor::New();
95 mLightSourceActor.SetProjectionMode(Dali::Camera::ORTHOGRAPHIC_PROJECTION);
96 mLightSourceActor.SetProperty(Dali::Actor::Property::POSITION, Vector3::ZERO);
97 mLightSourceActor.SetProperty(Dali::Actor::Property::ORIENTATION, Quaternion());
98 self.Add(mLightSourceActor);
101 // From CustomActorImpl.
103 void Light::OnSceneConnection(int depth)
105 Actor parent = Self().GetParent();
108 Scene3D::SceneView sceneView = Scene3D::SceneView::DownCast(parent);
111 mParentSceneView = sceneView;
114 GetImpl(sceneView).AddLight(Scene3D::Light::DownCast(Self()));
118 GetImpl(sceneView).SetShadow(Scene3D::Light::DownCast(Self()));
122 parent = parent.GetParent();
126 void Light::OnSceneDisconnection()
128 Scene3D::SceneView sceneView = mParentSceneView.GetHandle();
131 mParentSceneView.Reset();
132 GetImpl(sceneView).RemoveLight(Scene3D::Light::DownCast(Self()));
136 void Light::OnChildAdd(Actor& child)
140 void Light::OnChildRemove(Actor& child)
144 void Light::OnSizeSet(const Vector3& targetSize)
148 void Light::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
150 // @todo size negotiate background to new size, animate as well?
153 void Light::OnRelayout(const Vector2& size, RelayoutContainer& container)
157 void Light::OnSetResizePolicy(ResizePolicy::Type policy, Dimension::Type dimension)
161 Vector3 Light::GetNaturalSize()
163 return Vector3::ZERO;
166 float Light::CalculateChildSize(const Dali::Actor& child, Dimension::Type dimension)
171 float Light::GetHeightForWidth(float width)
176 float Light::GetWidthForHeight(float height)
181 bool Light::RelayoutDependentOnChildren(Dimension::Type dimension)
186 void Light::OnCalculateRelayoutSize(Dimension::Type dimension)
190 void Light::OnLayoutNegotiated(float size, Dimension::Type dimension)
194 Light& GetImplementation(Dali::Scene3D::Light& handle)
196 CustomActorImpl& customInterface = handle.GetImplementation();
197 Light& impl = dynamic_cast<Internal::Light&>(customInterface);
201 const Light& GetImplementation(const Dali::Scene3D::Light& handle)
203 const CustomActorImpl& customInterface = handle.GetImplementation();
204 // downcast to control
205 const Light& impl = dynamic_cast<const Internal::Light&>(customInterface);
211 void Light::Enable(bool enable)
213 if(enable == mIsEnabled)
219 Scene3D::SceneView sceneView = mParentSceneView.GetHandle();
227 GetImpl(sceneView).AddLight(Scene3D::Light::DownCast(Self()));
231 GetImpl(sceneView).RemoveLight(Scene3D::Light::DownCast(Self()));
235 bool Light::IsEnabled() const
240 void Light::EnableShadow(bool enable)
242 if(enable == mIsShadowEnabled)
246 mIsShadowEnabled = enable;
248 Scene3D::SceneView sceneView = mParentSceneView.GetHandle();
256 GetImpl(sceneView).SetShadow(Scene3D::Light::DownCast(Self()));
260 GetImpl(sceneView).RemoveShadow(Scene3D::Light::DownCast(Self()));
264 bool Light::IsShadowEnabled() const
266 return mIsShadowEnabled;
269 CameraActor Light::GetCamera() const
271 return mLightSourceActor;
274 void Light::EnableShadowSoftFiltering(bool useSoftFiltering)
276 mUseSoftFiltering = useSoftFiltering;
277 UpdateShadowUniforms();
280 bool Light::IsShadowSoftFilteringEnabled() const
282 return mUseSoftFiltering;
285 void Light::SetShadowIntensity(float shadowIntensity)
287 mShadowIntensity = shadowIntensity;
288 UpdateShadowUniforms();
291 float Light::GetShadowIntensity() const
293 return mShadowIntensity;
296 void Light::SetShadowBias(float shadowBias)
298 mShadowBias = shadowBias;
299 UpdateShadowUniforms();
302 float Light::GetShadowBias() const
307 // Public Static Method
309 uint32_t Light::GetMaximumEnabledLightCount()
311 return MAX_NUMBER_OF_LIGHT;
314 std::string_view Light::GetLightCountUniformName()
316 return LIGHT_COUNT_STRING;
319 std::string_view Light::GetLightDirectionUniformName()
321 return LIGHT_DIRECTION_STRING;
324 std::string_view Light::GetLightColorUniformName()
326 return LIGHT_COLOR_STRING;
329 std::string_view Light::GetShadowEnabledUniformName()
331 return SHADOW_ENABLED_STRING;
334 std::string_view Light::GetShadowViewProjectionMatrixUniformName()
336 return SHADOW_VIEW_PROJECTION_MATRIX_STRING;
341 void Light::UpdateShadowUniforms()
343 Scene3D::SceneView sceneView = mParentSceneView.GetHandle();
351 GetImpl(sceneView).UpdateShadowUniform(Scene3D::Light::DownCast(Self()));
355 } // namespace Internal
357 } // namespace Scene3D