I know there are a lot of resources on this, but none of them have worked for me.
Some are: webgl readpixels is always returning 0,0,0,0,
and this one: https://stackguides.com/questions/44869599/readpixels-from-webgl-canvas\
as well as this one: Read pixels from a WebGL texture
but none of them have been either helpful or successful.
The goal: Render an offscreen canvas with a WebGL shader, then use that as a texture in a separate WebGL shader.
Notes:
- For these WebGL shaders, I'm using a generic vertex shader used for pixel shaders, specifically, a raytracer/raymarcher. This is:
attribute vec2 a_position; void main() { gl_Position = vec4(a_position.xy, 0.0, 1.0); }. This vertex shader is inputted two triangles that cover the screen, so basically the fragment shader is doing all the work.
Problem: In order to get the image data off of the offscreen canvas, I've tried these methods:
- The WebGL
gl.readPixelsfunction
var capturedImageData = new Float32Array(screenWidth * screenHeight * 4);
gl.readPixels(0, 0, screenWidth, screenHeight, gl.RGBA, gl.FLOAT, capturedImageData);
- Using another canvas with
getContext('2d')
var offscreenCanvas = document.createElement("canvas");
offscreenCanvas.width = screenWidth;
offscreenCanvas.height = screenHeight;
var ctx = offscreenCanvas.getContext('2d');
ctx.drawImage(glCanvas, 0, 0);
var capturedImageData = ctx.getImageData(0, 0, screenWidth, screenHeight);
Both of these methods result in the capturedImageData array being filled with 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, . . . , which is obviously not right.
If anybody has experience with this problem, help would be greatly appreciated. Thanks!
A direct link to the program is: https://www.khanacademy.org/cs/-/6289631977619456