فهرست منبع

WebGLRenderer: Add MRT support to `readRenderTargetPixels()`. (#31204)

Michael Herzog 7 ماه پیش
والد
کامیت
5988c7a7e7
1فایلهای تغییر یافته به همراه15 افزوده شده و 4 حذف شده
  1. 15 4
      src/renderers/WebGLRenderer.js

+ 15 - 4
src/renderers/WebGLRenderer.js

@@ -2842,8 +2842,9 @@ class WebGLRenderer {
 		 * @param {number} height - The height of the copy region.
 		 * @param {number} height - The height of the copy region.
 		 * @param {TypedArray} buffer - The result buffer.
 		 * @param {TypedArray} buffer - The result buffer.
 		 * @param {number} [activeCubeFaceIndex] - The active cube face index.
 		 * @param {number} [activeCubeFaceIndex] - The active cube face index.
+		 * @param {number} [textureIndex=0] - The texture index of an MRT render target.
 		 */
 		 */
-		this.readRenderTargetPixels = function ( renderTarget, x, y, width, height, buffer, activeCubeFaceIndex ) {
+		this.readRenderTargetPixels = function ( renderTarget, x, y, width, height, buffer, activeCubeFaceIndex, textureIndex = 0 ) {
 
 
 			if ( ! ( renderTarget && renderTarget.isWebGLRenderTarget ) ) {
 			if ( ! ( renderTarget && renderTarget.isWebGLRenderTarget ) ) {
 
 
@@ -2866,7 +2867,7 @@ class WebGLRenderer {
 
 
 				try {
 				try {
 
 
-					const texture = renderTarget.texture;
+					const texture = renderTarget.textures[ textureIndex ];
 					const textureFormat = texture.format;
 					const textureFormat = texture.format;
 					const textureType = texture.type;
 					const textureType = texture.type;
 
 
@@ -2888,6 +2889,10 @@ class WebGLRenderer {
 
 
 					if ( ( x >= 0 && x <= ( renderTarget.width - width ) ) && ( y >= 0 && y <= ( renderTarget.height - height ) ) ) {
 					if ( ( x >= 0 && x <= ( renderTarget.width - width ) ) && ( y >= 0 && y <= ( renderTarget.height - height ) ) ) {
 
 
+						// when using MRT, select the corect color buffer for the subsequent read command
+
+						if ( renderTarget.textures.length > 1 ) _gl.readBuffer( _gl.COLOR_ATTACHMENT0 + textureIndex );
+
 						_gl.readPixels( x, y, width, height, utils.convert( textureFormat ), utils.convert( textureType ), buffer );
 						_gl.readPixels( x, y, width, height, utils.convert( textureFormat ), utils.convert( textureType ), buffer );
 
 
 					}
 					}
@@ -2918,9 +2923,10 @@ class WebGLRenderer {
 		 * @param {number} height - The height of the copy region.
 		 * @param {number} height - The height of the copy region.
 		 * @param {TypedArray} buffer - The result buffer.
 		 * @param {TypedArray} buffer - The result buffer.
 		 * @param {number} [activeCubeFaceIndex] - The active cube face index.
 		 * @param {number} [activeCubeFaceIndex] - The active cube face index.
+		 * @param {number} [textureIndex=0] - The texture index of an MRT render target.
 		 * @return {Promise<TypedArray>} A Promise that resolves when the read has been finished. The resolve provides the read data as a typed array.
 		 * @return {Promise<TypedArray>} A Promise that resolves when the read has been finished. The resolve provides the read data as a typed array.
 		 */
 		 */
-		this.readRenderTargetPixelsAsync = async function ( renderTarget, x, y, width, height, buffer, activeCubeFaceIndex ) {
+		this.readRenderTargetPixelsAsync = async function ( renderTarget, x, y, width, height, buffer, activeCubeFaceIndex, textureIndex = 0 ) {
 
 
 			if ( ! ( renderTarget && renderTarget.isWebGLRenderTarget ) ) {
 			if ( ! ( renderTarget && renderTarget.isWebGLRenderTarget ) ) {
 
 
@@ -2943,7 +2949,7 @@ class WebGLRenderer {
 					// set the active frame buffer to the one we want to read
 					// set the active frame buffer to the one we want to read
 					state.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
 					state.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
 
 
-					const texture = renderTarget.texture;
+					const texture = renderTarget.textures[ textureIndex ];
 					const textureFormat = texture.format;
 					const textureFormat = texture.format;
 					const textureType = texture.type;
 					const textureType = texture.type;
 
 
@@ -2962,6 +2968,11 @@ class WebGLRenderer {
 					const glBuffer = _gl.createBuffer();
 					const glBuffer = _gl.createBuffer();
 					_gl.bindBuffer( _gl.PIXEL_PACK_BUFFER, glBuffer );
 					_gl.bindBuffer( _gl.PIXEL_PACK_BUFFER, glBuffer );
 					_gl.bufferData( _gl.PIXEL_PACK_BUFFER, buffer.byteLength, _gl.STREAM_READ );
 					_gl.bufferData( _gl.PIXEL_PACK_BUFFER, buffer.byteLength, _gl.STREAM_READ );
+
+					// when using MRT, select the corect color buffer for the subsequent read command
+
+					if ( renderTarget.textures.length > 1 ) _gl.readBuffer( _gl.COLOR_ATTACHMENT0 + textureIndex );
+
 					_gl.readPixels( x, y, width, height, utils.convert( textureFormat ), utils.convert( textureType ), 0 );
 					_gl.readPixels( x, y, width, height, utils.convert( textureFormat ), utils.convert( textureType ), 0 );
 
 
 					// reset the frame buffer to the currently set buffer before waiting
 					// reset the frame buffer to the currently set buffer before waiting

粤ICP备19079148号