Selaa lähdekoodia

DataTextureLoader: Add `createDataTexture()` (#33705)

Michael Herzog 1 viikko sitten
vanhempi
sitoutus
459de90a03

+ 4 - 18
examples/jsm/loaders/EXRLoader.js

@@ -3343,6 +3343,10 @@ class EXRLoader extends DataTextureLoader {
 			format: EXRDecoder.format,
 			colorSpace: EXRDecoder.colorSpace,
 			type: this.type,
+			minFilter: LinearFilter,
+			magFilter: LinearFilter,
+			generateMipmaps: false,
+			flipY: false,
 		};
 
 	}
@@ -3386,24 +3390,6 @@ class EXRLoader extends DataTextureLoader {
 
 	}
 
-	load( url, onLoad, onProgress, onError ) {
-
-		function onLoadCallback( texture, texData ) {
-
-			texture.colorSpace = texData.colorSpace;
-			texture.minFilter = LinearFilter;
-			texture.magFilter = LinearFilter;
-			texture.generateMipmaps = false;
-			texture.flipY = false;
-
-			if ( onLoad ) onLoad( texture, texData );
-
-		}
-
-		return super.load( url, onLoadCallback, onProgress, onError );
-
-	}
-
 }
 
 export { EXRLoader };

+ 6 - 28
examples/jsm/loaders/HDRLoader.js

@@ -433,7 +433,12 @@ class HDRLoader extends DataTextureLoader {
 			header: rgbe_header_info.string,
 			gamma: rgbe_header_info.gamma,
 			exposure: rgbe_header_info.exposure,
-			type: type
+			type: type,
+			colorSpace: LinearSRGBColorSpace,
+			minFilter: LinearFilter,
+			magFilter: LinearFilter,
+			generateMipmaps: false,
+			flipY: true
 		};
 
 	}
@@ -451,33 +456,6 @@ class HDRLoader extends DataTextureLoader {
 
 	}
 
-	load( url, onLoad, onProgress, onError ) {
-
-		function onLoadCallback( texture, texData ) {
-
-			switch ( texture.type ) {
-
-				case FloatType:
-				case HalfFloatType:
-
-					texture.colorSpace = LinearSRGBColorSpace;
-					texture.minFilter = LinearFilter;
-					texture.magFilter = LinearFilter;
-					texture.generateMipmaps = false;
-					texture.flipY = true;
-
-					break;
-
-			}
-
-			if ( onLoad ) onLoad( texture, texData );
-
-		}
-
-		return super.load( url, onLoadCallback, onProgress, onError );
-
-	}
-
 }
 
 export { HDRLoader };

+ 1 - 20
examples/webgl_materials_matcap.html

@@ -190,27 +190,8 @@
 
 			function handleEXR( event ) {
 
-				const contents = event.target.result;
-
 				const loader = new EXRLoader();
-
-				loader.setDataType( THREE.HalfFloatType );
-
-				const texData = loader.parse( contents );
-
-				const texture = new THREE.DataTexture();
-
-				texture.image.width = texData.width;
-				texture.image.height = texData.height;
-				texture.image.data = texData.data;
-
-				texture.format = texData.format;
-				texture.type = texData.type;
-				texture.colorSpace = THREE.LinearSRGBColorSpace;
-				texture.minFilter = THREE.LinearFilter;
-				texture.magFilter = THREE.LinearFilter;
-				texture.generateMipmaps = false;
-				texture.flipY = false;
+				const texture = loader.createDataTexture( event.target.result );
 
 				updateMatcap( texture );
 

+ 1 - 20
examples/webgpu_materials_matcap.html

@@ -191,27 +191,8 @@
 
 			function handleEXR( event ) {
 
-				const contents = event.target.result;
-
 				const loader = new EXRLoader();
-
-				loader.setDataType( THREE.HalfFloatType );
-
-				const texData = loader.parse( contents );
-
-				const texture = new THREE.DataTexture();
-
-				texture.image.width = texData.width;
-				texture.image.height = texData.height;
-				texture.image.data = texData.data;
-
-				texture.format = texData.format;
-				texture.type = texData.type;
-				texture.colorSpace = THREE.LinearSRGBColorSpace;
-				texture.minFilter = THREE.LinearFilter;
-				texture.magFilter = THREE.LinearFilter;
-				texture.generateMipmaps = false;
-				texture.flipY = false;
+				const texture = loader.createDataTexture( event.target.result );
 
 				updateMatcap( texture );
 

+ 69 - 38
src/loaders/DataTextureLoader.js

@@ -74,77 +74,108 @@ class DataTextureLoader extends Loader {
 
 			}
 
-			if ( texData.image !== undefined ) {
+			scope._applyTexData( texture, texData );
 
-				texture.image = texData.image;
+			if ( onLoad ) onLoad( texture, texData );
 
-			} else if ( texData.data !== undefined ) {
+		}, onProgress, onError );
 
-				texture.image.width = texData.width;
-				texture.image.height = texData.height;
-				texture.image.data = texData.data;
 
-			}
+		return texture;
 
