Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Stupid person here, new to bytes

Discussion in 'Scripting' started by keenanwoodall, Aug 1, 2014.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    597
    I'm going through a voxel/dynamic mesh tutorial (which btw is amazing). The tutorial is written in cs but I'm a js guy. I'vebeen learning AND converting which has been a fun challenge. A ways into the tutorial the writer started using bytes. I have no idea what those are, how they work, or what they do. Because of my 100% lack of knowledge I don't know the syntax for it. I can't figure out how to translate the tutorials sections that use byte into javascript format. The specific part I'm stuck on is returning a byte. I'm sure it's very obvious because I couldn't find it on the internet anywhere! Heres my code. I'll put something obvious around the important section so that you can find it quick. The script is a bit large-ish
    Code (js):
    1.  
    2. import System.Collections.Generic;
    3.  
    4. @script AddComponentMenu ("Cool Tools/Random Generation/Polygon Generator")
    5. @script RequireComponent(MeshFilter)
    6. @script RequireComponent(MeshRenderer)
    7. @script RequireComponent(MeshCollider)
    8.     //Inspector Variables-----------------------------------------------------
    9. //These variables give us the ability to change the size of the blocks by feeding the variables into the blocks' byte
    10. var xAmount : int = 10;
    11. var yAmount : int = 10;
    12. //This is going to store all of the vertices of the mesh we are going to render
    13. var newVertices : List.<Vector3> = new List.<Vector3>();
    14. var colVertices : List.<Vector3> = new List.<Vector3>();
    15. //The triangles tell Unity how to build the each part of the mesh that connects vertices
    16. var newTriangles : List.<int> = new List.<int>();
    17. var colTriangles : List.<int> = new List.<int>();
    18. //The UV list tells Unity how to display/position the texture on each polygon
    19. var newUV : List.<Vector2> = new List.<Vector2>();
    20.  
    21.     //Hidden Variables--------------------------------------------------------
    22. //The mesh will be made up the vertices, polys, and UVs that we define. After we make them, we'll apply them to this mesh
    23. private var mesh : Mesh;
    24. private var col : MeshCollider;
    25. //The texture applied to this objects mesh has different colored tiles to represent things like grass, dirt, rock etc.
    26. //Here we define a float that will be changed in the Start function
    27. //The float will represent the percentage of the textures width that one tile takes up. We will call that percentage a texUnit
    28. private var texUnit : float = 0.25;    //The texture has 16 tiles, 4 wide, 4 tall. One tile is a fourth, or takes up 0.25 or the width
    29. private var texStone : Vector2 = new Vector2(0, 0);    //Here we are defining the texture coordinates for the stone tile portion of the texture
    30. private var texGrass : Vector2 = new Vector2(0, 1);    //Here we are defining the texture coordinates for the grass tile portion of the texture
    31. //This variable is important. It lets us know how many squares we have created so that
    32. //For instance, when we need to access the meshes 1046 vertice we can do so easily by knowing which square we're on and adding the ints to the triangles
    33. private var squareCount : int;
    34. //This variable is similar to the squareCount, but is used for the colliders
    35. private var colCount : int;
    36. //Here we will store all of the different block types/information. For example, 0 could be air, 1 grass, and 2 rock
    37. var blocks : byte[,];
    38.  
    39. function Start ()
    40. {
    41.     mesh = GetComponent(MeshFilter).mesh;
    42.     col = GetComponent(MeshCollider);
    43.  
    44.     GenTerrain();
    45.     BuildMesh();
    46.     UpdateMesh();
    47. }
    48.  
    49. function GenTerrain ()
    50. {
    51.     //This makes a sets the array of blocks' size to 10x10. The actual blocks will be built in a seperate function
    52.     blocks = new byte[xAmount, yAmount];
    53.     for(var px : int = 0; px < blocks.GetLength(0); px++)//px will stand for the blocks x position
    54.     {
    55.         for(var py : int = 0; py < blocks.GetLength(1); py++)//py will stand for the blocks y position
    56.         {
    57.             if(py == 5)
    58.             {
    59.                 blocks[px, py] = 2;
    60.             }
    61.             else if (py < 5)
    62.             {
    63.                 blocks[px, py] = 1;
    64.             }
    65.         }
    66.     }
    67. }
    68.  
    69. function BuildMesh ()
    70. {
    71.     for(var px = 0; px < blocks.GetLength(0); px++)
    72.     {
    73.         for(var py : int; py < blocks.GetLength(1); py++)
    74.         {
    75.             if(blocks != 0)
    76.             {
    77.                 GenCollider(px, py);
    78.                 if(blocks[px, py] == 1)
    79.                 {
    80.                     GenSquare(px, py, texStone);
    81.                 }
    82.                 else if(blocks[px, py] == 2)
    83.                 {
    84.                     GenSquare(px, py, texGrass);
    85.                 }
    86.             }
    87.         }
    88.     }
    89. }
    90.  
    91. function GenSquare (x : int, y : int, texture : Vector2)
    92. {
    93.     //This will create a simple square on the xy plane.
    94.     newVertices.Add(new Vector3(x, y, 0));    //This creates a vertice at the objects origin; the top-left
    95.     newVertices.Add(new Vector3(x + 1, y, 0));    //This creates a vertice on the top-right (i.e. +1 on the x axis which is left to right)
    96.     newVertices.Add(new Vector3(x + 1, y -1, 0));    //This creates the bottom-right vertice
    97.     newVertices.Add(new Vector3(x, y - 1, 0));    //This creates the last vertice on the bottom-left
    98.  
    99.     //Here we are connecting the vertices and making polygons, everything we are doing below is multiplied by square count so that a new square is made
    100.     //The polygons must be triangles (tris). Tris are made by adding the vertices that make it up counter-clockwise
    101.         //First 3 make up the triangle. They go: top-left to top-right to bottom-right. 3 points, decared in counter-clockwise order
    102.     newTriangles.Add(squareCount * 4);
    103.     newTriangles.Add((squareCount * 4) + 1);
    104.     newTriangles.Add((squareCount * 4) + 3);
    105.         //Second 3 up the bottom triangle. They go: top-right to bottom-right to bottom-left. 3 points, decared in counter-clockwise order
    106.     newTriangles.Add((squareCount * 4) + 1);
    107.     newTriangles.Add((squareCount * 4) + 2);
    108.     newTriangles.Add((squareCount * 4) + 3);
    109.  
    110.     newUV.Add(new Vector2(texUnit * texture.x, texUnit * texture.y + texUnit));
    111.     newUV.Add(new Vector2(texUnit * texture.x + texUnit, texUnit * texture.y + texUnit));
    112.     newUV.Add(new Vector2(texUnit * texture.x + texUnit, texUnit * texture.y));
    113.     newUV.Add(new Vector2(texUnit * texture.x, texUnit * texture.y));
    114.  
    115.     //Everytime the above sode is completed, we have succesfully created a new square, and there for need to raise the square count by one
    116.     squareCount++;
    117. }
    118.  
    119. function Block (x : int, y : int) : byte
    120. {
    121.     if(x == -1 || x == blocks.GetLength(0) || y == -1 || y == blocks.GetLength(1))
    122.     {
    123.         return (byte) 1;///////////////////////////////////////////////////////////////////////////////////HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    124.     }
    125.     return blocks[x,y];
    126. }
    127.  
    128. function GenCollider (x : int, y : int)
    129. {
    130.     //So we are doing about the same thing that the GenSquare function does, but instead of making flat squares this function creates squares that face left, right, up, and down
    131.     //We don't have to use Uvs because we are creating a collider and colliders aren't textured
    132.     //For optimization we will check everyside to see if there is another block next to it. If there isn't we'll make a collider, if there is we wont
    133.     //Top
    134.     if(Block(x,y + 1) == 0)
    135.     {
    136.         colVertices.Add(new Vector3(x, y, 1));
    137.         colVertices.Add(new Vector3(x + 1, y, 1));
    138.         colVertices.Add(new Vector3(x + 1, y, 0));
    139.         colVertices.Add(new Vector3(x, y, 0));
    140.  
    141.         ColliderTriangles();
    142.  
    143.         colCount++;
    144.     }
    145.     //Bottom
    146.     if(Block(x, y -1) == 0)
    147.     {
    148.         colVertices.Add(new Vector3(x, y -1, 0));
    149.         colVertices.Add(new Vector3(x + 1, y -1, 0));
    150.         colVertices.Add(new Vector3(x + 1, y -1, 1));
    151.         colVertices.Add(new Vector3(x, y -1, 1));
    152.  
    153.         ColliderTriangles();
    154.      
    155.         colCount++;
    156.     }
    157.     //Left
    158.     if(Block(x -1, y) == 0)
    159.     {
    160.         colVertices.Add(new Vector3(x, y -1, 1));
    161.         colVertices.Add(new Vector3(x, y, 1));
    162.         colVertices.Add(new Vector3(x, y, 0));
    163.         colVertices.Add(new Vector3(x, y -1, 0));
    164.  
    165.         ColliderTriangles();
    166.  
    167.         colCount++;
    168.     }
    169.     //Right
    170.     if(Block(x + 1, y) == 0)
    171.     {
    172.         colVertices.Add(new Vector3(x + 1, y, 1));
    173.         colVertices.Add(new Vector3(x + 1, y -1, 1));
    174.         colVertices.Add(new Vector3(x + 1, y -1, 0));
    175.         colVertices.Add(new Vector3(x + 1, y, 0));
    176.  
    177.         ColliderTriangles();
    178.    
    179.         colCount++;
    180.     }
    181. }
    182.  
    183. function ColliderTriangles ()
    184. {
    185.     colTriangles.Add(colCount * 4);
    186.     colTriangles.Add((colCount * 4) + 1);
    187.     colTriangles.Add((colCount * 4) + 3);
    188.     colTriangles.Add((colCount * 4) + 1);
    189.     colTriangles.Add((colCount * 4) + 2);
    190.     colTriangles.Add((colCount * 4) + 3);
    191. }
    192.  
    193.  
    194.  
    195. function UpdateMesh ()
    196. {
    197.     //Here we are creating a temporary mesh that we can set the collider equal to. Once the mesh is made, we apply it
    198.     var newMesh : Mesh = new Mesh();
    199.     newMesh.vertices = colVertices.ToArray();
    200.     newMesh.triangles = colTriangles.ToArray();
    201.     col.sharedMesh = newMesh;
    202.  
    203.     colVertices.Clear();
    204.     colTriangles.Clear();
    205.     colCount = 0;
    206.  
    207.     //Now we clear whatever mesh the MeshFilter is holding, to replace it with our own
    208.     mesh.Clear();
    209.     //For Unity to use or vertices(verts), and polygons/triangles(polys/tris), we have to convert the Lists that they are stored in to Arrays
    210.     //The reason is that Unity can't directly use them is unknown to me :) Arrays and Lists are similar so don't worry about it
    211.     mesh.vertices = newVertices.ToArray();
    212.     mesh.triangles = newTriangles.ToArray();
    213.     mesh.uv = newUV.ToArray();
    214.     //Now we call Optimize which probably won't do anything here, but it doesn't use up any processing so its good to throw in
    215.     mesh.Optimize();
    216.     //Using the RecalculateNormals command is basically going to make sure the normals are generated automatically. Thanks unity!
    217.     mesh.RecalculateNormals();
    218.     //Now we clear all the data so that the next time we make the mesh the count starts at 0 and so that we don't add on top of existing data
    219.     squareCount = 0;
    220.     newVertices.Clear();
    221.     newTriangles.Clear();
    222.     newUV.Clear();
    223. }
    her is the section of code in c#
    Code (csharp):
    1.  
    2. byte Block (int x, int y){
    3. if(x==-1 || x==blocks.GetLength(0) ||  y==-1 || y==blocks.GetLength(1)){return (byte)1;
    4. }
    5. return blocks[x,y];
    6. }
    Another thing I don't understand is making a variable into a function. What's up with that???
     
  2. lrlelaldl

    lrlelaldl

    Joined:
    Jul 27, 2014
    Posts:
    75
    Hey, not a JS person so can't help you out with that part specifically but here goes a bit of info:
    A byte, is a small unity of memory that is used to store information, it is equal to 8bits. a single bit is a unit of information that represents a single cell (on, off) or comonly referred to as 1 and 0.

    As for the "variable into a function" question. in C# you make functions that have a return operation, in this case the byte. So at th end of that function it is expected that you return a byte variable, this allows things like
    Code (PseudoCode):
    1. byte Bar(){return byte;}
    2. byte Foo = Bar();
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (javascript):
    1. Block (x : int, y : int) : byte {
    2.     if (x==-1 || x==blocks.GetLength(0) || y==-1 || y==blocks.GetLength(1)){
    3.         return 1;
    4.     }
    5.     return blocks[x,y];
    6. }
    --Eric
     
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Shouldn't it be function Block(), and not just Block().
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes, copy/paste error, sorry.

    --Eric
     
  6. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Yea I've been noticing copy/paste problems on this forum. It seems to just delete random words/characters when pasting.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Pretty sure that was just me not selecting the "function" part when copying. ;)

    --Eric
     
  8. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    597
    Thank you guys, this was very helpful
     
  9. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    597
    Ok, so this isn't completely relevant to the question title, but the script doesn't show any polys at all. I'm not sure why. I've checked through it a million times.(I've been commenting it to help me understand it better)
    Code (js):
    1.  
    2. import System.Collections.Generic;
    3.  
    4. @script AddComponentMenu ("Cool Tools/Random Generation/Polygon Generator")
    5. @script RequireComponent(MeshFilter)
    6. @script RequireComponent(MeshRenderer)
    7. @script RequireComponent(MeshCollider)
    8.     //Inspector Variables-----------------------------------------------------
    9. //The two variables below let us change how many blocks are created by feeding their values into the byte array
    10. var xAmount : int = 10;
    11. var yAmount : int = 10;
    12. //This is going to store all of the vertices of the mesh we are going to render
    13. var newVertices : List.<Vector3> = new List.<Vector3>();
    14. var colVertices : List.<Vector3> = new List.<Vector3>();
    15.  
    16. //The triangles tell Unity how to build the each part of the mesh that connects vertices
    17. var newTriangles : List.<int> = new List.<int>();
    18. var colTriangles : List.<int> = new List.<int>();
    19.  
    20. //The UV list tells Unity how to display/position the texture on each polygon
    21. var newUV : List.<Vector2> = new List.<Vector2>();
    22.  
    23. //Here we will store all of the different block types/information. For example, 0 could be air, 1 grass, and 2 rock
    24. var blocks : byte[,];
    25.  
    26.     //Hidden Variables--------------------------------------------------------
    27. //The mesh will be made up the vertices, polys, and UVs that we define. After we make them, we'll apply them to this mesh
    28. private var mesh : Mesh;
    29. private var col : MeshCollider;
    30.  
    31. //The texture applied to this objects mesh has different colored tiles to represent things like grass, dirt, rock etc.
    32. //Here we define a float that will be changed in the Start function
    33. //The float will represent the percentage of the textures width that one tile takes up. We will call that percentage a texUnit
    34. private var texUnit : float = 0.25;    //The texture has 16 tiles, 4 wide, 4 tall. One tile is a fourth, or takes up 0.25 or the width
    35. private var texStone : Vector2 = new Vector2(1, 0);    //Here we are defining the texture coordinates for the stone tile portion of the texture
    36. private var texGrass : Vector2 = new Vector2(0, 1);    //Here we are defining the texture coordinates for the grass tile portion of the texture
    37.  
    38. //This variable is important. It lets us know how many squares we have created so that
    39. //For instance, when we need to access the meshes 1046 vertice we can do so easily by knowing which square we're on and adding the ints to the triangles
    40. private var squareCount : int;
    41. private var colCount : int;
    42.  
    43. function Start ()
    44. {
    45.     mesh = GetComponent(MeshFilter).mesh;
    46.     col = GetComponent(MeshCollider);
    47.  
    48.     GenTerrain();
    49.     BuildMesh();
    50.     UpdateMesh();
    51. }
    52.  
    53. function GenTerrain ()
    54. {
    55.     //This makes a sets the array of blocks' size to 10x10. The actual blocks will be built in a seperate function
    56.     blocks = new byte[xAmount, yAmount];
    57.     for(var px : int = 0; px < blocks.GetLength(0); px++)//px will stand for the blocks that need to be generated on the x-axis
    58.     {
    59.         for(var py : int = 0; py < blocks.GetLength(1); py++)//py will stand for the blocks that need to be generated on the y-axis
    60.         {
    61.             if(py == 5)
    62.             {
    63.                 blocks[px, py] = 2;
    64.             }
    65.             else if (py < 5)
    66.             {
    67.                 blocks[px, py] = 1;
    68.             }
    69.             if(px == 5)
    70.             {
    71.                 blocks[px, py] = 0;
    72.             }
    73.         }
    74.     }
    75. }
    76.  
    77. function BuildMesh ()
    78. {
    79.     for (var px : int = 0; px < blocks.GetLength(0); px++)
    80.     {
    81.         for (var py : int = 0; py < blocks.GetLength(1); py++)
    82.         {
    83.             //If the block isn't air--
    84.             if(blocks[px, py] != 0)
    85.             {
    86.                 //--We call the GenCollider function to generate a collider for each square at the right place
    87.                 GenCollider(px, py);
    88.             }
    89.             else if(blocks[px, py] == 2)
    90.             {
    91.                 GenSquare(px, py, texGrass);
    92.             }
    93.         }
    94.     }
    95. }
    96.  
    97. function Block (x : int, y : int) : byte
    98. {
    99.     if(x == -1 || x == blocks.GetLength(0) || y == -1 || y == blocks.GetLength(1))
    100.     {
    101.         return 1;
    102.     }
    103.     return blocks[x, y];
    104. }
    105.  
    106. //This is the function where we generate the necessary colliders for each block when we update the mesh
    107. function GenCollider (x : int, y : int)
    108. {
    109.     //Top Face
    110.     if(Block(x, y + 1) == 0)
    111.     {
    112.         //The next 13 lines are the doing the same things that the GenSquare function does, but we are'nt making this square on the xy plane
    113.         //This square is the top sqaure of our shape. As you can see it has a value of 1 on the first two vertices in the z axis. This gives the shape depth
    114.         colVertices.Add(new Vector3(x, y, 1));//Back-left vertice
    115.         colVertices.Add(new Vector3(x + 1, y, 1));//Back-right vertice
    116.         colVertices.Add(new Vector3(x + 1, y, 0));//Front-right vertice
    117.         colVertices.Add(new Vector3(x, y, 0));//Front-left vertice
    118.         ColliderTriangles ();//Instead of typing out colTriangles.Add(blahblah) over and over for every face, we can put that code into a function and call it for every face
    119.         colCount++;
    120.     }
    121.     //Bottom Face
    122.     if(Block(x, y - 1) == 0)
    123.     {
    124.         colVertices.Add(new Vector3(x, y - 1, 0));
    125.         colVertices.Add(new Vector3(x + 1, y - 1, 0));
    126.         colVertices.Add(new Vector3(x + 1, y - 1, 1));
    127.         colVertices.Add(new Vector3(x, y - 1, 1));
    128.         ColliderTriangles();
    129.         colCount++;
    130.     }
    131.     //Left Face
    132.     if(Block(x - 1, y) == 0)
    133.     {
    134.         colVertices.Add(new Vector3(x, y - 1, 1));
    135.         colVertices.Add(new Vector3(x, y, 1));
    136.         colVertices.Add(new Vector3(x, y, 0));
    137.         colVertices.Add(new Vector3(x, y - 1, 0));
    138.         ColliderTriangles();
    139.         colCount++;
    140.     }
    141.     //Right Face
    142.     if(Block(x + 1, y) == 0)
    143.     {
    144.         colVertices.Add(new Vector3(x + 1, y, 1));
    145.         colVertices.Add(new Vector3(x + 1, y - 1, 1));
    146.         colVertices.Add(new Vector3(x + 1, y - 1, 0));
    147.         colVertices.Add(new Vector3(x + 1, y, 0));
    148.         ColliderTriangles();
    149.         colCount++;
    150.     }
    151. }
    152.  
    153. function ColliderTriangles ()
    154. {
    155.     colTriangles.Add(colCount * 4);
    156.     colTriangles.Add((colCount * 4) + 1);
    157.     colTriangles.Add((colCount * 4) + 3);
    158.     colTriangles.Add((colCount * 4) + 1);
    159.     colTriangles.Add((colCount * 4) + 2);
    160.     colTriangles.Add((colCount * 4) + 3);
    161. }
    162.  
    163. function GenSquare (x : int, y : int, texture : Vector2)
    164. {
    165.     //This will create a simple square on the xy plane.
    166.     newVertices.Add(new Vector3(x, y, 0));//This creates a vertice at the objects origin; the top-left
    167.     newVertices.Add(new Vector3(x + 1, y, 0));//This creates a vertice on the top-right (i.e. +1 on the x axis which is left to right)
    168.     newVertices.Add(new Vector3(x + 1, y - 1, 0));//This creates the bottom-right vertice
    169.     newVertices.Add(new Vector3(x, y - 1, 0));//This creates the last vertice on the bottom-left
    170.  
    171.     //Here we are connecting the vertices and making polygons, everything we are doing below is multiplied by square count so that a new square is made
    172.     //The polygons must be triangles (tris). Tris are made by adding the vertices that make it up counter-clockwise
    173.         //First 3 make up the triangle. They go: top-left to top-right to bottom-right. 3 points, decared in counter-clockwise order
    174.     newTriangles.Add(squareCount * 4);
    175.     newTriangles.Add((squareCount * 4) + 1);
    176.     newTriangles.Add((squareCount * 4) + 3);
    177.         //Second 3 up the bottom triangle. They go: top-right to bottom-right to bottom-left. 3 points, decared in counter-clockwise order
    178.     newTriangles.Add((squareCount * 4) + 1);
    179.     newTriangles.Add((squareCount * 4) + 2);
    180.     newTriangles.Add((squareCount * 4)  + 3);
    181.  
    182.     newUV.Add(new Vector2(texUnit * texture.x, texUnit * texture.y + texUnit));
    183.     newUV.Add(new Vector2(texUnit * texture.x + texUnit, texUnit * texture.y + texUnit));
    184.     newUV.Add(new Vector2(texUnit * texture.x + texUnit, texUnit * texture.y));
    185.     newUV.Add(new Vector2(texUnit * texture.x, texUnit * texture.y));
    186.  
    187.     //Everytime the above sode is completed, we have succesfully created a new square, and there for need to raise the square count by one
    188.     squareCount++;
    189. }
    190.  
    191. function UpdateMesh ()
    192. {
    193.     //Now we clear whatever mesh the MeshFilter is holding, to replace it with our own
    194.     mesh.Clear();
    195.     //For Unity to use or vertices(verts), and polygons/triangles(polys/tris), we have to convert the Lists that they are stored in to Arrays
    196.     //The reason is that Unity can't directly use them is unknown to me :) Arrays and Lists are similar so don't worry about it
    197.     mesh.vertices = newVertices.ToArray();
    198.     mesh.triangles = newTriangles.ToArray();
    199.     mesh.uv = newUV.ToArray();
    200.     //Now we call Optimize which probably won't do anything here, but it doesn't use up any processing so its good to throw in
    201.     mesh.Optimize();
    202.     //Using the RecalculateNormals command is basically going to make sure the normals are generated automatically. Thanks unity!
    203.     mesh.RecalculateNormals();
    204.     newVertices.Clear();
    205.     newTriangles.Clear();
    206.     newUV.Clear();
    207.     //Now we clear all the data so that the next time we make the mesh the count starts at 0 and so that we don't add on top of existing data
    208.     squareCount = 0;
    209.  
    210.     //Here we are creating a temporary mesh that we can make a collider out of. This is done very similarly to the way we create the visible mesh
    211.     var newMesh : Mesh = new Mesh();
    212.     newMesh.vertices = colVertices.ToArray();
    213.     newMesh.triangles = colTriangles.ToArray();
    214.     col.sharedMesh = newMesh;
    215.     colVertices.Clear();
    216.     colTriangles.Clear();
    217.     colCount = 0;
    218. }
    219. }
    Here's the c# version
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class PolygonGenerator : MonoBehaviour {
    7.  public List<Vector3> newVertices = new List<Vector3>();public List<int> newTriangles = new List<int>();public List<Vector2> newUV = new List<Vector2>(); public List<Vector3> colVertices = new List<Vector3>();public List<int> colTriangles = new List<int>();private int colCount; private Mesh mesh;private MeshCollider col; private float tUnit = 0.25f;private Vector2 tStone = new Vector2 (1, 0);private Vector2 tGrass = new Vector2 (0, 1);  public byte[,] blocks;private int squareCount;   // Use this for initializationvoid Start () {mesh = GetComponent<MeshFilter> ().mesh;col = GetComponent<MeshCollider> (); GenTerrain();BuildMesh();UpdateMesh();} void Update(){  } void GenTerrain(){blocks=new byte[10,10]; for(int px=0;px<blocks.GetLength(0);px++){for(int py=0;py<blocks.GetLength(1);py++){if(py==5){blocks[px,py]=2;} else if(py<5){blocks[px,py]=1;} if(px==5){blocks[px,py]=0;}}}} void BuildMesh(){for(int px=0;px<blocks.GetLength(0);px++){for(int py=0;py<blocks.GetLength(1);py++){ if(blocks[px,py]!=0){ GenCollider(px,py); if(blocks[px,py]==1){GenSquare(px,py,tStone);} else if(blocks[px,py]==2){GenSquare(px,py,tGrass);}}}}} byte Block (int x, int y){ if(x==-1 || x==blocks.GetLength(0) || y==-1 || y==blocks.GetLength(1)){return (byte)1;} return blocks[x,y];} void GenCollider(int x, int y){ //Topif(Block(x,y+1)==0){colVertices.Add( new Vector3 (x  , y  , 1));
    8.  colVertices.Add( new Vector3 (x + 1 , y  , 1));
    9.  colVertices.Add( new Vector3 (x + 1 , y  , 0 ));
    10.  colVertices.Add( new Vector3 (x  , y  , 0 ));
    11.  ColliderTriangles();
    12.  colCount++;
    13.  }
    14.  //bot
    15.  if(Block(x,y-1)==0){
    16.  colVertices.Add( new Vector3 (x  , y -1 , 0));
    17.  colVertices.Add( new Vector3 (x + 1 , y -1 , 0));
    18.  colVertices.Add( new Vector3 (x + 1 , y -1 , 1 ));
    19.  colVertices.Add( new Vector3 (x  , y -1 , 1 ));
    20.  ColliderTriangles();
    21.  colCount++;
    22.  }
    23.  //left
    24.  if(Block(x-1,y)==0){
    25.  colVertices.Add( new Vector3 (x  , y -1 , 1));
    26.  colVertices.Add( new Vector3 (x  , y  , 1));
    27.  colVertices.Add( new Vector3 (x  , y  , 0 ));
    28.  colVertices.Add( new Vector3 (x  , y -1 , 0 ));
    29.  ColliderTriangles();
    30.  colCount++;
    31.  }
    32.  //right
    33.  if(Block(x+1,y)==0){
    34.  colVertices.Add( new Vector3 (x +1 , y  , 1));
    35.  colVertices.Add( new Vector3 (x +1 , y -1 , 1));
    36.  colVertices.Add( new Vector3 (x +1 , y -1 , 0 ));
    37.  colVertices.Add( new Vector3 (x +1 , y  , 0 ));
    38.  ColliderTriangles();
    39.  colCount++;
    40.  }
    41.  }
    42.  void ColliderTriangles(){
    43.  colTriangles.Add(colCount*4);
    44.  colTriangles.Add((colCount*4)+1);
    45.  colTriangles.Add((colCount*4)+3);
    46.  colTriangles.Add((colCount*4)+1);
    47.  colTriangles.Add((colCount*4)+2);
    48.  colTriangles.Add((colCount*4)+3);
    49.  }
    50.  void GenSquare(int x, int y, Vector2 texture){
    51.  newVertices.Add( new Vector3 (x  , y  , 0 ));
    52.  newVertices.Add( new Vector3 (x + 1 , y  , 0 ));
    53.  newVertices.Add( new Vector3 (x + 1 , y-1 , 0 ));
    54.  newVertices.Add( new Vector3 (x  , y-1 , 0 ));
    55.  newTriangles.Add(squareCount*4);
    56.  newTriangles.Add((squareCount*4)+1);
    57.  newTriangles.Add((squareCount*4)+3);
    58.  newTriangles.Add((squareCount*4)+1);
    59.  newTriangles.Add((squareCount*4)+2);
    60.  newTriangles.Add((squareCount*4)+3);
    61.  newUV.Add(new Vector2 (tUnit * texture.x, tUnit * texture.y + tUnit));
    62.  newUV.Add(new Vector2 (tUnit * texture.x + tUnit, tUnit * texture.y + tUnit));
    63.  newUV.Add(new Vector2 (tUnit * texture.x + tUnit, tUnit * texture.y));
    64.  newUV.Add(new Vector2 (tUnit * texture.x, tUnit * texture.y));
    65.  squareCount++;
    66.  }
    67.  void UpdateMesh () {
    68.  mesh.Clear ();
    69.  mesh.vertices = newVertices.ToArray();
    70.  mesh.triangles = newTriangles.ToArray();
    71.  mesh.uv = newUV.ToArray();
    72.  mesh.Optimize ();
    73.  mesh.RecalculateNormals ();
    74.  newVertices.Clear();
    75.  newTriangles.Clear();
    76.  newUV.Clear();
    77.  squareCount=0;
    78.  Mesh newMesh = new Mesh();
    79.  newMesh.vertices = colVertices.ToArray();
    80.  newMesh.triangles = colTriangles.ToArray();
    81.  col.sharedMesh= newMesh;
    82.  colVertices.Clear();
    83.  colTriangles.Clear();
    84.  colCount=0;
    85.  }
    86. }
     
    Last edited: Aug 1, 2014
  10. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    597
    found it. I had my buildmesh function wrong
    Code (js):
    1.  
    2. function BuildMesh ()
    3. {
    4.     for (var px : int = 0; px < blocks.GetLength(0); px++)
    5.     {
    6.         for (var py : int = 0; py < blocks.GetLength(1); py++)
    7.         {
    8.             //If the block isn't air--
    9.             if(blocks[px, py] != 0)
    10.             {
    11.                 //--We call the GenCollider function to generate a collider for each square at the right place
    12.                 GenCollider(px, py);
    13.                 if(blocks[px, py] == 1)
    14.                 {
    15.                       GenSquare(px, py, texStone);
    16.                 }
    17.                 else if(blocks[px, py] == 2)
    18.                 {
    19.                     GenSquare(px, py, texGrass);
    20.                 }
    21.             }
    22.         }
    23.     }
    24. }