Search Unity

transform.InverseTransformDirection For ECS

Discussion in 'Entity Component System' started by Opeth001, Mar 29, 2019.

  1. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    Hi,

    Can someone Tell me how to use the equivalent of transform.InverseTransformDirection for ECS ?
    Knowing i have no access to the Transform Component within a ComponentSystem.
    Thank you.
     
  2. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    You have to access LocalToWorld component and manipulate its matrix directly (float4x4)
     
  3. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    I know but what's the logic behind transform.InverseTransformDirection.
    This is what I'm asking.
     
  4. topitsky

    topitsky

    Joined:
    Jan 12, 2016
    Posts:
    100
    Yes please, I'm stuck with this too. Would be nice if the unity.mathematics had some of the most common Transform math inside it.
     
    Last edited: Jul 23, 2019
  5. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    I haven't tested or really looked in, but I'd assume you'd just inverse your LocalToWorld matrix (math.inverse) to make a WorldToLocal matrix and use that.
     
    jdtec likes this.
  6. topitsky

    topitsky

    Joined:
    Jan 12, 2016
    Posts:
    100
    Code (CSharp):
    1.         public void Execute([ReadOnly] ref Velocity V, ref LocalVelocity LocalVelocity, [ReadOnly] ref Rotation Rotation, [ReadOnly] ref LocalToWorld LtW)
    2.         {
    3.             float3 l = math.mul(Rotation.Value, V.Value);
    4.             LocalVelocity.Value = new float3(l.x, l.y, l.z);
    5.         }
    This is what I'm trying to do. I have an animation system that needs the animators local velocity in order to find the right pose.
    This works for when the direction is forward or back, but doesnt work when the object is facing right or left.
    Any tips?
     
  7. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    if you want to use Transforms Functions like normal GO workflow, the Matrix4x4 contains many of them. (more infos here).

    you need to get the LocalToWorld ComponentData and use it to create a Matrix4x4.

    if you dont want to use the LocalToWorld ComponentData, you can create it using Translation, Rotation, Scale.
    Matrix4x4 matrix4x4 = Matrix4x4.identity;
    matrix4x4 .SetTRS(translation, Rotation, Scale);



    // Direction Functions
    //Matrix4x4 version of Transform.InverseTransformDirection
    Vector3 localDirection = matrix4x4.inverse.MultiplyVector(worldDirection);

    //Matrix4x4 version of Transform.TransformDirection
    Vector3 worldDirection = matrix4x4.MultiplyVector(localDirectionDirection);


    // Position Functions
    //Matrix4x4 version of Transform.InverseTransformPoint
    Vector3 localPosition = matrix4x4.inverse.MultiplyPoint3x4(worldPositon);

    // Matrix4x4 version of Transform.TransformPoint
    Vector3 worldPositon = matrix4x4.MultiplyPoint3x4(localPosition );

    Matrix4x4 is a struct so you can use all this inside jobs ;)
     
  8. topitsky

    topitsky

    Joined:
    Jan 12, 2016
    Posts:
    100
    Enormous thanks, Ill look into this

    EDIT: It works!
     
    Last edited: Jul 23, 2019
    Opeth001 likes this.
  9. filod

    filod

    Joined:
    Oct 3, 2015
    Posts:
    224
    in case someone still confusing:
    Code (CSharp):
    1. // InverseTransformPoint equivalent:
    2. math.transform(math.inverse(SomeLocalToWorld), WorldPostion)
     
  10. Sab_Rango

    Sab_Rango

    Joined:
    Aug 30, 2019
    Posts:
    121
    to get GO api: transform.TransformDirection() in ECS

    Code (CSharp):
    1.  
    2.         public static float3 LocalToWorld(this float4x4 localtoworld_Component, float3 direction)
    3.         {
    4.             return math.rotate(localtoworld_Component, direction);
    5.         }
     
    RaveBox, toomasio, jmcusack and 4 others like this.
  11. scottjdaley

    scottjdaley

    Joined:
    Aug 1, 2013
    Posts:
    163
    If you are on ECS 1.0 and using transforms v2, you can use LocalTransform.TransformDirection(), WorldTransform.TransformDirection() or the various methods available on the TransformAspect.
     
    Opeth001 likes this.