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