-
Notifications
You must be signed in to change notification settings - Fork 38
/
NoiseBrush.cs
97 lines (82 loc) · 3.47 KB
/
NoiseBrush.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
using System;
using System.ComponentModel;
using Artemis.Core;
using Artemis.Core.LayerBrushes;
using Artemis.Plugins.LayerBrushes.Noise.Utilities;
using SkiaSharp;
namespace Artemis.Plugins.LayerBrushes.Noise
{
public class NoiseBrush : PerLedLayerBrush<NoiseBrushProperties>
{
private static readonly Random Rand = new Random();
private SKColor[] _colorMap;
private OpenSimplexNoise _noise;
private float _x;
private float _y;
private float _z;
public override void EnableLayerBrush()
{
_x = Rand.Next(0, 4096);
_y = Rand.Next(0, 4096);
_z = Rand.Next(0, 4096);
_noise = new OpenSimplexNoise(Rand.Next(0, 4096));
Properties.GradientColor.BaseValue.PropertyChanged += GradientColorChanged;
CreateColorMap();
}
public override void DisableLayerBrush()
{
}
public override void Update(double deltaTime)
{
_x += Properties.ScrollSpeed.CurrentValue.X / 500f / (float) deltaTime;
_y += Properties.ScrollSpeed.CurrentValue.Y / 500f / (float) deltaTime;
_z += Properties.AnimationSpeed.CurrentValue / 500f / 0.04f * (float) deltaTime;
// A telltale sign of someone who can't do math very well
if (float.IsPositiveInfinity(_x) || float.IsNegativeInfinity(_x) || float.IsNaN(_x))
_x = 0;
if (float.IsPositiveInfinity(_y) || float.IsNegativeInfinity(_y) || float.IsNaN(_y))
_y = 0;
if (float.IsPositiveInfinity(_z) || float.IsNegativeInfinity(_z) || float.IsNaN(_z))
_z = 0;
}
public override SKColor GetColor(ArtemisLed led, SKPoint renderPoint)
{
SKColor mainColor = Properties.MainColor.CurrentValue;
SKColor secondColor = Properties.SecondaryColor.CurrentValue;
ColorGradient gradientColor = Properties.GradientColor.CurrentValue;
SKSize scale = Properties.Scale.CurrentValue;
float hardness = Properties.Hardness.CurrentValue / 50f;
float scrolledX = renderPoint.X + _x;
if (float.IsNaN(scrolledX) || float.IsInfinity(scrolledX))
scrolledX = 0;
float scrolledY = renderPoint.Y + _y;
if (float.IsNaN(scrolledY) || float.IsInfinity(scrolledY))
scrolledY = 0;
float evalX = scrolledX * (scale.Width / 4000f);
float evalY = scrolledY * (scale.Height / 4000f);
float v = (float) _noise.Evaluate(evalX, evalY, _z) * hardness;
float amount = Math.Max(0f, Math.Min(1f, v));
if (Properties.ColorType.BaseValue == ColorMappingType.Simple)
return mainColor.Interpolate(secondColor, amount);
if (gradientColor != null && _colorMap.Length == 101)
return _colorMap[(int) Math.Round(amount * 100, MidpointRounding.AwayFromZero)];
return SKColor.Empty;
}
private void GradientColorChanged(object sender, PropertyChangedEventArgs e)
{
CreateColorMap();
}
private void CreateColorMap()
{
SKColor[] colorMap = new SKColor[101];
for (int i = 0; i < 101; i++)
colorMap[i] = Properties.GradientColor.BaseValue.GetColor(i / 100f);
_colorMap = colorMap;
}
}
public enum ColorMappingType
{
Simple,
Gradient
}
}