From 3636733ef8257ac2eec7f58b587c664d0d01ba15 Mon Sep 17 00:00:00 2001 From: Cory Petkovsek <632766+TokisanGames@users.noreply.github.com> Date: Sun, 29 Oct 2023 17:51:00 +0700 Subject: [PATCH] Add get_texture_id() --- src/terrain_3d_storage.cpp | 22 ++++++++++++++++++++++ src/terrain_3d_storage.h | 1 + 2 files changed, 23 insertions(+) diff --git a/src/terrain_3d_storage.cpp b/src/terrain_3d_storage.cpp index e89f9239..cd8d0c9d 100644 --- a/src/terrain_3d_storage.cpp +++ b/src/terrain_3d_storage.cpp @@ -447,6 +447,27 @@ Color Terrain3DStorage::get_color(Vector3 p_global_position) { return clr; } +/** + * Returns: + * X = base index + * Y = overlay index + * Z = percentage blend between X and Y. Limited to the fixed values in RANGE. + * Interpretation of this data is up to the gamedev. Unfortunately due to blending, this isn't + * pixel perfect. I would have your player print this location as you walk around to see how the + * blending values look, then consider that the overlay texture is visible starting at a blend + * value of .3-.5, otherwise it's the base texture. + **/ +Vector3 Terrain3DStorage::get_texture_id(Vector3 p_global_position) { + // Get bit field from pixel + uint32_t bits; + *(real_t *)&bits = get_pixel(TYPE_CONTROL, p_global_position).r; + uint32_t base_index = bits >> 27u & 0x1Fu; + uint32_t overlay_index = bits >> 22u & 0x1Fu; + uint32_t blend_index = bits >> 19u & 0x7u; + real_t blend = RANGE[blend_index]; + return Vector3(real_t(base_index), real_t(overlay_index), blend); +} + /** * Returns sanitized maps of either a region set or a uniform set * Verifies size, vailidity, and format of maps @@ -975,6 +996,7 @@ void Terrain3DStorage::_bind_methods() { ClassDB::bind_method(D_METHOD("get_color", "global_position"), &Terrain3DStorage::get_color); ClassDB::bind_method(D_METHOD("get_control", "global_position"), &Terrain3DStorage::get_control); ClassDB::bind_method(D_METHOD("get_roughness", "global_position"), &Terrain3DStorage::get_roughness); + ClassDB::bind_method(D_METHOD("get_texture_id", "global_position"), &Terrain3DStorage::get_texture_id); ClassDB::bind_method(D_METHOD("force_update_maps", "map_type"), &Terrain3DStorage::force_update_maps, DEFVAL(TYPE_MAX)); ClassDB::bind_static_method("Terrain3DStorage", D_METHOD("load_image", "file_name", "cache_mode", "r16_height_range", "r16_size"), &Terrain3DStorage::load_image, DEFVAL(ResourceLoader::CACHE_MODE_IGNORE), DEFVAL(Vector2(0, 255)), DEFVAL(Vector2i(0, 0))); diff --git a/src/terrain_3d_storage.h b/src/terrain_3d_storage.h index 2a46122c..6a54c845 100644 --- a/src/terrain_3d_storage.h +++ b/src/terrain_3d_storage.h @@ -147,6 +147,7 @@ class Terrain3DStorage : public Resource { inline Color get_color(Vector3 p_global_position); inline Color get_control(Vector3 p_global_position) { return get_pixel(TYPE_CONTROL, p_global_position); } inline real_t get_roughness(Vector3 p_global_position) { return get_pixel(TYPE_COLOR, p_global_position).a; } + Vector3 get_texture_id(Vector3 p_global_position); TypedArray sanitize_maps(MapType p_map_type, const TypedArray &p_maps); void force_update_maps(MapType p_map = TYPE_MAX);