- Home /
Problems Generate a Chunk blocks (Space Game Infinity)
Hello, I'm creating a space voxel game with planets and such, but I have a problem on the part of voxels, I created a test for these voxels where I create a small chunk with perlin noise, however, at first, it was buggy, and the faces were generated within the mesh, I tried to optimize it with a database of bytes where you fill in a Method of zeros and ones, where 0 and air, and 1 and stone, 2 coal, 3 sand, 4 grass, and then a Method that updates the chunk, generating blocks from the database of bytes, however, the faces are strange and texture, although it uses only one tile per block, are showing several tiles on the different faces of the blocks, and then the one error 65000 vertices, although the mesh is 16x30 x16, I found that it did not create all the vertices, and create within the ifs, work, but just more buggy, finally, I will post the code below if anyone knows help me, I'll be grateful.
sing UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Chunk : MonoBehaviour {
public int X,Y,Z,MaterialIndex;
public Material[] Materials;
public byte[,,] BlocksInChunk;
[HideInInspector]
public Mesh ChunkMesh;
[HideInInspector]
public List<Vector3> Vertices;
[HideInInspector]
public List<int> Triangles;
[HideInInspector]
public List<Vector2> UVs;
[HideInInspector]
//public byte[,,] Blocks;
int CubesCount;
[System.Serializable]
public class Material {
public string Name;
public int Hardness;
public Vector2 PositionAndScale;
}
// Use this for initialization
void Start () {
gameObject.GetComponent<MeshFilter>().mesh = new Mesh();
ChunkMesh = gameObject.GetComponent<MeshFilter>().mesh;
BlocksInChunk = new byte[257,257,257];
//print (BlocksInChunk.GetLength(0));
//print (BlocksInChunk[0,0,0]);
//GenerateBlock(X,Y,Z,MaterialIndex);
//GenerateBlock(1,1,1,0);
//GenerateBlock(2,1,1,1);
//GenerateBlock(2,2,1,2);
//GenerateBlock(1,2,1,3);
GenerateChunk();
}
void GenerateChunk() {
for(int i=1;i<(17-1);i++)
//for(int j=1;j<(10-1);j++)
for(int k=1;k<(17-1);k++)
{
//GenerateBlock(i,j,k,Random.Range(0,4));
//BlocksInChunk[i,j,k] = (byte)Random.Range(0,4);
float Perlin = Mathf.PerlinNoise(i+0.1f,k+0.1f)*3;
BlocksInChunk[i,(int)Perlin,k] = 1;
//GenerateBlock(i,j+(int)Perlin,k,2);
for(int j=(int)Perlin;j>1;j--) {
BlocksInChunk[i,j,k] = 1;
}
}
//UpdateMesh();
UpdateChunk();
}
void UpdateChunk() {
for(int i=0;i<BlocksInChunk.GetLength(0);i++)
for(int j=0;j<BlocksInChunk.GetLength(1);j++)
for(int k=0;k<BlocksInChunk.GetLength(2);k++)
{
if(BlocksInChunk[i,j,k] > 0) {
GenerateBlock(i,j,k,BlocksInChunk[i,j,k]);
}
}
UpdateMesh();
}
void Update() {
//Debug.DrawLine(Vertices[0],Vertices[3]);
//Debug.DrawLine(Vertices[0],Vertices[1]);
//Debug.DrawLine(Vertices[1],Vertices[2]);
//Debug.DrawLine(Vertices[2],Vertices[3]);
//Debug.DrawLine(Vertices[4],Vertices[7]);
//Debug.DrawLine(Vertices[8],Vertices[11]);
//Debug.DrawLine(Vertices[12],Vertices[15]);
//Debug.DrawLine(Vertices[17],Vertices[19]);
//Debug.DrawLine(Vertices[20],Vertices[23]);
}
void UpdateMesh() {
ChunkMesh.vertices = Vertices.ToArray();
ChunkMesh.triangles = Triangles.ToArray();
ChunkMesh.uv = UVs.ToArray();
ChunkMesh.Optimize();
ChunkMesh.RecalculateBounds();
ChunkMesh.RecalculateNormals();
}
void GenerateBlock(int X,int Y,int Z,int Material) {
//if(BlocksInChunk[X,Y,Z] >= 0) {
//Back
//if(BlocksInChunk[X,Y,Z-1] <= 0) {
//}
//Front
//if(BlocksInChunk[X,Y,Z+1] <= 0) {
//}
//Left
//if(BlocksInChunk[X-1,Y,Z] <= 0) {
//}
//Right
//if(BlocksInChunk[X+1,Y,Z] <= 0) {
//}
//Up
//if(BlocksInChunk[X,Y+1,Z] <= 0) {
//}
//Down
//if(BlocksInChunk[X,Y-1,Z] <= 0) {
//}
//Triangles
//Back
if(BlocksInChunk[X,Y,Z-1] <= 0) {
Vertices.Add(new Vector3(X+0,Y+0,Z+0));
Vertices.Add(new Vector3(X+1,Y+0,Z+0));
Vertices.Add(new Vector3(X+1,Y+1,Z+0));
Vertices.Add(new Vector3(X+0,Y+1,Z+0));
UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
Triangles.Add((CubesCount*4)+0);//1
Triangles.Add((CubesCount*4)+3);//0
Triangles.Add((CubesCount*4)+2);//2
Triangles.Add((CubesCount*4)+1);//0
Triangles.Add((CubesCount*4)+0);//3
Triangles.Add((CubesCount*4)+2);//2
}
//Front
if(BlocksInChunk[X,Y,Z+1] <= 0) {
Vertices.Add(new Vector3(X+0,Y+0,Z+1));
Vertices.Add(new Vector3(X+1,Y+0,Z+1));
Vertices.Add(new Vector3(X+1,Y+1,Z+1));
Vertices.Add(new Vector3(X+0,Y+1,Z+1));
UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,0+Y+Materials[Material].PositionAndScale.y));
UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,0+Y+Materials[Material].PositionAndScale.y));
UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,1+Y+Materials[Material].PositionAndScale.y));
UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,1+Y+Materials[Material].PositionAndScale.y));
Triangles.Add((CubesCount*4)+5);//4
Triangles.Add((CubesCount*4)+6);//5
Triangles.Add((CubesCount*4)+7);//7
Triangles.Add((CubesCount*4)+4);//5
Triangles.Add((CubesCount*4)+5);//6
Triangles.Add((CubesCount*4)+7);//7
}
//Left
if(BlocksInChunk[X-1,Y,Z] <= 0) {
Vertices.Add(new Vector3(X+0,Y+0,Z+0));
Vertices.Add(new Vector3(X+0,Y+0,Z+1));
Vertices.Add(new Vector3(X+0,Y+1,Z+1));
Vertices.Add(new Vector3(X+0,Y+1,Z+0));
UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
Triangles.Add((CubesCount*4)+8);//1
Triangles.Add((CubesCount*4)+9);//0
Triangles.Add((CubesCount*4)+10);//2
Triangles.Add((CubesCount*4)+10);//0
Triangles.Add((CubesCount*4)+11);//3
Triangles.Add((CubesCount*4)+8);//2
}
//Right
if(BlocksInChunk[X+1,Y,Z] <= 0) {
Vertices.Add(new Vector3(X+1,Y+0,Z+0));
Vertices.Add(new Vector3(X+1,Y+0,Z+1));
Vertices.Add(new Vector3(X+1,Y+1,Z+1));
Vertices.Add(new Vector3(X+1,Y+1,Z+0));
UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
Triangles.Add((CubesCount*4)+12);//1
Triangles.Add((CubesCount*4)+15);//0
Triangles.Add((CubesCount*4)+14);//2
Triangles.Add((CubesCount*4)+13);//0
Triangles.Add((CubesCount*4)+12);//3
Triangles.Add((CubesCount*4)+14);//2
}
//Up
if(BlocksInChunk[X,Y+1,Z] <= 0) {
Vertices.Add(new Vector3(X+0,Y+1,Z+0));
Vertices.Add(new Vector3(X+1,Y+1,Z+0));
Vertices.Add(new Vector3(X+1,Y+1,Z+1));
Vertices.Add(new Vector3(X+0,Y+1,Z+1));
UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
Triangles.Add((CubesCount*4)+16);//0
Triangles.Add((CubesCount*4)+19);//3
Triangles.Add((CubesCount*4)+18);//2
Triangles.Add((CubesCount*4)+17);//1
Triangles.Add((CubesCount*4)+16);//0
Triangles.Add((CubesCount*4)+18);//2
}
//Down
if(BlocksInChunk[X,Y-1,Z] <= 0) {
Vertices.Add(new Vector3(X+0,Y+0,Z+0));
Vertices.Add(new Vector3(X+1,Y+0,Z+0));
Vertices.Add(new Vector3(X+1,Y+0,Z+1));
Vertices.Add(new Vector3(X+0,Y+0,Z+1));
UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
Triangles.Add((CubesCount*4)+22);//20/22
Triangles.Add((CubesCount*4)+20);//23/20
Triangles.Add((CubesCount*4)+21);//22/21
Triangles.Add((CubesCount*4)+22);//21/22
Triangles.Add((CubesCount*4)+23);//20/23
Triangles.Add((CubesCount*4)+20);//22/20
}
//UVs
//UV Front Map
//UV Back Map
//UV Left Map
//UV Right Map
//UV Up Map
//UV Down Map
//BlocksInChunk[X,Y,Z] = 1;
CubesCount++;
//UpdateMesh();
//}
}
}
Your answer
Follow this Question
Related Questions
Mesh generated from bezier curve loop going outside loop 0 Answers
How can I generate a up facing quad mesh with variable resolution? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Fatal Error! Size overflow in allocator. Pathfinding script using NativeList's 0 Answers