Gets the number of bytes per component in the underlying typed array.
The number of bytes per element (4 for Float32Array, 8 for Float64Array)
Gets the class name of this vector type.
The string "Vector4"
Converts the vector to a GLSL vec4 string representation with float precision.
A GLSL-compatible vec4 string (e.g., "vec4(1.0, 2.0, 3.0, 4.0)")
Converts the vector to a GLSL ivec4 string representation with integer values.
A GLSL-compatible ivec4 string (e.g., "ivec4(1, 2, 3, 4)")
Converts the vector to a GLSL uvec4 string representation with unsigned integer values.
A GLSL-compatible uvec4 string (e.g., "uvec4(1u, 2u, 3u, 4u)")
Gets the W component of the vector.
The W component value
Converts the vector to a WGSL vec4f string representation with float precision.
A WGSL-compatible vec4f string (e.g., "vec4f(1.0, 2.0, 3.0, 4.0)")
Converts the vector to a WGSL vec4i string representation with integer values.
A WGSL-compatible vec4i string (e.g., "vec4i(1, 2, 3, 4)")
Converts the vector to a WGSL vec4u string representation with unsigned integer values.
A WGSL-compatible vec4u string (e.g., "vec4u(1u, 2u, 3u, 4u)")
Gets the X component of the vector.
The X component value
Gets the Y component of the vector.
The Y component value
Gets the Z component of the vector.
The Z component value
StaticcompositionGets the component value at the specified index.
The index (0=x, 1=y, 2=z, 3=w)
The component value at the specified index
Creates a deep copy of this vector.
A new Vector4d instance with the same component values
Calculates the dot product between this vector and another vector. The dot product is the sum of the products of corresponding components.
The vector to calculate dot product with
The dot product of the two vectors
Converts the vector to a flat array representation.
An array containing the vector components [x, y, z, w]
Checks if this vector is a dummy vector (has no components).
True if the vector has no components, false otherwise
Checks if this vector is approximately equal to another vector within a specified tolerance.
The vector to compare with
The tolerance value for comparison (default: Number.EPSILON)
True if the vectors are approximately equal, false otherwise
Checks if this vector is strictly equal to another vector (exact component equality).
The vector to compare with
True if all components are exactly equal, false otherwise
Checks if the internal storage shares the same ArrayBuffer as the provided one. Useful for determining if vectors share underlying memory.
The ArrayBuffer to compare against
True if the same ArrayBuffer is used, false otherwise
Calculates the length (magnitude) of the vector using the Euclidean norm.
The length of the vector
Calculates the squared length (magnitude) of the vector. This is more efficient than length() when only comparing magnitudes.
The squared length of the vector
Calculates the distance from this vector to another vector.
The target vector to calculate distance to
The distance between the two vectors
Converts the vector to a string representation in the format "(x, y, z, w)".
A string representation of the vector
Converts the vector to an approximately formatted string representation with financial precision. Each component is formatted to a fixed number of decimal places.
A string with space-separated components followed by a newline
Gets the component value at the specified index. Alias for the at() method for convenience.
The index of the component to retrieve
The component value at the given index
Static_Adds two vectors component-wise and returns a new vector.
The left operand vector
The right operand vector
The typed array constructor to use for internal storage
A new vector containing the sum (l_vec + r_vec)
Static_Divides a vector by a scalar value and returns a new vector. If the divisor is zero, the result components will be set to Infinity and an error will be logged.
The vector to divide
The scalar value to divide by
The typed array constructor to use for internal storage
A new vector with each component divided by the scalar
Static_Divides the left vector by the right vector component-wise and returns a new vector. If any component of the right vector is zero, the corresponding result component will be set to Infinity.
The left operand vector (dividend)
The right operand vector (divisor)
The typed array constructor to use for internal storage
A new vector with component-wise division result
Static_Creates a dummy vector with no components (empty array). This is used as a placeholder when a vector is needed but not yet initialized.
The typed array constructor to use for internal storage
A new dummy vector with empty components
Static_Creates a new vector from individual component values.
The X component value
The Y component value
The Z component value
The W component value
The typed array constructor to use for internal storage
A new vector instance with the specified component values
Static_Creates a new vector from an array by copying the first 4 values. If the array has fewer than 4 elements, the remaining components will be undefined.
Array containing numeric values (at least 4 elements recommended)
The typed array constructor to use for internal storage
A new vector instance with the first 4 values from the array
Static_Creates a new vector from a 4-element array by copying the values.
Array containing exactly 4 numeric values [x, y, z, w]
The typed array constructor to use for internal storage
A new vector instance with the copied values
Static_Creates a new Vector4 from a Vector3, setting the W component to 1. This is commonly used for converting 3D positions to homogeneous coordinates.
The source Vector3 to copy from
The typed array constructor to use for internal storage
A new Vector4 with (vec3.x, vec3.y, vec3.z, 1.0)
Static_Creates a new vector by copying values from another Vector4.
The source Vector4 to copy from
The typed array constructor to use for internal storage
A new vector instance with copied values from the source vector
Static_Creates a new Vector4 from a Vector2, setting Z to 0 and W to 1. This is commonly used for converting 2D positions to homogeneous coordinates.
The source Vector2 to copy from
The typed array constructor to use for internal storage
A new Vector4 with (vec2.x, vec2.y, 0.0, 1.0)
Static_Multiplies a vector by a scalar value and returns a new vector. This operation scales the vector while preserving its direction.
The vector to multiply
The scalar value to multiply by
The typed array constructor to use for internal storage
A new vector with each component multiplied by the scalar
Static_Multiplies two vectors component-wise and returns a new vector. This is also known as the Hadamard product or element-wise multiplication.
The left operand vector
The right operand vector
The typed array constructor to use for internal storage
A new vector with each component being the product of corresponding components
Static_Creates a normalized version of the given vector. A normalized vector has a length of 1 while maintaining its direction.
The vector to normalize
The typed array constructor to use for internal storage
A new normalized vector
Static_Creates a vector with all components set to 1 (1, 1, 1, 1).
The typed array constructor to use for internal storage
A new vector with all components set to 1
Static_Subtracts the right vector from the left vector component-wise and returns a new vector.
The left operand vector (minuend)
The right operand vector (subtrahend)
The typed array constructor to use for internal storage
A new vector containing the difference (l_vec - r_vec)
Static_Creates a zero vector (0, 0, 0, 0).
The typed array constructor to use for internal storage
A new zero vector
StaticaddStaticaddAdds two vectors component-wise and stores the result in the output vector. This method modifies the output vector in-place for better performance.
The left operand vector
The right operand vector
The output vector to store the result (will be modified)
The modified output vector containing the sum
StaticdivideDivides a vector by a scalar value and returns a new vector. If the divisor is zero, the result components will be set to Infinity and an error will be logged.
The vector to divide
The scalar value to divide by
A new Vector4d with each component divided by the scalar
StaticdivideDivides a vector by a scalar value and stores the result in the output vector. If the divisor is zero, the result components will be set to Infinity and an error will be logged. This method modifies the output vector in-place for better performance.
The vector to divide
The scalar value to divide by
The output vector to store the result (will be modified)
The modified output vector with each component divided by the scalar
StaticdivideDivides the left vector by the right vector component-wise and returns a new vector. If any component of the right vector is zero, the corresponding result component will be set to Infinity.
A new Vector4d with component-wise division result
StaticdivideDivides the left vector by the right vector component-wise and stores the result in the output vector. If any component of the right vector is zero, the corresponding result component will be set to Infinity. This method modifies the output vector in-place for better performance.
The left operand vector (dividend)
The right operand vector (divisor)
The output vector to store the result (will be modified)
The modified output vector with component-wise division result
StaticdotStaticdummyCreates a dummy vector with no components (empty array). This is used as a placeholder when a vector is needed but not yet initialized.
A new dummy Vector4d with empty components
StaticfromCreates a new Vector4d from an ArrayBuffer. The ArrayBuffer should contain at least 32 bytes (4 float64 values).
The ArrayBuffer containing vector data
A new Vector4d instance using the ArrayBuffer data
StaticfromCreates a new Vector4d from individual component values.
The X component value
The Y component value
The Z component value
The W component value
A new Vector4d instance with the specified component values
StaticfromCreates a new Vector4d by copying values from an array. Takes the first 4 elements from the array. If the array has fewer than 4 elements, the remaining components will be undefined.
Array containing numeric values (at least 4 elements recommended)
A new Vector4d instance with copied values
StaticfromStaticfromCreates a new Vector4d from a Float64Array. The Float64Array is used directly without copying.
The Float64Array containing vector components
A new Vector4d instance using the provided Float64Array
StaticlengthStaticlengthCalculates the squared length (magnitude) of a vector. This is more efficient than length() when only comparing magnitudes.
The vector to calculate squared length for
The squared length of the vector
StaticmultiplyStaticmultiplyMultiplies a vector by a scalar value and stores the result in the output vector. This method modifies the output vector in-place for better performance.
The vector to multiply
The scalar value to multiply by
The output vector to store the result (will be modified)
The modified output vector with each component multiplied by the scalar
StaticmultiplyMultiplies two vectors component-wise and returns a new vector. This is also known as the Hadamard product or element-wise multiplication.
A new Vector4d with each component being the product of corresponding components
StaticmultiplyMultiplies two vectors component-wise and stores the result in the output vector. This method modifies the output vector in-place for better performance.
The left operand vector
The right operand vector
The output vector to store the result (will be modified)
The modified output vector with component-wise multiplication result
StaticnormalizeStaticoneCreates a vector with all components set to 1 (1, 1, 1, 1).
A new Vector4d with all components set to 1
StaticsubtractStaticsubtractSubtracts the right vector from the left vector component-wise and stores the result in the output vector. This method modifies the output vector in-place for better performance.
The left operand vector (minuend)
The right operand vector (subtrahend)
The output vector to store the result (will be modified)
The modified output vector containing the difference
Staticzero
Immutable 4D(x,y,z,w) Vector class with 64-bit float components.
This class provides the same functionality as Vector4 but with double precision floating-point components. Use this when higher precision is required for mathematical calculations or when working with very large or very small numbers.
Example: Basic usage: