reworked lighting uniforms
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / common_world.glsl
diff --git a/shaders/common_world.glsl b/shaders/common_world.glsl
new file mode 100644 (file)
index 0000000..329226a
--- /dev/null
@@ -0,0 +1,57 @@
+layout (std140) uniform ub_world_lighting
+{
+   vec3 g_directional;
+   vec3 g_sun_colour;
+   vec3 g_shadow_colour;
+   vec4 g_water_plane;
+   vec4 g_depth_bounds;
+   float g_water_fog;
+};
+
+uniform sampler2D g_world_depth;
+
+// Standard diffuse + spec models
+// ==============================
+
+vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )
+{
+   float flight = dot( g_directional, wnormal )*0.5+0.5;
+   return vfrag * mix( g_shadow_colour, g_sun_colour, flight );
+}
+
+vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )
+{
+   vec3 specdir = reflect( -g_directional, wnormal );
+   float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);
+   return vfrag + g_sun_colour*spec*fintensity;
+}
+
+float world_depth_sample( vec3 pos )
+{
+   vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; 
+   return texture( g_world_depth, depth_coord ).r;
+}
+
+float shadow_sample( vec3 vdir )
+{
+   vec3 sample_pos = aCo + vdir;
+   float height_sample = world_depth_sample( sample_pos );
+
+   float fdelta = height_sample - sample_pos.y;
+   return clamp( fdelta, 0.1, 0.2 )-0.1;
+}
+
+vec3 do_light_shadowing( vec3 vfrag )
+{
+   float faccum = 0.0;
+   faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));
+   faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));
+   faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));
+   faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));
+   faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);
+   faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);
+   faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);
+   faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);
+   return mix( vfrag, g_shadow_colour, faccum );
+}
+