通过上一节的解说,大家是否已经对HLSL有了较深刻的认识和理解,HLSL的渲染不仅仅局限于静态处理,通过时时更新HLSL代码的各全局变量值同样可以实现动画形式的渲染,非常Cool对吧~。那么本节我将向大家介绍如何在Silverlight平台上实现HLSL动画渲染特效。
以BandedSwirl(螺旋波纹)渲染特效为例,我们首先要做的是按照上一节的方法将BandedSwirl.ps文件添加进项目中,同时创建一个对应的BandedSwirl.cs文件:
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;
namespace Silverlight.Shader {
public class BandedSwirl : ShaderEffect {
public static DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input",typeof(BandedSwirl),0);
public static DependencyProperty CenterProperty = DependencyProperty.Register("Center",typeof(System.Windows.Point),new PropertyMetadata(new System.Windows.Point(),PixelshaderconstantCallback(0)));
public static DependencyProperty SpiralstrengthProperty = DependencyProperty.Register("Spiralstrength",typeof(double),new PropertyMetadata(new double(),PixelshaderconstantCallback(1)));
public static DependencyProperty distancethresholdProperty = DependencyProperty.Register("distancethreshold",PixelshaderconstantCallback(2)));
public BandedSwirl(PixelShader shader) {
PixelShader = shader;
this.UpdateShaderValue(InputProperty);
this.UpdateShaderValue(CenterProperty);
this.UpdateShaderValue(SpiralstrengthProperty);
this.UpdateShaderValue(distancethresholdProperty);
}
public virtual System.Windows.Media.Brush Input {
get {
return ((System.Windows.Media.Brush)(GetValue(InputProperty)));
}
set {
SetValue(InputProperty,value);
}
}
public virtual System.Windows.Point Center {
get {
return ((System.Windows.Point)(GetValue(CenterProperty)));
}
set {
SetValue(CenterProperty,value);
}
}
public virtual double Spiralstrength {
get {
return ((double)(GetValue(SpiralstrengthProperty)));
}
set {
SetValue(SpiralstrengthProperty,value);
}
}
public virtual double distancethreshold {
get {
return ((double)(GetValue(distancethresholdProperty)));
}
set {
SetValue(distancethresholdProperty,value);
}
}
}
}
pixelShader = new PixelShader() {
UriSource = GetShaderUri("BandedSwirl.ps")
};
BandedSwirl bandedSwirl = new BandedSwirl(pixelShader) {
Center = new Point(0.5,0.5),
Spiralstrength = 1,
distancethreshold = 0
};
Spirit.Effect = bandedSwirl;
最后就是关键环节了,如何实现动画效果呢?大家是否有注意到BandedSwirl类中的CenterProperty、SpiralstrengthProperty、distancethresholdProperty这三个DependencyProperty(关联属性)参数,它们分别代表该渲染特效的中心、螺旋强度和延伸阈值。由于是关联属性,所以我们可以直接使用Storyboard去实现基于它们的渐变动画,那么以动态修改distancethreshold值为例,具体实现代码如下:
BeginShaderAnimation(bandedSwirl,1,3,"distancethreshold");
///
/// 启动渲染动画
///
private void BeginShaderAnimation(DependencyObject shader,double from,double to,double timeSpanFromSeconds,string dependencyProperty) {
if (storyboard != null) { storyboard.Stop(); }
storyboard = new Storyboard();
storyboard.RepeatBehavior = RepeatBehavior.Forever;
storyboard.AutoReverse = true;
doubleAnimation = new DoubleAnimation();
doubleAnimation.From = from;
doubleAnimation.To = to;
doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(timeSpanFromSeconds));
Storyboard.SetTarget(doubleAnimation,shader);
Storyboard.SetTargetProperty(doubleAnimation,new PropertyPath(dependencyProperty));
storyboard.Children.Add(doubleAnimation);
storyboard.Begin();
}
通过一个Storyboard故事板,我们让bandedSwirl渲染特效的distancethreshold值在3秒时间内从0改变到1,然后反序列帧执行并不断循环。至此一个基于HLSL的螺旋波纹渲染特效就制作完成啦!以同样的方法我还特意制作了波浪渲染动画、放大渲染动画、模糊缩放渲染动画、环状发散渲染动画、挤压收缩渲染动画等几个动画,都非常非常的Cool哦~下面是它们的效果截图:
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/alamiye010/archive/2009/09/28/4612735.aspx
有些遗憾的是,目前的Silverlight3.0版本仅支持基于pixel(像素)的渲染,暂时还无法实现基于Vertex(顶点)的HLSL渲染,但是这些已经很大程度上能够满足我们现有的需求。基于HLSL的动画渲染特效能够通过简单的代码编写再配上合适的图片即可实现诸如光晕、雨雪、云雾、闪电、冰块等环境特效以及爆炸、激光、水晶等魔法特效,这一切一切的实现仅仅使用最大不过几十KB的存储空间,如果让我展望Silverlight的明天,我坚信,明天会更好。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。