- Home /
Voxel mesh generation not working.
I am building a voxel engine, and everything seems to be working(no errors), but when I try to build the mesh, it gives me an empty mesh with 0 verts and 0 tris. I can't tell if it is a problem with my voxel generator or my mesh generator(3d arrays don't show up in the inspector).
Yes, I know that the generation should be on the world class, not the chunk, and I will move it there just as soon as I can make the Chunks work(I am currently testing this with a single chunk, which is a mesh filter, mesh renderer, and Chunk script.)
Also, I want to keep the mesh-making separate from the voxel making and mesh assigning, because I am going to later incorporate other modifications to the mesh (like smoothing). Thanks!
Here is the Chunk itself:
 #pragma strict
 var Voxels:Voxel[,,]=new Voxel[16,16,16];
 var sample:Voxel[]=new Voxel[16];
 var mesh:Mesh;
 
 @ContextMenu("GenerateVoxels")
 function Generate(){
 var image : int[,]=new int[16,16];//image generation. Better gen will go here later.
     for(var x:int=0;x<16;x++){
         for(var y:int=0;y<16;y++){
         image[x,y]=Mathf.RoundToInt(16*Mathf.PerlinNoise(x,y));
         }
     }//Image -> Voxels.Add Caves?
     for(var xg:int=0;xg<16;xg++){
         for(var yg:int=0;yg<16;yg++){
             for(var zg:int=0;zg<16;zg++){
                 if(image[xg,yg]<= zg){
                     Voxels[xg,yg,zg]=new DirtVoxel();
                 }else{
                     Voxels[xg,yg,zg]=new AirVoxel();
                 }
             }
         }
     }
 }
 
 @ContextMenu("RenderMesh")
 function Render(){
 mesh = ChunkRenderer.Render(Voxels);
 print("Verts " + mesh.vertexCount);
 print("Tris " + mesh.triangles.Length);
 gameObject.GetComponent(MeshFilter).mesh=mesh;
 }
