Skip to content

Commit 151a294

Browse files
authored
Examples: Convert loaders to ES6 Part II. (#21614)
1 parent d1a5133 commit 151a294

17 files changed

+5746
-5693
lines changed

examples/js/loaders/GLTFLoader.js

Lines changed: 1280 additions & 1227 deletions
Large diffs are not rendered by default.

examples/js/loaders/HDRCubeTextureLoader.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
( function () {
22

3-
var HDRCubeTextureLoader = function ( manager ) {
3+
class HDRCubeTextureLoader extends THREE.Loader {
44

5-
THREE.Loader.call( this, manager );
6-
this.hdrLoader = new THREE.RGBELoader();
7-
this.type = THREE.UnsignedByteType;
5+
constructor( manager ) {
86

9-
};
7+
super( manager );
8+
this.hdrLoader = new THREE.RGBELoader();
9+
this.type = THREE.UnsignedByteType;
1010

11-
HDRCubeTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.prototype ), {
12-
constructor: HDRCubeTextureLoader,
13-
load: function ( urls, onLoad, onProgress, onError ) {
11+
}
12+
13+
load( urls, onLoad, onProgress, onError ) {
1414

1515
if ( ! Array.isArray( urls ) ) {
1616

@@ -23,7 +23,7 @@
2323

2424
}
2525

26-
var texture = new THREE.CubeTexture();
26+
const texture = new THREE.CubeTexture();
2727
texture.type = this.type;
2828

2929
switch ( texture.type ) {
@@ -54,20 +54,20 @@
5454

5555
}
5656

57-
var scope = this;
58-
var loaded = 0;
57+
const scope = this;
58+
let loaded = 0;
5959

6060
function loadHDRData( i, onLoad, onProgress, onError ) {
6161

6262
new THREE.FileLoader( scope.manager ).setPath( scope.path ).setResponseType( 'arraybuffer' ).setWithCredentials( scope.withCredentials ).load( urls[ i ], function ( buffer ) {
6363

6464
loaded ++;
65-
var texData = scope.hdrLoader.parse( buffer );
65+
const texData = scope.hdrLoader.parse( buffer );
6666
if ( ! texData ) return;
6767

6868
if ( texData.data !== undefined ) {
6969

70-
var dataTexture = new THREE.DataTexture( texData.data, texData.width, texData.height );
70+
const dataTexture = new THREE.DataTexture( texData.data, texData.width, texData.height );
7171
dataTexture.type = texture.type;
7272
dataTexture.encoding = texture.encoding;
7373
dataTexture.format = texture.format;
@@ -89,23 +89,25 @@
8989

9090
}
9191

92-
for ( var i = 0; i < urls.length; i ++ ) {
92+
for ( let i = 0; i < urls.length; i ++ ) {
9393

9494
loadHDRData( i, onLoad, onProgress, onError );
9595

9696
}
9797

9898
return texture;
9999

100-
},
101-
setDataType: function ( value ) {
100+
}
101+
102+
setDataType( value ) {
102103

103104
this.type = value;
104105
this.hdrLoader.setDataType( value );
105106
return this;
106107

107108
}
108-
} );
109+
110+
}
109111

110112
THREE.HDRCubeTextureLoader = HDRCubeTextureLoader;
111113

examples/js/loaders/KTXLoader.js

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
* ported from https://github.com/BabylonJS/Babylon.js/blob/master/src/Tools/babylon.khronosTextureContainer.ts
88
*/
99

10-
var KTXLoader = function ( manager ) {
10+
class KTXLoader extends THREE.CompressedTextureLoader {
1111

12-
THREE.CompressedTextureLoader.call( this, manager );
12+
constructor( manager ) {
1313

14-
};
14+
super( manager );
1515

16-
KTXLoader.prototype = Object.assign( Object.create( THREE.CompressedTextureLoader.prototype ), {
17-
constructor: KTXLoader,
18-
parse: function ( buffer, loadMipmaps ) {
16+
}
17+
18+
parse( buffer, loadMipmaps ) {
1919

20-
var ktx = new KhronosTextureContainer( buffer, 1 );
20+
const ktx = new KhronosTextureContainer( buffer, 1 );
2121
return {
2222
mipmaps: ktx.mipmaps( loadMipmaps ),
2323
width: ktx.pixelWidth,
@@ -28,25 +28,34 @@
2828
};
2929

3030
}
31-
} );
3231

33-
var KhronosTextureContainer = function () {
32+
}
33+
34+
const HEADER_LEN = 12 + 13 * 4; // identifier + header elements (not including key value meta-data pairs)
35+
// load types
36+
37+
const COMPRESSED_2D = 0; // uses a gl.compressedTexImage2D()
38+
//const COMPRESSED_3D = 1; // uses a gl.compressedTexImage3D()
39+
//const TEX_2D = 2; // uses a gl.texImage2D()
40+
//const TEX_3D = 3; // uses a gl.texImage3D()
41+
42+
class KhronosTextureContainer {
3443

3544
/**
3645
* @param {ArrayBuffer} arrayBuffer- contents of the KTX container file
3746
* @param {number} facesExpected- should be either 1 or 6, based whether a cube texture or or
3847
* @param {boolean} threeDExpected- provision for indicating that data should be a 3D texture, not implemented
3948
* @param {boolean} textureArrayExpected- provision for indicating that data should be a texture array, not implemented
4049
*/
41-
function KhronosTextureContainer( arrayBuffer, facesExpected
50+
constructor( arrayBuffer, facesExpected
4251
/*, threeDExpected, textureArrayExpected */
4352
) {
4453

4554
this.arrayBuffer = arrayBuffer; // Test that it is a ktx formatted file, based on the first 12 bytes, character representation is:
4655
// '´', 'K', 'T', 'X', ' ', '1', '1', 'ª', '\r', '\n', '\x1A', '\n'
4756
// 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A
4857

49-
var identifier = new Uint8Array( this.arrayBuffer, 0, 12 );
58+
const identifier = new Uint8Array( this.arrayBuffer, 0, 12 );
5059

5160
if ( identifier[ 0 ] !== 0xAB || identifier[ 1 ] !== 0x4B || identifier[ 2 ] !== 0x54 || identifier[ 3 ] !== 0x58 || identifier[ 4 ] !== 0x20 || identifier[ 5 ] !== 0x31 || identifier[ 6 ] !== 0x31 || identifier[ 7 ] !== 0xBB || identifier[ 8 ] !== 0x0D || identifier[ 9 ] !== 0x0A || identifier[ 10 ] !== 0x1A || identifier[ 11 ] !== 0x0A ) {
5261

@@ -56,10 +65,10 @@
5665
} // load the reset of the header in native 32 bit uint
5766

5867

59-
var dataSize = Uint32Array.BYTES_PER_ELEMENT;
60-
var headerDataView = new DataView( this.arrayBuffer, 12, 13 * dataSize );
61-
var endianness = headerDataView.getUint32( 0, true );
62-
var littleEndian = endianness === 0x04030201;
68+
const dataSize = Uint32Array.BYTES_PER_ELEMENT;
69+
const headerDataView = new DataView( this.arrayBuffer, 12, 13 * dataSize );
70+
const endianness = headerDataView.getUint32( 0, true );
71+
const littleEndian = endianness === 0x04030201;
6372
this.glType = headerDataView.getUint32( 1 * dataSize, littleEndian ); // must be 0 for compressed textures
6473

6574
this.glTypeSize = headerDataView.getUint32( 2 * dataSize, littleEndian ); // must be 1 for compressed textures
@@ -120,29 +129,28 @@
120129
// would need to make this more elaborate & adjust checks above to support more than one load type
121130

122131

123-
this.loadType = KhronosTextureContainer.COMPRESSED_2D;
124-
125-
} // return mipmaps for js
132+
this.loadType = COMPRESSED_2D;
126133

134+
}
127135

128-
KhronosTextureContainer.prototype.mipmaps = function ( loadMipmaps ) {
136+
mipmaps( loadMipmaps ) {
129137

130-
var mipmaps = []; // initialize width & height for level 1
138+
const mipmaps = []; // initialize width & height for level 1
131139

132-
var dataOffset = KhronosTextureContainer.HEADER_LEN + this.bytesOfKeyValueData;
133-
var width = this.pixelWidth;
134-
var height = this.pixelHeight;
135-
var mipmapCount = loadMipmaps ? this.numberOfMipmapLevels : 1;
140+
let dataOffset = HEADER_LEN + this.bytesOfKeyValueData;
141+
let width = this.pixelWidth;
142+
let height = this.pixelHeight;
143+
const mipmapCount = loadMipmaps ? this.numberOfMipmapLevels : 1;
136144

137-
for ( var level = 0; level < mipmapCount; level ++ ) {
145+
for ( let level = 0; level < mipmapCount; level ++ ) {
138146

139-
var imageSize = new Int32Array( this.arrayBuffer, dataOffset, 1 )[ 0 ]; // size per face, since not supporting array cubemaps
147+
const imageSize = new Int32Array( this.arrayBuffer, dataOffset, 1 )[ 0 ]; // size per face, since not supporting array cubemaps
140148

141149
dataOffset += 4; // size of the image + 4 for the imageSize field
142150

143-
for ( var face = 0; face < this.numberOfFaces; face ++ ) {
151+
for ( let face = 0; face < this.numberOfFaces; face ++ ) {
144152

145-
var byteArray = new Uint8Array( this.arrayBuffer, dataOffset, imageSize );
153+
const byteArray = new Uint8Array( this.arrayBuffer, dataOffset, imageSize );
146154
mipmaps.push( {
147155
'data': byteArray,
148156
'width': width,
@@ -160,22 +168,9 @@
160168

161169
return mipmaps;
162170

163-
};
164-
165-
KhronosTextureContainer.HEADER_LEN = 12 + 13 * 4; // identifier + header elements (not including key value meta-data pairs)
166-
// load types
167-
168-
KhronosTextureContainer.COMPRESSED_2D = 0; // uses a gl.compressedTexImage2D()
169-
170-
KhronosTextureContainer.COMPRESSED_3D = 1; // uses a gl.compressedTexImage3D()
171-
172-
KhronosTextureContainer.TEX_2D = 2; // uses a gl.texImage2D()
173-
174-
KhronosTextureContainer.TEX_3D = 3; // uses a gl.texImage3D()
175-
176-
return KhronosTextureContainer;
171+
}
177172

178-
}();
173+
}
179174

180175
THREE.KTXLoader = KTXLoader;
181176

0 commit comments

Comments
 (0)