Merge pull request #95 from tinmanjuggernaut/fix_blocky_noise
Enables 3D Noise on Blocky VoxelTerrainmaster
commit
56b5113f9b
|
@ -1,5 +1,14 @@
|
||||||
#include "voxel_stream_noise.h"
|
#include "voxel_stream_noise.h"
|
||||||
|
|
||||||
|
void VoxelStreamNoise::set_channel(VoxelBuffer::ChannelId channel) {
|
||||||
|
ERR_FAIL_INDEX(channel, VoxelBuffer::MAX_CHANNELS);
|
||||||
|
_channel = channel;
|
||||||
|
}
|
||||||
|
|
||||||
|
VoxelBuffer::ChannelId VoxelStreamNoise::get_channel() const {
|
||||||
|
return _channel;
|
||||||
|
}
|
||||||
|
|
||||||
void VoxelStreamNoise::set_noise(Ref<OpenSimplexNoise> noise) {
|
void VoxelStreamNoise::set_noise(Ref<OpenSimplexNoise> noise) {
|
||||||
_noise = noise;
|
_noise = noise;
|
||||||
}
|
}
|
||||||
|
@ -34,11 +43,21 @@ void VoxelStreamNoise::emerge_block(Ref<VoxelBuffer> out_buffer, Vector3i origin
|
||||||
|
|
||||||
if (origin_in_voxels.y > _height_start + _height_range) {
|
if (origin_in_voxels.y > _height_start + _height_range) {
|
||||||
|
|
||||||
buffer.clear_channel_f(VoxelBuffer::CHANNEL_SDF, 100.0);
|
if (_channel == VoxelBuffer::CHANNEL_SDF) {
|
||||||
|
buffer.clear_channel_f(_channel, 100.0);
|
||||||
|
}
|
||||||
|
else if (_channel == VoxelBuffer::CHANNEL_TYPE) {
|
||||||
|
buffer.clear_channel(_channel, 0);
|
||||||
|
}
|
||||||
|
|
||||||
} else if (origin_in_voxels.y + (buffer.get_size().y << lod) < _height_start) {
|
} else if (origin_in_voxels.y + (buffer.get_size().y << lod) < _height_start) {
|
||||||
|
|
||||||
buffer.clear_channel_f(VoxelBuffer::CHANNEL_SDF, -100.0);
|
if (_channel == VoxelBuffer::CHANNEL_SDF) {
|
||||||
|
buffer.clear_channel_f(_channel, -100.0);
|
||||||
|
}
|
||||||
|
else if (_channel == VoxelBuffer::CHANNEL_TYPE) {
|
||||||
|
buffer.clear_channel(_channel, 1);
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
@ -73,9 +92,11 @@ void VoxelStreamNoise::emerge_block(Ref<VoxelBuffer> out_buffer, Vector3i origin
|
||||||
float iso_scale = noise.get_period() * 0.1;
|
float iso_scale = noise.get_period() * 0.1;
|
||||||
//float noise_buffer_scale = 1.f / static_cast<float>(noise_buffer_step);
|
//float noise_buffer_scale = 1.f / static_cast<float>(noise_buffer_step);
|
||||||
|
|
||||||
for (int z = 0; z < buffer.get_size().z; ++z) {
|
Vector3i size = buffer.get_size();
|
||||||
for (int x = 0; x < buffer.get_size().x; ++x) {
|
|
||||||
for (int y = 0; y < buffer.get_size().y; ++y) {
|
for (int z = 0; z < size.z; ++z) {
|
||||||
|
for (int x = 0; x < size.x; ++x) {
|
||||||
|
for (int y = 0; y < size.y; ++y) {
|
||||||
|
|
||||||
float lx = origin_in_voxels.x + (x << lod);
|
float lx = origin_in_voxels.x + (x << lod);
|
||||||
float ly = origin_in_voxels.y + (y << lod);
|
float ly = origin_in_voxels.y + (y << lod);
|
||||||
|
@ -86,8 +107,12 @@ void VoxelStreamNoise::emerge_block(Ref<VoxelBuffer> out_buffer, Vector3i origin
|
||||||
float t = (ly - _height_start) / _height_range;
|
float t = (ly - _height_start) / _height_range;
|
||||||
float d = (n + 2.0 * t - 1.0) * iso_scale;
|
float d = (n + 2.0 * t - 1.0) * iso_scale;
|
||||||
|
|
||||||
buffer.set_voxel_f(d, x, y, z, VoxelBuffer::CHANNEL_SDF);
|
if (_channel == VoxelBuffer::CHANNEL_SDF) {
|
||||||
// TODO Support for blocky voxels
|
buffer.set_voxel_f(d, x, y, z, _channel);
|
||||||
|
}
|
||||||
|
else if (_channel == VoxelBuffer::CHANNEL_TYPE && d < 0) {
|
||||||
|
buffer.set_voxel(1, x, y, z, _channel);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -105,7 +130,11 @@ void VoxelStreamNoise::_bind_methods() {
|
||||||
ClassDB::bind_method(D_METHOD("set_height_range", "hrange"), &VoxelStreamNoise::set_height_range);
|
ClassDB::bind_method(D_METHOD("set_height_range", "hrange"), &VoxelStreamNoise::set_height_range);
|
||||||
ClassDB::bind_method(D_METHOD("get_height_range"), &VoxelStreamNoise::get_height_range);
|
ClassDB::bind_method(D_METHOD("get_height_range"), &VoxelStreamNoise::get_height_range);
|
||||||
|
|
||||||
|
ClassDB::bind_method(D_METHOD("set_channel", "channel"), &VoxelStreamNoise::set_channel);
|
||||||
|
ClassDB::bind_method(D_METHOD("get_channel"), &VoxelStreamNoise::get_channel);
|
||||||
|
|
||||||
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "OpenSimplexNoise"), "set_noise", "get_noise");
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "OpenSimplexNoise"), "set_noise", "get_noise");
|
||||||
ADD_PROPERTY(PropertyInfo(Variant::REAL, "height_start"), "set_height_start", "get_height_start");
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "height_start"), "set_height_start", "get_height_start");
|
||||||
ADD_PROPERTY(PropertyInfo(Variant::REAL, "height_range"), "set_height_range", "get_height_range");
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "height_range"), "set_height_range", "get_height_range");
|
||||||
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "channel", PROPERTY_HINT_ENUM, VoxelBuffer::CHANNEL_ID_HINT_STRING), "set_channel", "get_channel");
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,11 @@
|
||||||
|
|
||||||
class VoxelStreamNoise : public VoxelStream {
|
class VoxelStreamNoise : public VoxelStream {
|
||||||
GDCLASS(VoxelStreamNoise, VoxelStream)
|
GDCLASS(VoxelStreamNoise, VoxelStream)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
void set_channel(VoxelBuffer::ChannelId channel);
|
||||||
|
VoxelBuffer::ChannelId get_channel() const;
|
||||||
|
|
||||||
void set_noise(Ref<OpenSimplexNoise> noise);
|
void set_noise(Ref<OpenSimplexNoise> noise);
|
||||||
Ref<OpenSimplexNoise> get_noise() const;
|
Ref<OpenSimplexNoise> get_noise() const;
|
||||||
|
|
||||||
|
@ -23,6 +27,7 @@ protected:
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
VoxelBuffer::ChannelId _channel = VoxelBuffer::CHANNEL_TYPE;
|
||||||
Ref<OpenSimplexNoise> _noise;
|
Ref<OpenSimplexNoise> _noise;
|
||||||
FloatBuffer3D _noise_buffer;
|
FloatBuffer3D _noise_buffer;
|
||||||
float _height_start = 0;
|
float _height_start = 0;
|
||||||
|
|
|
@ -839,6 +839,7 @@ void VoxelTerrain::_process() {
|
||||||
VoxelBlock *block = _map->get_block(block_pos);
|
VoxelBlock *block = _map->get_block(block_pos);
|
||||||
bool update_neighbors = block == nullptr;
|
bool update_neighbors = block == nullptr;
|
||||||
block = _map->set_block_buffer(block_pos, ob.data.voxels_loaded);
|
block = _map->set_block_buffer(block_pos, ob.data.voxels_loaded);
|
||||||
|
block->set_world(get_world());
|
||||||
|
|
||||||
// TODO The following code appears to have order-dependency with block loading.
|
// TODO The following code appears to have order-dependency with block loading.
|
||||||
// i.e if block loading responses arrive in a different order they were requested in,
|
// i.e if block loading responses arrive in a different order they were requested in,
|
||||||
|
|
Loading…
Reference in New Issue