|
| 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 | +} |
0 commit comments