GLMW - WebAssembly powered Matrix and Vector library
This is an experimental near 1:1 port of gl-matrix v2.4.0 to WebAssembly.
In many cases glmw runs more than twice as fast as gl-matrix.
Some methods like *.str
and *.equals
are bridged and bring in some extra overhead.
Creating views with *.view
is cheap, because they return a typed subarray
of the WebAssembly module’s memory buffer.
mat4.create
and mat4.multiply
return a numeric address. To get an view on your data you need to use e.g. mat4.view(address)
. This returns a Float32Array
which is a direct view onto the allocated data in WebAssembly’s memory. You can manually read/write from this view.vec3.sqrLength
. You first have to convert it into the given module type (e.g. vec3.fromValues
) which then gives you the memory address of the allocated data.*.str
so a JavaScript String is returned.*.equals
so a JavaScript Boolean is returned.*.exactEquals
(see *.equals
).*.view
lets you create views onto your data in WebAssembly’s memory.*.free
to free data from WebAssembly’s memory.mat2
mat2d
mat3
vec2
quat
npm install glmw
or the browser distribution from here.
Before being able to use the library, you first have to call it’s init
method which then asynchronously compiles the WebAssembly module.
If you call a glmw
function before it got instantiated somewhere, then a TypeError
is thrown, because the function is simply not compiled yet.
This builds and compiles the WebAssembly module.
<script src="//rawgit.com/maierfelix/glmw/master/dist/glmw-browser.js"></script>
glmw.init().then(ready => {
// glmw is now ready and can be used anywhere
});
Import and call the init method in your main file. Afterwards you can use glmw
anywhere.
index.js
import { init, vec3 } from "glmw";
import calc from "./calc";
init().then(ready => {
// glmw is now ready and can be used anywhere
calc();
});
calc.js
import { mat4 } from "glmw";
export default function() {
return mat4.create();
};
Require and call the init method in your main file. Afterwards you can use glmw
anywhere.
index.js
const { init, vec3 } = require("glmw");
const calc = require("./calc");
init().then(ready => {
// glmw is now ready and can be used anywhere
calc();
});
calc.js
const { mat4 } = require("glmw");
module.exports = function() {
return mat4.create();
};
As you can see here, the API didn’t really change.
let a = vec3.create();
let b = vec3.fromValues(1.0, 2.0, 3.0);
vec3.add(a, a, b);
console.log( vec3.view(a) ); // Float32Array(3) [1, 2, 3]
First, this is what glmw returns to you. Instead of references, only the numeric addresses are returned:
a = mat4.create(); // 65688
b = mat4.create(); // 65760
c = mat4.multiply(a, a, b); // 65688
You can change data by hand this way:
vA = mat4.view(a); // Float32Array([1, 0, 0...])
vA[0] = 2; // you can now read/write
vA; // Float32Array([2, 0, 0...])
Since WebAssembly doesn’t have garbage collection yet, you have to be careful when and where you allocate data.
You can free data by calling *.free
:
a = mat4.create(); // allocate data for a
mat4.free(a); // a's data is now freed