From c64ecd4f6cd94b6436c007a4d44b8d5aa482b69b Mon Sep 17 00:00:00 2001 From: netpro2k Date: Fri, 5 Apr 2019 15:31:38 -0700 Subject: [PATCH 1/8] Prefer ImageBitmapLoader in TextureLoader --- src/loaders/TextureLoader.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/loaders/TextureLoader.js b/src/loaders/TextureLoader.js index 4607655ac4726d..23f73cc9a1cdf1 100644 --- a/src/loaders/TextureLoader.js +++ b/src/loaders/TextureLoader.js @@ -4,6 +4,7 @@ import { RGBAFormat, RGBFormat } from '../constants.js'; import { ImageLoader } from './ImageLoader.js'; +import { ImageBitmapLoader } from './ImageBitmapLoader.js'; import { Texture } from '../textures/Texture.js'; import { DefaultLoadingManager } from './LoadingManager.js'; @@ -22,7 +23,18 @@ Object.assign( TextureLoader.prototype, { var texture = new Texture(); - var loader = new ImageLoader( this.manager ); + var loader; + if ( window.createImageBitmap !== undefined ) { + + loader = new ImageBitmapLoader( this.manager ); + texture.flipY = false; + + } else { + + loader = new ImageLoader( this.manager ); + + } + loader.setCrossOrigin( this.crossOrigin ); loader.setPath( this.path ); From 40f373e2809caa7ce1593e23cc73e99954bb818a Mon Sep 17 00:00:00 2001 From: netpro2k Date: Fri, 5 Apr 2019 15:32:38 -0700 Subject: [PATCH 2/8] Add flipY to PlaneGeometry and PlaneBufferGeometry --- src/geometries/PlaneGeometry.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/geometries/PlaneGeometry.js b/src/geometries/PlaneGeometry.js index 6b4fb3fa17021c..3309d8d57aa52d 100644 --- a/src/geometries/PlaneGeometry.js +++ b/src/geometries/PlaneGeometry.js @@ -9,7 +9,7 @@ import { Float32BufferAttribute } from '../core/BufferAttribute.js'; // PlaneGeometry -function PlaneGeometry( width, height, widthSegments, heightSegments ) { +function PlaneGeometry( width, height, widthSegments, heightSegments, flipY ) { Geometry.call( this ); @@ -19,7 +19,8 @@ function PlaneGeometry( width, height, widthSegments, heightSegments ) { width: width, height: height, widthSegments: widthSegments, - heightSegments: heightSegments + heightSegments: heightSegments, + flipY }; this.fromBufferGeometry( new PlaneBufferGeometry( width, height, widthSegments, heightSegments ) ); @@ -32,7 +33,7 @@ PlaneGeometry.prototype.constructor = PlaneGeometry; // PlaneBufferGeometry -function PlaneBufferGeometry( width, height, widthSegments, heightSegments ) { +function PlaneBufferGeometry( width, height, widthSegments, heightSegments, flipY = true) { BufferGeometry.call( this ); @@ -42,7 +43,8 @@ function PlaneBufferGeometry( width, height, widthSegments, heightSegments ) { width: width, height: height, widthSegments: widthSegments, - heightSegments: heightSegments + heightSegments: heightSegments, + flipY: flipY }; width = width || 1; @@ -84,7 +86,11 @@ function PlaneBufferGeometry( width, height, widthSegments, heightSegments ) { normals.push( 0, 0, 1 ); uvs.push( ix / gridX ); - uvs.push( 1 - ( iy / gridY ) ); + uvs.push( + flipY ? + 1 - ( iy / gridY ) : + iy / gridY + ); } From 1d51d137148a072612e4e6619b1fc9a632226a0d Mon Sep 17 00:00:00 2001 From: netpro2k Date: Fri, 5 Apr 2019 15:40:14 -0700 Subject: [PATCH 3/8] Remove image after texture upload --- src/loaders/TextureLoader.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/loaders/TextureLoader.js b/src/loaders/TextureLoader.js index 23f73cc9a1cdf1..3f6e011aae3109 100644 --- a/src/loaders/TextureLoader.js +++ b/src/loaders/TextureLoader.js @@ -8,6 +8,7 @@ import { ImageBitmapLoader } from './ImageBitmapLoader.js'; import { Texture } from '../textures/Texture.js'; import { DefaultLoadingManager } from './LoadingManager.js'; +import { Cache } from './Cache.js'; function TextureLoader( manager ) { @@ -38,6 +39,7 @@ Object.assign( TextureLoader.prototype, { loader.setCrossOrigin( this.crossOrigin ); loader.setPath( this.path ); + const cacheKey = this.manager.resolveURL( url ); loader.load( url, function ( image ) { texture.image = image; @@ -48,6 +50,14 @@ Object.assign( TextureLoader.prototype, { texture.format = isJPEG ? RGBFormat : RGBAFormat; texture.needsUpdate = true; + texture.onUpdate = function () { + + Cache.remove( cacheKey ); + texture.image.close && texture.image.close(); + delete texture.image; + + }; + if ( onLoad !== undefined ) { onLoad( texture ); From 8e4f381d9525e57174b146d6ef235f9e5964fd22 Mon Sep 17 00:00:00 2001 From: netpro2k Date: Fri, 5 Apr 2019 15:40:48 -0700 Subject: [PATCH 4/8] Clear GLTFLoaderr internal cache after loading --- examples/js/loaders/GLTFLoader.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/js/loaders/GLTFLoader.js b/examples/js/loaders/GLTFLoader.js index f5c0106725605c..6a4d6f16541c1f 100644 --- a/examples/js/loaders/GLTFLoader.js +++ b/examples/js/loaders/GLTFLoader.js @@ -1669,6 +1669,8 @@ THREE.GLTFLoader = ( function () { onLoad( result ); + parser.cache.removeAll(); + } ).catch( onError ); }; From 20d6dcd9ceef1d40d5d9caea9a66fd4a5df35552 Mon Sep 17 00:00:00 2001 From: netpro2k Date: Fri, 5 Apr 2019 15:41:46 -0700 Subject: [PATCH 5/8] Add some temp debug logging --- src/loaders/TextureLoader.js | 1 + src/renderers/webgl/WebGLTextures.js | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/src/loaders/TextureLoader.js b/src/loaders/TextureLoader.js index 3f6e011aae3109..1422366d9ac4d0 100644 --- a/src/loaders/TextureLoader.js +++ b/src/loaders/TextureLoader.js @@ -52,6 +52,7 @@ Object.assign( TextureLoader.prototype, { texture.onUpdate = function () { + console.info( "Removing texture", texture.id, url ); Cache.remove( cacheKey ); texture.image.close && texture.image.close(); delete texture.image; diff --git a/src/renderers/webgl/WebGLTextures.js b/src/renderers/webgl/WebGLTextures.js index fdfb2e5d37ed41..2a3370193af642 100644 --- a/src/renderers/webgl/WebGLTextures.js +++ b/src/renderers/webgl/WebGLTextures.js @@ -736,6 +736,16 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, } + if ( window.ImageBitmap && texture.image instanceof ImageBitmap ) { + + console.info( "upload texture", "ImageBitmap", texture.id ); + + } else if ( texture.image instanceof HTMLImageElement ) { + + console.info( "upload texture", "HTMLImageElement", texture.id, texture.image.src ); + + } + } if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) { From 68edb77d335617a4b6a61e70320180d0240858a2 Mon Sep 17 00:00:00 2001 From: netpro2k Date: Fri, 5 Apr 2019 16:03:22 -0700 Subject: [PATCH 6/8] Delint --- src/geometries/PlaneGeometry.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/geometries/PlaneGeometry.js b/src/geometries/PlaneGeometry.js index 3309d8d57aa52d..b5715e44c7d71f 100644 --- a/src/geometries/PlaneGeometry.js +++ b/src/geometries/PlaneGeometry.js @@ -33,7 +33,7 @@ PlaneGeometry.prototype.constructor = PlaneGeometry; // PlaneBufferGeometry -function PlaneBufferGeometry( width, height, widthSegments, heightSegments, flipY = true) { +function PlaneBufferGeometry( width, height, widthSegments, heightSegments, flipY = true ) { BufferGeometry.call( this ); From 4eb12563038e01cada1cad4ece734f96077c1095 Mon Sep 17 00:00:00 2001 From: netpro2k Date: Mon, 8 Apr 2019 15:45:51 -0700 Subject: [PATCH 7/8] Fix possible race condition with image caching --- src/loaders/TextureLoader.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/loaders/TextureLoader.js b/src/loaders/TextureLoader.js index 1422366d9ac4d0..5c9751c4fbe49c 100644 --- a/src/loaders/TextureLoader.js +++ b/src/loaders/TextureLoader.js @@ -42,6 +42,9 @@ Object.assign( TextureLoader.prototype, { const cacheKey = this.manager.resolveURL( url ); loader.load( url, function ( image ) { + // Image was just added to cache before this function gets called, disable caching by immediatly removing it + Cache.remove( cacheKey ); + texture.image = image; // JPEGs can't have an alpha channel, so memory can be saved by storing them as RGB. @@ -53,7 +56,6 @@ Object.assign( TextureLoader.prototype, { texture.onUpdate = function () { console.info( "Removing texture", texture.id, url ); - Cache.remove( cacheKey ); texture.image.close && texture.image.close(); delete texture.image; From 54d1324fac189947b2b8dfafacbc3f74e6efd1dd Mon Sep 17 00:00:00 2001 From: netpro2k Date: Mon, 8 Apr 2019 15:46:10 -0700 Subject: [PATCH 8/8] Remove comments --- src/loaders/TextureLoader.js | 1 - src/renderers/webgl/WebGLTextures.js | 10 +++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/loaders/TextureLoader.js b/src/loaders/TextureLoader.js index 5c9751c4fbe49c..d1b8138b48cecb 100644 --- a/src/loaders/TextureLoader.js +++ b/src/loaders/TextureLoader.js @@ -55,7 +55,6 @@ Object.assign( TextureLoader.prototype, { texture.onUpdate = function () { - console.info( "Removing texture", texture.id, url ); texture.image.close && texture.image.close(); delete texture.image; diff --git a/src/renderers/webgl/WebGLTextures.js b/src/renderers/webgl/WebGLTextures.js index 2a3370193af642..ede52d10f48806 100644 --- a/src/renderers/webgl/WebGLTextures.js +++ b/src/renderers/webgl/WebGLTextures.js @@ -736,15 +736,15 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, } - if ( window.ImageBitmap && texture.image instanceof ImageBitmap ) { + // if ( window.ImageBitmap && texture.image instanceof ImageBitmap ) { - console.info( "upload texture", "ImageBitmap", texture.id ); + // console.info( "upload texture", "ImageBitmap", texture.id ); - } else if ( texture.image instanceof HTMLImageElement ) { + // } else if ( texture.image instanceof HTMLImageElement ) { - console.info( "upload texture", "HTMLImageElement", texture.id, texture.image.src ); + // console.info( "upload texture", "HTMLImageElement", texture.id, texture.image.src ); - } + // } }