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 femi · Jan 06, 2011 at 09:28 AM · transformmatrix

Difference between Transform.InverseTransformPoint(v) and Transform.worldToLocalMatrix * v

Basically, the following fragment

        Vector3 vertexLocal = transform.worldToLocalMatrix * vertex;
        Vector3 vertexLocalTest = transform.InverseTransformPoint(vertex);
        if (vertexLocal != vertexLocalTest) {
            Debug.LogError("I don't get it: " + vertexLocal + " != " + vertexLocalTest);
            return;
        }

Produces errors in console. It seems as if multiplication (vertexLocal) transforms the point not from world coordinates but from parent's coordinates. I assumed the two transformations were supposed to be identical. Aren't they?

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
Best Answer

Answer by runevision · Jan 06, 2011 at 10:08 AM

Don't use transform.worldToLocalMatrix * vertex

Use transform.worldToLocalMatrix.MultiplyPoint(vertex)

or transform.worldToLocalMatrix.MultiplyPoint3x4(vertex) (more efficient)

Then you'll get identical results as expected.

Comment
Add comment · Show 4 · 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 femi · Jan 06, 2011 at 10:28 AM 0
Share

Thanks.

Is it because my Vector3 got extended to Vector4 in some special way?

avatar image femi · Jan 06, 2011 at 10:35 AM 0
Share

Point is, I was trying to come up with a way to transform many points from ancestor's coordinate system to the descendant's, and wanted to premultiply a copy of descendant's worldToLocal$$anonymous$$atrix with ancestor's "localToWorld$$anonymous$$atrix" when I discovered that operator * SEE$$anonymous$$S to do what I wanted. But before I rejoice I want to understand why and if I can rely on this effect.

avatar image runevision ♦♦ · Jan 06, 2011 at 10:36 AM 1
Share

Actually the operator version takes a Vector4 and uses the 4th component to multiply with the position. When supplying a Vector3 it gets converted to a Vector4 with the 4th component being 0, thus the position of the matrix gets ignored.

avatar image femi · Jan 06, 2011 at 10:45 AM 0
Share

Thanks again. So that's just because the parent was translated, but not rotated, that it appeared to do what I needed.

avatar image
2

Answer by apantev · Feb 01, 2015 at 06:18 PM

Since worldToLocalMatrix is a 4x4 matrix and vertex is only a Vector3 when the two are multiplied together vertex is automatically converted to a Vector4 with a 4th component of 0.

So

 transform.worldToLocalMatrix * vertex

Is the same as

 transform.worldToLocalMatrix * new Vector4(vertex.x, vertex.y, vertex.z, 0)

But when that forth component is 0 the multiplication is ignoring the position of the matrix, which is why you see the wrong result.

As runevision has pointed out the following

 transform.InverseTransformPoint(vertex)

Is the same as

 transform.worldToLocalMatrix.MultiplyPoint3x4(vertex)

Which is the same as

 transform.worldToLocalMatrix * new Vector4(vertex.x, vertex.y, vertex.z, 1)
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 laurencenairne · Mar 14, 2018 at 11:47 AM 0
Share

I get that the first answer was accepted because it's correct and was there first.

But I actually think this answer is more complete as it explains in greater detail what is happening.

avatar image Bunny83 laurencenairne · Mar 14, 2018 at 12:25 PM 0
Share

But it misses the fact that "$$anonymous$$ultiplyPoint3x4" is more efficient for pure linear transformations as the 4th row of the matrix is not needed. If you do a "$$anonymous$$atrix4x4 * Vector4" you have to do more calculations for the last row which are pretty pointless as it will always be (0,0,0,1) and doesn't contribute to the x, y and z components.


$$anonymous$$ultiplyPoint3x4 expects just a Vector3 and outputs a Vector3. It treats the 4x4 matrix like a 3x3 matrix and just adds the 4th column to the result. Therefore:

  • $$anonymous$$ultiplyPoint3x4 does 9 multiplications; 9 additions

  • $$anonymous$$ultiplyPoint does 12 multiplications; 1 division; 12 additions.

  • the * operator does 16 multiplications; 12 additions


Note that "$$anonymous$$ultiplyPoint" does the homogeneous divide internally as it outputs a Vector3.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Use methods of Transform without linked gameObject 1 Answer

Matrix to transform bone orientation to world orientation 0 Answers

Transform to matrix 1 Answer

Is there such a thing as Unity Algebra? 0 Answers

What is worldToLocalMatrix.MultiplyPoint doing in the back end ? 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