now we're doing a bunch of them
[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 int g_light_count;
13 int g_light_preview;
14 int g_shadow_samples;
15
16 // g_time ?
17
18 //vec4 g_point_light_positions[32];
19 //vec4 g_point_light_colours[32];
20 };
21
22 uniform sampler2D g_world_depth;
23
24 float world_depth_sample( vec3 pos )
25 {
26 vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw;
27 return texture( g_world_depth, depth_coord ).r;
28 }
29
30 float world_water_depth( vec3 pos )
31 {
32 vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw;
33 float ref_depth = g_water_plane.y*g_water_plane.w;
34 return texture( g_world_depth, depth_coord ).g - ref_depth;
35 }
36
37 float shadow_sample( vec3 vdir )
38 {
39 vec3 sample_pos = aWorldCo + vdir;
40 float height_sample = world_depth_sample( sample_pos );
41
42 float fdelta = height_sample - sample_pos.y;
43 return clamp( fdelta, 0.1, 0.2 )-0.1;
44 }
45
46 vec3 apply_fog( vec3 vfrag, float fdist )
47 {
48 float dist = pow(fdist*0.0008,1.2);
49 return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );
50 }
51
52
53 // New lighting model
54
55 vec3 newlight_compute_ambient()
56 {
57 return g_ambient_colour.rgb;
58 }
59
60 float newlight_compute_sun_shadow()
61 {
62 if( g_shadow_samples == 0 )
63 {
64 return 1.0;
65 }
66
67 float fspread = g_light_colours[0].w;
68 vec3 vdir = g_light_directions[0].xyz;
69 float flength = g_light_directions[0].w;
70
71 float famt = 0.0;
72 famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);
73 famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);
74 famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);
75 famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);
76 famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);
77 famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);
78 famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);
79 famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);
80
81 return 1.0 - famt;
82 }
83
84 vec3 newlight_compute_world_diffuse( vec3 wnormal )
85 {
86 vec3 vtotal = g_ambient_colour.rgb;
87
88 for( int i=0; i<g_light_count; i++ )
89 {
90 vec3 vcolour = g_light_colours[i].rgb;
91 vec3 vdir = g_light_directions[i].xyz;
92
93 float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);
94 vtotal += vcolour*flight;
95 }
96
97 return vtotal;
98 }
99
100 vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )
101 {
102 vec3 vcolour = g_light_colours[0].rgb;
103 vec3 vdir = g_light_directions[0].xyz;
104
105 vec3 specdir = reflect( -vdir, wnormal );
106 float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);
107 return vcolour*spec*fintensity;
108 }
109
110 vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview,
111 vec3 light_pos, vec3 light_colour )
112 {
113 vec3 light_delta = (light_pos-aWorldCo) * 10.0;
114
115 float quadratic = dot(light_delta,light_delta);
116 float attenuation = 1.0f/( 1.0f + quadratic );
117 attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );
118
119 return light_colour*attenuation;
120 }