v128
v128 is an high performance javascript library for 3D matrix vector calculations using 128 bits vector type from WebAssembly
Installation :
npm install v128
Getting started
Web browser :
<script src="node_module/v128/dist/v128-min.js"></script>
Node.js :
const {v128} = require("v128");
await v128.init(4);
let cameraPos = v128.vector.new(0,2.5,-4);
let center = v128.vector.new(0,0,0);
let up = v128.vector.new(0,1,0);
let viewMatrix = v128.matrix.lookAt(cameraPos,center,up,v128.matrix.new());
let projectionMatrix = v128.matrix.perspective(Math.PI/2,4/3,0.1,1000,v128.matrix.new());
let viewProjection = v128.matrix.multiply(viewMatrix, projectionMatrix,v128.matrix.new());
WebGL compatibility :
gl.uniformMatrix4fv(projectionLocation, false, v128.memory.toArray(projectionMatrix));
How to build :
prerequisite :
step to build : just type :
> make
API Reference
object
v128 : WebAssembly & Javascript module fast matrix vector calculations using SIMD vector 128 bits.
Kind: global namespace
-
v128 :
object
-
.ready :
Promise
-
.memory :
object
- .randomize()
-
.alloc(size) ⇒
UInt32
- .free(pointer)
- .fill(pointer, ...vals)
-
.slice(pointer) ⇒
Float32Array
-
.toArray(pointer) ⇒
Float32Array
-
.matrix :
object
-
.new(...vals) ⇒
UInt32
- .free(pointer)
- .identity([pMatDest]) ⇒
-
.multiply(pMatA, pMatB, pMatDest) ⇒
UInt32
-
.transform(pMat, pVec, pVecDest) ⇒
UInt32
-
.lookAt(pCamPos, pTargetPos, pUpAxis, pMatDest) ⇒
UInt32
-
.invert(pMat, pMatDest) ⇒
UInt32
-
.perspective(fovy, aspect, near, far, pMatDest) ⇒
UInt32
-
.fromTranslation(pVec, pMatDest) ⇒
UInt32
-
.fromScaling(pVec, pMatDest) ⇒
UInt32
-
.fromXRotation(rad, pMatDest) ⇒
UInt32
-
.fromYRotation(rad, pMatDest) ⇒
UInt32
-
.fromZRotation(rad, pMatDest) ⇒
UInt32
-
.rotateX(pMat, angle, pMatDest) ⇒
UInt32
-
.rotateY(pMat, angle, pMatDest) ⇒
UInt32
-
.rotateZ(pMat, angle, pMatDest) ⇒
UInt32
-
.rotateX(pMat, pVec, pMatDest) ⇒
UInt32
-
.new(...vals) ⇒
-
.vector :
object
-
.new(...vals) ⇒
UInt32
- .free(pointer)
-
.length(pVec) ⇒
Number
-
.normalize(pVec, pVecDest) ⇒
UInt32
-
.add(pVecA, pVecB, pVecDest) ⇒
UInt32
-
.sub(pVecA, pVecB, pVecDest) ⇒
UInt32
-
.mul(pVecA, pVecB, pVecDest) ⇒
UInt32
-
.div(pVecA, pVecB, pVecDest) ⇒
UInt32
-
.cross(pVecA, pVecB, pVecDest) ⇒
UInt32
-
.dot(pVecA, pVecB) ⇒
Number
-
.scale(pVec, scale, pVecDest) ⇒
UInt32
-
.new(...vals) ⇒
-
.uniformBlock :
object
-
.vertexBuffer :
object
-
.init(size) ⇒
Promise
-
.ready :
Promise
v128.ready : Promise resolve when API is ready
Kind: static property of v128
object
v128.memory : memory API
Kind: static namespace of v128
-
.memory :
object
- .randomize()
-
.alloc(size) ⇒
UInt32
- .free(pointer)
- .fill(pointer, ...vals)
-
.slice(pointer) ⇒
Float32Array
-
.toArray(pointer) ⇒
Float32Array
memory.randomize()
Randomize all the memory
Kind: instance method of memory
UInt32
memory.alloc(size) ⇒ allocate float memory array
Kind: instance method of memory
Returns: UInt32
- the pointer from v128 memory
Param | Type | Description |
---|---|---|
size | Number |
the number of float to allocate |
memory.free(pointer)
free float memory
Kind: instance method of memory
Param | Type | Description |
---|---|---|
pointer | UInt32 |
the pointer to free |
memory.fill(pointer, ...vals)
fill float memory with given values
Kind: instance method of memory
Param | Type | Description |
---|---|---|
pointer | UInt32 |
|
...vals | Numbers |
number values to fill |
Float32Array
memory.slice(pointer) ⇒ get copy of portion float memory
Kind: instance method of memory
Param | Type |
---|---|
pointer | UInt32 |
Float32Array
memory.toArray(pointer) ⇒ get read/write access of portion float memory
Kind: instance method of memory
Param | Type |
---|---|
pointer | UInt32 |
object
v128.matrix : matrix API
Kind: static namespace of v128
-
.matrix :
object
-
.new(...vals) ⇒
UInt32
- .free(pointer)
- .identity([pMatDest]) ⇒
-
.multiply(pMatA, pMatB, pMatDest) ⇒
UInt32
-
.transform(pMat, pVec, pVecDest) ⇒
UInt32
-
.lookAt(pCamPos, pTargetPos, pUpAxis, pMatDest) ⇒
UInt32
-
.invert(pMat, pMatDest) ⇒
UInt32
-
.perspective(fovy, aspect, near, far, pMatDest) ⇒
UInt32
-
.fromTranslation(pVec, pMatDest) ⇒
UInt32
-
.fromScaling(pVec, pMatDest) ⇒
UInt32
-
.fromXRotation(rad, pMatDest) ⇒
UInt32
-
.fromYRotation(rad, pMatDest) ⇒
UInt32
-
.fromZRotation(rad, pMatDest) ⇒
UInt32
-
.rotateX(pMat, angle, pMatDest) ⇒
UInt32
-
.rotateY(pMat, angle, pMatDest) ⇒
UInt32
-
.rotateZ(pMat, angle, pMatDest) ⇒
UInt32
-
.rotateX(pMat, pVec, pMatDest) ⇒
UInt32
-
.new(...vals) ⇒
UInt32
matrix.new(...vals) ⇒ fast create new matrix from initial values
Kind: instance method of matrix
Returns: UInt32
- the pointer to new matrix
Param | Type | Description |
---|---|---|
...vals | Numbers |
number values to fill into matrix |
matrix.free(pointer)
free the matrix
Kind: instance method of matrix
Param | Type | Description |
---|---|---|
pointer | UInt32 |
the pointer of matrix to free |
matrix.identity([pMatDest]) ⇒
set or create matrix identity
Kind: instance method of matrix
Returns: the pointer of matrix identity
Param | Type | Description |
---|---|---|
[pMatDest] | UInt32 |
the pointer of matrix to set |
UInt32
matrix.multiply(pMatA, pMatB, pMatDest) ⇒ fast multiply 2 matrix (WebAssembly method)
Kind: instance method of matrix
Returns: UInt32
- the pointer to result matrix A*B
Param | Type | Description |
---|---|---|
pMatA | UInt32 |
pointer of matrix A |
pMatB | UInt32 |
pointer of matrix B |
pMatDest | UInt32 |
pointer of result matrix A*B |
UInt32
matrix.transform(pMat, pVec, pVecDest) ⇒ fast multiply matrix * vector (WebAssembly method)
Kind: instance method of matrix
Returns: UInt32
- the pointer to result transformed vector
Param | Type | Description |
---|---|---|
pMat | UInt32 |
pointer of matrix |
pVec | UInt32 |
pointer of vector |
pVecDest | UInt32 |
pointer of result transformed vector (matrix * vector) |
UInt32
matrix.lookAt(pCamPos, pTargetPos, pUpAxis, pMatDest) ⇒ fast create view matrix from camera position & target position (WebAssembly method)
Kind: instance method of matrix
Returns: UInt32
- the pointer to result view matrix
Param | Type | Description |
---|---|---|
pCamPos | UInt32 |
pointer of camera position |
pTargetPos | UInt32 |
pointer of target position |
pUpAxis | UInt32 |
pointer of up axis |
pMatDest | UInt32 |
pointer of result view matrix |
UInt32
matrix.invert(pMat, pMatDest) ⇒ fast invert matrix (WebAssembly method)
Kind: instance method of matrix
Returns: UInt32
- the pointer to inversed matrix
Param | Type | Description |
---|---|---|
pMat | UInt32 |
pointer of th matrix |
pMatDest | UInt32 |
pointer of inversed matrix |
UInt32
matrix.perspective(fovy, aspect, near, far, pMatDest) ⇒ create projection matrix from perspective data
Kind: instance method of matrix
Returns: UInt32
- the pointer to result projection matrix
Param | Type | Description |
---|---|---|
fovy | number |
Vertical field of view in radians |
aspect | number |
Aspect ratio. typically viewport width/height |
near | number |
Near clipping bound of the frustum |
far | number |
Far clipping bound of the frustum |
pMatDest | UInt32 |
pointer of result projection matrix |
UInt32
matrix.fromTranslation(pVec, pMatDest) ⇒ Creates a matrix from a vector translation
Kind: instance method of matrix
Returns: UInt32
- the pointer to result translated matrix
Param | Type | Description |
---|---|---|
pVec | UInt32 |
pointer of Translation vector |
pMatDest | UInt32 |
pointer of result translated matrix |
UInt32
matrix.fromScaling(pVec, pMatDest) ⇒ Creates a matrix from a vector scaling
Kind: instance method of matrix
Returns: UInt32
- the pointer to result scaled matrix
Param | Type | Description |
---|---|---|
pVec | UInt32 |
pointer of scaling vector |
pMatDest | UInt32 |
pointer of result scaled matrix |
UInt32
matrix.fromXRotation(rad, pMatDest) ⇒ Creates a matrix from the given angle around the X axis
Kind: instance method of matrix
Returns: UInt32
- the pointer to result rotated matrix
Param | Type | Description |
---|---|---|
rad | Number |
the angle to rotate the matrix by |
pMatDest | UInt32 |
pointer of result rotated matrix |
UInt32
matrix.fromYRotation(rad, pMatDest) ⇒ Creates a matrix from the given angle around the Y axis
Kind: instance method of matrix
Returns: UInt32
- the pointer to result rotated matrix
Param | Type | Description |
---|---|---|
rad | Number |
the angle to rotate the matrix by |
pMatDest | UInt32 |
pointer of result rotated matrix |
UInt32
matrix.fromZRotation(rad, pMatDest) ⇒ Creates a matrix from the given angle around the Z axis
Kind: instance method of matrix
Returns: UInt32
- the pointer to result rotated matrix
Param | Type | Description |
---|---|---|
rad | Number |
the angle to rotate the matrix by |
pMatDest | UInt32 |
pointer of result rotated matrix |
UInt32
matrix.rotateX(pMat, angle, pMatDest) ⇒ Rotates a matrix by the given angle around the X axis
Kind: instance method of matrix
Returns: UInt32
- the pointer of the receiving matrix
Param | Type | Description |
---|---|---|
pMat | UInt32 |
pointer of matrix to rotate |
angle | Number |
the angle in radian to rotate the matrix by |
pMatDest | UInt32 |
pointer of the receiving matrix |
UInt32
matrix.rotateY(pMat, angle, pMatDest) ⇒ Rotates a matrix by the given angle around the Y axis
Kind: instance method of matrix
Returns: UInt32
- the pointer of the receiving matrix
Param | Type | Description |
---|---|---|
pMat | UInt32 |
pointer of matrix to rotate |
angle | Number |
the angle in radian to rotate the matrix by |
pMatDest | UInt32 |
pointer of the receiving matrix |
UInt32
matrix.rotateZ(pMat, angle, pMatDest) ⇒ Rotates a matrix by the given angle around the Z axis
Kind: instance method of matrix
Returns: UInt32
- the pointer of the receiving matrix
Param | Type | Description |
---|---|---|
pMat | UInt32 |
pointer of matrix to rotate |
angle | Number |
the angle in radian to rotate the matrix by |
pMatDest | UInt32 |
pointer of the receiving matrix |
UInt32
matrix.rotateX(pMat, pVec, pMatDest) ⇒ Translates a matrix by the given vector
Kind: instance method of matrix
Returns: UInt32
- the pointer of the receiving matrix
Param | Type | Description |
---|---|---|
pMat | UInt32 |
pointer of matrix to translate |
pVec | Number |
pointer of vector to translate by |
pMatDest | UInt32 |
pointer of the receiving matrix |
object
v128.vector : vector API
Kind: static namespace of v128
-
.vector :
object
-
.new(...vals) ⇒
UInt32
- .free(pointer)
-
.length(pVec) ⇒
Number
-
.normalize(pVec, pVecDest) ⇒
UInt32
-
.add(pVecA, pVecB, pVecDest) ⇒
UInt32
-
.sub(pVecA, pVecB, pVecDest) ⇒
UInt32
-
.mul(pVecA, pVecB, pVecDest) ⇒
UInt32
-
.div(pVecA, pVecB, pVecDest) ⇒
UInt32
-
.cross(pVecA, pVecB, pVecDest) ⇒
UInt32
-
.dot(pVecA, pVecB) ⇒
Number
-
.scale(pVec, scale, pVecDest) ⇒
UInt32
-
.new(...vals) ⇒
UInt32
vector.new(...vals) ⇒ fast create new vector from initial values
Kind: instance method of vector
Returns: UInt32
- the pointer to new vector
Param | Type | Description |
---|---|---|
...vals | Numbers |
number values to fill into vector |
vector.free(pointer)
free the vector
Kind: instance method of vector
Param | Type | Description |
---|---|---|
pointer | UInt32 |
the pointer of vector to free |
Number
vector.length(pVec) ⇒ get fast length of 3D Homogeneous coordinates vector (WebAssembly method)
Kind: instance method of vector
Returns: Number
- the length of vector
Param | Type | Description |
---|---|---|
pVec | UInt32 |
pointer of vector |
UInt32
vector.normalize(pVec, pVecDest) ⇒ fast normalize 3D Homogeneous coordinates vector (WebAssembly method)
Kind: instance method of vector
Returns: UInt32
- the pointer of normalized vector
Param | Type | Description |
---|---|---|
pVec | UInt32 |
pointer of vector |
pVecDest | UInt32 |
pointer of receive normalized vector |
UInt32
vector.add(pVecA, pVecB, pVecDest) ⇒ fast add two 3D Homogeneous coordinates vector (WebAssembly method)
Kind: instance method of vector
Returns: UInt32
- the pointer of result vector
Param | Type | Description |
---|---|---|
pVecA | UInt32 |
pointer of vector A |
pVecB | UInt32 |
pointer of vector B |
pVecDest | UInt32 |
pointer of receive sum result vector ( A + B ) |
UInt32
vector.sub(pVecA, pVecB, pVecDest) ⇒ fast sub two 3D Homogeneous coordinates vector (WebAssembly method)
Kind: instance method of vector
Returns: UInt32
- the pointer of result vector
Param | Type | Description |
---|---|---|
pVecA | UInt32 |
pointer of vector A |
pVecB | UInt32 |
pointer of vector B |
pVecDest | UInt32 |
pointer of receive sum result vector ( A - B ) |
UInt32
vector.mul(pVecA, pVecB, pVecDest) ⇒ fast multiply two 3D Homogeneous coordinates vector (WebAssembly method)
Kind: instance method of vector
Returns: UInt32
- the pointer of result vector
Param | Type | Description |
---|---|---|
pVecA | UInt32 |
pointer of vector A |
pVecB | UInt32 |
pointer of vector B |
pVecDest | UInt32 |
pointer of receive multiply result vector ( A * B ) |
UInt32
vector.div(pVecA, pVecB, pVecDest) ⇒ fast divide two 3D Homogeneous coordinates vector (WebAssembly method)
Kind: instance method of vector
Returns: UInt32
- the pointer of result vector
Param | Type | Description |
---|---|---|
pVecA | UInt32 |
pointer of vector A |
pVecB | UInt32 |
pointer of vector B |
pVecDest | UInt32 |
pointer of receive divide result vector ( A / B ) |
UInt32
vector.cross(pVecA, pVecB, pVecDest) ⇒ fast cross product of two 3D Homogeneous coordinates vector (WebAssembly method)
Kind: instance method of vector
Returns: UInt32
- the pointer of result vector
Param | Type | Description |
---|---|---|
pVecA | UInt32 |
pointer of vector A |
pVecB | UInt32 |
pointer of vector B |
pVecDest | UInt32 |
pointer of receive cross product result vector ( A.B ) |
Number
vector.dot(pVecA, pVecB) ⇒ fast dot product of two 3D Homogeneous coordinates vector (WebAssembly method)
Kind: instance method of vector
Returns: Number
- the pointer of result vector
Param | Type | Description |
---|---|---|
pVecA | UInt32 |
pointer of vector A |
pVecB | UInt32 |
pointer of vector B |
UInt32
vector.scale(pVec, scale, pVecDest) ⇒ fast scale vector by a scalar number
Kind: instance method of vector
Returns: UInt32
- the pointer of result vector
Param | Type | Description |
---|---|---|
pVec | UInt32 |
pointer of vector to scale |
scale | Number |
amount to scale the vector by |
pVecDest | UInt32 |
pointer of receive result vector |
object
v128.uniformBlock : WebGL2 Uniform Buffer Objects API (UBOs) using std140 layout.
Kind: static namespace of v128
object
v128.vertexBuffer : WebGL Vertex Buffer Objects API (VBOs)
Kind: static namespace of v128
Promise
v128.init(size) ⇒ Initialize the v128 API
Kind: static method of v128
Returns: Promise
- resolve when API is ready
Param | Type | Description |
---|---|---|
size | Number |
the number of page for v128 Memory (page = 64Kb) |