smoothstep for the horizon
[vrfileman] / sdr / skydome.p.glsl
1 uniform vec3 horiz_color, mid_color, zenith_color, fog_color;
2
3 const float mid_point = 0.25;
4 const float horiz_scale = 20.0;
5
6 vec3 sky_grad(float t, float mid);
7 float pnoise(float p, float rep);
8 float fbm(float p, int octaves, float rep);
9
10 void main()
11 {
12         float tx = gl_TexCoord[0].x;
13         float ty = 1.0 - gl_TexCoord[0].y;
14         vec3 color = sky_grad(ty, mid_point);
15
16         float horiz = ty + fbm(tx * horiz_scale, 4, horiz_scale) * 0.035;
17         color = mix(fog_color, color, smoothstep(0.034, 0.035, horiz));
18
19         gl_FragColor.rgb = color;
20         gl_FragColor.a = 1.0;
21 }
22
23 vec3 sky_grad(float t, float mid)
24 {
25         float t_low = min(t / mid, 1.0);
26         float t_high = min((1.0 - t) / (1.0 - mid), 1.0);
27
28         return mix(zenith_color, mix(horiz_color, mid_color, t_low), t_high);
29 }
30
31 float pnoise(vec2 p, vec2 rep);
32
33 float pnoise(float p, float rep)
34 {
35         return pnoise(vec2(p, 0.0), vec2(rep, rep));
36 }
37
38 float fbm(float p, int octaves, float rep)
39 {
40         float res = 0.0;
41         float freq = 1.0;
42         float scale = 1.0;
43
44         for(int i=0; i<octaves; i++) {
45                 res += pnoise(p * freq, rep * freq) * scale;
46                 freq *= 2.0;
47                 scale *= 0.5;
48         }
49         return res;
50 }
51
52 //
53 // GLSL textureless classic 2D noise "cnoise",
54 // with an RSL-style periodic variant "pnoise".
55 // Author:  Stefan Gustavson (stefan.gustavson@liu.se)
56 // Version: 2011-08-22
57 //
58 // Many thanks to Ian McEwan of Ashima Arts for the
59 // ideas for permutation and gradient selection.
60 //
61 // Copyright (c) 2011 Stefan Gustavson. All rights reserved.
62 // Distributed under the MIT license. See LICENSE file.
63 // https://github.com/stegu/webgl-noise
64 //
65
66 vec4 mod289(vec4 x)
67 {
68   return x - floor(x * (1.0 / 289.0)) * 289.0;
69 }
70
71 vec4 permute(vec4 x)
72 {
73   return mod289(((x*34.0)+1.0)*x);
74 }
75
76 vec4 taylor_inv_sqrt(vec4 r)
77 {
78   return 1.79284291400159 - 0.85373472095314 * r;
79 }
80
81 vec2 fade(vec2 t) {
82   return t*t*t*(t*(t*6.0-15.0)+10.0);
83 }
84
85 // Classic perlin noise
86 float cnoise(vec2 p)
87 {
88   vec4 pi = floor(p.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
89   vec4 pf = fract(p.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
90   pi = mod289(pi); // To avoid truncation effects in permutation
91   vec4 ix = pi.xzxz;
92   vec4 iy = pi.yyww;
93   vec4 fx = pf.xzxz;
94   vec4 fy = pf.yyww;
95
96   vec4 i = permute(permute(ix) + iy);
97
98   vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
99   vec4 gy = abs(gx) - 0.5 ;
100   vec4 tx = floor(gx + 0.5);
101   gx = gx - tx;
102
103   vec2 g00 = vec2(gx.x,gy.x);
104   vec2 g10 = vec2(gx.y,gy.y);
105   vec2 g01 = vec2(gx.z,gy.z);
106   vec2 g11 = vec2(gx.w,gy.w);
107
108   vec4 norm = taylor_inv_sqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
109   g00 *= norm.x;  
110   g01 *= norm.y;  
111   g10 *= norm.z;  
112   g11 *= norm.w;  
113
114   float n00 = dot(g00, vec2(fx.x, fy.x));
115   float n10 = dot(g10, vec2(fx.y, fy.y));
116   float n01 = dot(g01, vec2(fx.z, fy.z));
117   float n11 = dot(g11, vec2(fx.w, fy.w));
118
119   vec2 fade_xy = fade(pf.xy);
120   vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
121   float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
122   return 2.3 * n_xy;
123 }
124
125 // Classic perlin noise, periodic variant
126 float pnoise(vec2 p, vec2 rep)
127 {
128   vec4 pi = floor(p.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
129   vec4 pf = fract(p.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
130   pi = mod(pi, rep.xyxy); // To create noise with explicit period
131   pi = mod289(pi);        // To avoid truncation effects in permutation
132   vec4 ix = pi.xzxz;
133   vec4 iy = pi.yyww;
134   vec4 fx = pf.xzxz;
135   vec4 fy = pf.yyww;
136
137   vec4 i = permute(permute(ix) + iy);
138
139   vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
140   vec4 gy = abs(gx) - 0.5 ;
141   vec4 tx = floor(gx + 0.5);
142   gx = gx - tx;
143
144   vec2 g00 = vec2(gx.x,gy.x);
145   vec2 g10 = vec2(gx.y,gy.y);
146   vec2 g01 = vec2(gx.z,gy.z);
147   vec2 g11 = vec2(gx.w,gy.w);
148
149   vec4 norm = taylor_inv_sqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
150   g00 *= norm.x;  
151   g01 *= norm.y;  
152   g10 *= norm.z;  
153   g11 *= norm.w;  
154
155   float n00 = dot(g00, vec2(fx.x, fy.x));
156   float n10 = dot(g10, vec2(fx.y, fy.y));
157   float n01 = dot(g01, vec2(fx.z, fy.z));
158   float n11 = dot(g11, vec2(fx.w, fy.w));
159
160   vec2 fade_xy = fade(pf.xy);
161   vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
162   float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
163   return 2.3 * n_xy;
164 }