ShaderEffect Class
Updated: July 2008
Provides a custom bitmap effect by using a PixelShader.
Assembly: PresentationCore (in PresentationCore.dll)
Derive from the ShaderEffect class to implement a custom effect based on a single pixel shader.
The following steps show how to create a custom effect.
Load a PixelShader from precompiled High Level Shading Language (HLSL) bytecode.
Define dependency properties that represent the parameters of the effect and the Brush-based surface inputs. Use one of the RegisterPixelShaderSamplerProperty overloads to associate these inputs with register numbers that are referenced in the HLSL bytecode.
The following code example shows how to derive from the ShaderEffect class.
using System; using System.Windows; using System.Windows.Media; using System.Windows.Media.Effects; using System.Reflection; namespace ShaderEffectDemo { public class ThresholdEffect : ShaderEffect { private static PixelShader _pixelShader = new PixelShader() { UriSource = MakePackUri("ThresholdEffect.fx.ps") }; public ThresholdEffect() { PixelShader = _pixelShader; UpdateShaderValue(InputProperty); UpdateShaderValue(ThresholdProperty); UpdateShaderValue(BlankColorProperty); } // MakePackUri is a utility method for computing a pack uri // for the given resource. public static Uri MakePackUri(string relativeFile) { Assembly a = typeof(ThresholdEffect).Assembly; // Extract the short name. string assemblyShortName = a.ToString().Split(',')[0]; string uriString = "pack://application:,,,/" + assemblyShortName + ";component/" + relativeFile; return new Uri(uriString); } /////////////////////////////////////////////////////////////////////// #region Input dependency property public Brush Input { get { return (Brush)GetValue(InputProperty); } set { SetValue(InputProperty, value); } } public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(ThresholdEffect), 0); #endregion /////////////////////////////////////////////////////////////////////// #region Threshold dependency property public double Threshold { get { return (double)GetValue(ThresholdProperty); } set { SetValue(ThresholdProperty, value); } } public static readonly DependencyProperty ThresholdProperty = DependencyProperty.Register("Threshold", typeof(double), typeof(ThresholdEffect), new UIPropertyMetadata(0.5, PixelShaderConstantCallback(0))); #endregion /////////////////////////////////////////////////////////////////////// #region BlankColor dependency property public Color BlankColor { get { return (Color)GetValue(BlankColorProperty); } set { SetValue(BlankColorProperty, value); } } public static readonly DependencyProperty BlankColorProperty = DependencyProperty.Register("BlankColor", typeof(Color), typeof(ThresholdEffect), new UIPropertyMetadata(Colors.Transparent, PixelShaderConstantCallback(1))); #endregion } }
The following code example shows a shader that corresponds to the previous ShaderEffect class.
// Threshold shader // Object Declarations sampler2D implicitInput : register(s0); float threshold : register(c0); float4 blankColor : register(c1); //-------------------------------------------------------------------------------------- // Pixel Shader //-------------------------------------------------------------------------------------- float4 main(float2 uv : TEXCOORD) : COLOR { float4 color = tex2D(implicitInput, uv); float intensity = (color.r + color.g + color.b) / 3; float4 result; if (intensity > threshold) { result = color; } else { result = blankColor; } return result; }
The following XAML shows how to use the custom shader effect.
<Window x:Class="ShaderEffectDemo.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ShaderEffectDemo" Title="Window1" Height="300" Width="300"> <Window.Resources> <local:ThresholdEffect x:Key="thresholdEffect" Threshold="0.25" BlankColor="Orange" /> </Window.Resources> <Grid Effect="{StaticResource thresholdEffect}"> </Grid> </Window>
- UIPermission
for full access to UI resources. Associated enumeration: UIPermissionWindow.AllWindows
System.Windows.Threading.DispatcherObject
System.Windows.DependencyObject
System.Windows.Freezable
System.Windows.Media.Animation.Animatable
System.Windows.Media.Effects.Effect
System.Windows.Media.Effects.ShaderEffect
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.