Skip to content
0.2.0-dev Download ZIP
85 lines ·4.0 KB Raw
1shader_type spatial;
2
3uniform sampler2D colour_map : source_color, hint_default_white, filter_linear_mipmap_anisotropic, repeat_disable;
4uniform sampler2D normal_map : hint_normal, filter_linear_mipmap, repeat_disable;
5uniform sampler2D weight_map : hint_default_black, filter_linear_mipmap, repeat_disable;
6uniform sampler2D perturb : hint_default_white, filter_linear_mipmap, repeat_enable;
7uniform sampler2D detail_colour_0 : source_color, hint_default_white, filter_linear_mipmap_anisotropic, repeat_enable;
8uniform sampler2D detail_colour_1 : source_color, hint_default_white, filter_linear_mipmap_anisotropic, repeat_enable;
9uniform sampler2D detail_colour_2 : source_color, hint_default_white, filter_linear_mipmap_anisotropic, repeat_enable;
10uniform sampler2D detail_colour_3 : source_color, hint_default_white, filter_linear_mipmap_anisotropic, repeat_enable;
11uniform sampler2D detail_normal_0 : hint_normal, filter_linear_mipmap_anisotropic, repeat_enable;
12uniform sampler2D detail_normal_1 : hint_normal, filter_linear_mipmap_anisotropic, repeat_enable;
13uniform sampler2D detail_normal_2 : hint_normal, filter_linear_mipmap_anisotropic, repeat_enable;
14uniform sampler2D detail_normal_3 : hint_normal, filter_linear_mipmap_anisotropic, repeat_enable;
15uniform bool height_blend = false;
16uniform bool snowy = false;
17
18#include "level.gdshaderinc"
19uniform bool has_perturb = false;
20
21varying vec3 model_position;
22
23void vertex() {
24 model_position = VERTEX;
25}
26
27vec3 unpack_tangent(vec4 texel) {
28 return normalize(vec3(2.0 * texel.x - 1.0, 1.0 - 2.0 * texel.y, texel.z));
29}
30
31vec4 average(sampler2D map) {
32 return textureLod(map, vec2(0.5), 20.0);
33}
34
35void fragment() {
36 vec2 base_uv = UV;
37 if (has_perturb) {
38 base_uv += (texture(perturb, UV * 200.0).xy - 0.5) * 0.001;
39 }
40 vec2 detail_uv = vec2(0.1 * model_position.x, 1.0 - 0.1 * model_position.z);
41 vec4 w = texture(weight_map, base_uv);
42 w.a = max(0.0, 1.0 - (w.r + w.g + w.b));
43 vec4 n0 = texture(detail_normal_0, detail_uv);
44 vec4 n1 = texture(detail_normal_1, detail_uv);
45 vec4 n2 = texture(detail_normal_2, detail_uv);
46 vec4 n3 = texture(detail_normal_3, detail_uv);
47 if (height_blend) {
48 w = pow(w + vec4(n0.a, n1.a, n2.a, n3.a), vec4(16.0));
49 }
50 w /= max(dot(w, vec4(1.0)), 1e-5);
51 float fade = clamp(length(VERTEX) / 200.0, 0.0, 1.0);
52 vec3 base_normal = normalize(texture(normal_map, base_uv).xyz * 2.0 - 1.0);
53 vec3 bitangent = normalize(cross(vec3(1.0, 0.0, 0.0), base_normal));
54 vec3 tangent = normalize(cross(base_normal, bitangent));
55 vec4 normal_blend = n0 * w.r + n1 * w.g + n2 * w.b + n3 * w.a;
56 vec3 world_normal = normalize(mat3(tangent, bitangent, base_normal) * mix(unpack_tangent(normal_blend), vec3(0.0, 0.0, 1.0), fade));
57 vec4 base = texture(colour_map, base_uv);
58 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));
59 vec4 tint = base / avg;
60 tint.a = 1.0;
61 vec4 blended = texture(detail_colour_0, detail_uv) * w.r + texture(detail_colour_1, detail_uv) * w.g
62 + texture(detail_colour_2, detail_uv) * w.b + texture(detail_colour_3, detail_uv) * w.a;
63 vec4 colour = mix(blended * tint, base, fade);
64 vec3 world = CAMERA_POSITION_WORLD + (INV_VIEW_MATRIX * vec4(VERTEX, 0.0)).xyz;
65 vec3 albedo = colour.rgb;
66 float roughness = 0.99;
67 float spec = blended.a;
68 if (snowy) {
69 roughness = mix(mix(0.99, 0.7, w.g), 0.2, w.r);
70 spec = mix(mix(spec, 0.4, w.g), 0.5, w.r);
71 albedo *= mix(1.0, 0.25, w.b) * mix(1.0, 0.5, w.a) * mix(1.0, 0.8, w.r);
72 }
73 vec3 normal = normalize(mat3(MODEL_MATRIX) * world_normal);
74 float dist = length(world - CAMERA_POSITION_WORLD);
75 og_surface(albedo, roughness, spec, normal, world, dist, TIME, true, false);
76 albedo *= 1.0 + og_caustics(world, normal, TIME) * level_sun_colour;
77 NORMAL = normalize((VIEW_MATRIX * vec4(normal, 0.0)).xyz);
78 ALBEDO = albedo;
79 ROUGHNESS = roughness;
80 SPECULAR = snowy || og_snow() ? 0.5 * spec : 0.0;
81 EMISSION = albedo * og_volcano_glow(world, normal);
82 if (has_level_sky) {
83 FOG = og_fog(world, CAMERA_POSITION_WORLD, TIME, false);
84 }
85}