Search Unity

How would I accomplish this via job system?

Discussion in 'Data Oriented Technology Stack' started by Lecht, Mar 24, 2018.

  1. Lecht

    Lecht

    Joined:
    Jul 1, 2014
    Posts:
    24
    How would you do something like this in a job? The only part I'm not sure about is the vector transformations. Not sure how they work behind the scenes.

    This is what I'm trying to convert:

    Code (CSharp):
    1. Vector3 p = transform2.position;
    2. p.z = transform.position.z;
    3. transform.position = p;
    4. transform.rotation = transform2.rotation;
    5. transform.localScale = Vector3.one;
    6. transform.localScale = transform.InverseTransformVector (transform2.TransformVector(transform2.localScale));
    This is what I came up with:

    Code (CSharp):
    1. struct TransformJob : IJob
    2. {
    3.     public TransformAccess Transform;
    4.     public TransformAccess Transform2;
    5.     public void Execute()
    6.     {
    7.         Vector3 p = Transform2.position;
    8.         p.z = Transform.position.z;
    9.         Transform.position = p;
    10.         Transform.rotation = Transform2.rotation;
    11.         Transform.localScale = Vector3.one;
    12.  
    13.         var initialMatrix = Matrix4x4.TRS(Transform2.position, Transform2.rotation, Transform2.localScale);
    14.         var scaleMatrix = Matrix4x4.Scale(Transform2.localScale);
    15.  
    16.         var transformed = initialMatrix*scaleMatrix;
    17.  
    18.         var initialMatrix2 = Matrix4x4.TRS(Transform.position, Transform.rotation, Transform.localScale).inverse * transformed;
    19.  
    20.         Transform.localScale = initialMatrix2.lossyScale;
    21.     }
    22. }
    23.