From aefc61a1aff091e31436c60d004531b5905cecba Mon Sep 17 00:00:00 2001 From: hop311 Date: Fri, 17 Nov 2023 18:45:15 +0000 Subject: Cosmetic terrain map colour tinting --- game/src/Game/GameSession/TerrainMap.gdshader | 72 +++++++++++++++++++++------ game/src/Game/GlobalClass/ShaderManager.gd | 37 ++++++++++---- 2 files changed, 83 insertions(+), 26 deletions(-) (limited to 'game/src/Game') diff --git a/game/src/Game/GameSession/TerrainMap.gdshader b/game/src/Game/GameSession/TerrainMap.gdshader index ff8708a..35a108b 100644 --- a/game/src/Game/GameSession/TerrainMap.gdshader +++ b/game/src/Game/GameSession/TerrainMap.gdshader @@ -15,13 +15,23 @@ uniform sampler2DArray terrain_tex: source_color, repeat_enable, filter_linear; // The number of times the terrain textures should tile vertically uniform float terrain_tile_factor; // Map stripe mask texture -uniform sampler2D stripe_tex: source_color, repeat_enable, filter_nearest; +uniform sampler2D stripe_tex: source_color, repeat_enable, filter_linear; // The number of times the stripe texture should tile vertically uniform float stripe_tile_factor; +// Land map tint +uniform sampler2D colormap_land_tex: source_color, repeat_enable, filter_linear; +// Water map tint +uniform sampler2D colormap_water_tex: source_color, repeat_enable, filter_linear; const vec3 highlight_colour = vec3(1.0); -vec3 get_terrain_colour(vec2 uv, vec2 corner, vec2 half_pixel_size, vec2 terrain_uv, vec2 stripe_uv) { +vec3 get_terrain_colour( + vec2 uv, vec2 corner, vec2 half_pixel_size, // Components for calculating province sampling UV + float stripe_mask, // Stripe mask value - between 0 (base) and 1 (stripe) + vec2 terrain_uv, // UV coordinates scaled for terrain texture tiling + vec3 land_tint_colour, vec3 water_tint_colour // Colours for tinting terrain +) { + uvec3 province_data = read_uvec3(fma(corner, half_pixel_size, uv)); uint province_index = uvec2_to_uint(province_data.rg); uint terrain_index = province_data.b; @@ -30,33 +40,63 @@ vec3 get_terrain_colour(vec2 uv, vec2 corner, vec2 half_pixel_size, vec2 terrain vec4 province_base_colour = texelFetch(province_colour_tex, ivec2(province_data.rg), 0); province_data.r += 1u; // Add 1 to "x coordinate" to move from base to strip colour vec4 province_stripe_colour = texelFetch(province_colour_tex, ivec2(province_data.rg), 0); - float stripe = texture(stripe_tex, stripe_uv).b; // Stripe mask value - 0 for base, 1 for stripe - vec4 province_colour = mix(province_base_colour, province_stripe_colour, stripe); + vec4 province_colour = mix(province_base_colour, province_stripe_colour, stripe_mask); vec3 terrain_colour = texture(terrain_tex, vec3(terrain_uv, float(terrain_index))).rgb; - vec3 mixed_colour = mix(terrain_colour, province_colour.rgb, province_colour.a); + vec3 tint_colour = mix(land_tint_colour, water_tint_colour, float(terrain_index == 0u)); + vec3 tinted_terrain_colour = mix(terrain_colour, tint_colour, 0.3); + vec3 mixed_colour = mix(tinted_terrain_colour, province_colour.rgb, province_colour.a); + + float highlight_mix_val = 0.1 * (float(province_index == hover_index) + float(province_index == selected_index)); + return mix(mixed_colour, highlight_colour, highlight_mix_val); +} - float mix_val = 0.1 * (float(province_index == hover_index) + float(province_index == selected_index)); - return mix(mixed_colour, highlight_colour, mix_val); +// Rescale UV coordinates to remove squashing caused by normalisation +vec2 denormalise(vec2 uv, vec2 dims) { + return vec2(uv.x * dims.x / dims.y, uv.y); } vec3 mix_terrain_colour(vec2 uv) { vec2 map_size = vec2(textureSize(province_shape_tex, 0).xy) * province_shape_subdivisions; - vec2 pixel_offset = fract(fma(uv, map_size, vec2(0.5))); + vec2 uv_centred = fma(uv, map_size, vec2(0.5)); + vec2 pixel_offset = fract(uv_centred); vec2 half_pixel_size = 0.49 / map_size; - // UV coords adjusted to remove squashing caused by normalisation relative to map dimensions. - vec2 unscaled_uv = vec2(uv.x * map_size.x / map_size.y, uv.y); + // UV coords adjusted to remove squashing caused by normalisation relative to map dimensions + vec2 unscaled_uv = denormalise(uv, map_size); - vec2 terrain_uv = unscaled_uv * terrain_tile_factor; vec2 stripe_uv = unscaled_uv * stripe_tile_factor; + // Stripe mask value - between 0 (base) and 1 (stripe) + float stripe_mask = texture(stripe_tex, stripe_uv).b; + + vec2 terrain_uv = unscaled_uv * terrain_tile_factor; + vec2 colormap_uv = vec2(uv.x, 1.0 - uv.y); + vec3 colormap_land_colour = texture(colormap_land_tex, colormap_uv).rgb; + vec3 colormap_water_colour = texture(colormap_water_tex, colormap_uv).rgb; return mix( - mix(get_terrain_colour(uv, vec2(-1, -1), half_pixel_size, terrain_uv, stripe_uv), - get_terrain_colour(uv, vec2(+1, -1), half_pixel_size, terrain_uv, stripe_uv), pixel_offset.x), - mix(get_terrain_colour(uv, vec2(-1, +1), half_pixel_size, terrain_uv, stripe_uv), - get_terrain_colour(uv, vec2(+1, +1), half_pixel_size, terrain_uv, stripe_uv), pixel_offset.x), - pixel_offset.y); + mix( + get_terrain_colour( + uv, vec2(-1, -1), half_pixel_size, stripe_mask, + terrain_uv, colormap_land_colour, colormap_water_colour + ), + get_terrain_colour( + uv, vec2(+1, -1), half_pixel_size, stripe_mask, + terrain_uv, colormap_land_colour, colormap_water_colour + ), pixel_offset.x + ), + mix( + get_terrain_colour( + uv, vec2(-1, +1), half_pixel_size, stripe_mask, + terrain_uv, colormap_land_colour, colormap_water_colour + ), + get_terrain_colour( + uv, vec2(+1, +1), half_pixel_size, stripe_mask, + terrain_uv, colormap_land_colour, colormap_water_colour + ), pixel_offset.x + ), + pixel_offset.y + ); } void fragment() { diff --git a/game/src/Game/GlobalClass/ShaderManager.gd b/game/src/Game/GlobalClass/ShaderManager.gd index 1af606c..ace3c3a 100644 --- a/game/src/Game/GlobalClass/ShaderManager.gd +++ b/game/src/Game/GlobalClass/ShaderManager.gd @@ -10,6 +10,16 @@ const param_terrain_tex : StringName = &"terrain_tex" const param_terrain_tile_factor : StringName = &"terrain_tile_factor" const param_stripe_tex : StringName = &"stripe_tex" const param_stripe_tile_factor : StringName = &"stripe_tile_factor" +const param_colormap_land_tex : StringName = &"colormap_land_tex" +const param_colormap_water_tex : StringName = &"colormap_water_tex" + +func _set_shader_texture(shader_material : ShaderMaterial, texture_path : StringName, texture_param : StringName) -> Error: + var texture := AssetManager.get_texture(texture_path) + if texture == null: + push_error("Failed to get texture: ", texture_path) + return FAILED + shader_material.set_shader_parameter(texture_param, texture) + return OK func set_up_shader(material : Material, add_cosmetic_textures : bool) -> Error: # Shader Material @@ -21,38 +31,45 @@ func set_up_shader(material : Material, add_cosmetic_textures : bool) -> Error: return FAILED var shader_material : ShaderMaterial = material + var ret : Error = OK + # Province shape texture var province_shape_texture := GameSingleton.get_province_shape_texture() if province_shape_texture == null: push_error("Failed to get province shape texture!") - return FAILED + ret = FAILED shader_material.set_shader_parameter(param_province_shape_tex, province_shape_texture) var subdivisions := GameSingleton.get_province_shape_image_subdivisions() if subdivisions.x < 1 or subdivisions.y < 1: push_error("Invalid province shape image subdivision: ", subdivisions.x, "x", subdivisions.y) - return FAILED + ret = FAILED shader_material.set_shader_parameter(param_province_shape_subdivisions, Vector2(subdivisions)) if add_cosmetic_textures: + # Province colour texture var map_province_colour_texture := GameSingleton.get_province_colour_texture() if map_province_colour_texture == null: push_error("Failed to get province colour image!") - return FAILED + ret = FAILED shader_material.set_shader_parameter(param_province_colour_tex, map_province_colour_texture) # Terrain texture var terrain_texture := GameSingleton.get_terrain_texture() if terrain_texture == null: push_error("Failed to get terrain texture!") - return FAILED + ret = FAILED shader_material.set_shader_parameter(param_terrain_tex, terrain_texture) # Stripe texture - var stripe_texture := AssetManager.get_texture(&"map/terrain/stripes.dds") - if stripe_texture == null: - push_error("Failed to get stripe texture!") - return FAILED - shader_material.set_shader_parameter(param_stripe_tex, stripe_texture) + if _set_shader_texture(shader_material, &"map/terrain/stripes.dds", param_stripe_tex) != OK: + ret = FAILED - return OK + # Land colormap + if _set_shader_texture(shader_material, &"map/terrain/colormap.dds", param_colormap_land_tex) != OK: + ret = FAILED + # Water colormap + if _set_shader_texture(shader_material, &"map/terrain/colormap_water.dds", param_colormap_water_tex) != OK: + ret = FAILED + + return ret -- cgit v1.2.3-56-ga3b1