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
0
Question by artygrand · Mar 04, 2021 at 06:47 AM · mathtransformdirection

Problem with transform.InverseTransformDirection and non-uniform scale

I made the plane to work with mesh vertices

 var plane = new Plane(
      hit.transform.InverseTransformDirection(normal),
      hit.transform.InverseTransformPoint(position)
 );

If the object has the uniform scale then all looks good

alt text

but otherwise the plane normal has weird angle and I don't understand why.

alt text

with.InverseTransformVector even worser. The angle goes 2 times bigger

alt text

Blue line - my plane

Green spheres - plane intersections

white sphere - plane center

UPDATE

There is code to reproduce

Place component to default cube in zero position, rotate it with Y != 0, scale ONE horizontal dimesion. You will see onesided plane.

 using UnityEngine;

 public class PlaceOnCube : MonoBehaviour
 {
 public Vector3 PlaneOrigin = new Vector3(0, 0, 0);
 public Vector3 PlaneNormal = new Vector3(1, 0, 0);

 private void OnDrawGizmos()
 {
     Gizmos.DrawSphere(PlaneOrigin, .03f);

     var plane = new Plane(
            transform.InverseTransformDirection(PlaneNormal),
            transform.InverseTransformPoint(PlaneOrigin)
            );

     ReGeneratePlane(plane);
 }

 private void ReGeneratePlane(Plane plane)
 {
     var meshfilter = transform.GetComponent<MeshFilter>();
     var mesh = meshfilter.mesh;
     var tris = mesh.triangles;
     var verts = mesh.vertices;
     var newTris = new int[36 + 6];
     var newVerts = new Vector3[24 + 4];

     for (int j = 0; j < verts.Length; j++)
         newVerts[j] = verts[j];

     newVerts[24] = plane.ClosestPointOnPlane(new Vector3(-2, -1, 0));
     newVerts[25] = plane.ClosestPointOnPlane(new Vector3(2, -1, 0));
     newVerts[26] = plane.ClosestPointOnPlane(new Vector3(-2, 1, 0));
     newVerts[27] = plane.ClosestPointOnPlane(new Vector3(2, 1, 0));

     for (int j = 0; j < tris.Length; j++)
         newTris[j] = tris[j];

     newTris[36] = 24;
     newTris[37] = 26;
     newTris[38] = 25;
     newTris[39] = 26;
     newTris[40] = 27;
     newTris[41] = 25;

     mesh.vertices = newVerts;
     mesh.triangles = newTris;

     meshfilter.mesh = mesh;
 }
 }


Comment
Add comment · Show 2
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 elenzil · Mar 04, 2021 at 09:28 PM 0
Share

re:

 transform.GetComponent<MeshFilter>();


off-topic, but GetComponent() is a member of Component, which is a baseclass of MonoBehavior, which your own class is, so you can just do GetComponent() without the transform.

regarding your question, it's unclear to me what your goal is. ie, given this cube, what do you want to achieve ?

avatar image artygrand elenzil · Mar 05, 2021 at 05:14 AM 0
Share

I know about GetComponent, it's just fast copypasted dirty test code. Problem with plane, it sholud stay still on cube rotation, like in first gif.

1 Reply

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

Answer by Namey5 · Mar 05, 2021 at 07:28 AM

What's happening is that your plane's normal vector is being scaled with the object's transformation. In this case vector magnitude doesn't matter, however if it was you would also notice that the vector grows and shrinks. The solution for this is to essentially 'invert' the scale that's being applied.

Your line

 transform.InverseTransformDirection(PlaneNormal)

is doing this behind the scenes;

 transform.worldToLocalMatrix.MultiplyVector (PlaneNormal)


To fix the scaling, we instead need to transform the vector by the inverse-transpose of the same matrix;

 transform.worldToLocalMatrix.inverse.transpose.MultiplyVector (PlaneNormal)

The inverse of the worldToLocalMatrix is just the opposite transformation, so we can use this instead;

 transform.localToWorldMatrix.transpose.MultiplyVector (PlaneNormal)


If you want to read more, this article has a good explanation;

https://paroj.github.io/gltut/Illumination/Tut09%20Normal%20Transformation.html

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 artygrand · Mar 05, 2021 at 08:32 AM 0
Share

Where do you found info worldToLocalMatrix.MultiplyVector? In github in unity source code method InverseTransformDirection is external and hidden.

avatar image Namey5 artygrand · Mar 05, 2021 at 08:48 AM 0
Share

It's an industry-standard implementation - I don't work for Unity nor can I see their source code, but that's just how these transformations are done.

avatar image artygrand Namey5 · Mar 05, 2021 at 08:55 AM 0
Share

Thank you!

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

122 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

Related Questions

Looking at a target in 2D 0 Answers

[MATH] Vectors, magnitudes, and bears; Oh My! -Solved 2 Answers

Quick Math Help with Normalized numbers 2 Answers

Unity thinks that 14 = 15? 1 Answer

Move an object to a specific distance 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