shader_type spatial; uniform sampler2D colour_map : source_color, hint_default_white, filter_linear_mipmap, repeat_enable; uniform sampler2D normal_map : hint_normal, filter_linear_mipmap_anisotropic, repeat_enable; uniform sampler2D screen : hint_screen_texture, filter_linear_mipmap, repeat_disable; uniform sampler2D depth : hint_depth_texture, filter_nearest, repeat_disable; uniform vec3 colour_tint = vec3(1.0); instance uniform vec3 evil_colour = vec3(1.0); #include "level.gdshaderinc" varying vec3 model_normal; varying vec3 model_tangent; varying vec3 model_bitangent; void vertex() { model_normal = NORMAL; model_tangent = TANGENT; model_bitangent = CUSTOM0.xyz; } float distance_at(vec2 uv, mat4 inverse_projection) { vec4 view = inverse_projection * vec4(uv * 2.0 - 1.0, texture(depth, uv).r, 1.0); return -view.z / view.w; } void fragment() { vec3 tint = evil_colour * colour_tint; mat3 rotation = mat3(normalize(MODEL_MATRIX[0].xyz), normalize(MODEL_MATRIX[1].xyz), normalize(MODEL_MATRIX[2].xyz)); vec3 n = normalize(model_normal); vec3 t = normalize(model_tangent - n * dot(n, model_tangent)); vec3 b = normalize(model_bitangent); vec4 texel = texture(normal_map, UV); vec3 tangent_normal = normalize(vec3(2.0 * texel.x - 1.0, 1.0 - 2.0 * texel.y, texel.z)); vec3 normal = normalize((VIEW_MATRIX * vec4(rotation * (mat3(t, b, n) * tangent_normal), 0.0)).xyz); if (!FRONT_FACING) { normal = -normal; } float water_depth = -VERTEX.z; float old_depth = distance_at(SCREEN_UV, INV_PROJECTION_MATRIX) - water_depth; vec2 distort = tangent_normal.xy * 0.1 / max(0.1, tint.g); distort *= clamp(old_depth, 0.0, 1.0); distort /= water_depth + 0.3; vec2 uv = SCREEN_UV + distort; float behind = distance_at(uv, INV_PROJECTION_MATRIX) - water_depth; if (behind < 0.25) { uv -= distort * 0.5; behind = distance_at(uv, INV_PROJECTION_MATRIX) - water_depth; } if (behind < 0.125) { uv -= distort * 0.5; behind = distance_at(uv, INV_PROJECTION_MATRIX) - water_depth; } vec3 under = textureLod(screen, uv, 0.0).rgb; if (FRONT_FACING) { vec3 deep = define_swamp ? vec3(188.0, 152.0, 98.0) / 255.0 * 0.05 : define_cave_arena ? vec3(0.06, 0.09, 0.06) : vec3(0.02, 0.03, 0.02); float thickness = define_swamp ? 2.0 : define_cave_arena ? 8.0 : 0.3; under = mix(under, deep, clamp(pow(max(behind, 0.0) * thickness * tint.b, 0.2), 0.0, 1.0)); } float glancing = clamp(1.0 + dot(normalize(VERTEX), normal), 0.0, 1.0); float fresnel = !FRONT_FACING ? pow(glancing, 0.2) : define_more_reflect ? pow(glancing, 2.0) : define_cave_arena ? pow(glancing, 3.0) : 0.8 * pow(glancing, 5.0); NORMAL = normal; ALBEDO = vec3(0.0); EMISSION = under * (1.0 - fresnel); ROUGHNESS = tint.r * 0.3; SPECULAR = 0.25; ALPHA = 1.0; if (has_level_sky) { vec3 world = CAMERA_POSITION_WORLD + (INV_VIEW_MATRIX * vec4(VERTEX, 0.0)).xyz; FOG = og_fog(world, CAMERA_POSITION_WORLD, TIME, true); } }