Writing your first ShaderEffect for WPF 3.5 SP1
WPF architect Greg Schechter was written a series of articles on the new hardware accelerated shaders recently released in beta form, as part of .NET 3.5 SP1 beta. This article includes some sample code that was enough to get me started down the path of writing a very simple shader effect - I've dubbed it "watery" but it's more just "wavy".
In this example the "waviness" of the effect is bound to the value of the slider at the bottom of the screen. One thing that I thought was very nicely done by the WPF team was the mechanism for passing parameters from WPF into the HLSL code using an attached property declaration like this:
public static readonly DependencyProperty WavinessProperty =
DependencyProperty.Register("Waviness", typeof(double), typeof(WaterEffect), new UIPropertyMetadata(0d, PixelShaderConstantCallback(0)));
The last part is the most important bit, which wires up the dependency property that has been defined in our C# WPF code to a constant in the [very crude] pixel shader HLSL code:
sampler2D implicitInput : register(s0);
float waviness : register(C0);
float4 main( float2 Tex : TEXCOORD ) : COLOR
{
float4 Color;
Tex.y = Tex.y + (sin(Tex.x * waviness)* 0.01);
Color = tex2D( implicitInput, Tex.xy);
return Color;
}
At the moment this sample is very much alpha, and sometimes does some very weird things to the contents of the window. Regardless I'm very impressed at how approachable the WPF team have made this stuff.
Categories:
#
Neat post
posted by
OJ
on
5/15/2008 8:12:32 AM
:
This is a great little demo on how easy it can be to add funky effects using pixels shaders :)
I'm sure you've seen this before, but in case you haven't (and for the benefit of those others who haven't), check out <a href="
http://channel9.msdn.com/ShowPost.aspx?PostID=403854">this</a> neat commentry and demo of the new WPF features.
Cheers!
#
Finally! Attractive Pixel Geometry For The Masses
posted by
Mr. Sustainable
on
5/15/2008 10:49:00 PM
:
I learned web development from the desktop publishing side of the world - which means that I had an eye for image quality long before I had an eye for clean code and stable applications. With information like this, I can return to my roots and produce products which make everyone happy.