From 94f287d3eaaf4df4dd9d189f880a3f86a4d3464c Mon Sep 17 00:00:00 2001 From: Michael Herzog Date: Mon, 9 Sep 2024 11:18:53 +0200 Subject: [PATCH] TextureUtils: Add WebGPU version. (#29348) * TextureUtils: Add WebGPU version. * TextureUtilsGPU: Clean up. * TextureUtilsGPU: Fix uv coords. --- examples/jsm/utils/TextureUtils.js | 1 + examples/jsm/utils/TextureUtilsGPU.js | 63 +++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 examples/jsm/utils/TextureUtilsGPU.js diff --git a/examples/jsm/utils/TextureUtils.js b/examples/jsm/utils/TextureUtils.js index 0f944e5ffde10f..625c7354dc870b 100644 --- a/examples/jsm/utils/TextureUtils.js +++ b/examples/jsm/utils/TextureUtils.js @@ -83,6 +83,7 @@ export function decompress( texture, maxTextureSize = Infinity, renderer = null readableTexture.magFilter = texture.magFilter; readableTexture.wrapS = texture.wrapS; readableTexture.wrapT = texture.wrapT; + readableTexture.colorSpace = texture.colorSpace; readableTexture.name = texture.name; if ( _renderer ) { diff --git a/examples/jsm/utils/TextureUtilsGPU.js b/examples/jsm/utils/TextureUtilsGPU.js new file mode 100644 index 00000000000000..9c44273c9530e3 --- /dev/null +++ b/examples/jsm/utils/TextureUtilsGPU.js @@ -0,0 +1,63 @@ +import { + QuadMesh, + NodeMaterial, + WebGPURenderer, + CanvasTexture +} from 'three'; +import { texture, uv } from 'three/tsl'; + +let _renderer; +const _quadMesh = /*@__PURE__*/ new QuadMesh(); + +export async function decompress( blitTexture, maxTextureSize = Infinity, renderer = null ) { + + if ( renderer === null ) { + + renderer = _renderer = new WebGPURenderer(); + await renderer.init(); + + } + + const material = new NodeMaterial(); + material.fragmentNode = texture( blitTexture ).uv( uv().flipY() ); + + const width = Math.min( blitTexture.image.width, maxTextureSize ); + const height = Math.min( blitTexture.image.height, maxTextureSize ); + + const currentOutputColorSpace = renderer.outputColorSpace; + + renderer.setSize( width, height ); + renderer.outputColorSpace = blitTexture.colorSpace; + + _quadMesh.material = material; + _quadMesh.render( renderer ); + + renderer.outputColorSpace = currentOutputColorSpace; + + const canvas = document.createElement( 'canvas' ); + const context = canvas.getContext( '2d' ); + + canvas.width = width; + canvas.height = height; + + context.drawImage( renderer.domElement, 0, 0, width, height ); + + const readableTexture = new CanvasTexture( canvas ); + + readableTexture.minFilter = blitTexture.minFilter; + readableTexture.magFilter = blitTexture.magFilter; + readableTexture.wrapS = blitTexture.wrapS; + readableTexture.wrapT = blitTexture.wrapT; + readableTexture.colorSpace = blitTexture.colorSpace; + readableTexture.name = blitTexture.name; + + if ( _renderer !== null ) { + + _renderer.dispose(); + _renderer = null; + + } + + return readableTexture; + +}