needs a lot of cleaning but lights are OK
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / common_world.glsl
1 layout (location = 0) out vec4 oColour;
2
3 layout (std140) uniform ub_world_lighting
4 {
5 vec4 g_light_colours[3];
6 vec4 g_light_directions[3];
7 vec4 g_ambient_colour;
8
9 vec4 g_water_plane;
10 vec4 g_depth_bounds;
11 float g_water_fog;
12 float g_time;
13 int g_light_count;
14 int g_light_preview;
15 int g_shadow_samples;
16
17 // g_time ?
18
19 //vec4 g_point_light_positions[32];
20 //vec4 g_point_light_colours[32];
21 };
22
23 uniform sampler2D g_world_depth;
24
25 float world_depth_sample( vec3 pos )
26 {
27 vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw;
28 return texture( g_world_depth, depth_coord ).r;
29 }
30
31 float world_water_depth( vec3 pos )
32 {
33 vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw;
34 float ref_depth = g_water_plane.y*g_water_plane.w;
35 return texture( g_world_depth, depth_coord ).g - ref_depth;
36 }
37
38 float shadow_sample( vec3 vdir )
39 {
40 vec3 sample_pos = aWorldCo + vdir;
41 float height_sample = world_depth_sample( sample_pos );
42
43 float fdelta = height_sample - sample_pos.y;
44 return clamp( fdelta, 0.1, 0.2 )-0.1;
45 }
46
47 vec3 apply_fog( vec3 vfrag, float fdist )
48 {
49 float dist = pow(fdist*0.0008,1.2);
50 return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );
51 }
52
53
54 // New lighting model
55
56 vec3 newlight_compute_ambient()
57 {
58 return g_ambient_colour.rgb;
59 }
60
61 float newlight_compute_sun_shadow( vec3 dir )
62 {
63 if( g_shadow_samples == 0 )
64 {
65 return 1.0;
66 }
67
68 float fspread = g_light_colours[0].w;
69 vec3 vdir = dir;
70 float flength = g_light_directions[0].w;
71
72 float famt = 0.0;
73 famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);
74 famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);
75 famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);
76 famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);
77 famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);
78 famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);
79 famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);
80 famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);
81
82 return 1.0 - famt;
83 }
84
85 vec3 newlight_compute_world_diffuse( vec3 wnormal )
86 {
87 vec3 vtotal = g_ambient_colour.rgb;
88
89 for( int i=0; i<g_light_count; i++ )
90 {
91 vec3 vcolour = g_light_colours[i].rgb;
92 vec3 vdir = g_light_directions[i].xyz;
93
94 float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);
95 vtotal += vcolour*flight;
96 }
97
98 return vtotal;
99 }
100
101 vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )
102 {
103 vec3 vcolour = g_light_colours[0].rgb;
104 vec3 vdir = g_light_directions[0].xyz;
105
106 vec3 specdir = reflect( -vdir, wnormal );
107 float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);
108 return vcolour*spec*fintensity;
109 }
110
111 float newlight_specular( vec3 wnormal, vec3 dir, vec3 halfview, float exponent )
112 {
113 vec3 specdir = reflect( -dir, wnormal );
114 return pow(max(dot( halfview, specdir ), 0.0), exponent);
115 }
116
117 vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview,
118 vec3 light_colour, vec3 light_pos )
119 {
120 vec3 light_delta = (light_pos-aWorldCo) * 10.0;
121
122 float quadratic = dot(light_delta,light_delta);
123 float attenuation = 1.0f/( 1.0f + quadratic );
124 attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );
125
126 return light_colour*attenuation;
127 }
128
129 vec3 newlight_compute_spot( vec3 wnormal, vec3 halfview,
130 vec3 light_colour, vec3 light_pos,
131 vec4 light_dir )
132 {
133 vec3 light_delta = (light_pos-aWorldCo) * 10.0;
134
135 float quadratic = dot(light_delta,light_delta);
136 float attenuation = 1.0f/( 1.0f + quadratic );
137
138 light_delta = normalize( light_delta );
139 attenuation *= max( 0.0, dot( light_delta, wnormal ) );
140
141 float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) ),
142 falloff = max( 0.0,( spot_theta - light_dir.w ) / (1.0-light_dir.w) );
143
144 return light_colour*attenuation*falloff;
145 }