Skip to content

Commit

Permalink
Terrain3DStorage: Add get_normal() utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
lw64 authored and TokisanGames committed Nov 20, 2023
1 parent 3f79ad2 commit 29be39a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/terrain_3d_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,18 @@ Ref<Image> Terrain3DStorage::layered_to_image(MapType p_map_type) {
return img;
}

Vector3 Terrain3DStorage::get_normal(Vector3 p_global_position) {
real_t left = get_height(p_global_position + Vector3(-1.0f, 0.0f, 0.0f));
real_t right = get_height(p_global_position + Vector3(1.0f, 0.0f, 0.0f));
real_t back = get_height(p_global_position + Vector3(0.f, 0.f, -1.0f));
real_t front = get_height(p_global_position + Vector3(0.f, 0.f, 1.0f));
Vector3 horizontal = Vector3(2.0f, right - left, 0.0f);
Vector3 vertical = Vector3(0.0f, back - front, 2.0f);
Vector3 normal = vertical.cross(horizontal).normalized();
normal.z *= -1.0f;
return normal;
}

void Terrain3DStorage::print_audit_data() {
LOG(INFO, "Dumping storage data");
LOG(INFO, "_modified: ", _modified);
Expand Down Expand Up @@ -1065,6 +1077,7 @@ void Terrain3DStorage::_bind_methods() {
ClassDB::bind_method(D_METHOD("import_images", "images", "global_position", "offset", "scale"), &Terrain3DStorage::import_images, DEFVAL(Vector3(0, 0, 0)), DEFVAL(0.0), DEFVAL(1.0));
ClassDB::bind_method(D_METHOD("export_image", "file_name", "map_type"), &Terrain3DStorage::export_image);
ClassDB::bind_method(D_METHOD("layered_to_image", "map_type"), &Terrain3DStorage::layered_to_image);
ClassDB::bind_method(D_METHOD("get_normal", "global_position"), &Terrain3DStorage::get_normal);

int ro_flags = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_READ_ONLY;
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "version", PROPERTY_HINT_NONE, "", ro_flags), "set_version", "get_version");
Expand Down
3 changes: 3 additions & 0 deletions src/terrain_3d_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ class Terrain3DStorage : public Resource {
Error export_image(String p_file_name, MapType p_map_type = TYPE_HEIGHT);
Ref<Image> layered_to_image(MapType p_map_type);

// Utility
Vector3 get_normal(Vector3 global_position);

// Testing
void print_audit_data();

Expand Down

0 comments on commit 29be39a

Please sign in to comment.