Struct Vector3
Represents a 3D vector with X, Y, and Z components. Commonly used for geometric operations, physics, lighting, and 3D movement.
public struct Vector3
- Inherited Members
Remarks
The struct is laid out sequentially as three 32-bit floats (12 bytes), making it directly compatible with SIMD operations and GPU shader inputs. Use Vector3 for hardware-accelerated batch processing.
Constructors
Vector3(float, float, float)
Initializes a new Vector3 with the specified components.
public Vector3(float x, float y, float z)
Parameters
Fields
Back
A unit vector pointing backward along the positive Z axis (0, 0, 1).
public static readonly Vector3 Back
Field Value
Down
A unit vector pointing down along the negative Y axis (0, -1, 0).
public static readonly Vector3 Down
Field Value
Forward
A unit vector pointing forward along the negative Z axis (0, 0, -1) in a right-handed coordinate system.
public static readonly Vector3 Forward
Field Value
Left
A unit vector pointing left along the negative X axis (-1, 0, 0).
public static readonly Vector3 Left
Field Value
One
A unit vector (1, 1, 1).
public static readonly Vector3 One
Field Value
Right
A unit vector pointing right along the positive X axis (1, 0, 0).
public static readonly Vector3 Right
Field Value
Up
A unit vector pointing up along the positive Y axis (0, 1, 0).
public static readonly Vector3 Up
Field Value
X
The X component of the vector.
public float X
Field Value
Y
The Y component of the vector.
public float Y
Field Value
Z
The Z component of the vector.
public float Z
Field Value
Zero
A zero vector (0, 0, 0).
public static readonly Vector3 Zero
Field Value
Properties
Magnitude
Returns the magnitude (length) of the vector.
public float Magnitude { get; }
Property Value
MagnitudeSquared
Returns the squared magnitude of the vector, avoiding a square root computation. Useful for distance comparisons where the exact length is not needed.
public float MagnitudeSquared { get; }
Property Value
Normalized
Returns a normalized (unit length) copy of this vector. Returns Zero if the vector has zero magnitude.
public Vector3 Normalized { get; }
Property Value
Methods
Angle(Vector3, Vector3)
Returns the angle in radians between two vectors. Returns 0 if either vector has zero magnitude.
public static float Angle(Vector3 lhs, Vector3 rhs)
Parameters
Returns
Cross(Vector3, Vector3)
Returns the cross product of two vectors. The result is a vector perpendicular to both inputs, with direction determined by the right-hand rule.
public static Vector3 Cross(Vector3 lhs, Vector3 rhs)
Parameters
Returns
Dot(Vector3, Vector3)
Returns the dot product of two vectors.
public static float Dot(Vector3 lhs, Vector3 rhs)
Parameters
Returns
Equals(object?)
Indicates whether this instance and a specified object are equal.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current instance.
Returns
- bool
true if
objand this instance are the same type and represent the same value; otherwise, false.
Equals(Vector3)
Returns true if this vector equals another Vector3.
public bool Equals(Vector3 other)
Parameters
otherVector3
Returns
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
Lerp(Vector3, Vector3, float)
Linearly interpolates between two vectors by the factor t.
public static Vector3 Lerp(Vector3 start, Vector3 end, float t)
Parameters
startVector3The start vector at t = 0.
endVector3The end vector at t = 1.
tfloatThe interpolation factor, typically in the range [0, 1].
Returns
Max(Vector3, Vector3)
Returns the component-wise maximum of two vectors.
public static Vector3 Max(Vector3 lhs, Vector3 rhs)
Parameters
Returns
Min(Vector3, Vector3)
Returns the component-wise minimum of two vectors.
public static Vector3 Min(Vector3 lhs, Vector3 rhs)
Parameters
Returns
Project(Vector3, Vector3)
Projects lhs onto rhs.
Returns Zero if rhs has zero magnitude.
public static Vector3 Project(Vector3 lhs, Vector3 rhs)
Parameters
Returns
Reflect(Vector3, Vector3)
Reflects the vector across the given surface normal.
The normal is assumed to be unit length.
public static Vector3 Reflect(Vector3 vector, Vector3 normal)
Parameters
vectorVector3The incident vector to reflect.
normalVector3The surface normal (should be normalized).
Returns
ToString()
Returns the fully qualified type name of this instance.
public override string ToString()
Returns
- string
The fully qualified type name.
Operators
operator +(Vector3, Vector3)
Adds two vectors.
public static Vector3 operator +(Vector3 lhs, Vector3 rhs)
Parameters
Returns
operator /(Vector3, float)
Divides a vector by a scalar.
public static Vector3 operator /(Vector3 v, float f)
Parameters
Returns
operator ==(Vector3, Vector3)
Returns true if two vectors have equal components.
public static bool operator ==(Vector3 lhs, Vector3 rhs)
Parameters
Returns
implicit operator Vector3(Vector3)
public static implicit operator Vector3(Vector3 v)
Parameters
vVector3
Returns
implicit operator Vector3((float x, float y, float z))
Implicitly converts a tuple to a Vector3.
public static implicit operator Vector3((float x, float y, float z) t)
Parameters
Returns
implicit operator Vector3(Point3)
public static implicit operator Vector3(Point3 p)
Parameters
pPoint3
Returns
implicit operator Vector3(Vector3)
Implicitly converts this vector to a Vector3 for use with SIMD-accelerated operations.
public static implicit operator Vector3(Vector3 v)
Parameters
vVector3
Returns
operator !=(Vector3, Vector3)
Returns true if any component of two vectors differs.
public static bool operator !=(Vector3 lhs, Vector3 rhs)
Parameters
Returns
operator *(float, Vector3)
Multiplies a scalar by a vector.
public static Vector3 operator *(float f, Vector3 v)
Parameters
Returns
operator *(Vector3, float)
Multiplies a vector by a scalar.
public static Vector3 operator *(Vector3 v, float f)
Parameters
Returns
operator *(Vector3, Vector3)
Multiplies two vectors component-wise.
public static Vector3 operator *(Vector3 lhs, Vector3 rhs)
Parameters
Returns
operator -(Vector3, Vector3)
Subtracts one vector from another.
public static Vector3 operator -(Vector3 lhs, Vector3 rhs)
Parameters
Returns
operator -(Vector3)
Negates a vector.
public static Vector3 operator -(Vector3 v)
Parameters
vVector3