Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Procedural Cube Generation

Discussion in 'Scripting' started by eevielution, Jul 9, 2012.

  1. eevielution

    eevielution

    Joined:
    Dec 21, 2010
    Posts:
    26
    I have a piece of code that I have been putting together thanks to other thread posts, and it draws a 1x1x1 cube. I am planning to create a randomly generated terrain with this, so I'm trying to keep it as optimized as possible.

    Code (csharp):
    1. var newVertices : Vector3[];
    2. var newUV : Vector2[];
    3. var newTriangles : int[];
    4. var cubePosition : Vector3 = Vector3(1,1,1);
    5. //var material : Texture2D;
    6.  
    7. function Start () {
    8.     DrawCube();
    9. }
    10.  
    11. function DrawCube () {
    12.     var cubeX=cubePosition.x;
    13.     var cubeY=cubePosition.y;
    14.     var cubeZ=cubePosition.z;
    15.    
    16.     var mesh : Mesh = new Mesh ();
    17.     GetComponent(MeshFilter).mesh = mesh;
    18.     mesh.Clear();
    19.     //Actual creation.
    20.     var p1 = Vector3(0,0,0);
    21.     var p2 = Vector3(cubeX,0,0);
    22.     var p3 = Vector3(0,cubeY,0);
    23.     var p4 = Vector3(0,0,cubeZ);
    24.     var p5 = Vector3(cubeX,0,cubeZ);
    25.     var p6 = Vector3(cubeX,cubeY,0);
    26.     var p7 = Vector3(0,cubeY,cubeZ);
    27.     var p8 = Vector3(cubeX,cubeY,cubeZ);
    28.    
    29.     newVertices = new Array (p1,p2,p3,p4,p5,p6,p7,p8);
    30.    
    31.     newTriangles = [
    32.     0,2,1, 1,2,5,
    33.    
    34.     3,0,1, 1,4,3,
    35.    
    36.     0,3,2, 2,3,6,
    37.    
    38.     1,5,4, 5,7,4,
    39.    
    40.     6,3,4, 6,4,7,
    41.    
    42.     6,5,2, 7,5,6
    43.     ];
    44.    
    45.     mesh.vertices = newVertices;
    46.     mesh.triangles = newTriangles;
    47.     mesh.uv = newUV;
    48.     mesh.RecalculateNormals();  
    49.     mesh.RecalculateBounds();  
    50.     mesh.Optimize();
    51.     renderer.material.color = Color.gray;
    52. }
    My issue with the code is, how do I have it generate separate cubes every time I run the function? Do I put them all in different GameObjects? (I expect this to be slow and a waste of space) If I, say, run it again with a different cubePosition variable set, it just extends the cube until it reaches the new Vector3 coordinates. Any ideas?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,503
  3. snottlebocket

    snottlebocket

    Joined:
    Feb 6, 2010
    Posts:
    18
    I've been playing around with the same thing. So far I'm kind of stuck on the vertices. Every piece of material I read states that each corner needs 3 separate vertices because the tri's want unique verts. But nobody actually makes 3 verts per corner.

    As for your question. This article suggests using the combinechildren function for optimalization. It does have a few caveats though.
     
  4. eevielution

    eevielution

    Joined:
    Dec 21, 2010
    Posts:
    26
    Thanks guys, some very useful links from mgear :)

    However, I don't understand how I would go about making many cubes? Would I make this cube into a class for reusability? When I re-run the function now, like I said it just extends the existing cube mesh to be larger.
     
  5. snottlebocket

    snottlebocket

    Joined:
    Feb 6, 2010
    Posts:
    18
    Not sure if this would work in unity but in most languages I'd just create a function that builds a cube in a temporary variable and returns that object.

    Then just use a loop to repeat that function while stuffing the return object into an array.
     
  6. eevielution

    eevielution

    Joined:
    Dec 21, 2010
    Posts:
    26
    Okay, I got all of the different cubes going into separate GameObjects (not sure how else to do it) and a 10x10x10 group of cubes here. All is well and good so far, but the shading is acting funny, I am eventually aiming to do a gradually shaded ground that changes shades of green (no texture, just green) depending on location, and this doesn't sell that look well. Any tips on fixing this ugly shading?
    Photo Here
     
  7. rstehwien

    rstehwien

    Joined:
    Dec 30, 2007
    Posts:
    101
  8. Ted-Chirvasiu

    Ted-Chirvasiu

    Joined:
    Sep 7, 2010
    Posts:
    381
    Unity uses Per-Vertex shading - That means that the normals will actually be counted per vertex,not per triangle.

    Make a simple Unity cube.You'll see that it doesn't have only 8 verts.It has 24.That's because the cube is made out of 6 planes that are not bound together.
    That's exacly what you want to do.Imagine that you would procedurally build 6 planes in the shape of a cube.That way you will have rough edges.

    The other way is to use per-triangle shading,but i'm not sure how to do it.You might need to mess around with shaders as well if you want to keep the low vert count.

    Good luck!