]> git.tdb.fi Git - r2c2.git/blob - data/r2c2/sky.shader
Don't crash if a train has no router
[r2c2.git] / data / r2c2 / sky.shader
1 vertex_shader "
2         varying vec3 eye_look_dir;
3         void main()
4         {
5                 vec4 eye_pos = vec4((gl_ModelViewMatrix*vec4(gl_Vertex.xyz, 0.0)).xyz, 1.0);
6                 gl_Position = gl_ProjectionMatrix*eye_pos;
7                 eye_look_dir = eye_pos.xyz;
8         }";
9
10 fragment_shader "
11         uniform vec3 eye_sky_dir;
12         uniform vec3 wavelengths;
13         uniform float scatter_constant;
14         uniform float sun_intensity;
15         uniform float sun_size;
16         uniform bool srgb_output;
17         varying vec3 eye_look_dir;
18         float thickness(float c, float r)
19         {
20                 float rc = r*c;
21                 return sqrt(2.0*r+1.0+rc*rc)-rc;
22         }
23         float srgb(float c)
24         {
25                 if(c<0.0031308)
26                         return 12.92*c;
27                 else
28                         return 1.055*pow(c, 1.0/2.4)-0.055;
29         }
30         void main()
31         {
32                 vec3 n_eye_look_dir = normalize(eye_look_dir);
33                 vec3 n_eye_sun_dir = normalize(gl_LightSource[0].position.xyz);
34
35                 if(dot(n_eye_look_dir, eye_sky_dir)>0.0)
36                 {
37                         float look_scale = thickness(dot(n_eye_look_dir, eye_sky_dir), 635.0);
38                         float sun_scale = thickness(dot(n_eye_sun_dir, eye_sky_dir), 635.0);
39                         float c = dot(n_eye_look_dir, n_eye_sun_dir);
40                         float scatter_strength = (1.0+c*c)/2.0;
41                         vec3 scatter_factor = scatter_constant/pow(wavelengths, vec3(4.0));
42
43                         vec3 color;
44                         float scale_ratio = look_scale/sun_scale;
45                         if(scale_ratio>0.999 && scale_ratio<1.001)
46                                 color = sun_intensity*scatter_strength*scatter_factor*sun_scale*exp(-scatter_factor*sun_scale);
47                         else
48                                 color = sun_intensity*scatter_strength*(exp(-scatter_factor*sun_scale)-exp(-scatter_factor*look_scale))/(1.0-sun_scale/look_scale);
49                         if(dot(n_eye_look_dir, n_eye_sun_dir)>cos(sun_size))
50                                 color += exp(-scatter_factor*look_scale)*sun_intensity;
51                         if(srgb_output)
52                                 gl_FragColor = vec4(srgb(color.r), srgb(color.g), srgb(color.b), 1.0);
53                         else
54                                 gl_FragColor = vec4(color, 1.0);
55                 }
56                 else
57                         gl_FragColor = vec4(vec3(0.0), 1.0);
58         }";