137 lines
4.5 KiB
C++
137 lines
4.5 KiB
C++
#include "terrain_modifier.h"
|
|
#include "core/config/engine.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);
|
|
ADD_SIGNAL(MethodInfo(sig_changed));
|
|
}
|
|
|
|
float TerrainModifier::blend(float under, float over) {
|
|
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);
|
|
}
|
|
}
|
|
|
|
void TerrainModifier::_notification(int what) {
|
|
switch (what) {
|
|
default:
|
|
return;
|
|
case NOTIFICATION_ENTER_TREE:
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
set_notify_transform(true);
|
|
}
|
|
case NOTIFICATION_TRANSFORM_CHANGED:
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
emit_signal(sig_changed);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
void TerrainModifier::set_blend_distance(float value) {
|
|
this->blend_distance = value;
|
|
emit_signal(sig_changed);
|
|
}
|
|
|
|
float TerrainModifier::get_blend_distance() const {
|
|
return this->blend_distance;
|
|
}
|
|
|
|
void TerrainModifier::set_blend_mode(BlendMode mode) {
|
|
this->blend_mode = mode;
|
|
emit_signal(sig_changed);
|
|
}
|
|
|
|
TerrainModifier::BlendMode TerrainModifier::get_blend_mode() const {
|
|
return this->blend_mode;
|
|
}
|
|
|
|
String const TerrainModifier::sig_changed{ "changed" };
|
|
|
|
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())
|
|
};
|
|
float const weight{ this->distance_weight_curve->sample_baked(weight_offset) };
|
|
return weight <= 0.f ? before : Math::lerp(before, blend(before, this->distance_height_curve->sample_baked(height_offset) + get_global_position().y), weight);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
void TerrainModifierDistance::set_distance_weight_curve(Ref<Curve> curve) {
|
|
this->distance_weight_curve = curve;
|
|
update_configuration_warnings();
|
|
emit_signal(sig_changed);
|
|
}
|
|
|
|
Ref<Curve> TerrainModifierDistance::get_distance_weight_curve() const {
|
|
return this->distance_weight_curve;
|
|
}
|
|
|
|
void TerrainModifierDistance::set_distance_height_curve(Ref<Curve> curve) {
|
|
this->distance_height_curve = curve;
|
|
update_configuration_warnings();
|
|
emit_signal(sig_changed);
|
|
}
|
|
|
|
Ref<Curve> TerrainModifierDistance::get_distance_height_curve() const {
|
|
return this->distance_height_curve;
|
|
}
|