Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by vitorabner · Oct 01, 2013 at 06:20 AM · runtimeplanecreatepoints

Create a plane from points

Hi Guys,

Please, I need help: I have n points (x, y, z), where n> 2

From these points, i need to build a plane. Any idea where I can start? This is possible? alt text Thank you

plano.png (6.8 kB)
Comment
Add comment · Show 3
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 ShadoX · Oct 01, 2013 at 07:32 AM 0
Share

You'll want to look into creating mesh from scratch, there should be enough tutorials and examples on the web to get you started on this. Like, http://answers.unity3d.com/questions/8023/creating-a-mesh-procedurally.html , for example

avatar image Hoeloe · Oct 01, 2013 at 10:30 AM 1
Share

I should add that given n>3 points, it's not always possible to build a plane - They would have to be guaranteed to lie in the same spacial plane in order to build a geometric plane out of them.

avatar image vitorabner · Oct 01, 2013 at 07:38 PM 0
Share

Thank you ShadoX and Hoebe, i solved my problem ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by robertbu · Oct 01, 2013 at 04:58 PM

I'm assuming you are talking about building a mesh in the shape defined by the vertices. If your polygon is axes aligned, and if it is convex, then it is fairly easy to do. If it is not axes aligned, then I'd likely rotate it to match an the axes, build the mesh, then rotate the vertices again. If it is not convex, then it becomes a much harder problem and you'll likely have to dig around in some CompSci posts for the algorithm.

I would approach the problem by finding a center point and then defining the triangles in terms of this center point:

alt text

Here is a bit of source that will build polygons. Attach the script to an empty game object, define your polygon shape in the 'poly' array in the Inspector, and attach a material to display to the 'mat' parameter. The polygon is built on the XY plane and any 'z' components in the 'poly' Vector3s are ignored.

 using UnityEngine;
 using System.Collections;
 
 public class PlaneFromPoly : MonoBehaviour {
     
     public Material mat;
     public Vector3[] poly;  // Initialized in the inspector
 
     void Start () {
         if (poly == null || poly.Length < 3) {
             Debug.Log ("Define 2D polygon in 'poly' in the the Inspector");
             return;
         }
         
         MeshFilter mf = gameObject.AddComponent<MeshFilter>();
         
         Mesh mesh = new Mesh();
         mf.mesh = mesh;
         
         Renderer rend = gameObject.AddComponent<MeshRenderer>();
         rend.material = mat;
 
         Vector3 center = FindCenter ();
                 
         Vector3[] vertices = new Vector3[poly.Length+1];
         vertices[0] = Vector3.zero;
         
         for (int i = 0; i < poly.Length; i++) {
             poly[i].z = 0.0f;
             vertices[i+1] = poly[i] - center;
         }
         
         mesh.vertices = vertices;
         
         int[] triangles = new int[poly.Length*3];
         
         for (int i = 0; i < poly.Length-1; i++) {
             triangles[i*3] = i+2;
             triangles[i*3+1] = 0;
             triangles[i*3+2] = i + 1;
         }
         
         triangles[(poly.Length-1)*3] = 1;
         triangles[(poly.Length-1)*3+1] = 0;
         triangles[(poly.Length-1)*3+2] = poly.Length;
         
         mesh.triangles = triangles = triangles;
         mesh.uv = BuildUVs(vertices);
 
         mesh.RecalculateBounds();
         mesh.RecalculateNormals();
 
     }
     
     Vector3 FindCenter() {
         Vector3 center = Vector3.zero;
         foreach (Vector3 v3 in poly) {
             center += v3;    
         }
         return center / poly.Length;
     }
     
     Vector2[] BuildUVs(Vector3[] vertices) {
         
         float xMin = Mathf.Infinity;
         float yMin = Mathf.Infinity;
         float xMax = -Mathf.Infinity;
         float yMax = -Mathf.Infinity;
         
         foreach (Vector3 v3 in vertices) {
             if (v3.x < xMin)
                 xMin = v3.x;
             if (v3.y < yMin)
                 yMin = v3.y;
             if (v3.x > xMax)
                 xMax = v3.x;
             if (v3.y > yMax)
                 yMax = v3.y;
         }
         
         float xRange = xMax - xMin;
         float yRange = yMax - yMin;
             
         Vector2[] uvs = new Vector2[vertices.Length];
         for (int i = 0; i < vertices.Length; i++) {
             uvs[i].x = (vertices[i].x - xMin) / xRange;
             uvs[i].y = (vertices[i].y - yMin) / yRange;
             
         }
         return uvs;
     }
 }

The uvs are defined in terms of the minimum and maximum points and result in the shape cookie cutting out the texture attached to a material (assuming your shader has textures and deals with them in a standard way). Try a material using Unlit/Texture to see what I mean.

I'm not sure that the algorithm that I am using will find a center point within the bounds of the polygon for all convex polygons. If this is a problem, you can have the center specified as a parameter (rather than calculated), or you can elect to implement some other center finding method:

http://en.wikipedia.org/wiki/Centroid


polytoplane.png (41.6 kB)
Comment
Add comment · Show 7 · 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 vitorabner · Oct 01, 2013 at 07:31 PM 0
Share

Wow! robertbu, you are amazing! really, really thank you!

Your script is basically what I need, because all my polygons are convex and axes are aligned.

I studied your script, and now i know more about mesh and polygons in Unity 3D.

Thank you again!

avatar image robertbu · Oct 01, 2013 at 07:50 PM 0
Share

As a side note, you are likely better off special casing triangles and building a single triangle rather than using this algorithm.

avatar image Raynoko · Sep 18, 2014 at 02:21 PM 0
Share

your script is good, but do you know rewrite him? i mean, if i will change one of all x,y,z axis, then this point change position in playmode? because this script work only one ("Start") so i need Update, or something like that. Thank you

avatar image Raynoko · Sep 18, 2014 at 02:50 PM 0
Share

your script is good, but do you know rewrite him? i mean, if i will change one of all x,y,z axis, then this point change position in playmode? because this script work only one ("Start") so i need Update, or something like that. Thank you

avatar image robertbu · Sep 18, 2014 at 02:56 PM 0
Share

@Raynoko - I don't understand what you are asking. What does, "if i will change one of all x,y,z axis, then this point change position in playmode" means? Note that after the mesh is built, you can move the vertices in the $$anonymous$$esh.vertices array at runtime and the mesh will change.

Show more comments

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

18 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

Related Questions

Sprite creation problem 1 Answer

create game object on the fly 2 Answers

Create plane from 2 Vectors of a symmetry line segment 1 Answer

Create a plane on Voronoi 1 Answer

Create scenes at runtime 2 Answers


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