Search Unity

draw polygons script

Discussion in 'Scripting' started by chubbspet, Jul 19, 2012.

  1. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    Hi there guys. I got the script below from a forum post that I (unfortunetally) cant remember where, but thanks to the author. I converted it to C# and it works fine, just attach it to a gameObject that has a MeshFilter. My question is this: This script draws all the lines including the diagonal lines to create triangles in stead of four-sided polys and that is what I need - think about the typical way 3DS Max would draw an editable poly. Can someone see a way to mod this code to avoid it from drawing the diagonal lines? Any help will be appreciated!

    Here is the C# version, feel free to use it!

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class wire_frame : MonoBehaviour {
    6.  
    7.     Vector3 P0 ;
    8.     Vector3 P1 ;
    9.     Vector3 P2 ;
    10.  
    11.     List<Vector3> wires = new List<Vector3>();
    12.  
    13.     Color lineColor ;
    14.     public GameObject myMesh;
    15.  
    16.     static Material lineMaterial;
    17.  
    18.     void Start ()
    19.     {
    20.        
    21.         CreateLineMaterial();
    22.  
    23.         MeshFilter filter = myMesh.GetComponent<MeshFilter>();
    24.         var mesh = filter.mesh;
    25.         var vertices = mesh.vertices;
    26.         var triangles = mesh.triangles;
    27.  
    28.         print(vertices.Length);
    29.    
    30.         for (int k = 0; k < triangles.Length / 3; k++)
    31.         {
    32.             wires.Add (vertices[triangles[k * 3]]);
    33.             wires.Add (vertices[triangles[k * 3 + 1]]);
    34.             wires.Add (vertices[triangles[k * 3 + 2]]);
    35.         }
    36.    
    37.         wires.Add (vertices[triangles[triangles.Length - 2]]);
    38.         wires.Add (vertices[triangles[triangles.Length - 1]]);
    39.     }
    40.  
    41.     void OnPostRender()
    42.     {  
    43.         lineMaterial.SetPass(0);
    44.    
    45.         GL.Begin(GL.LINES);
    46.         GL.Color(lineColor);
    47.    
    48.         for (int i = 0; i < wires.Count / 3; i++)
    49.         {
    50.             P0 = myMesh.transform.TransformPoint (wires[i * 3]);
    51.             P1 = myMesh.transform.TransformPoint (wires[i * 3 + 1]);
    52.             P2 = myMesh.transform.TransformPoint (wires[i * 3 + 2]);
    53.        
    54.             GL.Vertex3(P0.x, P0.y, P0.z);
    55.             GL.Vertex3(P1.x, P1.y, P1.z);
    56.             GL.Vertex3(P2.x, P2.y, P2.z);
    57.             GL.Vertex3(P0.x, P0.y, P0.z);
    58.         }
    59.            
    60.         GL.End();
    61.     }
    62.  
    63.     static void CreateLineMaterial()
    64.     {
    65.         if( !lineMaterial ) {
    66.             lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" +
    67.                 "SubShader { Pass { " +
    68.                 "    Blend SrcAlpha OneMinusSrcAlpha " +
    69.                 "    ZWrite Off Cull Front Fog { Mode Off } " +
    70.                 "} } }" );
    71.             lineMaterial.hideFlags = HideFlags.HideAndDontSave;
    72.             lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
    73.         }
    74.     }
    75. }
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There isn't really a way, aside from creating the quads manually or parsing a format where the mesh is stored as quads instead of triangles.

    --Eric
     
  3. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    Thanks eric. Im still curious though how an app like 3ds max display these "square" lines in the top, left and right viewports for example, and you have to convert the mesh away from a poly to see the triangles
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    They're storing the mesh as quads (or other polygons) internally, so they can display lines that way. Once something has been converted to triangles, it's fairly hard to convert back to quads.

    --Eric
     
  5. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    @ chubbspet

    The above code was not work for me............
    pls help me. How the script draw ploygon
     
  6. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    Ok, got it, thanks
     
  7. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220

    Attach the script to main camera and drag your gameobject into the variable field. Sorry my first explanation was wrong!