WebGLRenderer: bugfix when reading pixel in MRT #32506
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a bug when reading pixels from MRT. When using readRenderTargetPixels or readRenderTargetPixelsAsync, the code checks whether the WebGL context can read a pixel from the requested texture:
https://github.com/mrdoob/three.js/blob/1246386f547c5b4e64d3705debfe5a22f18cc91c/src/renderers/WebGLRenderer.js#L2916C1-L2921C7
This is done in textureFormatReadable function by using
gl.getParameter( gl.IMPLEMENTATION_COLOR_READ_FORMAT ). Nonetheless, at this point, we did not define the readBuffer yet, so the result will always return the format from COLOR_ATTACHMENT0, raising some errors when I attempt to read pixels from other color attachments. The fix was basically moving the readBuffer calling function before this check.An example of this bug: https://codepen.io/andredsm/full/jEqXVme. Check the console to see the error. In the code of this example, if you comment the second read, everything works, since we are only reading from the COLOR_ATTACHMENT0.
Finally, I made this pure WebGL2 example showing that the output from
gl.getParameter( gl.IMPLEMENTATION_COLOR_READ_FORMAT )is affected by the state ofgl.readBuffer: https://codepen.io/andredsm/pen/pvyqEqV.