Search Unity

TransformAccess to float4x4, make it fast

Discussion in 'Entity Component System' started by davenirline, Jan 19, 2019.

  1. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    I have an IJobParallelForTransform in which I use TransformAccess to do some transformation. However, TransformAccess does not have the matrix values like Transform has. I had to derive the matrix which uses at least one matrix multiplication.

    Code (CSharp):
    1. private struct Job : IJobParallelForTransform {
    2.     public void Execute(int index, TransformAccess transform) {
    3.         float4x4 rotationTranslationMatrix = new float4x4(transform.rotation, transform.position);
    4.         float4x4 scaleMatrix = float4x4.Scale(transform.localScale);
    5.         float4x4 matrix = math.mul(rotationTranslationMatrix, scaleMatrix);
    6.         // ... Use matrix for transformations
    7.     }
    8. }
    Is there any way to avoid the matrix multiplication? It could have been if TransformAccess has stored Transform's matrix.
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Just use LocalToWorld component instead of TransformAccess? It's already got the transform matrix.
     
  3. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    I can't, as much as I want to. I'm using hybrid approach and I can't easily turn the existing setup into pure ECS.
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    I haven't used hybrid in a long time, but unless something has changed I'm pretty sure you can still have position/rotation/etc components on hybrid entities which will create a LocalToWorld component. Then just add a CopyTransformFromGameObject component to update them.
     
    Last edited: Jan 20, 2019
    davenirline likes this.
  5. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    I see. That's an interesting idea. I'll give it a try.