game/terrain_shader.gdshader

56 lines
2.0 KiB
Plaintext

shader_type spatial;
uniform vec2 AREA;
uniform sampler2D LAYER_1_ALBEDO : hint_default_black;
uniform sampler2D LAYER_1_NORMAL_MAP : hint_default_black;
uniform sampler2D LAYER_2_ALBEDO : hint_default_black;
uniform sampler2D LAYER_2_NORMAL_MAP : hint_default_black;
uniform sampler2D LAYER_3_ALBEDO : hint_default_black;
uniform sampler2D LAYER_3_NORMAL_MAP : hint_default_black;
uniform sampler2D LAYER_4_ALBEDO : hint_default_black;
uniform sampler2D LAYER_4_NORMAL_MAP : hint_default_black;
uniform float MAX_HEIGHT = 100.0;
uniform sampler2D TERRAIN_MAP : hint_default_transparent, repeat_disable;
void fragment() {
vec2 uv = UV * AREA;
vec2 uv_alt = uv * -0.25;
vec3 splat_map = texture(TERRAIN_MAP, UV).rgb;
float blank = clamp(1.0 - splat_map.r - splat_map.g - splat_map.b, 0.0, 1.0);
ALBEDO = blank * 0.5 * (texture(LAYER_1_ALBEDO, uv).rgb + texture(LAYER_1_ALBEDO, uv_alt).rgb);
ALBEDO += splat_map.r * 0.5 * (texture(LAYER_2_ALBEDO, uv).rgb + texture(LAYER_2_ALBEDO, uv_alt).rgb);
ALBEDO += splat_map.g * 0.5 * (texture(LAYER_3_ALBEDO, uv).rgb + texture(LAYER_3_ALBEDO, uv_alt).rgb);
ALBEDO += splat_map.b * 0.5 * (texture(LAYER_4_ALBEDO, uv).rgb + texture(LAYER_4_ALBEDO, uv_alt).rgb);
NORMAL_MAP = blank * 0.5 * (texture(LAYER_1_NORMAL_MAP, uv).rgb + texture(LAYER_1_NORMAL_MAP, uv_alt).rgb);
NORMAL_MAP += splat_map.r * 0.5 * (texture(LAYER_1_NORMAL_MAP, uv).rgb + texture(LAYER_2_NORMAL_MAP, uv_alt).rgb);
NORMAL_MAP += splat_map.g * 0.5 * (texture(LAYER_2_NORMAL_MAP, uv).rgb + texture(LAYER_3_NORMAL_MAP, uv_alt).rgb);
NORMAL_MAP += splat_map.b * 0.5 * (texture(LAYER_3_NORMAL_MAP, uv).rgb + texture(LAYER_4_NORMAL_MAP, uv_alt).rgb);
}
float height(vec2 uv) {
return MAX_HEIGHT * texture(TERRAIN_MAP, uv).a;
}
void vertex() {
VERTEX.y = height(UV);
float left = height(vec2(UV.x - 1.0, UV.y));
float right = height(vec2(UV.x + 1.0, UV.y));
float forward = height(vec2(UV.x, UV.y + 1.0));
float backward = height(vec2(UV.x, UV.y - 1.0));
NORMAL = normalize(vec3(left - right, 2.0, forward - forward));
}