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 /
  • Help Room /
avatar image
0
Question by iko79 · Jan 27, 2016 at 04:53 PM · transformmatrix4x4

how to set transformation matrices of transform

Hi,

I'm getting the world-to-local matrix of an object from an AR plugin I'm currently working on, now I try to apply the according transformation in Unity and I'm stuck. I noticed there is a Transform.worldToLocalMatrix property, but it only provides a getter. Also, I tried to find a way of decomposing the matrix into translation, rotation and scale, so I can set those accordingly, again letting Unity build the transformation matrix out of them, but again, I cannot find a way to do so, except for programming the decomposition by myself.

As I am not considering this - setting a transformation matrix - as a very uncommon thing to do, I am wondering: what is the recommended way to do it, what am I missing?

Thanks for your help.

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 Owen-Reynolds · Jan 27, 2016 at 05:37 PM 0
Share

There are a few old Q's that seem to cover this pretty well.

avatar image iko79 Owen-Reynolds · Jan 28, 2016 at 08:54 AM 0
Share

If so, I cannot find them. Which ones do you have in $$anonymous$$d?

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by iko79 · Jan 28, 2016 at 05:03 PM

Okay... here is what I apparently had to do, in case somebody is looking for a solution. Still looking for a better way for doing this, if there is one.

 transform.localPosition = mat.GetColumn( 3 );
 transform.localScale = new Vector3( mat.GetColumn( 0 ).magnitude, mat.GetColumn( 1 ).magnitude, mat.GetColumn( 2 ).magnitude );
 float w = Mathf.Sqrt( 1.0f + mat.m00 + mat.m11 + mat.m22 ) / 2.0f;
 transform.localRotation = new Quaternion( ( mat.m21 - mat.m12 ) / ( 4.0f * w ), ( mat.m02 - mat.m20 ) / ( 4.0f * w ), ( mat.m10 - mat.m01 ) / ( 4.0f * w ), w );
Comment
Add comment · Show 1 · 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 lucas_la · Apr 10 at 06:47 PM 0
Share

Thanks for sharing :)

avatar image
0

Answer by EvilTak · Jan 28, 2016 at 01:05 PM

If worldToLocal is the Matrix4x4 that you are trying to set as the world to local matrix of a transform, simply assign its inverse to the Transform.localToWorldMatrix variable. The local to world matrix of an object is always the inverse of the world to local matrix of that object, and vice versa.

 Matrix4x4 worldToLocal = // Your matrix
 theTransform.localToWorldMatrix = worldToLocal.inverse;
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 iko79 · Jan 28, 2016 at 04:56 PM 0
Share

Yeah, well, no... I know that that's the inverse. However, both of them are read-only [1,2], at least in v5 they are. I read that while this is the most common thing to do in pure graphics engines such as OpenSceneGraph, in the case of Unity3D it wouldn't be a good idea to expose an interface for directly setting transformation matrices, since it would complicate many things, such as physics engine updates and what not. I'm just wondering what a good alternative is, since Unity also doesn't seem to provide utilities for, say, matrix decomposition. The $$anonymous$$atrix4x4 class is pretty sparse alltogether, frankly speaking.

[1] http://docs.unity3d.com/ScriptReference/Transform-localToWorld$$anonymous$$atrix.html

[2] http://docs.unity3d.com/ScriptReference/Transform-worldToLocal$$anonymous$$atrix.html

avatar image Lesliehdez · Feb 15, 2018 at 05:31 PM 0
Share

Hi, i need apply my own $$anonymous$$ATRIX4X4= 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] to my complete object, how can i do that?

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

38 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

Related Questions

Please help :( Matrix4x4.MultiplyPoint process compared to standard dot product matrix Matrix4x4.MultiplyPoint3X4 0 Answers

How to translate World <->Local position without Transform component with Matrix4x4? 0 Answers

Linecast with transforms rotation? 0 Answers

Setting some Vector3 as new Vector3.up for a Transform 0 Answers

Transform rotation with an ease 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