Skip to content

WebGLRenderer - Add MRT support to .readRenderTargetPixels() #31089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from

Conversation

repalash
Copy link
Contributor

Fixes: #22403

Description

Adds support to read by texture index in WebGLRenderer.readRenderTargetPixels.

For the test, duplicated the example webgl_multiple_rendertargets to webgl_multiple_rendertargets_read and added readRenderTargetPixels on mouse position for both buffers. It can also be added to the same example to avoid duplication.

This contribution is funded by ThreePipe

Copy link

github-actions bot commented May 12, 2025

📦 Bundle size

Full ESM build, minified and gzipped.

Before After Diff
WebGL 337.26
78.64
337.4
78.67
+136 B
+32 B
WebGPU 550.09
152.49
550.09
152.49
+0 B
+0 B
WebGPU Nodes 549.45
152.33
549.45
152.33
+0 B
+0 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Before After Diff
WebGL 468.12
113.14
468.25
113.17
+136 B
+35 B
WebGPU 625.46
169.27
625.46
169.27
+0 B
+0 B
WebGPU Nodes 580.32
158.58
580.32
158.58
+0 B
+0 B

*/
this.readRenderTargetPixels = function ( renderTarget, x, y, width, height, buffer, activeCubeFaceIndex ) {
this.readRenderTargetPixels = function ( renderTarget, x, y, width, height, buffer, activeCubeFaceIndex, textureIndex ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use a default parameter like in WebGPURenderer so textureIndex = 0.

<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - Multiple Render Targets Read Pixels</title>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of creating a new example, do you mind enhancing webgl_read_float_buffer instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, do you mean changing the target there to be with multiple textures or adding an option in the UI to switch between single and multiple?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just use MRT.

@repalash
Copy link
Contributor Author

Also do you prefer to keep the stackoverflow link as comment for the explanation, or should I remove it?

@Mugen87
Copy link
Collaborator

Mugen87 commented May 13, 2025

Also do you prefer to keep the stackoverflow link as comment for the explanation, or should I remove it?

Let's remove it. We just implement what the WebGL spec suggest so it should be clear what's going on.

@repalash
Copy link
Contributor Author

The example can be tried here - https://rawcdn.githack.com/PalashBansal96/three.js/read-mrt-2/examples/webgl_multiple_rendertargets_read.html

@gkjohnson
Copy link
Collaborator

gkjohnson commented May 13, 2025

Can we also update the readRenderTargetPixelsAsync function to keep the signatures in sync? The change look minimal.

Thanks for adding this!

@repalash
Copy link
Contributor Author

Oh right, I missed the WebGLRenderer.readRenderTargetPixelsAsync in WebGLRenderer since it was added recently, will add that.

Also, the signature of Renderer.readRenderTargetPixelsAsync(webgpu) is different. Keeping it with webgl would require a breaking change either in webgl or webgpu. What do you suggest?
https://github.com/mrdoob/three.js/blob/dev/src/renderers/common/Renderer.js#L2570

@Mugen87
Copy link
Collaborator

Mugen87 commented May 14, 2025

Let's focus on adding textureIndex for now. Harmonizing the signatures can be done in a later PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Renderer: Add MRT support to .readRenderTargetPixels().
3 participants