From 7c5a6166d8f48c7cb6848a103a4d7de7e21dcf75 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 25 Nov 2021 00:35:04 +0000 Subject: Add slope colouring to terrain shader --- gfx/gl/shaders/landmassShader.fs | 48 ++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 9 deletions(-) (limited to 'gfx') diff --git a/gfx/gl/shaders/landmassShader.fs b/gfx/gl/shaders/landmassShader.fs index 09e4565..ee943d7 100644 --- a/gfx/gl/shaders/landmassShader.fs +++ b/gfx/gl/shaders/landmassShader.fs @@ -10,6 +10,8 @@ uniform vec3 lightColor; uniform vec3 ambientColor; const vec3 grass = vec3(.1, .4, .05); +const vec3 slope = vec3(.6, .6, .4); +const vec3 rock = vec3(.2, .2, .1); const vec3 sand = vec3(.76, .7, .5); const vec3 snow = vec3(.97, .97, .99); @@ -17,21 +19,49 @@ const float beachline = .5; const float snowline_low = 28; const float snowline_high = 30; +const float slope_min = .99; +const float slope_mid = .95; +const float slope_max = .90; + +vec3 +mixBetween(vec3 colA, vec3 colB, float blend, float low, float high) +{ + return mix(colA, colB, (blend - low) / (high - low)); +} + void main() { - gl_FragColor = texture(sampler, texCoord0); + vec4 tex = texture(sampler, texCoord0); + gl_FragColor = tex; gl_FragColor.rgb *= clamp(ambientColor + (dot(-lightDirection, normal0) * lightColor), 0.0, 1.0); - if (height < beachline) { + + if (height < beachline) { // Sandy beach gl_FragColor.rgb *= sand; } - else if (height > snowline_high) { - gl_FragColor.rgb *= snow; - } - else if (height > snowline_low) { - gl_FragColor.rgb *= mix(grass, snow, (height - snowline_low) / (snowline_high - snowline_low)); + else if (normal0.z < slope_max) { // Dark rocky face + gl_FragColor.rgb *= rock; } - else { - gl_FragColor.rgb *= grass; + else { // Colour by lesser slope + if (normal0.z < slope_mid) { + gl_FragColor.rgb *= mixBetween(rock, slope, normal0.z, slope_max, slope_mid); + } + else if (normal0.z < slope_min) { + gl_FragColor.rgb *= mixBetween(slope, grass, normal0.z, slope_mid, slope_min); + } + else { + gl_FragColor.rgb *= grass; + } + + // Add a snow covering + if (height > snowline_low) { + vec3 tsnow = tex.rgb * snow; + if (height > snowline_high) { + gl_FragColor.rgb = tsnow; + } + else { + gl_FragColor.rgb = mixBetween(gl_FragColor.rgb, tsnow, height, snowline_low, snowline_high); + } + } } } -- cgit v1.2.3