Last time I asked if you find the pixel shaders useful in WPF applications. There were a couple of answers giving links to online resources, but no one did actually say they use shaders or find any practical application. Most of the examples in the internet target transition effects and I currently can’t think of any other practical application. Why so? Well, transition effects are good way to veil long loading times without using WPF transitions/animations, i.e. without increasing the CPU load (assuming effects are hardware accelerated). Applying effects constantly, on the other hand, will require rendering the scene all the time, which will inevitably increase the CPU load. For line-of-business applications this, of course, is highly unwanted effect.
So, back to the transition effects, let me share one of my accomplishments with you:
And here is the code*:
sampler2D implicitInput : register(s0); // Progress range [0; 1] // TransitionRange [0; 1) // Center X, Y values range: [0; 1] float4 ToGreyScale(float4 color) : COLOR; float4 main(float2 uv : TEXCOORD) : COLOR float totalProgress = progress * (1 + transitionRange); float p = EvaluateProgress(totalProgress, center); float distance = length(distanceVector); if (distance <= p2) if (distance > p) float d = pow((distance - p2) / (p - p2), 2); return (1-d) * d * ToGreyScale(originalColor) + (1-d) * originalColor; float EvaluateProgress(float progress, float2 center) return progress * dist; float4 ToGreyScale(float4 color) : COLOR |
* Since I use ShaderPad to write effects, the center X & Y values are split in two. Also some calculations can be extracted outside the shader (such as most of the code in the EvaluateProgress method).