Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Rhyusaky · Jul 20, 2016 at 09:02 PM · c#buggenerationprocedural meshchunk

Problems Generate a Chunk blocks (Space Game Infinity)

Hello, I'm creating a space voxel game with planets and such, but I have a problem on the part of voxels, I created a test for these voxels where I create a small chunk with perlin noise, however, at first, it was buggy, and the faces were generated within the mesh, I tried to optimize it with a database of bytes where you fill in a Method of zeros and ones, where 0 and air, and 1 and stone, 2 coal, 3 sand, 4 grass, and then a Method that updates the chunk, generating blocks from the database of bytes, however, the faces are strange and texture, although it uses only one tile per block, are showing several tiles on the different faces of the blocks, and then the one error 65000 vertices, although the mesh is 16x30 x16, I found that it did not create all the vertices, and create within the ifs, work, but just more buggy, finally, I will post the code below if anyone knows help me, I'll be grateful.

 sing UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Chunk : MonoBehaviour {
     public int X,Y,Z,MaterialIndex;
     public Material[] Materials;
     public byte[,,] BlocksInChunk;
     [HideInInspector]
     public Mesh ChunkMesh;
     [HideInInspector]
     public List<Vector3> Vertices;
     [HideInInspector]
     public List<int> Triangles;
     [HideInInspector]
     public List<Vector2> UVs;
     [HideInInspector]
     //public byte[,,] Blocks;
     int CubesCount;
 
     [System.Serializable]
     public class Material {
         public string Name;
         public int Hardness;
         public Vector2 PositionAndScale;
     }
     // Use this for initialization
     void Start () {
         gameObject.GetComponent<MeshFilter>().mesh = new Mesh();
         ChunkMesh = gameObject.GetComponent<MeshFilter>().mesh;
         BlocksInChunk = new byte[257,257,257];
         //print (BlocksInChunk.GetLength(0));
         //print (BlocksInChunk[0,0,0]);
         //GenerateBlock(X,Y,Z,MaterialIndex);
         //GenerateBlock(1,1,1,0);
         //GenerateBlock(2,1,1,1);
         //GenerateBlock(2,2,1,2);
         //GenerateBlock(1,2,1,3);
         GenerateChunk();
     }
     void GenerateChunk() {
         for(int i=1;i<(17-1);i++)
             //for(int j=1;j<(10-1);j++)
                 for(int k=1;k<(17-1);k++)
             {
                 //GenerateBlock(i,j,k,Random.Range(0,4));
                 //BlocksInChunk[i,j,k] = (byte)Random.Range(0,4);
                 float Perlin = Mathf.PerlinNoise(i+0.1f,k+0.1f)*3;
                 BlocksInChunk[i,(int)Perlin,k] = 1;
                 //GenerateBlock(i,j+(int)Perlin,k,2);
             for(int j=(int)Perlin;j>1;j--) {
                 BlocksInChunk[i,j,k] = 1;
             }
             }
         //UpdateMesh();
         UpdateChunk();
     }
     void UpdateChunk() {
         for(int i=0;i<BlocksInChunk.GetLength(0);i++)
             for(int j=0;j<BlocksInChunk.GetLength(1);j++)
                 for(int k=0;k<BlocksInChunk.GetLength(2);k++)
             {
                 if(BlocksInChunk[i,j,k] > 0) {
                 GenerateBlock(i,j,k,BlocksInChunk[i,j,k]);
                 }
             }
         UpdateMesh();
     }
     void Update() {
         //Debug.DrawLine(Vertices[0],Vertices[3]);
         //Debug.DrawLine(Vertices[0],Vertices[1]);
         //Debug.DrawLine(Vertices[1],Vertices[2]);
         //Debug.DrawLine(Vertices[2],Vertices[3]);
         //Debug.DrawLine(Vertices[4],Vertices[7]);
         //Debug.DrawLine(Vertices[8],Vertices[11]);
         //Debug.DrawLine(Vertices[12],Vertices[15]);
         //Debug.DrawLine(Vertices[17],Vertices[19]);
         //Debug.DrawLine(Vertices[20],Vertices[23]);
     }
     void UpdateMesh() {
         ChunkMesh.vertices = Vertices.ToArray();
         ChunkMesh.triangles = Triangles.ToArray();
         ChunkMesh.uv = UVs.ToArray();
 
         ChunkMesh.Optimize();
         ChunkMesh.RecalculateBounds();
         ChunkMesh.RecalculateNormals();
     }
     void GenerateBlock(int X,int Y,int Z,int Material) {
         //if(BlocksInChunk[X,Y,Z] >= 0) {
         //Back
         //if(BlocksInChunk[X,Y,Z-1] <= 0) {
 
         //}
         //Front
         //if(BlocksInChunk[X,Y,Z+1] <= 0) {
 
         //}
         //Left
         //if(BlocksInChunk[X-1,Y,Z] <= 0) {
 
         //}
         //Right
         //if(BlocksInChunk[X+1,Y,Z] <= 0) {
 
         //}
         //Up
         //if(BlocksInChunk[X,Y+1,Z] <= 0) {
 
         //}
         //Down
         //if(BlocksInChunk[X,Y-1,Z] <= 0) {
 
         //}
 
         //Triangles
         //Back
         if(BlocksInChunk[X,Y,Z-1] <= 0) {
             Vertices.Add(new Vector3(X+0,Y+0,Z+0));
             Vertices.Add(new Vector3(X+1,Y+0,Z+0));
             Vertices.Add(new Vector3(X+1,Y+1,Z+0));
             Vertices.Add(new Vector3(X+0,Y+1,Z+0));
             UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
             UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
             UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
             UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
             Triangles.Add((CubesCount*4)+0);//1
             Triangles.Add((CubesCount*4)+3);//0
             Triangles.Add((CubesCount*4)+2);//2
             Triangles.Add((CubesCount*4)+1);//0
             Triangles.Add((CubesCount*4)+0);//3
             Triangles.Add((CubesCount*4)+2);//2
         }
 
         //Front
         if(BlocksInChunk[X,Y,Z+1] <= 0) {
             Vertices.Add(new Vector3(X+0,Y+0,Z+1));
             Vertices.Add(new Vector3(X+1,Y+0,Z+1));
             Vertices.Add(new Vector3(X+1,Y+1,Z+1));
             Vertices.Add(new Vector3(X+0,Y+1,Z+1));
             UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,0+Y+Materials[Material].PositionAndScale.y));
             UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,0+Y+Materials[Material].PositionAndScale.y));
             UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,1+Y+Materials[Material].PositionAndScale.y));
             UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,1+Y+Materials[Material].PositionAndScale.y));
             Triangles.Add((CubesCount*4)+5);//4
             Triangles.Add((CubesCount*4)+6);//5
             Triangles.Add((CubesCount*4)+7);//7
             Triangles.Add((CubesCount*4)+4);//5
             Triangles.Add((CubesCount*4)+5);//6
             Triangles.Add((CubesCount*4)+7);//7
         }
 
         //Left
         if(BlocksInChunk[X-1,Y,Z] <= 0) {
             Vertices.Add(new Vector3(X+0,Y+0,Z+0));
             Vertices.Add(new Vector3(X+0,Y+0,Z+1));
             Vertices.Add(new Vector3(X+0,Y+1,Z+1));
             Vertices.Add(new Vector3(X+0,Y+1,Z+0));
             UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
             UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
             UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
             UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
             Triangles.Add((CubesCount*4)+8);//1
             Triangles.Add((CubesCount*4)+9);//0
             Triangles.Add((CubesCount*4)+10);//2
             Triangles.Add((CubesCount*4)+10);//0
             Triangles.Add((CubesCount*4)+11);//3
             Triangles.Add((CubesCount*4)+8);//2
         }
         //Right
         if(BlocksInChunk[X+1,Y,Z] <= 0) {
             Vertices.Add(new Vector3(X+1,Y+0,Z+0));
             Vertices.Add(new Vector3(X+1,Y+0,Z+1));
             Vertices.Add(new Vector3(X+1,Y+1,Z+1));
             Vertices.Add(new Vector3(X+1,Y+1,Z+0));
             UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
             UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
             UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
             UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
             Triangles.Add((CubesCount*4)+12);//1
             Triangles.Add((CubesCount*4)+15);//0
             Triangles.Add((CubesCount*4)+14);//2
             Triangles.Add((CubesCount*4)+13);//0
             Triangles.Add((CubesCount*4)+12);//3
             Triangles.Add((CubesCount*4)+14);//2
         }
         //Up
         if(BlocksInChunk[X,Y+1,Z] <= 0) {
             Vertices.Add(new Vector3(X+0,Y+1,Z+0));
             Vertices.Add(new Vector3(X+1,Y+1,Z+0));
             Vertices.Add(new Vector3(X+1,Y+1,Z+1));
             Vertices.Add(new Vector3(X+0,Y+1,Z+1));
             UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
             UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
             UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
             UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
             Triangles.Add((CubesCount*4)+16);//0
             Triangles.Add((CubesCount*4)+19);//3
             Triangles.Add((CubesCount*4)+18);//2
             Triangles.Add((CubesCount*4)+17);//1
             Triangles.Add((CubesCount*4)+16);//0
             Triangles.Add((CubesCount*4)+18);//2
         }
         //Down
         if(BlocksInChunk[X,Y-1,Z] <= 0) {
             Vertices.Add(new Vector3(X+0,Y+0,Z+0));
             Vertices.Add(new Vector3(X+1,Y+0,Z+0));
             Vertices.Add(new Vector3(X+1,Y+0,Z+1));
             Vertices.Add(new Vector3(X+0,Y+0,Z+1));
             UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
             UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,0+Materials[Material].PositionAndScale.y));
             UVs.Add(new Vector2(1+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
             UVs.Add(new Vector2(0+Materials[Material].PositionAndScale.x,1+Materials[Material].PositionAndScale.y));
             Triangles.Add((CubesCount*4)+22);//20/22
             Triangles.Add((CubesCount*4)+20);//23/20
             Triangles.Add((CubesCount*4)+21);//22/21
             Triangles.Add((CubesCount*4)+22);//21/22
             Triangles.Add((CubesCount*4)+23);//20/23
             Triangles.Add((CubesCount*4)+20);//22/20
 
         }
 
         //UVs
         //UV Front Map
 
         //UV Back Map
 
         //UV Left Map
 
         //UV Right Map
 
         //UV Up Map
 
         //UV Down Map
 
 
 
         //BlocksInChunk[X,Y,Z] = 1;
         CubesCount++;
         //UpdateMesh();
         //}
     }
 }
 

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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Mesh generated from bezier curve loop going outside loop 0 Answers

How can I generate a up facing quad mesh with variable resolution? 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Fatal Error! Size overflow in allocator. Pathfinding script using NativeList's 0 Answers


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