Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Procedural Grid; Circle

Discussion in 'Scripting' started by dragonice501, Feb 26, 2016.

  1. dragonice501

    dragonice501

    Joined:
    Dec 2, 2015
    Posts:
    146
    I have the code to create a grid of squares with any given length or width, but what if i want to create a circle instead of a rectangle or square and have it start at a certain point rather than the world origin? (See attached image for a better idea)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
    5. public class Grid : MonoBehaviour {
    6.  
    7.     public int xSize, zSize;
    8.  
    9.     private Vector3[] vertices;
    10.  
    11.     private Mesh mesh;
    12.  
    13.     private void Awake ()
    14.     {
    15.         Generate();
    16.     }
    17.  
    18.     private void Generate ()
    19.     {
    20.         GetComponent<MeshFilter>().mesh = mesh = new Mesh();
    21.         mesh.name = "Procedural Grid";
    22.  
    23.         vertices = new Vector3[(xSize + 1) * (zSize + 1)];
    24.         for (int i = 0, z = 0; z <= zSize; z++)
    25.             for (int x = 0; x <= xSize; x++, i++)
    26.                 vertices[i] = new Vector3(x, 0, z);
    27.        
    28.         mesh.vertices = vertices;
    29.  
    30.         int[] triangles = new int[xSize * zSize * 6];
    31.         for (int ti = 0, vi = 0, y = 0; y < zSize; y++, vi++)
    32.             for (int x = 0; x < xSize; x++, ti += 6, vi++)
    33.             {
    34.                 triangles[ti] = vi;
    35.                 triangles[ti + 3] = triangles[ti + 2] = vi + 1;
    36.                 triangles[ti + 4] = triangles[ti + 1] = vi + xSize + 1;
    37.                 triangles[ti + 5] = vi + xSize + 2;
    38.             }
    39.         mesh.triangles = triangles;
    40.         mesh.RecalculateNormals ();
    41.     }
    42. }
    43.  
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,370
    From your blow-up image, it looks like you want to simply apply a "yes/no" answer at each cell location as you build your normal square grid.

    To do that, at the point in your inner loop when you make the two tris, just ask "Is the centroid of the vertices referred to in these tris too far away from my centerpoint?" and then don't make the tris.

    As a second step of optimization, you could go back and reprocess the vert list to only include verts that are referenced in the tris, and then obviously fixup up the tris list so they point to the new vert offsets after the optimization.
     
  3. dragonice501

    dragonice501

    Joined:
    Dec 2, 2015
    Posts:
    146
    Could there be a away to change the pattern in which the triangles are generate? Instead of to the right one at a time, they wrap around each other in a spiral motion?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,370
    You could lay them out like a pie, pizza wedges... that's traditionally how a circle is generated.

    Make one vert at the center, then N verts around the edge, and each triangle is:

    vert 0
    vert 1 + x
    vert 1 + ((x + 1) % N)

    ... for each x from 0 to N - 1.

    If you have enough wedges, it's gonna be pretty round.
     
  5. joshmcewin

    joshmcewin

    Joined:
    Sep 17, 2019
    Posts:
    1
    this will make it make a circle but it goes left to right
    Code (CSharp):
    1.  
    2. Vector2 center = new Vector2(0,0);
    3. int radius = 4;
    4. int radiusSquared = radius * radius;    
    5.         for (int x = -radius; x <= radius; ++x)
    6.         {
    7.             for (int y = -radius; y <= radius; ++y)
    8.             {              
    9.                 //if within radius
    10.                 if (new Vector2(x, y).sqrMagnitude <= radiusSquared)
    11.                 {
    12.                     // make object at Vector2(center.x + x, center.y + y)
    13.                 }
    14.             }
    15.         }
    sorry for Bumping this.
     
    StartStart likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,370
    Here is how to report problems correctly in the Unity3D forums:

    http://plbm.com/?p=220
     
  7. Shiv2k3

    Shiv2k3

    Joined:
    Jun 30, 2019
    Posts:
    112
    Could you please explain this in more detail please?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,370
  9. Shiv2k3

    Shiv2k3

    Joined:
    Jun 30, 2019
    Posts:
    112
    Hey where do I add that wedge code? I am really confused on how you would do that could you please tell me? This is my current code, all its doing is creating a list of vertices and than only adding triangles at each vertex if the vertex's distance is less than .5 from the vec3.zero.

    Code (CSharp):
    1.     private void Plane()
    2.     {
    3.         vert = 0;
    4.         for (float z = 0; z < res; z++)
    5.         {
    6.             for (float x = 0; x < res; x++)
    7.             {
    8.                 Vector3 top = new Vector3(x - res / 2, 0, z - res / 2) / (res - 1);
    9.  
    10.                 topVerts.Add(top);
    11.  
    12.                 if (z != res - 1 && x != res - 1)
    13.                 {
    14.                     if (Vector3.Distance(top, Vector3.zero) < 0.5f)
    15.                     {
    16.                         topTris.Add(vert);
    17.                         topTris.Add(vert + res);
    18.                         topTris.Add(vert + res + 1);
    19.  
    20.                         topTris.Add(vert + res + 1);
    21.                         topTris.Add(vert + 1);
    22.                         topTris.Add(vert);
    23.  
    24.                     }
    25.                 }
    26.  
    27.                 vert++;
    28.             }
    29.         }
    30.  
    31.     }
    32.  
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,370
    I really don't know how to answer that because I have no idea what you are even doing.

    I suppose the answer is to add it where it gives you the effect you want. It's just vertices and triangles.
     
  11. Shiv2k3

    Shiv2k3

    Joined:
    Jun 30, 2019
    Posts:
    112
    I mean when I use that code I posted above it just creates a circle with hard edges, but I want it to be smooth around the edge so how do I implement your little bit of code into mine
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,370
    The code I linked also creates a circle with hard edges.

    Screen Shot 2021-07-06 at 1.35.18 PM.png

    You might want to just create the desired soft-edge circle with Photoshop and then import it as a sprite and use that directly.