1////////////////////////////////////////////////
2//
3// sky shaders
4//
5////////////////////////////////////////////////
6
7shader 0 "skybox" [
8    attribute vec4 vvertex, vcolor;
9    attribute vec2 vtexcoord0;
10    uniform mat4 skymatrix;
11    uniform float ldrscale;
12    varying vec4 colorscale;
13    varying vec2 texcoord0;
14    void main(void)
15    {
16        gl_Position = skymatrix * vvertex;
17        texcoord0 = vtexcoord0;
18        colorscale = vec4(ldrscale * vcolor.rgb, vcolor.a);
19    }
20] [
21    uniform sampler2D tex0;
22    varying vec4 colorscale;
23    varying vec2 texcoord0;
24    fragdata(0, fragcolor, vec4)
25    void main(void)
26    {
27        vec4 color = texture2D(tex0, texcoord0);
28        #pragma CUBE2_swizzle color
29        fragcolor = colorscale * color;
30    }
31]
32
33shader 0 "skyboxoverbright" [
34    attribute vec4 vvertex, vcolor;
35    attribute vec2 vtexcoord0;
36    uniform mat4 skymatrix;
37    uniform float ldrscale;
38    varying vec4 colorscale;
39    varying vec2 texcoord0;
40    void main(void)
41    {
42        gl_Position = skymatrix * vvertex;
43        texcoord0 = vtexcoord0;
44        colorscale = vec4(ldrscale * vcolor.rgb, vcolor.a);
45    }
46] [
47    uniform sampler2D tex0;
48    uniform vec3 overbrightparams;
49    uniform float ldrscale;
50    varying vec4 colorscale;
51    varying vec2 texcoord0;
52    fragdata(0, fragcolor, vec4)
53    void main(void)
54    {
55        vec4 color = texture2D(tex0, texcoord0);
56        #pragma CUBE2_swizzle color
57        float lum = dot(vec3(@lumweights), color.rgb);
58        float overbright = mix(overbrightparams.x, overbrightparams.y, clamp(lum - overbrightparams.z, 0.0, 1.0));
59        color.rgb *= overbright;
60        fragcolor = colorscale * color;
61    }
62]
63
64shader 0 "atmosphere" [
65    attribute vec4 vvertex;
66    uniform mat4 sunmatrix;
67    varying vec3 camvec;
68    void main(void)
69    {
70        gl_Position = vvertex;
71
72        vec4 p = sunmatrix * vvertex;
73        camvec = p.xyz / p.w;
74    }
75] [
76    // adapted from http://blog.cloudparty.com/2013/09/25/stunning-procedural-skies-in-webgl-part-2/
77    uniform vec3 sunlight;
78    uniform vec3 sundir;
79    uniform vec3 sundiskparams;
80    uniform vec3 atmoradius;
81    uniform float gm;
82    uniform vec3 betar, betam, betarm;
83    uniform vec2 hdrgamma;
84    uniform float atmoalpha;
85    varying vec3 camvec;
86    fragdata(0, fragcolor, vec4)
87
88    vec3 calcextinction(float dist)
89    {
90        return exp2(-dist * betarm);
91    }
92
93    vec3 calcscatter(float costheta)
94    {
95        float rphase = 1.0 + costheta*costheta;
96        float mphase = pow(1.0 + gm*(gm - 2.0*costheta), -1.5);
97        return betar*rphase + betam*mphase;
98    }
99
100    float baseopticaldepth(vec3 ray)
101    {
102        float a = atmoradius.x * max(ray.z, min(sundir.z, 0.0));
103        return sqrt(a*a + atmoradius.z) - a;
104    }
105
106    float opticaldepth(vec3 pos, vec3 ray)
107    {
108        pos.z = max(pos.z, 0.0) + atmoradius.x;
109        float a = dot(pos, ray);
110        return sqrt(a*a + atmoradius.y - dot(pos, pos)) - a;
111    }
112
113    void main(void)
114    {
115        vec3 camdir = normalize(camvec);
116        float costheta = dot(camdir, sundir);
117
118        // optical depth along view ray
119        float raydist = baseopticaldepth(camdir);
120
121        // extinction of light along view ray
122        vec3 extinction = calcextinction(raydist);
123
124        // optical depth for incoming light hitting the view ray
125        float lightraydist = opticaldepth(camdir * (raydist * max(0.15 + 0.75 * sundir.z, 0.0)), sundir);
126
127        // cast a ray towards the sun and calculate the incoming extincted light
128        vec3 incominglight = calcextinction(lightraydist);
129
130        // calculate the in-scattering
131        vec3 scattering = calcscatter(costheta) * (1.0 - extinction);
132
133        // combine
134        vec3 inscatter = incominglight * scattering;
135
136        // sun disk
137        vec3 sundisk = sundiskparams.z * extinction * pow(clamp(costheta*sundiskparams.x + sundiskparams.y, 0.0, 1.0), 8.0);
138
139        inscatter += sundisk;
140        @(hdrgammaencode inscatter)
141
142        fragcolor = vec4(sunlight * inscatter, atmoalpha);
143    }
144]
145
146shader 0 "skyfog" [
147    attribute vec4 vvertex, vcolor;
148    uniform mat4 skymatrix;
149    uniform float ldrscale;
150    varying vec4 color;
151    void main(void)
152    {
153        gl_Position = skymatrix * vvertex;
154        color = vec4(ldrscale * vcolor.rgb, vcolor.a);
155    }
156] [
157    uniform sampler2D tex0;
158    varying vec4 color;
159    fragdata(0, fragcolor, vec4)
160    void main(void)
161    {
162        fragcolor = color;
163    }
164]
165
166