- Home /
Filling Closed Shapes
Hello,
I am using Vectrosity to draw some some shapes on screen run time. I also need to fill these shapes and none of them are really trivial shapes. So after doing some research I figured the best way would be to do it by creating a run-time mesh. Now this almost works for me but has a big issue.
This is my process.
1) I have defined my complex shapes as a set of 2D vertices that I extracted from Illustrator. These vertices I feed to the Vectrosity's Catmul spline function to create my shapes.
2) I then project these vertices in the real space using ScreenToWorldPoint to get 3D vertices.
3) I then pass these vertices to a program that Triangulates these points to a mesh (I use the classic Triangulate script on wiki)
4) I then clean up a little and align
Now all this works well if the camera is not rotated and the forward vector of the camera is orthogonal to the XY plane. The moment I rotate the camera the alignment of the mesh and the vector lines is completely gone.
Here is the image of the vectorline and mesh when the camera is not rotated
And here is the image when the camera is rotated
Here is the function that projects the point into 3D space
void DrawPolygon () {
Vector2[] tempVerts = new Vector2[allLinePoints.Count];
for(int i=0; i<allLinePoints.Count; i++) {
Vector3 projectedPoint = Camera.main.ScreenToWorldPoint(new Vector3(allLinePoints[i].x, allLinePoints[i].y, 10.0f));
tempVerts[i] = new Vector2(projectedPoint.x, projectedPoint.y);
}
polyTarget.SendMessage("Create", tempVerts);
}
This message is sent to a generic Triangulation script. Here it is
using UnityEngine;
using System.Collections;
using System.Linq;
public class CreatePolygon : MonoBehaviour {
public Material meshMaterial;
public void Create (Vector2[] verts) {
Triangulator tr = new Triangulator (verts);
int[] indices = tr.Triangulate ();
// Create the Vector3 vertices
Vector3[] vertices = new Vector3[verts.Length];
for (int i=0; i<vertices.Length; i++) {
vertices[i] = new Vector3(verts[i].x, verts[i].y, 0);
}
// Create the mesh
Mesh msh = new Mesh();
msh.vertices = vertices;
msh.triangles = indices;
msh.RecalculateNormals();
msh.RecalculateBounds();
// Set up game object with mesh;
gameObject.AddComponent(typeof(MeshRenderer));
MeshFilter filter = gameObject.AddComponent(typeof(MeshFilter)) as MeshFilter;
filter.mesh = msh;
GetComponent<Renderer> ().material = meshMaterial;
StartCoroutine (FlipNormal ());
}
public void ResetMesh (Vector2[] verts) {
Triangulator tr = new Triangulator (verts);
int[] indices = tr.Triangulate ();
// Create the Vector3 vertices
Vector3[] vertices = new Vector3[verts.Length];
for (int i=0; i<vertices.Length; i++) {
vertices[i] = new Vector3(verts[i].x, verts[i].y, 0);
}
// Create the mesh
Mesh msh = new Mesh();
msh.vertices = vertices;
msh.triangles = indices;
msh.RecalculateNormals();
msh.RecalculateBounds();
// Set up game object with mesh;
MeshFilter filter = GetComponent<MeshFilter> ();
filter.mesh = msh;
GetComponent<Renderer> ().material = meshMaterial;
StartCoroutine (FlipNormal ());
}
IEnumerator FlipNormal() {
yield return new WaitForSeconds (0.1f);
Mesh mesh = (GetComponent<MeshFilter> () as MeshFilter).mesh;
mesh.uv = mesh.uv.Select(o => new Vector2(1 - o.x, o.y)).ToArray();
mesh.triangles = mesh.triangles.Reverse().ToArray();
}
}
Now this seems to be an obvious problem to have but I am just not being able to crack it. I have a feeling the solution is some mathematics that is completely escaping me right now. Can someone please help and point me to the right direction. I need to be able to align the mesh to the outline even when the camera is rotated.
Thanks
Answer by romitraj · Feb 26, 2016 at 05:55 AM
Can someone please have a look at this? This thing is now really choking our dev cycle. Even if someone can point me to a possible solution, it'll be great help.
Answer by yarune · Sep 28, 2016 at 03:36 PM
Vectrosity draws to a separate GameObject with screen space overlay canvas component. So whatever you point the camera to, the Vectrosity lines will stay put..
Your answer
Follow this Question
Related Questions
How to make a square mesh and remove a part of it in real time? 0 Answers
Spawn script on mesh crashes Unity 0 Answers
Hi, It is possible to get all the attributes inside a mesh with an script 1 Answer
Most Optimal way of computing Delaunay Triangulation given an Array of Vector3 points 0 Answers
Problems with UV on created mesh 1 Answer