- Home /
Cube Voxel not working. Help please
I am trying to make a very basic flat terrain with square voxels. Im new to voxels and im trying to learn. I understand how to make a 3d cube but now im trying to make multiple of them and make it to where it only generates the sides of the cube nessesary in order to speed up the game and have it lag less. I got this far but for some reason it wont work. Im getting an error saying that the array index is out of range. Does anyone have any ideas on how to help me? Thank you. MyScript:
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Chunk : MonoBehaviour { private List newVertices = new List(); private List newTriangles = new List(); private List newUV = new List();
private float tUnit = 0.25f;
public Vector2 tStone = new Vector2 (1, 0);
public Vector2 tGrass = new Vector2 (3, 0);
public Vector2 tDirt = new Vector2 (2, 0);
public Vector2 tSand = new Vector2 (0, 0);
public Vector2 tBlue = new Vector2 (0, 1);
public Vector2 tYellow = new Vector2 (1, 1);
private Mesh mesh;
private MeshCollider col;
private int faceCount;
public byte[ , , ] data;
public int chunkX = 20;
public int chunkY = 20;
public int chunkZ = 20;
void Start ()
{
data = new byte[chunkX, chunkY, chunkZ];
for (int x=0; x<chunkX; x++)
{
for (int y=0; y<chunkY; y++)
{
for (int z=0; z<chunkZ; z++)
{
if (y<5)
{
data[x, y, z] = 1; // Makes it solid
}else{
data[x, y, z] = 0; // Makes it air
}
}
}
}
mesh = GetComponent<MeshFilter> ().mesh;
col = GetComponent<MeshCollider> ();
GenerateMesh ();
}
void GenerateMesh ()
{
for (int x=0; x<chunkX; x++)
{
for (int y=0; y<chunkY; y++)
{
for (int z=0; z<chunkZ; z++)
{
if (data[x+1, y, z] == 0) // Checks if air to the side
{
CubeEast(x, y, z, data[x, y, z]); // If air next to it then make the side of the cube
}
if (data[x, y+1, z] == 0)
{
CubeTop(x, y, z, data[x, y, z]);
}
if (data[x, y, z+1] == 0)
{
CubeNorth(x, y, z, data[x, y, z]);
}
if (data[x-1, y, z] == 0)
{
CubeWest(x, y, z, data[x, y, z]);
}
if (data[x, y-1, z] == 0)
{
CubeBot(x, y, z, data[x, y, z]);
}
if (data[x, y, z-1] == 0)
{
CubeSouth(x, y, z, data[x, y, z]);
}
}
}
}
UpdateMesh ();
}
void CubeTop (int x, int y, int z, byte block)
{
newVertices.Add(new Vector3 (x, y, z + 1));
newVertices.Add(new Vector3 (x + 1, y, z + 1));
newVertices.Add(new Vector3 (x + 1, y, z ));
newVertices.Add(new Vector3 (x, y, z ));
Vector2 texturePos;
texturePos=tGrass;
Cube (texturePos);
}
void CubeNorth(int x, int y, int z, byte block)
{
newVertices.Add(new Vector3 (x + 1, y-1, z + 1));
newVertices.Add(new Vector3 (x + 1, y, z + 1));
newVertices.Add(new Vector3 (x, y, z + 1));
newVertices.Add(new Vector3 (x, y-1, z + 1));
Vector2 texturePos;
texturePos=tStone;
Cube (texturePos);
}
void CubeEast(int x, int y, int z, byte block)
{
newVertices.Add(new Vector3 (x + 1, y - 1, z));
newVertices.Add(new Vector3 (x + 1, y, z));
newVertices.Add(new Vector3 (x + 1, y, z + 1));
newVertices.Add(new Vector3 (x + 1, y - 1, z + 1));
Vector2 texturePos;
texturePos=tDirt;
Cube (texturePos);
}
void CubeSouth(int x, int y, int z, byte block)
{
newVertices.Add(new Vector3 (x, y - 1, z));
newVertices.Add(new Vector3 (x, y, z));
newVertices.Add(new Vector3 (x + 1, y, z));
newVertices.Add(new Vector3 (x + 1, y - 1, z));
Vector2 texturePos;
texturePos=tSand;
Cube (texturePos);
}
void CubeWest(int x, int y, int z, byte block)
{
newVertices.Add(new Vector3 (x, y- 1, z + 1));
newVertices.Add(new Vector3 (x, y, z + 1));
newVertices.Add(new Vector3 (x, y, z));
newVertices.Add(new Vector3 (x, y - 1, z));
Vector2 texturePos;
texturePos=tBlue;
Cube (texturePos);
}
void CubeBot(int x, int y, int z, byte block)
{
newVertices.Add(new Vector3 (x, y-1, z ));
newVertices.Add(new Vector3 (x + 1, y-1, z ));
newVertices.Add(new Vector3 (x + 1, y-1, z + 1));
newVertices.Add(new Vector3 (x, y-1, z + 1));
Vector2 texturePos;
texturePos=tYellow;
Cube (texturePos);
}
void UpdateMesh ()
{
mesh.Clear ();
mesh.vertices = newVertices.ToArray();
mesh.uv = newUV.ToArray();
mesh.triangles = newTriangles.ToArray();
mesh.Optimize ();
mesh.RecalculateNormals ();
col.sharedMesh=null;
col.sharedMesh=mesh;
newVertices.Clear();
newUV.Clear();
newTriangles.Clear();
faceCount=0;
}
void Cube (Vector2 texturePos)
{
newTriangles.Add(faceCount * 4 );
newTriangles.Add(faceCount * 4 + 1 );
newTriangles.Add(faceCount * 4 + 2 );
newTriangles.Add(faceCount * 4 );
newTriangles.Add(faceCount * 4 + 2 );
newTriangles.Add(faceCount * 4 + 3 );
newUV.Add(new Vector2 (tUnit * texturePos.x + tUnit, tUnit * texturePos.y));
newUV.Add(new Vector2 (tUnit * texturePos.x + tUnit, tUnit * texturePos.y + tUnit));
newUV.Add(new Vector2 (tUnit * texturePos.x, tUnit * texturePos.y + tUnit));
newUV.Add(new Vector2 (tUnit * texturePos.x, tUnit * texturePos.y));
faceCount++;
}
}
Can you past the actual Error you are getting ins$$anonymous$$d of just paraphrasing it. The Error message indicates the script and what line it occurred on which is vital information you have left out.
Answer by Cherno · Sep 30, 2014 at 08:53 AM
If an array index is out of range, it's probably because you either exceed the bounds of your [,,] array, or the uvs and vertices lists don't match.