Creating custom shaders for WPF applications
Example approach This creates a greyscale shader: Create a folder for your custom shaders Create a new class to interface the shader:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
Imports System.Windows.Media.Effects Public Class GrayscaleEffect Inherits ShaderEffect Private Shared _pixelShader As PixelShader = New PixelShader() With { .UriSource = New Uri("pack://application:,,,/WpfWorkspace;component/ShaderEffects/GrayscaleEffect.ps") } Public Sub New() PixelShader = _pixelShader UpdateShaderValue(InputProperty) UpdateShaderValue(DesaturationFactorProperty) End Sub Public Shared ReadOnly InputProperty As DependencyProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", GetType(GrayscaleEffect), 0) Public Property Input As Brush Get Return CType(GetValue(InputProperty), Brush) End Get Set(ByVal value As Brush) SetValue(InputProperty, value) End Set End Property Public Shared ReadOnly DesaturationFactorProperty As DependencyProperty = DependencyProperty.Register("DesaturationFactor", GetType(Double), GetType(GrayscaleEffect), New UIPropertyMetadata(0.0, PixelShaderConstantCallback(0), AddressOf CoerceDesaturationFactor)) Public Property DesaturationFactor As Double Get Return CDbl(GetValue(DesaturationFactorProperty)) End Get Set(ByVal value As Double) SetValue(DesaturationFactorProperty, value) End Set End Property Private Shared Function CoerceDesaturationFactor(ByVal d As DependencyObject, ByVal value As Object) As Object Dim effect As GrayscaleEffect = CType(d, GrayscaleEffect) Dim newFactor As Double = CDbl(value) If newFactor < 0.0 OrElse newFactor > 1.0 Then Return effect.DesaturationFactor End If Return newFactor End Function End Class |
Make sure […]