shader_type spatial; uniform sampler2D colour_map : source_color, hint_default_white, filter_linear_mipmap_anisotropic, repeat_disable; uniform sampler2D normal_map : hint_normal, filter_linear_mipmap, repeat_disable; uniform sampler2D weight_map : hint_default_black, filter_linear_mipmap, repeat_disable; uniform sampler2D perturb : hint_default_white, filter_linear_mipmap, repeat_enable; uniform sampler2D detail_colour_0 : source_color, hint_default_white, filter_linear_mipmap_anisotropic, repeat_enable; uniform sampler2D detail_colour_1 : source_color, hint_default_white, filter_linear_mipmap_anisotropic, repeat_enable; uniform sampler2D detail_colour_2 : source_color, hint_default_white, filter_linear_mipmap_anisotropic, repeat_enable; uniform sampler2D detail_colour_3 : source_color, hint_default_white, filter_linear_mipmap_anisotropic, repeat_enable; uniform sampler2D detail_normal_0 : hint_normal, filter_linear_mipmap_anisotropic, repeat_enable; uniform sampler2D detail_normal_1 : hint_normal, filter_linear_mipmap_anisotropic, repeat_enable; uniform sampler2D detail_normal_2 : hint_normal, filter_linear_mipmap_anisotropic, repeat_enable; uniform sampler2D detail_normal_3 : hint_normal, filter_linear_mipmap_anisotropic, repeat_enable; uniform bool height_blend = false; uniform bool snowy = false; #include "level.gdshaderinc" uniform bool has_perturb = false; varying vec3 model_position; void vertex() { model_position = VERTEX; } vec3 unpack_tangent(vec4 texel) { return normalize(vec3(2.0 * texel.x - 1.0, 1.0 - 2.0 * texel.y, texel.z)); } vec4 average(sampler2D map) { return textureLod(map, vec2(0.5), 20.0); } void fragment() { vec2 base_uv = UV; if (has_perturb) { base_uv += (texture(perturb, UV * 200.0).xy - 0.5) * 0.001; } vec2 detail_uv = vec2(0.1 * model_position.x, 1.0 - 0.1 * model_position.z); vec4 w = texture(weight_map, base_uv); w.a = max(0.0, 1.0 - (w.r + w.g + w.b)); vec4 n0 = texture(detail_normal_0, detail_uv); vec4 n1 = texture(detail_normal_1, detail_uv); vec4 n2 = texture(detail_normal_2, detail_uv); vec4 n3 = texture(detail_normal_3, detail_uv); if (height_blend) { w = pow(w + vec4(n0.a, n1.a, n2.a, n3.a), vec4(16.0)); } w /= max(dot(w, vec4(1.0)), 1e-5); float fade = clamp(length(VERTEX) / 200.0, 0.0, 1.0); vec3 base_normal = normalize(texture(normal_map, base_uv).xyz * 2.0 - 1.0); vec3 bitangent = normalize(cross(vec3(1.0, 0.0, 0.0), base_normal)); vec3 tangent = normalize(cross(base_normal, bitangent)); vec4 normal_blend = n0 * w.r + n1 * w.g + n2 * w.b + n3 * w.a; vec3 world_normal = normalize(mat3(tangent, bitangent, base_normal) * mix(unpack_tangent(normal_blend), vec3(0.0, 0.0, 1.0), fade)); vec4 base = texture(colour_map, base_uv); vec4 avg = max(average(detail_colour_0) * w.r + average(detail_colour_1) * w.g + average(detail_colour_2) * w.b + average(detail_colour_3) * w.a, vec4(0.01)); vec4 tint = base / avg; tint.a = 1.0; vec4 blended = texture(detail_colour_0, detail_uv) * w.r + texture(detail_colour_1, detail_uv) * w.g + texture(detail_colour_2, detail_uv) * w.b + texture(detail_colour_3, detail_uv) * w.a; vec4 colour = mix(blended * tint, base, fade); vec3 world = CAMERA_POSITION_WORLD + (INV_VIEW_MATRIX * vec4(VERTEX, 0.0)).xyz; vec3 albedo = colour.rgb; float roughness = 0.99; float spec = blended.a; if (snowy) { roughness = mix(mix(0.99, 0.7, w.g), 0.2, w.r); spec = mix(mix(spec, 0.4, w.g), 0.5, w.r); albedo *= mix(1.0, 0.25, w.b) * mix(1.0, 0.5, w.a) * mix(1.0, 0.8, w.r); } vec3 normal = normalize(mat3(MODEL_MATRIX) * world_normal); float dist = length(world - CAMERA_POSITION_WORLD); og_surface(albedo, roughness, spec, normal, world, dist, TIME, true, false); albedo *= 1.0 + og_caustics(world, normal, TIME) * level_sun_colour; NORMAL = normalize((VIEW_MATRIX * vec4(normal, 0.0)).xyz); ALBEDO = albedo; ROUGHNESS = roughness; SPECULAR = snowy || og_snow() ? 0.5 * spec : 0.0; EMISSION = albedo * og_volcano_glow(world, normal); if (has_level_sky) { FOG = og_fog(world, CAMERA_POSITION_WORLD, TIME, false); } }