aboutsummaryrefslogtreecommitdiff
path: root/game/addons/zylann.hterrain/shaders/array_global.gdshader
blob: b72dc5343d2f3d9515fee571f9abcd68cfae52dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// This shader is used to bake the global albedo map.
// It exposes a subset of the main shader API, so uniform names were not modified.

shader_type spatial;

// I had to remove source_color` from colormap in Godot 3 because it makes sRGB conversion kick in,
// which snowballs to black when doing GPU painting on that texture...
uniform sampler2D u_terrain_colormap;
uniform sampler2D u_terrain_splat_index_map;
uniform sampler2D u_terrain_splat_weight_map;

uniform sampler2DArray u_ground_albedo_bump_array : source_color;

// TODO Have UV scales for each texture in an array?
uniform float u_ground_uv_scale;
// Keep depth blending because it has a high effect on the final result
uniform bool u_depth_blending = true;


vec3 get_depth_blended_weights(vec3 splat, vec3 bumps) {
   float dh = 0.2;

   vec3 h = bumps + splat;

   // TODO Keep improving multilayer blending, there are still some edge cases...
   // Mitigation: nullify layers with near-zero splat
   h *= smoothstep(0, 0.05, splat);

   vec3 d = h + dh;
   d.r -= max(h.g, h.b);
   d.g -= max(h.r, h.b);
   d.b -= max(h.g, h.r);

   vec3 w = clamp(d, 0, 1);
   // Had to normalize, since this approach does not preserve components summing to 1
   return w / (w.x + w.y + w.z);
}

void vertex() {
   vec4 wpos = MODEL_MATRIX * vec4(VERTEX, 1);
   vec2 cell_coords = wpos.xz;
   // Must add a half-offset so that we sample the center of pixels,
   // otherwise bilinear filtering of the textures will give us mixed results (#183)
   cell_coords += vec2(0.5);

   // Normalized UV
   UV = (cell_coords / vec2(textureSize(u_terrain_splat_index_map, 0)));
}

void fragment() {
   vec4 tint = texture(u_terrain_colormap, UV);
   vec4 tex_splat_indexes = texture(u_terrain_splat_index_map, UV);
   vec4 tex_splat_weights = texture(u_terrain_splat_weight_map, UV);
   // TODO Can't use texelFetch!
   // https://github.com/godotengine/godot/issues/31732
   
   vec3 splat_indexes = tex_splat_indexes.rgb * 255.0;

   // Get bump at normal resolution so depth blending is accurate
   vec2 ground_uv = UV / u_ground_uv_scale;
   float b0 = texture(u_ground_albedo_bump_array, vec3(ground_uv, splat_indexes.x)).a;
   float b1 = texture(u_ground_albedo_bump_array, vec3(ground_uv, splat_indexes.y)).a;
   float b2 = texture(u_ground_albedo_bump_array, vec3(ground_uv, splat_indexes.z)).a;

   // Take the center of the highest mip as color, because we can't see details from far away.
   vec2 ndc_center = vec2(0.5, 0.5);
   vec3 a0 = textureLod(u_ground_albedo_bump_array, vec3(ndc_center, splat_indexes.x), 10.0).rgb;
   vec3 a1 = textureLod(u_ground_albedo_bump_array, vec3(ndc_center, splat_indexes.y), 10.0).rgb;
   vec3 a2 = textureLod(u_ground_albedo_bump_array, vec3(ndc_center, splat_indexes.z), 10.0).rgb;

   vec3 splat_weights = vec3(
      tex_splat_weights.r, 
      tex_splat_weights.g,
      1.0 - tex_splat_weights.r - tex_splat_weights.g
   );
   
   // TODO An #ifdef macro would be nice! Or copy/paste everything in a different shader...
   if (u_depth_blending) {
      splat_weights = get_depth_blended_weights(splat_weights, vec3(b0, b1, b2));
   }

   ALBEDO = tint.rgb * (
        a0 * splat_weights.x 
      + a1 * splat_weights.y
      + a2 * splat_weights.z
   );
}