-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebGLFilters.js
More file actions
44 lines (40 loc) · 1.79 KB
/
WebGLFilters.js
File metadata and controls
44 lines (40 loc) · 1.79 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
// Thanks https://gamemechanicexplorer.com/#lightning-3
// Fragment shaders are small programs that run on the graphics card and alter
// the pixels of a texture. Every framework implements shaders differently but
// the concept is the same. This shader takes the lightning texture and alters
// the pixels so that it appears to be glowing. Shader programming itself is
// beyond the scope of this tutorial.
//
// There are a ton of good resources out there to learn it. Odds are that your
// framework already includes many of the most popular shaders out of the box.
//
// This is an OpenGL/WebGL feature. Because it runs in your web browser
// you need a browser that support WebGL for this to work.
Phaser.Filter.Glow = function (game) {
Phaser.Filter.call(this, game);
this.fragmentSrc = [
'precision lowp float;',
'varying vec2 vTextureCoord;',
'varying vec4 vColor;',
'uniform sampler2D uSampler;',
'void main() {',
'vec4 sum = vec4(0);',
'vec2 texcoord = vTextureCoord;',
'for(int xx = -4; xx <= 4; xx++) {',
'for(int yy = -3; yy <= 3; yy++) {',
'float dist = sqrt(float(xx*xx) + float(yy*yy));',
'float factor = 0.0;',
'if (dist == 0.0) {',
'factor = 2.0;',
'} else {',
'factor = 2.0/abs(float(dist));',
'}',
'sum += texture2D(uSampler, texcoord + vec2(xx, yy) * 0.002) * factor;',
'}',
'}',
'gl_FragColor = sum * 0.025 + texture2D(uSampler, texcoord);',
'}'
];
};
Phaser.Filter.Glow.prototype = Object.create(Phaser.Filter.prototype);
Phaser.Filter.Glow.prototype.constructor = Phaser.Filter.Glow;