Skip to content
0.2.0-dev Download ZIP
76 lines ·2.9 KB Raw
1shader_type spatial;
2
3uniform sampler2D colour_map : source_color, hint_default_white, filter_linear_mipmap, repeat_enable;
4uniform sampler2D normal_map : hint_normal, filter_linear_mipmap_anisotropic, repeat_enable;
5uniform sampler2D screen : hint_screen_texture, filter_linear_mipmap, repeat_disable;
6uniform sampler2D depth : hint_depth_texture, filter_nearest, repeat_disable;
7uniform vec3 colour_tint = vec3(1.0);
8instance uniform vec3 evil_colour = vec3(1.0);
9
10#include "level.gdshaderinc"
11
12varying vec3 model_normal;
13varying vec3 model_tangent;
14varying vec3 model_bitangent;
15
16void vertex() {
17 model_normal = NORMAL;
18 model_tangent = TANGENT;
19 model_bitangent = CUSTOM0.xyz;
20}
21
22float distance_at(vec2 uv, mat4 inverse_projection) {
23 vec4 view = inverse_projection * vec4(uv * 2.0 - 1.0, texture(depth, uv).r, 1.0);
24 return -view.z / view.w;
25}
26
27void fragment() {
28 vec3 tint = evil_colour * colour_tint;
29 mat3 rotation = mat3(normalize(MODEL_MATRIX[0].xyz), normalize(MODEL_MATRIX[1].xyz), normalize(MODEL_MATRIX[2].xyz));
30 vec3 n = normalize(model_normal);
31 vec3 t = normalize(model_tangent - n * dot(n, model_tangent));
32 vec3 b = normalize(model_bitangent);
33 vec4 texel = texture(normal_map, UV);
34 vec3 tangent_normal = normalize(vec3(2.0 * texel.x - 1.0, 1.0 - 2.0 * texel.y, texel.z));
35 vec3 normal = normalize((VIEW_MATRIX * vec4(rotation * (mat3(t, b, n) * tangent_normal), 0.0)).xyz);
36 if (!FRONT_FACING) {
37 normal = -normal;
38 }
39
40 float water_depth = -VERTEX.z;
41 float old_depth = distance_at(SCREEN_UV, INV_PROJECTION_MATRIX) - water_depth;
42 vec2 distort = tangent_normal.xy * 0.1 / max(0.1, tint.g);
43 distort *= clamp(old_depth, 0.0, 1.0);
44 distort /= water_depth + 0.3;
45 vec2 uv = SCREEN_UV + distort;
46 float behind = distance_at(uv, INV_PROJECTION_MATRIX) - water_depth;
47 if (behind < 0.25) {
48 uv -= distort * 0.5;
49 behind = distance_at(uv, INV_PROJECTION_MATRIX) - water_depth;
50 }
51 if (behind < 0.125) {
52 uv -= distort * 0.5;
53 behind = distance_at(uv, INV_PROJECTION_MATRIX) - water_depth;
54 }
55
56 vec3 under = textureLod(screen, uv, 0.0).rgb;
57 if (FRONT_FACING) {
58 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);
59 float thickness = define_swamp ? 2.0 : define_cave_arena ? 8.0 : 0.3;
60 under = mix(under, deep, clamp(pow(max(behind, 0.0) * thickness * tint.b, 0.2), 0.0, 1.0));
61 }
62
63 float glancing = clamp(1.0 + dot(normalize(VERTEX), normal), 0.0, 1.0);
64 float fresnel = !FRONT_FACING ? pow(glancing, 0.2) : define_more_reflect ? pow(glancing, 2.0)
65 : define_cave_arena ? pow(glancing, 3.0) : 0.8 * pow(glancing, 5.0);
66 NORMAL = normal;
67 ALBEDO = vec3(0.0);
68 EMISSION = under * (1.0 - fresnel);
69 ROUGHNESS = tint.r * 0.3;
70 SPECULAR = 0.25;
71 ALPHA = 1.0;
72 if (has_level_sky) {
73 vec3 world = CAMERA_POSITION_WORLD + (INV_VIEW_MATRIX * vec4(VERTEX, 0.0)).xyz;
74 FOG = og_fog(world, CAMERA_POSITION_WORLD, TIME, true);
75 }
76}