2 * Copyright (c) 2023 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/integration-api/debug.h>
23 #include <dali/public-api/object/type-registry-helper.h>
24 #include <dali/public-api/object/type-registry.h>
27 #include <dali-scene3d/internal/controls/scene-view/scene-view-impl.h>
37 static constexpr uint32_t MAX_NUMBER_OF_LIGHT = 5;
38 static constexpr std::string_view LIGHT_COUNT_STRING("uLightCount");
39 static constexpr std::string_view LIGHT_DIRECTION_STRING("uLightDirection");
40 static constexpr std::string_view LIGHT_COLOR_STRING("uLightColor");
41 static constexpr std::string_view SHADOW_ENABLED_STRING("uIsShadowEnabled");
42 static constexpr std::string_view SHADOW_VIEW_PROJECTION_MATRIX_STRING("uShadowLightViewProjectionMatrix");
45 * Creates control through type registry
49 return Scene3D::Light::New();
52 // Setup properties, signals and actions using the type-registry.
53 DALI_TYPE_REGISTRATION_BEGIN(Scene3D::Light, Dali::CustomActor, Create);
54 DALI_TYPE_REGISTRATION_END()
55 } // unnamed namespace
57 Dali::Scene3D::Light Light::New()
59 // Create the implementation, temporarily owned on stack
60 IntrusivePtr<Light> nodeImpl = new Light();
62 // Pass ownership to handle
63 Scene3D::Light handle(*nodeImpl);
65 // Second-phase init of the implementation
66 // This can only be done after the CustomActor connection has been made...
67 nodeImpl->Initialize();
73 : CustomActorImpl(ActorFlags::DISABLE_SIZE_NEGOTIATION)
81 void Light::Initialize()
83 Self().SetProperty(Dali::Actor::Property::COLOR, Color::WHITE);
85 // Directional Light setting
86 mLightSourceActor = Dali::CameraActor::New();
87 mLightSourceActor.SetProjectionMode(Dali::Camera::ORTHOGRAPHIC_PROJECTION);
88 mLightSourceActor.SetProperty(Dali::Actor::Property::POSITION, Vector3::ZERO);
89 mLightSourceActor.SetProperty(Dali::Actor::Property::ORIENTATION, Quaternion());
90 Self().Add(mLightSourceActor);
93 void Light::Enable(bool enable)
95 if(enable == mIsEnabled)
101 Scene3D::SceneView sceneView = mParentSceneView.GetHandle();
109 GetImpl(sceneView).AddLight(Scene3D::Light::DownCast(Self()));
113 GetImpl(sceneView).RemoveLight(Scene3D::Light::DownCast(Self()));
117 bool Light::IsEnabled() const
122 void Light::EnableShadow(bool enable)
124 if(enable == mIsShadowEnabled)
128 mIsShadowEnabled = enable;
130 Scene3D::SceneView sceneView = mParentSceneView.GetHandle();
138 GetImpl(sceneView).SetShadow(Scene3D::Light::DownCast(Self()));
142 GetImpl(sceneView).RemoveShadow(Scene3D::Light::DownCast(Self()));
146 bool Light::IsShadowEnabled() const
148 return mIsShadowEnabled;
151 CameraActor Light::GetCamera() const
153 return mLightSourceActor;
156 void Light::EnableShadowSoftFiltering(bool useSoftFiltering)
158 mUseSoftFiltering = useSoftFiltering;
159 UpdateShadowUniforms();
162 bool Light::IsShadowSoftFilteringEnabled() const
164 return mUseSoftFiltering;
167 void Light::SetShadowIntensity(float shadowIntensity)
169 mShadowIntensity = shadowIntensity;
170 UpdateShadowUniforms();
173 float Light::GetShadowIntensity() const
175 return mShadowIntensity;
178 void Light::SetShadowBias(float shadowBias)
180 mShadowBias = shadowBias;
181 UpdateShadowUniforms();
184 float Light::GetShadowBias() const
189 void Light::OnSceneConnection(int depth)
191 Actor parent = Self().GetParent();
194 Scene3D::SceneView sceneView = Scene3D::SceneView::DownCast(parent);
197 mParentSceneView = sceneView;
200 GetImpl(sceneView).AddLight(Scene3D::Light::DownCast(Self()));
204 GetImpl(sceneView).SetShadow(Scene3D::Light::DownCast(Self()));
208 parent = parent.GetParent();
212 void Light::OnSceneDisconnection()
214 Scene3D::SceneView sceneView = mParentSceneView.GetHandle();
217 mParentSceneView.Reset();
218 GetImpl(sceneView).RemoveLight(Scene3D::Light::DownCast(Self()));
222 void Light::OnChildAdd(Actor& child)
226 void Light::OnChildRemove(Actor& child)
230 void Light::OnSizeSet(const Vector3& targetSize)
234 void Light::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
236 // @todo size negotiate background to new size, animate as well?
239 void Light::OnRelayout(const Vector2& size, RelayoutContainer& container)
243 void Light::OnSetResizePolicy(ResizePolicy::Type policy, Dimension::Type dimension)
247 Vector3 Light::GetNaturalSize()
249 return Vector3::ZERO;
252 float Light::CalculateChildSize(const Dali::Actor& child, Dimension::Type dimension)
257 float Light::GetHeightForWidth(float width)
262 float Light::GetWidthForHeight(float height)
267 bool Light::RelayoutDependentOnChildren(Dimension::Type dimension)
272 void Light::OnCalculateRelayoutSize(Dimension::Type dimension)
276 void Light::OnLayoutNegotiated(float size, Dimension::Type dimension)
280 Light& GetImplementation(Dali::Scene3D::Light& handle)
282 CustomActorImpl& customInterface = handle.GetImplementation();
283 Light& impl = dynamic_cast<Internal::Light&>(customInterface);
287 const Light& GetImplementation(const Dali::Scene3D::Light& handle)
289 const CustomActorImpl& customInterface = handle.GetImplementation();
290 // downcast to control
291 const Light& impl = dynamic_cast<const Internal::Light&>(customInterface);
297 uint32_t Light::GetMaximumEnabledLightCount()
299 return MAX_NUMBER_OF_LIGHT;
302 std::string_view Light::GetLightCountUniformName()
304 return LIGHT_COUNT_STRING;
307 std::string_view Light::GetLightDirectionUniformName()
309 return LIGHT_DIRECTION_STRING;
312 std::string_view Light::GetLightColorUniformName()
314 return LIGHT_COLOR_STRING;
317 std::string_view Light::GetShadowEnabledUniformName()
319 return SHADOW_ENABLED_STRING;
322 std::string_view Light::GetShadowViewProjectionMatrixUniformName()
324 return SHADOW_VIEW_PROJECTION_MATRIX_STRING;
327 void Light::UpdateShadowUniforms()
329 Scene3D::SceneView sceneView = mParentSceneView.GetHandle();
337 GetImpl(sceneView).UpdateShadowUniform(Scene3D::Light::DownCast(Self()));
341 } // namespace Internal
343 } // namespace Scene3D