Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 unity_HxF9bz13Mt5zfw · Jun 27, 2018 at 08:24 AM · 2d3dplanes

Converting a 3D polygon into a 2D polygon

Suppose you have a set of coordinates on a 3D plane forming a polygon . I want to convert this 3d polygon into a 2d polygon with same lengths . How do I go about this? Are there any methods in unity that can help me do this

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
2

Answer by Bunny83 · Jun 27, 2018 at 10:43 AM

If your points are all on the same plane you just need to project them onto two unit vectors on that plane

To determine the plane normal you can simply calculate the cross product between two edges of your polygon. So just pick 3 points (A, B and C) and do

 Vector3 normal = Vector3.Cross(B-A, C-A).normalized;

Next you want to define your first unit vector on that plane. If you don't care about the rotation you can simply use one of the edges. Otherwise you may want to project one of the world axes onto the plane. You should make sure to not pick a direction that is too close to the plane normal

 Vector3 u;
 If (Mathf.Abs(Vector3.Dot(Vector3.forward, normal)) < 0.2f)
     u = Vector3.ProjectOnPlane(Vector3.right, normal);
 else
     u = Vector3.ProjectOnPlane(Vector3.forward, normal);

To get the second unit vector just calculate the cross product between the first unit vector and the plane normal:

 Vector3 v = Vector3.Cross(u, normal).normalized;

Now you can simply project each point of your polygon onto both unit vectors using the dot product. The two resulting values are your 2d coordinates of your points

 Vector2 Project(Vector3 aPoint)
 {
     return new Vector2(Vector3.Dot(aPoint, u), Vector3.Dot(aPoint, v));
 }


Keep in mind that the origin will still be the same. So you may want to offset all points by your desired pivot position. If you have the pivot as a 3d point on your 3d plane you can project it the same way as you do with the other points. Just subtract the pivot from all points in the end and they will be relative to that point.


If you need more help you should be more specific about your issue. Note that if the 3d plane is aligned with one of the 3 worldspace planes things could be simplified by quite a bit. Though the solution i've posted here works for arbitrarily rotated planes in space. Though keep in mind that a plane in 3d can be viewed from both sides so the winding order of your polygon may be important. You may need to flip your plane normal


Note that if your polygon points are not in the same plane you will get some sort of distortion. If you're looking for a way to unwrap a 3d mesh that's a completely different topic and doesn't have one generic solution.

Comment
Add comment · Show 3 · 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 unity_JulianTP · Jan 02, 2020 at 05:47 PM 0
Share

@Bunny83 Thanks for that great and detailed answer! That is exactly what I was looking for. Now I am stucking with the reverse transformation. I want to calculate points within the just transformed 2D plane and project these 2d points on my old 3d plane. I googled it but dont find a good approach. Do you have an idea?

avatar image Bunny83 unity_JulianTP · Jan 02, 2020 at 06:03 PM 2
Share

Next time when you have a question, please ask a seperate question and do not post an answer. The reverse is even simpler. Since you have 2d vectors and you still have the "u" and "v" vectors you can simply do

 Vector3 p = vec2d.x * u + vec2d.y * v;

Of course if you have shifted all your points by a common pivot you have to add that as well. This simple formula in general maps 2d points to a plane in 3d when you have two linear independent unit vectors of that plane.


If you still have trouble understanding basic linear algebra I highly recommend the 3b1b series on linear algebra

avatar image unity_JulianTP Bunny83 · Jan 05, 2020 at 03:28 PM 0
Share

Hi and thanks alot for that fast reply! It worked like a charm!

avatar image
0

Answer by lucas_la · Sep 04, 2021 at 10:28 PM

This function will do that. You may want to set the reduced dimension to zero to avoid the floating point approximation to zero.

 List<Vector3> alignVerticesOnPlane(List<Vector3> vertices)
      {
          List<Vector3> verticesOnPlane = new List<Vector3>();
  
          //1. Subtract vertices[0] from all points
          for (int i = 0; i < vertices.Count; i++)
              verticesOnPlane.Add(vertices[i] - vertices[0]);
  
          //2. Find the normalized plane normal
          Vector3 A = verticesOnPlane[1];
          Vector3 B = verticesOnPlane[2];
          Vector3 n = (Vector3.Cross(A, B) / Vector3.Magnitude(Vector3.Cross(A, B)));
  
          //3. Create a quaternion with the normal of the plane and the direction you want it to face
          Quaternion rotation = Quaternion.FromToRotation(n, Vector3.forward);    //change Vector3.forward to align with other planes
  
          //4. Apply the quaternion rotation to all points
          for (int i = 0; i < verticesOnPlane.Count; i++)
              verticesOnPlane[i] = rotation * verticesOnPlane[i];
  
          return verticesOnPlane;     //  =)
      }
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

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

Would a game like ZombieVille USA 2 be considered 2D or 2.5D? 0 Answers

resizing spherecast radius over distance? 4 Answers

Isometric camera 2d or 3d 2 Answers

Can you use 2D joint data to animate a 3D avatar? 0 Answers

How can I animate the surface of a 3D object? 0 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