I have a 129-by-129 height map. Tell me how to add it to the simulation. thanks
Code: Select all
using BepuPhysics.Collidables;
using BepuUtilities.Memory;
using System.Numerics;
namespace Godless_Lands_Game.Terrain
{
class TerrainCollider
{
public Mesh Mesh { get; private set; }
public TerrainCollider(int width, int height, float[] heightMap, BufferPool pool)
{
Mesh = CreateDeformedPlane(width, height, heightMap, pool);
}
private Mesh CreateDeformedPlane(int width, int height, float[] heightMap, BufferPool pool)
{
pool.Take<Vector3>(width * height, out var vertices);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
vertices[height * i + j] = new Vector3(width, heightMap[i * width + j], height);
}
}
var quadWidth = width - 1;
var quadHeight = height - 1;
var triangleCount = quadWidth * quadHeight * 2;
pool.Take<Triangle>(triangleCount, out var triangles);
for (int i = 0; i < quadHeight; ++i)
{
for (int j = 0; j < quadWidth; ++j)
{
var triangleIndex = (i * quadWidth + j) * 2;
ref var triangle0 = ref triangles[triangleIndex];
ref var v00 = ref vertices[width * i + j];
ref var v01 = ref vertices[width * i + j + 1];
ref var v10 = ref vertices[width * (i + 1) + j];
ref var v11 = ref vertices[width * (i + 1) + j + 1];
triangle0.A = v00;
triangle0.B = v01;
triangle0.C = v10;
ref var triangle1 = ref triangles[triangleIndex + 1];
triangle1.A = v01;
triangle1.B = v11;
triangle1.C = v10;
}
}
pool.Return(ref vertices);
return new Mesh(triangles, Vector3.One, pool);
}
}
}