通过上一节的解说,大家是否已经对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);
}
}
}
}
接下来在后台cs代码中创建渲染特效实例:
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的明天,我坚信,明天会更好。
总结以上是内存溢出为你收集整理的HLSL自定义渲染特效之完美攻略(中)全部内容,希望文章能够帮你解决HLSL自定义渲染特效之完美攻略(中)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)