2 * Copyright (c) 2014 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 "shadow-view-impl.h"
24 #include <dali/public-api/animation/constraint.h>
25 #include <dali/public-api/common/stage.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/devel-api/object/type-registry-helper.h>
28 #include <dali/public-api/render-tasks/render-task-list.h>
29 #include <dali/integration-api/debug.h>
32 #include <dali-toolkit/internal/controls/shadow-view/shadow-view-impl.h>
33 #include <dali-toolkit/internal/filters/blur-two-pass-filter.h>
36 // pixel format / size - set from JSON
37 // aspect ratio property needs to be able to be constrained also for cameras. (now do-able)
38 // default near clip value
41 /////////////////////////////////////////////////////////
42 // IMPLEMENTATION NOTES
44 // As the ShadowView actor changes size, the amount of pixels we need to blur changes. Therefore we need some way of doing this. However:-
45 // OnSetSize() does not get called when ShadowView object size is modified using a Constraint.
46 // OnSizeAnimation() only gets called once per AnimateTo/By() and if an Animation has N such calls then only the final one will end up being used. Therefore we can't use
47 // OnSizeAnimation() to alter render target sizes.
48 // To get around the above problems, we use fixed sized render targets, from the last SetSize() call (which calls OnSetSize()), then we adjust the internal cameras / actors
49 // to take account of the changed ShadowView object size, projecting to the unchanged render target sizes. This is done relative to the fixed render target / actor sizes
50 // by using constraints relative to the ShadowView actor size.
68 return Toolkit::ShadowView::New();
71 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ShadowView, Toolkit::Control, Create )
72 DALI_TYPE_REGISTRATION_END()
74 const float BLUR_STRENGTH_DEFAULT = 1.0f;
76 const Vector3 DEFAULT_LIGHT_POSITION(300.0f, 250.0f, 600.0f);
77 const float DEFAULT_FIELD_OF_VIEW_RADIANS = Math::PI / 4.0f; // 45 degrees
79 const Vector4 DEFAULT_SHADOW_COLOR = Vector4(0.2f, 0.2f, 0.2f, 0.8f);
81 const char* const SHADER_LIGHT_CAMERA_PROJECTION_MATRIX_PROPERTY_NAME = "uLightCameraProjectionMatrix";
82 const char* const SHADER_LIGHT_CAMERA_VIEW_MATRIX_PROPERTY_NAME = "uLightCameraViewMatrix";
83 const char* const SHADER_SHADOW_COLOR_PROPERTY_NAME = "uShadowColor";
84 const char* const BLUR_STRENGTH_PROPERTY_NAME = "BlurStrengthProperty";
85 const char* const SHADOW_COLOR_PROPERTY_NAME = "ShadowColorProperty";
87 const char* const RENDER_SHADOW_VERTEX_SOURCE =
89 " attribute mediump vec2 aPosition;\n"
90 " uniform mediump mat4 uMvpMatrix;\n"
91 " uniform mediump mat4 uModelMatrix;\n"
92 " uniform vec3 uSize;\n"
93 " varying vec2 vTexCoord;\n"
95 " uniform mediump mat4 uLightCameraProjectionMatrix;\n"
96 " uniform mediump mat4 uLightCameraViewMatrix;\n"
100 " mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n"
101 " vertexPosition.xyz *= uSize;\n"
102 " gl_Position = uMvpMatrix * vertexPosition;\n"
103 " vec4 textureCoords = uLightCameraProjectionMatrix * uLightCameraViewMatrix * uModelMatrix * vertexPosition;\n"
104 " vTexCoord = 0.5 + 0.5 * (textureCoords.xy/textureCoords.w);\n"
107 const char* const RENDER_SHADOW_FRAGMENT_SOURCE =
108 "varying mediump vec2 vTexCoord;\n"
109 "uniform lowp vec4 uShadowColor;\n"
110 "uniform sampler2D sTexture;\n"
114 " lowp float alpha;\n"
115 " alpha = texture2D(sTexture, vec2(vTexCoord.x, vTexCoord.y)).a;\n"
116 " gl_FragColor = vec4(uShadowColor.rgb, uShadowColor.a * alpha);\n"
121 ShadowView::ShadowView( float downsampleWidthScale, float downsampleHeightScale )
122 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ),
123 mChildrenRoot(Actor::New()),
124 mCachedShadowColor(DEFAULT_SHADOW_COLOR),
125 mCachedBackgroundColor(DEFAULT_SHADOW_COLOR.r, DEFAULT_SHADOW_COLOR.g, DEFAULT_SHADOW_COLOR.b, 0.0f),
126 mBlurStrengthPropertyIndex(Property::INVALID_INDEX),
127 mShadowColorPropertyIndex(Property::INVALID_INDEX),
128 mDownsampleWidthScale(downsampleWidthScale),
129 mDownsampleHeightScale(downsampleHeightScale)
133 ShadowView::~ShadowView()
137 Toolkit::ShadowView ShadowView::New(float downsampleWidthScale, float downsampleHeightScale)
139 ShadowView* impl = new ShadowView(downsampleWidthScale, downsampleHeightScale);
141 Dali::Toolkit::ShadowView handle = Dali::Toolkit::ShadowView( *impl );
143 // Second-phase init of the implementation
144 // This can only be done after the CustomActor connection has been made...
150 void ShadowView::SetShadowPlaneBackground(Actor shadowPlaneBackground)
152 mShadowPlaneBg = shadowPlaneBackground;
154 mShadowPlane = Toolkit::ImageView::New();
155 mShadowPlane.SetName( "SHADOW_PLANE" );
156 mShadowPlane.SetParentOrigin(ParentOrigin::CENTER);
157 mShadowPlane.SetAnchorPoint(AnchorPoint::CENTER);
159 mShadowPlane.SetImage(mOutputImage);
160 mShadowPlane.SetProperty( Toolkit::ImageView::Property::IMAGE, mShadowRenderShader );
161 SetShaderConstants();
163 // Rather than parent the shadow plane drawable and have constraints to move it to the same
164 // position, instead parent the shadow plane drawable on the shadow plane passed in.
165 mShadowPlaneBg.Add(mShadowPlane);
166 mShadowPlane.SetParentOrigin(ParentOrigin::CENTER);
167 mShadowPlane.SetZ(1.0f);
171 mShadowPlane.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
173 mBlurRootActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
176 void ShadowView::SetPointLight(Actor pointLight)
178 mPointLight = pointLight;
183 void ShadowView::SetPointLightFieldOfView(float fieldOfView)
185 mCameraActor.SetFieldOfView(fieldOfView);
188 void ShadowView::SetShadowColor(Vector4 color)
190 mCachedShadowColor = color;
191 mCachedBackgroundColor.r = color.r;
192 mCachedBackgroundColor.g = color.g;
193 mCachedBackgroundColor.b = color.b;
197 mShadowPlane.SetProperty( mShadowColorPropertyIndex, mCachedShadowColor );
201 mRenderSceneTask.SetClearColor( mCachedBackgroundColor );
205 void ShadowView::Activate()
207 DALI_ASSERT_ALWAYS( Self().OnStage() && "ShadowView should be on stage before calling Activate()\n" );
209 // make sure resources are allocated and start the render tasks processing
213 void ShadowView::Deactivate()
215 DALI_ASSERT_ALWAYS( Self().OnStage() && "ShadowView should be on stage before calling Deactivate()\n" )
217 // stop render tasks processing
218 // Note: render target resources are automatically freed since we set the Image::Unused flag
222 ///////////////////////////////////////////////////////////
227 void ShadowView::OnInitialize()
229 // root actor to parent all user added actors. Used as source actor for shadow render task.
230 mChildrenRoot.SetParentOrigin( ParentOrigin::CENTER );
231 mChildrenRoot.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
233 Vector2 stageSize = Stage::GetCurrent().GetSize();
234 mCameraActor = CameraActor::New(stageSize);
236 mCameraActor.SetParentOrigin( ParentOrigin::CENTER );
238 // Target is constrained to point at the shadow plane origin
239 mCameraActor.SetNearClippingPlane( 1.0f );
240 mCameraActor.SetType( Dali::Camera::FREE_LOOK ); // Camera orientation constrained to point at shadow plane world position
241 mCameraActor.SetOrientation(Radian(Degree(180)), Vector3::YAXIS);
242 mCameraActor.SetPosition(DEFAULT_LIGHT_POSITION);
245 Property::Map customShader;
246 customShader[ "vertexShader" ] = RENDER_SHADOW_VERTEX_SOURCE;
247 customShader[ "fragmentShader" ] = RENDER_SHADOW_FRAGMENT_SOURCE;
249 customShader[ "subdivideGridX" ] = 20;
250 customShader[ "subdivideGridY" ] = 20;
252 customShader[ "hints" ] = "outputIsTransparent";
254 mShadowRenderShader[ "shader" ] = customShader;
256 // Create render targets needed for rendering from light's point of view
257 mSceneFromLightRenderTarget = FrameBufferImage::New( stageSize.width, stageSize.height, Pixel::RGBA8888 );
259 mOutputImage = FrameBufferImage::New( stageSize.width * 0.5f, stageSize.height * 0.5f, Pixel::RGBA8888 );
261 //////////////////////////////////////////////////////
262 // Connect to actor tree
264 Self().Add( mChildrenRoot );
265 Stage::GetCurrent().Add( mCameraActor );
267 mBlurFilter.SetRefreshOnDemand(false);
268 mBlurFilter.SetInputImage(mSceneFromLightRenderTarget);
269 mBlurFilter.SetOutputImage(mOutputImage);
270 mBlurFilter.SetSize(stageSize * 0.5f);
271 mBlurFilter.SetPixelFormat(Pixel::RGBA8888);
273 mBlurRootActor = Actor::New();
274 mBlurRootActor.SetName( "BLUR_ROOT_ACTOR" );
276 // Turn off inheritance to ensure filter renders properly
277 mBlurRootActor.SetParentOrigin( ParentOrigin::CENTER );
278 mBlurRootActor.SetInheritOrientation(false);
279 mBlurRootActor.SetInheritScale(false);
280 mBlurRootActor.SetColorMode(USE_OWN_COLOR);
282 Self().Add(mBlurRootActor);
284 mBlurFilter.SetRootActor(mBlurRootActor);
285 mBlurFilter.SetBackgroundColor(Vector4::ZERO);
287 CustomActor self = Self();
288 // Register a property that the user can use to control the blur in the internal object
289 mBlurStrengthPropertyIndex = self.RegisterProperty(BLUR_STRENGTH_PROPERTY_NAME, BLUR_STRENGTH_DEFAULT);
291 Constraint blurStrengthConstraint = Constraint::New<float>( mBlurFilter.GetHandleForAnimateBlurStrength(), mBlurFilter.GetBlurStrengthPropertyIndex(), EqualToConstraint() );
292 blurStrengthConstraint.AddSource( Source( self, mBlurStrengthPropertyIndex) );
293 blurStrengthConstraint.Apply();
296 void ShadowView::OnChildAdd( Actor& child )
298 Control::OnChildAdd( child );
300 if( child != mChildrenRoot && child != mBlurRootActor)
302 mChildrenRoot.Add( child );
306 void ShadowView::OnChildRemove( Actor& child )
308 mChildrenRoot.Remove( child );
310 Control::OnChildRemove( child );
313 void ShadowView::ConstrainCamera()
315 if( mPointLight && mShadowPlane )
317 // Constrain camera to look directly at center of shadow plane. (mPointLight position
318 // is under control of application, can't use transform inheritance)
320 Constraint cameraOrientationConstraint = Constraint::New<Quaternion> ( mCameraActor, Actor::Property::ORIENTATION, &LookAt );
321 cameraOrientationConstraint.AddSource( Source( mShadowPlane, Actor::Property::WORLD_POSITION ) );
322 cameraOrientationConstraint.AddSource( Source( mPointLight, Actor::Property::WORLD_POSITION ) );
323 cameraOrientationConstraint.AddSource( Source( mShadowPlane, Actor::Property::WORLD_ORIENTATION ) );
324 cameraOrientationConstraint.Apply();
326 Constraint pointLightPositionConstraint = Constraint::New<Vector3>( mCameraActor, Actor::Property::POSITION, EqualToConstraint() );
327 pointLightPositionConstraint.AddSource( Source( mPointLight, Actor::Property::WORLD_POSITION ) );
328 pointLightPositionConstraint.Apply();
332 void ShadowView::CreateRenderTasks()
334 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
336 // We want the first task to render the scene from the light
337 mRenderSceneTask = taskList.CreateTask();
339 mRenderSceneTask.SetCameraActor( mCameraActor );
340 mRenderSceneTask.SetSourceActor( mChildrenRoot );
341 mRenderSceneTask.SetTargetFrameBuffer( mSceneFromLightRenderTarget );
342 mRenderSceneTask.SetInputEnabled( false );
343 mRenderSceneTask.SetClearEnabled( true );
345 // background color for render task should be the shadow color, but with alpha 0
346 // we don't want to blend the edges of the content with a BLACK at alpha 0, but
347 // the same shadow color at alpha 0.
348 mRenderSceneTask.SetClearColor( mCachedBackgroundColor );
350 mBlurFilter.Enable();
353 void ShadowView::RemoveRenderTasks()
355 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
357 taskList.RemoveTask(mRenderSceneTask);
358 mRenderSceneTask.Reset();
360 mBlurFilter.Disable();
363 void ShadowView::SetShaderConstants()
365 Property::Index lightCameraProjectionMatrixPropertyIndex = mShadowPlane.RegisterProperty( SHADER_LIGHT_CAMERA_PROJECTION_MATRIX_PROPERTY_NAME, Matrix::IDENTITY );
366 Constraint projectionMatrixConstraint = Constraint::New<Dali::Matrix>( mShadowPlane, lightCameraProjectionMatrixPropertyIndex, EqualToConstraint() );
367 projectionMatrixConstraint.AddSource( Source( mCameraActor, CameraActor::Property::PROJECTION_MATRIX ) );
368 projectionMatrixConstraint.Apply();
370 Property::Index lightCameraViewMatrixPropertyIndex = mShadowPlane.RegisterProperty( SHADER_LIGHT_CAMERA_VIEW_MATRIX_PROPERTY_NAME, Matrix::IDENTITY );
371 Constraint viewMatrixConstraint = Constraint::New<Dali::Matrix>( mShadowPlane, lightCameraViewMatrixPropertyIndex, EqualToConstraint() );
372 viewMatrixConstraint.AddSource( Source( mCameraActor, CameraActor::Property::VIEW_MATRIX ) );
373 viewMatrixConstraint.Apply();
375 mShadowColorPropertyIndex = mShadowPlane.RegisterProperty( SHADER_SHADOW_COLOR_PROPERTY_NAME, mCachedShadowColor );
378 } // namespace Internal
380 } // namespace Toolkit