add MultiplyBlendEffect

This commit is contained in:
NaBian committed 2020-02-21 23:56:42 +08:00
1 parent 24d323c09d
commit f6da5e1101
15 files changed
+502 -71

No files matched your search

@@ -68,6 +68,8 @@
<Compile Include="$(MSBuildThisFileDirectory)Media\Animation\LinearGeometryKeyFrame.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Media\Animation\ResolvedKeyFrameEntry.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Media\Animation\SplineGeometryKeyFrame.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Media\Effects\BlendEffect.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Media\Effects\BlendEffectMode.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Media\Effects\BrightnessEffect.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Media\Effects\ColorComplementEffect.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Media\Effects\ColorMatrixEffect.cs" />
@@ -972,7 +974,10 @@
</Page>
</ItemGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)Resources\Effects\BrightnessEffect.fx" />
<None Include="$(MSBuildThisFileDirectory)Resources\Effects\ColorComplementEffect.fx" />
<None Include="$(MSBuildThisFileDirectory)Resources\Effects\ColorMatrixEffect.fx" />
<None Include="$(MSBuildThisFileDirectory)Resources\Effects\ContrastEffect.fx" />
<None Include="$(MSBuildThisFileDirectory)Resources\Effects\GrayScaleEffect.fx" />
</ItemGroup>
<ItemGroup>
@@ -0,0 +1,61 @@
//reference doc : https://docs.microsoft.com/en-us/windows/win32/direct2d/blend#blending-examples
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;
namespace HandyControl.Media.Effects
{
public class BlendEffect : ShaderEffect
{
private static readonly Dictionary<BlendEffectMode, PixelShader> ShaderDic;
public static readonly DependencyProperty BackgroundProperty = RegisterPixelShaderSamplerProperty("Background", typeof(BlendEffect), 0);
public static readonly DependencyProperty ForegroundProperty = RegisterPixelShaderSamplerProperty("Foreground", typeof(BlendEffect), 1);
static BlendEffect()
{
ShaderDic = new Dictionary<BlendEffectMode, PixelShader>();
}
public BlendEffect()
{
if (!ShaderDic.ContainsKey(Mode))
{
ShaderDic[Mode] = new PixelShader
{
UriSource = new Uri($"pack://application:,,,/HandyControl;component/Resources/Effects/{Mode.ToString()}BlendEffect.ps")
};
}
PixelShader = ShaderDic[Mode];
UpdateShaderValue(BackgroundProperty);
UpdateShaderValue(ForegroundProperty);
}
public Brush Background
{
get => (Brush)GetValue(BackgroundProperty);
set => SetValue(BackgroundProperty, value);
}
public Brush Foreground
{
get => (Brush)GetValue(ForegroundProperty);
set => SetValue(ForegroundProperty, value);
}
public static readonly DependencyProperty ModeProperty = DependencyProperty.Register(
"Mode", typeof(BlendEffectMode), typeof(BlendEffect), new PropertyMetadata(default(BlendEffectMode), PixelShaderConstantCallback(0)));
public BlendEffectMode Mode
{
get => (BlendEffectMode) GetValue(ModeProperty);
set => SetValue(ModeProperty, value);
}
}
}
@@ -0,0 +1,32 @@
namespace HandyControl.Media.Effects
{
public enum BlendEffectMode
{
Multiply,
Screen,
Darken,
Lighten,
Dissolve,
ColorBurn,
LinearBurn,
DarkerColor,
LighterColor,
ColorDodge,
LinearDodge,
Overlay,
SoftLight,
HardLight,
VividLight,
LinearLight,
PinLight,
HardMix,
Difference,
Exclusion,
Hue,
Saturation,
Color,
Luminosity,
Subtract,
Division
}
}
@@ -46,11 +46,6 @@ float4 main(float2 uv : TEXCOORD) : COLOR
k, l, m, n,
p, q, r, s
};
float4 matrixA2 =
{
e, j, o, t
};
return mul(matrixA1, color) + matrixA2;
return mul(matrixA1, color) + float4( e, j, o, t );
}
@@ -0,0 +1,15 @@
sampler2D background : register(s0);
sampler2D foreground : register(s1);
float4 main(float2 uv : TEXCOORD) : COLOR
{
float4 b = tex2D(background, uv);
float4 f = tex2D(foreground, uv);
float4 o = b;
f.rgb *= f.a;
b.rgb *= b.a;
o.rgb = f.rgb * b.rgb + f.rgb * (1 - b.a) + b.rgb * (1 - f.a);
return o;
}