-			texture.wrapS = texData.wrapS !== undefined ? texData.wrapS : ClampToEdgeWrapping;
-			texture.wrapT = texData.wrapT !== undefined ? texData.wrapT : ClampToEdgeWrapping;
+	}
 
-			texture.magFilter = texData.magFilter !== undefined ? texData.magFilter : LinearFilter;
-			texture.minFilter = texData.minFilter !== undefined ? texData.minFilter : LinearFilter;
+	/**
+	 * Parses the given buffer and returns a configured data texture. Use this method
+	 * for parsing texture data that is already in memory (e.g. drag and drop or data
+	 * loaded from a server) without going through {@link DataTextureLoader#load}.
+	 *
+	 * @param {ArrayBuffer} buffer - The raw texture data.
+	 * @return {DataTexture} The data texture.
+	 */
+	createDataTexture( buffer ) {
 
-			texture.anisotropy = texData.anisotropy !== undefined ? texData.anisotropy : 1;
+		const texture = new DataTexture();
 
-			if ( texData.colorSpace !== undefined ) {
+		this._applyTexData( texture, this.parse( buffer ) );
 
-				texture.colorSpace = texData.colorSpace;
+		return texture;
 
-			}
+	}
 
-			if ( texData.flipY !== undefined ) {
+	/**
+	 * Applies the given parsed texture data to the given data texture.
+	 *
+	 * @private
+	 * @param {DataTexture} texture - The data texture.
+	 * @param {DataTextureLoader~TexData} texData - The parsed texture data.
+	 */
+	_applyTexData( texture, texData ) {
 
-				texture.flipY = texData.flipY;
+		if ( texData.image !== undefined ) {
 
-			}
+			texture.image = texData.image;
 
-			if ( texData.format !== undefined ) {
+		} else if ( texData.data !== undefined ) {
 
-				texture.format = texData.format;
+			texture.image.width = texData.width;
+			texture.image.height = texData.height;
+			texture.image.data = texData.data;
 
-			}
+		}
 
-			if ( texData.type !== undefined ) {
+		texture.wrapS = texData.wrapS !== undefined ? texData.wrapS : ClampToEdgeWrapping;
+		texture.wrapT = texData.wrapT !== undefined ? texData.wrapT : ClampToEdgeWrapping;
 
-				texture.type = texData.type;
+		texture.magFilter = texData.magFilter !== undefined ? texData.magFilter : LinearFilter;
+		texture.minFilter = texData.minFilter !== undefined ? texData.minFilter : LinearFilter;
 
-			}
+		texture.anisotropy = texData.anisotropy !== undefined ? texData.anisotropy : 1;
 
-			if ( texData.mipmaps !== undefined ) {
+		if ( texData.colorSpace !== undefined ) {
 
-				texture.mipmaps = texData.mipmaps;
-				texture.minFilter = LinearMipmapLinearFilter; // presumably...
+			texture.colorSpace = texData.colorSpace;
 
-			}
+		}
 
-			if ( texData.mipmapCount === 1 ) {
+		if ( texData.flipY !== undefined ) {
 
-				texture.minFilter = LinearFilter;
+			texture.flipY = texData.flipY;
 
-			}
+		}
 
-			if ( texData.generateMipmaps !== undefined ) {
+		if ( texData.format !== undefined ) {
 
-				texture.generateMipmaps = texData.generateMipmaps;
+			texture.format = texData.format;
 
-			}
+		}
 
-			texture.needsUpdate = true;
+		if ( texData.type !== undefined ) {
 
-			if ( onLoad ) onLoad( texture, texData );
+			texture.type = texData.type;
 
-		}, onProgress, onError );
+		}
 
+		if ( texData.mipmaps !== undefined ) {
 
-		return texture;
+			texture.mipmaps = texData.mipmaps;
+			texture.minFilter = LinearMipmapLinearFilter; // presumably...
+
+		}
+
+		if ( texData.mipmapCount === 1 ) {
+
+			texture.minFilter = LinearFilter;
+
+		}
+
+		if ( texData.generateMipmaps !== undefined ) {
+
+			texture.generateMipmaps = texData.generateMipmaps;
+
+		}
+
+		texture.needsUpdate = true;
 
 	}
 

粤ICP备19079148号