浏览代码

Save memory by loading JPEGs as RGB by default (#9203)

JPEGs can't have an alpha channel, so it's optimal to load them as RGB
textures instead of RGBA. This saves 25% of texture memory on
OpenGL-based WebGL implementations. The optimization is especially
significant on memory-constrained mobile platforms.
Olli Etuaho 9 年之前
父节点
当前提交
da0d1c1fb7
共有 1 个文件被更改,包括 6 次插入0 次删除
  1. 6 0
      src/loaders/TextureLoader.js

+ 6 - 0
src/loaders/TextureLoader.js

@@ -19,6 +19,12 @@ Object.assign( THREE.TextureLoader.prototype, {
 		loader.setPath( this.path );
 		loader.setPath( this.path );
 		loader.load( url, function ( image ) {
 		loader.load( url, function ( image ) {
 
 
+			// JPEGs can't have an alpha channel, so memory can be saved by storing them as RGB.
+			var isJPEG = url.search(/\.(jpg|jpeg)$/) > 0;
+			if (!isJPEG) {
+				isJPEG = url.search(/^data\:image\/jpeg/) == 0;
+			}
+			texture.format = isJPEG ? THREE.RGBFormat : THREE.RGBAFormat;
 			texture.image = image;
 			texture.image = image;
 			texture.needsUpdate = true;
 			texture.needsUpdate = true;