Kaynağa Gözat

Change TGALoader ouput from DataTexture with pixels to Texture with canvas

* Revert "Fix tga parsing bug of horizontal origin."

This reverts commit 318c4ccfe77a2ac6541145f7491a95d5f37105ae.

* Change TGALoader output from DataTexture with pixels to Texture with canvas
Takahiro 9 yıl önce
ebeveyn
işleme
fff179add1
1 değiştirilmiş dosya ile 53 ekleme ve 27 silme
  1. 53 27
      examples/js/loaders/TGALoader.js

+ 53 - 27
examples/js/loaders/TGALoader.js

@@ -1,6 +1,7 @@
 /*
  * @author Daosheng Mu / https://github.com/DaoshengMu/
  * @author mrdoob / http://mrdoob.com/
+ * @author takahirox / https://github.com/takahirox/
  */
 
 THREE.TGALoader = function ( manager ) {
@@ -9,11 +10,34 @@ THREE.TGALoader = function ( manager ) {
 
 };
 
-// extend THREE.BinaryTextureLoader
-THREE.TGALoader.prototype = Object.create( THREE.BinaryTextureLoader.prototype );
+THREE.TGALoader.prototype.load = function ( url, onLoad, onProgress, onError ) {
+
+	var scope = this;
+
+	var texture = new THREE.Texture();
+
+	var loader = new THREE.XHRLoader( this.manager );
+	loader.setResponseType( 'arraybuffer' );
+
+	loader.load( url, function ( buffer ) {
+
+		texture.image = scope.parse( buffer );
+		texture.needsUpdate = true;
+
+		if ( onLoad !== undefined ) {
+
+			onLoad( texture );
+
+		}
+
+	}, onProgress, onError );
+
+	return texture;
+
+};
 
 // reference from vthibault, https://github.com/vthibault/roBrowser/blob/master/src/Loaders/Targa.js
-THREE.TGALoader.prototype._parser = function ( buffer ) {
+THREE.TGALoader.prototype.parse = function ( buffer ) {
 
 	// TGA Constants
 	var TGA_TYPE_NO_DATA = 0,
@@ -374,15 +398,14 @@ THREE.TGALoader.prototype._parser = function ( buffer ) {
 
 	}
 
-	function getTgaRGBA( width, height, image, palette ) {
+	function getTgaRGBA( data, width, height, image, palette ) {
 
 		var x_start,
 			y_start,
 			x_step,
 			y_step,
 			x_end,
-			y_end,
-			data = new Uint8Array( width * height * 4 );
+			y_end;
 
 		switch ( ( header.flags & TGA_ORIGIN_MASK ) >> TGA_ORIGIN_SHIFT ) {
 			default:
@@ -390,36 +413,36 @@ THREE.TGALoader.prototype._parser = function ( buffer ) {
 				x_start = 0;
 				x_step = 1;
 				x_end = width;
-				y_start = height - 1;
-				y_step = -1;
-				y_end = -1;
+				y_start = 0;
+				y_step = 1;
+				y_end = height;
 				break;
 
 			case TGA_ORIGIN_BL:
 				x_start = 0;
 				x_step = 1;
 				x_end = width;
-				y_start = 0;
-				y_step = 1;
-				y_end = height;
+				y_start = height - 1;
+				y_step = - 1;
+				y_end = - 1;
 				break;
 
 			case TGA_ORIGIN_UR:
 				x_start = width - 1;
 				x_step = - 1;
 				x_end = - 1;
-				y_start = height - 1;
-				y_step = -1;
-				y_end = -1;
+				y_start = 0;
+				y_step = 1;
+				y_end = height;
 				break;
 
 			case TGA_ORIGIN_BR:
 				x_start = width - 1;
 				x_step = - 1;
 				x_end = - 1;
-				y_start = 0;
-				y_step = 1;
-				y_end = height;
+				y_start = height - 1;
+				y_step = - 1;
+				y_end = - 1;
 				break;
 
 		}
@@ -471,15 +494,18 @@ THREE.TGALoader.prototype._parser = function ( buffer ) {
 
 	}
 
+	var canvas = document.createElement( 'canvas' );
+	canvas.width = header.width;
+	canvas.height = header.height;
+
+	var context = canvas.getContext( '2d' );
+	var imageData = context.createImageData( header.width, header.height );
+
 	var result = tgaParse( use_rle, use_pal, header, offset, content );
-	var rgbaData = getTgaRGBA( header.width, header.height, result.pixel_data, result.palettes );
-
-	return {
-		width: header.width,
-		height: header.height,
-		data: rgbaData,
-		magFilter: THREE.NearestFilter,
-		minFilter: THREE.NearestFilter
-	};
+	var rgbaData = getTgaRGBA( imageData.data, header.width, header.height, result.pixel_data, result.palettes );
+
+	context.putImageData( imageData, 0, 0 );
+
+	return canvas;
 
 };