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): var theVertices = new Array(); theVertices.Push(Vector3((0,0,0)); theVertices.Push(Vector3((1,0,0)); theVertices.Push(Vector3((1,1,0)); theVertices.Push(Vector3((0,1,0)); DrawPoly(theVertices); Suggestions?
http://unity3d.com/support/documentation/ScriptReference/MeshFilter.html http://unity3d.com/support/documentation/ScriptReference/Mesh.html
Thanks for the reply. It looks like I have to create a new mesh. The example they provide: Code (csharp): function Start () { var mesh = new Mesh (); GetComponent (MeshFilter).mesh = mesh; mesh.vertices = newVertices; mesh.uv = newUV; mesh.triangles = newTriangles; } 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): var mesh : Mesh; var gameObject : GameObject; CreateMesh() { var meshCreated : Mesh = new Mesh(); ///blah blah blah, create some mesh return meshCreated } mesh = CreateMesh(); gameObject.AddComponent("MeshFilter"); gameObject.AddComponent("MeshRenderer"); gameObject.MeshFilter.mesh = mesh; 2B) How can I change the material for this game object? Something like this?: Code (csharp): var myMaterial : Material; //Drag and drop a material onto the inspector gameObject.MeshRenderer.Materials[0] = myMaterial; Thanks, Alex
Got it working thanks. If anyone is curious, here is my code Code (csharp): function updatePolygon() { for(x = 0; x < 2; x++) { //Destroy old game object Destroy(myObject[x]); //New mesh and game object myObject[x] = new GameObject(); myObject[x].name = "MousePolygon"; var mesh : Mesh = new Mesh(); //Components var MF = myObject[x].AddComponent("MeshFilter"); var MR = myObject[x].AddComponent("MeshRenderer"); //myObject[x].AddComponent(); //Create mesh mesh = CreateMesh(x); //Assign materials MR.material = myMaterial; //Assign mesh to game object MF.mesh = mesh; } } Code (csharp): function CreateMesh(num : int) { var x : int; //Counter //Create a new mesh var mesh : Mesh = new Mesh(); //Vertices var vertex = new Vector3[nodePositions.length]; for(x = 0; x < nodePositions.length; x++) { vertex[x] = nodePositions[x]; } //UVs var uvs = new Vector2[vertex.length]; for(x = 0; x < vertex.length; x++) { if((x%2) == 0) { uvs[x] = Vector2(0,0); } else { uvs[x] = Vector2(1,1); } } //Triangles var tris = new int[3 * (vertex.length - 2)]; //3 verts per triangle * num triangles var C1 : int; var C2 : int; var C3 : int; if(num == 0) { C1 = 0; C2 = 1; C3 = 2; for(x = 0; x < tris.length; x+=3) { tris[x] = C1; tris[x+1] = C2; tris[x+2] = C3; C2++; C3++; } } else { C1 = 0; C2 = vertex.length - 1; C3 = vertex.length - 2; for(x = 0; x < tris.length; x+=3) { tris[x] = C1; tris[x+1] = C2; tris[x+2] = C3; C2--; C3--; } } //Assign data to mesh mesh.vertices = vertex; mesh.uv = uvs; mesh.triangles = tris; //Recalculations mesh.RecalculateNormals(); mesh.RecalculateBounds(); mesh.Optimize(); //Name the mesh mesh.name = "MyMesh"; //Return the mesh return mesh; } 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.
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()?
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!
If you want to draw a polygon in the new UI check this out: https://github.com/CiaccoDavide/Unity-UI-Polygon
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...