/
lightwave.js
74 lines (50 loc) · 2.01 KB
/
lightwave.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { create: create, update: update });
var filter;
var sprite;
function create() {
// From http://glslsandbox.com/e#18918.0
// Tenjix",
var fragmentSrc = [
"precision mediump float;",
"uniform float time;",
"uniform vec2 resolution;",
"#define PI 3.1415926535897932384626433832795",
"const float position = 0.0;",
"const float scale = 1.0;",
"const float intensity = 1.0;",
// "varying vec2 surfacePosition;",
// "vec2 pos;",
"float band(vec2 pos, float amplitude, float frequency) {",
"float wave = scale * amplitude * sin(1.0 * PI * frequency * pos.x + time) / 2.05;",
"float light = clamp(amplitude * frequency * 0.02, 0.001 + 0.001 / scale, 5.0) * scale / abs(wave - pos.y);",
"return light;",
"}",
"void main() {",
"vec3 color = vec3(1.5, 0.5, 10.0);",
"color = color == vec3(0.0)? vec3(10.5, 0.5, 1.0) : color;",
"vec2 pos = (gl_FragCoord.xy / resolution.xy);",
"pos.y += - 0.5;",
"float spectrum = 0.0;",
"const float lim = 28.0;",
"#define time time*0.037 + pos.x*10.",
"for(float i = 0.0; i < lim; i++){",
"spectrum += band(pos, 1.0*sin(time*0.1/PI), 1.0*sin(time*i/lim))/pow(lim, 0.25);",
"}",
"spectrum += band(pos, cos(10.7), 2.5);",
"spectrum += band(pos, 0.4, sin(2.0));",
"spectrum += band(pos, 0.05, 4.5);",
"spectrum += band(pos, 0.1, 7.0);",
"spectrum += band(pos, 0.1, 1.0);",
"gl_FragColor = vec4(color * spectrum, spectrum);",
"}"
];
filter = new Phaser.Filter(game, null, fragmentSrc);
filter.setResolution(800, 600);
sprite = game.add.sprite();
sprite.width = 800;
sprite.height = 600;
sprite.filters = [ filter ];
}
function update() {
filter.update();
}