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

Draw Polygon

Discussion in 'Scripting' started by AlexG, Jul 12, 2010.

  1. AlexG

    AlexG

    Joined:
    Jul 7, 2010
    Posts:
    45
    Hey there,

    I would like unity to draw a 3D polygon during run-time based on the location of some Vector3 vertices

    I want the entire polygon to be filled (no wireframe).

    For example, the following would draw a square:

    Code (csharp):
    1.  
    2. var theVertices = new Array();
    3.  
    4. theVertices.Push(Vector3((0,0,0));
    5. theVertices.Push(Vector3((1,0,0));
    6. theVertices.Push(Vector3((1,1,0));
    7. theVertices.Push(Vector3((0,1,0));
    8.  
    9. DrawPoly(theVertices);
    10.  
    Suggestions?
     
    folino9 likes this.
  2. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
  3. AlexG

    AlexG

    Joined:
    Jul 7, 2010
    Posts:
    45
    Thanks for the reply.

    It looks like I have to create a new mesh. The example they provide:

    Code (csharp):
    1.  
    2. function Start  ()
    3. {
    4.      var mesh = new Mesh ();
    5.      GetComponent (MeshFilter).mesh = mesh;
    6.      mesh.vertices = newVertices;
    7.      mesh.uv = newUV;
    8.      mesh.triangles = newTriangles;
    9. }
    10.  
    I'm assuming that newVerticies, newUV, and newTriangles are all the arrays of data that I would have offhand. I'm then assigning this data to the mesh. Makes sense. Some questions:

    1) I'm looking at a function, Graphics.DrawMesh(). I can use this to draw the mesh I create without having to create a game object right?

    2A) Can I also assign this mesh to a new game object and have it draw it for me automatically? Something like this?:

    Code (csharp):
    1.  
    2. var mesh : Mesh;
    3. var gameObject : GameObject;
    4.  
    5. CreateMesh()
    6. {
    7.    var meshCreated : Mesh = new Mesh();
    8.    ///blah blah blah, create some mesh
    9.    return meshCreated
    10. }
    11.  
    12. mesh = CreateMesh();
    13.  
    14. gameObject.AddComponent("MeshFilter");
    15. gameObject.AddComponent("MeshRenderer");
    16.  
    17. gameObject.MeshFilter.mesh = mesh;
    18.  
    2B) How can I change the material for this game object? Something like this?:

    Code (csharp):
    1.  
    2. var myMaterial : Material; //Drag and drop a material onto the inspector
    3.  
    4. gameObject.MeshRenderer.Materials[0] = myMaterial;
    5.  
    Thanks,
    Alex
     
  4. AlexG

    AlexG

    Joined:
    Jul 7, 2010
    Posts:
    45
    Got it working thanks.

    If anyone is curious, here is my code

    Code (csharp):
    1.  
    2. function updatePolygon()
    3. {
    4.     for(x = 0; x < 2; x++)
    5.     {
    6.         //Destroy old game object
    7.         Destroy(myObject[x]);
    8.  
    9.         //New mesh and game object
    10.         myObject[x] = new GameObject();
    11.         myObject[x].name = "MousePolygon";
    12.         var mesh : Mesh = new Mesh();
    13.        
    14.         //Components
    15.         var MF = myObject[x].AddComponent("MeshFilter");
    16.         var MR = myObject[x].AddComponent("MeshRenderer");
    17.         //myObject[x].AddComponent();
    18.        
    19.         //Create mesh
    20.         mesh = CreateMesh(x);
    21.        
    22.         //Assign materials
    23.         MR.material = myMaterial;
    24.        
    25.         //Assign mesh to game object
    26.         MF.mesh = mesh;
    27.     }
    28. }
    29.  
    Code (csharp):
    1.  
    2. function CreateMesh(num : int)
    3. {
    4.     var x : int; //Counter
    5.  
    6.     //Create a new mesh
    7.     var mesh : Mesh = new Mesh();
    8.    
    9.     //Vertices
    10.     var vertex = new Vector3[nodePositions.length];
    11.    
    12.     for(x = 0; x < nodePositions.length; x++)
    13.     {
    14.         vertex[x] = nodePositions[x];
    15.     }
    16.    
    17.     //UVs
    18.     var uvs = new Vector2[vertex.length];
    19.    
    20.     for(x = 0; x < vertex.length; x++)
    21.     {
    22.         if((x%2) == 0)
    23.         {
    24.             uvs[x] = Vector2(0,0);
    25.         }
    26.         else
    27.         {
    28.             uvs[x] = Vector2(1,1);
    29.         }
    30.     }
    31.    
    32.     //Triangles
    33.     var tris = new int[3 * (vertex.length - 2)];    //3 verts per triangle * num triangles
    34.     var C1 : int;
    35.     var C2 : int;
    36.     var C3 : int;
    37.    
    38.     if(num == 0)
    39.     {
    40.         C1 = 0;
    41.         C2 = 1;
    42.         C3 = 2;
    43.        
    44.         for(x = 0; x < tris.length; x+=3)
    45.         {
    46.             tris[x] = C1;
    47.             tris[x+1] = C2;
    48.             tris[x+2] = C3;
    49.            
    50.             C2++;
    51.             C3++;
    52.         }
    53.     }
    54.     else
    55.     {
    56.         C1 = 0;
    57.         C2 = vertex.length - 1;
    58.         C3 = vertex.length - 2;
    59.        
    60.         for(x = 0; x < tris.length; x+=3)
    61.         {
    62.             tris[x] = C1;
    63.             tris[x+1] = C2;
    64.             tris[x+2] = C3;
    65.            
    66.             C2--;
    67.             C3--;
    68.         }  
    69.     }
    70.    
    71.     //Assign data to mesh
    72.     mesh.vertices = vertex;
    73.     mesh.uv = uvs;
    74.     mesh.triangles = tris;
    75.    
    76.     //Recalculations
    77.     mesh.RecalculateNormals();
    78.     mesh.RecalculateBounds();  
    79.     mesh.Optimize();
    80.    
    81.     //Name the mesh
    82.     mesh.name = "MyMesh";
    83.    
    84.     //Return the mesh
    85.     return mesh;
    86. }
    87.  
    This creates a mesh and attaches it to two game objects. The mesh is stitched counter-clockwise in one object, and clockwise in another game object. Then both objects are drawn on top of each other. The reason I do this is so that way the object is visible on the front and back.
     
  5. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    Glad to see you got that working :) Just out of curiosity, why do you destroy the game objects each update, rather than just using go.GetComponent("MeshFilter").mesh.Clear()?
     
    sunwangshu and pachermann like this.
  6. AlexG

    AlexG

    Joined:
    Jul 7, 2010
    Posts:
    45
    Cause im bad!

    Thanks for the pro tip
     
  7. rule62

    rule62

    Joined:
    Feb 14, 2011
    Posts:
    2
    Hi Alex,

    How did you actually get this to work? How to set up a scene without gameobjects to start with?

    Where does the data input into these scripts come from?

    thanks!
     
  8. HabibAli

    HabibAli

    Joined:
    Aug 19, 2013
    Posts:
    3
    Any one has a sample project for drawing polygon please?
     
  9. ciaccodavide

    ciaccodavide

    Joined:
    Jun 27, 2013
    Posts:
    1
    unity_96aman96, SerenityTn and Ryiah like this.
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    I'm torn between complimenting what appears to be a useful and free plugin, or blasting you for necroposting just to spam your own plugin...
     
    MaxEden likes this.
  11. unity_96aman96

    unity_96aman96

    Joined:
    Nov 3, 2018
    Posts:
    2
    ErnieTheMighty likes this.