Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Zarenityx · May 05, 2013 at 07:08 AM · meshverticesgenerationvoxeltriangles

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;
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · May 05, 2013 at 01:40 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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);

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zarenityx · May 12, 2013 at 10:20 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Connecting flatshaded vertices 0 Answers

Setting triangles failing 0 Answers

can 5 verts and 4 triangles be done on a square texture? 1 Answer

Does mesh.triangles also return a copy of the triangle array? 1 Answer

Rebuilding mesh.triangles after removing vertices 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges