WPF自定义着色器效果双重动画不起作用 [英] WPF Custom Shader Effect Double Animations Not Working
问题描述
我无法在vb.net内获得双重动画.我已经成功编译了HLSL文件,当我将它在xaml文件中绑定到控件时,它就可以正常工作.我也可以使用.xaml.vb文件中的自定义效果,但无法在.xaml.vb文件中使用DoubleAnimation.
这是我的代码列表:
-----像素着色器代码------
I can''t get a double animation inside the vb.net working. I''ve successfully compiled HLSL file and when I bind it in the xaml file to a control, it works just fine. I can also use custom effect from the .xaml.vb file, but I can not get it to work with DoubleAnimation in the .xaml.vb file.
Here is the list of my codes:
----- Pixel Shader Code ------
// http://www.silverlightshow.net/items/Book-Folding-effect-using-Pixel-Shader.aspx
sampler2D input : register(s0);
/// <summary>The Left</summary>;
/// <minvalue>0</minvalue>;
/// <maxvalue>1</maxvalue>;
/// <defaultvalue>0</defaultvalue>;
float left : register(c0);
float4 transform(float2 uv : TEXCOORD) : COLOR
{
float right = 1 - left;
// transforming the curent point (uv) according to the new boundaries.
float2 tuv = float2((uv.x - left) / (right - left), uv.y);
float tx = tuv.x;
if (tx > 0.5)
{
tx = 1 - tx;
}
float top = left * tx;
float bottom = 1 - top;
if (uv.y >= top && uv.y <= bottom)
{
//linear interpolation between 0 and 1 considering the angle of folding.
float ty = lerp(0, 1, (tuv.y - top) / (bottom - top));
// get the pixel from the transformed x and interpolated y.
return tex2D(input, float2(tuv.x, ty));
}
return 0;
}
float4 main(float2 uv : TEXCOORD) : COLOR
{
float right = 1 - left;
if(uv.x > left && uv.x < right)
{
return transform(uv);
}
return 0;
}
----- .xaml.vb文件--------
----- .xaml.vb file --------
''Procedure I call to start double animation.
Public Sub ApplyEffect()
Dim sb As New Storyboard()
Dim da As New DoubleAnimation()
Storyboard.SetTarget(da, myButton)
Storyboard.SetTargetProperty(da, New PropertyPath(WPF_Effects.clEffects.clEffects.LeftProperty))
da.From = 0
da.[To] = 10
da.Duration = (TimeSpan.FromSeconds(2))
da.AutoReverse = True
da.RepeatBehavior = RepeatBehavior.Forever
sb.Children.Add(da)
sb.Begin(myButton)
End Sub
---- clEffects类-----
---- clEffects Class -----
Option Strict Off
Option Explicit On
Imports System
''Imports System.Net
Imports System.Windows
Imports System.Windows.Media
Imports System.Windows.Media.Effects
Imports System.Windows.Media.Media3D
#Region "FADE IN TRANSITION EFFECT"
Namespace clEffects
///<summary>A transition effect </summary>
Public Class clEffects
Inherits ShaderEffect
Public Shared ReadOnly InputProperty As DependencyProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", GetType(clEffects), 0)
Public Shared ReadOnly LeftProperty As DependencyProperty = DependencyProperty.Register("Left", GetType(Double), GetType(clEffects), New UIPropertyMetadata(CType(0.0R, Double), PixelShaderConstantCallback(0)))
Public Sub New()
MyBase.New()
Dim pixelShader As PixelShader = New PixelShader()
pixelShader.UriSource = New Uri("/WPF_Effects;component/Effect.ps", UriKind.Relative)
Me.PixelShader = pixelShader
Me.UpdateShaderValue(InputProperty)
Me.UpdateShaderValue(LeftProperty)
End Sub
Public Property Input() As Brush
Get
Return CType(Me.GetValue(InputProperty), Brush)
End Get
Set(ByVal value As Brush)
Me.SetValue(InputProperty, value)
End Set
End Property
''''''<summary>The Left </summary>
Public Property Left() As Double
Get
Return CType(Me.GetValue(LeftProperty), Double)
End Get
Set(ByVal value As Double)
Me.SetValue(LeftProperty, value)
End Set
End Property
End Class
End Namespace
#End Region
------ XAML WPF用户控件------
------ XAML WPF User Control ------
<usercontrol x:class="ucWPF" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006";
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WPF_Effects.clEffects"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<grid>
<stackpanel>
<Button Name="myButton" Content="Click!" Click="StartAnimation" Width="153"
Margin="60" Height="74">
<Button.RenderTransform>
<transformgroup>
<translatetransform x:name="myTranslateTransform" />
<scaletransform x:name="myScaleTransform" /><br mode=" hold=" /> </TransformGroup><br mode="></scaletransform></transformgroup></stackpanel></grid></usercontrol>
推荐答案
我终于知道了.
这是您需要做的:
在.xaml文件中,向对象添加资源代码,在我的情况下,我使用了StackPanel:
I finally figured it out.
Here is what you need to do:
in the .xaml file add a Resource code to an object, in my case I used StackPanel:
<stackpanel.resources>
<storyboard x:name="MyStoryboard" x:key="myEffect" xmlns:x="#unknown">
<doubleanimation storyboard.targetproperty="clEffects.Left" storyboard.targetname="sbEffect">
/>
</doubleanimation></storyboard>
</stackpanel.resources>
将自定义效果添加到.xaml文件中的按钮:
add custom effect to the button in .xaml file:
<Button.Effect>
<local:cleffects x:name="myclEffects" xmlns:x="#unknown" xmlns:local="#unknown" />
</Button.Effect>
将.xaml.vb代码修改为此:
``此代码可以放置在按钮事件或任何其他事件中.
modify .xaml.vb code to this:
''This code can be placed in a button event or any other events.
Dim da as New DoubleAnimation()
Storyboard.SetTarget(da, myButton1)
da.From = 0
da.[To] = 0.5
da.Duration = (TimeSpan.FromSeconds(2))
myclEffects.BeginAnimation(clEffects.clEffects.LeftProperty, da)
''clEffects is a custom HLSL Shader effect.
我还没有对此进行大量试验,所以我不知道缺点是什么.但是现在这解决了我的问题.
希望有人也会对此有用.
I have not experimented alot with this yet, so I don''t know what the draw backs are. But for now this solved my issue.
Hope someone will find a use for this too.
这篇关于WPF自定义着色器效果双重动画不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!