forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoaderParser.js
More file actions
515 lines (450 loc) · 19 KB
/
Copy pathLoaderParser.js
File metadata and controls
515 lines (450 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* Phaser.LoaderParser parses data objects from Phaser.Loader that need more preparation before they can be inserted into the Cache.
*
* @class Phaser.LoaderParser
*/
Phaser.LoaderParser = {
/**
* Alias for xmlBitmapFont, for backwards compatibility.
*
* @method Phaser.LoaderParser.bitmapFont
* @param {object} xml - XML data you want to parse.
* @param {PIXI.BaseTexture} baseTexture - The BaseTexture this font uses.
* @param {number} [xSpacing=0] - Additional horizontal spacing between the characters.
* @param {number} [ySpacing=0] - Additional vertical spacing between the characters.
* @return {object} The parsed Bitmap Font data.
*/
bitmapFont: function (xml, baseTexture, xSpacing, ySpacing) {
return this.xmlBitmapFont(xml, baseTexture, xSpacing, ySpacing);
},
/**
* Parse a Bitmap Font from an XML file.
*
* @method Phaser.LoaderParser.xmlBitmapFont
* @param {object} xml - XML data you want to parse.
* @param {PIXI.BaseTexture} baseTexture - The BaseTexture this font uses.
* @param {number} [xSpacing=0] - Additional horizontal spacing between the characters.
* @param {number} [ySpacing=0] - Additional vertical spacing between the characters.
* @param {Phaser.Frame} [frame] - Optional Frame, if this font is embedded in a texture atlas.
* @return {object} The parsed Bitmap Font data.
*/
xmlBitmapFont: function (xml, baseTexture, xSpacing, ySpacing, frame) {
var data = {};
var info = xml.getElementsByTagName('info')[0];
var common = xml.getElementsByTagName('common')[0];
data.font = info.getAttribute('face');
data.size = parseInt(info.getAttribute('size'), 10);
data.lineHeight = parseInt(common.getAttribute('lineHeight'), 10) + ySpacing;
data.chars = {};
var letters = xml.getElementsByTagName('char');
var x = (frame) ? frame.x : 0;
var y = (frame) ? frame.y : 0;
for (var i = 0; i < letters.length; i++)
{
var charCode = parseInt(letters[i].getAttribute('id'), 10);
data.chars[charCode] = {
x: x + parseInt(letters[i].getAttribute('x'), 10),
y: y + parseInt(letters[i].getAttribute('y'), 10),
width: parseInt(letters[i].getAttribute('width'), 10),
height: parseInt(letters[i].getAttribute('height'), 10),
xOffset: parseInt(letters[i].getAttribute('xoffset'), 10),
yOffset: parseInt(letters[i].getAttribute('yoffset'), 10),
xAdvance: parseInt(letters[i].getAttribute('xadvance'), 10) + xSpacing,
kerning: {}
};
}
var kernings = xml.getElementsByTagName('kerning');
for (i = 0; i < kernings.length; i++)
{
var first = parseInt(kernings[i].getAttribute('first'), 10);
var second = parseInt(kernings[i].getAttribute('second'), 10);
var amount = parseInt(kernings[i].getAttribute('amount'), 10);
data.chars[second].kerning[first] = amount;
}
return this.finalizeBitmapFont(baseTexture, data);
},
/**
* Parse a Bitmap Font from a JSON file.
*
* @method Phaser.LoaderParser.jsonBitmapFont
* @param {object} json - JSON data you want to parse.
* @param {PIXI.BaseTexture} baseTexture - The BaseTexture this font uses.
* @param {number} [xSpacing=0] - Additional horizontal spacing between the characters.
* @param {number} [ySpacing=0] - Additional vertical spacing between the characters.
* @param {Phaser.Frame} [frame] - Optional Frame, if this font is embedded in a texture atlas.
* @return {object} The parsed Bitmap Font data.
*/
jsonBitmapFont: function (json, baseTexture, xSpacing, ySpacing, frame) {
var data = {
font: json.font.info._face,
size: parseInt(json.font.info._size, 10),
lineHeight: parseInt(json.font.common._lineHeight, 10) + ySpacing,
chars: {}
};
var x = (frame) ? frame.x : 0;
var y = (frame) ? frame.y : 0;
json.font.chars["char"].forEach(
function parseChar(letter) {
var charCode = parseInt(letter._id, 10);
data.chars[charCode] = {
x: x + parseInt(letter._x, 10),
y: y + parseInt(letter._y, 10),
width: parseInt(letter._width, 10),
height: parseInt(letter._height, 10),
xOffset: parseInt(letter._xoffset, 10),
yOffset: parseInt(letter._yoffset, 10),
xAdvance: parseInt(letter._xadvance, 10) + xSpacing,
kerning: {}
};
}
);
if (json.font.kernings && json.font.kernings.kerning)
{
json.font.kernings.kerning.forEach(
function parseKerning(kerning) {
data.chars[kerning._second].kerning[kerning._first] = parseInt(kerning._amount, 10);
}
);
}
return this.finalizeBitmapFont(baseTexture, data);
},
/**
* Finalize Bitmap Font parsing.
*
* @method Phaser.LoaderParser.finalizeBitmapFont
* @private
* @param {PIXI.BaseTexture} baseTexture - The BaseTexture this font uses.
* @param {object} bitmapFontData - Pre-parsed bitmap font data.
* @return {object} The parsed Bitmap Font data.
*/
finalizeBitmapFont: function (baseTexture, bitmapFontData) {
Object.keys(bitmapFontData.chars).forEach(
function addTexture(charCode) {
var letter = bitmapFontData.chars[charCode];
letter.texture = new PIXI.Texture(baseTexture, new Phaser.Rectangle(letter.x, letter.y, letter.width, letter.height));
}
);
return bitmapFontData;
},
/**
* Extract PVR header from loaded binary
*
* @method Phaser.LoaderParser.pvr
* @param {ArrayBuffer} arrayBuffer
* @return {object} The parsed PVR file including texture data.
*/
pvr: function (arrayBuffer) {
// Reference: http://cdn.imgtec.com/sdk-documentation/PVR+File+Format.Specification.pdf
// PVR 3 header structure
// ---------------------------------------
// address: 0, size: 4 bytes version
// address: 4, size: 4 bytes flags
// address: 8, size: 8 bytes pixel format
// address: 16, size: 4 bytes color space
// address: 20, size: 4 bytes channel type
// address: 24, size: 4 bytes height
// address: 28, size: 4 bytes width
// address: 32, size: 4 bytes depth
// address: 36, size: 4 bytes number of surfaces
// address: 40, size: 4 bytes number of faces
// address: 44, size: 4 bytes number of mipmaps
// address: 48, size: 4 bytes meta data size
// ---------------------------------------
var uintArray = new Uint32Array(arrayBuffer.slice(0, 52)),
byteArray = new Uint8Array(arrayBuffer),
pvrHeader = null,
pixelFormat = (uintArray[3] << 32 | uintArray[2]),
compressionAlgorithm,
glExtensionFormat = 0;
if (uintArray[0] === 0x03525650 &&
[ // Validate WebGL Pixel Format
0, 1, 2, 3,
6, 7, 9, 11
].indexOf(pixelFormat) >= 0
) {
if (pixelFormat >= 0 && pixelFormat <= 3) {
compressionAlgorithm = 'PVRTC';
} else if (pixelFormat >= 7 && pixelFormat <= 11) {
compressionAlgorithm = 'S3TC';
} else if (pixelFormat === 6) {
compressionAlgorithm = 'ETC1';
}
switch (pixelFormat) {
case 0:
glExtensionFormat = 0x8C01;
break;
case 1:
glExtensionFormat = 0x8C03;
break;
case 2:
glExtensionFormat = 0x8C00;
break;
case 3:
glExtensionFormat = 0x8C02;
break;
case 6:
glExtensionFormat = 0x8D64;
break;
case 7:
glExtensionFormat = 0x83F1;
break;
case 9:
glExtensionFormat = 0x83F2;
break;
case 11:
glExtensionFormat = 0x83F3;
break;
default:
glExtensionFormat = -1;
}
pvrHeader = {
complete: true,
fileFormat: 'PVR',
compressionAlgorithm: compressionAlgorithm,
flags: uintArray[1],
pixelFormat: pixelFormat,
colorSpace: uintArray[4],
channelType: uintArray[5],
height: uintArray[6],
width: uintArray[7],
depth: uintArray[8],
numberOfSurfaces: uintArray[9],
numberOfFaces: uintArray[10],
numberOfMipmaps: uintArray[11],
metaDataSize: uintArray[12],
textureData: byteArray.subarray(52 + uintArray[12], byteArray.byteLength),
glExtensionFormat: glExtensionFormat
};
}
return pvrHeader;
},
/**
* Extract DDS header from loaded binary
*
* @method Phaser.LoaderParser.dds
* @param {ArrayBuffer} arrayBuffer
* @return {object} The parsed DDS file including texture data.
*/
dds: function (arrayBuffer) {
// Reference at: https://msdn.microsoft.com/en-us/library/windows/desktop/bb943982(v=vs.85).aspx
// DDS header structure
// ---------------------------------------
// address: 0, size: 4 bytes Identifier 'DDS '
// address: 4, size: 4 bytes size
// address: 8, size: 4 bytes flags
// address: 12, size: 4 bytes height
// address: 16, size: 4 bytes width
// address: 20, size: 4 bytes pitch or linear size
// address: 24, size: 4 bytes depth
// address: 28, size: 4 bytes mipmap count
// address: 32, size: 44 bytes reserved space 1
// address: 76, size: 4 pixel format size
// address: 80, size: 4 pixel format flag
// address: 84, size: 4 pixel format four CC
// address: 88, size: 4 pixel format bit count
// address: 92, size: 4 pixel format R bit mask
// address: 96, size: 4 pixel format G bit mask
// address: 100, size: 4 pixel format B bit mask
// address: 104, size: 4 pixel format A bit mask
// address: 108, size: 4 caps 1
// address: 112, size: 4 caps 2
// address: 116, size: 4 caps 3
// address: 120, size: 4 caps 4
// address: 124, size: 4 reserved 2
// -- DXT10 extension
// address: 130, size: 4 DXGI Format
// address: 134, size: 4 resource dimension
// address: 138, size: 4 misc flag
// address: 142, size: 4 array size
// address: 146, size: 4 misc flag 2
// ---------------------------------------
var byteArray = new Uint8Array(arrayBuffer),
uintArray = new Uint32Array(arrayBuffer),
ddsHeader = null;
if (byteArray[0] === 0x44 &&
byteArray[1] === 0x44 &&
byteArray[2] === 0x53 &&
byteArray[3] === 0x20) {
ddsHeader = {
complete: true,
fileFormat: 'DDS',
compressionAlgorithm: 'S3TC',
size: uintArray[1],
flags: uintArray[2],
height: uintArray[3],
width: uintArray[4],
pitch: uintArray[5],
depth: uintArray[6],
mipmapCount: uintArray[7],
formatSize: uintArray[19],
formatFlag: uintArray[19],
formatFourCC: [
String.fromCharCode(byteArray[84]),
String.fromCharCode(byteArray[85]),
String.fromCharCode(byteArray[86]),
String.fromCharCode(byteArray[87])
].join(''),
formatBitCount: uintArray[21],
formatRBitMask: uintArray[22],
formatGBitMask: uintArray[23],
formatBBitMask: uintArray[24],
formatABitMask: uintArray[25],
caps1: uintArray[26],
caps2: uintArray[27],
caps3: uintArray[28],
caps4: uintArray[29],
reserved2: uintArray[30],
DXGIFormat: null,
resourceDimension: null,
miscFlag: null,
arraySize: null,
textureData: byteArray.subarray(uintArray[1] + 4, byteArray.byteLength)
};
if (ddsHeader.formatFourCC === 'DX10') {
ddsHeader.DXGIFormat = uintArray[31];
ddsHeader.resourceDimension = uintArray[32];
ddsHeader.miscFlag = uintArray[33];
ddsHeader.arraySize = uintArray[34];
ddsHeader.miscFlag = uintArray[35];
}
}
return ddsHeader;
},
/**
* Extract KTX header from loaded binary
*
* @method Phaser.LoaderParser.ktx
* @param {ArrayBuffer} arrayBuffer
* @return {object} The parsed KTX file including texture data.
*/
ktx: function (arrayBuffer) {
// Reference: https://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/
// KTX header structure
// ---------------------------------------
// address: 0, size 12 bytes: Identifier '«KTX 11»\r\n\x1A\n'
// address: 12, size 4 bytes: endianness
// address: 16, size 4 bytes: GL type
// address: 20, size 4 bytes: GL type size
// address: 24, size 4 bytes: GL format
// address: 28, size 4 bytes: GL internal format
// address: 32, size 4 bytes: GL base internal format
// address: 36, size 4 bytes: pixel width
// address: 40, size 4 bytes: pixel height
// address: 44, size 4 bytes: pixel depth
// address: 48, size 4 bytes: number of array elements
// address: 52, size 4 bytes: number of faces
// address: 56, size 4 bytes: number of mipmap levels
// address: 60, size 4 bytes: bytes of key value data
// address: 64, size 4 bytes: key and value bytes size
// address: X, size 1 byte : key and value
// address: X, size 1 byte : value padding
// address: X, size 4 byte : image size
// ---------------------------------------
var byteArray = new Uint8Array(arrayBuffer),
uintArray = new Uint32Array(arrayBuffer),
ktxHeader = null,
imageSizeOffset = 16 + (uintArray[15] / 4) | 0,
imageSize = uintArray[imageSizeOffset],
glInternalFormat = uintArray[7],
compressionAlgorithm = 0;
if (byteArray[0] === 0xAB && byteArray[1] === 0x4B &&
byteArray[2] === 0x54 && byteArray[3] === 0x58 &&
byteArray[4] === 0x20 && byteArray[5] === 0x31 &&
byteArray[6] === 0x31 && byteArray[7] === 0xBB &&
byteArray[8] === 0x0D && byteArray[9] === 0x0A &&
byteArray[10] === 0x1A && byteArray[11] === 0x0A &&
//Check if internal GL format is supported by WebGL
[
// ETC1
0x8D64,
// PVRTC
0x8C00, 0x8C01, 0x8C02, 0x8C03,
// DXTC | S3TC
0x83F0, 0x83F1, 0x83F2, 0x83F3
].indexOf(glInternalFormat) >= 0) {
switch (glInternalFormat) {
case 0x8D64:
compressionAlgorithm = 'ETC1';
break;
case 0x8C00:
case 0x8C01:
case 0x8C02:
case 0x8C03:
compressionAlgorithm = 'PVRTC';
break;
case 0x83F0:
case 0x83F1:
case 0x83F2:
case 0x83F3:
compressionAlgorithm = 'S3TC';
break;
}
ktxHeader = {
complete: true,
fileFormat: 'KTX',
compressionAlgorithm: compressionAlgorithm,
endianness: uintArray[3],
glType: uintArray[4],
glTypeSize: uintArray[5],
glFormat: uintArray[6],
glInternalFormat: uintArray[7],
glBaseInternalFormat: uintArray[8],
width: uintArray[9],
height: uintArray[10],
pixelDepth: uintArray[11],
numberOfArrayElements: uintArray[12],
numberOfFaces: uintArray[13],
numberOfMipmapLevels: uintArray[14],
bytesOfKeyValueData: uintArray[15],
keyAndValueByteSize: uintArray[16],
imageSize: imageSize,
textureData: byteArray.subarray((imageSizeOffset + 1) * 4, imageSize + 100)
};
}
return ktxHeader;
},
/**
* Extract PKM header from loaded binary
*
* @method Phaser.LoaderParser.pkm
* @param {ArrayBuffer} arrayBuffer
* @return {object} The parsed PKM file including texture data.
*/
pkm: function (arrayBuffer) {
// PKM header structure
// ---------------------------------------
// address: 0, size 4 bytes: for 'PKM '
// address: 4, size 2 bytes: for version
// address: 6, size 2 bytes: for type
// address: 8, size 2 bytes: for extended width
// address: 10, size 2 bytes: for extended height
// address: 12, size 2 bytes: for original width
// address: 14, size 2 bytes: for original height
// address: 16, texture data
// ---------------------------------------
var byteArray = new Uint8Array(arrayBuffer),
pkmHeader = null;
if (byteArray[0] === 0x50 &&
byteArray[1] === 0x4B &&
byteArray[2] === 0x4D &&
byteArray[3] === 0x20) {
pkmHeader = {
complete: true,
fileFormat: 'PKM',
compressionAlgorithm: 'ETC1',
format: ((byteArray[6] << 8 | byteArray[7])) & 0xFFFF,
width: ((byteArray[8] << 8 | byteArray[9])) & 0xFFFF,
height: ((byteArray[10] << 8 | byteArray[11])) & 0xFFFF,
originalWidth: ((byteArray[12] << 8 | byteArray[13])) & 0xFFFF,
originalHeight: ((byteArray[14] << 8 | byteArray[15])) & 0xFFFF,
textureData: byteArray.subarray(16, byteArray.length)
};
}
return pkmHeader;
}
};