199 lines
5.6 KiB
C++
199 lines
5.6 KiB
C++
#include "terrain_modifier.h"
|
|
#include "core/config/engine.h"
|
|
#include "core/variant/variant.h"
|
|
#include "macros.h"
|
|
#include "terrain/terrain.h"
|
|
#include <algorithm>
|
|
|
|
void TerrainModifier::_bind_methods() {
|
|
BIND_PROPERTY(Variant::FLOAT, blend_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);
|
|
}
|
|
this->thread_safe_global_position = get_global_position();
|
|
case NOTIFICATION_TRANSFORM_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) {
|
|
return over;
|
|
}
|
|
float const smooth_center_distance{ center_distance * center_distance };
|
|
return over + smooth_center_distance;
|
|
}
|
|
|
|
void TerrainModifier::push_changed(Rect2 area) {
|
|
if (this->terrain) {
|
|
this->terrain->push_changed(area);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
float TerrainModifier::get_blend_distance() const {
|
|
return this->blend_distance;
|
|
}
|
|
|
|
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");
|
|
}
|
|
|
|
void TerrainModifierDistance::curves_changed() {
|
|
this->lock.lock_exclusive();
|
|
if (this->distance_weight_curve.is_valid()) {
|
|
this->distance_weight_curve->bake();
|
|
}
|
|
this->lock.unlock_exclusive();
|
|
if (!update_bounds()) {
|
|
push_changed(get_bounds());
|
|
}
|
|
}
|
|
|
|
bool TerrainModifierDistance::update_bounds() {
|
|
Rect2 const before{ get_bounds() };
|
|
Rect2 bounds{};
|
|
Vector3 position{ get_thread_safe_global_position() };
|
|
bounds.position = { position.x, position.z };
|
|
bounds.size = { 0, 0 };
|
|
this->lock.lock_shared();
|
|
if (this->distance_weight_curve.is_valid()) {
|
|
float const max_radius{ this->distance_weight_curve->get_max_domain() };
|
|
float const max_diameter{ 2.f * max_radius };
|
|
bounds.size = { max_diameter, max_diameter };
|
|
bounds.position -= { max_radius, max_radius };
|
|
}
|
|
this->lock.unlock_shared();
|
|
this->lock.lock_exclusive();
|
|
bool const changed{ before != bounds };
|
|
if (changed) {
|
|
set_bounds(bounds);
|
|
push_changed(before);
|
|
push_changed(bounds);
|
|
}
|
|
this->lock.unlock_exclusive();
|
|
return changed;
|
|
}
|
|
|
|
void TerrainModifierDistance::_notification(int what) {
|
|
switch (what) {
|
|
default:
|
|
return;
|
|
case NOTIFICATION_READY:
|
|
update_bounds();
|
|
set_notify_transform(true);
|
|
return;
|
|
case NOTIFICATION_TRANSFORM_CHANGED:
|
|
if (is_inside_tree()) {
|
|
if (!update_bounds()) {
|
|
push_changed(get_bounds());
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
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->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 weight{ this->distance_weight_curve->sample(weight_offset) };
|
|
float out{ weight <= 0.f ? before : Math::lerp(before, blend(before, 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");
|
|
}
|
|
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;
|
|
}
|