Skip to content

Commit 378ffc7

Browse files
committed
Caustics filter
1 parent b2fc6c4 commit 378ffc7

5 files changed

Lines changed: 163 additions & 1 deletion

File tree

examples/_site/examples.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@
194194
"file": "lightbeams.js",
195195
"title": "lightbeams"
196196
},
197+
{
198+
"file": "marble.js",
199+
"title": "marble"
200+
},
197201
{
198202
"file": "plasma.js",
199203
"title": "plasma"

examples/filters/marble.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.WEBGL, 'phaser-example', { preload: preload, create: create, update: update });
3+
4+
var background;
5+
var filter;
6+
7+
function preload() {
8+
9+
game.load.image('phaser', 'assets/sprites/phaser2.png');
10+
game.load.script('filter', '../filters/Marble.js');
11+
12+
}
13+
14+
function create() {
15+
16+
var logo = game.add.sprite(game.world.centerX, game.world.centerY, 'phaser');
17+
logo.anchor.setTo(0.5, 0.5);
18+
19+
background = game.add.sprite(0, 0);
20+
background.width = 800;
21+
background.height = 600;
22+
23+
filter = game.add.filter('Marble', 800, 600);
24+
//filter.alpha = 0.0;
25+
26+
background.filters = [filter];
27+
28+
}
29+
30+
function update() {
31+
32+
filter.update();
33+
34+
}

filters/CausticLight.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* A sample demonstrating how to create new Phaser Filters.
3+
*/
4+
Phaser.Filter.SampleFilter = function (game) {
5+
6+
Phaser.Filter.call(this, game);
7+
8+
this.uniforms.divisor = { type: '1f', value: 0.5 };
9+
10+
// The fragment shader source
11+
this.fragmentSrc = [
12+
13+
"precision mediump float;",
14+
"uniform vec3 resolution;",
15+
"uniform float time;",
16+
17+
"const int complexity = 40; // More points of color.",
18+
"const float mouse_factor = 25.0; // Makes it more/less jumpy.",
19+
"const float mouse_offset = 5.0; // Drives complexity in the amount of curls/cuves. Zero is a single whirlpool.",
20+
"const float fluid_speed = 45.0; // Drives speed, higher number will make it slower.",
21+
"const float color_intensity = 0.30;",
22+
23+
"const float Pi = 3.14159;",
24+
25+
"float sinApprox(float x) {",
26+
"x = Pi + (2.0 * Pi) * floor(x / (2.0 * Pi)) - x;",
27+
"return (4.0 / Pi) * x - (4.0 / Pi / Pi) * x * abs(x);",
28+
"}",
29+
30+
"float cosApprox(float x) {",
31+
"return sinApprox(x + 0.5 * Pi);",
32+
"}",
33+
34+
"void main()",
35+
"{",
36+
"vec2 p=(2.0*gl_FragCoord.xy-resolution)/max(resolution.x,resolution.y);",
37+
"for(int i=1;i<complexity;i++)",
38+
"{",
39+
"vec2 newp=p;",
40+
"newp.x+=0.6/float(i)*sin(float(i)*p.y+time/fluid_speed+0.3*float(i))+mouse.y/mouse_factor+mouse_offset;",
41+
"newp.y+=0.6/float(i)*sin(float(i)*p.x+time/fluid_speed+0.3*float(i+10))-mouse.x/mouse_factor+mouse_offset;",
42+
"p=newp;",
43+
"}",
44+
"vec3 col=vec3(color_intensity*sin(3.0*p.x)+color_intensity,color_intensity*sin(3.0*p.y)+color_intensity,color_intensity*sin(p.x+p.y)+color_intensity);",
45+
"gl_FragColor=vec4(col, 1.0);",
46+
"}"
47+
];
48+
49+
};
50+
51+
Phaser.Filter.SampleFilter.prototype = Object.create(Phaser.Filter.prototype);
52+
Phaser.Filter.SampleFilter.prototype.constructor = Phaser.Filter.SampleFilter;
53+
54+
Phaser.Filter.SampleFilter.prototype.init = function (width, height, divisor) {
55+
56+
if (typeof divisor == 'undefined') { divisor = 0.5 };
57+
58+
this.setResolution(width, height);
59+
this.uniforms.divisor.value = divisor;
60+
61+
}

