Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Alternatives to 'computeWorldPosition'

Discussion in 'Project Tiny' started by leeham9, Jan 30, 2019.

  1. leeham9

    leeham9

    Joined:
    Oct 25, 2015
    Posts:
    12
    I'm looking to try optimizing my game a bit, and I noticed that I use 'computeWorldPosition' quite a bit.

    Apparently it's quite resource intensive, and Unity has suggested using 'TransformObjectToWorld' instead. The issue, is that I'm not entirely sure how to use TransformObjectToWorld? I couldn't really find any documentation on it either. I've tried to apply the 4x4matrix of TransformObjectToWorld to a Vector3, but it always seems to be a bit off?

    Any help with this would be appreciated, and if I missed something in the documentation please link it for me!
     
    reallyhexln likes this.
  2. reallyhexln

    reallyhexln

    Joined:
    Jun 18, 2018
    Posts:
    69
    The same issue.
    I can convert world points to local points, window points to world points, world points to window points, get world position and get local position, but can not convert local position to world position.
    Is there no way to reach this with Tiny?
     
  3. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    78
    Not sure if it would be better, but you could write your own function to calculate the world position, since the world position from an entity is basically recursively the world position of its parent added to its own (local) position.
     
    reallyhexln likes this.
  4. ER_Dolleman

    ER_Dolleman

    Joined:
    Dec 10, 2018
    Posts:
    19
    This is how I'm doing it right now:
    Code (JavaScript):
    1.  
    2. public static ToGlobalPosition(world: ut.World, entity: ut.Entity):Vector3 {
    3.  
    4.             let node = world.getComponentData(entity, ut.Core2D.TransformNode);
    5.             let transform = world.getComponentData(entity, ut.Core2D.TransformLocalPosition);
    6.  
    7.             let worldpos = transform.position;
    8.  
    9.             let parent = node.parent;
    10.             while(!parent.isNone()) {
    11.                 let parentTransform = world.getComponentData(parent, ut.Core2D.TransformLocalPosition);
    12.                 worldpos = parentTransform.position.add(worldpos);
    13.  
    14.                 parent = world.getComponentData(parent, ut.Core2D.TransformNode).parent;
    15.             }
    16.  
    17.             return worldpos;
    18. }
    Not sure if this is better for performance however
     
    Last edited: Apr 11, 2019
    reallyhexln likes this.
  5. reallyhexln

    reallyhexln

    Joined:
    Jun 18, 2018
    Posts:
    69
    Thank you very much, elroydolleman_unity!
    computeWorldPosition having problems with calculating position for hierarchy with UI layouts, your solution do not have such issue, it just works.
    Thank you again!
     
    ER_Dolleman likes this.
  6. Deleted User

    Deleted User

    Guest

    Code (CSharp):
    1. const transformMatrix = world.getComponentData(entity, TransformObjectToWorld).matrix
    2. const pos = new Vector3().applyMatrix4(transformMatrix)
     
    ER_Dolleman likes this.