WPF自定义效果动画编程

时间:2015-10-07 13:07:03

标签: c# wpf animation shader hlsl

我尝试在WPF中以编程方式使用像素着色器效果动画,但是下面的代码使用了大量的CPU能力。 还有其他方法吗?

■通话方式 在执行序列时设置效果。

private void Button_Click(object sender, RoutedEventArgs e)
{
    string path = System.IO.Path.GetFullPath(@"WateryEffect.ps");
    var uri = new Uri(path);
    WpfEffect wpfeffect = new WpfEffect(uri);

    BitmapImage img1 = new BitmapImage(new Uri(System.IO.Path.GetFullPath(@"3a28168e68.jpg")));
    ImageBrush imgbrush1 = new ImageBrush(img1);
    wpfeffect.input1 = imgbrush1;

    VisualBrush vbrush = new VisualBrush(text1);
    wpfeffect.input1 = vbrush;

    BitmapImage img2 = new BitmapImage(new Uri(System.IO.Path.GetFullPath(@"e98f58fa-2e58-4d38-9949-f3f40b6a2b13_7.jpg")));
    ImageBrush imgbrush2 = new ImageBrush(img2);
    wpfeffect.input2 = imgbrush2;

    double i = 1;
    do
    {
        wpfeffect.para0 = i;

        image1.Effect = wpfeffect;
        image2.Effect = wpfeffect;
        image3.Effect = wpfeffect;
        image4.Effect = wpfeffect;
        DoEvents();
        i = i - 0.0001;

    } while (i > -1);

}

1 个答案:

答案 0 :(得分:1)

为此做一个循环违背了WPF的全部优势。您需要将此效果更新作为DoubleAnimation运行。我没有你的.ps文件,但我假设para01是DependencyProperty。

试试这段代码:

DoubleAnimation da = 
new DoubleAnimation(0.0, 1.0, new Duration(new TimeSpan(0,0,1)); // adjust as needed

(image1.Effect as WpfEffect).BeginAnimation(WpfEffect.para01Property, da);
(image2.Effect as WpfEffect).BeginAnimation(WpfEffect.para01Property, da);
(image3.Effect as WpfEffect).BeginAnimation(WpfEffect.para01Property, da);
(image4.Effect as WpfEffect).BeginAnimation(WpfEffect.para01Property, da);