authority/modules/terrain/terrain_modifier.cpp

226 lines
6.7 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));
}
void TerrainModifier::_notification(int what) {
switch (what) {
default:
return;
case NOTIFICATION_ENTER_TREE:
if (Engine::get_singleton()->is_editor_hint()) {
set_notify_transform(true);
}
this->thread_safe_global_position = get_global_position();
case NOTIFICATION_TRANSFORM_CHANGED:
if (Engine::get_singleton()->is_editor_hint()) {
emit_signal(sig_changed);
}
this->thread_safe_global_position = get_global_position();
return;
}
}
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::changed() {
this->dirty = true;
emit_signal(sig_changed);
}
void TerrainModifier::changed_deferred() {
callable_mp(this, &self_type::changed).call_deferred();
}
float TerrainModifier::evaluate_at(Vector2 world_coordinate, float before) {
Vector3 const global_position{ get_thread_safe_global_position() };
world_coordinate -= { global_position.x, global_position.z };
return blend(before, 0.0);
}
Vector3 TerrainModifier::get_thread_safe_global_position() const {
return this->thread_safe_global_position;
}
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 SharedMutex::lock_shared() {
this->lock.lock();
this->shared_count++;
this->lock.unlock();
}
void SharedMutex::unlock_shared() {
this->lock.lock();
this->shared_count--;
this->lock.unlock();
}
void SharedMutex::lock_exclusive() {
while (true) {
this->lock.lock();
if (this->shared_count == 0) {
return;
}
this->lock.unlock();
}
}
void SharedMutex::unlock_exclusive() {
this->lock.unlock();
}
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");
}
void TerrainModifierDistance::curves_changed() {
this->lock.lock_shared();
if (this->distance_height_curve.is_valid()) {
this->distance_height_curve->bake();
}
if (this->distance_weight_curve.is_valid()) {
this->distance_weight_curve->bake();
}
this->lock.unlock_shared();
changed();
}
float TerrainModifierDistance::distance_at(Vector2 const &world_coordinate) {
Vector3 const global_position{ get_thread_safe_global_position() };
return world_coordinate.distance_to({ global_position.x, global_position.z });
}
float TerrainModifierDistance::evaluate_at(Vector2 world_coordinate, float before) {
this->lock.lock_shared();
if (this->distance_weight_curve.is_null() || this->distance_height_curve.is_null()) {
this->lock.unlock_shared();
return before;
}
float const distance{ distance_at(world_coordinate) };
if (distance >= this->distance_weight_curve->get_max_domain()) {
this->lock.unlock_shared();
return before;
}
float const weight_offset{
std::clamp(distance, this->distance_weight_curve->get_min_domain(), this->distance_weight_curve->get_max_domain())
};
float const height_offset{
std::clamp(distance, this->distance_height_curve->get_min_domain(), this->distance_height_curve->get_max_domain())
};
this->lock.unlock_shared();
this->lock.lock_exclusive();
float const weight{ this->distance_weight_curve->sample_baked(weight_offset) };
float const height{ this->distance_height_curve->sample_baked(height_offset) };
this->lock.unlock_exclusive();
this->lock.lock_shared();
float out{ weight <= 0.f ? before : Math::lerp(before, blend(before, height + get_thread_safe_global_position().y), weight) };
this->lock.unlock_shared();
return out;
}
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->lock.lock_exclusive();
if (Engine::get_singleton()->is_editor_hint()) {
if (this->distance_weight_curve.is_valid()) {
this->distance_weight_curve->disconnect_changed(callable_mp(this, &self_type::curves_changed));
}
if (curve.is_valid()) {
curve->connect_changed(callable_mp(this, &self_type::curves_changed));
}
}
this->distance_weight_curve = curve;
this->lock.unlock_exclusive();
curves_changed();
update_configuration_warnings();
}
Ref<Curve> TerrainModifierDistance::get_distance_weight_curve() const {
return this->distance_weight_curve;
}
void TerrainModifierDistance::set_distance_height_curve(Ref<Curve> curve) {
this->lock.lock_exclusive();
if (Engine::get_singleton()->is_editor_hint()) {
if (this->distance_height_curve.is_valid()) {
this->distance_height_curve->disconnect_changed(callable_mp(this, &self_type::curves_changed));
}
if (curve.is_valid()) {
curve->connect_changed(callable_mp(this, &self_type::curves_changed));
}
}
this->distance_height_curve = curve;
this->lock.unlock_exclusive();
curves_changed();
update_configuration_warnings();
}
Ref<Curve> TerrainModifierDistance::get_distance_height_curve() const {
return this->distance_height_curve;
}