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
3
Question by aaronflippo · Apr 20, 2013 at 07:19 AM · transformoptimizationmatrix4x4

"Baking" Object scale into mesh

I have a hierarchy of Unity objects, and each object in the hierarchy has a (non-uniform) scale and rotation.

For example, my hierarchy may be A->B->C.

I'm trying to write a script to "bake" the scale of these objects into the meshes in the hierarchy, so from the point of view of the rendering engine, all objects will have a scale of (1,1,1). This will allow batching to work more reliably, and more importantly, should avoid huge spikes I'm getting due to lots of calls to BakeScaledMeshCollisionData. The object hierarchies in question will be used as runtime-prefabs. I've verified this solution works for my needs using simple un-parented objects.

What I need help with, is figuring out how to do this in a hierarchy of objects. In other words - given the hierarchy above, how do I determine for each object what scale to apply?

Basically - how do I recreate the effects of the transforms of all the objects in the hierarchy into a single matrix, that I can then use to transform the verts of the mesh, while setting the localscale of each object in the hierachy to 1.0, to get the same effect?

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 whydoidoit · Apr 20, 2013 at 07:26 AM 0
Share

Well the big thing is getting an accurate scale the whole way down - it sounds like you should probably be using something more accurate than a float - perhaps double or Decimal. You can get an approximation of the scale form lossyScale - but to do it yourself, you just have to Scale through the individual vectors (component wise multiplication) - the all you are doing is the exact same thing to the local position of the mesh renderer and every vertex in the mesh.

avatar image aaronflippo · Apr 22, 2013 at 01:58 PM 0
Share

Hmm, I don't think component-wise multiplication will be sufficient. The rotation of each object in the hierarchy will affect the way scale is applied to the children, no? For instance, a scale applied to a rotated child becomes a skew...

avatar image whydoidoit · Apr 22, 2013 at 02:16 PM 0
Share

Are you using none uniform scaling?? Ugh if you are :s

avatar image aaronflippo · Apr 22, 2013 at 02:26 PM 0
Share

Yep, that's the whole point of doing the baking, as uniformly scaled meshes don't incur these performance issues. What I'm looking for is a general-purpose solution.

1 Reply

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

Answer by Bunny83 · Apr 22, 2013 at 02:56 PM

I would suggest to simply move the topmost parent to 0,0,0 (world origin) and simply use transform.TransformPoint() on each vertex. The resulting vertices are transformed as you wish. Since you removed the root translation you can simply use the vertices as local coordinates of an object sitting at the world origin with eulerangles 0,0,0 and a scale of 1,1,1. Once the new object is created, just re-add the translation to move it to it's original position.

edit
The solution is actually way simpler than imagined ;)

 Vector3 Bake(Transform aOld, Transform aNew, Vector3 aPos)
 {
     var P = aOld.TransformPoint(aPos);
     return aNew.InverseTransformPoint(P);
 }

So just create a new object at the same world position / rotation as the nested original object and execute Bake for each vertex ;)

 Transform oldObj;
 
 
 Transform newObj = (new GameObject(oldObj.name + "_Baked")).transform;
 newObj.position = oldObj.position;
 newObj.rotation = oldObj.rotation;
 newObj.localScale = Vector3.one;
 
 for(int i = 0; i < verts.Length,i++)
     verts[i] = Bake(oldObj, newObj, verts[i]);
Comment
Add comment · Show 7 · 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 aaronflippo · Apr 22, 2013 at 03:01 PM 0
Share

Hmm, so you're suggesting doing this for each object in the hierarchy, using its own transform to translate the verts on the mesh?

avatar image Bunny83 · Apr 22, 2013 at 03:06 PM 1
Share

Yes, sure ;) The problem is that all 3 things affecting the result: scale, rotation, position except the "outer" position. So in the end you would need the whole matrix stack of your hierarchy, so it's easier to directly use it ;)

avatar image aaronflippo · Apr 22, 2013 at 03:09 PM 0
Share

Yeah that's what I'm trying to wrap my head around. To complicate things - I won't be zero-ing out the local rotation/translation of each object in the hierarchy, just the scale. So it seems I want to take the total of the parent's effects on each child, $$anonymous$$us the translation, plus the child's local scale, and apply that to the verts.

avatar image Bunny83 · Apr 22, 2013 at 03:35 PM 0
Share

I've updated my answer ;)

avatar image aaronflippo · Apr 22, 2013 at 03:44 PM 0
Share

Oh this is cool. So if I'm understanding the new solution: you're creating a new object at the same point in the hierarchy(but with a scale of 1), then taking each vert in the old object, transfor$$anonymous$$g it to world space, then transfor$$anonymous$$g it into the new local space of the copy object?

Show more comments

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

13 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

Related Questions

How to manually calculate localToWorldMatrix/worldToLocalMatrix? 1 Answer

Are .gameObject and .transform both using GetComponent() in the background? 1 Answer

Why on earth can I not just get and set the Matrix4x4 of a Transform?!? 2 Answers

Transform and "native" components optimization 1 Answer

transform.localScale very slow on device (contrary to Android Remote) 2 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