From cef26c486709ab370d53085cb01fa893d0b5c827 Mon Sep 17 00:00:00 2001 From: David Peicho Date: Tue, 9 Feb 2021 23:04:43 +0100 Subject: [PATCH 1/6] WebGLRenderer: add copyTextureToTexture3D --- src/renderers/WebGLRenderer.js | 56 ++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/renderers/WebGLRenderer.js b/src/renderers/WebGLRenderer.js index 5c50a0c11b923f..ca943cd486f68b 100644 --- a/src/renderers/WebGLRenderer.js +++ b/src/renderers/WebGLRenderer.js @@ -1987,6 +1987,62 @@ function WebGLRenderer( parameters ) { }; + this.copyTextureToTexture3D = function ( box, srcTexture, dstTexture, level = 0 ) { + + if ( ! _this.isWebGL1Renderer ) { + + console.warn( 'THREE.WebGLRenderer.copyTextureToTexture3D: can only be used with WebGL2.' ); + return; + + } + + const { width, height, data } = srcTexture.image; + const glFormat = utils.convert( dstTexture.format ); + const glType = utils.convert( dstTexture.type ); + let glTarget; + + if ( dstTexture.isDataTexture3D ) { + + textures.setTexture3D( dstTexture, 0 ); + glTarget = _gl.TEXTURE_3D; + + } else if ( dstTexture.isDataTexture2DArray ) { + + textures.setTexture2DArray( dstTexture, 0 ); + glTarget = _gl.TEXTURE_2D_ARRAY; + + } else { + + console.warn( 'THREE.WebGLRenderer.copyTextureToTexture3D: only supports THREE.DataTexture3D and THREE.DataTexture2DArray.' ); + return; + + } + + _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, dstTexture.flipY ); + _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, dstTexture.premultiplyAlpha ); + _gl.pixelStorei( _gl.UNPACK_ALIGNMENT, dstTexture.unpackAlignment ); + _gl.pixelStorei( _gl.UNPACK_ROW_LENGTH, width ); + _gl.pixelStorei( _gl.UNPACK_IMAGE_HEIGHT, height ); + _gl.pixelStorei( _gl.UNPACK_SKIP_PIXELS, box.x ); + _gl.pixelStorei( _gl.UNPACK_SKIP_ROWS, box.y ); + _gl.pixelStorei( _gl.UNPACK_SKIP_IMAGES, box.z ); + + _gl.texSubImage3D( + glTarget, + level, + box.x, + box.y, + box.z, + box.max.x - box.min.x + 1, + box.max.y - box.min.y + 1, + box.max.z - box.min.z + 1, + glFormat, + glType, + data + ); + + }; + this.initTexture = function ( texture ) { textures.setTexture2D( texture, 0 ); From 33692c84b106d8676d33466e42179e961f967b9d Mon Sep 17 00:00:00 2001 From: David Peicho Date: Wed, 10 Feb 2021 00:28:58 +0100 Subject: [PATCH 2/6] WebGLRenderer: implement copyTextureToTexture3D --- src/renderers/WebGLRenderer.js | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/renderers/WebGLRenderer.js b/src/renderers/WebGLRenderer.js index ca943cd486f68b..182bb412f0d4bf 100644 --- a/src/renderers/WebGLRenderer.js +++ b/src/renderers/WebGLRenderer.js @@ -1987,9 +1987,9 @@ function WebGLRenderer( parameters ) { }; - this.copyTextureToTexture3D = function ( box, srcTexture, dstTexture, level = 0 ) { + this.copyTextureToTexture3D = function ( sourceBox, position, srcTexture, dstTexture, level = 0 ) { - if ( ! _this.isWebGL1Renderer ) { + if ( _this.isWebGL1Renderer ) { console.warn( 'THREE.WebGLRenderer.copyTextureToTexture3D: can only be used with WebGL2.' ); return; @@ -2021,26 +2021,38 @@ function WebGLRenderer( parameters ) { _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, dstTexture.flipY ); _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, dstTexture.premultiplyAlpha ); _gl.pixelStorei( _gl.UNPACK_ALIGNMENT, dstTexture.unpackAlignment ); + + // @todo: we can move those parameters to the 3D texture + // classes just like `UNPACK_ALIGNMENT`, and then we can use them when + // uploading texture. If we decide to keep this function this way with the + // `sourceBox` parameter, then we need to reset those parameters to the + // default values either after the `texSubImage3D` call, or when uploading + // a texture. _gl.pixelStorei( _gl.UNPACK_ROW_LENGTH, width ); _gl.pixelStorei( _gl.UNPACK_IMAGE_HEIGHT, height ); - _gl.pixelStorei( _gl.UNPACK_SKIP_PIXELS, box.x ); - _gl.pixelStorei( _gl.UNPACK_SKIP_ROWS, box.y ); - _gl.pixelStorei( _gl.UNPACK_SKIP_IMAGES, box.z ); + _gl.pixelStorei( _gl.UNPACK_SKIP_PIXELS, sourceBox.min.x ); + _gl.pixelStorei( _gl.UNPACK_SKIP_ROWS, sourceBox.min.y ); + _gl.pixelStorei( _gl.UNPACK_SKIP_IMAGES, sourceBox.min.z ); _gl.texSubImage3D( glTarget, level, - box.x, - box.y, - box.z, - box.max.x - box.min.x + 1, - box.max.y - box.min.y + 1, - box.max.z - box.min.z + 1, + position.x, + position.y, + position.z, + sourceBox.max.x - sourceBox.min.x + 1, + sourceBox.max.y - sourceBox.min.y + 1, + sourceBox.max.z - sourceBox.min.z + 1, glFormat, glType, data ); + // Generate mipmaps only when copying level 0 + if ( level === 0 && dstTexture.generateMipmaps ) _gl.generateMipmap( glTarget ); + + state.unbindTexture(); + }; this.initTexture = function ( texture ) { From 0c9b21e48d7763d3c2b093fa05424beafe6ce537 Mon Sep 17 00:00:00 2001 From: David Peicho Date: Wed, 10 Feb 2021 00:38:27 +0100 Subject: [PATCH 3/6] rollup.config.js: add constants for unpack* WebGL2 --- utils/build/rollup.config.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/utils/build/rollup.config.js b/utils/build/rollup.config.js index 240e78392aa170..912649d8333174 100644 --- a/utils/build/rollup.config.js +++ b/utils/build/rollup.config.js @@ -153,6 +153,11 @@ function glconstants() { MAX_FRAGMENT_UNIFORM_VECTORS: 36349, UNPACK_FLIP_Y_WEBGL: 37440, UNPACK_PREMULTIPLY_ALPHA_WEBGL: 37441, + UNPACK_ROW_LENGTH: 3314, + UNPACK_IMAGE_HEIGHT: 32878, + UNPACK_SKIP_PIXELS: 3316, + UNPACK_SKIP_ROWS: 3315, + UNPACK_SKIP_IMAGES: 32877, MAX_SAMPLES: 36183, READ_FRAMEBUFFER: 36008, DRAW_FRAMEBUFFER: 36009 From dac51599b7d91bca0615fffe56a3a070db96a194 Mon Sep 17 00:00:00 2001 From: David Peicho Date: Wed, 17 Feb 2021 14:32:17 +0100 Subject: [PATCH 4/6] docs: add API entry for --- docs/api/en/renderers/WebGLRenderer.html | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/api/en/renderers/WebGLRenderer.html b/docs/api/en/renderers/WebGLRenderer.html index ce1ba15f2502bc..640b3694ae13db 100644 --- a/docs/api/en/renderers/WebGLRenderer.html +++ b/docs/api/en/renderers/WebGLRenderer.html @@ -310,6 +310,9 @@

[method:null copyFramebufferToTexture]( [param:Vector2 position], [param:Tex

[method:null copyTextureToTexture]( [param:Vector2 position], [param:Texture srcTexture], [param:Texture dstTexture], [param:Number level] )

Copies all pixels of a texture to an existing texture starting from the given position. Enables access to [link:https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/texSubImage2D WebGLRenderingContext.texSubImage2D].

+

[method:null copyTextureToTexture3D]( [param:Box3 sourceBox], [param:Vector2 position], [param:Texture srcTexture], [param:Texture dstTexture], [param:Number level] )

+

Copies the pixels of a texture in the bounds '[page:Box3 sourceBox]' in the desination texture starting from the given position. Enables access to [link:https://developer.mozilla.org/en-US/docs/Web/API/WebGL2RenderingContext/texSubImage3D WebGL2RenderingContext.texSubImage3D].

+

[method:null dispose]( )

Dispose of the current rendering context.

From 6cfb83c59b0b167f9e4804bed8f1423cb22d7f8f Mon Sep 17 00:00:00 2001 From: David Peicho Date: Wed, 17 Feb 2021 14:32:43 +0100 Subject: [PATCH 5/6] WebGLRenderer: copyTextureToTexture3D: reset pixelStorei param --- src/renderers/WebGLRenderer.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/renderers/WebGLRenderer.js b/src/renderers/WebGLRenderer.js index 182bb412f0d4bf..451552aca98644 100644 --- a/src/renderers/WebGLRenderer.js +++ b/src/renderers/WebGLRenderer.js @@ -2022,12 +2022,12 @@ function WebGLRenderer( parameters ) { _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, dstTexture.premultiplyAlpha ); _gl.pixelStorei( _gl.UNPACK_ALIGNMENT, dstTexture.unpackAlignment ); - // @todo: we can move those parameters to the 3D texture - // classes just like `UNPACK_ALIGNMENT`, and then we can use them when - // uploading texture. If we decide to keep this function this way with the - // `sourceBox` parameter, then we need to reset those parameters to the - // default values either after the `texSubImage3D` call, or when uploading - // a texture. + const unpackRowLen = _gl.getParameter( _gl.UNPACK_ROW_LENGTH ); + const unpackImageHeight = _gl.getParameter( _gl.UNPACK_IMAGE_HEIGHT ); + const unpackSkipPixels = _gl.getParameter( _gl.UNPACK_SKIP_PIXELS ); + const unpackSkipRows = _gl.getParameter( _gl.UNPACK_SKIP_ROWS ); + const unpackSkipImages = _gl.getParameter( _gl.UNPACK_SKIP_IMAGES ); + _gl.pixelStorei( _gl.UNPACK_ROW_LENGTH, width ); _gl.pixelStorei( _gl.UNPACK_IMAGE_HEIGHT, height ); _gl.pixelStorei( _gl.UNPACK_SKIP_PIXELS, sourceBox.min.x ); @@ -2048,6 +2048,12 @@ function WebGLRenderer( parameters ) { data ); + _gl.pixelStorei( _gl.UNPACK_ROW_LENGTH, unpackRowLen ); + _gl.pixelStorei( _gl.UNPACK_IMAGE_HEIGHT, unpackImageHeight ); + _gl.pixelStorei( _gl.UNPACK_SKIP_PIXELS, unpackSkipPixels ); + _gl.pixelStorei( _gl.UNPACK_SKIP_ROWS, unpackSkipRows ); + _gl.pixelStorei( _gl.UNPACK_SKIP_IMAGES, unpackSkipImages ); + // Generate mipmaps only when copying level 0 if ( level === 0 && dstTexture.generateMipmaps ) _gl.generateMipmap( glTarget ); From c2d3e24ab4d6de57d2a356b2619f5167e0224839 Mon Sep 17 00:00:00 2001 From: David Peicho Date: Wed, 17 Feb 2021 14:33:02 +0100 Subject: [PATCH 6/6] examples: add webgl2_materials_texture3d_partialupdate.html --- examples/files.json | 1 + ...gl2_materials_texture3d_partialupdate.html | 358 ++++++++++++++++++ 2 files changed, 359 insertions(+) create mode 100644 examples/webgl2_materials_texture3d_partialupdate.html diff --git a/examples/files.json b/examples/files.json index 6c0a0db3c02dd0..e6928ef0a17e5d 100644 --- a/examples/files.json +++ b/examples/files.json @@ -319,6 +319,7 @@ "webgl2_materials_texture3d", "webgl2_multisampled_renderbuffers", "webgl2_rendertarget_texture2darray", + "webgl2_materials_texture3d_partialupdate.html", "webgl2_volume_cloud", "webgl2_volume_instancing", "webgl2_volume_perlin" diff --git a/examples/webgl2_materials_texture3d_partialupdate.html b/examples/webgl2_materials_texture3d_partialupdate.html new file mode 100644 index 00000000000000..db841419f16290 --- /dev/null +++ b/examples/webgl2_materials_texture3d_partialupdate.html @@ -0,0 +1,358 @@ + + + + three.js webgl2 - volume - cloud + + + + + + +
+ three.js webgl2 - volume - cloud +
+ + + + +