Skip to content

Commit 57918bb

Browse files
committed
Removed the need for raw-loader in webpack. Shaders now build to standard JS files. Removed fs requirement.
1 parent c89728d commit 57918bb

30 files changed

Lines changed: 325 additions & 48 deletions

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ The Loader has been given an overhaul to improve its performance and extensibili
5252

5353
### Updates
5454

55-
* If you're using Webpack with Phaser you'll need to update your config to match our new one.
56-
* We've swapped use of the Webpack DefinePlugin so instead of setting a global flag for the compilation of the Canvas and WebGL renderers, we now use a typeof check instead. This means you should now be able to ingest the Phaser source more easily outside of Webpack without having to define any global vars first (thanks @tgrajewski)
57-
* Under Webpack we still use the raw-loader to import our shader source, but outside of Webpack we now use `require.extensions` to load the shader source via fs. This should allow you to bundle Phaser with packages other than Webpack more easily (thanks @tgrajewski)
55+
* If you're using Webpack with Phaser you'll need to update your config to match our new one. The two changes are: We've removed the need for `raw-loader` and we've changed the syntax of the DefinePlugin calls:
56+
* We've swapped use of the Webpack DefinePlugin so instead of setting a global flag for the compilation of the Canvas and WebGL renderers, we use a typeof check instead. This means you should now be able to ingest the Phaser source more easily outside of Webpack without having to define any global vars (thanks @tgrajewski)
57+
* Under Webpack we still no longer use `raw-loader` to import our shader source. Instead it's compiled to plain JS files during our in-house workflow. This should allow you to bundle Phaser with packages other than Webpack more easily.
5858
* The Texture Manager will now emit an `addtexture` event whenever you add a new texture to it, which includes when you load image files from the Loader (as it automatically populates the Texture Manager). Once you receive an `addtexture` event you know the image is loaded and the texture is safe to be applied to a Game Object.
5959
* BitmapMask and GeometryMask both have new `destroy` methods which clear their references, freeing them for gc.
6060
* CanvasPool has a new argument `selfParent` which allows the canvas itself to be the parent key, used for later removal.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"lint": "eslint --config .eslintrc.json \"src/**/*.js\"",
2222
"lintfix": "eslint --config .eslintrc.json \"src/**/*.js\" --fix",
2323
"sloc": "node-sloc \"./src\" --include-extensions \"js\"",
24+
"bundleshaders": "node scripts/bundle-shaders.js",
2425
"postinstall": "node scripts/support.js"
2526
},
2627
"keywords": [

scripts/bundle-shaders.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
let fs = require('fs-extra');
2+
3+
/*
4+
BitmapMask.frag
5+
BitmapMask.vert
6+
DeferredDiffuse.frag
7+
DeferredDiffuse.vert
8+
FlatTint.frag
9+
FlatTint.vert
10+
ForwardDiffuse.frag
11+
GBuffer.frag
12+
TextureTint.frag
13+
TextureTint.vert
14+
*/
15+
16+
let srcdir = './src/renderer/webgl/shaders/src/';
17+
let destdir = './src/renderer/webgl/shaders/';
18+
19+
let files = fs.readdirSync(srcdir);
20+
21+
files.forEach(function (file) {
22+
23+
let shaderSource = fs.readFileSync(srcdir + file, 'utf8');
24+
let type = file.substr(-4);
25+
let shaderFilename = file.substr(0, file.lastIndexOf('.')) + '-' + type + '.js';
26+
27+
let outputSource = 'module.exports = [\n';
28+
29+
let lines = shaderSource.split('\n');
30+
31+
for (var i = 0; i < lines.length; i++)
32+
{
33+
let line = lines[i].trimRight();
34+
35+
if (i < lines.length - 1)
36+
{
37+
outputSource = outputSource.concat(" '" + line + "',\n");
38+
}
39+
else
40+
{
41+
outputSource = outputSource.concat(" '" + line + "'\n");
42+
}
43+
}
44+
45+
outputSource = outputSource.concat('].join(\'\\n\');\n');
46+
47+
fs.writeFile(destdir + shaderFilename, outputSource, function (error) {
48+
49+
if (error)
50+
{
51+
throw error;
52+
}
53+
else
54+
{
55+
console.log('Saved', shaderFilename);
56+
}
57+
58+
});
59+
60+
});

src/phaser.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,6 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7-
// For file loading of shader source outside of Webpack
8-
// See: https://github.com/photonstorm/phaser/issues/3598
9-
10-
/* eslint-disable */
11-
if (typeof SHADER_REQUIRE)
12-
{
13-
var fs = require('fs');
14-
15-
require.extensions['.frag'] = function (module, filename) {
16-
module.exports = fs.readFileSync(filename, 'utf8');
17-
};
18-
19-
require.extensions['.vert'] = function (module, filename) {
20-
module.exports = fs.readFileSync(filename, 'utf8');
21-
};
22-
}
23-
/* eslint-enable */
24-
257
require('./polyfills');
268

279
var CONST = require('./const');

src/renderer/webgl/pipelines/BitmapMaskPipeline.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
*/
77

88
var Class = require('../../../utils/Class');
9-
var ShaderSourceFS = require('../shaders/BitmapMask.frag');
10-
var ShaderSourceVS = require('../shaders/BitmapMask.vert');
9+
var ShaderSourceFS = require('../shaders/BitmapMask-frag.js');
10+
var ShaderSourceVS = require('../shaders/BitmapMask-vert.js');
1111
var WebGLPipeline = require('../WebGLPipeline');
1212

1313
/**

src/renderer/webgl/pipelines/FlatTintPipeline.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ var Class = require('../../../utils/Class');
99
var Commands = require('../../../gameobjects/graphics/Commands');
1010
var Earcut = require('../../../geom/polygon/Earcut');
1111
var ModelViewProjection = require('./components/ModelViewProjection');
12-
var ShaderSourceFS = require('../shaders/FlatTint.frag');
13-
var ShaderSourceVS = require('../shaders/FlatTint.vert');
12+
var ShaderSourceFS = require('../shaders/FlatTint-frag.js');
13+
var ShaderSourceVS = require('../shaders/FlatTint-vert.js');
1414
var Utils = require('../Utils');
1515
var WebGLPipeline = require('../WebGLPipeline');
1616

src/renderer/webgl/pipelines/ForwardDiffuseLightPipeline.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
var Class = require('../../../utils/Class');
9-
var ShaderSourceFS = require('../shaders/ForwardDiffuse.frag');
9+
var ShaderSourceFS = require('../shaders/ForwardDiffuse-frag.js');
1010
var TextureTintPipeline = require('./TextureTintPipeline');
1111

1212
var LIGHT_COUNT = 10;

src/renderer/webgl/pipelines/TextureTintPipeline.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
var Class = require('../../../utils/Class');
99
var ModelViewProjection = require('./components/ModelViewProjection');
10-
var ShaderSourceFS = require('../shaders/TextureTint.frag');
11-
var ShaderSourceVS = require('../shaders/TextureTint.vert');
10+
var ShaderSourceFS = require('../shaders/TextureTint-frag.js');
11+
var ShaderSourceVS = require('../shaders/TextureTint-vert.js');
1212
var Utils = require('../Utils');
1313
var WebGLPipeline = require('../WebGLPipeline');
1414

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module.exports = [
2+
'#define SHADER_NAME PHASER_BITMAP_MASK_FS',
3+
'',
4+
'precision mediump float;',
5+
'',
6+
'uniform vec2 uResolution;',
7+
'uniform sampler2D uMainSampler;',
8+
'uniform sampler2D uMaskSampler;',
9+
'uniform bool uInvertMaskAlpha;',
10+
'',
11+
'void main()',
12+
'{',
13+
' vec2 uv = gl_FragCoord.xy / uResolution;',
14+
' vec4 mainColor = texture2D(uMainSampler, uv);',
15+
' vec4 maskColor = texture2D(uMaskSampler, uv);',
16+
' float alpha = mainColor.a;',
17+
'',
18+
' if (!uInvertMaskAlpha)',
19+
' {',
20+
' alpha *= (maskColor.a);',
21+
' }',
22+
' else',
23+
' {',
24+
' alpha *= (1.0 - maskColor.a);',
25+
' }',
26+
'',
27+
' gl_FragColor = vec4(mainColor.rgb * alpha, alpha);',
28+
'}',
29+
''
30+
].join('\n');
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = [
2+
'#define SHADER_NAME PHASER_BITMAP_MASK_VS',
3+
'',
4+
'precision mediump float;',
5+
'',
6+
'attribute vec2 inPosition;',
7+
'',
8+
'void main()',
9+
'{',
10+
' gl_Position = vec4(inPosition, 0.0, 1.0);',
11+
'}',
12+
''
13+
].join('\n');

0 commit comments

Comments
 (0)