|
7 | 7 | * ported from https://github.com/BabylonJS/Babylon.js/blob/master/src/Tools/babylon.khronosTextureContainer.ts |
8 | 8 | */ |
9 | 9 |
|
10 | | - var KTXLoader = function ( manager ) { |
| 10 | + class KTXLoader extends THREE.CompressedTextureLoader { |
11 | 11 |
|
12 | | - THREE.CompressedTextureLoader.call( this, manager ); |
| 12 | + constructor( manager ) { |
13 | 13 |
|
14 | | - }; |
| 14 | + super( manager ); |
15 | 15 |
|
16 | | - KTXLoader.prototype = Object.assign( Object.create( THREE.CompressedTextureLoader.prototype ), { |
17 | | - constructor: KTXLoader, |
18 | | - parse: function ( buffer, loadMipmaps ) { |
| 16 | + } |
| 17 | + |
| 18 | + parse( buffer, loadMipmaps ) { |
19 | 19 |
|
20 | | - var ktx = new KhronosTextureContainer( buffer, 1 ); |
| 20 | + const ktx = new KhronosTextureContainer( buffer, 1 ); |
21 | 21 | return { |
22 | 22 | mipmaps: ktx.mipmaps( loadMipmaps ), |
23 | 23 | width: ktx.pixelWidth, |
|
28 | 28 | }; |
29 | 29 |
|
30 | 30 | } |
31 | | - } ); |
32 | 31 |
|
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 { |
34 | 43 |
|
35 | 44 | /** |
36 | 45 | * @param {ArrayBuffer} arrayBuffer- contents of the KTX container file |
37 | 46 | * @param {number} facesExpected- should be either 1 or 6, based whether a cube texture or or |
38 | 47 | * @param {boolean} threeDExpected- provision for indicating that data should be a 3D texture, not implemented |
39 | 48 | * @param {boolean} textureArrayExpected- provision for indicating that data should be a texture array, not implemented |
40 | 49 | */ |
41 | | - function KhronosTextureContainer( arrayBuffer, facesExpected |
| 50 | + constructor( arrayBuffer, facesExpected |
42 | 51 | /*, threeDExpected, textureArrayExpected */ |
43 | 52 | ) { |
44 | 53 |
|
45 | 54 | this.arrayBuffer = arrayBuffer; // Test that it is a ktx formatted file, based on the first 12 bytes, character representation is: |
46 | 55 | // '´', 'K', 'T', 'X', ' ', '1', '1', 'ª', '\r', '\n', '\x1A', '\n' |
47 | 56 | // 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A |
48 | 57 |
|
49 | | - var identifier = new Uint8Array( this.arrayBuffer, 0, 12 ); |
| 58 | + const identifier = new Uint8Array( this.arrayBuffer, 0, 12 ); |
50 | 59 |
|
51 | 60 | 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 ) { |
52 | 61 |
|
|
56 | 65 | } // load the reset of the header in native 32 bit uint |
57 | 66 |
|
58 | 67 |
|
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; |
63 | 72 | this.glType = headerDataView.getUint32( 1 * dataSize, littleEndian ); // must be 0 for compressed textures |
64 | 73 |
|
65 | 74 | this.glTypeSize = headerDataView.getUint32( 2 * dataSize, littleEndian ); // must be 1 for compressed textures |
|
120 | 129 | // would need to make this more elaborate & adjust checks above to support more than one load type |
121 | 130 |
|
122 | 131 |
|
123 | | - this.loadType = KhronosTextureContainer.COMPRESSED_2D; |
124 | | - |
125 | | - } // return mipmaps for js |
| 132 | + this.loadType = COMPRESSED_2D; |
126 | 133 |
|
| 134 | + } |
127 | 135 |
|
128 | | - KhronosTextureContainer.prototype.mipmaps = function ( loadMipmaps ) { |
| 136 | + mipmaps( loadMipmaps ) { |
129 | 137 |
|
130 | | - var mipmaps = []; // initialize width & height for level 1 |
| 138 | + const mipmaps = []; // initialize width & height for level 1 |
131 | 139 |
|
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; |
136 | 144 |
|
137 | | - for ( var level = 0; level < mipmapCount; level ++ ) { |
| 145 | + for ( let level = 0; level < mipmapCount; level ++ ) { |
138 | 146 |
|
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 |
140 | 148 |
|
141 | 149 | dataOffset += 4; // size of the image + 4 for the imageSize field |
142 | 150 |
|
143 | | - for ( var face = 0; face < this.numberOfFaces; face ++ ) { |
| 151 | + for ( let face = 0; face < this.numberOfFaces; face ++ ) { |
144 | 152 |
|
145 | | - var byteArray = new Uint8Array( this.arrayBuffer, dataOffset, imageSize ); |
| 153 | + const byteArray = new Uint8Array( this.arrayBuffer, dataOffset, imageSize ); |
146 | 154 | mipmaps.push( { |
147 | 155 | 'data': byteArray, |
148 | 156 | 'width': width, |
|
160 | 168 |
|
161 | 169 | return mipmaps; |
162 | 170 |
|
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 | + } |
177 | 172 |
|
178 | | - }(); |
| 173 | + } |
179 | 174 |
|
180 | 175 | THREE.KTXLoader = KTXLoader; |
181 | 176 |
|
|
0 commit comments