feat: multithreading for terrain mesh gen

This commit is contained in:
Sara Gerretsen 2026-02-24 23:16:21 +01:00
parent d3561eb218
commit 8ff1b1404d
7 changed files with 130 additions and 30 deletions

View file

@ -18,10 +18,12 @@ void TerrainModifier::_notification(int what) {
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;
}
}
@ -58,12 +60,20 @@ void TerrainModifier::changed() {
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_global_position() };
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);
@ -89,24 +99,40 @@ void TerrainModifierDistance::_bind_methods() {
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();
changed();
}
float TerrainModifierDistance::distance_at(Vector2 const &world_coordinate) {
Vector3 const global_position{ get_global_position() };
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) {
if (this->distance_weight_curve.is_null() || this->distance_height_curve.is_null()) {
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()) {
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())
std::clamp(distance, this->safe_weight_curve->get_min_domain(), this->safe_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);
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())
};
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);
}
PackedStringArray TerrainModifierDistance::get_configuration_warnings() const {
@ -123,12 +149,13 @@ PackedStringArray TerrainModifierDistance::get_configuration_warnings() const {
void TerrainModifierDistance::set_distance_weight_curve(Ref<Curve> curve) {
if (Engine::get_singleton()->is_editor_hint()) {
if (this->distance_weight_curve.is_valid()) {
this->distance_weight_curve->disconnect_changed(callable_mp(cast_to<TerrainModifier>(this), &self_type::changed));
this->distance_weight_curve->disconnect_changed(callable_mp(this, &self_type::curves_changed));
}
if (curve.is_valid()) {
curve->connect_changed(callable_mp(cast_to<TerrainModifier>(this), &self_type::changed));
curve->connect_changed(callable_mp(this, &self_type::curves_changed));
}
}
curves_changed();
this->distance_weight_curve = curve;
update_configuration_warnings();
changed();
@ -141,12 +168,13 @@ Ref<Curve> TerrainModifierDistance::get_distance_weight_curve() const {
void TerrainModifierDistance::set_distance_height_curve(Ref<Curve> curve) {
if (Engine::get_singleton()->is_editor_hint()) {
if (this->distance_height_curve.is_valid()) {
this->distance_height_curve->disconnect_changed(callable_mp(cast_to<TerrainModifier>(this), &self_type::changed));
this->distance_height_curve->disconnect_changed(callable_mp(this, &self_type::curves_changed));
}
if (curve.is_valid()) {
curve->connect_changed(callable_mp(cast_to<TerrainModifier>(this), &self_type::changed));
curve->connect_changed(callable_mp(this, &self_type::curves_changed));
}
}
curves_changed();
this->distance_height_curve = curve;
update_configuration_warnings();
changed();