void ShadowView::SetShaderConstants()
{
    CustomActor self = Self();

    mShadowRenderShader.SetUniform( SHADER_LIGHT_CAMERA_PROJECTION_MATRIX_PROPERTY_NAME, Matrix::IDENTITY );
    mShadowRenderShader.SetUniform( SHADER_LIGHT_CAMERA_VIEW_MATRIX_PROPERTY_NAME, Matrix::IDENTITY );
    mShadowRenderShader.SetUniform( SHADER_SHADOW_COLOR_PROPERTY_NAME, mCachedShadowColor );

    Property::Index lightCameraProjectionMatrixPropertyIndex = mShadowRenderShader.GetPropertyIndex(SHADER_LIGHT_CAMERA_PROJECTION_MATRIX_PROPERTY_NAME);
    Property::Index lightCameraViewMatrixPropertyIndex = mShadowRenderShader.GetPropertyIndex(SHADER_LIGHT_CAMERA_VIEW_MATRIX_PROPERTY_NAME);

    Constraint projectionMatrixConstraint = Constraint::New<Dali::Matrix>( lightCameraProjectionMatrixPropertyIndex, Source( mCameraActor, CameraActor::PROJECTION_MATRIX ), EqualToConstraintMatrix());
    Constraint viewMatrixConstraint = Constraint::New<Dali::Matrix>( lightCameraViewMatrixPropertyIndex, Source( mCameraActor, CameraActor::VIEW_MATRIX ), EqualToConstraintMatrix());

    mShadowRenderShader.ApplyConstraint(projectionMatrixConstraint);
    mShadowRenderShader.ApplyConstraint(viewMatrixConstraint);

    // Register a property that the user can use to control the blur in the internal object
    mBlurStrengthPropertyIndex = self.RegisterProperty(BLUR_STRENGTH_PROPERTY_NAME, BLUR_STRENGTH_DEFAULT);
    mBlurFilter.GetHandleForAnimateBlurStrength().ApplyConstraint( Constraint::New<float>( mBlurFilter.GetBlurStrengthPropertyIndex() ,
            Source( self, mBlurStrengthPropertyIndex),
            EqualToConstraint()) );

    //  Register a property that the user can use to control the color of the shadow.
    Property::Index index = mShadowRenderShader.GetPropertyIndex(SHADER_SHADOW_COLOR_PROPERTY_NAME);
    mShadowColorPropertyIndex = self.RegisterProperty(SHADOW_COLOR_PROPERTY_NAME, mCachedShadowColor);

    mShadowRenderShader.ApplyConstraint(Constraint::New<Dali::Vector4>( index, Source( self, mShadowColorPropertyIndex ), EqualToConstraint()) );
}
Exemple #2
0
void ShadowView::OnInitialize()
{
  // root actor to parent all user added actors. Used as source actor for shadow render task.
  mChildrenRoot.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
  mChildrenRoot.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );

  Vector2 stageSize = Stage::GetCurrent().GetSize();
  mCameraActor = CameraActor::New(stageSize);

  mCameraActor.SetParentOrigin( ParentOrigin::CENTER );

  // Target is constrained to point at the shadow plane origin
  mCameraActor.SetNearClippingPlane( 1.0f );
  mCameraActor.SetType( Dali::Camera::FREE_LOOK ); // Camera orientation constrained to point at shadow plane world position
  mCameraActor.SetOrientation(Radian(Degree(180)), Vector3::YAXIS);
  mCameraActor.SetPosition(DEFAULT_LIGHT_POSITION);


  Property::Map customShader;
  customShader[ "vertex-shader" ] = RENDER_SHADOW_VERTEX_SOURCE;
  customShader[ "fragment-shader" ] = RENDER_SHADOW_FRAGMENT_SOURCE;

  customShader[ "subdivide-grid-x" ] = 20;
  customShader[ "subdivide-grid-y" ] = 20;

  customShader[ "hints" ] = "output-is-transparent";

  mShadowRenderShader[ "shader" ] = customShader;

  // Create render targets needed for rendering from light's point of view
  mSceneFromLightRenderTarget = FrameBufferImage::New( stageSize.width, stageSize.height, Pixel::RGBA8888 );

  mOutputImage = FrameBufferImage::New( stageSize.width * 0.5f, stageSize.height * 0.5f, Pixel::RGBA8888 );

  //////////////////////////////////////////////////////
  // Connect to actor tree

  Self().Add( mChildrenRoot );
  Stage::GetCurrent().Add( mCameraActor );

  mBlurFilter.SetRefreshOnDemand(false);
  mBlurFilter.SetInputImage(mSceneFromLightRenderTarget);
  mBlurFilter.SetOutputImage(mOutputImage);
  mBlurFilter.SetSize(stageSize * 0.5f);
  mBlurFilter.SetPixelFormat(Pixel::RGBA8888);

  mBlurRootActor = Actor::New();
  mBlurRootActor.SetName( "BLUR_ROOT_ACTOR" );

  // Turn off inheritance to ensure filter renders properly
  mBlurRootActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
  mBlurRootActor.SetInheritOrientation(false);
  mBlurRootActor.SetInheritScale(false);
  mBlurRootActor.SetColorMode(USE_OWN_COLOR);

  Self().Add(mBlurRootActor);

  mBlurFilter.SetRootActor(mBlurRootActor);
  mBlurFilter.SetBackgroundColor(Vector4::ZERO);

  CustomActor self = Self();
  // Register a property that the user can use to control the blur in the internal object
  mBlurStrengthPropertyIndex = self.RegisterProperty(BLUR_STRENGTH_PROPERTY_NAME, BLUR_STRENGTH_DEFAULT);

  Constraint blurStrengthConstraint = Constraint::New<float>( mBlurFilter.GetHandleForAnimateBlurStrength(), mBlurFilter.GetBlurStrengthPropertyIndex(), EqualToConstraint() );
  blurStrengthConstraint.AddSource( Source( self, mBlurStrengthPropertyIndex) );
  blurStrengthConstraint.Apply();
}