From 22a8e81424ea49378df67de479e20f1d40194f28 Mon Sep 17 00:00:00 2001 From: Felix Herbst Date: Tue, 6 Sep 2022 23:24:55 +0200 Subject: [PATCH] only use one temp render context, clean up renderer after writing file, make metalnessMap and roughnessMap readable --- examples/jsm/exporters/GLTFExporter.js | 41 +++++++++++++++++++++----- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/examples/jsm/exporters/GLTFExporter.js b/examples/jsm/exporters/GLTFExporter.js index b77f4ddb8863d9..02b55faf6d3fc0 100644 --- a/examples/jsm/exporters/GLTFExporter.js +++ b/examples/jsm/exporters/GLTFExporter.js @@ -545,6 +545,12 @@ class GLTFWriter { } + // Clean up in case we had to create a temporary renderer for blitting compressed textures. + if (this.temporaryRenderer) { + + this.temporaryRenderer.dispose(); + + } } @@ -751,12 +757,19 @@ class GLTFWriter { const temporaryScene = new Scene(); temporaryScene.add( fullscreenQuad ); - const temporaryRenderer = new WebGLRenderer( { antialias: false } ); - temporaryRenderer.setSize( Math.min(map.image.width, maxTextureSize), Math.min(map.image.height, maxTextureSize) ); - temporaryRenderer.clear(); - temporaryRenderer.render( temporaryScene, temporaryCam ); + if (!this.temporaryRenderer) { + + this.temporaryRenderer = new WebGLRenderer( { antialias: false } ); + + } + + this.temporaryRenderer.setSize( Math.min(map.image.width, maxTextureSize), Math.min(map.image.height, maxTextureSize) ); + this.temporaryRenderer.clear(); + this.temporaryRenderer.render( temporaryScene, temporaryCam ); - return new Texture( temporaryRenderer.domElement ); + const readableTexture = new Texture( this.temporaryRenderer.domElement ); + readableTexture.userData.mimeType = 'image/png'; + return readableTexture; } @@ -788,6 +801,22 @@ class GLTFWriter { console.warn( 'THREE.GLTFExporter: Merged metalnessMap and roughnessMap textures.' ); + if ( typeof CompressedTexture !== 'undefined') { + + if ( metalnessMap instanceof CompressedTexture ) { + + metalnessMap = this.buildReadableTexture( metalnessMap ); + + } + + if ( roughnessMap instanceof CompressedTexture ) { + + roughnessMap = this.buildReadableTexture( roughnessMap ); + + } + + } + const metalness = metalnessMap?.image; const roughness = roughnessMap?.image; @@ -1180,7 +1209,6 @@ class GLTFWriter { console.error( 'GLTFExporter: Only RGBAFormat is supported.', image ); } - if ( image.width > options.maxTextureSize || image.height > options.maxTextureSize ) { console.warn( 'GLTFExporter: Image size is bigger than maxTextureSize', image ); @@ -1311,7 +1339,6 @@ class GLTFWriter { if ( typeof CompressedTexture !== 'undefined' && map instanceof CompressedTexture ) { modifiedMap = this.buildReadableTexture( map, options.maxTextureSize ); - modifiedMap.userData.mimeType = 'image/png'; }