I need assistance with encoding object positions (x,y,z) and sending them to a GLSL shader for decoding, performing calculations, and then returning the results back to the CPU. I have done some research on this topic and found some relevant resources like decode rgb value to single float without bit-shift in glsl. However, despite my efforts, I have not been successful in properly encoding and decoding the data.
Below is an excerpt from my code.`...
function init() {
...
// Code snippet for initializing buffers and textures
vec1 = new THREE.Vector3(2.6, 3.3, 100.80); // example position vector
// Encoding the vector values into Uint8Array for texture creation
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
]);
// Rendering function here...
var pixels = new Uint8Array(WIDTH * HEIGHT * 4);
gl.readPixels(0, 0, WIDTH, HEIGHT, gl.RGBA, gl.FLOAT, pixels);
pixels = new Uint8Array(pixels.buffer);
// Decoding the results obtained from GLSL
var color = {
r: pixels[0],
g: pixels[1],
b: pixels[2]
};
float1 = decodeVec3ToFloat(color);
}
// Other functions definitions follow...