Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by Long2904 · Feb 23, 2021 at 04:15 AM · custom editordrawinghandlesgltriangle

How to draw a triangle in the center of a line using GL?

I am making a node-based custom editor window (something like the animator window). I now can draw nodes and edges between them. The thing I want to do now is to draw a triangle arrowhead in the middle of the edge (like the one you see in the animator window). From this post, I started to try GL to draw the triangle. Here's my code:

     private void OnGUI()
     {
         // Do other stuff...
 
         Vector2 startPos = ...; // The start position (Center of the from node. This is from the node's rect which mean it used the rect's coordinate system)
         Vector2 endPos = ...; // The end position (Center of the to node. Also from a rect)
 
         Vector2 center = MathUtils.Average(startPos, endPos); // The center of the current edge
         Vector2 dir = (endPos - startPos).normalized; // The direction of the current egde
 
         // Making an equilateral triangle
         Vector3 a = center + dir                               * 10; // From the center move 10 more unit along the line's direction
         Vector3 b = center + Rotate(dir,  120 * Mathf.Deg2Rad) * 10; // Rotate the dir  120 degree and move along that direction 10 more unit
         Vector3 c = center + Rotate(dir, -120 * Mathf.Deg2Rad) * 10; // Rotate the dir -120 degree and move along that direction 10 more unit
 
         // The coordinate of the GUI is (0, 0) on the top left conner, this will convert it to the normal GL coordinate ((0, 0) on the bottom left connor)
         // Also the position property is a inheritance property from the EditorWindow (in case you don't know)
         a.y = position.height - a.y;
         b.y = position.height - b.y;
         c.y = position.height - c.y;
 
         // InverseLerp will return the value from [(0, 0), (1, 1)] because that what GL.Vertex() use
         a = InverseLerp(Vector2.zero, position.size, a);
         b = InverseLerp(Vector2.zero, position.size, b);
         c = InverseLerp(Vector2.zero, position.size, c);
 
         DrawGLTriangle(a, b, c); // Draw the triangle
         Handles.DrawAAPolyLine(10, startPos, endPos); // Draw the egde between the two current nodes
 
         // Do other stuff...
     }
 
     public static Vector2 InverseLerp(Vector2 start, Vector2 end, Vector2 value)
     {
         return (value - start) / (end - start);
     }
 
     public static Vector2 Rotate(Vector2 v, float angle)
     {
         return new Vector2(
         v.x * Mathf.Cos(angle) - v.y * Mathf.Sin(angle),
         v.x * Mathf.Sin(angle) + v.y * Mathf.Cos(angle)
     );
     }
 
     // This function is directly taken from Unity https://docs.unity3d.com/ScriptReference/GL.TRIANGLES.html
     public static void DrawGLTriangle(Vector3 a, Vector3 b, Vector3 c)
     {
         GL.PushMatrix();
         GL.LoadOrtho();
         GL.Begin(GL.TRIANGLES);
         GL.Vertex(a);
         GL.Vertex(b);
         GL.Vertex(c);
         GL.End();
         GL.PopMatrix();
     }

Here's the result, the triangle is not on the line. Its x position seems correct but the y position is a little higher than it should be. It seems to rotate correctly.

alt text

bandicam-2021-02-23-11-04-55-688.gif (399.2 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

0 Replies

· Add your reply
  • Sort: 

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

114 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

Related Questions

Invert DrawLine direction in editor Window. 1 Answer

Custom Handle, affect multiple objects 1 Answer

OnInspectorGUI Custom Drawing? 1 Answer

Custom Handle. Mutliple Objects 0 Answers

How to draw flat triangles in 3D space? 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