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 · Sep 30, 2014 at 03:05 AM · c#meshgenerationvoxel

Cube Voxel not working. Help please

I am trying to make a very basic flat terrain with square voxels. Im new to voxels and im trying to learn. I understand how to make a 3d cube but now im trying to make multiple of them and make it to where it only generates the sides of the cube nessesary in order to speed up the game and have it lag less. I got this far but for some reason it wont work. Im getting an error saying that the array index is out of range. Does anyone have any ideas on how to help me? Thank you. MyScript:

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class Chunk : MonoBehaviour { private List newVertices = new List(); private List newTriangles = new List(); private List newUV = new List();

 private float tUnit = 0.25f;
 public Vector2 tStone = new Vector2 (1, 0);
 public Vector2 tGrass = new Vector2 (3, 0);
 public Vector2 tDirt = new Vector2 (2, 0);
 public Vector2 tSand = new Vector2 (0, 0);
 public Vector2 tBlue = new Vector2 (0, 1);
 public Vector2 tYellow = new Vector2 (1, 1);
 
 private Mesh mesh;
 private MeshCollider col;
 
 private int faceCount;
 
 public byte[ , , ] data;
 public int chunkX = 20;
 public int chunkY = 20;
 public int chunkZ = 20;
 
 void Start ()
 {
     data = new byte[chunkX, chunkY, chunkZ];
     for (int x=0; x<chunkX; x++)
     {
         for (int y=0; y<chunkY; y++)
         {
             for (int z=0; z<chunkZ; z++)
             {
                 if (y<5)
                 {
                     data[x, y, z] = 1; // Makes it solid
                 }else{
                     data[x, y, z] = 0; // Makes it air
                 }
             }
         }
     }
     
     mesh = GetComponent<MeshFilter> ().mesh;
     col = GetComponent<MeshCollider> ();
     
     GenerateMesh ();
 }
 
 void GenerateMesh ()
 {
     for (int x=0; x<chunkX; x++)
     {
         for (int y=0; y<chunkY; y++)
         {
             for (int z=0; z<chunkZ; z++)
             {
                 if (data[x+1, y, z] == 0)  // Checks if air to the side
                 {
                     CubeEast(x, y, z, data[x, y, z]);  // If air next to it then make the side of the cube
                 }
                 if (data[x, y+1, z] == 0)
                 {
                     CubeTop(x, y, z, data[x, y, z]);
                 }
                 if (data[x, y, z+1] == 0)
                 {
                     CubeNorth(x, y, z, data[x, y, z]);
                 }
                 if (data[x-1, y, z] == 0)
                 {
                     CubeWest(x, y, z, data[x, y, z]);
                 }
                 if (data[x, y-1, z] == 0)
                 {
                     CubeBot(x, y, z, data[x, y, z]);
                 }
                 if (data[x, y, z-1] == 0)
                 {
                     CubeSouth(x, y, z, data[x, y, z]);
                 }
             }
         }
     }
     
     UpdateMesh ();
 }
 
 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=tGrass;
     Cube (texturePos);
 }
 
 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=tStone;
     Cube (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=tDirt;
     Cube (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=tSand;
     Cube (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=tBlue;
     Cube (texturePos);
 }
 
 void CubeBot(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=tYellow;
     Cube (texturePos);
 }
 
 void UpdateMesh ()
 {
     mesh.Clear ();
     mesh.vertices = newVertices.ToArray();
     mesh.uv = newUV.ToArray();
     mesh.triangles = newTriangles.ToArray();
     mesh.Optimize ();
     mesh.RecalculateNormals ();
     
     col.sharedMesh=null;
     col.sharedMesh=mesh;
     
     newVertices.Clear();
     newUV.Clear();
     newTriangles.Clear();
     
     faceCount=0;
 }
 
 void Cube (Vector2 texturePos) 
 {
     newTriangles.Add(faceCount * 4  );
     newTriangles.Add(faceCount * 4 + 1 );
     newTriangles.Add(faceCount * 4 + 2 );
     newTriangles.Add(faceCount * 4  );
     newTriangles.Add(faceCount * 4 + 2 );
     newTriangles.Add(faceCount * 4 + 3 );
     
     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++;
 }

}

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 mattyman174 · Sep 30, 2014 at 06:11 AM 1
Share

Can you past the actual Error you are getting ins$$anonymous$$d of just paraphrasing it. The Error message indicates the script and what line it occurred on which is vital information you have left out.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · Sep 30, 2014 at 08:53 AM

If an array index is out of range, it's probably because you either exceed the bounds of your [,,] array, or the uvs and vertices lists don't match.

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

27 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 avatar image

Related Questions

Mesh creation by code not working? 0 Answers

How do I make multiple meshes in the same script? 1 Answer

Mesh generated from bezier curve loop going outside loop 0 Answers

Huge C# Procedural Generation Errors. Why? 1 Answer

Marching cubes problem, some tris not drawing properly 2 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