game/terrain_shader.gdshader

59 lines
1.9 KiB
Plaintext
Raw Normal View History

2023-01-10 02:36:09 +01:00
shader_type spatial;
uniform sampler2D ALBEDO_MAP_0 : hint_default_black;
2023-01-10 02:36:09 +01:00
uniform sampler2D ALBEDO_MAP_R : hint_default_black;
2023-01-10 02:36:09 +01:00
uniform sampler2D ALBEDO_MAP_G : hint_default_black;
2023-01-10 02:36:09 +01:00
uniform sampler2D ALBEDO_MAP_B : hint_default_black;
2023-01-10 02:36:09 +01:00
uniform float MAX_HEIGHT = 100.0;
2023-01-10 02:36:09 +01:00
uniform sampler2D NORMAL_MAP_0 : hint_default_black;
2023-01-10 02:36:09 +01:00
uniform sampler2D NORMAL_MAP_R : hint_default_black;
2023-01-10 02:36:09 +01:00
uniform sampler2D NORMAL_MAP_G : hint_default_black;
2023-01-10 02:36:09 +01:00
uniform sampler2D NORMAL_MAP_B : hint_default_black;
2023-01-10 02:36:09 +01:00
uniform vec2 SIZE;
2023-01-10 02:36:09 +01:00
uniform sampler2D TERRAIN_MAP : hint_default_transparent, repeat_disable;
void fragment() {
vec2 uv = UV * SIZE;
2023-01-10 02:36:09 +01:00
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(ALBEDO_MAP_0, uv).rgb + texture(ALBEDO_MAP_0, uv_alt).rgb);
ALBEDO += splat_map.r * 0.5 * (texture(ALBEDO_MAP_R, uv).rgb + texture(ALBEDO_MAP_R, uv_alt).rgb);
ALBEDO += splat_map.g * 0.5 * (texture(ALBEDO_MAP_G, uv).rgb + texture(ALBEDO_MAP_G, uv_alt).rgb);
ALBEDO += splat_map.b * 0.5 * (texture(ALBEDO_MAP_B, uv).rgb + texture(ALBEDO_MAP_B, uv_alt).rgb);
2023-01-10 02:36:09 +01:00
NORMAL_MAP = blank * 0.5 * (texture(NORMAL_MAP_0, uv).rgb + texture(NORMAL_MAP_0, uv_alt).rgb);
NORMAL_MAP += splat_map.r * 0.5 * (texture(NORMAL_MAP_R, uv).rgb + texture(NORMAL_MAP_R, uv_alt).rgb);
NORMAL_MAP += splat_map.g * 0.5 * (texture(NORMAL_MAP_G, uv).rgb + texture(NORMAL_MAP_G, uv_alt).rgb);
NORMAL_MAP += splat_map.b * 0.5 * (texture(NORMAL_MAP_B, uv).rgb + texture(NORMAL_MAP_B, uv_alt).rgb);
2023-01-10 02:36:09 +01:00
}
float height(vec2 uv) {
return MAX_HEIGHT * texture(TERRAIN_MAP, uv).a;
}
void vertex() {
VERTEX.y = height(UV);
2023-01-10 19:02:26 +01:00
vec2 texel_size = UV / SIZE;
2023-01-10 02:36:09 +01:00
2023-01-10 19:02:26 +01:00
vec4 h = vec4(
height(UV + (texel_size * vec2(0, -1))),
height(UV + (texel_size * vec2(-1, 0))),
height(UV + (texel_size * vec2( 1, 0))),
height(UV + (texel_size * vec2( 0, 1))));
NORMAL = normalize(vec3(h.y - h.z, 2.0, h.x - h.w));
2023-01-10 02:36:09 +01:00
}