terrain-module/terrain_modifier_noise.cpp
2026-04-22 22:46:58 +02:00

52 lines
1.4 KiB
C++

#include "terrain_modifier_noise.h"
#include "core/math/math_defs.h"
#include "macros.h"
void TerrainModifierNoise::_bind_methods() {
BIND_HPROPERTY(Variant::OBJECT, noise, PROPERTY_HINT_RESOURCE_TYPE, "Noise");
BIND_PROPERTY(Variant::FLOAT, noise_amplitude);
}
void TerrainModifierNoise::push_changed_all() {
push_changed(get_bounds());
}
void TerrainModifierNoise::noise_changed() {
{
SharedMutex::LockExclusive exclusive{ this->lock };
this->noise_buffer = this->noise->duplicate_deep();
}
push_changed_all();
}
float TerrainModifierNoise::evaluate_at(Vector2 world_coordinates, float before) {
SharedMutex::LockShared shared{ this->lock };
if (this->noise.is_null()) {
return before;
}
return 0.5f * this->noise_amplitude * (this->noise_buffer->get_noise_2d(world_coordinates.x, world_coordinates.y) + 1.f) + before;
}
void TerrainModifierNoise::set_noise(Ref<Noise> noise) {
{
SharedMutex::LockExclusive exclusive{ this->lock };
if (noise == this->noise) {
return;
}
if (this->noise.is_valid()) {
this->noise->disconnect_changed(callable_mp(this, &self_type::noise_changed));
}
if (noise.is_valid()) {
noise->connect_changed(callable_mp(this, &self_type::noise_changed));
set_bounds({ { -Math::INF, -Math::INF }, { Math::INF, Math::INF } });
} else {
set_bounds({ { 0, 0 }, { 0, 0 } });
}
this->noise = noise;
}
noise_changed();
}
Ref<Noise> TerrainModifierNoise::get_noise() const {
return this->noise;
}