globalTransform property

Matrix4 get globalTransform

The world-space transform of this node, with every ancestor's transform applied.

Cached: O(1) when the cache is current, recomputed up the parent chain only after a transform change.

Implementation

Matrix4 get globalTransform {
  if (!_worldTransformDirty) return _worldTransform;
  final parent = _parent;
  final selfFlip =
      !excludeFromWindingParity && _localTransform.determinant() < 0;
  if (parent == null) {
    _worldTransform.setFrom(_localTransform);
    _windingFlipped = selfFlip;
  } else {
    _worldTransform
      ..setFrom(parent.globalTransform)
      ..multiply(_localTransform);
    // parent.globalTransform above refreshed the parent's cache, so
    // parent._windingFlipped is current.
    _windingFlipped = selfFlip != parent._windingFlipped;
  }
  _worldTransformDirty = false;
  return _worldTransform;
}
set globalTransform (Matrix4 transform)

Assigns the world-space transform of this node, automatically computing the localTransform needed to place the node at transform given the current parent transform.

If the node has no parent, this is equivalent to assigning localTransform directly.

Implementation

set globalTransform(Matrix4 transform) {
  final parent = _parent;
  if (parent == null) {
    localTransform = transform;
  } else {
    // Solve `transform == parent.globalTransform * localTransform` for
    // localTransform. (`Matrix4.invert` returns the determinant and mutates
    // the receiver — `copyInverse` is the non-destructive version.)
    final parentInverse = Matrix4.identity();
    parent.globalTransform.copyInverse(parentInverse);
    localTransform = parentInverse * transform;
  }
}