feat: started work on terrain editor tools
This commit is contained in:
parent
a61b52806a
commit
ac139f01b6
11 changed files with 423 additions and 0 deletions
82
modules/terrain/terrain_modifier.cpp
Normal file
82
modules/terrain/terrain_modifier.cpp
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
#include "terrain_modifier.h"
|
||||
#include "core/variant/variant.h"
|
||||
#include "macros.h"
|
||||
#include <algorithm>
|
||||
|
||||
void TerrainModifier::_bind_methods() {
|
||||
BIND_HPROPERTY(Variant::INT, blend_mode, PROPERTY_HINT_ENUM, BlendMode_hint());
|
||||
BIND_PROPERTY(Variant::FLOAT, blend_distance);
|
||||
}
|
||||
|
||||
float TerrainModifier::blend(float under, float over, float weight) {
|
||||
if (weight <= 0.0) {
|
||||
return under;
|
||||
}
|
||||
over = Math::lerp(under, over, weight);
|
||||
float const difference{ under - over };
|
||||
float const distance{ Math::abs(difference) };
|
||||
// .25 because we need half of each half of the blend range to be used
|
||||
float const center_distance{
|
||||
this->blend_distance == 0.f
|
||||
? 0.f
|
||||
: this->blend_distance * 0.25f - distance / this->blend_distance
|
||||
};
|
||||
if (center_distance < 0.f) {
|
||||
if (this->blend_mode == Override) {
|
||||
return over;
|
||||
} else if (this->blend_mode == Add) {
|
||||
return under > over ? under : over;
|
||||
} else {
|
||||
return under > over ? over : under;
|
||||
}
|
||||
}
|
||||
float const smooth_center_distance{ center_distance * center_distance };
|
||||
if (this->blend_mode == Override) {
|
||||
return over + smooth_center_distance;
|
||||
} else {
|
||||
return (this->blend_mode == Add
|
||||
? (under > over ? under : over) + smooth_center_distance
|
||||
: (under > over ? over : under) - smooth_center_distance);
|
||||
}
|
||||
}
|
||||
|
||||
float TerrainModifier::evaluate_at(Vector2 world_coordinate, float before) {
|
||||
Vector3 const global_position{ get_global_position() };
|
||||
world_coordinate -= { global_position.x, global_position.z };
|
||||
return blend(before, 0.0, 1.0);
|
||||
}
|
||||
|
||||
void TerrainModifierDistance::_bind_methods() {
|
||||
BIND_HPROPERTY(Variant::OBJECT, distance_weight_curve, PROPERTY_HINT_RESOURCE_TYPE, "Curve");
|
||||
BIND_HPROPERTY(Variant::OBJECT, distance_height_curve, PROPERTY_HINT_RESOURCE_TYPE, "Curve");
|
||||
}
|
||||
|
||||
float TerrainModifierDistance::distance_at(Vector2 const &world_coordinate) {
|
||||
Vector3 const global_position{ get_global_position() };
|
||||
return world_coordinate.distance_to({ global_position.x, global_position.z });
|
||||
}
|
||||
|
||||
float TerrainModifierDistance::evaluate_at(Vector2 world_coordinate, float before) {
|
||||
if (this->distance_weight_curve.is_null() || this->distance_height_curve.is_null()) {
|
||||
return before;
|
||||
}
|
||||
float const distance{ distance_at(world_coordinate) };
|
||||
float const height_offset{
|
||||
std::clamp(distance, this->distance_height_curve->get_min_domain(), this->distance_height_curve->get_max_domain())
|
||||
};
|
||||
float const weight_offset{
|
||||
std::clamp(distance, this->distance_weight_curve->get_min_domain(), this->distance_weight_curve->get_max_domain())
|
||||
};
|
||||
return blend(before, this->distance_height_curve->sample_baked(height_offset) + get_global_position().y, this->distance_weight_curve->sample_baked(weight_offset));
|
||||
}
|
||||
|
||||
PackedStringArray TerrainModifierDistance::get_configuration_warnings() const {
|
||||
PackedStringArray warnings{ super_type::get_configuration_warnings() };
|
||||
if (this->distance_weight_curve.is_null()) {
|
||||
warnings.push_back("distance_weight_curve is invalid, add a valid distance_weight_curve");
|
||||
}
|
||||
if (this->distance_height_curve.is_null()) {
|
||||
warnings.push_back("distance_height_curve is invalid, add a valid distance_height_curve");
|
||||
}
|
||||
return warnings;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue