2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2016-04-04 21:15:01 +00:00
|
|
|
* @copyright 2016 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-10-03 01:38:35 +00:00
|
|
|
* Phaser.LoaderParser parses data objects from Phaser.Loader that need more preparation before they can be inserted into the Cache.
|
2013-10-01 12:54:29 +00:00
|
|
|
*
|
2013-10-03 01:38:35 +00:00
|
|
|
* @class Phaser.LoaderParser
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-10-03 01:38:35 +00:00
|
|
|
Phaser.LoaderParser = {
|
2014-02-14 04:34:57 +00:00
|
|
|
|
2013-09-10 22:51:35 +00:00
|
|
|
/**
|
2015-07-16 00:17:03 +00:00
|
|
|
* Alias for xmlBitmapFont, for backwards compatibility.
|
2014-07-09 04:39:49 +00:00
|
|
|
*
|
2013-10-03 01:38:35 +00:00
|
|
|
* @method Phaser.LoaderParser.bitmapFont
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {object} xml - XML data you want to parse.
|
2015-07-16 00:17:03 +00:00
|
|
|
* @param {PIXI.BaseTexture} baseTexture - The BaseTexture this font uses.
|
2014-07-09 04:39:49 +00:00
|
|
|
* @param {number} [xSpacing=0] - Additional horizontal spacing between the characters.
|
|
|
|
* @param {number} [ySpacing=0] - Additional vertical spacing between the characters.
|
2015-07-16 00:17:03 +00:00
|
|
|
* @return {object} The parsed Bitmap Font data.
|
2013-09-10 22:51:35 +00:00
|
|
|
*/
|
2015-07-16 00:17:03 +00:00
|
|
|
bitmapFont: function (xml, baseTexture, xSpacing, ySpacing) {
|
|
|
|
|
|
|
|
return this.xmlBitmapFont(xml, baseTexture, xSpacing, ySpacing);
|
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
},
|
2013-09-10 22:51:35 +00:00
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
/**
|
|
|
|
* Parse a Bitmap Font from an XML file.
|
|
|
|
*
|
|
|
|
* @method Phaser.LoaderParser.xmlBitmapFont
|
|
|
|
* @param {object} xml - XML data you want to parse.
|
2015-07-16 00:17:03 +00:00
|
|
|
* @param {PIXI.BaseTexture} baseTexture - The BaseTexture this font uses.
|
2015-06-06 10:35:08 +00:00
|
|
|
* @param {number} [xSpacing=0] - Additional horizontal spacing between the characters.
|
|
|
|
* @param {number} [ySpacing=0] - Additional vertical spacing between the characters.
|
2016-09-27 00:59:11 +00:00
|
|
|
* @param {Phaser.Frame} [frame] - Optional Frame, if this font is embedded in a texture atlas.
|
2015-07-16 00:17:03 +00:00
|
|
|
* @return {object} The parsed Bitmap Font data.
|
2015-06-06 10:35:08 +00:00
|
|
|
*/
|
2016-09-27 00:59:11 +00:00
|
|
|
xmlBitmapFont: function (xml, baseTexture, xSpacing, ySpacing, frame) {
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2013-09-10 22:51:35 +00:00
|
|
|
var data = {};
|
2014-02-14 03:34:35 +00:00
|
|
|
var info = xml.getElementsByTagName('info')[0];
|
|
|
|
var common = xml.getElementsByTagName('common')[0];
|
|
|
|
|
|
|
|
data.font = info.getAttribute('face');
|
|
|
|
data.size = parseInt(info.getAttribute('size'), 10);
|
2014-02-14 04:34:57 +00:00
|
|
|
data.lineHeight = parseInt(common.getAttribute('lineHeight'), 10) + ySpacing;
|
2013-09-10 22:51:35 +00:00
|
|
|
data.chars = {};
|
|
|
|
|
2014-02-14 03:34:35 +00:00
|
|
|
var letters = xml.getElementsByTagName('char');
|
2013-09-10 22:51:35 +00:00
|
|
|
|
2016-09-27 00:59:11 +00:00
|
|
|
var x = (frame) ? frame.x : 0;
|
|
|
|
var y = (frame) ? frame.y : 0;
|
|
|
|
|
2013-09-10 22:51:35 +00:00
|
|
|
for (var i = 0; i < letters.length; i++)
|
|
|
|
{
|
2014-02-14 03:34:35 +00:00
|
|
|
var charCode = parseInt(letters[i].getAttribute('id'), 10);
|
2013-09-10 22:51:35 +00:00
|
|
|
|
|
|
|
data.chars[charCode] = {
|
2016-09-27 00:59:11 +00:00
|
|
|
x: x + parseInt(letters[i].getAttribute('x'), 10),
|
|
|
|
y: y + parseInt(letters[i].getAttribute('y'), 10),
|
2015-06-06 10:35:08 +00:00
|
|
|
width: parseInt(letters[i].getAttribute('width'), 10),
|
|
|
|
height: parseInt(letters[i].getAttribute('height'), 10),
|
2014-02-14 03:34:35 +00:00
|
|
|
xOffset: parseInt(letters[i].getAttribute('xoffset'), 10),
|
|
|
|
yOffset: parseInt(letters[i].getAttribute('yoffset'), 10),
|
2014-02-14 04:34:57 +00:00
|
|
|
xAdvance: parseInt(letters[i].getAttribute('xadvance'), 10) + xSpacing,
|
2015-06-06 10:35:08 +00:00
|
|
|
kerning: {}
|
2013-09-10 22:51:35 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-02-14 03:34:35 +00:00
|
|
|
var kernings = xml.getElementsByTagName('kerning');
|
2013-09-10 22:51:35 +00:00
|
|
|
|
|
|
|
for (i = 0; i < kernings.length; i++)
|
|
|
|
{
|
2014-02-14 03:34:35 +00:00
|
|
|
var first = parseInt(kernings[i].getAttribute('first'), 10);
|
|
|
|
var second = parseInt(kernings[i].getAttribute('second'), 10);
|
|
|
|
var amount = parseInt(kernings[i].getAttribute('amount'), 10);
|
2013-09-10 22:51:35 +00:00
|
|
|
|
|
|
|
data.chars[second].kerning[first] = amount;
|
|
|
|
}
|
|
|
|
|
2015-07-16 00:17:03 +00:00
|
|
|
return this.finalizeBitmapFont(baseTexture, data);
|
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
},
|
2013-09-10 22:51:35 +00:00
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
/**
|
|
|
|
* Parse a Bitmap Font from a JSON file.
|
|
|
|
*
|
|
|
|
* @method Phaser.LoaderParser.jsonBitmapFont
|
|
|
|
* @param {object} json - JSON data you want to parse.
|
2015-07-16 00:17:03 +00:00
|
|
|
* @param {PIXI.BaseTexture} baseTexture - The BaseTexture this font uses.
|
2015-06-06 10:35:08 +00:00
|
|
|
* @param {number} [xSpacing=0] - Additional horizontal spacing between the characters.
|
|
|
|
* @param {number} [ySpacing=0] - Additional vertical spacing between the characters.
|
2016-09-27 00:59:11 +00:00
|
|
|
* @param {Phaser.Frame} [frame] - Optional Frame, if this font is embedded in a texture atlas.
|
2015-07-16 00:17:03 +00:00
|
|
|
* @return {object} The parsed Bitmap Font data.
|
2015-06-06 10:35:08 +00:00
|
|
|
*/
|
2016-09-27 00:59:11 +00:00
|
|
|
jsonBitmapFont: function (json, baseTexture, xSpacing, ySpacing, frame) {
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
var data = {
|
2015-07-16 11:30:55 +00:00
|
|
|
font: json.font.info._face,
|
2015-06-06 10:35:08 +00:00
|
|
|
size: parseInt(json.font.info._size, 10),
|
|
|
|
lineHeight: parseInt(json.font.common._lineHeight, 10) + ySpacing,
|
|
|
|
chars: {}
|
|
|
|
};
|
|
|
|
|
2016-09-27 00:59:11 +00:00
|
|
|
var x = (frame) ? frame.x : 0;
|
|
|
|
var y = (frame) ? frame.y : 0;
|
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
json.font.chars["char"].forEach(
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
function parseChar(letter) {
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
var charCode = parseInt(letter._id, 10);
|
|
|
|
|
|
|
|
data.chars[charCode] = {
|
2016-09-27 00:59:11 +00:00
|
|
|
x: x + parseInt(letter._x, 10),
|
|
|
|
y: y + parseInt(letter._y, 10),
|
2015-06-06 10:35:08 +00:00
|
|
|
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: {}
|
|
|
|
};
|
|
|
|
}
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
);
|
|
|
|
|
2016-09-27 13:27:12 +00:00
|
|
|
if (json.font.kernings && json.font.kernings.kerning)
|
|
|
|
{
|
2015-07-16 11:30:55 +00:00
|
|
|
json.font.kernings.kerning.forEach(
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-07-16 11:30:55 +00:00
|
|
|
function parseKerning(kerning) {
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-07-16 11:30:55 +00:00
|
|
|
data.chars[kerning._second].kerning[kerning._first] = parseInt(kerning._amount, 10);
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-07-16 11:30:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
2015-06-06 10:35:08 +00:00
|
|
|
|
2015-07-16 00:17:03 +00:00
|
|
|
return this.finalizeBitmapFont(baseTexture, data);
|
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finalize Bitmap Font parsing.
|
|
|
|
*
|
|
|
|
* @method Phaser.LoaderParser.finalizeBitmapFont
|
|
|
|
* @private
|
2015-07-16 00:17:03 +00:00
|
|
|
* @param {PIXI.BaseTexture} baseTexture - The BaseTexture this font uses.
|
2015-06-06 10:35:08 +00:00
|
|
|
* @param {object} bitmapFontData - Pre-parsed bitmap font data.
|
2015-07-16 00:17:03 +00:00
|
|
|
* @return {object} The parsed Bitmap Font data.
|
|
|
|
*/
|
|
|
|
finalizeBitmapFont: function (baseTexture, bitmapFontData) {
|
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
Object.keys(bitmapFontData.chars).forEach(
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
function addTexture(charCode) {
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
var letter = bitmapFontData.chars[charCode];
|
|
|
|
|
2015-07-16 00:17:03 +00:00
|
|
|
letter.texture = new PIXI.Texture(baseTexture, new Phaser.Rectangle(letter.x, letter.y, letter.width, letter.height));
|
|
|
|
|
2015-06-06 10:54:14 +00:00
|
|
|
}
|
2015-07-16 00:17:03 +00:00
|
|
|
|
2015-06-06 10:35:08 +00:00
|
|
|
);
|
2013-09-10 22:51:35 +00:00
|
|
|
|
2015-07-16 00:17:03 +00:00
|
|
|
return bitmapFontData;
|
|
|
|
|
2016-07-28 22:42:46 +00:00
|
|
|
},
|
2016-07-29 01:33:31 +00:00
|
|
|
|
2016-07-27 23:47:05 +00:00
|
|
|
/**
|
|
|
|
* Extract PVR header from loaded binary
|
|
|
|
*
|
2016-09-20 00:45:02 +00:00
|
|
|
* @method Phaser.LoaderParser.pvr
|
|
|
|
* @param {ArrayBuffer} arrayBuffer
|
2016-07-27 23:47:05 +00:00
|
|
|
* @return {object} The parsed PVR file including texture data.
|
|
|
|
*/
|
2016-07-29 01:33:31 +00:00
|
|
|
pvr: function (arrayBuffer) {
|
2016-09-20 00:45:02 +00:00
|
|
|
|
2016-07-27 23:47:05 +00:00
|
|
|
// 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,
|
2016-07-29 03:02:34 +00:00
|
|
|
pixelFormat = (uintArray[3] << 32 | uintArray[2]),
|
2016-08-01 23:40:35 +00:00
|
|
|
compressionAlgorithm,
|
|
|
|
glExtensionFormat = 0;
|
2016-07-27 23:47:05 +00:00
|
|
|
|
|
|
|
if (uintArray[0] === 0x03525650 &&
|
|
|
|
[ // Validate WebGL Pixel Format
|
|
|
|
0, 1, 2, 3,
|
|
|
|
6, 7, 9, 11
|
|
|
|
].indexOf(pixelFormat) >= 0
|
|
|
|
) {
|
2016-07-29 03:02:34 +00:00
|
|
|
if (pixelFormat >= 0 && pixelFormat <= 3) {
|
|
|
|
compressionAlgorithm = 'PVRTC';
|
|
|
|
} else if (pixelFormat >= 7 && pixelFormat <= 11) {
|
|
|
|
compressionAlgorithm = 'S3TC';
|
2016-09-20 00:45:02 +00:00
|
|
|
} else if (pixelFormat === 6) {
|
2016-07-29 03:02:34 +00:00
|
|
|
compressionAlgorithm = 'ETC1';
|
|
|
|
}
|
2016-09-20 00:45:02 +00:00
|
|
|
|
2016-08-01 23:40:35 +00:00
|
|
|
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;
|
|
|
|
}
|
2016-09-20 00:45:02 +00:00
|
|
|
|
2016-07-27 23:47:05 +00:00
|
|
|
pvrHeader = {
|
2016-08-01 23:40:35 +00:00
|
|
|
complete: true,
|
2016-07-29 01:33:31 +00:00
|
|
|
fileFormat: 'PVR',
|
2016-07-29 03:02:34 +00:00
|
|
|
compressionAlgorithm: compressionAlgorithm,
|
2016-07-27 23:47:05 +00:00
|
|
|
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],
|
2016-08-01 23:40:35 +00:00
|
|
|
textureData: byteArray.subarray(52 + uintArray[12], byteArray.byteLength),
|
|
|
|
glExtensionFormat: glExtensionFormat
|
2016-07-27 23:47:05 +00:00
|
|
|
};
|
|
|
|
}
|
2016-09-20 00:45:02 +00:00
|
|
|
|
2016-07-27 23:47:05 +00:00
|
|
|
return pvrHeader;
|
2016-09-20 00:45:02 +00:00
|
|
|
|
2016-07-27 23:47:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract DDS header from loaded binary
|
|
|
|
*
|
2016-09-20 00:45:02 +00:00
|
|
|
* @method Phaser.LoaderParser.dds
|
|
|
|
* @param {ArrayBuffer} arrayBuffer
|
2016-07-27 23:47:05 +00:00
|
|
|
* @return {object} The parsed DDS file including texture data.
|
|
|
|
*/
|
2016-07-29 01:33:31 +00:00
|
|
|
dds: function (arrayBuffer) {
|
2016-09-20 00:45:02 +00:00
|
|
|
|
2016-07-27 23:47:05 +00:00
|
|
|
// 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 = {
|
2016-08-01 23:40:35 +00:00
|
|
|
complete: true,
|
2016-07-29 01:33:31 +00:00
|
|
|
fileFormat: 'DDS',
|
2016-07-29 03:02:34 +00:00
|
|
|
compressionAlgorithm: 'S3TC',
|
2016-07-27 23:47:05 +00:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
2016-09-20 00:45:02 +00:00
|
|
|
|
2016-07-27 23:47:05 +00:00
|
|
|
return ddsHeader;
|
2016-09-20 00:45:02 +00:00
|
|
|
|
2016-07-27 23:47:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract KTX header from loaded binary
|
|
|
|
*
|
2016-09-20 00:45:02 +00:00
|
|
|
* @method Phaser.LoaderParser.ktx
|
|
|
|
* @param {ArrayBuffer} arrayBuffer
|
2016-07-27 23:47:05 +00:00
|
|
|
* @return {object} The parsed KTX file including texture data.
|
|
|
|
*/
|
2016-07-29 01:33:31 +00:00
|
|
|
ktx: function (arrayBuffer) {
|
2016-09-20 00:45:02 +00:00
|
|
|
|
2016-07-27 23:47:05 +00:00
|
|
|
// 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,
|
2016-07-29 03:02:34 +00:00
|
|
|
imageSize = uintArray[imageSizeOffset],
|
|
|
|
glInternalFormat = uintArray[7],
|
|
|
|
compressionAlgorithm = 0;
|
|
|
|
|
2016-07-27 23:47:05 +00:00
|
|
|
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 &&
|
2016-07-29 01:33:31 +00:00
|
|
|
//Check if internal GL format is supported by WebGL
|
|
|
|
[
|
|
|
|
// ETC1
|
|
|
|
0x8D64,
|
|
|
|
// PVRTC
|
|
|
|
0x8C00, 0x8C01, 0x8C02, 0x8C03,
|
|
|
|
// DXTC | S3TC
|
|
|
|
0x83F0, 0x83F1, 0x83F2, 0x83F3
|
2016-07-29 03:02:34 +00:00
|
|
|
].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;
|
|
|
|
}
|
2016-09-20 00:45:02 +00:00
|
|
|
|
2016-07-27 23:47:05 +00:00
|
|
|
ktxHeader = {
|
2016-08-01 23:40:35 +00:00
|
|
|
complete: true,
|
2016-07-29 01:33:31 +00:00
|
|
|
fileFormat: 'KTX',
|
2016-07-29 03:02:34 +00:00
|
|
|
compressionAlgorithm: compressionAlgorithm,
|
2016-07-27 23:47:05 +00:00
|
|
|
endianness: uintArray[3],
|
|
|
|
glType: uintArray[4],
|
|
|
|
glTypeSize: uintArray[5],
|
|
|
|
glFormat: uintArray[6],
|
|
|
|
glInternalFormat: uintArray[7],
|
|
|
|
glBaseInternalFormat: uintArray[8],
|
2016-08-01 23:40:35 +00:00
|
|
|
width: uintArray[9],
|
|
|
|
height: uintArray[10],
|
2016-07-27 23:47:05 +00:00
|
|
|
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)
|
|
|
|
};
|
|
|
|
}
|
2016-09-20 00:45:02 +00:00
|
|
|
|
2016-07-27 23:47:05 +00:00
|
|
|
return ktxHeader;
|
2016-09-20 00:45:02 +00:00
|
|
|
|
2016-07-27 23:47:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract PKM header from loaded binary
|
|
|
|
*
|
2016-09-20 00:45:02 +00:00
|
|
|
* @method Phaser.LoaderParser.pkm
|
|
|
|
* @param {ArrayBuffer} arrayBuffer
|
2016-07-27 23:47:05 +00:00
|
|
|
* @return {object} The parsed PKM file including texture data.
|
|
|
|
*/
|
2016-07-29 01:33:31 +00:00
|
|
|
pkm: function (arrayBuffer) {
|
2016-09-20 00:45:02 +00:00
|
|
|
|
2016-07-27 23:47:05 +00:00
|
|
|
// 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) {
|
2016-09-20 00:45:02 +00:00
|
|
|
|
2016-07-27 23:47:05 +00:00
|
|
|
pkmHeader = {
|
2016-08-01 23:40:35 +00:00
|
|
|
complete: true,
|
2016-07-29 01:33:31 +00:00
|
|
|
fileFormat: 'PKM',
|
2016-07-29 03:02:34 +00:00
|
|
|
compressionAlgorithm: 'ETC1',
|
2016-07-27 23:47:05 +00:00
|
|
|
format: ((byteArray[6] << 8 | byteArray[7])) & 0xFFFF,
|
2016-08-01 23:40:35 +00:00
|
|
|
width: ((byteArray[8] << 8 | byteArray[9])) & 0xFFFF,
|
|
|
|
height: ((byteArray[10] << 8 | byteArray[11])) & 0xFFFF,
|
2016-07-27 23:47:05 +00:00
|
|
|
originalWidth: ((byteArray[12] << 8 | byteArray[13])) & 0xFFFF,
|
|
|
|
originalHeight: ((byteArray[14] << 8 | byteArray[15])) & 0xFFFF,
|
|
|
|
textureData: byteArray.subarray(16, byteArray.length)
|
|
|
|
};
|
|
|
|
}
|
2016-09-20 00:45:02 +00:00
|
|
|
|
2016-07-27 23:47:05 +00:00
|
|
|
return pkmHeader;
|
2016-09-20 00:45:02 +00:00
|
|
|
|
2016-07-27 23:47:05 +00:00
|
|
|
}
|
2016-09-20 00:45:02 +00:00
|
|
|
|
2014-03-10 06:59:32 +00:00
|
|
|
};
|