diff options
author | Hop311 <hop3114@gmail.com> | 2023-04-07 01:42:53 +0200 |
---|---|---|
committer | Hop311 <hop3114@gmail.com> | 2023-04-07 01:42:53 +0200 |
commit | 513a1328edb89ac695c70164158933aee4546cd7 (patch) | |
tree | 8d979d4e8c728bb80543aa13e037a328829122fd | |
parent | 14c2358c7837e644349ea37866607fcd1459d2bc (diff) |
Province index and colour lookup texture
-rw-r--r-- | extension/src/MapMesh.cpp | 5 | ||||
-rw-r--r-- | extension/src/MapMesh.hpp | 1 | ||||
-rw-r--r-- | extension/src/MapSingleton.cpp | 125 | ||||
-rw-r--r-- | extension/src/MapSingleton.hpp | 10 | ||||
-rw-r--r-- | extension/src/openvic2/Map.cpp | 37 | ||||
-rw-r--r-- | extension/src/openvic2/Map.hpp | 11 | ||||
-rw-r--r-- | game/src/GameSession/MapView.gd | 37 | ||||
-rw-r--r-- | game/src/GameSession/TerrainMap.gdshader | 46 |
8 files changed, 224 insertions, 48 deletions
diff --git a/extension/src/MapMesh.cpp b/extension/src/MapMesh.cpp index 2fa6be4..cdbeec9 100644 --- a/extension/src/MapMesh.cpp +++ b/extension/src/MapMesh.cpp @@ -19,6 +19,7 @@ void MapMesh::_bind_methods() { ClassDB::bind_method(D_METHOD("get_subdivide_depth"), &MapMesh::get_subdivide_depth); ClassDB::bind_method(D_METHOD("get_core_aabb"), &MapMesh::get_core_aabb); + ClassDB::bind_method(D_METHOD("is_valid_uv_coord"), &MapMesh::is_valid_uv_coord); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "aspect_ratio", PROPERTY_HINT_NONE, "suffix:m"), "set_aspect_ratio", "get_aspect_ratio"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "repeat_proportion", PROPERTY_HINT_NONE, "suffix:m"), "set_repeat_proportion", "get_repeat_proportion"); @@ -72,6 +73,10 @@ AABB MapMesh::get_core_aabb() const { return AABB{ size * -0.5f, size }; } +bool MapMesh::is_valid_uv_coord(godot::Vector2 const& uv) const { + return -repeat_proportion <= uv.x && uv.x <= 1.0f + repeat_proportion && 0.0f <= uv.y && uv.y <= 1.0f; +} + Array MapMesh::_create_mesh_array() const { Array arr; arr.resize(Mesh::ARRAY_MAX); diff --git a/extension/src/MapMesh.hpp b/extension/src/MapMesh.hpp index 5427894..d8727cf 100644 --- a/extension/src/MapMesh.hpp +++ b/extension/src/MapMesh.hpp @@ -27,6 +27,7 @@ namespace OpenVic2 { int get_subdivide_depth() const; godot::AABB get_core_aabb() const; + bool is_valid_uv_coord(godot::Vector2 const& uv) const; godot::Array _create_mesh_array() const override; }; diff --git a/extension/src/MapSingleton.cpp b/extension/src/MapSingleton.cpp index 9d13496..71977da 100644 --- a/extension/src/MapSingleton.cpp +++ b/extension/src/MapSingleton.cpp @@ -12,8 +12,11 @@ MapSingleton* MapSingleton::singleton = nullptr; void MapSingleton::_bind_methods() { ClassDB::bind_method(D_METHOD("load_province_identifier_file", "file_path"), &MapSingleton::load_province_identifier_file); ClassDB::bind_method(D_METHOD("load_province_shape_file", "file_path"), &MapSingleton::load_province_shape_file); - ClassDB::bind_method(D_METHOD("get_province_shape_image"), &MapSingleton::get_province_shape_image); - ClassDB::bind_method(D_METHOD("get_province_identifier_from_colour", "colour"), &MapSingleton::get_province_identifier_from_colour); + ClassDB::bind_method(D_METHOD("get_province_identifier_from_pixel_coords", "coords"), &MapSingleton::get_province_identifier_from_pixel_coords); + ClassDB::bind_method(D_METHOD("get_width"), &MapSingleton::get_width); + ClassDB::bind_method(D_METHOD("get_height"), &MapSingleton::get_height); + ClassDB::bind_method(D_METHOD("get_province_index_image"), &MapSingleton::get_province_index_image); + ClassDB::bind_method(D_METHOD("get_province_colour_image"), &MapSingleton::get_province_colour_image); } MapSingleton* MapSingleton::get_singleton() { @@ -105,20 +108,17 @@ Error MapSingleton::load_province_identifier_file(String const& file_path) { continue; } std::string error_message; - if (map.add_province(identifier.utf8().get_data(), colour, error_message)) { - UtilityFunctions::print(error_message.c_str()); - } else { + if (!map.add_province(identifier.utf8().get_data(), colour, error_message)) { UtilityFunctions::push_error(error_message.c_str()); err = FAILED; } } + map.lock_provinces(); return err; } -String MapSingleton::get_province_identifier_from_colour(Province::colour_t colour) { - const Province* province = map.get_province_by_colour(colour); - if (province) return province->get_identifier().c_str(); - else return String{}; +static Province::colour_t colour_at(PackedByteArray const& colour_data_array, int32_t idx) { + return (colour_data_array[idx * 3] << 16) | (colour_data_array[idx * 3 + 1] << 8) | colour_data_array[idx * 3 + 2]; } Error MapSingleton::load_province_shape_file(String const& file_path) { @@ -133,13 +133,13 @@ Error MapSingleton::load_province_shape_file(String const& file_path) { province_shape_image.unref(); return err; } - int32_t width = province_shape_image->get_width(); - int32_t height = province_shape_image->get_height(); + width = province_shape_image->get_width(); + height = province_shape_image->get_height(); if (width < 1 || height < 1) { UtilityFunctions::push_error("Invalid dimensions (", width, "x", height, ") for province shape file: ", file_path); err = FAILED; } - static const Image::Format expected_format = Image::Format::FORMAT_RGB8; + static const Image::Format expected_format = Image::FORMAT_RGB8; Image::Format format = province_shape_image->get_format(); if (format != expected_format) { UtilityFunctions::push_error("Invalid format (", format, ", should be ", expected_format, ") for province shape file: ", file_path); @@ -149,9 +149,106 @@ Error MapSingleton::load_province_shape_file(String const& file_path) { province_shape_image.unref(); return err; } + + std::vector<bool> province_checklist(map.get_province_count()); + + PackedByteArray shape_data_array = province_shape_image->get_data(), index_data_array; + index_data_array.resize(width * height * sizeof(Province::index_t)); + Province::index_t* index_data = reinterpret_cast<Province::index_t*>(index_data_array.ptrw()); + + for (int32_t y = 0; y < height; ++y) { + for (int32_t x = 0; x < width; ++x) { + const int32_t idx = x + y * width; + const Province::colour_t colour = colour_at(shape_data_array, idx); + if (colour == Province::NULL_COLOUR) { + index_data[idx] = Province::NULL_INDEX; + continue; + } + if (x > 0) { + const int32_t jdx = idx - 1; + if (colour_at(shape_data_array, jdx) == colour) { + index_data[idx] = index_data[jdx]; + continue; + } + } + if (y > 0) { + const int32_t jdx = idx - width; + if (colour_at(shape_data_array, jdx) == colour) { + index_data[idx] = index_data[jdx]; + continue; + } + } + const Province* province = map.get_province_by_colour(colour); + if (province) { + Province::index_t index = province->get_index(); + index_data[idx] = index; + province_checklist[index - 1] = true; + continue; + } + UtilityFunctions::push_error("Unrecognised province colour ", Province::colour_to_hex_string(colour).c_str(), " at (", x, ", ", y, ")"); + err = FAILED; + index_data[idx] = Province::NULL_INDEX; + } + } + + for (size_t idx = 0; idx < province_checklist.size(); ++idx) { + if (!province_checklist[idx]) { + Province* province = map.get_province_by_index(idx + 1); + if (province) UtilityFunctions::push_error("Province missing from shape image: ", province->to_string().c_str()); + else UtilityFunctions::push_error("Province missing for index: ", idx + 1); + err = FAILED; + } + } + + province_index_image = Image::create_from_data(width, height, false, Image::FORMAT_RG8, index_data_array); + if (province_index_image.is_null()) { + UtilityFunctions::push_error("Failed to create province ID image"); + err = FAILED; + } + + PackedByteArray colour_data_array; + colour_data_array.resize((Province::MAX_INDEX + 1) * 3); + for (int64_t idx = 1; idx <= map.get_province_count(); ++idx) { + const Province* province = map.get_province_by_index(idx); + if (province) { + const Province::colour_t colour = province->get_colour(); + colour_data_array[3 * idx + 0] = (colour >> 16) & 0xFF; + colour_data_array[3 * idx + 1] = (colour >> 8) & 0xFF; + colour_data_array[3 * idx + 2] = colour & 0xFF; + } else UtilityFunctions::push_error("Missing province at index ", idx); + } + static const int32_t PROVINCE_INDEX_SQRT = 1 << (sizeof(Province::index_t) * 4); + province_colour_image = Image::create_from_data(PROVINCE_INDEX_SQRT, PROVINCE_INDEX_SQRT, false, Image::FORMAT_RGB8, colour_data_array); + if (province_colour_image.is_null()) { + UtilityFunctions::push_error("Failed to create province colour image"); + err = FAILED; + } + return err; } -Ref<Image> MapSingleton::get_province_shape_image() const { - return province_shape_image; +String MapSingleton::get_province_identifier_from_pixel_coords(Vector2i const& coords) { + if (province_index_image.is_valid() && 0 <= coords.x && coords.x < width && 0 <= coords.y && coords.y < height) { + const PackedByteArray index_data_array = province_index_image->get_data(); + const Province::index_t* index_data = reinterpret_cast<const Province::index_t*>(index_data_array.ptr()); + const Province* province = map.get_province_by_colour(index_data[coords.x + coords.y * width]); + if (province) return province->get_identifier().c_str(); + } + return String{}; +} + +int32_t MapSingleton::get_width() const { + return width; +} + +int32_t MapSingleton::get_height() const { + return height; +} + +Ref<Image> MapSingleton::get_province_index_image() const { + return province_index_image; +} + +Ref<Image> MapSingleton::get_province_colour_image() const { + return province_colour_image; } diff --git a/extension/src/MapSingleton.hpp b/extension/src/MapSingleton.hpp index fb62e11..71761cd 100644 --- a/extension/src/MapSingleton.hpp +++ b/extension/src/MapSingleton.hpp @@ -10,7 +10,8 @@ namespace OpenVic2 { static MapSingleton* singleton; - godot::Ref<godot::Image> province_shape_image; + godot::Ref<godot::Image> province_shape_image, province_index_image, province_colour_image; + int32_t width = 0, height = 0; Map map; protected: @@ -24,7 +25,10 @@ namespace OpenVic2 { godot::Error load_province_identifier_file(godot::String const& file_path); godot::Error load_province_shape_file(godot::String const& file_path); - godot::String get_province_identifier_from_colour(Province::colour_t colour); - godot::Ref<godot::Image> get_province_shape_image() const; + godot::String get_province_identifier_from_pixel_coords(godot::Vector2i const& coords); + int32_t get_width() const; + int32_t get_height() const; + godot::Ref<godot::Image> get_province_index_image() const; + godot::Ref<godot::Image> get_province_colour_image() const; }; } diff --git a/extension/src/openvic2/Map.cpp b/extension/src/openvic2/Map.cpp index 542a876..d980b88 100644 --- a/extension/src/openvic2/Map.cpp +++ b/extension/src/openvic2/Map.cpp @@ -1,12 +1,17 @@ #include "Map.hpp" +#include <cassert> #include <sstream> #include <iomanip> using namespace OpenVic2; -Province::Province(std::string const& new_identifier, colour_t new_colour) : - identifier(new_identifier), colour(new_colour) {} +Province::Province(index_t new_index, std::string const& new_identifier, colour_t new_colour) : + index(new_index), identifier(new_identifier), colour(new_colour) { + assert(index != NULL_INDEX); + assert(!identifier.empty()); + assert(colour != NULL_COLOUR); +} std::string Province::colour_to_hex_string(colour_t colour) { std::ostringstream stream; @@ -14,6 +19,10 @@ std::string Province::colour_to_hex_string(colour_t colour) { return stream.str(); } +Province::index_t Province::get_index() const { + return index; +} + std::string const& Province::get_identifier() const { return identifier; } @@ -23,15 +32,23 @@ Province::colour_t Province::get_colour() const { } std::string Province::to_string() const { - return "(" + identifier + ", " + colour_to_hex_string(colour) + ")"; + return "(#" + std::to_string(index) + ", " + identifier + ", 0x" + colour_to_hex_string(colour) + ")"; } bool Map::add_province(std::string const& identifier, Province::colour_t colour, std::string& error_message) { + if (provinces_locked) { + error_message = "The map's province list has already been locked!"; + return false; + } + if (provinces.size() >= Province::MAX_INDEX) { + error_message = "The map's province list is full - there can be at most " + std::to_string(Province::MAX_INDEX) + " provinces"; + return false; + } if (colour == Province::NULL_COLOUR || colour > Province::MAX_COLOUR) { error_message = "Invalid province colour: " + Province::colour_to_hex_string(colour); return false; } - Province new_province{ identifier, colour }; + Province new_province{ static_cast<Province::index_t>(provinces.size() + 1), identifier, colour }; for (Province const& province : provinces) { if (province.identifier == identifier) { error_message = "Duplicate province identifiers: " + province.to_string() + " and " + new_province.to_string(); @@ -47,6 +64,18 @@ bool Map::add_province(std::string const& identifier, Province::colour_t colour, return true; } +void Map::lock_provinces() { + provinces_locked = true; +} + +size_t Map::get_province_count() const { + return provinces.size(); +} + +Province* Map::get_province_by_index(Province::index_t index) { + return index != Province::NULL_INDEX && index <= provinces.size() ? &provinces[index - 1] : nullptr; +} + Province* Map::get_province_by_identifier(std::string const& identifier) { for (Province& province : provinces) if (province.identifier == identifier) return &province; diff --git a/extension/src/openvic2/Map.hpp b/extension/src/openvic2/Map.hpp index f3fd1ef..365d78b 100644 --- a/extension/src/openvic2/Map.hpp +++ b/extension/src/openvic2/Map.hpp @@ -8,16 +8,20 @@ namespace OpenVic2 { struct Province { using colour_t = uint32_t; + using index_t = uint16_t; friend struct Map; static const colour_t NULL_COLOUR = 0, MAX_COLOUR = 0xFFFFFF; + static const index_t NULL_INDEX = 0, MAX_INDEX = 0xFFFF; private: + index_t index; std::string identifier; colour_t colour; - Province(std::string const& identifier, colour_t colour); + Province(index_t index, std::string const& identifier, colour_t colour); public: static std::string colour_to_hex_string(colour_t colour); + index_t get_index() const; std::string const& get_identifier() const; colour_t get_colour() const; std::string to_string() const; @@ -26,9 +30,14 @@ namespace OpenVic2 { struct Map { private: std::vector<Province> provinces; + bool provinces_locked = false; public: bool add_province(std::string const& identifier, Province::colour_t colour, std::string& error_message); + void lock_provinces(); + size_t get_province_count() const; + + Province* get_province_by_index(Province::index_t index); Province* get_province_by_identifier(std::string const& identifier); Province* get_province_by_colour(Province::colour_t colour); }; diff --git a/game/src/GameSession/MapView.gd b/game/src/GameSession/MapView.gd index 4a44bce..67938f2 100644 --- a/game/src/GameSession/MapView.gd +++ b/game/src/GameSession/MapView.gd @@ -11,7 +11,8 @@ const _action_zoomout : StringName = &"map_zoomout" const _action_drag : StringName = &"map_drag" const _action_click : StringName = &"map_click" -const _shader_param_provinces : StringName = &"province_tex" +const _shader_param_province_index : StringName = &"province_index_tex" +const _shader_param_province_colour : StringName = &"province_colour_tex" const _shader_param_hover_pos : StringName = &"hover_pos" const _shader_param_selected_pos : StringName = &"selected_pos" @@ -34,7 +35,8 @@ const _shader_param_selected_pos : StringName = &"selected_pos" @export var _map_mesh_instance : MeshInstance3D var _map_mesh : MapMesh var _map_shader_material : ShaderMaterial -var _map_province_shape_image : Image +var _map_image_size : Vector2 +var _map_province_index_image : Image var _map_mesh_corner : Vector2 var _map_mesh_dims : Vector2 @@ -49,17 +51,14 @@ func _ready(): if _map_mesh_instance == null: push_error("MapView's _map_mesh variable hasn't been set!") return - _map_province_shape_image = MapSingleton.get_province_shape_image() - if _map_province_shape_image == null: - push_error("Failed to get province shape image!") - return if not _map_mesh_instance.mesh is MapMesh: push_error("Invalid map mesh class: ", _map_mesh_instance.mesh.get_class(), "(expected MapMesh)") return _map_mesh = _map_mesh_instance.mesh # Set map mesh size and get bounds - _map_mesh.aspect_ratio = float(_map_province_shape_image.get_width()) / float(_map_province_shape_image.get_height()) + _map_image_size = Vector2(Vector2i(MapSingleton.get_width(), MapSingleton.get_height())) + _map_mesh.aspect_ratio = _map_image_size.x / _map_image_size.y var map_mesh_aabb := _map_mesh.get_core_aabb() * _map_mesh_instance.transform _map_mesh_corner = Vector2( min(map_mesh_aabb.position.x, map_mesh_aabb.end.x), @@ -78,18 +77,28 @@ func _ready(): push_error("Invalid map mesh material class: ", map_material.get_class()) return _map_shader_material = map_material - var texture := ImageTexture.create_from_image(_map_province_shape_image) - _map_shader_material.set_shader_parameter(_shader_param_provinces, texture) + # Province index texture + _map_province_index_image = MapSingleton.get_province_index_image() + if _map_province_index_image == null: + push_error("Failed to get province index image!") + return + var province_index_texture := ImageTexture.create_from_image(_map_province_index_image) + _map_shader_material.set_shader_parameter(_shader_param_province_index, province_index_texture) + # Province colour texture + var province_colour_image = MapSingleton.get_province_colour_image() + if province_colour_image == null: + push_error("Failed to get province colour image!") + return + var province_colour_texture := ImageTexture.create_from_image(province_colour_image) + _map_shader_material.set_shader_parameter(_shader_param_province_colour, province_colour_texture) func _unhandled_input(event : InputEvent): if event.is_action_pressed(_action_click): # Check if the mouse is outside of bounds - var mouse_inside_flag := 0 < _mouse_pos_map.x and _mouse_pos_map.x < 1 and 0 < _mouse_pos_map.y and _mouse_pos_map.y < 1 - if mouse_inside_flag: - var mouse_pixel_pos := Vector2i(_mouse_pos_map * Vector2(_map_province_shape_image.get_size())) - var province_colour := _map_province_shape_image.get_pixelv(mouse_pixel_pos).to_argb32() & 0xFFFFFF - var province_identifier := MapSingleton.get_province_identifier_from_colour(province_colour) + if _map_mesh.is_valid_uv_coord(_mouse_pos_map): _map_shader_material.set_shader_parameter(_shader_param_selected_pos, _mouse_pos_map) + var mouse_pixel_pos := Vector2i(_mouse_pos_map * _map_image_size) + var province_identifier := MapSingleton.get_province_identifier_from_pixel_coords(mouse_pixel_pos) province_selected.emit(province_identifier) elif event is InputEventMouseMotion and Input.is_action_pressed(_action_drag): diff --git a/game/src/GameSession/TerrainMap.gdshader b/game/src/GameSession/TerrainMap.gdshader index 61b7032..be2bbe1 100644 --- a/game/src/GameSession/TerrainMap.gdshader +++ b/game/src/GameSession/TerrainMap.gdshader @@ -3,24 +3,46 @@ shader_type spatial; render_mode unshaded; // Cosmetic terrain texture -uniform sampler2D terrain_tex: source_color, repeat_enable; -// Province shape texture -uniform sampler2D province_tex: source_color, repeat_enable; +uniform sampler2D terrain_tex: source_color, repeat_enable, filter_linear; +// Province index texture +uniform sampler2D province_index_tex : repeat_enable, filter_nearest; +// Province colour texture +uniform sampler2D province_colour_tex: source_color, repeat_enable, filter_nearest; // Position of the mouse over the map mesh in UV coords uniform vec2 hover_pos; // Position in UV coords of a pixel belonging to the currently selected province uniform vec2 selected_pos; -const vec3 NULL_COLOUR = vec3(0.0); +uvec2 vec2_to_uvec2(vec2 v) { + return uvec2(v * 256.0); +} + +uint uvec2_to_uint(uvec2 v) { + return (v.y << 8u) | v.x; +} + +uvec2 read_uvec2(sampler2D tex, vec2 uv) { + return vec2_to_uvec2(texture(tex, uv).rg); +} + +uint read_uint16(sampler2D tex, vec2 uv) { + return uvec2_to_uint(read_uvec2(tex, uv)); +} void fragment() { - vec3 terrain_colour = texture(terrain_tex, UV).rgb; - vec3 prov_colour = texture(province_tex, UV).rgb; - vec3 hover_colour = texture(province_tex, hover_pos).rgb; - vec3 selected_colour = texture(province_tex, selected_pos).rgb; + uvec2 prov_idx_split = read_uvec2(province_index_tex, UV); + + uint prov_index = uvec2_to_uint(prov_idx_split); + uint hover_index = read_uint16(province_index_tex, hover_pos); + uint selected_index = read_uint16(province_index_tex, selected_pos); + // Boost prov_colour's contribution if it matches hover_colour or selected_colour - float mix_val = float(prov_colour == hover_colour) * 0.3 + float(prov_colour == selected_colour) * 0.5; - // Set to 0 if the province has NULL colour - mix_val *= 1.0 - float(prov_colour == NULL_COLOUR); - ALBEDO = mix(terrain_colour, prov_colour, mix_val); + float mix_val = float(prov_index == hover_index) * 0.3 + float(prov_index == selected_index) * 0.5; + // Don't mix if the province index is 0 + mix_val *= float(prov_index != 0u); + + vec3 terrain_colour = texture(terrain_tex, UV).rgb; + vec3 province_colour = texelFetch(province_colour_tex, ivec2(prov_idx_split), 0).rgb; + + ALBEDO = mix(terrain_colour, province_colour, mix_val); } |