Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TGALoader use DataTextureLoader #21377

Merged
merged 5 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 13 additions & 42 deletions examples/js/loaders/TGALoader.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,13 @@
THREE.TGALoader = function ( manager ) {

THREE.Loader.call( this, manager );
THREE.DataTextureLoader.call( this, manager );

};

THREE.TGALoader.prototype = Object.assign( Object.create( THREE.Loader.prototype ), {
THREE.TGALoader.prototype = Object.assign( Object.create( THREE.DataTextureLoader.prototype ), {

constructor: THREE.TGALoader,

load: function ( url, onLoad, onProgress, onError ) {

var scope = this;

var texture = new THREE.Texture();

var loader = new THREE.FileLoader( this.manager );
loader.setResponseType( 'arraybuffer' );
loader.setPath( this.path );
loader.setWithCredentials( this.withCredentials );

loader.load( url, function ( buffer ) {

texture.image = scope.parse( buffer );
texture.needsUpdate = true;

if ( onLoad !== undefined ) {

onLoad( texture );

}

}, onProgress, onError );

return texture;

},

parse: function ( buffer ) {

// reference from vthibault, https://github.com/vthibault/roBrowser/blob/master/src/Loaders/Targa.js
Expand Down Expand Up @@ -521,21 +493,20 @@ THREE.TGALoader.prototype = Object.assign( Object.create( THREE.Loader.prototype

//

var useOffscreen = typeof OffscreenCanvas !== 'undefined';

var canvas = useOffscreen ? new OffscreenCanvas( header.width, header.height ) : 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 imageData = new Uint8Array( header.width * header.height * 4 );
var result = tgaParse( use_rle, use_pal, header, offset, content );
getTgaRGBA( imageData.data, header.width, header.height, result.pixel_data, result.palettes );
getTgaRGBA( imageData, header.width, header.height, result.pixel_data, result.palettes );

return {

context.putImageData( imageData, 0, 0 );
data: imageData,
width: header.width,
height: header.height,
flipY: true,
generateMipmaps: true,
minFilter: THREE.LinearMipmapLinearFilter,

return canvas;
};

}

Expand Down
60 changes: 15 additions & 45 deletions examples/jsm/loaders/TGALoader.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,18 @@
import {
FileLoader,
Loader,
Texture
DataTextureLoader,
LinearMipmapLinearFilter
} from '../../../build/three.module.js';

var TGALoader = function ( manager ) {

Loader.call( this, manager );
DataTextureLoader.call( this, manager );

};

TGALoader.prototype = Object.assign( Object.create( Loader.prototype ), {
TGALoader.prototype = Object.assign( Object.create( DataTextureLoader.prototype ), {

constructor: TGALoader,

load: function ( url, onLoad, onProgress, onError ) {

var scope = this;

var texture = new Texture();

var loader = new FileLoader( this.manager );
loader.setResponseType( 'arraybuffer' );
loader.setPath( this.path );
loader.setWithCredentials( this.withCredentials );

loader.load( url, function ( buffer ) {

texture.image = scope.parse( buffer );
texture.needsUpdate = true;

if ( onLoad !== undefined ) {

onLoad( texture );

}

}, onProgress, onError );

return texture;

},

parse: function ( buffer ) {

// reference from vthibault, https://github.com/vthibault/roBrowser/blob/master/src/Loaders/Targa.js
Expand Down Expand Up @@ -527,21 +498,20 @@ TGALoader.prototype = Object.assign( Object.create( Loader.prototype ), {

//

var useOffscreen = typeof OffscreenCanvas !== 'undefined';

var canvas = useOffscreen ? new OffscreenCanvas( header.width, header.height ) : 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 imageData = new Uint8Array( header.width * header.height * 4 );
var result = tgaParse( use_rle, use_pal, header, offset, content );
getTgaRGBA( imageData.data, header.width, header.height, result.pixel_data, result.palettes );
getTgaRGBA( imageData, header.width, header.height, result.pixel_data, result.palettes );

return {

context.putImageData( imageData, 0, 0 );
data: imageData,
width: header.width,
height: header.height,
flipY: true,
generateMipmaps: true,
minFilter: LinearMipmapLinearFilter,

return canvas;
};

}

Expand Down
6 changes: 6 additions & 0 deletions src/loaders/DataTextureLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ DataTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ),

}

if ( texData.generateMipmaps !== undefined ) {

texture.generateMipmaps = texData.generateMipmaps;

}

texture.needsUpdate = true;

if ( onLoad ) onLoad( texture, texData );
Expand Down