mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Optimized noise layer
This commit is contained in:
parent
f91ea0f992
commit
c99e549101
@ -159,6 +159,7 @@
|
|||||||
<Compile Include="Exceptions\ArtemisCoreException.cs" />
|
<Compile Include="Exceptions\ArtemisCoreException.cs" />
|
||||||
<Compile Include="Extensions\DirectoryInfoExtensions.cs" />
|
<Compile Include="Extensions\DirectoryInfoExtensions.cs" />
|
||||||
<Compile Include="Extensions\DoubleExtensions.cs" />
|
<Compile Include="Extensions\DoubleExtensions.cs" />
|
||||||
|
<Compile Include="Extensions\FloatExtensions.cs" />
|
||||||
<Compile Include="Extensions\RgbColorExtensions.cs" />
|
<Compile Include="Extensions\RgbColorExtensions.cs" />
|
||||||
<Compile Include="Extensions\RgbDeviceExtensions.cs" />
|
<Compile Include="Extensions\RgbDeviceExtensions.cs" />
|
||||||
<Compile Include="Extensions\RgbRectangleExtensions.cs" />
|
<Compile Include="Extensions\RgbRectangleExtensions.cs" />
|
||||||
|
|||||||
12
src/Artemis.Core/Extensions/FloatExtensions.cs
Normal file
12
src/Artemis.Core/Extensions/FloatExtensions.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Artemis.Core.Extensions
|
||||||
|
{
|
||||||
|
public static class FloatExtensions
|
||||||
|
{
|
||||||
|
public static int RoundToInt(this float number)
|
||||||
|
{
|
||||||
|
return (int)Math.Round(number, MidpointRounding.AwayFromZero);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -7,16 +7,16 @@ namespace Artemis.Plugins.LayerElements.Noise
|
|||||||
{
|
{
|
||||||
public class NoiseLayerElement : LayerElement
|
public class NoiseLayerElement : LayerElement
|
||||||
{
|
{
|
||||||
private static Random _rand = new Random();
|
private const int Scale = 6;
|
||||||
|
private static readonly Random Rand = new Random();
|
||||||
private readonly OpenSimplexNoise _noise;
|
private readonly OpenSimplexNoise _noise;
|
||||||
private float _z;
|
private float _z;
|
||||||
private const int Scale = 6;
|
|
||||||
|
|
||||||
public NoiseLayerElement(Layer layer, Guid guid, NoiseLayerElementSettings settings, LayerElementDescriptor descriptor) : base(layer, guid, settings, descriptor)
|
public NoiseLayerElement(Layer layer, Guid guid, NoiseLayerElementSettings settings, LayerElementDescriptor descriptor) : base(layer, guid, settings, descriptor)
|
||||||
{
|
{
|
||||||
Settings = settings;
|
Settings = settings;
|
||||||
|
|
||||||
_z = _rand.Next(0, 4096);
|
_z = Rand.Next(0, 4096);
|
||||||
_noise = new OpenSimplexNoise(Guid.GetHashCode());
|
_noise = new OpenSimplexNoise(Guid.GetHashCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,22 +41,27 @@ namespace Artemis.Plugins.LayerElements.Noise
|
|||||||
|
|
||||||
public override void Render(SKPath framePath, SKCanvas canvas)
|
public override void Render(SKPath framePath, SKCanvas canvas)
|
||||||
{
|
{
|
||||||
// Scale down the render path
|
// Scale down the render path to avoid computing a value for every pixel
|
||||||
var width = (int) Math.Round(Math.Max(Layer.RenderRectangle.Width, Layer.RenderRectangle.Height) / Scale);
|
var width = (int) (Math.Max(Layer.RenderRectangle.Width, Layer.RenderRectangle.Height) / Scale);
|
||||||
var height = (int) Math.Round(Math.Max(Layer.RenderRectangle.Width, Layer.RenderRectangle.Height) / Scale);
|
var height = (int) (Math.Max(Layer.RenderRectangle.Width, Layer.RenderRectangle.Height) / Scale);
|
||||||
|
|
||||||
using (var bitmap = new SKBitmap(new SKImageInfo(width, height)))
|
using (var bitmap = new SKBitmap(new SKImageInfo(width, height)))
|
||||||
{
|
{
|
||||||
for (var x = 0; x < width; x++)
|
// Only compute pixels inside LEDs, due to scaling there may be some rounding issues but it's neglect-able
|
||||||
|
foreach (var artemisLed in Layer.Leds)
|
||||||
{
|
{
|
||||||
for (var y = 0; y < height; y++)
|
var xStart = artemisLed.AbsoluteRenderRectangle.Left / Scale;
|
||||||
{
|
var xEnd = artemisLed.AbsoluteRenderRectangle.Right / Scale;
|
||||||
// This check is actually more expensive then _noise.Evaluate() ^.^'
|
var yStart = artemisLed.AbsoluteRenderRectangle.Top / Scale;
|
||||||
// if (!framePath.Contains(x * Scale, y * Scale))
|
var yEnd = artemisLed.AbsoluteRenderRectangle.Bottom / Scale;
|
||||||
// continue;
|
|
||||||
|
|
||||||
var v = _noise.Evaluate(Settings.XScale * x / width, Settings.YScale * y / height, _z);
|
for (var x = xStart; x < xEnd; x++)
|
||||||
bitmap.SetPixel(x, y, new SKColor(0, 0, 0, (byte) ((v + 1) * 127)));
|
{
|
||||||
|
for (var y = yStart; y < yEnd; y++)
|
||||||
|
{
|
||||||
|
var v = _noise.Evaluate(Settings.XScale * x / width, Settings.YScale * y / height, _z);
|
||||||
|
bitmap.SetPixel((int) x, (int) y, new SKColor(0, 0, 0, (byte) ((v + 1) * 127)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user