Darstellung120 Zeilen
WorldChunk.cs
Ein Chunk besteht aus Material-Schichten (Gras-Seiten, Gras-Deckfläche, Rinde und Jahresringe der Stämme, Bretter, alle anderen Blöcke) plus einem unsichtbaren Kollisions-Mesh. So trägt jede Fläche ihre eigene Textur.
Assets/Scripts/WorldChunk.cs
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
namespace Blockwerk
{
public class WorldChunk : MonoBehaviour
{
public Vector2Int Origin { get; private set; }
private readonly List<GameObject> _layers = new List<GameObject>();
public void Build(VoxelWorld world, int originX, int originZ)
{
Origin = new Vector2Int(originX, originZ);
Clear();
BuildCollision(world, originX, originZ);
foreach (BlockType type in Enum.GetValues(typeof(BlockType)))
{
if (type == BlockType.Air) continue;
if (type == BlockType.Grass)
{
AddLayer(world, originX, originZ, type, ChunkMesher.FaceSet.Sides,
BlockMaterials.GrassSide(), "Grass Sides");
AddLayer(world, originX, originZ, type, ChunkMesher.FaceSet.Top,
BlockMaterials.GrassTop(), "Grass Top");
continue;
}
// Stamm: Rinde an den Seiten, Jahresringe nur an den Enden.
if (BlockInfo.IsLog(type))
{
AddLayer(world, originX, originZ, type, ChunkMesher.FaceSet.Sides,
BlockMaterials.LogSide(type), type + " Bark");
AddLayer(world, originX, originZ, type, ChunkMesher.FaceSet.Ends,
BlockMaterials.LogEnd(type), type + " Rings");
continue;
}
if (BlockInfo.IsPlanks(type))
{
AddLayer(world, originX, originZ, type, ChunkMesher.FaceSet.All,
BlockMaterials.Planks(type), type.ToString());
continue;
}
// Slabs und Treppen aus Holz tragen die Bretter ihrer Holzart.
if (BlockInfo.IsSlab(type) || BlockInfo.IsStairs(type))
{
Material material = BlockInfo.WoodKey(type) != null
? BlockMaterials.Planks(type)
: BlockMaterials.For(type);
AddLayer(world, originX, originZ, type, ChunkMesher.FaceSet.All,
material, type.ToString());
continue;
}
AddLayer(world, originX, originZ, type, ChunkMesher.FaceSet.All,
BlockMaterials.For(type), type.ToString());
}
}
private void AddLayer(VoxelWorld world, int originX, int originZ, BlockType type,
ChunkMesher.FaceSet faces, Material material, string layerName)
{
Mesh mesh = ChunkMesher.Build(world, originX, originZ, type, faces);
if (mesh == null) return;
var layer = new GameObject(layerName);
layer.transform.SetParent(transform, false);
layer.AddComponent<MeshFilter>().sharedMesh = mesh;
var renderer = layer.AddComponent<MeshRenderer>();
renderer.sharedMaterial = material;
renderer.shadowCastingMode = type == BlockType.Water
? ShadowCastingMode.Off
: ShadowCastingMode.On;
_layers.Add(layer);
}
private void BuildCollision(VoxelWorld world, int originX, int originZ)
{
Mesh mesh = ChunkMesher.BuildCollision(world, originX, originZ);
if (mesh == null) return;
var collision = new GameObject("Collision");
collision.transform.SetParent(transform, false);
collision.AddComponent<MeshCollider>().sharedMesh = mesh;
_layers.Add(collision);
}
public void Clear()
{
foreach (GameObject layer in _layers)
{
if (layer == null) continue;
if (layer.TryGetComponent(out MeshFilter filter) && filter.sharedMesh != null)
Destroy(filter.sharedMesh);
Destroy(layer);
}
_layers.Clear();
}
private void OnDestroy()
{
Clear();
}
}
}