slightly better?
[vrfileman] / sdr / glink.p.glsl
1 uniform float tsec, phase;
2
3 float cnoise(vec2 p);
4 float fbm(vec2 p, int octaves);
5
6 void main()
7 {
8         const vec3 color = vec3(0.2, 0.4, 1.0);
9
10         float tm = tsec + phase;
11
12         vec2 uv = gl_TexCoord[0].st;
13         vec2 pt = uv * vec2(2.0) - vec2(1.0);
14
15         float pulse = fbm(vec2(uv.y * 50.0 - tm * 0.5, 0.0), 2);
16
17         float d_horiz = abs(pt.x);
18         float d_vert = max(abs(pt.y), 0.0);
19
20         float beam_sharpness = 80.0 * clamp(pow(uv.y, 0.5), 0.0, 1.0) * (pulse * 0.5 + 1.0);
21         float beam_intensity = 6.0 * min(0.01 / (uv.y * uv.y) - 0.0004, 1.0);
22
23         float glow_u = pow(1.0 - smoothstep(0.0, 1.0, d_horiz), beam_sharpness) * beam_intensity;
24         float glow_v = 1.0 - smoothstep(0.98, 1.0, d_vert);
25
26         float glow = glow_u * glow_v;
27
28         gl_FragColor.rgb = color * glow;// + vec3(0.0, 1.0, 0.0) * step(0.99, max(abs(pt.x), abs(pt.y)));
29         gl_FragColor.a = 1.0;
30 }
31
32 float fbm(vec2 p, int octaves)
33 {
34         float res = 0.0;
35         float freq = 1.0;
36         float scale = 1.0;
37
38         for(int i=0; i<octaves; i++) {
39                 res += cnoise(p * freq) * scale;
40                 freq *= 2.0;
41                 scale *= 0.5;
42         }
43         return res;
44 }
45
46 //
47 // GLSL textureless classic 2D noise "cnoise",
48 // with an RSL-style periodic variant "pnoise".
49 // Author:  Stefan Gustavson (stefan.gustavson@liu.se)
50 // Version: 2011-08-22
51 //
52 // Many thanks to Ian McEwan of Ashima Arts for the
53 // ideas for permutation and gradient selection.
54 //
55 // Copyright (c) 2011 Stefan Gustavson. All rights reserved.
56 // Distributed under the MIT license. See LICENSE file.
57 // https://github.com/stegu/webgl-noise
58 //
59
60 vec4 mod289(vec4 x)
61 {
62   return x - floor(x * (1.0 / 289.0)) * 289.0;
63 }
64
65 vec4 permute(vec4 x)
66 {
67   return mod289(((x*34.0)+1.0)*x);
68 }
69
70 vec4 taylor_inv_sqrt(vec4 r)
71 {
72   return 1.79284291400159 - 0.85373472095314 * r;
73 }
74
75 vec2 fade(vec2 t) {
76   return t*t*t*(t*(t*6.0-15.0)+10.0);
77 }
78
79 // Classic perlin noise
80 float cnoise(vec2 p)
81 {
82   vec4 pi = floor(p.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
83   vec4 pf = fract(p.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
84   pi = mod289(pi); // To avoid truncation effects in permutation
85   vec4 ix = pi.xzxz;
86   vec4 iy = pi.yyww;
87   vec4 fx = pf.xzxz;
88   vec4 fy = pf.yyww;
89
90   vec4 i = permute(permute(ix) + iy);
91
92   vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
93   vec4 gy = abs(gx) - 0.5 ;
94   vec4 tx = floor(gx + 0.5);
95   gx = gx - tx;
96
97   vec2 g00 = vec2(gx.x,gy.x);
98   vec2 g10 = vec2(gx.y,gy.y);
99   vec2 g01 = vec2(gx.z,gy.z);
100   vec2 g11 = vec2(gx.w,gy.w);
101
102   vec4 norm = taylor_inv_sqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
103   g00 *= norm.x;  
104   g01 *= norm.y;  
105   g10 *= norm.z;  
106   g11 *= norm.w;  
107
108   float n00 = dot(g00, vec2(fx.x, fy.x));
109   float n10 = dot(g10, vec2(fx.y, fy.y));
110   float n01 = dot(g01, vec2(fx.z, fy.z));
111   float n11 = dot(g11, vec2(fx.w, fy.w));
112
113   vec2 fade_xy = fade(pf.xy);
114   vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
115   float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
116   return 2.3 * n_xy;
117 }