Files
GladiatorIdle/Mono/Arena/ModelTestScene.cs

151 lines
7.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using GladiatorIdle.Gfx;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using D = GladiatorIdle.Gfx.Draw;
namespace GladiatorIdle.Arena
{
/// <summary>
/// Test: ein Gladiator als 3D-Modell (Mixamo-Bewegungen) vor der Arena. Start mit --model3d.
/// Pfeile/1–9: Bewegung, Leertaste: anhalten, Maus ziehen: drehen, Rad: Abstand,
/// V: Ansicht (vorn / schräg / Duell-Seite), T: Teamfarbe, Esc: beenden.
/// </summary>
internal sealed class ModelTestScene
{
public readonly Model3D? Model;
readonly BasicEffect _fx;
readonly Texture2D? _backdrop;
public int ClipIndex;
public float Time, Yaw, Distance = 520;
public bool Paused;
public int Color = -1;
float _blend = 1;
int _lastX = -1;
int _prevWheel;
static readonly float[] Views = { 0, MathHelper.ToRadians(35), MathHelper.ToRadians(90) };
int _view;
Matrix? _fit; // Modell in Zentimeter, Füße auf dem Boden (aus der ersten Haltung)
public ModelTestScene(GraphicsDevice gd)
{
// --model=<name>: andere Figur ansehen (Standard: hero_murmillo_1)
string name = Array.Find(Environment.GetCommandLineArgs(), x => x.StartsWith("--model="))?[8..] ?? "hero_murmillo_1";
Model = Model3D.Load(gd, name);
Model?.Arm(Array.Find(Environment.GetCommandLineArgs(), x => x.StartsWith("--arm="))?[6..] ?? name); // --arm=<figur>: deren Ausrüstung
if (Model != null) DevShot.Log($"model3d {name}: {Model.BladeLog}");
_backdrop = Assets.Sprite("arena_colosseum", "jpg");
_fx = new BasicEffect(gd) { VertexColorEnabled = true, LightingEnabled = true, PreferPerPixelLighting = true };
_fx.AmbientLightColor = new Vector3(0.42f, 0.38f, 0.36f);
_fx.DirectionalLight0.Enabled = true;
_fx.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(-0.4f, -0.7f, -0.6f));
_fx.DirectionalLight0.DiffuseColor = new Vector3(0.95f, 0.85f, 0.72f);
_fx.DirectionalLight0.SpecularColor = new Vector3(0.25f, 0.22f, 0.2f);
_fx.DirectionalLight1.Enabled = true; // kühles Gegenlicht von hinten links
_fx.DirectionalLight1.Direction = Vector3.Normalize(new Vector3(0.7f, -0.2f, 0.7f));
_fx.DirectionalLight1.DiffuseColor = new Vector3(0.25f, 0.3f, 0.4f);
_fx.DirectionalLight2.Enabled = false;
_fx.SpecularPower = 24;
Yaw = Views[1];
_view = 1;
}
public Model3D.Clip? Clip => Model != null && Model.Clips.Count > 0 ? Model.Clips[ClipIndex % Model.Clips.Count] : null;
public void Play(int index)
{
if (Model == null || Model.Clips.Count == 0) return;
ClipIndex = (index % Model.Clips.Count + Model.Clips.Count) % Model.Clips.Count;
Time = 0;
Model.BeginBlend();
_blend = 0;
}
public void SetView(int v) { _view = v % Views.Length; Yaw = Views[_view]; }
public void Update(float dt, Input input)
{
if (Model == null) return;
if (input.Pressed(Keys.Right)) Play(ClipIndex + 1);
if (input.Pressed(Keys.Left)) Play(ClipIndex - 1);
for (int k = 0; k < 9; k++) if (input.Pressed(Keys.D1 + k)) Play(k);
if (input.Pressed(Keys.Space)) Paused = !Paused;
if (input.Pressed(Keys.V)) SetView(_view + 1);
if (input.Pressed(Keys.T)) Color = Color + 1 >= Appearance.Colors.Length ? -1 : Color + 1;
// drehen mit gedrückter linker Maustaste, Abstand mit dem Rad
if (input.Mouse.LeftButton == ButtonState.Pressed)
{
if (_lastX >= 0) Yaw += (input.Mouse.X - _lastX) * 0.01f;
_lastX = input.Mouse.X;
}
else _lastX = -1;
int wheel = input.Mouse.ScrollWheelValue;
Distance = MathHelper.Clamp(Distance - (wheel - _prevWheel) * 0.3f, 200, 1200);
_prevWheel = wheel;
if (!Paused) Time += dt;
_blend = Math.Min(1, _blend + dt / 0.15f);
Model.SetColor(Color);
}
public void Draw(GraphicsDevice gd, SpriteBatch sb, float scale, Vector2 offset)
{
var vp = gd.Viewport;
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp);
if (_backdrop != null)
{
float k = MathF.Max(vp.Width / (float)_backdrop.Width, vp.Height / (float)_backdrop.Height);
sb.Draw(_backdrop, new Vector2(vp.Width / 2f, vp.Height / 2f), null, new Color(0.55f, 0.52f, 0.5f), 0,
new Vector2(_backdrop.Width / 2f, _backdrop.Height / 2f), k, SpriteEffects.None, 0);
}
sb.End();
if (Model == null || Clip is not Model3D.Clip clip)
{
sb.Begin(transformMatrix: Matrix.CreateScale(scale) * Matrix.CreateTranslation(offset.X, offset.Y, 0));
D.Text(sb, Assets.SansBold, 30, L.t("model3d.missing"), new Vector2(Camera.W / 2, Camera.H / 2), Microsoft.Xna.Framework.Color.White, Align.Center);
sb.End();
return;
}
bool loop = clip.Name is "idle" or "walk" or "run" or "blockidle" or "strafe"; // i18n-ok
Model.Pose(clip, Time, loop, _blend);
var target = new Vector3(0, 95, 0);
var view = Matrix.CreateLookAt(target + new Vector3(0, 25, Distance), target, Vector3.Up);
var proj = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(32), vp.AspectRatio, 10, 5000);
if (_fit == null && _blend >= 1)
{
// robuste Grenzen (einzelne Ausreißer-Punkte verschieben sonst Boden und Größe)
float Q(Func<Vector3, float> axis, float q)
{
var a = Array.ConvertAll(Model.Vertices, v => axis(v.Position));
Array.Sort(a);
return a[(int)(q * (a.Length - 1))];
}
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;
_fit = Matrix.CreateTranslation(-cx, -y0, -cz) * Matrix.CreateScale(180 / MathF.Max(1e-3f, y1 - y0));
DevShot.Log($"model3d fit: unit {Model.Unit} feet {y0:0.0} top {y1:0.0}");
}
var world = (_fit ?? Matrix.Identity) * Matrix.CreateRotationY(Yaw);
// Schatten unter den Füßen
var feet = vp.Project(Vector3.Zero, proj, view, world);
var side = vp.Project(new Vector3(60, 0, 0), proj, view, Matrix.Identity);
float w = MathF.Abs(side.X - vp.Project(Vector3.Zero, proj, view, Matrix.Identity).X) * 2.4f;
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
D.Glow(sb, new Vector2(feet.X, feet.Y), new Vector2(w, w * 0.22f), Microsoft.Xna.Framework.Color.Black * 0.7f);
sb.End();
_fx.World = world; _fx.View = view; _fx.Projection = proj;
Model.Draw(gd, _fx);
sb.Begin(transformMatrix: Matrix.CreateScale(scale) * Matrix.CreateTranslation(offset.X, offset.Y, 0));
string info = $"{ClipIndex + 1}/{Model.Clips.Count} {clip.Name} {Time % MathF.Max(clip.Length, 0.01f):0.00}/{clip.Length:0.00} s"; // i18n-ok
D.Text(sb, Assets.SansBold, 26, info, new Vector2(40, 40), D.Gold);
D.Text(sb, Assets.Sans, 18, L.t("model3d.help"), new Vector2(40, 80), D.Sand);
sb.End();
}
}
}