filters/Marble.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* A sample demonstrating how to create new Phaser Filters.
3+
*/
4+
Phaser.Filter.Marble = function (game) {
5+
6+
Phaser.Filter.call(this, game);
7+
8+
//this.uniforms.divisor = { type: '1f', value: 0.5 };
9+
10+
// The fragment shader source
11+
this.fragmentSrc = [
12+
13+
"precision mediump float;",
14+
"uniform vec3 resolution;",
15+
"uniform float time;",
16+
"//uniform vec2 mouse;",
17+
18+
"const int complexity = 40; // More points of color.",
19+
"const float mouse_factor = 25.0; // Makes it more/less jumpy.",
20+
"const float mouse_offset = 5.0; // Drives complexity in the amount of curls/cuves. Zero is a single whirlpool.",
21+
"const float fluid_speed = 45.0; // Drives speed, higher number will make it slower.",
22+
"const float color_intensity = 0.30;",
23+
24+
"const float Pi = 3.14159;",
25+
26+
"float sinApprox(float x) {",
27+
"x = Pi + (2.0 * Pi) * floor(x / (2.0 * Pi)) - x;",
28+
"return (4.0 / Pi) * x - (4.0 / Pi / Pi) * x * abs(x);",
29+
"}",
30+
31+
"float cosApprox(float x) {",
32+
"return sinApprox(x + 0.5 * Pi);",
33+
"}",
34+
35+
"//vec3 mouse = vec3(0,0,0);",
36+
37+
"void main()",
38+
"{",
39+
"vec2 p=(2.0*gl_FragCoord.xy-resolution)/max(resolution.x,resolution.y);",
40+
"for(int i=1;i<complexity;i++)",
41+
"{",
42+
"vec2 newp=p;",
43+
"//newp.x+=0.6/float(i)*sin(float(i)*p.y+time/fluid_speed+0.3*float(i))+mouse.y/mouse_factor+mouse_offset;",
44+
"//newp.y+=0.6/float(i)*sin(float(i)*p.x+time/fluid_speed+0.3*float(i+10))-mouse.x/mouse_factor+mouse_offset;",
45+
"newp.x+=0.6/float(i)*sin(float(i)*p.y+time/fluid_speed+0.3*float(i));",
46+
"newp.y+=0.6/float(i)*sin(float(i)*p.x+time/fluid_speed+0.3*float(i+10));",
47+
"p=newp;",
48+
"}",
49+
"vec3 col=vec3(color_intensity*sin(3.0*p.x)+color_intensity,color_intensity*sin(3.0*p.y)+color_intensity,color_intensity*sin(p.x+p.y)+color_intensity);",
50+
"gl_FragColor=vec4(col, 1.0);",
51+
"}"
52+
];
53+
54+
};
55+
56+
Phaser.Filter.Marble.prototype = Object.create(Phaser.Filter.prototype);
57+
Phaser.Filter.Marble.prototype.constructor = Phaser.Filter.Marble;
58+
59+
Phaser.Filter.Marble.prototype.init = function (width, height) {
60+
61+
this.setResolution(width, height);
62+
63+
}

src/core/Filter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Phaser.Filter = function (game, uniforms, fragmentSrc) {
5454

5555
resolution: { type: '3f', value: { x: 256, y: 256, z: 0 }},
5656
time: { type: '1f', value: 0 },
57-
mouse: { type: '4f', value: { x: 0, y: 0, z: 0, w: 0 }}
57+
mouse: { type: '4fv', value: { x: 0, y: 0, z: 0, w: 0 }}
5858

5959
};
6060

0 commit comments

Comments
 (0)