@web-engine-dev/math / BatchMath
Variable: BatchMath
constBatchMath:object
High-performance batch math operations for transforming multiple vectors at once.
These functions operate on packed Float32Arrays to minimize allocations and maximize cache efficiency. Ideal for particle systems, mesh processing, and any scenario involving many similar transformations.
Type Declaration
addScalarVec3Array()
addScalarVec3Array(
source,scalar,out,count?):void
Adds a scalar to all components in a Vec3 array.
Parameters
source
Float32Array
Source array with packed Vec3s
scalar
number
Scalar to add
out
Float32Array
Output array
count?
number
Number of vectors
Returns
void
addVec2Array()
addVec2Array(
a,b,out,count?):void
Adds two Vec2 arrays element-wise. Optimized with loop unrolling for SIMD auto-vectorization.
Parameters
a
Float32Array
First array with packed Vec2s
b
Float32Array
Second array with packed Vec2s
out
Float32Array
Output array
count?
number
Number of vectors
Returns
void
addVec3Array()
addVec3Array(
a,b,out,count?):void
Adds two Vec3 arrays with loop unrolling for SIMD optimization.
Parameters
a
Float32Array
First array with packed Vec3s
b
Float32Array
Second array with packed Vec3s
out
Float32Array
Output array
count?
number
Number of vectors
Returns
void
computeMVP()
computeMVP(
model,view,projection,out):Mat4
Computes MVP matrix (Model * View * Projection) efficiently. Common operation for rendering - optimized to minimize temporaries.
Parameters
model
Model matrix
view
View matrix
projection
Projection matrix
out
Output MVP matrix
Returns
out for chaining
computeMVPBatch()
computeMVPBatch(
models,viewProjection,out,count?):void
Computes MVP matrices for multiple objects efficiently. Batch version that shares view-projection computation.
Parameters
models
Float32Array
Array of model matrices (packed Float32Array, 16 floats each)
viewProjection
Pre-computed view-projection matrix
out
Float32Array
Output array for MVP matrices
count?
number
Number of matrices
Returns
void
crossVec3Array()
crossVec3Array(
a,b,out,count?):void
Computes cross products between corresponding Vec3s in two arrays.
Parameters
a
Float32Array
First array with packed Vec3s
b
Float32Array
Second array with packed Vec3s
out
Float32Array
Output array with packed Vec3s
count?
number
Number of vectors
Returns
void
dotVec2Array()
dotVec2Array(
a,b,out,count?):void
Computes dot products between corresponding Vec2s.
Parameters
a
Float32Array
First array with packed Vec2s
b
Float32Array
Second array with packed Vec2s
out
Float32Array
Output array for dot products
count?
number
Number of vectors
Returns
void
dotVec3Array()
dotVec3Array(
a,b,out,count?):void
Computes dot products between corresponding Vec3s in two arrays.
Parameters
a
Float32Array
First array with packed Vec3s
b
Float32Array
Second array with packed Vec3s
out
Float32Array
Output array for dot products (length = count)
count?
number
Number of vectors
Returns
void
invertMat4Array()
invertMat4Array(
matrices,out,count?):boolean
Inverts an array of Mat4s.
Parameters
matrices
Float32Array
Source array with packed Mat4s
out
Float32Array
Output array
count?
number
Number of matrices
Returns
boolean
true if all matrices were invertible, false if any failed
lengthSqVec3Array()
lengthSqVec3Array(
source,out,count?):void
Computes squared lengths for all Vec3s in an array. Useful for distance comparisons without sqrt.
Parameters
source
Float32Array
Source array with packed Vec3s
out
Float32Array
Output array for squared lengths
count?
number
Number of vectors
Returns
void
lengthVec3Array()
lengthVec3Array(
source,out,count?):void
Computes lengths for all Vec3s in an array.
Parameters
source
Float32Array
Source array with packed Vec3s
out
Float32Array
Output array for lengths
count?
number
Number of vectors
Returns
void
lerpVec2Array()
lerpVec2Array(
a,b,t,out,count?):void
Linear interpolation between two Vec2 arrays.
Parameters
a
Float32Array
Start array with packed Vec2s
b
Float32Array
End array with packed Vec2s
t
number
Interpolation factor
out
Float32Array
Output array
count?
number
Number of vectors
Returns
void
lerpVec3Array()
lerpVec3Array(
a,b,t,out,count?,aOffset?,bOffset?,outOffset?):void
Linear interpolation between two Vec3 arrays.
Parameters
a
Float32Array
Start array with packed Vec3s
b
Float32Array
End array with packed Vec3s
t
number
Interpolation factor (0 = a, 1 = b)
out
Float32Array
Output array
count?
number
Number of vectors to interpolate
aOffset?
number = 0
Starting index in a array
bOffset?
number = 0
Starting index in b array
outOffset?
number = 0
Starting index in output array
Returns
void
multiplyMat4Array()
multiplyMat4Array(
matrices,multiplier,out,count?):void
Multiplies an array of Mat4s by a single Mat4. Useful for applying a parent transform to many child transforms.
Parameters
matrices
Float32Array
Source array with packed Mat4s (16 floats each)
multiplier
Matrix to multiply each by
out
Float32Array
Output array
count?
number
Number of matrices
Returns
void
multiplyQuatArray()
multiplyQuatArray(
a,b,out,count?):void
Multiplies corresponding quaternions in two arrays.
Parameters
a
Float32Array
First array with packed quaternions
b
Float32Array
Second array with packed quaternions
out
Float32Array
Output array
count?
number
Number of quaternions
Returns
void
mulVec3Array()
mulVec3Array(
a,b,out,count?):void
Multiplies Vec3 arrays element-wise (component-wise multiplication).
Parameters
a
Float32Array
b
Float32Array
out
Float32Array
count?
number
Returns
void
normalizeQuatArray()
normalizeQuatArray(
source,out,count?):void
Normalizes an array of quaternions.
Parameters
source
Float32Array
Source array with packed quaternions
out
Float32Array
Output array
count?
number
Number of quaternions
Returns
void
normalizeVec2Array()
normalizeVec2Array(
source,out,count?):void
Normalizes all Vec2s in an array.
Parameters
source
Float32Array
Source array with packed Vec2s
out
Float32Array
Output array
count?
number
Number of vectors
Returns
void
normalizeVec3Array()
normalizeVec3Array(
source,out,count?):void
Normalizes all Vec3s in an array.
Parameters
source
Float32Array
Source array with packed Vec3s
out
Float32Array
Output array
count?
number
Number of vectors
Returns
void
normalizeVec3Strided()
normalizeVec3Strided(
source,out,stride,offset?,count?):void
Normalizes Vec3s in a strided buffer. Useful for re-normalizing normals after transformation.
Parameters
source
Float32Array
Source buffer
out
Float32Array
Output buffer
stride
number
Number of floats between consecutive Vec3s
offset?
number = 0
Offset to first Vec3
count?
number
Number of Vec3s
Returns
void
packQuatArray()
packQuatArray(
quats,out?):Float32Array
Packs an array of Quat objects into a Float32Array.
Parameters
quats
readonly Quat[]
out?
Float32Array<ArrayBufferLike>
Returns
Float32Array
packVec2Array()
packVec2Array(
vectors,out?):Float32Array
Packs an array of Vec2 objects into a Float32Array.
Parameters
vectors
readonly object[]
out?
Float32Array<ArrayBufferLike>
Returns
Float32Array
packVec3Array()
packVec3Array(
vectors,out?):Float32Array
Packs an array of Vec3 objects into a Float32Array.
Parameters
vectors
readonly Vec3[]
Array of Vec3 objects
out?
Float32Array<ArrayBufferLike>
Optional pre-allocated output array
Returns
Float32Array
Packed Float32Array
packVec4Array()
packVec4Array(
vectors,out?):Float32Array
Packs an array of Vec4 objects into a Float32Array.
Parameters
vectors
readonly Vec4[]
out?
Float32Array<ArrayBufferLike>
Returns
Float32Array
rotateVec3Array()
rotateVec3Array(
source,quat,out,count?,sourceOffset?,outOffset?):void
Rotates an array of Vec3s by a quaternion.
Parameters
source
Float32Array
Source array with packed Vec3s
quat
Rotation quaternion
out
Float32Array
Output array
count?
number
Number of vectors to transform
sourceOffset?
number = 0
Starting index in source array
outOffset?
number = 0
Starting index in output array
Returns
void
scaleVec2Array()
scaleVec2Array(
source,scalar,out,count?):void
Scales all Vec2s in an array.
Parameters
source
Float32Array
Source array with packed Vec2s
scalar
number
Scale factor
out
Float32Array
Output array
count?
number
Number of vectors
Returns
void
scaleVec3Array()
scaleVec3Array(
source,scalar,out,count?):void
Scales all Vec3s in an array by a scalar.
Parameters
source
Float32Array
Source array with packed Vec3s
scalar
number
Scale factor
out
Float32Array
Output array
count?
number
Number of vectors
Returns
void
slerpQuatArray()
slerpQuatArray(
a,b,t,out,count?):void
Spherical linear interpolation between two quaternion arrays.
Parameters
a
Float32Array
Start array with packed quaternions (x,y,z,w format)
b
Float32Array
End array with packed quaternions
t
number
Interpolation factor (0 = a, 1 = b)
out
Float32Array
Output array
count?
number
Number of quaternions
Returns
void
slerpQuatArrayFast()
slerpQuatArrayFast(
a,b,t,out,count?):void
Fast slerp for normalized quaternions with shortcut for small angles. When quaternions are nearly parallel, uses linear interpolation. When exactly opposite, returns a stable interpolation path.
Parameters
a
Float32Array
Start quaternion array (packed)
b
Float32Array
End quaternion array (packed)
t
number
Interpolation factor
out
Float32Array
Output array
count?
number
Number of quaternions
Returns
void
subVec3Array()
subVec3Array(
a,b,out,count?):void
Subtracts two Vec3 arrays with loop unrolling.
Parameters
a
Float32Array
b
Float32Array
out
Float32Array
count?
number
Returns
void
transformVec2Array()
transformVec2Array(
source,m00,m01,m10,m11,tx,ty,out,count?):void
Transforms an array of Vec2s by a 3x3 transformation (treating as 2D homogeneous). Uses the upper 2x2 for rotation/scale and the translation from column 3. This is optimized for SIMD auto-vectorization with unrolled loops.
Parameters
source
Float32Array
Source array with packed Vec2s
m00
number
Matrix element [0,0]
m01
number
Matrix element [0,1]
m10
number
Matrix element [1,0]
m11
number
Matrix element [1,1]
tx
number
Translation X
ty
number
Translation Y
out
Float32Array
Output array
count?
number
Number of vectors to transform
Returns
void
transformVec3DirectionArray()
transformVec3DirectionArray(
source,matrix,out,count?,sourceOffset?,outOffset?):void
Transforms an array of Vec3s by a Mat4 as directions (w=0, no translation).
Parameters
source
Float32Array
Source array with packed Vec3s
matrix
Transformation matrix
out
Float32Array
Output array (can be same as source for in-place)
count?
number
Number of vectors to transform
sourceOffset?
number = 0
Starting index in source array
outOffset?
number = 0
Starting index in output array
Returns
void
transformVec3DirectionStrided()
transformVec3DirectionStrided(
source,matrix,out,stride,offset?,count?):void
Transforms Vec3 directions (normals) in a strided buffer. Ignores translation component.
Parameters
source
Float32Array
Source buffer
matrix
Transformation matrix (should use normal matrix for correct lighting)
out
Float32Array
Output buffer
stride
number
Number of floats between consecutive Vec3s
offset?
number = 0
Offset to first Vec3
count?
number
Number of Vec3s
Returns
void
transformVec3PointArray()
transformVec3PointArray(
source,matrix,out,count?,sourceOffset?,outOffset?):void
Transforms an array of Vec3s (packed as x,y,z,x,y,z,...) by a Mat4. Treats vectors as points (w=1, with perspective divide).
Parameters
source
Float32Array
Source array with packed Vec3s (length must be multiple of 3)
matrix
Transformation matrix
out
Float32Array
Output array (can be same as source for in-place)
count?
number
Number of vectors to transform (defaults to source.length / 3)
sourceOffset?
number = 0
Starting index in source array
outOffset?
number = 0
Starting index in output array
Returns
void
transformVec3Strided()
transformVec3Strided(
source,matrix,out,stride,offset?,count?):void
Transforms Vec3s in a strided buffer (e.g., interleaved vertex data). Useful for transforming positions in vertex buffers that also contain normals, UVs, etc.
Parameters
source
Float32Array
Source buffer with interleaved data
matrix
Transformation matrix
out
Float32Array
Output buffer (can be same as source)
stride
number
Number of floats between consecutive Vec3s
offset?
number = 0
Offset to first Vec3 in the buffer
count?
number
Number of Vec3s to transform
Returns
void
transformVec4Array()
transformVec4Array(
source,matrix,out,count?,sourceOffset?,outOffset?):void
Transforms an array of Vec4s by a Mat4.
Parameters
source
Float32Array
Source array with packed Vec4s
matrix
Transformation matrix
out
Float32Array
Output array
count?
number
Number of vectors to transform
sourceOffset?
number = 0
Starting index in source array
outOffset?
number = 0
Starting index in output array
Returns
void
unpackQuatArray()
unpackQuatArray(
packed,count?):Quat[]
Unpacks a Float32Array into an array of Quat objects.
Parameters
packed
Float32Array
count?
number
Returns
Quat[]
unpackVec2Array()
unpackVec2Array(
packed,count?):object[]
Unpacks a Float32Array into an array of Vec2-like objects.
Parameters
packed
Float32Array
count?
number
Returns
object[]
unpackVec3Array()
unpackVec3Array(
packed,count?):Vec3[]
Unpacks a Float32Array into an array of Vec3 objects.
Parameters
packed
Float32Array
Packed Float32Array
count?
number
Number of vectors to unpack
Returns
Vec3[]
Array of Vec3 objects
unpackVec4Array()
unpackVec4Array(
packed,count?):Vec4[]
Unpacks a Float32Array into an array of Vec4 objects.
Parameters
packed
Float32Array
count?
number
Returns
Vec4[]
Examples
// Transform 1000 vertices by a matrix
const vertices = new Float32Array(3000); // 1000 Vec3s packed as [x,y,z,x,y,z,...]
const matrix = Mat4.rotationY(Math.PI / 4);
BatchMath.transformVec3Array(vertices, matrix, vertices); // In-place transform// Batch lerp for animation blending
const current = new Float32Array(300);
const target = new Float32Array(300);
const result = new Float32Array(300);
BatchMath.lerpVec3Array(current, target, 0.5, result);