Files
GladiatorIdle/Mono/Gfx/Mesh3D.cs
2026-11-09 16:13:28 +01:00

63 lines
3.1 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 System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace GladiatorIdle.Gfx
{
/// <summary>
/// Einfaches Gitter für Kulissen (Arena, Reise): Vierecke, Quader, Zylinder mit Farbe und
/// Textur-Koordinaten; einmal gebaut (<see cref="Freeze"/>) und mit BasicEffect gezeichnet.
/// </summary>
internal sealed class Mesh3D
{
public readonly List<VertexPNCT> V = new();
public readonly List<int> I = new();
public Texture2D? Tex;
public bool Lit = true, Wrap, Mirror, Fog = true;
public Vector3 Tint = Vector3.One;
public VertexPNCT[]? VA; public int[]? IA;
public void Quad(Vector3 a, Vector3 b, Vector3 c, Vector3 d, Vector3 n, Color col,
Vector2 ta, Vector2 tb, Vector2 tc, Vector2 td)
{
int i = V.Count;
V.Add(new VertexPNCT { Position = a, Normal = n, Color = col, Uv = ta });
V.Add(new VertexPNCT { Position = b, Normal = n, Color = col, Uv = tb });
V.Add(new VertexPNCT { Position = c, Normal = n, Color = col, Uv = tc });
V.Add(new VertexPNCT { Position = d, Normal = n, Color = col, Uv = td });
I.AddRange(new[] { i, i + 1, i + 2, i, i + 2, i + 3 });
}
/// Quader (achsengerade), Mitte c, Größe s.
public void Box(Vector3 c, Vector3 s, Color col)
{
var h = s / 2;
Vector3 P(float x, float y, float z) => c + new Vector3(x * h.X, y * h.Y, z * h.Z);
var z0 = Vector2.Zero;
Quad(P(-1, -1, 1), P(1, -1, 1), P(1, 1, 1), P(-1, 1, 1), Vector3.UnitZ, col, z0, z0, z0, z0);
Quad(P(1, -1, -1), P(-1, -1, -1), P(-1, 1, -1), P(1, 1, -1), -Vector3.UnitZ, col, z0, z0, z0, z0);
Quad(P(-1, -1, -1), P(-1, -1, 1), P(-1, 1, 1), P(-1, 1, -1), -Vector3.UnitX, col, z0, z0, z0, z0);
Quad(P(1, -1, 1), P(1, -1, -1), P(1, 1, -1), P(1, 1, 1), Vector3.UnitX, col, z0, z0, z0, z0);
Quad(P(-1, -1, -1), P(1, -1, -1), P(1, -1, 1), P(-1, -1, 1), -Vector3.UnitY, col, z0, z0, z0, z0);
}
/// Senkrechter Zylinder (Becken, Pfosten): Mitte unten c, Radius r, Höhe h (nach oben = −y).
public void Cylinder(Vector3 c, float r, float h, Color col, int seg = 12, float rTop = -1)
{
if (rTop < 0) rTop = r;
var z0 = Vector2.Zero;
for (int i = 0; i < seg; i++)
{
float a0 = MathF.Tau * i / seg, a1 = MathF.Tau * (i + 1) / seg;
Vector3 Pt(float a, float rad, float y) => c + new Vector3(MathF.Cos(a) * rad, y, MathF.Sin(a) * rad);
var n = new Vector3(MathF.Cos((a0 + a1) / 2), 0, MathF.Sin((a0 + a1) / 2));
Quad(Pt(a0, r, 0), Pt(a1, r, 0), Pt(a1, rTop, -h), Pt(a0, rTop, -h), n, col, z0, z0, z0, z0);
Quad(Pt(a0, rTop, -h), Pt(a1, rTop, -h), c + new Vector3(0, -h, 0), c + new Vector3(0, -h, 0), -Vector3.UnitY, col, z0, z0, z0, z0);
}
}
public void Freeze() { VA = V.ToArray(); IA = I.ToArray(); }
}
}