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

@@ -71,17 +71,56 @@ namespace GladiatorIdle.Gfx
// Vorlagen je Datei: einmal gelesen, danach teilt jede Figur Gitter, Gewichte, Clips und Textur
// und hat nur eigene Haltung, eigene Puffer und eigene Waffen (Duellstart ohne Plattenzugriff).
static readonly Dictionary<string, Model3D?> _templates = new();
// im Hintergrund vorbereitete Vorlagen (Datei gelesen, Textur entpackt, Stufen gerechnet)
static readonly System.Collections.Concurrent.ConcurrentDictionary<string, Model3D> _parsed = new();
static string? PathOf(string name) => Assets.RootDir is string root ? Path.Combine(root, "Models", name + ".gim") : null;
/// Beim Start: alle Figuren im Hintergrund vorbereiten, damit später nichts ruckelt.
public static void PreloadAll()
{
if (Assets.RootDir is not string root || !Directory.Exists(Path.Combine(root, "Models"))) return;
var names = Directory.GetFiles(Path.Combine(root, "Models"), "*.gim").Select(f => Path.GetFileNameWithoutExtension(f)!).ToArray();
System.Threading.Tasks.Task.Run(() =>
{
foreach (var n in names)
{
if (_parsed.ContainsKey(n)) continue;
try { _parsed[n] = new Model3D(PathOf(n)!); } catch { /* beim Bedarf noch einmal auf dem Hauptthread */ }
}
});
}
public static Model3D? Load(GraphicsDevice gd, string name)
{
if (!_templates.TryGetValue(name, out var t))
{
string? path = Assets.RootDir is string root ? Path.Combine(root, "Models", name + ".gim") : null;
_templates[name] = t = path != null && File.Exists(path) ? new Model3D(gd, path) : null;
var sw = System.Diagnostics.Stopwatch.StartNew();
string? path = PathOf(name);
bool pre = _parsed.TryRemove(name, out t);
if (!pre) t = path != null && File.Exists(path) ? new Model3D(path) : null;
t?.EnsureTexture();
_templates[name] = t;
if (DevShot.Active) DevShot.Log($"timing: model {name} {(pre ? "from preload" : "loaded")} in {sw.ElapsedMilliseconds} ms");
}
return t == null ? null : new Model3D(t);
}
Color[][]? _texLevels; int _texW, _texH;
Assets.PaintInfo? _texInfo;
// vorgerechnete Hauttöne (Stufen + Teamfarben-Daten), geteilt mit allen Figuren dieser Vorlage
Dictionary<float, (Color[][] Levels, Assets.PaintInfo Info)> _skinPre = new();
/// Textur aus den vorbereiteten Stufen hochladen (Hauptthread).
void EnsureTexture()
{
if (_texture != null || _texLevels == null) return;
_texture = Assets.Upload(_texLevels, _texW, _texH);
Assets.RegisterPixels(_texture, _texLevels[0]);
if (_texInfo != null) Assets.RegisterPaintInfo(_texture, _texInfo);
_texLevels = null;
}
/// Neue Figur auf den geteilten Daten einer Vorlage.
Model3D(Model3D src)
{
@@ -92,12 +131,13 @@ namespace GladiatorIdle.Gfx
Vertices = (VertexPNCT[])src.Vertices.Clone();
Bounds = src.Bounds;
_texture = src._texture;
_skinPre = src._skinPre;
Unit = src.Unit;
_pose = (float[])_rest.Clone(); _from = (float[])_rest.Clone();
_global = new Matrix[_names.Length]; _skin = new Matrix[_names.Length];
}
Model3D(GraphicsDevice gd, string path)
Model3D(string path)
{
using var r = new BinaryReader(File.OpenRead(path), Encoding.UTF8);
string magic = Encoding.ASCII.GetString(r.ReadBytes(4));
@@ -137,7 +177,22 @@ namespace GladiatorIdle.Gfx
Clips.Add(clip);
}
if (v2 && r.ReadInt32() is int texLen && texLen > 0)
using (var ms = new MemoryStream(r.ReadBytes(texLen))) _texture = Assets.Mipmapped(Texture2D.FromStream(gd, ms)); // Stufen: kein Flimmern klein/weit weg
{
// PNG ohne Grafikkarte entpacken (auch im Hintergrund), Stufen gegen Flimmern klein/weit weg
var img = StbImageSharp.ImageResult.FromMemory(r.ReadBytes(texLen), StbImageSharp.ColorComponents.RedGreenBlueAlpha);
var px = new Color[img.Width * img.Height];
for (int i = 0; i < px.Length; i++) px[i] = new Color(img.Data[i * 4], img.Data[i * 4 + 1], img.Data[i * 4 + 2], img.Data[i * 4 + 3]);
_texW = img.Width; _texH = img.Height;
_texLevels = Assets.MipLevels(px, _texW, _texH);
_texInfo = Assets.ComputePaintInfo(px);
// Vorlagen 2/3 der Gladiatoren nutzen diese Figur mit dunkler bzw. heller Haut: schon jetzt rechnen
if (Path.GetFileName(path).StartsWith("hero_"))
foreach (float dark in new[] { 1f, -1f })
{
var skin = SkinPixels(px, dark);
_skinPre[dark] = (Assets.MipLevels(skin, _texW, _texH), Assets.ComputePaintInfo(skin));
}
}
Vertices = new VertexPNCT[nv];
for (int i = 0; i < nv; i++) Vertices[i] = new VertexPNCT { Position = _pos[i], Normal = _nrm[i], Color = _baseCol[i], Uv = uv[i] };
Bounds = BoundingBox.CreateFromPoints(_pos);
@@ -157,10 +212,32 @@ namespace GladiatorIdle.Gfx
public void SetSkin(float dark)
{
if (_texture == null || dark == 0) return;
if (_skins.TryGetValue((_texture, dark), out var done)) { _texture = done; _color = -2; return; }
if (_skinPre.TryGetValue(dark, out var pre))
{
var tex = Assets.Upload(pre.Levels, _texture.Width, _texture.Height);
Assets.RegisterPixels(tex, pre.Levels[0]);
Assets.RegisterPaintInfo(tex, pre.Info);
_skins[(_texture, dark)] = tex;
_texture = tex; _color = -2;
return;
}
var original = _texture; float key = dark;
var data = SkinPixels(Assets.KnownPixels(_texture) ?? GetPixels(_texture), dark);
_texture = Assets.MipTexture(data, _texture.Width, _texture.Height);
Assets.RegisterPixels(_texture, data);
_skins[(original, key)] = _texture;
_color = -2; // Teamfarbe neu auf die geänderte Textur
}
/// Hautton umrechnen (reine Pixel-Rechnung, auch im Hintergrund).
static Color[] SkinPixels(Color[] src, float dark)
{
bool pale = dark < 0; dark = MathF.Abs(dark); // negativ: hellere, blassere Haut
var data = new Color[_texture.Width * _texture.Height];
_texture.GetData(data);
for (int i = 0; i < data.Length; i++)
var data = (Color[])src.Clone();
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 c = data[i];
float r = c.R / 255f, g = c.G / 255f, b = c.B / 255f;
@@ -181,10 +258,13 @@ namespace GladiatorIdle.Gfx
nr = gray + (nr - gray) * boost; ng = gray + (ng - gray) * boost; nb = gray + (nb - gray) * boost;
data[i] = new Color(Math.Clamp(nr, 0, 1), Math.Clamp(ng, 0, 1), Math.Clamp(nb, 0, 1), c.A / 255f);
}
_texture = Assets.MipTexture(data, _texture.Width, _texture.Height);
_color = -2; // Teamfarbe neu auf die geänderte Textur
});
return data;
}
static readonly Dictionary<(Texture2D, float), Texture2D> _skins = new();
static Color[] GetPixels(Texture2D t) { var d = new Color[t.Width * t.Height]; t.GetData(d); return d; }
/// 0 unterhalb a, 1 zwischen b und c, 0 oberhalb d, linear dazwischen.
static float Band(float x, float a, float b, float c, float d) =>
x <= a || x >= d ? 0 : x < b ? (x - a) / (b - a) : x > c ? (d - x) / (d - c) : 1;
@@ -284,14 +364,18 @@ namespace GladiatorIdle.Gfx
/// Figur auf 180 Einheiten Höhe, Füße auf y = 0, mittig (aus der jetzigen Haltung, robust gegen Ausreißer).
public Matrix FitNow()
{
float Q(Func<Vector3, float> axis, float q)
// je Achse einmal sortieren (jeder 3. Punkt reicht für die Quantile)
float[] Sorted(Func<Vector3, float> axis)
{
var a = Array.ConvertAll(Vertices, v => axis(v.Position));
var a = new float[(Vertices.Length + 2) / 3];
for (int i = 0; i < a.Length; i++) a[i] = axis(Vertices[i * 3].Position);
Array.Sort(a);
return a[(int)(q * (a.Length - 1))];
return a;
}
float y0 = Q(p => p.Y, 0.002f), y1 = Q(p => p.Y, 0.998f);
float cx = (Q(p => p.X, 0.01f) + Q(p => p.X, 0.99f)) / 2, cz = (Q(p => p.Z, 0.01f) + Q(p => p.Z, 0.99f)) / 2;
static float Q(float[] a, float q) => a[(int)(q * (a.Length - 1))];
var ys = Sorted(p => p.Y); var xs = Sorted(p => p.X); var zs = Sorted(p => p.Z);
float y0 = Q(ys, 0.002f), y1 = Q(ys, 0.998f);
float cx = (Q(xs, 0.01f) + Q(xs, 0.99f)) / 2, cz = (Q(zs, 0.01f) + Q(zs, 0.99f)) / 2;
return Matrix.CreateTranslation(-cx, -y0, -cz) * Matrix.CreateScale(180 / MathF.Max(1e-3f, y1 - y0));
}
@@ -442,7 +526,8 @@ namespace GladiatorIdle.Gfx
else if (Is("recruit")) { AddSword(kind: "rudis"); AddShield(); }
else if (Is("slave")) AddSword(kind: "club");
else if (Is("dimachaerus")) { AddSword(); AddSword("Left"); }
else if (Is("brawler", "sagittarius", "essedarius", "laquearius", "doctore")) { AddSword(); AddShield(kind: "round"); }
else if (Is("brawler")) { AddCestus(); AddCestus("Left"); } // Faustkämpfer
else if (Is("sagittarius", "essedarius", "laquearius", "doctore")) { AddSword(); AddShield(kind: "round"); }
else { AddSword(); AddShield(); } // Murmillo, Secutor, Provocator, Prätorianer, Crupellarius, Champions
AimBlades();
}
@@ -576,6 +661,19 @@ namespace GladiatorIdle.Gfx
Props.Add(p);
}
/// Cestus: Lederriemen mit Bronzebeschlag um die Faust (Faustkämpfer).
public void AddCestus(string side = "Right")
{
var b = new MeshBuilder();
var leather = new Color(96, 62, 36); var bronze = new Color(176, 136, 62);
b.Box(new Vector3(0, 0, 0), new Vector3(9, 7, 9), leather); // Riemen um die Faust
b.Box(new Vector3(4, 0, 0), new Vector3(3, 7.6f, 9.6f), bronze); // Beschlag über den Knöcheln
b.Box(new Vector3(-9, 0, 0), new Vector3(10, 6, 6), leather); // Riemen am Handgelenk
var p = b.Build(Bone(side + "Hand"), Grip(side));
p.NoAvoid = true;
Props.Add(p);
}
/// Gladius in der Faust (Knochen-Präfix "Right"/"Left"). Klinge zeigt vom kleinen Finger zum Zeigefinger.
public void AddSword(string side = "Right", string kind = "gladius")
{