I am working on a Minecraft clone and I have done chunk generation.
Chunk.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chunk : MonoBehaviour
{
Mesh mesh;
public MeshFilter meshFilter;
public static int chunkSize = 16;
public Block.BlockType[,,] blocks;
public bool isUpdate = false;
public static int count;
private void Start()
{
blocks = new Block.BlockType[chunkSize, chunkSize, chunkSize];
for (int x = 0; x < chunkSize; x++)
{
for (int y = 0; y < chunkSize; y++)
{
for (int z = 0; z < chunkSize; z++)
{
blocks[x, y, z] = Block.BlockType.air;
}
}
}
blocks[8, 8, 8] = Block.BlockType.stone;
isUpdate = true;
}
private void Update()
{
if(isUpdate)
{
isUpdate = false;
ChunkUpdate();
}
}
public Block.BlockType GetBlock(int x, int y, int z)
{
return blocks[x, y, z];
}
public void ChunkUpdate()
{
for (int x = 0; x < chunkSize; x++)
{
for (int y = 0; y < chunkSize; y++)
{
for (int z = 0; z < chunkSize; z++)
{
Block.RenderBlock(x, y, z, this);
}
}
}
mesh = new Mesh();
meshFilter.mesh.Clear();
meshFilter.mesh.vertices = Block.vertices.ToArray();
meshFilter.mesh.triangles = Block.triangles.ToArray();
meshFilter.mesh.RecalculateNormals();
count = 0;
}
}
Block.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Block
{
public enum BlockType
{
air,
stone,
}
public static List<Vector3> vertices = new List<Vector3>();
public static List<int> triangles = new List<int>();
public static void RenderBlock(int x, int y, int z, Chunk chunk)
{
if (chunk.GetBlock(x, y, z) != BlockType.air)
{
if (!isSolid(x, y + 1, z, chunk))
{
vertices.Add(new Vector3(0, 1, 0) + new Vector3(x, y, z));
vertices.Add(new Vector3(0, 1, 1) + new Vector3(x, y, z));
vertices.Add(new Vector3(1, 1, 1) + new Vector3(x, y, z));
vertices.Add(new Vector3(1, 1, 0) + new Vector3(x, y, z));
AddTriangles();
}
if (!isSolid(x, y - 1, z, chunk))
{
vertices.Add(new Vector3(0, 0, 0) + new Vector3(x, y, z));
vertices.Add(new Vector3(1, 0, 0) + new Vector3(x, y, z));
vertices.Add(new Vector3(1, 0, 1) + new Vector3(x, y, z));
vertices.Add(new Vector3(0, 0, 1) + new Vector3(x, y, z));
AddTriangles();
}
if (!isSolid(x, y, z + 1, chunk))
{
vertices.Add(new Vector3(0, 0, 1) + new Vector3(x, y, z));
vertices.Add(new Vector3(1, 0, 1) + new Vector3(x, y, z));
vertices.Add(new Vector3(1, 1, 1) + new Vector3(x, y, z));
vertices.Add(new Vector3(0, 1, 1) + new Vector3(x, y, z));
AddTriangles();
}
if (!isSolid(x, y, z - 1, chunk))
{
vertices.Add(new Vector3(0, 0, 0) + new Vector3(x, y, z));
vertices.Add(new Vector3(0, 1, 0) + new Vector3(x, y, z));
vertices.Add(new Vector3(1, 1, 0) + new Vector3(x, y, z));
vertices.Add(new Vector3(1, 0, 0) + new Vector3(x, y, z));
AddTriangles();
}
if (!isSolid(x + 1, y, z, chunk))
{
vertices.Add(new Vector3(1, 0, 0) + new Vector3(x, y, z));
vertices.Add(new Vector3(1, 1, 0) + new Vector3(x, y, z));
vertices.Add(new Vector3(1, 1, 1) + new Vector3(x, y, z));
vertices.Add(new Vector3(1, 0, 1) + new Vector3(x, y, z));
AddTriangles();
}
if (!isSolid(x - 1, y, z, chunk))
{
vertices.Add(new Vector3(0, 0, 0) + new Vector3(x, y, z));
vertices.Add(new Vector3(0, 0, 1) + new Vector3(x, y, z));
vertices.Add(new Vector3(0, 1, 1) + new Vector3(x, y, z));
vertices.Add(new Vector3(0, 1, 0) + new Vector3(x, y, z));
AddTriangles();
}
}
}
public static void AddTriangles()
{
triangles.Add((Chunk.count * 4) + 0);
triangles.Add((Chunk.count * 4) + 1);
triangles.Add((Chunk.count * 4) + 2);
triangles.Add((Chunk.count * 4) + 0);
triangles.Add((Chunk.count * 4) + 2);
triangles.Add((Chunk.count * 4) + 3);
Chunk.count++;
}
public static bool isSolid(int x, int y, int z, Chunk chunk)
{
if(chunk.GetBlock(x, y, z) == BlockType.air)
{
return false;
}
else
{
return true;
}
}
}
There are two scripts, Block.cs contains information about blocks, and Chunk.cs generates a chunk. Everything works well, but when I generate a block on the border of a chunk, it gives an error. Block.cs also checks adjacent blocks, if the adjacent block is air, then the side of the block is rendered, otherwise it is not, this chunk is optimized in this way. But the block does not check adjacent blocks outside the chunk.
This problem made it difficult for me to create a Minecraft clone. I would be grateful if you have the opportunity to help me with this.