title: How to calculate the (proper) transformation matrix between two frames (axial systems) in Unity3D
body: <p>For a project in Unity3D I'm trying to transform all objects in the world by changing frames. What this means is that the origin of the new frame is rotated, translated, and scaled to match the origin of the old frame, then this operation is applied to all other objects (including the old origin).
  For this, I need a generalized, 3-dimensional (thus 4x4) Transformation-Matrix.</p>
  
  <p>I have looked at using Unity's built-in Matrix4x4.TRS()-method, but this seems useless, as it only applies the Translation, Rotation &amp; Scale to a defined point.
  What I'm looking for, is a change of frames, in which the new frame has a different origin, rotation, AND scale, with regards to the original one.</p>
  
  <p>To visualize the problem, I've made a small GIF (I currently have a working version in 3D, without using a Matrix, and without any rotation):
  <a href="https://gyazo.com/8a7ab04dfef2c96f53015084eefbdb01" rel="nofollow noreferrer">https://gyazo.com/8a7ab04dfef2c96f53015084eefbdb01</a></p>
  
  <p>The values for each sphere:</p>
  
  <pre><code>Origin1 (Red Sphere)
  
  Before:   
  Position (-10, 0, 0)
  Rotation (0,0,0)
  Scale    (4,4,4)   
  
  After:
  Position (10, 0, 0)
  Rotation (0,0,0)
  Scale    (8,8,8)   
  
  -
  Origin2 (Blue Sphere)
  
  Before:   
  Position (-20, 0, 0)
  Rotation (0,0,0)
  Scale    (2,2,2)   
  
  After:
  Position (-10, 0, 0)
  Rotation (0,0,0)
  Scale    (4,4,4)   
  
  -
  World-Object (White Sphere)
  
  Before:   
  Position (0, 0, 10)
  Rotation (0,0,0)
  Scale    (2,2,2)   
  
  After:
  Position (30, 0, 20)
  Rotation (0,0,0)
  Scale    (4,4,4)  
  </code></pre>
  
  <p>Currently I'm simply taking the Vector between the 2 origins, scaling that to the difference between the two origins, then applying that on top of the new position of the original (first) origin.
  This will of course not work when rotation is applied to any of the 2 origins.</p>
  
  <pre><code>// Position in original axes
  Vector3 positionBefore = testPosition.TestPosition - origin.TestPosition;
  // Position in new axes
  Vector3 positionAfter = (positionBefore * scaleFactor) + origin.transform.position;
  </code></pre>
  
  <p>What I'm looking for is a Matrix that can do this (and include rotation, such that Origin2 is rotated to the rotation Origin1 was in before the transformation, and all other objects are moved to their correct positions).</p>
  
  <p>Is there a way to do this without doing the full calculation on every Vector (i.e. transforming the positionbefore-Vector)? It needs to be applied to a (very) large number of objects every frame, thus it needs to be (fairly) optimized.</p>
  
  <p>Edit: Scaling will ALWAYS be uniform.</p>