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 Theacesofspades · Jan 11, 2015 at 07:08 PM · c#colliderruntimevoxelmesh collider

Mesh collider not working correctly on voxel terrain

So i have been trying to make a voxel world and I have it generating perfectly but the only problem i am having right now is generating the collider. For some reason it is all wacky and only generates some of the collider or will generate the collider where the mesh isnt. Can anyone take a look at this? I know its sort of a lot but im not sure what else to post so thank you.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ChunkScript : MonoBehaviour 
 {
     public byte[ , , ] data;
 
     public List<Vector2> allTextures = new List<Vector2> ();
 
     public int chunkSizeX = 50;
     public int chunkSizeY = 50;
     
     public List<Vector3> newVertices = new List<Vector3>();
     public List<int> newTriangles = new List<int>();
     public List<Vector2> newUV = new List<Vector2>();
     private Mesh mesh;
     
     public float texturesAcross = 4;
     private float tUnit;
     
     private int faceCount;
     
     public List<Vector3> colVertices = new List<Vector3>();
     public List<int> colTriangles = new List<int>();
     private int colCount;
     
     private MeshCollider col;
 
     public float scale1 = 10;
     public float height1 = 3;
     public float power1 = 1;
 
     public int startHeight2 = 300;
     public float scale2 = 20;
     public float height2 = 4;
     public float power2 = 0;
     public int addition2 = 10;
 
     public bool update=false;
 
 
     void Start () 
     {
         tUnit = 1 / texturesAcross;
         
         mesh = GetComponent<MeshFilter> ().mesh;
         col = GetComponent<MeshCollider> ();
 
         GenerateMesh ();
     }
 
     void Update ()
     {
         if(update)
         {
             UpdateBlockSides();
             update=false;
         }
     }
 
     void OnGUI ()
     {
         GUILayout.Label ("Scale: ");
         scale1 = GUILayout.HorizontalSlider (scale1, 0, 100);
         GUILayout.Label ("Height: ");
         height1 = GUILayout.HorizontalSlider (height1, 0, chunkSizeY-1);
         GUILayout.Label ("Power: ");
         power1 = GUILayout.HorizontalSlider (power1, 0, 2);
 
         if (GUILayout.Button("GenerateMesh"))
         {
             GenerateMesh ();
         }
     }
 
     void GenerateMesh ()
     {
         data = new byte[chunkSizeX, chunkSizeY, chunkSizeX];
 
         for (int x=0; x<data.GetLength(0); x++)
         {
             for (int z=0; z<data.GetLength(2); z++)
             {
                 int grass = PerlinNoise(x,0,z,scale1,height1,power1);
                 grass += PerlinNoise(x,startHeight2,z,scale2,height2,power2)+addition2;
                 grass += 3;
 
                 int dirt = PerlinNoise(x,0,z,scale1,height1,power1);
                 dirt += PerlinNoise(x,startHeight2,z,scale2,height2,power2)+addition2;
                 dirt += 2;
 
                 int stone = PerlinNoise(x,0,z,scale1,height1,power1);
                 stone += PerlinNoise(x,startHeight2,z,scale2,height2,power2)+addition2;
                 stone += 0;
 
                 for (int y=0; y<data.GetLength(1); y++)
                 {
                     if (y < grass)
                     {
                         data[x, y, z] = 1;
                     }
                     if (y < dirt)
                     {
                         data[x, y, z] = 2;
                     }
                     if (y < stone)
                     {
                         data[x, y, z] = 3;
                     }
                 }
             }
         }
 
         UpdateBlockSides ();
     }
 
     int PerlinNoise(int x,int y, int z, float scale, float height, float power)
     {
         float rValue;
         rValue=Noise.GetNoise (((double)x) / scale, ((double)y)/ scale, ((double)z) / scale);
         rValue*=height;
         
         if(power!=0)
         {
             rValue=Mathf.Pow( rValue, power);
         }
         
         return (int) rValue;
     }
 
     void UpdateBlockSides ()
     {
         for (int x=0; x<data.GetLength(0); x++)
         {
             for (int y=0; y<data.GetLength(1); y++)
             {
                 for (int z=0; z<data.GetLength(2); z++)
                 {
                     if (data[x, y, z] != 0)
                     {
                         GenerateCollider (x, y, z);
 
                         if (Block (x+1, y, z) == 0)
                         {
                             CubeEast(x, y, z, data[x, y, z]);
                         }
                         if (Block (x-1, y, z) == 0)
                         {
                             CubeWest(x, y, z, data[x, y, z]);
                         }
                         if (Block (x, y+1, z) == 0)
                         {
                             CubeTop(x, y, z, data[x, y, z]);
                         }
                         if (Block (x, y-1, z) == 0)
                         {
                             CubeBottom(x, y, z, data[x, y, z]);
                         }
                         if (Block (x, y, z+1) == 0)
                         {
                             CubeNorth(x, y, z, data[x, y, z]);
                         }
                         if (Block (x, y, z-1) == 0)
                         {
                             CubeSouth(x, y, z, data[x, y, z]);
                         }
                     }
                 }
             }
         }
 
         UpdateMesh ();
     }
 
     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 = allTextures[block];
         
         CreateTexture (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 = allTextures[block];
         
         CreateTexture (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 = allTextures[block];
         
         CreateTexture (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 = allTextures[block];
         
         CreateTexture (texturePos);
     }
     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 = allTextures[block];
 
         CreateTexture (texturePos);
     }
     void CubeBottom (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 = allTextures[block];
         
         CreateTexture (texturePos);
     }
 
     void CreateTexture (Vector2 texturePos)
     {
         newTriangles.Add(faceCount * 4  ); //1
         newTriangles.Add(faceCount * 4 + 1 ); //2
         newTriangles.Add(faceCount * 4 + 2 ); //3
         newTriangles.Add(faceCount * 4  ); //1
         newTriangles.Add(faceCount * 4 + 2 ); //3
         newTriangles.Add(faceCount * 4 + 3 ); //4
 
         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 ++;
     }
 
     void UpdateMesh ()
     {
         mesh.Clear ();
         mesh.vertices = newVertices.ToArray();
         mesh.triangles = newTriangles.ToArray();
         mesh.uv = newUV.ToArray();
         mesh.Optimize ();
         mesh.RecalculateNormals ();
         
         faceCount=0;
         newVertices.Clear();
         newTriangles.Clear();
         newUV.Clear();
         
         Mesh newMesh = new Mesh();
         newMesh.vertices = colVertices.ToArray();
         newMesh.triangles = colTriangles.ToArray();
         col.sharedMesh= newMesh;
         colVertices.Clear();
         colTriangles.Clear();
         colCount=0;
     }
 
     void ColliderTriangles()
     {
         colTriangles.Add(colCount*4);
         colTriangles.Add((colCount*4)+1);
         colTriangles.Add((colCount*4)+3);
         colTriangles.Add((colCount*4)+1);
         colTriangles.Add((colCount*4)+2);
         colTriangles.Add((colCount*4)+3);
     }
 
 
     void GenerateCollider(int x, int y, int z)
     {
         //Top
         if(Block(x,y+1,z)==0){
             colVertices.Add( new Vector3 (x  , y  , 1));
             colVertices.Add( new Vector3 (x + 1 , y  , 1));
             colVertices.Add( new Vector3 (x + 1 , y  , 0 ));
             colVertices.Add( new Vector3 (x  , y  , 0 ));
             
             ColliderTriangles();
             
             colCount++;
         }
         
         //bot
         if(Block(x,y-1,z)==0){
             colVertices.Add( new Vector3 (x  , y -1 , 0));
             colVertices.Add( new Vector3 (x + 1 , y -1 , 0));
             colVertices.Add( new Vector3 (x + 1 , y -1 , 1 ));
             colVertices.Add( new Vector3 (x  , y -1 , 1 ));
             
             ColliderTriangles();
             colCount++;
         }
         
         //left
         if(Block(x-1,y,z)==0){
             colVertices.Add( new Vector3 (x  , y -1 , 1));
             colVertices.Add( new Vector3 (x  , y  , 1));
             colVertices.Add( new Vector3 (x  , y  , 0 ));
             colVertices.Add( new Vector3 (x  , y -1 , 0 ));
             
             ColliderTriangles();
             
             colCount++;
         }
         
         //right
         if(Block(x+1,y,z)==0){
             colVertices.Add( new Vector3 (x +1 , y  , 1));
             colVertices.Add( new Vector3 (x +1 , y -1 , 1));
             colVertices.Add( new Vector3 (x +1 , y -1 , 0 ));
             colVertices.Add( new Vector3 (x +1 , y  , 0 ));
             
             ColliderTriangles();
             
             colCount++;
         }
 
         //Front
         if(Block(x,y,z+1)==0){
             colVertices.Add(new Vector3 (x + 1, y-1, z + 1));
             colVertices.Add(new Vector3 (x + 1, y, z + 1));
             colVertices.Add(new Vector3 (x, y, z + 1));
             colVertices.Add(new Vector3 (x, y-1, z + 1));
             
             ColliderTriangles();
             
             colCount++;
         }
 
         //Back
         if(Block(x,y,z-1)==0){
             colVertices.Add(new Vector3 (x, y - 1, z));
             colVertices.Add(new Vector3 (x, y, z));
             colVertices.Add(new Vector3 (x + 1, y, z));
             colVertices.Add(new Vector3 (x + 1, y - 1, z));
             
             ColliderTriangles();
             
             colCount++;
         }
     }
 
     public byte Block(int x, int y, int z)
     {
         if( x>=data.GetLength(0) || x<0 || y>=data.GetLength(0) || y<0 || z>=data.GetLength(0) || z<0)
         {
             return (byte) 1;
         }
         
         return data[x,y,z];
     }
 }
 
Comment
Add comment
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

1 Reply

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

Answer by Theacesofspades · Jan 11, 2015 at 08:37 PM

I had to add z to all of the vertecies.

Comment
Add comment · 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

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

26 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to create a custom cuboids at runtime? 1 Answer

Object hover over collider during runtime? 2 Answers

Runtime mesh decimation 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