Hi again,
Another problem has arisen.
Whats happened is I have created a small voxel land which runs pretty smoothly, the only problem is that underground is dark, as it should be. I have attached a spotlight onto my Main Camera (3rd person Controller), and it lights up the terrain pretty badly, Some image examples are shown below.
Here’s my code that I’m currently using to generate the terrain.
function GenerateMesh()
{
var mesh = voxelMesh.GetComponent(MeshFilter).mesh;
mesh.Clear();
var newVertices = new Vector3[0];
var newTriangles = new int[0];
var newUVs = new Vector2[0];
var newColors = new Color[0];
var newNormals = new Vector3[0];
_triangles = [ 0, 1, 2, 2, 1, 3 ];
var _topNormals = [Vector3( 0, 1, 0), Vector3( 0, 1, 0), Vector3( 0, 1, 0), Vector3( 0, 1, 0)];
var _bottomNormals = [Vector3( 0,-1, 0), Vector3( 0,-1, 0), Vector3( 0,-1, 0), Vector3( 0,-1, 0)];
var _frontNormals = [Vector3( 0, 0,-1), Vector3( 0, 0,-1), Vector3( 0, 0,-1), Vector3( 0, 0,-1)];
var _backNormals = [Vector3( 0, 0, 1), Vector3( 0, 0, 1), Vector3( 0, 0, 1), Vector3( 0, 0, 1)];
var _leftNormals = [Vector3(-1, 0, 0), Vector3(-1, 0, 0), Vector3(-1, 0, 0), Vector3(-1, 0, 0)];
var _rightNormals = [Vector3( 1, 0, 0), Vector3( 1, 0, 0), Vector3( 1, 0, 0), Vector3( 1, 0, 0)];
for(x=0; x<dim.x; x++)
{
for(y=0; y<dim.y; y++)
{
for(z=0; z<dim.z; z++)
{
if(gridArray[x,y,z] != 0)
{
//////////////
// Vertices //
//////////////
var y1 = y * -1;
frontVertices = [Vector3(x,y1,z), Vector3(x,y1+1,z), Vector3(x+1,y1,z), Vector3(x+1,y1+1,z)];
topVertices = [Vector3(x,y1+1,z), Vector3(x,y1+1,z+1), Vector3(x+1,y1+1,z), Vector3(x+1,y1+1,z+1)];
leftVertices = [Vector3(x,y1,z+1), Vector3(x,y1+1,z+1), Vector3(x,y1,z), Vector3(x,y1+1,z)];
rightVertices = [Vector3(x+1,y1,z), Vector3(x+1,y1+1,z), Vector3(x+1,y1,z+1), Vector3(x+1,y1+1,z+1)];
backVertices = [Vector3(x+1,y1,z+1), Vector3(x+1,y1+1,z+1), Vector3(x,y1,z+1), Vector3(x,y1+1,z+1)];
bottomVertices = [Vector3(x,y1,z+1), Vector3(x,y1,z), Vector3(x+1,y1,z+1),Vector3(x+1,y1,z)];
////////////
// Coding //
////////////
if(CheckGrid(Vector3(x,y-1,z)) == 0)
{
// Draw Top Face
newVertices += topVertices;
newTriangles += _triangles;
newColors += [colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z]];
_triangles = IncreaseIntArray(_triangles, 4);
newNormals += _topNormals;
}
if(CheckGrid(Vector3(x,y+1,z)) == 0)
{
// Draw Bottom Face
newVertices += bottomVertices;
newTriangles += _triangles;
newColors += [colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z]];
_triangles = IncreaseIntArray(_triangles, 4);
newNormals += _bottomNormals;
}
if(CheckGrid(Vector3(x-1,y,z)) == 0)
{
// Draw Left Face
newVertices += leftVertices;
newTriangles += _triangles;
newColors += [colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z]];
_triangles = IncreaseIntArray(_triangles, 4);
newNormals += _leftNormals;
}
if(CheckGrid(Vector3(x+1,y,z)) == 0)
{
// Draw Right Face
newVertices += rightVertices;
newTriangles += _triangles;
newColors += [colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z]];
_triangles = IncreaseIntArray(_triangles, 4);
newNormals += _rightNormals;
}
if(CheckGrid(Vector3(x,y,z-1)) == 0)
{
// Draw Front Face
newVertices += frontVertices;
newTriangles += _triangles;
newColors += [colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z]];
_triangles = IncreaseIntArray(_triangles, 4);
newNormals += _frontNormals;
}
if(CheckGrid(Vector3(x,y,z+1)) == 0)
{
// Draw Back Face
newVertices += backVertices;
newTriangles += _triangles;
newColors += [colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z]];
_triangles = IncreaseIntArray(_triangles, 4);
newNormals += _backNormals;
}
}
}
}
}
mesh.Clear();
mesh.vertices = newVertices;
mesh.triangles = newTriangles;
mesh.colors = newColors;
mesh.normals = newNormals;
mesh.Optimize();
voxelMesh.GetComponent(MeshCollider).sharedMesh = null;
voxelMesh.GetComponent(MeshCollider).sharedMesh = mesh;
voxelMesh.renderer.material = voxelMaterial;
}
Any ideas how to fix it?