haxiomic / vector-math

Shader-math in haxe: library for GLSL vector operations, complete with swizzles and all
MIT License
39 stars 7 forks source link

Converting to Float32Array to pass to gpu (WebGL) #4

Closed hansagames closed 3 years ago

hansagames commented 3 years ago

Hi, any plans to add converter to Float32Array in order to pass Mat4 data to gpu or better add extra utils for that ?

Thank you

haxiomic commented 3 years ago

I've just created a PR for a new method copyIntoArray(array-like, index), so in your case you might do

gl.uniformMatrix4fv(cameraLoc, false, cameraMatrix.copyIntoArray(new Float32Array(), 0));

// or have a single Float32Array for all uploads
var matrixUploadArray = new Float32Array(16);

// per-frame
cameraMatrix.copyIntoArray(matrixUploadArray, 0);
gl.uniformMatrix4fv(cameraLoc, false, matrixUploadArray);

// or if you're setting some vertex data
var vertexBuffer = new Float32Array(vertexCount * 3);
for (i in 0...vertexCount) {
    var position = vec3(0.0);
    // some clever vector math to generate position 
    // ...
    position.copyIntoArray(vertexBuffer, i * 3);
}

The generated code is a bit chunky because it sets the fields one by one – I benchmarked it and this seems to be the fastest method still

hansagames commented 3 years ago

@haxiomic thanks looks good

haxiomic commented 3 years ago

Awesome, merged :)