chore: massively sped up mesh gen performance
This commit is contained in:
parent
8ff1b1404d
commit
811970a306
7 changed files with 201 additions and 79 deletions
|
|
@ -57,6 +57,7 @@ float TerrainModifier::blend(float under, float over) {
|
|||
}
|
||||
|
||||
void TerrainModifier::changed() {
|
||||
this->dirty = true;
|
||||
emit_signal(sig_changed);
|
||||
}
|
||||
|
||||
|
|
@ -94,15 +95,46 @@ TerrainModifier::BlendMode TerrainModifier::get_blend_mode() const {
|
|||
|
||||
String const TerrainModifier::sig_changed{ "changed" };
|
||||
|
||||
void ReadWriteMutex::lock_read() {
|
||||
this->read_lock.lock();
|
||||
this->read_count++;
|
||||
this->read_lock.unlock();
|
||||
}
|
||||
|
||||
void ReadWriteMutex::unlock_read() {
|
||||
this->read_lock.lock();
|
||||
this->read_count--;
|
||||
this->read_lock.unlock();
|
||||
}
|
||||
|
||||
void ReadWriteMutex::lock_write() {
|
||||
while (true) {
|
||||
this->read_lock.lock();
|
||||
if (this->read_count == 0) {
|
||||
return;
|
||||
}
|
||||
this->read_lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void ReadWriteMutex::unlock_write() {
|
||||
this->read_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->mtx.lock();
|
||||
this->safe_curves_dirty = true;
|
||||
this->mtx.unlock();
|
||||
this->lock.lock_write();
|
||||
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_write();
|
||||
changed();
|
||||
}
|
||||
|
||||
|
|
@ -112,27 +144,34 @@ float TerrainModifierDistance::distance_at(Vector2 const &world_coordinate) {
|
|||
}
|
||||
|
||||
float TerrainModifierDistance::evaluate_at(Vector2 world_coordinate, float before) {
|
||||
this->mtx.lock();
|
||||
if (this->safe_curves_dirty) {
|
||||
this->safe_weight_curve = this->distance_weight_curve->duplicate_deep();
|
||||
this->safe_weight_curve->bake();
|
||||
this->safe_height_curve = this->distance_height_curve->duplicate_deep();
|
||||
this->safe_height_curve->bake();
|
||||
this->safe_curves_dirty = false;
|
||||
}
|
||||
this->mtx.unlock();
|
||||
if (this->safe_weight_curve.is_null() || this->safe_height_curve.is_null()) {
|
||||
this->lock.lock_read();
|
||||
if (this->distance_weight_curve.is_null() || this->distance_height_curve.is_null()) {
|
||||
this->lock.unlock_read();
|
||||
return before;
|
||||
}
|
||||
float const distance{ distance_at(world_coordinate) };
|
||||
if (distance >= this->distance_weight_curve->get_max_domain()) {
|
||||
this->lock.unlock_read();
|
||||
return before;
|
||||
}
|
||||
float const weight_offset{
|
||||
std::clamp(distance, this->safe_weight_curve->get_min_domain(), this->safe_weight_curve->get_max_domain())
|
||||
std::clamp(distance, this->distance_weight_curve->get_min_domain(), this->distance_weight_curve->get_max_domain())
|
||||
};
|
||||
float const weight{ this->safe_weight_curve->sample_baked(weight_offset) };
|
||||
float const height_offset{
|
||||
std::clamp(distance, this->safe_height_curve->get_min_domain(), this->safe_height_curve->get_max_domain())
|
||||
std::clamp(distance, this->distance_height_curve->get_min_domain(), this->distance_height_curve->get_max_domain())
|
||||
};
|
||||
return weight <= 0.f ? before : Math::lerp(before, blend(before, this->safe_height_curve->sample_baked(height_offset) + get_thread_safe_global_position().y), weight);
|
||||
this->lock.unlock_read();
|
||||
|
||||
this->lock.lock_write();
|
||||
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_write();
|
||||
|
||||
this->lock.lock_read();
|
||||
float out{ weight <= 0.f ? before : Math::lerp(before, blend(before, height + get_thread_safe_global_position().y), weight) };
|
||||
this->lock.unlock_read();
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
PackedStringArray TerrainModifierDistance::get_configuration_warnings() const {
|
||||
|
|
@ -147,6 +186,7 @@ PackedStringArray TerrainModifierDistance::get_configuration_warnings() const {
|
|||
}
|
||||
|
||||
void TerrainModifierDistance::set_distance_weight_curve(Ref<Curve> curve) {
|
||||
this->lock.lock_write();
|
||||
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));
|
||||
|
|
@ -155,10 +195,10 @@ void TerrainModifierDistance::set_distance_weight_curve(Ref<Curve> curve) {
|
|||
curve->connect_changed(callable_mp(this, &self_type::curves_changed));
|
||||
}
|
||||
}
|
||||
curves_changed();
|
||||
this->distance_weight_curve = curve;
|
||||
this->lock.unlock_write();
|
||||
curves_changed();
|
||||
update_configuration_warnings();
|
||||
changed();
|
||||
}
|
||||
|
||||
Ref<Curve> TerrainModifierDistance::get_distance_weight_curve() const {
|
||||
|
|
@ -166,6 +206,7 @@ Ref<Curve> TerrainModifierDistance::get_distance_weight_curve() const {
|
|||
}
|
||||
|
||||
void TerrainModifierDistance::set_distance_height_curve(Ref<Curve> curve) {
|
||||
this->lock.lock_write();
|
||||
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));
|
||||
|
|
@ -174,10 +215,10 @@ void TerrainModifierDistance::set_distance_height_curve(Ref<Curve> curve) {
|
|||
curve->connect_changed(callable_mp(this, &self_type::curves_changed));
|
||||
}
|
||||
}
|
||||
curves_changed();
|
||||
this->distance_height_curve = curve;
|
||||
this->lock.unlock_write();
|
||||
curves_changed();
|
||||
update_configuration_warnings();
|
||||
changed();
|
||||
}
|
||||
|
||||
Ref<Curve> TerrainModifierDistance::get_distance_height_curve() const {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue