107 lines
3.4 KiB
C++
107 lines
3.4 KiB
C++
#include "terrain_modifier_distance.h"
|
|
#include "core/typedefs.h"
|
|
#include "macros.h"
|
|
|
|
void TerrainModifierDistance::_bind_methods() {
|
|
BIND_HPROPERTY(Variant::OBJECT, distance_weight_curve, PROPERTY_HINT_RESOURCE_TYPE, "Curve");
|
|
}
|
|
|
|
void TerrainModifierDistance::curves_changed() {
|
|
if (!update_bounds()) {
|
|
push_changed(get_bounds());
|
|
}
|
|
SharedMutex::LockExclusive exclusive{ this->lock };
|
|
this->distance_weight_curve_buffer = this->distance_weight_curve.is_valid() ? this->distance_weight_curve->duplicate(true) : nullptr;
|
|
}
|
|
|
|
bool TerrainModifierDistance::update_bounds() {
|
|
bool changed{ false };
|
|
Rect2 bounds{};
|
|
{
|
|
SharedMutex::LockShared shared{ this->lock };
|
|
Rect2 const before{ get_bounds() };
|
|
Vector3 position{ get_thread_safe_global_position() };
|
|
bounds.position = { position.x, position.z };
|
|
bounds.size = { 0, 0 };
|
|
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 };
|
|
}
|
|
changed = before != bounds;
|
|
}
|
|
{
|
|
SharedMutex::LockExclusive exclusive{ this->lock };
|
|
set_bounds(bounds);
|
|
}
|
|
|
|
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) {
|
|
SharedMutex::LockShared shared{ this->lock };
|
|
if (this->distance_weight_curve_buffer.is_null()) {
|
|
return before;
|
|
}
|
|
float const distance{ distance_at(world_coordinate) };
|
|
if (distance >= this->distance_weight_curve_buffer->get_max_domain()) {
|
|
return before;
|
|
}
|
|
float const weight_offset{ CLAMP(distance, this->distance_weight_curve_buffer->get_min_domain(), this->distance_weight_curve_buffer->get_max_domain()) };
|
|
float const weight{ this->distance_weight_curve_buffer->sample(weight_offset) };
|
|
float out{ weight <= 0.f ? before : Math::lerp(before, get_thread_safe_global_position().y, weight) };
|
|
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) {
|
|
{
|
|
SharedMutex::LockExclusive exclusive{ this->lock };
|
|
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;
|
|
}
|
|
curves_changed();
|
|
update_configuration_warnings();
|
|
}
|
|
|
|
Ref<Curve> TerrainModifierDistance::get_distance_weight_curve() const {
|
|
return this->distance_weight_curve;
|
|
}
|