Use bytes for internal RGBA components of Color, iterate particles sequentially in ParticleEmitters, increase limit for CPU particles, reduce size of Particle struct.

This commit is contained in:
2024-10-16 00:28:39 +02:00
parent 692fdf8ef0
commit 7c7c61fd56
7 changed files with 71 additions and 78 deletions

View File

@@ -34,25 +34,33 @@ namespace Voile
public static Color Green = new(0x00FF00);
public static Color Red = new(0xFF0000);
public float R { get; set; }
public float G { get; set; }
public float B { get; set; }
public float A { get; set; } = 1.0f;
public byte R { get; set; }
public byte G { get; set; }
public byte B { get; set; }
public byte A { get; set; } = 255;
public int Argb
{
get
{
int a = (int)Math.Round(A * 255f) << 24;
int r = (int)Math.Round(R * 255f) << 16;
int g = (int)Math.Round(G * 255f) << 8;
int b = (int)Math.Round(B * 255f);
int a = A << 24;
int r = R << 16;
int g = G << 8;
int b = B;
return a | r | g | b;
}
}
public Color(float r, float g, float b, float a)
public Color(float r, float g, float b, float a = 1.0f)
{
R = (byte)Math.Clamp(r * 255, 0, 255);
G = (byte)Math.Clamp(g * 255, 0, 255);
B = (byte)Math.Clamp(b * 255, 0, 255);
A = (byte)Math.Clamp(a * 255, 0, 255);
}
public Color(byte r, byte g, byte b, byte a = 255)
{
R = r;
G = g;
@@ -60,22 +68,16 @@ namespace Voile
A = a;
}
public Color(byte r, byte g, byte b, byte a)
{
R = r / 255f;
G = g / 255f;
B = b / 255f;
A = a / 255f;
}
public Color(int hex)
{
A = 1.0f;
B = (hex & 0xFF) / 255.0f;
hex >>= 8;
G = (hex & 0xFF) / 255.0f;
hex >>= 8;
R = (hex & 0xFF) / 255.0f;
A = 255; // Default alpha to 255 if not provided
B = (byte)(hex & 0xFF);
G = (byte)((hex >> 8) & 0xFF);
R = (byte)((hex >> 16) & 0xFF);
if (hex > 0xFFFFFF) // If the hex value includes alpha
{
A = (byte)((hex >> 24) & 0xFF);
}
}
public static Color FromHexString(string hex)
@@ -104,20 +106,18 @@ namespace Voile
public Color Lightened(float amount)
{
var result = this;
result.R = result.R + (1.0f - result.R) * amount;
result.G = result.G + (1.0f - result.G) * amount;
result.B = result.B + (1.0f - result.B) * amount;
result.R = (byte)Math.Min(255, R + (255 - R) * amount);
result.G = (byte)Math.Min(255, G + (255 - G) * amount);
result.B = (byte)Math.Min(255, B + (255 - B) * amount);
return result;
}
public Color Darkened(float amount)
{
var result = this;
result.R = result.R * (1.0f - amount);
result.G = result.G * (1.0f - amount);
result.B = result.B * (1.0f - amount);
result.R = (byte)(R * (1.0f - amount));
result.G = (byte)(G * (1.0f - amount));
result.B = (byte)(B * (1.0f - amount));
return result;
}