aboutsummaryrefslogtreecommitdiff
path: root/game/src/GameSession/TerrainMap.gdshader
diff options
context:
space:
mode:
author Hop311 <Hop3114@gmail.com>2023-04-18 19:55:10 +0200
committer GitHub <noreply@github.com>2023-04-18 19:55:10 +0200
commitea077c8a7c78477bd247c7fbd21de13bcf2285e9 (patch)
tree0cc2be6ef3ab1f2af5c2806f60abe988ca6aa7b5 /game/src/GameSession/TerrainMap.gdshader
parent258a088018d36e987b3ffe4a9b418a6c21ad9217 (diff)
parent1fdd198f943a41468b03b2cdc62c24147f707239 (diff)
Merge pull request #88 from OpenVic2Project/map-drawing
Further Map Stuff
Diffstat (limited to 'game/src/GameSession/TerrainMap.gdshader')
-rw-r--r--game/src/GameSession/TerrainMap.gdshader50
1 files changed, 37 insertions, 13 deletions
diff --git a/game/src/GameSession/TerrainMap.gdshader b/game/src/GameSession/TerrainMap.gdshader
index 7aca0f9..9ce1a24 100644
--- a/game/src/GameSession/TerrainMap.gdshader
+++ b/game/src/GameSession/TerrainMap.gdshader
@@ -2,16 +2,18 @@ shader_type spatial;
render_mode unshaded;
-// Cosmetic terrain texture
-uniform sampler2D terrain_tex: source_color, repeat_enable, filter_linear;
+// Cosmetic farmlands terrain texture
+uniform sampler2D farmlands_tex: source_color, repeat_enable, filter_linear;
// Province index texture
-uniform sampler2D province_index_tex : repeat_enable, filter_nearest;
+uniform sampler2D province_index_tex : source_color, repeat_enable, filter_nearest;
// Province colour texture
uniform sampler2D province_colour_tex: source_color, repeat_enable, filter_nearest;
// Index of the mouse over the map mesh
uniform uint hover_index;
// Index of the currently selected province
uniform uint selected_index;
+// The number of times the terrain textures should tile vertically
+uniform float terrain_tile_factor;
uvec2 vec2_to_uvec2(vec2 v) {
return uvec2(v * 255.0);
@@ -29,17 +31,39 @@ uint read_uint16(sampler2D tex, vec2 uv) {
return uvec2_to_uint(read_uvec2(tex, uv));
}
-void fragment() {
- uvec2 prov_idx_split = read_uvec2(province_index_tex, UV);
- uint prov_index = uvec2_to_uint(prov_idx_split);
+const vec3 water_colour = vec3(0, 0, 1);
+
+vec3 get_terrain_colour(vec2 uv, vec2 corner, vec2 half_pixel_size, vec2 terrain_uv) {
+ uvec2 index_split = read_uvec2(province_index_tex, fma(corner, half_pixel_size, uv));
+ uint index = uvec2_to_uint(index_split);
+ vec4 province_data = texelFetch(province_colour_tex, ivec2(index_split), 0);
+ vec3 province_colour = province_data.rgb;
+ float is_land = province_data.a;
+ vec3 farmlands_colour = texture(farmlands_tex, terrain_uv).rgb;
+ vec3 terrain_colour = mix(water_colour, farmlands_colour, is_land);
+ float mix_val = 0.4 + float(index == hover_index) * 0.2 + float(index == selected_index) * 0.2;
+ vec3 mixed_colour = mix(terrain_colour, province_colour, mix_val);
+ return mixed_colour;
+}
- // Boost prov_colour's contribution if it matches hover_colour or selected_colour
- float mix_val = 0.3 + float(prov_index == hover_index) * 0.3 + float(prov_index == selected_index) * 0.3;
- // Don't mix if the province index is 0
- mix_val *= float(prov_index != 0u);
+vec3 mix_terrain_colour(vec2 uv) {
+ vec2 map_size = vec2(textureSize(province_index_tex, 0));
+ vec2 pixel_offset = mod(fma(uv, map_size, vec2(0.5)), 1.0);
+ vec2 half_pixel_size = 0.49 / map_size;
- vec3 terrain_colour = texture(terrain_tex, UV).rgb;
- vec3 province_colour = texelFetch(province_colour_tex, ivec2(prov_idx_split), 0).rgb;
+ vec2 terrain_uv = uv;
+ terrain_uv.x *= map_size.x / map_size.y;
+ terrain_uv *= terrain_tile_factor;
- ALBEDO = mix(terrain_colour, province_colour, mix_val);
+ return mix(
+ mix(get_terrain_colour(uv, vec2(-1, -1), half_pixel_size, terrain_uv),
+ get_terrain_colour(uv, vec2(+1, -1), half_pixel_size, terrain_uv), pixel_offset.x),
+ mix(get_terrain_colour(uv, vec2(-1, +1), half_pixel_size, terrain_uv),
+ get_terrain_colour(uv, vec2(+1, +1), half_pixel_size, terrain_uv), pixel_offset.x),
+ pixel_offset.y);
+}
+
+void fragment() {
+ vec3 terrain_colour = mix_terrain_colour(UV);
+ ALBEDO = terrain_colour;
}