Darstellung245 Zeilen
ChunkMesher.cs
Baut aus dem Datenmodell ein echtes Mesh — und zwar nur die Flächen, die man wirklich sieht. Innere Blöcke und verdeckte Seiten kosten dadurch keine Dreiecke. Flächen lassen sich in Seiten und Enden trennen, damit ein Stamm außen Rinde und oben Jahresringe trägt.
Assets/Scripts/ChunkMesher.cs
using System.Collections.Generic;
using UnityEngine;
namespace Blockwerk
{
/// <summary>Erzeugt pro Chunk und Blockart ein Mesh mit ausschließlich sichtbaren Flächen.</summary>
public static class ChunkMesher
{
public const int ChunkSize = 16;
private static readonly Vector3Int[] Directions =
{
Vector3Int.right, Vector3Int.left, Vector3Int.up,
Vector3Int.down, Vector3Int.forward, Vector3Int.back
};
// Vier Eckpunkte je Richtung, gegen den Uhrzeigersinn von außen gesehen.
private static readonly Vector3[,] Corners =
{
{ new Vector3(1,0,0), new Vector3(1,1,0), new Vector3(1,1,1), new Vector3(1,0,1) },
{ new Vector3(0,0,1), new Vector3(0,1,1), new Vector3(0,1,0), new Vector3(0,0,0) },
{ new Vector3(0,1,0), new Vector3(0,1,1), new Vector3(1,1,1), new Vector3(1,1,0) },
{ new Vector3(0,0,1), new Vector3(0,0,0), new Vector3(1,0,0), new Vector3(1,0,1) },
{ new Vector3(1,0,1), new Vector3(1,1,1), new Vector3(0,1,1), new Vector3(0,0,1) },
{ new Vector3(0,0,0), new Vector3(0,1,0), new Vector3(1,1,0), new Vector3(1,0,0) }
};
/// <summary>
/// Welche Flächen ein Mesh enthält. Gras braucht Deck- und Seitenflächen getrennt,
/// Stämme Rinde an den Seiten und Jahresringe an den beiden Enden.
/// </summary>
public enum FaceSet { All, Sides, Top, Ends }
public static Mesh Build(VoxelWorld world, int originX, int originZ, BlockType type, FaceSet faces = FaceSet.All)
{
var vertices = new List<Vector3>();
var uvs = new List<Vector2>();
var triangles = new List<int>();
int endX = Mathf.Min(originX + ChunkSize, VoxelWorld.SizeX);
int endZ = Mathf.Min(originZ + ChunkSize, VoxelWorld.SizeZ);
for (int x = originX; x < endX; x++)
{
for (int z = originZ; z < endZ; z++)
{
for (int y = 0; y < VoxelWorld.Height; y++)
{
BlockType current = world.Get(x, y, z);
if (current != type) continue;
// Slabs und Treppen sind keine vollen Würfel — sie werden als Kisten gebaut.
if (BlockInfo.IsSlab(current) || BlockInfo.IsStairs(current))
{
AddShapeBoxes(world, current, x, y, z, originX, originZ, vertices, uvs, triangles);
continue;
}
for (int face = 0; face < 6; face++)
{
if (!Includes(faces, face)) continue;
Vector3Int normal = Directions[face];
if (!IsFaceVisible(world, x + normal.x, y + normal.y, z + normal.z, type)) continue;
int start = vertices.Count;
for (int corner = 0; corner < 4; corner++)
{
Vector3 offset = Corners[face, corner];
vertices.Add(new Vector3(
x - originX + offset.x,
y + offset.y,
z - originZ + offset.z));
uvs.Add(new Vector2(offset.x, offset.y));
}
triangles.Add(start);
triangles.Add(start + 1);
triangles.Add(start + 2);
triangles.Add(start);
triangles.Add(start + 2);
triangles.Add(start + 3);
}
}
}
}
if (vertices.Count == 0) return null;
var mesh = new Mesh { name = type + "_" + originX + "_" + originZ };
mesh.indexFormat = vertices.Count > 65000
? UnityEngine.Rendering.IndexFormat.UInt32
: UnityEngine.Rendering.IndexFormat.UInt16;
mesh.SetVertices(vertices);
mesh.SetUVs(0, uvs);
mesh.SetTriangles(triangles, 0);
mesh.RecalculateNormals();
mesh.RecalculateBounds();
return mesh;
}
/// <summary>Slab = untere Hälfte. Treppe = untere Hälfte plus Stufe auf der Seite, zu der sie ansteigt.</summary>
private static void AddShapeBoxes(VoxelWorld world, BlockType type, int x, int y, int z,
int originX, int originZ, List<Vector3> vertices, List<Vector2> uvs, List<int> triangles)
{
float ox = x - originX;
float oz = z - originZ;
AddBox(vertices, uvs, triangles, Vector3.zero, new Vector3(1f, 0.5f, 1f), ox, y, oz);
if (!BlockInfo.IsStairs(type)) return;
switch (world.Facing(x, y, z))
{
case 1: // Osten
AddBox(vertices, uvs, triangles, new Vector3(0.5f, 0.5f, 0f), new Vector3(1f, 1f, 1f), ox, y, oz);
break;
case 2: // Süden
AddBox(vertices, uvs, triangles, new Vector3(0f, 0.5f, 0.5f), new Vector3(1f, 1f, 1f), ox, y, oz);
break;
case 3: // Westen
AddBox(vertices, uvs, triangles, new Vector3(0f, 0.5f, 0f), new Vector3(0.5f, 1f, 1f), ox, y, oz);
break;
default: // Norden
AddBox(vertices, uvs, triangles, new Vector3(0f, 0.5f, 0f), new Vector3(1f, 1f, 0.5f), ox, y, oz);
break;
}
}
/// <summary>Eine Kiste aus sechs Flächen — ohne Nachbar-Prüfung, weil sie ihre Zelle nicht füllt.</summary>
private static void AddBox(List<Vector3> vertices, List<Vector2> uvs, List<int> triangles,
Vector3 min, Vector3 max, float ox, float oy, float oz)
{
for (int face = 0; face < 6; face++)
{
int start = vertices.Count;
for (int corner = 0; corner < 4; corner++)
{
Vector3 c = Corners[face, corner];
vertices.Add(new Vector3(
ox + Mathf.Lerp(min.x, max.x, c.x),
oy + Mathf.Lerp(min.y, max.y, c.y),
oz + Mathf.Lerp(min.z, max.z, c.z)));
uvs?.Add(new Vector2(c.x, c.y));
}
triangles.Add(start);
triangles.Add(start + 1);
triangles.Add(start + 2);
triangles.Add(start);
triangles.Add(start + 2);
triangles.Add(start + 3);
}
}
private static bool Includes(FaceSet faces, int face)
{
bool vertical = face == 2 || face == 3;
switch (faces)
{
case FaceSet.Sides: return !vertical;
case FaceSet.Top: return face == 2;
case FaceSet.Ends: return vertical;
default: return true;
}
}
/// <summary>
/// Ein Mesh mit allen festen Blöcken des Chunks. Es wird nicht gezeichnet, sondern trägt
/// nur den MeshCollider — so bleibt die Kollision unabhängig von den Material-Schichten.
/// </summary>
public static Mesh BuildCollision(VoxelWorld world, int originX, int originZ)
{
var vertices = new List<Vector3>();
var triangles = new List<int>();
int endX = Mathf.Min(originX + ChunkSize, VoxelWorld.SizeX);
int endZ = Mathf.Min(originZ + ChunkSize, VoxelWorld.SizeZ);
for (int x = originX; x < endX; x++)
{
for (int z = originZ; z < endZ; z++)
{
for (int y = 0; y < VoxelWorld.Height; y++)
{
BlockType current = world.Get(x, y, z);
if (!BlockInfo.IsSolid(current)) continue;
// Auch die Kollision folgt der Blockform: unten die halbe Zelle,
// bei Treppen zusätzlich die Stufe.
if (BlockInfo.IsSlab(current) || BlockInfo.IsStairs(current))
{
AddShapeBoxes(world, current, x, y, z, originX, originZ, vertices, null, triangles);
continue;
}
for (int face = 0; face < 6; face++)
{
int start = vertices.Count;
for (int corner = 0; corner < 4; corner++)
{
Vector3 offset = Corners[face, corner];
vertices.Add(new Vector3(
x - originX + offset.x,
y + offset.y,
z - originZ + offset.z));
}
triangles.Add(start);
triangles.Add(start + 1);
triangles.Add(start + 2);
triangles.Add(start);
triangles.Add(start + 2);
triangles.Add(start + 3);
}
}
}
}
if (vertices.Count == 0) return null;
var mesh = new Mesh { name = "Collision_" + originX + "_" + originZ };
mesh.indexFormat = vertices.Count > 65000
? UnityEngine.Rendering.IndexFormat.UInt32
: UnityEngine.Rendering.IndexFormat.UInt16;
mesh.SetVertices(vertices);
mesh.SetTriangles(triangles, 0);
mesh.RecalculateBounds();
return mesh;
}
/// <summary>Eine Fläche ist sichtbar, wenn dahinter Luft liegt — oder Wasser hinter festem Gestein.</summary>
private static bool IsFaceVisible(VoxelWorld world, int x, int y, int z, BlockType self)
{
BlockType neighbour = world.Get(x, y, z);
if (neighbour == BlockType.Air) return true;
return self != BlockType.Water && neighbour == BlockType.Water;
}
}
}