Documentation

Unity Engine

Download Unity

Unity Engine

quaternion4

Four quaternions stored as a structure of arrays, to process four rotations at the same time.
Read time 3 minutesLast updated 3 hours ago

Definition

public struct quaternion4 : IEquatable<quaternion4>, IFormattable

Remarks

Use a quaternion4 to apply the same operation to four rotations at once, for better performance than processing the rotations one at a time. For example, you can blend the rotation channels of a rig four at a time. The rotation channels of an AnimationStream are stored as quaternion4 chunks.
Each of the fields quaternion4.x, quaternion4.y, quaternion4.z, and quaternion4.w is a
float4
that holds one component of all four quaternions. Lane 0 of every field belongs to the first quaternion, lane 1 to the second quaternion, and so on. This layout is called structure of arrays and lets the central processing unit (CPU) process the four lanes together with single instruction, multiple data (SIMD) instructions.
The operators of this struct and the methods in Quaternion4Extensions apply the operation to each lane separately.
The following example uses the
Unity.Animations
and
Unity.Mathematics
namespaces. It combines four parent rotations with four local rotations in one operation, then reads each result back.
Additional Resources: Quaternion4Extensions

Examples

// This example combines four parent rotations with four local rotations in one operation.// Lane i of the result is math.mul(parentRotations[i], localRotations[i]).public static quaternion[] CombineFourRotations(quaternion[] parentRotations, quaternion[] localRotations){ // Pack four quaternions into one quaternion4. Each field of a quaternion4 is a float4 that holds one // component of all four quaternions. For example, parents.x holds the x components of the four parent // rotations. The y, z, and w fields hold the other components in the same way. quaternion4 parents = new quaternion4( parentRotations[0], parentRotations[1], parentRotations[2], parentRotations[3]); quaternion4 locals = new quaternion4( localRotations[0], localRotations[1], localRotations[2], localRotations[3]); // Multiply the four pairs at once. quaternion4 combined = parents.Mul(locals); // Read each lane back as a regular quaternion. quaternion[] results = new quaternion[4]; for (int lane = 0; lane < 4; ++lane) { results[lane] = math.quaternion( combined.x[lane], combined.y[lane], combined.z[lane], combined.w[lane]); } return results;}

Fields

Value

Description

wThe w components of the four quaternions, one in each lane.
xThe x components of the four quaternions, one in each lane.
yThe y components of the four quaternions, one in each lane.
zThe z components of the four quaternions, one in each lane.

Static Fields

Value

Description

identityA quaternion4 where all four quaternions are the identity rotation.

Constructors

Constructor

Description

quaternion4(Unity.Mathematics.float4,Unity.Mathematics.float4,Unity.Mathematics.float4,Unity.Mathematics.float4)Creates a quaternion4 from four vectors that hold the components of the four quaternions.
quaternion4(Unity.Mathematics.float4x4)Creates a quaternion4 from the four columns of a matrix.
quaternion4(Unity.Mathematics.quaternion,Unity.Mathematics.quaternion,Unity.Mathematics.quaternion,Unity.Mathematics.quaternion)Creates a quaternion4 from four quaternions.

Methods

Method

Description

EqualsDetermines whether this quaternion4 equals another object.
GetHashCodeComputes a hash code for the quaternion4.
ToStringConverts the quaternion4 to a string.

Operators

Operator

Description

operator +Adds the components of two quaternion4 values.
operator *Multiplies every component of a quaternion4 by a scalar.