cyberenv
[vrfileman] / sdr / skydome.p.glsl
diff --git a/sdr/skydome.p.glsl b/sdr/skydome.p.glsl
new file mode 100644 (file)
index 0000000..62a3475
--- /dev/null
@@ -0,0 +1,164 @@
+uniform vec3 horiz_color, mid_color, zenith_color, fog_color;
+
+const float mid_point = 0.25;
+const float horiz_scale = 20.0;
+
+vec3 sky_grad(float t, float mid);
+float pnoise(float p, float rep);
+float fbm(float p, int octaves, float rep);
+
+void main()
+{
+       float tx = gl_TexCoord[0].x;
+       float ty = 1.0 - gl_TexCoord[0].y;
+       vec3 color = sky_grad(ty, mid_point);
+
+       float horiz = ty + fbm(tx * horiz_scale, 4, horiz_scale) * 0.035;
+       color = mix(fog_color, color, step(0.035, horiz));
+
+       gl_FragColor.rgb = color;
+       gl_FragColor.a = 1.0;
+}
+
+vec3 sky_grad(float t, float mid)
+{
+       float t_low = min(t / mid, 1.0);
+       float t_high = min((1.0 - t) / (1.0 - mid), 1.0);
+
+       return mix(zenith_color, mix(horiz_color, mid_color, t_low), t_high);
+}
+
+float pnoise(vec2 p, vec2 rep);
+
+float pnoise(float p, float rep)
+{
+       return pnoise(vec2(p, 0.0), vec2(rep, rep));
+}
+
+float fbm(float p, int octaves, float rep)
+{
+       float res = 0.0;
+       float freq = 1.0;
+       float scale = 1.0;
+
+       for(int i=0; i<octaves; i++) {
+               res += pnoise(p * freq, rep * freq) * scale;
+               freq *= 2.0;
+               scale *= 0.5;
+       }
+       return res;
+}
+
+//
+// GLSL textureless classic 2D noise "cnoise",
+// with an RSL-style periodic variant "pnoise".
+// Author:  Stefan Gustavson (stefan.gustavson@liu.se)
+// Version: 2011-08-22
+//
+// Many thanks to Ian McEwan of Ashima Arts for the
+// ideas for permutation and gradient selection.
+//
+// Copyright (c) 2011 Stefan Gustavson. All rights reserved.
+// Distributed under the MIT license. See LICENSE file.
+// https://github.com/stegu/webgl-noise
+//
+
+vec4 mod289(vec4 x)
+{
+  return x - floor(x * (1.0 / 289.0)) * 289.0;
+}
+
+vec4 permute(vec4 x)
+{
+  return mod289(((x*34.0)+1.0)*x);
+}
+
+vec4 taylor_inv_sqrt(vec4 r)
+{
+  return 1.79284291400159 - 0.85373472095314 * r;
+}
+
+vec2 fade(vec2 t) {
+  return t*t*t*(t*(t*6.0-15.0)+10.0);
+}
+
+// Classic perlin noise
+float cnoise(vec2 p)
+{
+  vec4 pi = floor(p.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
+  vec4 pf = fract(p.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
+  pi = mod289(pi); // To avoid truncation effects in permutation
+  vec4 ix = pi.xzxz;
+  vec4 iy = pi.yyww;
+  vec4 fx = pf.xzxz;
+  vec4 fy = pf.yyww;
+
+  vec4 i = permute(permute(ix) + iy);
+
+  vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
+  vec4 gy = abs(gx) - 0.5 ;
+  vec4 tx = floor(gx + 0.5);
+  gx = gx - tx;
+
+  vec2 g00 = vec2(gx.x,gy.x);
+  vec2 g10 = vec2(gx.y,gy.y);
+  vec2 g01 = vec2(gx.z,gy.z);
+  vec2 g11 = vec2(gx.w,gy.w);
+
+  vec4 norm = taylor_inv_sqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
+  g00 *= norm.x;  
+  g01 *= norm.y;  
+  g10 *= norm.z;  
+  g11 *= norm.w;  
+
+  float n00 = dot(g00, vec2(fx.x, fy.x));
+  float n10 = dot(g10, vec2(fx.y, fy.y));
+  float n01 = dot(g01, vec2(fx.z, fy.z));
+  float n11 = dot(g11, vec2(fx.w, fy.w));
+
+  vec2 fade_xy = fade(pf.xy);
+  vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
+  float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
+  return 2.3 * n_xy;
+}
+
+// Classic perlin noise, periodic variant
+float pnoise(vec2 p, vec2 rep)
+{
+  vec4 pi = floor(p.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
+  vec4 pf = fract(p.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
+  pi = mod(pi, rep.xyxy); // To create noise with explicit period
+  pi = mod289(pi);        // To avoid truncation effects in permutation
+  vec4 ix = pi.xzxz;
+  vec4 iy = pi.yyww;
+  vec4 fx = pf.xzxz;
+  vec4 fy = pf.yyww;
+
+  vec4 i = permute(permute(ix) + iy);
+
+  vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
+  vec4 gy = abs(gx) - 0.5 ;
+  vec4 tx = floor(gx + 0.5);
+  gx = gx - tx;
+
+  vec2 g00 = vec2(gx.x,gy.x);
+  vec2 g10 = vec2(gx.y,gy.y);
+  vec2 g01 = vec2(gx.z,gy.z);
+  vec2 g11 = vec2(gx.w,gy.w);
+
+  vec4 norm = taylor_inv_sqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
+  g00 *= norm.x;  
+  g01 *= norm.y;  
+  g10 *= norm.z;  
+  g11 *= norm.w;  
+
+  float n00 = dot(g00, vec2(fx.x, fy.x));
+  float n10 = dot(g10, vec2(fx.y, fy.y));
+  float n01 = dot(g01, vec2(fx.z, fy.z));
+  float n11 = dot(g11, vec2(fx.w, fy.w));
+
+  vec2 fade_xy = fade(pf.xy);
+  vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
+  float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
+  return 2.3 * n_xy;
+}