Eigene Kampfstile je Gegner (Faustkampf, Axt-Champions), schnelleres Laden

Stand von main 975b62f0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 14:20:38 +01:00
parent e29db47b20
commit 35a5ac5032
10 changed files with 225 additions and 30 deletions

View File

@@ -116,28 +116,46 @@ namespace GladiatorIdle.Gfx
}
/// Textur aus Pixeln mit Mipmap-Stufen (2×2-Mittel je Stufe).
public static Texture2D MipTexture(Color[] data, int w, int h)
public static Texture2D MipTexture(Color[] data, int w, int h) => Upload(MipLevels(data, w, h), w, h);
/// Mipmap-Stufen nur aus Pixeln (ohne Grafikkarte, also auch im Hintergrund nutzbar).
public static Color[][] MipLevels(Color[] data, int w, int h)
{
var t = new Texture2D(_gd, w, h, true, SurfaceFormat.Color);
t.SetData(0, null, data, 0, data.Length);
var levels = new List<Color[]> { data };
var cur = data; int cw = w, ch = h;
for (int level = 1; level < t.LevelCount; level++)
while (cw > 1 || ch > 1)
{
int nw = Math.Max(1, cw / 2), nh = Math.Max(1, ch / 2);
var next = new Color[nw * nh];
for (int y = 0; y < nh; y++)
var src = cur; int sw = cw, sh = ch;
System.Threading.Tasks.Parallel.For(0, nh, y =>
{
for (int x = 0; x < nw; x++)
{
int x0 = Math.Min(cw - 1, x * 2), x1 = Math.Min(cw - 1, x * 2 + 1), y0 = Math.Min(ch - 1, y * 2), y1 = Math.Min(ch - 1, y * 2 + 1);
var a = cur[y0 * cw + x0]; var b = cur[y0 * cw + x1]; var c = cur[y1 * cw + x0]; var d = cur[y1 * cw + x1];
int x0 = Math.Min(sw - 1, x * 2), x1 = Math.Min(sw - 1, x * 2 + 1), y0 = Math.Min(sh - 1, y * 2), y1 = Math.Min(sh - 1, y * 2 + 1);
var a = src[y0 * sw + x0]; var b = src[y0 * sw + x1]; var c = src[y1 * sw + x0]; var d = src[y1 * sw + x1];
next[y * nw + x] = new Color((a.R + b.R + c.R + d.R + 2) / 4, (a.G + b.G + c.G + d.G + 2) / 4, (a.B + b.B + c.B + d.B + 2) / 4, (a.A + b.A + c.A + d.A + 2) / 4);
}
t.SetData(level, null, next, 0, next.Length);
});
levels.Add(next);
cur = next; cw = nw; ch = nh;
}
return levels.ToArray();
}
/// Fertige Stufen zur Grafikkarte (Hauptthread, schnell).
public static Texture2D Upload(Color[][] levels, int w, int h)
{
var t = new Texture2D(_gd, w, h, true, SurfaceFormat.Color);
for (int i = 0; i < Math.Min(levels.Length, t.LevelCount); i++) t.SetData(i, null, levels[i], 0, levels[i].Length);
return t;
}
/// Pixel einer Textur schon bekannt (Figuren): Einfärben braucht dann kein Zurücklesen von der Grafikkarte.
public static void RegisterPixels(Texture2D t, Color[] data) { lock (_known) _known[t] = data; }
public static Color[]? KnownPixels(Texture2D t) { lock (_known) return _known.TryGetValue(t, out var d) ? d : null; }
static readonly Dictionary<Texture2D, Color[]> _known = new();
/// Bild eines Video-Sprites: Farbe vor Schwarz (JPEG, also schon vormultipliziert)
/// plus Deckkraft (Graustufen-PNG) — zusammengesetzt zu einer Textur.
public static Texture2D? LoadFrame(string relBase)
@@ -194,15 +212,78 @@ namespace GladiatorIdle.Gfx
public static Texture2D Paint(Texture2D src, int colorIndex)
{
if (_painted.TryGetValue((src, colorIndex), out var t)) return t;
var data = new Color[src.Width * src.Height];
src.GetData(data);
PaintPixels(data, colorIndex);
var sw = System.Diagnostics.Stopwatch.StartNew();
var info = PaintInfoOf(src);
if (info.Index.Length == 0) return _painted[(src, colorIndex)] = src; // keine Teamfarben-Flächen
var data = (Color[])info.Data.Clone();
var c = Appearance.Colors[colorIndex];
System.Threading.Tasks.Parallel.For(0, (info.Index.Length + 8191) / 8192, chunk =>
{
int end = Math.Min(info.Index.Length, (chunk + 1) * 8192);
for (int k = chunk * 8192; k < end; k++)
{
int i = info.Index[k];
var px = data[i];
float a = px.A / 255f, r = px.R / 255f / a, g = px.G / 255f / a, b = px.B / 255f / a;
float max = info.Max[k], s = info.Sat[k], w = info.Weight[k];
var (nr, ng, nb) = Hsv(c.Hue, MathHelper.Clamp(s * c.Sat, 0, 1), MathHelper.Clamp(max * c.Val, 0, 1));
r += (nr - r) * w; g += (ng - g) * w; b += (nb - b) * w;
data[i] = new Color(r * a, g * a, b * a, a);
}
});
if (src.LevelCount > 1) t = MipTexture(data, src.Width, src.Height); // Figuren: Stufen behalten
else { t = new Texture2D(_gd, src.Width, src.Height); t.SetData(data); }
_painted[(src, colorIndex)] = t;
if (DevShot.Active && src.Width > 512) DevShot.Log($"timing: paint {src.Width}x{src.Height} color {colorIndex} in {sw.ElapsedMilliseconds} ms ({info.Index.Length} px)");
return t;
}
/// Je Quelle einmal: Pixel und welche davon Teamfarbe (Magenta) sind, mit Sättigung, Helligkeit, Gewicht.
public sealed record PaintInfo(Color[] Data, int[] Index, float[] Sat, float[] Max, float[] Weight);
/// Teamfarben-Daten vorab (auch im Hintergrund) und für eine Textur hinterlegen.
public static void RegisterPaintInfo(Texture2D t, PaintInfo info) { lock (_known) _paintInfo[t] = info; }
static readonly Dictionary<Texture2D, PaintInfo> _paintInfo = new();
static PaintInfo PaintInfoOf(Texture2D src)
{
lock (_known) if (_paintInfo.TryGetValue(src, out var info)) return info;
Color[]? known;
lock (_known) _known.TryGetValue(src, out known);
var data = known ?? new Color[src.Width * src.Height];
if (known == null) src.GetData(data);
var made = ComputePaintInfo(data);
lock (_known) _paintInfo[src] = made;
return made;
}
/// Welche Pixel Teamfarbe (Magenta) sind – reine Rechnung, auch im Hintergrund.
public static PaintInfo ComputePaintInfo(Color[] data)
{
// Gewicht je Pixel parallel, danach nur die Teamfarben-Pixel einsammeln
var wAll = new float[data.Length]; var sAll = new float[data.Length]; var mAll = new float[data.Length];
System.Threading.Tasks.Parallel.For(0, (data.Length + 16383) / 16384, chunk =>
{
for (int i = chunk * 16384, end = Math.Min(data.Length, (chunk + 1) * 16384); i < end; i++)
{
var px = data[i];
if (px.A == 0) continue;
float a = px.A / 255f, r = px.R / 255f / a, g = px.G / 255f / a, b = px.B / 255f / a;
float max = MathF.Max(r, MathF.Max(g, b)), min = MathF.Min(r, MathF.Min(g, b)), d = max - min;
if (max <= 0.08f || d <= 0.001f) continue;
float s = d / max;
float h = max == r ? 60 * (((g - b) / d) % 6) : max == g ? 60 * ((b - r) / d + 2) : 60 * ((r - g) / d + 4);
if (h < 0) h += 360;
float w = Smooth(48, 30, MathF.Abs(h - 312)) * Smooth(0.18f, 0.32f, s);
if (w > 0) { wAll[i] = w; sAll[i] = s; mAll[i] = max; }
}
});
var idx = new List<int>(); var sat = new List<float>(); var mx = new List<float>(); var wt = new List<float>();
for (int i = 0; i < data.Length; i++)
if (wAll[i] > 0) { idx.Add(i); sat.Add(sAll[i]); mx.Add(mAll[i]); wt.Add(wAll[i]); }
return new PaintInfo(data, idx.ToArray(), sat.ToArray(), mx.ToArray(), wt.ToArray());
}
/// Umfärben auf Pixeldaten (vormultipliziert) — auch im Hintergrund nutzbar.
public static void PaintPixels(Color[] data, int colorIndex)
{