270 lines
8.4 KiB
C#
270 lines
8.4 KiB
C#
using System;
|
|
using Godot;
|
|
using Ymir.Box3D;
|
|
|
|
namespace RustToDust.Client.Tests;
|
|
|
|
/// <summary>
|
|
/// Shared test track for <see cref="WalkBakeoff"/>:
|
|
/// 20m flat approach (-14m..+6m, spawning at Z=0) -> 14 deg ramp (+4.5m rise over 18.05m)
|
|
/// -> flat plateau (24.5m..30.5m) -> gentle -0.38m dip & +0.58m step (30.5m..35.5m) -> upper plateau (to 64m).
|
|
/// Registers an exact <see cref="Box3DSoaWorld.AddStaticHeightField"/> and builds a matching visual mesh.
|
|
/// </summary>
|
|
public sealed class WalkBakeoffTrack
|
|
{
|
|
public const float MinX = -16f;
|
|
public const float MaxX = 16f;
|
|
public const float MinZ = -16f;
|
|
public const float MaxZ = 64f;
|
|
public const float GridStep = 0.50f;
|
|
public const float TrackFriction = 0.85f;
|
|
|
|
public const float LeftLaneX = -5.0f;
|
|
public const float RightLaneX = 5.0f;
|
|
|
|
private static readonly float RampSlope = MathF.Tan(14f * MathF.PI / 180f); // ~0.249328
|
|
private const float RampRise = 4.50f;
|
|
private const float RampZStart = 6.0f;
|
|
private static readonly float RampRun = RampRise / RampSlope; // ~18.0485m
|
|
private static readonly float RampZEnd = RampZStart + RampRun; // ~24.0485m
|
|
private const float RampFillet = 0.60f;
|
|
|
|
private const float DipStartZ = 30.5f;
|
|
private const float DipBottomZ = 33.0f;
|
|
private const float DipEndZ = 35.5f;
|
|
private const float DipHeight = 4.12f;
|
|
private const float UpperPlateauHeight = 4.70f;
|
|
|
|
public int CountX { get; }
|
|
public int CountZ { get; }
|
|
public float MinHeight { get; }
|
|
public float MaxHeight { get; }
|
|
public float[] Heights { get; }
|
|
|
|
public WalkBakeoffTrack()
|
|
{
|
|
CountX = (int)MathF.Round((MaxX - MinX) / GridStep) + 1;
|
|
CountZ = (int)MathF.Round((MaxZ - MinZ) / GridStep) + 1;
|
|
Heights = new float[CountX * CountZ];
|
|
|
|
float minH = float.PositiveInfinity;
|
|
float maxH = float.NegativeInfinity;
|
|
for (int gz = 0; gz < CountZ; gz++)
|
|
{
|
|
float wz = MinZ + gz * GridStep;
|
|
for (int gx = 0; gx < CountX; gx++)
|
|
{
|
|
float wx = MinX + gx * GridStep;
|
|
float h = EvalHeight(wx, wz);
|
|
Heights[gz * CountX + gx] = h;
|
|
if (h < minH) minH = h;
|
|
if (h > maxH) maxH = h;
|
|
}
|
|
}
|
|
|
|
MinHeight = minH;
|
|
MaxHeight = maxH;
|
|
}
|
|
|
|
public static float EvalHeight(float x, float z)
|
|
{
|
|
_ = x;
|
|
float zA = RampZStart - RampFillet;
|
|
float zB = RampZStart + RampFillet;
|
|
float zC = RampZEnd - RampFillet;
|
|
float zD = RampZEnd + RampFillet;
|
|
|
|
if (z <= zA)
|
|
{
|
|
return 0f;
|
|
}
|
|
if (z < zB)
|
|
{
|
|
float d = z - zA;
|
|
return RampSlope * d * d / (4f * RampFillet);
|
|
}
|
|
if (z <= zC)
|
|
{
|
|
return RampSlope * (z - RampZStart);
|
|
}
|
|
if (z < zD)
|
|
{
|
|
float d = zD - z;
|
|
return RampRise - RampSlope * d * d / (4f * RampFillet);
|
|
}
|
|
if (z <= DipStartZ)
|
|
{
|
|
return RampRise;
|
|
}
|
|
if (z < DipBottomZ)
|
|
{
|
|
float t = (z - DipStartZ) / (DipBottomZ - DipStartZ);
|
|
float s = 0.5f - 0.5f * MathF.Cos(MathF.PI * t);
|
|
return RampRise + (DipHeight - RampRise) * s;
|
|
}
|
|
if (z < DipEndZ)
|
|
{
|
|
float t = (z - DipBottomZ) / (DipEndZ - DipBottomZ);
|
|
float s = 0.5f - 0.5f * MathF.Cos(MathF.PI * t);
|
|
return DipHeight + (UpperPlateauHeight - DipHeight) * s;
|
|
}
|
|
return UpperPlateauHeight;
|
|
}
|
|
|
|
/// <summary>Bilinear interpolation matching the static heightfield triangles.</summary>
|
|
public float SampleHeight(float x, float z)
|
|
{
|
|
float fx = Math.Clamp((x - MinX) / GridStep, 0f, CountX - 1.0001f);
|
|
float fz = Math.Clamp((z - MinZ) / GridStep, 0f, CountZ - 1.0001f);
|
|
int ix = (int)fx;
|
|
int iz = (int)fz;
|
|
float tx = fx - ix;
|
|
float tz = fz - iz;
|
|
float h00 = Heights[iz * CountX + ix];
|
|
float h10 = Heights[iz * CountX + ix + 1];
|
|
float h01 = Heights[(iz + 1) * CountX + ix];
|
|
float h11 = Heights[(iz + 1) * CountX + ix + 1];
|
|
float hx0 = h00 + (h10 - h00) * tx;
|
|
float hx1 = h01 + (h11 - h01) * tx;
|
|
return hx0 + (hx1 - hx0) * tz;
|
|
}
|
|
|
|
public Vector3 SampleNormal(float x, float z)
|
|
{
|
|
const float eps = 0.25f;
|
|
float hx0 = SampleHeight(x - eps, z);
|
|
float hx1 = SampleHeight(x + eps, z);
|
|
float hz0 = SampleHeight(x, z - eps);
|
|
float hz1 = SampleHeight(x, z + eps);
|
|
return new Vector3(hx0 - hx1, 2f * eps, hz0 - hz1).Normalized();
|
|
}
|
|
|
|
public float SampleSlopeZ(float x, float z)
|
|
{
|
|
const float eps = 0.20f;
|
|
return (SampleHeight(x, z + eps) - SampleHeight(x, z - eps)) / (2f * eps);
|
|
}
|
|
|
|
public Box3DSoaBodyHandle AddToWorld(Box3DSoaWorld world, ulong stableId = 1)
|
|
{
|
|
return world.AddStaticHeightField(
|
|
stableId,
|
|
(MinX, 0f, MinZ),
|
|
Heights,
|
|
CountX,
|
|
CountZ,
|
|
(GridStep, 1f, GridStep),
|
|
MinHeight,
|
|
MaxHeight,
|
|
friction: TrackFriction,
|
|
restitution: 0.02f);
|
|
}
|
|
|
|
public MeshInstance3D BuildVisualMesh()
|
|
{
|
|
var st = new SurfaceTool();
|
|
st.Begin(Mesh.PrimitiveType.Triangles);
|
|
|
|
for (int gz = 0; gz < CountZ - 1; gz++)
|
|
{
|
|
float z0 = MinZ + gz * GridStep;
|
|
float z1 = z0 + GridStep;
|
|
for (int gx = 0; gx < CountX - 1; gx++)
|
|
{
|
|
float x0 = MinX + gx * GridStep;
|
|
float x1 = x0 + GridStep;
|
|
|
|
float y00 = Heights[gz * CountX + gx];
|
|
float y10 = Heights[gz * CountX + gx + 1];
|
|
float y01 = Heights[(gz + 1) * CountX + gx];
|
|
float y11 = Heights[(gz + 1) * CountX + gx + 1];
|
|
|
|
AddTriVertex(st, x0, y00, z0);
|
|
AddTriVertex(st, x1, y10, z0);
|
|
AddTriVertex(st, x1, y11, z1);
|
|
|
|
AddTriVertex(st, x0, y00, z0);
|
|
AddTriVertex(st, x1, y11, z1);
|
|
AddTriVertex(st, x0, y01, z1);
|
|
}
|
|
}
|
|
|
|
st.GenerateNormals();
|
|
var mat = new StandardMaterial3D
|
|
{
|
|
VertexColorUseAsAlbedo = true,
|
|
Roughness = 0.88f,
|
|
Metallic = 0.06f
|
|
};
|
|
|
|
return new MeshInstance3D
|
|
{
|
|
Name = "BakeoffTrackMesh",
|
|
Mesh = st.Commit(),
|
|
MaterialOverride = mat
|
|
};
|
|
}
|
|
|
|
private void AddTriVertex(SurfaceTool st, float x, float y, float z)
|
|
{
|
|
st.SetNormal(SampleNormal(x, z));
|
|
st.SetColor(VertexColorAt(x, z));
|
|
st.AddVertex(new Vector3(x, y, z));
|
|
}
|
|
|
|
private static Color VertexColorAt(float x, float z)
|
|
{
|
|
// Desert rock base with subtle 1m grid and distinct lane markings
|
|
bool meterGrid = MathF.Abs(x - MathF.Round(x)) < 0.05f || MathF.Abs(z - MathF.Round(z)) < 0.05f;
|
|
bool fiveMeterTick = MathF.Abs(z - MathF.Round(z / 5f) * 5f) < 0.14f && MathF.Abs(x) < 9.5f;
|
|
|
|
Color col = new(0.36f, 0.30f, 0.24f);
|
|
if (z >= RampZStart && z <= RampZEnd)
|
|
{
|
|
col = new Color(0.41f, 0.33f, 0.25f);
|
|
}
|
|
else if (z >= DipStartZ && z <= DipEndZ)
|
|
{
|
|
col = new Color(0.33f, 0.28f, 0.26f);
|
|
}
|
|
|
|
// Left lane (X = -5m): cool cyan/slate lane rails
|
|
float distLeft = MathF.Abs(x - LeftLaneX);
|
|
if (distLeft < 2.6f)
|
|
{
|
|
col = col.Lerp(new Color(0.24f, 0.32f, 0.36f), 0.38f);
|
|
if (distLeft > 2.35f)
|
|
{
|
|
col = new Color(0.28f, 0.72f, 0.84f);
|
|
}
|
|
}
|
|
|
|
// Right lane (X = +5m): warm amber/sand lane rails
|
|
float distRight = MathF.Abs(x - RightLaneX);
|
|
if (distRight < 2.6f)
|
|
{
|
|
col = col.Lerp(new Color(0.38f, 0.30f, 0.21f), 0.38f);
|
|
if (distRight > 2.35f)
|
|
{
|
|
col = new Color(0.92f, 0.58f, 0.22f);
|
|
}
|
|
}
|
|
|
|
// Center divider strip
|
|
if (MathF.Abs(x) < 0.16f)
|
|
{
|
|
col = new Color(0.55f, 0.52f, 0.46f);
|
|
}
|
|
|
|
if (fiveMeterTick)
|
|
{
|
|
col = col.Lerp(new Color(0.80f, 0.76f, 0.68f), 0.32f);
|
|
}
|
|
else if (meterGrid)
|
|
{
|
|
col = col.Darkened(0.08f);
|
|
}
|
|
|
|
return col;
|
|
}
|
|
}
|