Skip to content

Commit

Permalink
Add get_texture_id()
Browse files Browse the repository at this point in the history
  • Loading branch information
TokisanGames committed Oct 29, 2023
1 parent 6f8de9b commit 3636733
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/terrain_3d_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)));
Expand Down
1 change: 1 addition & 0 deletions src/terrain_3d_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Image> sanitize_maps(MapType p_map_type, const TypedArray<Image> &p_maps);
void force_update_maps(MapType p_map = TYPE_MAX);

Expand Down

0 comments on commit 3636733

Please sign in to comment.