Vector4
Class representing a 4D vector with x, y, z, w components.
A 4D vector is an ordered quadruplet of numbers (labeled x, y, z, and
w), which can be used to represent a point in 4D space, a direction and
length, or any arbitrary ordered quadruplet of numbers.
- Mathematical operations (add, subtract, multiply, divide)
- Vector operations (dot product, normalization)
- Geometric operations (distance, clamping)
- Matrix transformations (4x4 matrix operations)
Methods
Vector4()
Default constructor - creates a vector at origin (0, 0, 0, 1)
Vector4(f32 x, f32 y, f32 z, f32 w)
Value constructor for common init path
| x | X component of the vector | 0.0f |
| y | Y component of the vector | 0.0f |
| z | Z component of the vector | 0.0f |
| w | W component of the vector | 1.0f |
Vector4& set(f32 x, f32 y, f32 z, f32 w)
Sets the vector components
| x | X component value | |
| y | Y component value | |
| z | Z component value | |
| w | W component value |
Vector4& setScalar(f32 scalar)
Sets all components to the same value
| scalar | Value to set for all components |
Vector4& setX(f32 x)
Sets the x component
| x | X component value |
Vector4& setY(f32 y)
Sets the y component
| y | Y component value |
Vector4& setZ(f32 z)
Sets the z component
| z | Z component value |
Vector4& setW(f32 w)
Sets the w component
| w | W component value |
Vector4& setComponent(u32 index, f32 value)
Sets component by index (0=x, 1=y, 2=z, 3=w)
| index | Component index (0, 1, 2, or 3) | |
| value | Value to set |
f32 getComponent(u32 index) const
Gets component by index (0=x, 1=y, 2=z, 3=w)
| index | Component index (0, 1, 2, or 3) |
Vector4 clone() const
Creates a copy of this vector
Vector4& copy(const Vector4& v)
Copies values from another vector
| v | Source vector |
Vector4& copy(const Vector3& v)
Copies values from a Vector3 (w defaults to 1)
| v | Vector3 to copy from |
Vector4& add(const Vector4& v)
Adds another vector to this one
| v | Vector to add |
Vector4& addScalar(f32 s)
Adds scalar to all components
| s | Scalar value to add |
Vector4& addVectors(const Vector4& a, const Vector4& b)
Sets this vector to a + b
| a | First vector | |
| b | Second vector |
Vector4& addScaledVector(const Vector4& v, f32 s)
Adds scaled vector to this one
| v | Vector to scale and add | |
| s | Scale factor |
Vector4& sub(const Vector4& v)
Subtracts another vector from this one
| v | Vector to subtract |
Vector4& subScalar(f32 s)
Subtracts scalar from all components
| s | Scalar value to subtract |
Vector4& subVectors(const Vector4& a, const Vector4& b)
Sets this vector to a - b
| a | First vector | |
| b | Second vector |
Vector4& multiply(const Vector4& v)
Multiplies this vector by another component-wise
| v | Vector to multiply by |
Vector4& multiplyScalar(f32 scalar)
Multiplies all components by scalar
| scalar | Scalar value to multiply by |
Vector4& divide(const Vector4& v)
Divides this vector by another component-wise
| v | Vector to divide by |
Vector4& divideScalar(f32 scalar)
Divides all components by scalar
| scalar | Scalar value to divide by |
Vector4& applyMatrix4(const Matrix4& m)
Multiplies this vector with the given 4x4 matrix
| m | 4x4 matrix to apply |
Vector4& min(const Vector4& v)
Sets each component to minimum of this and v
| v | Vector to compare with |
Vector4& max(const Vector4& v)
Sets each component to maximum of this and v
| v | Vector to compare with |
Vector4& clamp(const Vector4& min, const Vector4& max)
Clamps each component between min and max vectors
| min | Minimum values for each component | |
| max | Maximum values for each component |
Vector4& clampScalar(f32 minVal, f32 maxVal)
Clamps each component between scalar values
| minVal | Minimum value for all components | |
| maxVal | Maximum value for all components |
Vector4& clampLength(f32 min, f32 max)
Clamps vector length between min and max values
| min | Minimum length | |
| max | Maximum length |
Vector4& floor()
Rounds each component down to nearest integer
Vector4& ceil()
Rounds each component up to nearest integer
Vector4& round()
Rounds each component to nearest integer
Vector4& roundToZero()
Rounds each component toward zero
Vector4& negate()
Negates all components
f32 dot(const Vector4& v) const
Calculates dot product with another vector
| v | Vector to dot with |
f32 lengthSq() const
Calculates squared length of vector
f32 length() const
Calculates length of vector
f32 manhattanLength() const
Calculates Manhattan length of vector
Vector4& normalize()
Normalizes vector to unit length
Vector4& setLength(f32 length)
Sets vector to specified length while preserving direction
| length | Desired length |
Vector4& lerp(const Vector4& v, f32 alpha)
Linearly interpolates toward another vector
| v | Target vector | |
| alpha | Interpolation factor (0-1) |
Vector4& lerpVectors(const Vector4& v1, const Vector4& v2, f32 alpha)
Sets this vector to lerp between v1 and v2
| v1 | First vector | |
| v2 | Second vector | |
| alpha | Interpolation factor (0-1) |
Vector4& setFromMatrixPosition(const Matrix4& m)
Sets vector components to the position elements of the given transformation matrix
| m | 4x4 matrix |
Vector4& setAxisAngleFromQuaternion(const Quaternion& q)
Sets the x, y and z components to the quaternion's axis and w to the angle
| q | Quaternion to set from |
Vector4& setAxisAngleFromRotationMatrix(const Matrix4& m)
Sets the x, y and z components to the axis of rotation and w to the angle
| m | 4x4 matrix of which the upper left 3x3 matrix is a pure rotation matrix |
bool equals(const Vector4& v) const
Checks if this vector equals another
| v | Vector to compare with |
Vector4& fromArray(const f32* array, u32 offset)
Sets vector from array of floats
| array | Array of float values | |
| offset | Starting offset in array |
f32* toArray(f32* array, u32 offset) const
Writes vector components to array
| array | Target array (nullptr for static temp) | |
| offset | Starting offset in array |
Vector4& fromBufferAttribute(const class BufferAttribute& attribute, u32 index)
Sets vector from GPU buffer attribute
| attribute | Buffer attribute to read from | |
| index | Vertex index in buffer |
Vector4& random()
Sets vector to random values between 0 and 1
Vector4& operator+=(const Vector4& v)
Adds another vector to this one
| v | Vector to add |
Vector4& operator-=(const Vector4& v)
Subtracts another vector from this one
| v | Vector to subtract |
Vector4& operator*=(f32 scalar)
Multiplies all components by scalar
| scalar | Scalar value to multiply by |
Vector4& operator/=(f32 scalar)
Divides all components by scalar
| scalar | Scalar value to divide by |
bool operator==(const Vector4& v) const
Checks if this vector equals another
| v | Vector to compare with |
bool operator!=(const Vector4& v) const
Checks if this vector does not equal another
| v | Vector to compare with |
Properties
| id | u32 | Unique identifier |
| isVector4 | bool | Read-only tag |
| name | string | Optional display name |
| uuid | string | UUID string |
| w | f32 | W component (height alias) |
| x | f32 | X component |
| y | f32 | Y component |
| z | f32 | Z component (width alias) |