Struct Transform3D
- Namespace
- Godot
- Assembly
- GodotSharp.dll
3×4 matrix (3 rows, 4 columns) used for 3D linear transformations. It can represent transformations such as translation, rotation, or scaling. It consists of a Basis (first 3 columns) and a Vector3 for the origin (last column).
For more information, read this documentation article: https://docs.godotengine.org/en/latest/tutorials/math/matrices_and_transforms.html
public struct Transform3D : IEquatable<Transform3D>
- Implements
- Inherited Members
Constructors
Transform3D(Basis, Vector3)
Constructs a transformation matrix from the given basis
and
origin
vector.
public Transform3D(Basis basis, Vector3 origin)
Parameters
basis
BasisThe Basis to create the basis from.
origin
Vector3The origin vector, or column index 3.
Transform3D(Projection)
Constructs a transformation matrix from the given projection
by trimming the last row of the projection matrix (projection.X.W
,
projection.Y.W
, projection.Z.W
, and projection.W.W
are not copied over).
public Transform3D(Projection projection)
Parameters
projection
ProjectionThe Projection to create the transform from.
Transform3D(Vector3, Vector3, Vector3, Vector3)
Constructs a transformation matrix from 4 vectors (matrix columns).
public Transform3D(Vector3 column0, Vector3 column1, Vector3 column2, Vector3 origin)
Parameters
column0
Vector3The X vector, or column index 0.
column1
Vector3The Y vector, or column index 1.
column2
Vector3The Z vector, or column index 2.
origin
Vector3The origin vector, or column index 3.
Transform3D(float, float, float, float, float, float, float, float, float, float, float, float)
Constructs a transformation matrix from the given components.
Arguments are named such that xy is equal to calling Basis.X.Y
.
public Transform3D(float xx, float yx, float zx, float xy, float yy, float zy, float xz, float yz, float zz, float ox, float oy, float oz)
Parameters
xx
floatThe X component of the X column vector, accessed via
t.Basis.X.X
or[0][0]
.yx
floatThe X component of the Y column vector, accessed via
t.Basis.Y.X
or[1][0]
.zx
floatThe X component of the Z column vector, accessed via
t.Basis.Z.X
or[2][0]
.xy
floatThe Y component of the X column vector, accessed via
t.Basis.X.Y
or[0][1]
.yy
floatThe Y component of the Y column vector, accessed via
t.Basis.Y.Y
or[1][1]
.zy
floatThe Y component of the Z column vector, accessed via
t.Basis.Y.Y
or[2][1]
.xz
floatThe Z component of the X column vector, accessed via
t.Basis.X.Y
or[0][2]
.yz
floatThe Z component of the Y column vector, accessed via
t.Basis.Y.Y
or[1][2]
.zz
floatThe Z component of the Z column vector, accessed via
t.Basis.Y.Y
or[2][2]
.ox
floatThe X component of the origin vector, accessed via
t.Origin.X
or[2][0]
.oy
floatThe Y component of the origin vector, accessed via
t.Origin.Y
or[2][1]
.oz
floatThe Z component of the origin vector, accessed via
t.Origin.Z
or[2][2]
.
Fields
Basis
The Basis of this transform. Contains the X, Y, and Z basis vectors (columns 0 to 2) and is responsible for rotation and scale.
public Basis Basis
Field Value
Origin
The origin vector (column 3, the fourth column). Equivalent to array index [3]
.
public Vector3 Origin
Field Value
Properties
FlipX
The transform that will flip something along the X axis.
public static Transform3D FlipX { get; }
Property Value
- Transform3D
Equivalent to
new Transform(Vector3.Left, Vector3.Up, Vector3.Back, Vector3.Zero)
.
FlipY
The transform that will flip something along the Y axis.
public static Transform3D FlipY { get; }
Property Value
- Transform3D
Equivalent to
new Transform(Vector3.Right, Vector3.Down, Vector3.Back, Vector3.Zero)
.
FlipZ
The transform that will flip something along the Z axis.
public static Transform3D FlipZ { get; }
Property Value
- Transform3D
Equivalent to
new Transform(Vector3.Right, Vector3.Up, Vector3.Forward, Vector3.Zero)
.
Identity
The identity transform, with no translation, rotation, or scaling applied.
This is used as a replacement for Transform()
in GDScript.
Do not use new Transform()
with no arguments in C#, because it sets all values to zero.
public static Transform3D Identity { get; }
Property Value
- Transform3D
Equivalent to
new Transform(Vector3.Right, Vector3.Up, Vector3.Back, Vector3.Zero)
.
this[int]
public Vector3 this[int column] { readonly get; set; }
Parameters
column
intWhich column vector.
Property Value
Exceptions
- ArgumentOutOfRangeException
column
is not 0, 1, 2 or 3.
this[int, int]
Access matrix elements in column-major order. The fourth column is the Origin vector.
public float this[int column, int row] { readonly get; set; }
Parameters
column
intWhich column, the matrix horizontal position.
row
intWhich row, the matrix vertical position.
Property Value
Methods
AffineInverse()
Returns the inverse of the transform, under the assumption that the basis is invertible (must have non-zero determinant).
public readonly Transform3D AffineInverse()
Returns
- Transform3D
The inverse transformation matrix.
- See Also
Equals(Transform3D)
Returns true if the transforms are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Transform3D) instead, which is more reliable.
public readonly bool Equals(Transform3D other)
Parameters
other
Transform3DThe other transform to compare.
Returns
- bool
Whether or not the matrices are exactly equal.
Equals(object?)
Returns true if the transform is exactly equal
to the given object (obj
).
Note: Due to floating-point precision errors, consider using
IsEqualApprox(Transform3D) instead, which is more reliable.
public override readonly bool Equals(object? obj)
Parameters
obj
objectThe object to compare with.
Returns
- bool
Whether or not the transform and the object are exactly equal.
GetHashCode()
Serves as the hash function for Transform3D.
public override readonly int GetHashCode()
Returns
- int
A hash code for this transform.
InterpolateWith(Transform3D, float)
Returns a transform interpolated between this transform and another
transform
by a given weight
(on the range of 0.0 to 1.0).
public readonly Transform3D InterpolateWith(Transform3D transform, float weight)
Parameters
transform
Transform3DThe other transform.
weight
floatA value on the range of 0.0 to 1.0, representing the amount of interpolation.
Returns
- Transform3D
The interpolated transform.
Inverse()
Returns the inverse of the transform, under the assumption that the transformation basis is orthonormal (i.e. rotation/reflection is fine, scaling/skew is not). Use AffineInverse() for non-orthonormal transforms (e.g. with scaling).
public readonly Transform3D Inverse()
Returns
- Transform3D
The inverse matrix.
IsEqualApprox(Transform3D)
Returns true if this transform and other
are approximately equal,
by running IsEqualApprox(Vector3) on each component.
public readonly bool IsEqualApprox(Transform3D other)
Parameters
other
Transform3DThe other transform to compare.
Returns
- bool
Whether or not the matrices are approximately equal.
IsFinite()
Returns true if this transform is finite, by calling IsFinite(float) on each component.
public readonly bool IsFinite()
Returns
- bool
Whether this vector is finite or not.
LookingAt(Vector3, Vector3?, bool)
Returns a copy of the transform rotated such that the forward axis (-Z)
points towards the target
position.
The up axis (+Y) points as close to the up
vector
as possible while staying perpendicular to the forward axis.
The resulting transform is orthonormalized.
The existing rotation, scale, and skew information from the original transform is discarded.
The target
and up
vectors cannot be zero,
cannot be parallel to each other, and are defined in global/parent space.
public readonly Transform3D LookingAt(Vector3 target, Vector3? up = null, bool useModelFront = false)
Parameters
target
Vector3The object to look at.
up
Vector3?The relative up direction.
useModelFront
boolIf true, then the model is oriented in reverse, towards the model front axis (+Z, Vector3.ModelFront), which is more useful for orienting 3D models.
Returns
- Transform3D
The resulting transform.
Orthonormalized()
Returns the transform with the basis orthogonal (90 degrees), and normalized axis vectors (scale of 1 or -1).
public readonly Transform3D Orthonormalized()
Returns
- Transform3D
The orthonormalized transform.
Rotated(Vector3, float)
Rotates the transform around the given axis
by angle
(in radians).
The axis must be a normalized vector.
The operation is done in the parent/global frame, equivalent to
multiplying the matrix from the left.
public readonly Transform3D Rotated(Vector3 axis, float angle)
Parameters
axis
Vector3The axis to rotate around. Must be normalized.
angle
floatThe angle to rotate, in radians.
Returns
- Transform3D
The rotated transformation matrix.
RotatedLocal(Vector3, float)
Rotates the transform around the given axis
by angle
(in radians).
The axis must be a normalized vector.
The operation is done in the local frame, equivalent to
multiplying the matrix from the right.
public readonly Transform3D RotatedLocal(Vector3 axis, float angle)
Parameters
axis
Vector3The axis to rotate around. Must be normalized.
angle
floatThe angle to rotate, in radians.
Returns
- Transform3D
The rotated transformation matrix.
Scaled(Vector3)
Scales the transform by the given 3D scale
factor.
The operation is done in the parent/global frame, equivalent to
multiplying the matrix from the left.
public readonly Transform3D Scaled(Vector3 scale)
Parameters
scale
Vector3The scale to introduce.
Returns
- Transform3D
The scaled transformation matrix.
ScaledLocal(Vector3)
Scales the transform by the given 3D scale
factor.
The operation is done in the local frame, equivalent to
multiplying the matrix from the right.
public readonly Transform3D ScaledLocal(Vector3 scale)
Parameters
scale
Vector3The scale to introduce.
Returns
- Transform3D
The scaled transformation matrix.
ToString()
Converts this Transform3D to a string.
public override readonly string ToString()
Returns
- string
A string representation of this transform.
ToString(string?)
Converts this Transform3D to a string with the given format
.
public readonly string ToString(string? format)
Parameters
format
string
Returns
- string
A string representation of this transform.
Translated(Vector3)
Translates the transform by the given offset
.
The operation is done in the parent/global frame, equivalent to
multiplying the matrix from the left.
public readonly Transform3D Translated(Vector3 offset)
Parameters
offset
Vector3The offset to translate by.
Returns
- Transform3D
The translated matrix.
TranslatedLocal(Vector3)
Translates the transform by the given offset
.
The operation is done in the local frame, equivalent to
multiplying the matrix from the right.
public readonly Transform3D TranslatedLocal(Vector3 offset)
Parameters
offset
Vector3The offset to translate by.
Returns
- Transform3D
The translated matrix.
Operators
operator ==(Transform3D, Transform3D)
Returns true if the transforms are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Transform3D) instead, which is more reliable.
public static bool operator ==(Transform3D left, Transform3D right)
Parameters
left
Transform3DThe left transform.
right
Transform3DThe right transform.
Returns
- bool
Whether or not the transforms are exactly equal.
operator !=(Transform3D, Transform3D)
Returns true if the transforms are not equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Transform3D) instead, which is more reliable.
public static bool operator !=(Transform3D left, Transform3D right)
Parameters
left
Transform3DThe left transform.
right
Transform3DThe right transform.
Returns
- bool
Whether or not the transforms are not equal.
operator *(Aabb, Transform3D)
Returns an AABB transformed (multiplied) by the inverse transformation matrix,
under the assumption that the transformation basis is orthonormal (i.e. rotation/reflection
is fine, scaling/skew is not).
aabb * transform
is equivalent to transform.Inverse() * aabb
. See Inverse().
For transforming by inverse of an affine transformation (e.g. with scaling) transform.AffineInverse() * aabb
can be used instead. See AffineInverse().
public static Aabb operator *(Aabb aabb, Transform3D transform)
Parameters
aabb
AabbAn AABB to inversely transform.
transform
Transform3DThe transformation to apply.
Returns
- Aabb
The inversely transformed AABB.
operator *(Plane, Transform3D)
Returns a Plane transformed (multiplied) by the inverse transformation matrix.
plane * transform
is equivalent to transform.AffineInverse() * plane
. See AffineInverse().
public static Plane operator *(Plane plane, Transform3D transform)
Parameters
plane
PlaneA Plane to inversely transform.
transform
Transform3DThe transformation to apply.
Returns
- Plane
The inversely transformed Plane.
operator *(Transform3D, Aabb)
Returns an AABB transformed (multiplied) by the transformation matrix.
public static Aabb operator *(Transform3D transform, Aabb aabb)
Parameters
transform
Transform3DThe transformation to apply.
aabb
AabbAn AABB to transform.
Returns
- Aabb
The transformed AABB.
operator *(Transform3D, Plane)
Returns a Plane transformed (multiplied) by the transformation matrix.
public static Plane operator *(Transform3D transform, Plane plane)
Parameters
transform
Transform3DThe transformation to apply.
plane
PlaneA Plane to transform.
Returns
- Plane
The transformed Plane.
operator *(Transform3D, Transform3D)
Composes these two transformation matrices by multiplying them together. This has the effect of transforming the second transform (the child) by the first transform (the parent).
public static Transform3D operator *(Transform3D left, Transform3D right)
Parameters
left
Transform3DThe parent transform.
right
Transform3DThe child transform.
Returns
- Transform3D
The composed transform.
operator *(Transform3D, Vector3)
Returns a Vector3 transformed (multiplied) by the transformation matrix.
public static Vector3 operator *(Transform3D transform, Vector3 vector)
Parameters
transform
Transform3DThe transformation to apply.
vector
Vector3A Vector3 to transform.
Returns
- Vector3
The transformed Vector3.
operator *(Transform3D, Vector3[])
Returns a copy of the given Vector3[] transformed (multiplied) by the transformation matrix.
public static Vector3[] operator *(Transform3D transform, Vector3[] array)
Parameters
transform
Transform3DThe transformation to apply.
array
Vector3[]A Vector3[] to transform.
Returns
- Vector3[]
The transformed copy of the Vector3[].
operator *(Vector3, Transform3D)
Returns a Vector3 transformed (multiplied) by the inverse transformation matrix,
under the assumption that the transformation basis is orthonormal (i.e. rotation/reflection
is fine, scaling/skew is not).
vector * transform
is equivalent to transform.Inverse() * vector
. See Inverse().
For transforming by inverse of an affine transformation (e.g. with scaling) transform.AffineInverse() * vector
can be used instead. See AffineInverse().
public static Vector3 operator *(Vector3 vector, Transform3D transform)
Parameters
vector
Vector3A Vector3 to inversely transform.
transform
Transform3DThe transformation to apply.
Returns
- Vector3
The inversely transformed Vector3.
operator *(Vector3[], Transform3D)
Returns a copy of the given Vector3[] transformed (multiplied) by the inverse transformation matrix,
under the assumption that the transformation basis is orthonormal (i.e. rotation/reflection
is fine, scaling/skew is not).
array * transform
is equivalent to transform.Inverse() * array
. See Inverse().
For transforming by inverse of an affine transformation (e.g. with scaling) transform.AffineInverse() * array
can be used instead. See AffineInverse().
public static Vector3[] operator *(Vector3[] array, Transform3D transform)
Parameters
array
Vector3[]A Vector3[] to inversely transform.
transform
Transform3DThe transformation to apply.
Returns
- Vector3[]
The inversely transformed copy of the Vector3[].