And here is the ChunkRenderer class:
 import System.Collections.Generic;
 #pragma strict
 
 static function Render(Voxels : Voxel[,,]):Mesh
 {
     var vertices : List.<Vector3> = new List.<Vector3>();
     var triangles : List.<int> = new List.<int>();
     
     for(var x:int=1; x<15; x++){
         for(var y:int=1;y<15;y++){
             for(var z:int=1;z<15;z++){
             //iterate through all voxels in the chunk and detect solidity/transparency to determine faces
             var voxel:Voxel = Voxels[x,y,z];
             var top:Voxel=Voxels[x,y+1,z];
             var bottom:Voxel=Voxels[x,y-1,z];
             var front:Voxel=Voxels[x+1,y,z];
             var back:Voxel=Voxels[x-1,y,z];
             var right:Voxel=Voxels[x,y,z+1];
             var left:Voxel=Voxels[x,y,z-1];
             var vertexIndex:int;
             if(voxel.solid){
                 if(top.transparent){
                         vertexIndex= vertices.Count;
                         vertices.Add(new Vector3(x-0.5,y+0.5,z-0.5));
                         vertices.Add(new Vector3(x+0.5,y+0.5,z-0.5));
                         vertices.Add(new Vector3(x+0.5,y+0.5,z+0.5));
                         vertices.Add(new Vector3(x-0.5,y+0.5,z+0.5));
  
                         // first triangle for the block top
                         triangles.Add(vertexIndex);
                         triangles.Add(vertexIndex+1);
                         triangles.Add(vertexIndex+2);
                          
                         // second triangle for the block top
                         triangles.Add(vertexIndex+2);
                         triangles.Add(vertexIndex+3);
                         triangles.Add(vertexIndex);
                 }
                 if(bottom.transparent){            //CHECK WINDING ORDER!
                         vertexIndex= vertices.Count;
                         vertices.Add(new Vector3(x-0.5,y-0.5,z-0.5));
                         vertices.Add(new Vector3(x+0.5,y-0.5,z-0.5));
                         vertices.Add(new Vector3(x+0.5,y-0.5,z+0.5));
                         vertices.Add(new Vector3(x-0.5,y-0.5,z+0.5));
                         
                         //first triangle for the block bottom
                         triangles.Add(vertexIndex);
                         triangles.Add(vertexIndex+1);
                         triangles.Add(vertexIndex+2);
                         //second triangle for the block bottom
                         triangles.Add(vertexIndex+2);
                         triangles.Add(vertexIndex+3);
                         triangles.Add(vertexIndex);
                 }
                 if(front.transparent){
                     vertexIndex= vertices.Count;
                     vertices.Add(new Vector3(x+0.5,y+0.5,z-0.5));
                     vertices.Add(new Vector3(x+0.5,y-0.5,z-0.5));
                     vertices.Add(new Vector3(x+0.5,y-0.5,z+0.5));
                     vertices.Add(new Vector3(x+0.5,y+0.5,z+0.5));
                     
                     //first triangle for the block front
                     triangles.Add(vertexIndex);
                     triangles.Add(vertexIndex+1);
                     triangles.Add(vertexIndex+2);
                     //second triangle for the block front
                     triangles.Add(vertexIndex+2);
                     triangles.Add(vertexIndex+3);
                     triangles.Add(vertexIndex);
                 }
                 if(back.transparent){
                     vertexIndex= vertices.Count;
                     vertices.Add(new Vector3(x-0.5,y+0.5,z-0.5));
                     vertices.Add(new Vector3(x-0.5,y+0.5,z+0.5));
                     vertices.Add(new Vector3(x-0.5,y-0.5,z+0.5));
                     vertices.Add(new Vector3(x-0.5,y-0.5,z-0.5));
                     
                     //first triangle for the block back
                     triangles.Add(vertexIndex);
                     triangles.Add(vertexIndex+1);
                     triangles.Add(vertexIndex+2);
                     //second triangle for the block back
                     triangles.Add(vertexIndex+2);
                     triangles.Add(vertexIndex+3);
                     triangles.Add(vertexIndex);
                 }
                 if(right.transparent){
                     vertexIndex= vertices.Count;
                     vertices.Add(new Vector3(x-0.5,y+0.5,z+0.5));
                     vertices.Add(new Vector3(x+0.5,y+0.5,z+0.5));
                     vertices.Add(new Vector3(x+0.5,y-0.5,z+0.5));
                     
                     //first triangle for the block right
                     triangles.Add(vertexIndex);
                     triangles.Add(vertexIndex+1);
                     triangles.Add(vertexIndex+2);
                     //second triangle for the block right
                     triangles.Add(vertexIndex+2);
                     triangles.Add(vertexIndex+3);
                     triangles.Add(vertexIndex);
                 }
                 if(left.transparent){
                     vertexIndex= vertices.Count;
                     vertices.Add(new Vector3(x+0.5,y+0.5,z-0.5));
                     vertices.Add(new Vector3(x-0.5,y+0.5,z-0.5));
                     vertices.Add(new Vector3(x-0.5,y-0.5,z-0.5));
                     vertices.Add(new Vector3(x+0.5,y-0.5,z-0.5));
                     
                     //first triangle for the block left
                     triangles.Add(vertexIndex);
                     triangles.Add(vertexIndex+1);
                     triangles.Add(vertexIndex+2);
                     //second triangle for the block left
                     triangles.Add(vertexIndex+2);
                     triangles.Add(vertexIndex+3);
                     triangles.Add(vertexIndex);
                 }
             }
             }
         }
     }//end loop
     //build mesh
     var mesh:Mesh = new Mesh();
     mesh.vertices = vertices.ToArray();
     mesh.triangles = triangles.ToArray();
     
     return mesh;
 }
i think it would be hard for someone to sort through your code, but there are many useful questions on here eg
http://answers.unity3d.com/questions/293607/tiling-uv-mapping.html
http://answers.unity3d.com/questions/321428/adding-mesh-collider-in-run-time-slow.html
http://answers.unity3d.com/questions/315059/how-to-improve-performance-while-generating-extrud.html
be sure to carefully note the GOTCHYA in Unity mesh, explained here and many times...
http://answers.unity3d.com/questions/417483/is-there-a-way-to-assign-meshverticesi-directly.html
Answer by Fattie · May 05, 2013 at 09:20 AM
 if ( top.transparent )
wouldn't those be:
 if ( NOT top.transparent ) ...
you could solve this in seconds with Debug.Log lines. Like Debug,Log("I am adding a left side, we are up to " + vertexIndex);
Thanks! It works! The problem was in the very beginning of the code, where the for loops didn't pick up on my air voxels, so there were no transparent faces.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                