Skip to content

Commit c6abf9d

Browse files
committed
Add support for loading compressed textures
1 parent 04e7356 commit c6abf9d

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ View sample usage in this [Phaser ES6 Boilerplate](https://github.com/goldfire/p
1414
* Integrated with Webpack for automatic cache-busting.
1515
* Supports all filetypes.
1616
* Supports asset postfix for retina support ('@2x', '@3x', etc).
17+
* Supports automatic loading of compressed textures (PVRTC, S3TC, ETC1).
1718

1819
## Install
1920

@@ -98,6 +99,16 @@ The font loader uses [Web Font Loader](https://github.com/typekit/webfontloader)
9899

99100
All sprite/atlas files are loaded as JSON hashes (which can be output using [TexturePacker](https://www.codeandweb.com/texturepacker), [Shoebox](http://renderhjs.net/shoebox/) and others). All you have to specify in the manifest is the image filename, but you'll also need to include the JSON hash file alongside it, which will automatically get loaded and used.
100101

102+
## Loading Compressed Textures (Phaser-CE 2.7.7+)
103+
104+
[Compressed textures](https://phaser.io/tutorials/advanced-rendering-tutorial/part6) can be great to lower memory usage, especially on mobile devices; however, it isn't necessarily straight-forward how to load these (especially for atlas files). This loader makes it as simple as putting the files in your directory, so long as they match the naming conventions:
105+
106+
#### PVRTC `texture.pvrtc.pvr` & `texture.pvrtc.json`
107+
#### S3TC `texture.s3tc.pvr` & `texture.s3tc.json`
108+
#### ETC1 `texture.etc1.pkm` & `texture.etc1.json`
109+
110+
Simply include any or all of these files alongside the `PNG` equivalent and if one of them is compatible with the current browser/hardware it will be loaded and used (falling back to the `PNG` file).
111+
101112
## Directory Structure
102113

103114
Specify the base directory in your Webpack config:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "phaser-webpack-loader",
3-
"version": "0.2.1",
3+
"version": "1.0.0",
44
"author": "GoldFire Studios, Inc. (http://goldfirestudios.com)",
55
"description": "Asset loader for Phaser + Webpack.",
66
"author": "James Simpson <[email protected]> (http://goldfirestudios.com)",

src/index.js

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default class WebpackLoader extends Phaser.Plugin {
4242
return Promise.all([
4343
this._loadAssets(),
4444
this._loadFonts(),
45-
]);
45+
]).then(this._addSprites.bind(this));
4646
}
4747

4848
/**
@@ -82,13 +82,33 @@ export default class WebpackLoader extends Phaser.Plugin {
8282
});
8383
}
8484

85+
/**
86+
* Create the texture atlases once the image data has loaded.
87+
* This is required in order to load compressed textures as an atlas.
88+
*/
89+
_addSprites() {
90+
this.assets.sprites.forEach((asset) => {
91+
// Get the image/data for the sprite atlas.
92+
const dir = 'sprites/';
93+
const name = asset.split('.')[0];
94+
const image = this.game.cache.getItem(name, Phaser.Cache.IMAGE);
95+
const compression = image.data.compressionAlgorithm;
96+
const format = compression ? `.${compression.toLowerCase()}` : '';
97+
const data = require(`assets/${dir}${name}${this.postfix}${format}.json`);
98+
99+
// Add the sprite atlas to the cache.
100+
this.game.cache.addTextureAtlas(name, null, image.data, data, Phaser.Loader.TEXTURE_ATLAS_JSON_HASH);
101+
});
102+
}
103+
85104
/**
86105
* Load an image.
87106
* @param {String} name Name of the file.
88107
* @param {String} ext File extension.
89108
*/
90109
_loadImage(name, ext) {
91-
const file = require(`assets/images/${name}${this.postfix}.${ext}`);
110+
const dir = 'images/';
111+
const file = require(`assets/${dir}${name}${this.postfix}.${ext}`);
92112
this.game.load.image(name, file);
93113
}
94114

@@ -98,9 +118,29 @@ export default class WebpackLoader extends Phaser.Plugin {
98118
* @param {String} ext File extension.
99119
*/
100120
_loadSprite(name, ext) {
101-
const file = require(`assets/sprites/${name}${this.postfix}.${ext}`);
102-
const data = require(`assets/sprites/${name}${this.postfix}.json`);
103-
this.game.load.atlasJSONHash(name, file, null, data);
121+
const dir = 'sprites/';
122+
123+
// Determine which formats are available.
124+
const formats = {};
125+
126+
try {
127+
formats.truecolor = require(`assets/${dir}${name}${this.postfix}.${ext}`);
128+
} catch (e) {}
129+
130+
try {
131+
formats.pvrtc = require(`assets/${dir}${name}${this.postfix}.pvrtc.pvr`);
132+
} catch (e) {}
133+
134+
try {
135+
formats.s3tc = require(`assets/${dir}${name}${this.postfix}.s3tc.pvr`);
136+
} catch (e) {}
137+
138+
try {
139+
formats.etc1 = require(`assets/${dir}${name}${this.postfix}.etc1.pkm`);
140+
} catch (e) {}
141+
142+
// Load the format that works on this platform.
143+
this.game.load.image(name, formats);
104144
}
105145

106146
/**
@@ -109,7 +149,8 @@ export default class WebpackLoader extends Phaser.Plugin {
109149
* @param {String} ext File extension.
110150
*/
111151
_loadAudio(name, ext) {
112-
const file = require(`assets/audio/${name}.${ext}`);
152+
const dir = 'audio/';
153+
const file = require(`assets/${dir}${name}.${ext}`);
113154
this.game.load.audio(name, file);
114155
}
115156

@@ -119,8 +160,9 @@ export default class WebpackLoader extends Phaser.Plugin {
119160
* @param {String} ext File extension.
120161
*/
121162
_loadBitmapFont(name, ext) {
122-
const file = require(`assets/fonts/${name}${this.postfix}.${ext}`);
123-
const data = require(`assets/fonts/${name}${this.postfix}.xml`);
163+
const dir = 'fonts/';
164+
const file = require(`assets/${dir}${name}${this.postfix}.${ext}`);
165+
const data = require(`assets/${dir}${name}${this.postfix}.xml`);
124166
this.game.load.bitmapFont(name, file, data);
125167
}
126168
}

0 commit comments

Comments
 (0)