| 1 |
/* OpenGL shader effect (used to enhance the Castle Game Engine shaders, |
| 2 |
see https://castle-engine.io/compositing_shaders.php ), |
| 3 |
applied over terrain. |
| 4 |
|
| 5 |
This adjusts terrain color, mixing textures, based on current height. */ |
| 6 |
|
| 7 |
uniform sampler2D tex_1; |
| 8 |
uniform sampler2D tex_2; |
| 9 |
uniform sampler2D tex_3; |
| 10 |
uniform sampler2D tex_4; |
| 11 |
|
| 12 |
uniform vec3 color_1; |
| 13 |
uniform vec3 color_2; |
| 14 |
uniform vec3 color_3; |
| 15 |
uniform vec3 color_4; |
| 16 |
|
| 17 |
// These values are packed in vec4, one float per layer |
| 18 |
uniform vec4 uv_scale; |
| 19 |
uniform vec4 metallic; |
| 20 |
uniform vec4 roughness; |
| 21 |
|
| 22 |
uniform float height_1; |
| 23 |
uniform float height_2; |
| 24 |
|
| 25 |
uniform float layers_influence; |
| 26 |
uniform float steep_emphasize; |
| 27 |
|
| 28 |
varying vec3 terrain_position; |
| 29 |
varying vec3 terrain_normal; |
| 30 |
|
| 31 |
// avoid redeclaring when no "separate compilation units" available (OpenGLES) |
| 32 |
#ifndef GL_ES |
| 33 |
vec4 castle_texture_color_to_linear(const in vec4 srgbIn); |
| 34 |
#endif |
| 35 |
|
| 36 |
void PLUG_main_texture_apply(inout vec4 fragment_color, const in vec3 normal) |
| 37 |
{ |
| 38 |
float h = terrain_position.y; |
| 39 |
|
| 40 |
/* We flip terrain_position.z, to map texture more naturally, when viewed from above. |
| 41 |
This is consistent with calculating TexCoord for TCastleTerrainData.Height. |
| 42 |
We just flip the sign, because the terrain textures always have repeat = true, |
| 43 |
so there's no need to shift the texture in any way. |
| 44 |
*/ |
| 45 |
vec2 uv = vec2(terrain_position.x, -terrain_position.z); |
| 46 |
|
| 47 |
/* What does this mean? |
| 48 |
normal_slope (normal.y) |
| 49 |
= 0 means a vertical face |
| 50 |
= 1 means a horizontal face |
| 51 |
*/ |
| 52 |
float normal_slope = normalize(terrain_normal).y; |
| 53 |
|
| 54 |
vec3 c1 = color_1 * castle_texture_color_to_linear(texture2D(tex_1, uv * uv_scale.x)).rgb; |
| 55 |
vec3 c2 = color_2 * castle_texture_color_to_linear(texture2D(tex_2, uv * uv_scale.y)).rgb; |
| 56 |
vec3 c3 = color_3 * castle_texture_color_to_linear(texture2D(tex_3, uv * uv_scale.z)).rgb; |
| 57 |
vec3 c4 = color_4 * castle_texture_color_to_linear(texture2D(tex_4, uv * uv_scale.w)).rgb; |
| 58 |
|
| 59 |
float height_mix = smoothstep(height_1, height_2, h); |
| 60 |
vec3 flat_color = mix(c1, c3, height_mix); |
| 61 |
vec3 steep_color = mix(c2, c4, height_mix); |
| 62 |
vec3 modified_color = mix(steep_color, flat_color, pow(normal_slope, steep_emphasize)); |
| 63 |
|
| 64 |
fragment_color.rgb = mix(fragment_color.rgb, modified_color, layers_influence); |
| 65 |
} |
| 66 |
|
| 67 |
void PLUG_material_metallic_roughness(inout float metallic_final, inout float roughness_final) |
| 68 |
{ |
| 69 |
float h = terrain_position.y; |
| 70 |
float normal_slope = normalize(terrain_normal).y; |
| 71 |
float height_mix = smoothstep(height_1, height_2, h); |
| 72 |
|
| 73 |
float flat_metallic = mix(metallic.x, metallic.z, height_mix); |
| 74 |
float steep_metallic = mix(metallic.y, metallic.w, height_mix); |
| 75 |
float modified_metallic = mix(steep_metallic, flat_metallic, pow(normal_slope, steep_emphasize)); |
| 76 |
metallic_final = mix(metallic_final, modified_metallic, layers_influence); |
| 77 |
|
| 78 |
float flat_roughness = mix(roughness.x, roughness.z, height_mix); |
| 79 |
float steep_roughness = mix(roughness.y, roughness.w, height_mix); |
| 80 |
float modified_roughness = mix(steep_roughness, flat_roughness, pow(normal_slope, steep_emphasize)); |
| 81 |
roughness_final = mix(roughness_final, modified_roughness, layers_influence); |
| 82 |
} |