Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 1337GameDev · Jul 30, 2015 at 07:14 PM · rotationmeshproceduralinvisibletriangles

Creating a mesh "outline" around gameobject messes up when the gameobject rotates...

I have a gameobject, basically the character the player controls.

I want to create a mesh, that represents a "frame" of a trapezoid around this gameobject.

Creating the outer vertices works fine, but creating the inner vertices messes up (they are not evenly spaced away from the other vertices and the triangles I create seem to "flip" around and face the wrong direction and become invisible.

I have tried subtracting coordinates (but when the object rotates, these coordinates no longer are correct and the mesh looks messed up.

I have also tried scaling the outer vertices by a factor of 0.75, and this also seems to mess up too when the object is rotated.

How can I create this shape, without the rotation of the object throwing off the mesh creation?

Trapezoid vertices example

framecoords.png (22.8 kB)
Comment
Add comment · Show 4
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 _Gkxd · Jul 30, 2015 at 07:28 PM 0
Share

It seems like you're trying to make a miter joint. The math behind it is discussed in one of the posts here. (Scroll down until you see the post with diagrams.)

It's a bit difficult to understand what you mean when you say "the triangles flip around and face the wrong direction" without code or screenshots of how it messes up.

avatar image OrbitGames · Jul 30, 2015 at 07:33 PM 0
Share

You are probably setting the triangles clockwise, not counter clock wise(might be the other way around) You should flip the order in which you are adding the vertecies

avatar image 1337GameDev · Jul 30, 2015 at 09:24 PM 0
Share

Well I have created the mesh, and when the rotation is zero, it shows perfectly. But when it rotates, the vertices either dont stay in their correct relative locations (and hence the triangles get created in reverse) or the edges become very small/thin and become transparent.

The first image is the original mesh.

The second is the mesh generated when the object is rotated.

The third is when I raise the mesh in the editor and look at the underside of it. (it should be transparent but it's not as some vertices get converted to flipped triangles) Original Rotated

screen-shot-2015-07-30-at-34257-pm.png (57.5 kB)
screen-shot-2015-07-30-at-34320-pm.png (89.8 kB)
avatar image 1337GameDev · Jul 30, 2015 at 09:28 PM 0
Share

The last image wouldnt attach to the prior post -_-

Underside

screen-shot-2015-07-30-at-34416-pm.png (101.7 kB)

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Scribe · Jul 31, 2015 at 01:35 PM

This is pseudo code unfortunately, however I feel like the Vector3.Slerp method should be useful here possibly:

 Vector3 OUL;
 Vector3 OLL;
 Vector3 OUR;
 Vector3 OLR;
 
 Float distIn = 0.5f; //how far in we come from the edge
 
 //go round clockwise
 Vector3 topEdge = (OUR-OUL).normalized*distIn;
 Vector3 rightEdge = (OLR-OUR).normalized*distIn;
 Vector3 bottomEdge = (OLL-OLR).normalized*distIn;
 Vector3 leftEdge = (OUL-OLL).normalized*distIn;
 
 
 Vector3 IUL = OUL + Vector3.Slerp(topEdge, -leftEdge, 0.5f);
 Vector3 IUR = OUR + Vector3.Slerp(-topEdge, rightEdge, 0.5f);
 Vector3 ILL = OLL + Vector3.Slerp(-bottomEdge, leftEdge, 0.5f);
 Vector3 ILR = OLR + Vector3.Slerp(bottomEdge, -rightEdge, 0.5f);
 
 Vector3[] verts = {OUL, OLL, OUR, OLR, IUL, IUR, ILL, ILR};

 Int[] triangles = {    0, 2, 4,
             4, 2, 5,
             2, 3, 5,
             5, 3, 7,
             3, 1, 7,
             7, 1, 6,
             1, 0, 6,
             6, 0, 4};


Maybe you can make use of that, it should do the rotation calculations for you if you do it all within the same coordinates, i.e. either all world coords, or all local coords.

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 OrbitGames · Jul 31, 2015 at 01:06 PM

The problem in my opinion is, that you offset the inside along the x and y axis of the world, not of the gameObject. Try ofsetting them by transform.up W + transform.right W or something.

As far as looking at it from the bottom, it is transparent because Meshes are only rendered on one side. You should add all vertecies and triangles again with inverted order, so the triangles are set the other way around.

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 1337GameDev · Jul 31, 2015 at 03:26 PM

I found a "reasonable" solution, where I use the opposite sides, create a direction vector, and multiple by my intended "width" of the frame I want. It is slightly to big width-wise, but it should work for now. My complication was trying to create a function that creates the inner vertices FROM the outer ones, without using coordinates.

 public static Vector3 TranslateDistanceBetweenPoints(this Vector3 start, Vector3 end, float distance) 
     {    
         Vector3 dir = end - start;
         Vector3 finalPos = start + (dir.normalized * distance);
         return finalPos;
     }
 
 public void generateMesh()
 {
                 //this assumes the "result" structure contains the outer vertices
 
                 innerResult.UpperLeft = result.UpperLeft.TranslateDistanceBetweenPoints(result.LowerRight,widthOfFrame);
         innerResult.UpperRight = result.UpperRight.TranslateDistanceBetweenPoints(result.LowerLeft,widthOfFrame);
         innerResult.LowerLeft = result.LowerLeft.TranslateDistanceBetweenPoints(result.UpperRight,widthOfFrame);
         innerResult.LowerRight = result.LowerRight.TranslateDistanceBetweenPoints(result.UpperLeft,widthOfFrame);
         
         //now use these points to create a mesh from the inner and outer points
         //UL
         //UR
         //LL
         //LR
         Vector3[] vertices = new Vector3[]
         {
             //outer
             result.UpperLeft,//0
             result.UpperRight,//1
             result.LowerLeft,//2
             result.LowerRight,//3
             //inner
             innerResult.UpperLeft,//4
             innerResult.UpperRight,//5
             innerResult.LowerLeft,//6
             innerResult.LowerRight//7
         };
         
         Vector3[] normals = 
         {
             -Vector3.forward,
             -Vector3.forward,
             -Vector3.forward,
             -Vector3.forward,
             
             -Vector3.forward,
             -Vector3.forward,
             -Vector3.forward,
             -Vector3.forward
         };
         
         Vector2[] uv = new Vector2[]
         {
             new Vector2(1, 1),
             new Vector2(1, 0),
             new Vector2(0, 1),
             new Vector2(0, 0),
             
             new Vector2(1, 1),
             new Vector2(1, 0),
             new Vector2(0, 1),
             new Vector2(0, 0)
         };
         
         int[] triangles = new int[]
         {
             //upper
             0, 1, 4,
             4, 1, 5,
             
             //lower
             2, 6, 7,
             2, 7, 3,
             
             //left
             2, 0, 4,
             2, 4, 6,
             
             //right
             7, 5, 1,
             7, 1, 3
         };
         
         
         meshFilter.mesh.Clear();
         meshFilter.mesh.MarkDynamic();
         meshFilter.mesh.vertices = vertices;
         meshFilter.mesh.triangles = triangles;
         meshFilter.mesh.normals = normals;
         meshFilter.mesh.uv = uv;
 }
Comment
Add comment · Show 2 · 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 Scribe · Jul 31, 2015 at 03:47 PM 0
Share

Did you check my answer, it does a similar thing using the outer points to interpret the inner points.

avatar image 1337GameDev · Jul 31, 2015 at 10:10 PM 0
Share

Yeah. It did help after I found my solution. Thanks for the advice.

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

24 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

Related Questions

Procedural mesh creation issue 1 Answer

Generate mesh from raycast positions, independent of rotations 0 Answers

Easy way to convert a bunch of vertices to triangles or uv's? 1 Answer

Procedural cube code 0 Answers

Procedural Mesh Generation - Split Arrays into sections 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