commit d5bdd4f1f1f17df87f8d694e1b5d8f16b25035b2
parent ee467f003cf4bb64c6e2eb619ec875b507f08cfb
Author: Michael Savage <mikejsavage@gmail.com>
Date: Wed, 1 Nov 2017 23:04:51 +0200
Sample normal/horizonmaps in the vertex shader. Fixes blockiness at the sides of the screen with high FOV
Diffstat:
1 file changed, 12 insertions(+), 14 deletions(-)
diff --git a/shaders/clipmap.glsl b/shaders/clipmap.glsl
@@ -16,8 +16,7 @@ layout( std140 ) uniform clipmap {
struct VSOut {
vec3 pos;
- vec3 normal;
- float horizon;
+ vec2 uv;
};
uniform sampler2D heightmap;
@@ -35,15 +34,8 @@ void main() {
vec2 uv = floor( xy / scale ) * scale / textureSize( heightmap, 0 ) + 0.5;
float z = 255.0 * texture( heightmap, uv ).r + texture( heightmap, uv ).g;
- vec2 normal_xy = texture( normalmap, uv ).xy * 2.0 - 1.0;
- float normal_z = sqrt( 1.0 - normal_xy.x * normal_xy.x - normal_xy.y * normal_xy.y );
- vec3 normal = vec3( normal_xy.x, normal_xy.y, normal_z );
-
- float horizon_angle = texture( horizonmap, uv ).r;
-
v2f.pos = vec3( xy, z );
- v2f.normal = normal;
- v2f.horizon = horizon_angle;
+ v2f.uv = uv;
gl_Position = P * V * vec4( floor( xy ), z, 1.0 );
}
@@ -53,11 +45,17 @@ in VSOut v2f;
out vec4 screen_colour;
void main() {
+ vec2 normal_xy = texture( normalmap, v2f.uv ).xy * 2.0 - 1.0;
+ float normal_z = sqrt( 1.0 - normal_xy.x * normal_xy.x - normal_xy.y * normal_xy.y );
+ vec3 normal = vec3( normal_xy.x, normal_xy.y, normal_z );
+
+ float horizon = texture( horizonmap, v2f.uv ).r;
+
// ground colour
vec3 ground;
if( v2f.pos.z > 175 ) {
// snow/rocks
- if( v2f.normal.z > 0.5 ) {
+ if( normal.z > 0.5 ) {
ground = vec3( 0.8, 0.8, 0.8 );
}
else {
@@ -68,7 +66,7 @@ void main() {
ground = vec3( 0.0, 0.25, 1.0 );
}
else {
- if( v2f.normal.z > 0.8 ) {
+ if( normal.z > 0.8 ) {
ground = vec3( 0.4, 1.0, 0.4 );
}
else {
@@ -78,8 +76,8 @@ void main() {
// sunlight + sky ambient lighting
// area of a chord is somewhat similar to smoothstep
- float sun_visible_fraction = smoothstep( -0.2, 0.2, sun_angle - v2f.horizon );
- float sunlight_lambert = max( 0, dot( v2f.normal, sun_dir ) );
+ float sun_visible_fraction = smoothstep( -0.2, 0.2, sun_angle - horizon );
+ float sunlight_lambert = max( 0, dot( normal, sun_dir ) );
vec3 sunlight = sun_visible_fraction * sunlight_lambert * vec3( 0.9, 0.9, 0.5 );
vec3 ambient = vec3( 0.05, 0.05, 0.15 );