This repository has been archived by the owner on Dec 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdditiveEffect.cs
105 lines (89 loc) · 3.26 KB
/
AdditiveEffect.cs
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;
namespace OSharp.Animation.WPF
{
public class BlendEffect : ShaderEffect
{
#region Dependency Properties
public static readonly DependencyProperty BaseProperty =
ShaderEffect.RegisterPixelShaderSamplerProperty("Base", typeof(BlendEffect), 0);
public static readonly DependencyProperty BlendProperty =
ShaderEffect.RegisterPixelShaderSamplerProperty("Blend", typeof(BlendEffect), 1);
public static readonly DependencyProperty ModeProperty =
DependencyProperty.Register("Mode", typeof(BlendModes), typeof(BlendEffect),
new PropertyMetadata(BlendModes.Normal, OnModeChanged));
public static readonly DependencyProperty AmountProperty =
DependencyProperty.Register("Amount", typeof(double), typeof(BlendEffect),
new UIPropertyMetadata(0.5, PixelShaderConstantCallback(0)), OnValidateAmount);
/// <summary>
/// Brush that acts as the input for the background layer
/// </summary>
public Brush Base
{
get => (Brush)GetValue(BaseProperty);
set => SetValue(BaseProperty, value);
}
/// <summary>
/// Brush that acts as the input for the foreground layer
/// </summary>
public Brush Blend
{
get => (Brush)GetValue(BlendProperty);
set => SetValue(BlendProperty, value);
}
/// <summary>
/// Intensity of the color blending between 0 and 1.0
/// </summary>
public double Amount
{
get => (double)GetValue(AmountProperty);
set => SetValue(AmountProperty, value);
}
/// <summary>
/// Blend mode for calculating the result layer
/// </summary>
public BlendModes Mode
{
get => (BlendModes)GetValue(ModeProperty);
set => SetValue(ModeProperty, value);
}
#endregion
private static void OnModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (!(d is BlendEffect effect && e.NewValue is BlendModes modes)) return;
string shader;
switch (modes)
{
case BlendModes.Multiply:
shader = "shader/multiply.ps";
break;
case BlendModes.Normal:
default:
shader = "shader/add.ps";
break;
}
effect.PixelShader.UriSource = Global.MakePackUri(shader);
}
private static bool OnValidateAmount(object value)
{
return value is double amount && (amount >= 0 && amount <= 1.0);
}
public BlendEffect()
{
PixelShader = new PixelShader
{
UriSource = Global.MakePackUri("shader/add.ps")
};
Mode = BlendModes.Normal;
UpdateShaderValue(BaseProperty);
UpdateShaderValue(BlendProperty);
UpdateShaderValue(AmountProperty);
}
}
}