一大早就看到群友討論怎么快速讓界面黑白灰效果,這不突然想起來N年前咱簡單通過ShaderEffects調節過飽和度、對比度、亮度;今天再次玩耍一下;來看看最終實現的效果:1、核心代碼:
sampler2D implicitInput : register(s0);
float?factor?:?register(c0);
float4 main(float2 uv : TEXCOORD) : COLOR
{float4 color = tex2D(implicitInput, uv);float gray = color.r * 0.3 + color.g * 0.59 + color.b *0.11; float4 result; result.r = (color.r - gray) * factor + gray;result.g = (color.g - gray) * factor + gray;result.b = (color.b - gray) * factor + gray;result.a = color.a;return result;
}
2、在Shazzam中編譯生成cs和ps文件:
3、來看看cs文件:
public class GrayscaleEffectEffect : ShaderEffect{public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(GrayscaleEffectEffect), 0);public static readonly DependencyProperty FactorProperty = DependencyProperty.Register("Factor", typeof(double), typeof(GrayscaleEffectEffect), new UIPropertyMetadata(((double)(0D)), PixelShaderConstantCallback(0)));public GrayscaleEffectEffect(){PixelShader pixelShader = new PixelShader();pixelShader.UriSource = new Uri("/MapDemo;component/ShaderEffects/GrayscaleEffectEffect.ps", UriKind.Relative);this.PixelShader = pixelShader;this.UpdateShaderValue(InputProperty);this.UpdateShaderValue(FactorProperty);}public Brush Input{get{return ((Brush)(this.GetValue(InputProperty)));}set{this.SetValue(InputProperty, value);}}public double Factor{get{return ((double)(this.GetValue(FactorProperty)));}set{this.SetValue(FactorProperty, value);}}}
4、前臺調用就很簡單
<Grid Grid.Row="1"><Grid.Background><ImageBrush ImageSource="Images/Back.jpg" Stretch="Fill"/></Grid.Background><Grid.Effect><ShaderEffects:GrayscaleEffectEffect Factor="{Binding ElementName=BlackTbtn,Path=IsChecked,Converter={StaticResource ToggleButtonIsCheckedToGrayscaleEffectEffectConverter}}"/></Grid.Effect>
</Grid>
5、最后來個參考
http://bursjootech.blogspot.com/2008/06/grayscale-effect-pixel-shader-effect-in.html
最終簡單的效果先這樣吧;以后有時間的話,可以再去摸索一下更復雜的效果
;編程不息、Bug不止、無Bug、無生活
;改bug的冷靜、編碼的激情、完成后的喜悅、挖坑的激動 、填坑的興奮;這也許就是屌絲程序員的樂趣吧;今天就到這里吧;希望自己有動力一步一步堅持下去;生命不息,代碼不止;大家抽空可以看看今天分享的效果,有好的意見和想法,可以在留言板隨意留言;我看到后會第一時間回復大家,多謝大家的一直默默的關注和支持!如果覺得不錯,那就伸出您的小手點個贊并關注一下!