How to encode and send float data to GLSL from Javascript/THREE.js and decode results -
i encode object positions (x,y,z) , send glsl shader, decode data, perform calculations , send results cpu. have researched issue , have found partial answers decode rgb value single float without bit-shift in glsl, have not been successful in encoding , decoding results.
here part of code.`...
function init() { ... buffer = gl.createbuffer(); gl.bindbuffer(gl.array_buffer, buffer); gl.bufferdata( gl.array_buffer, new float32array([ -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0 ]), gl.static_draw ); texture = gl.createtexture(); gl.bindtexture(gl.texture_2d, texture); vec1 = new three.vector3(2.6, 3.3, 100.80); //example position vector data = new uint8array([float2color(vec1.x).r, float2color(vec1.x).g, float2color(vec1.x).b, 255, //x float2color(vec1.y).r, float2color(vec1.y).g, float2color(vec1.y).b, 255, //y float2color(vec1.z).r, float2color(vec1.z).g, float2color(vec1.z).b, 255 //z ]); // encodes give me int8array [ 2, 0, 0, 255, 3, 0, 0, 255, 100, 0, 2 more… ] gl.teximage2d(gl.texture_2d, level, gl.rgba, width, height, 0, gl.rgba, gl.unsigned_byte, data); } //render function function render() { ... gl.drawarrays(gl.points, 0, 2); var pixels = new uint8array(width * height * 4); gl.readpixels(0, 0, width, height, gl.rgba, gl.float, pixels); pixels = new uint8array(pixels.buffer); //after getting results glsl, pixels //uint8array [ 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, … ] var color = { r: pixels[0], g: pixels[1], b: pixels[2] }; float1 = decodevec3tofloat(color); // decode , use data after position updated in glsl } function float2color( f ) { b = math.floor(f / 255.0 / 255.0); g = math.floor((f - (b * 255.0 * 255.0) ) / 255.0); // r = math.floor(f - (b * 255.0 * 255.0) - (g * 255.0) ); r = math.floor(f % 255); return {r:r, g:g, b:b}; } function decodevec3tofloat(color) { var result; result = color.r * 255.0; result += color.g * 255.0 * 255.0; result += color.b * 255.0 * 255.0 * 255.0; return result; }
i've been working sort of thing myself. can use datatextures achieve this, , pass them in , out buffers manipulated simulation shader. best example i've found far uses dated version of three.js (v58) can updated. found helpful scripts updated , toy around until understood enough use case. note in example there fixed values passed through in simulation shader, changing x,y,z values rendered in , out on buffers. luck!
update:
i have since discoverer old three.fboutils script using above has been updated , reworked , called three.gpucomputationrenderer -- works great! (v80)
Comments
Post a Comment