Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by romitraj · Feb 24, 2016 at 08:54 AM · scripting problemmeshscreentoworldpointtriangulation

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

alt text

And here is the image when the camera is rotated

alt text

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

1.jpg (19.0 kB)
2.jpg (21.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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..

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

40 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges