using System.Collections.Generic; using UnityEngine; namespace BlockWorld { /// 区块世界:16×64×16 的方块块,面剔除 + AO 生成网格 public class World : MonoBehaviour { public const int CX = 16, CZ = 16, CY = 64, Sea = 18; public int Seed = 20261119; public int StreamRadius = 4; public Material SolidMat, WaterMat; public class Chunk { public Vector2Int coord; public readonly byte[] data = new byte[CX * CY * CZ]; public bool generated, dirty = true; public GameObject solidGo, waterGo; public Mesh solidMesh, waterMesh; public int At(int x, int y, int z) => data[(y * CZ + z) * CX + x]; public void Set(int x, int y, int z, int id) => data[(y * CZ + z) * CX + x] = (byte)id; } readonly Dictionary chunks = new Dictionary(); readonly List removeList = new List(); static readonly int[][][] Faces = { new[] { new[] { 0, 1, 0 }, new[] { 0, 1, 0 }, new[] { 0, 1, 1 }, new[] { 1, 1, 1 }, new[] { 1, 1, 0 } }, new[] { new[] { 0, -1, 0 }, new[] { 0, 0, 1 }, new[] { 0, 0, 0 }, new[] { 1, 0, 0 }, new[] { 1, 0, 1 } }, new[] { new[] { 0, 0, 1 }, new[] { 0, 0, 1 }, new[] { 1, 0, 1 }, new[] { 1, 1, 1 }, new[] { 0, 1, 1 } }, new[] { new[] { 0, 0, -1 }, new[] { 1, 0, 0 }, new[] { 0, 0, 0 }, new[] { 0, 1, 0 }, new[] { 1, 1, 0 } }, new[] { new[] { 1, 0, 0 }, new[] { 1, 0, 1 }, new[] { 1, 0, 0 }, new[] { 1, 1, 0 }, new[] { 1, 1, 1 } }, new[] { new[] { -1, 0, 0 }, new[] { 0, 0, 0 }, new[] { 0, 0, 1 }, new[] { 0, 1, 1 }, new[] { 0, 1, 0 } }, }; static readonly float[] Shade = { 1.0f, 0.52f, 0.86f, 0.86f, 0.72f, 0.72f }; static readonly float[] AoLevel = { 0.55f, 0.72f, 0.86f, 1.0f }; public int HeightAt(int wx, int wz) { float n = Noise.Fbm2(wx * 0.042f, wz * 0.042f, 4, 0); float m = Noise.Fbm2(wx * 0.011f + 917, wz * 0.011f, 3, 1301); int h = Mathf.RoundToInt(Sea + 2 + (n - 0.5f) * 20 + (m - 0.5f) * 14); return Mathf.Clamp(h, 3, CY - 12); } public Chunk GetChunk(int cx, int cz, bool create) { var k = new Vector2Int(cx, cz); if (chunks.TryGetValue(k, out var c)) return c; if (!create) return null; c = new Chunk { coord = k }; chunks[k] = c; return c; } public int GetBlock(int x, int y, int z) { if (y < 0) return Blocks.Bedrock; if (y >= CY) return Blocks.Air; var c = GetChunk(x >> 4, z >> 4, false); if (c == null || !c.generated) return Blocks.Air; return c.At(x & 15, y, z & 15); } public bool SetBlock(int x, int y, int z, int id) { if (y < 0 || y >= CY) return false; var c = GetChunk(x >> 4, z >> 4, true); if (!c.generated) Generate(c); c.Set(x & 15, y, z & 15, id); c.dirty = true; // 边界上的邻居也要重建 int lx = x & 15, lz = z & 15; if (lx == 0) MarkDirty((x >> 4) - 1, z >> 4); if (lx == 15) MarkDirty((x >> 4) + 1, z >> 4); if (lz == 0) MarkDirty(x >> 4, (z >> 4) - 1); if (lz == 15) MarkDirty(x >> 4, (z >> 4) + 1); return true; } void MarkDirty(int cx, int cz) { var c = GetChunk(cx, cz, false); if (c != null) c.dirty = true; } public void Generate(Chunk c) { if (c.generated) return; int ox = c.coord.x * CX, oz = c.coord.y * CZ; for (int lz = 0; lz < CZ; lz++) for (int lx = 0; lx < CX; lx++) { int h = HeightAt(ox + lx, oz + lz); for (int y = 0; y < CY; y++) { int id = Blocks.Air; if (y == 0) id = Blocks.Bedrock; else if (y < h - 4) id = Blocks.Stone; else if (y < h) id = Blocks.Dirt; else if (y == h) id = h <= Sea + 1 ? Blocks.Sand : h > 34 ? Blocks.Snow : Blocks.Grass; else if (y <= Sea) id = Blocks.Water; if (id != Blocks.Air) c.Set(lx, y, lz, id); } } c.generated = true; c.dirty = true; } public void Stream(Vector3 playerPos, Transform parent) { int pcx = Mathf.FloorToInt(playerPos.x) >> 4; int pcz = Mathf.FloorToInt(playerPos.z) >> 4; for (int dz = -StreamRadius; dz <= StreamRadius; dz++) for (int dx = -StreamRadius; dx <= StreamRadius; dx++) { if (dx * dx + dz * dz > (StreamRadius + 0.5f) * (StreamRadius + 0.5f)) continue; var c = GetChunk(pcx + dx, pcz + dz, true); if (!c.generated) Generate(c); } removeList.Clear(); foreach (var kv in chunks) { if (Mathf.Abs(kv.Key.x - pcx) > StreamRadius + 2 || Mathf.Abs(kv.Key.y - pcz) > StreamRadius + 2) removeList.Add(kv.Key); } foreach (var k in removeList) { var c = chunks[k]; Destroy(c.solidGo); Destroy(c.waterGo); chunks.Remove(k); } } /// 每帧最多重建 N 个脏区块,避免卡顿 public void RebuildDirty(Transform parent, int budget = 2) { foreach (var kv in chunks) { if (budget <= 0) break; var c = kv.Value; if (!c.generated || !c.dirty) continue; c.dirty = false; budget--; BuildMeshes(c, parent); } } void BuildMeshes(Chunk c, Transform parent) { var sv = new List(); var suv = new List(); var scol = new List(); var st = new List(); var wv = new List(); var wuv = new List(); var wcol = new List(); var wt = new List(); int ox = c.coord.x * CX, oz = c.coord.y * CZ; for (int y = 0; y < CY; y++) for (int lz = 0; lz < CZ; lz++) for (int lx = 0; lx < CX; lx++) { int id = c.At(lx, y, lz); if (id == Blocks.Air) continue; int wx = ox + lx, wz = oz + lz; bool water = id == Blocks.Water; for (int f = 0; f < 6; f++) { var face = Faces[f]; int nb = GetBlock(wx + face[0][0], y + face[0][1], wz + face[0][2]); if (Blocks.IsOpaque(nb)) continue; if (nb == id) continue; AddFace(water ? wv : sv, water ? wuv : suv, water ? wcol : scol, water ? wt : st, f, wx, y, wz, id, face); } } c.solidMesh = Build(c.solidMesh, c.solidGo, sv, suv, scol, st, SolidMat, parent, new Vector3(c.coord.x * CX, 0, c.coord.y * CZ), out c.solidGo); c.waterMesh = Build(c.waterMesh, c.waterGo, wv, wuv, wcol, wt, WaterMat, parent, new Vector3(c.coord.x * CX, 0, c.coord.y * CZ), out c.waterGo); } Mesh Build(Mesh mesh, GameObject go, List v, List uv, List col, List tris, Material mat, Transform parent, Vector3 origin, out GameObject outGo) { outGo = go; if (tris.Count == 0) { if (go != null) go.SetActive(false); return mesh; } if (mesh == null) { mesh = new Mesh { indexFormat = UnityEngine.Rendering.IndexFormat.UInt32 }; mesh.MarkDynamic(); } mesh.Clear(); mesh.SetVertices(v); mesh.SetUVs(0, uv); mesh.SetColors(col); mesh.SetTriangles(tris, 0); mesh.RecalculateNormals(); mesh.RecalculateBounds(); if (go == null) { go = new GameObject("chunk"); go.transform.SetParent(parent, false); go.AddComponent(); var mr = go.AddComponent(); mr.sharedMaterial = mat; mr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; } go.transform.position = origin; go.GetComponent().sharedMesh = mesh; if (!go.activeSelf) go.SetActive(true); return mesh; } static void AddFace(List v, List uv, List col, List tris, int f, int wx, int y, int wz, int id, int[][] face) { int axis = f / 2; int[] axes = axis == 0 ? new[] { 0, 2 } : axis == 1 ? new[] { 0, 1 } : new[] { 2, 1 }; int tile = Blocks.TileOf(id, axis == 0 ? (f == 0 ? 0 : 2) : 1); float u0 = AtlasTexture.U0(tile), v0 = AtlasTexture.V0(tile), du = AtlasTexture.Du; int baseIndex = v.Count; for (int k = 0; k < 4; k++) { var cc = face[k + 1]; v.Add(new Vector3(wx + cc[0], y + cc[1], wz + cc[2])); float u = axis == 0 ? cc[0] : axis == 1 ? cc[0] : 1 - cc[2]; float vv = axis == 0 ? cc[2] : 1 - cc[1]; uv.Add(new Vector2(u0 + Mathf.Clamp(u, 0.001f, 0.999f) * du, v0 + Mathf.Clamp(vv, 0.001f, 0.999f) * du)); int nx = face[0][0], ny = face[0][1], nz = face[0][2]; bool s1 = Blocks.IsSolid(Get(wx + nx, y + ny, wz + nz, axes[0], cc)); bool s2 = Blocks.IsSolid(Get(wx + nx, y + ny, wz + nz, axes[1], cc)); int ao = (s1 && s2) ? 0 : 3 - ((s1 ? 1 : 0) + (s2 ? 1 : 0)); float l = Shade[f] * AoLevel[ao]; col.Add(new Color(l, l, l, 1f)); } tris.Add(baseIndex); tris.Add(baseIndex + 1); tris.Add(baseIndex + 2); tris.Add(baseIndex); tris.Add(baseIndex + 2); tris.Add(baseIndex + 3); } // AO 用相邻格判断;静态 World 访问需要一个实例,这里用单例引用 static World instance; void Awake() { instance = this; } static int Get(int x, int y, int z, int axis, int[] cc) { if (instance == null) return Blocks.Air; int dx = axis == 0 ? (cc[0] == 1 ? 1 : -1) : 0; int dy = axis == 1 ? (cc[1] == 1 ? 1 : -1) : 0; int dz = axis == 2 ? (cc[2] == 1 ? 1 : -1) : 0; return instance.GetBlock(x + dx, y + dy, z + dz); } } }