mirror of
https://github.com/Relintai/godot_voxel.git
synced 2025-02-10 21:00:08 +01:00
Added VoxelProviderNoise for testing
This commit is contained in:
parent
1baa13ff3b
commit
fc4dee54d7
84
providers/voxel_provider_noise.cpp
Normal file
84
providers/voxel_provider_noise.cpp
Normal file
@ -0,0 +1,84 @@
|
||||
#include "voxel_provider_noise.h"
|
||||
|
||||
void VoxelProviderNoise::set_noise(Ref<OpenSimplexNoise> noise) {
|
||||
_noise = noise;
|
||||
}
|
||||
|
||||
Ref<OpenSimplexNoise> VoxelProviderNoise::get_noise() const {
|
||||
return _noise;
|
||||
}
|
||||
|
||||
void VoxelProviderNoise::set_height_start(real_t y) {
|
||||
_height_start = y;
|
||||
}
|
||||
|
||||
real_t VoxelProviderNoise::get_height_start() const {
|
||||
return _height_start;
|
||||
}
|
||||
|
||||
void VoxelProviderNoise::set_height_range(real_t hrange) {
|
||||
_height_range = hrange;
|
||||
}
|
||||
|
||||
real_t VoxelProviderNoise::get_height_range() const {
|
||||
return _height_range;
|
||||
}
|
||||
|
||||
void VoxelProviderNoise::emerge_block(Ref<VoxelBuffer> out_buffer, Vector3i origin_in_voxels, int lod) {
|
||||
|
||||
ERR_FAIL_COND(out_buffer.is_null());
|
||||
ERR_FAIL_COND(_noise.is_null());
|
||||
|
||||
OpenSimplexNoise &noise = **_noise;
|
||||
VoxelBuffer &buffer = **out_buffer;
|
||||
|
||||
if (origin_in_voxels.y > _height_start + _height_range) {
|
||||
|
||||
buffer.clear_channel_f(VoxelBuffer::CHANNEL_ISOLEVEL, 100.0);
|
||||
|
||||
} else if (origin_in_voxels.y + (buffer.get_size().y << lod) < _height_start) {
|
||||
|
||||
buffer.clear_channel_f(VoxelBuffer::CHANNEL_ISOLEVEL, -100.0);
|
||||
|
||||
} else {
|
||||
|
||||
float iso_scale = noise.get_period() * 0.1;
|
||||
|
||||
for (int z = 0; z < buffer.get_size().z; ++z) {
|
||||
for (int x = 0; x < buffer.get_size().x; ++x) {
|
||||
for (int y = 0; y < buffer.get_size().y; ++y) {
|
||||
|
||||
float ly = origin_in_voxels.y + (y << lod);
|
||||
|
||||
float n = noise.get_noise_3d(
|
||||
origin_in_voxels.x + (x << lod),
|
||||
ly,
|
||||
origin_in_voxels.z + (z << lod));
|
||||
|
||||
float t = (ly - _height_start) / _height_range;
|
||||
|
||||
float d = (n + 2.0 * t - 1.0) * iso_scale;
|
||||
|
||||
buffer.set_voxel_f(d, x, y, z, VoxelBuffer::CHANNEL_ISOLEVEL);
|
||||
// TODO Support for blocky voxels
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void VoxelProviderNoise::_bind_methods() {
|
||||
|
||||
ClassDB::bind_method(D_METHOD("set_noise", "noise"), &VoxelProviderNoise::set_noise);
|
||||
ClassDB::bind_method(D_METHOD("get_noise"), &VoxelProviderNoise::get_noise);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("set_height_start", "hstart"), &VoxelProviderNoise::set_height_start);
|
||||
ClassDB::bind_method(D_METHOD("get_height_start"), &VoxelProviderNoise::get_height_start);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("set_height_range", "hrange"), &VoxelProviderNoise::set_height_range);
|
||||
ClassDB::bind_method(D_METHOD("get_height_range"), &VoxelProviderNoise::get_height_range);
|
||||
|
||||
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_range"), "set_height_range", "get_height_range");
|
||||
}
|
30
providers/voxel_provider_noise.h
Normal file
30
providers/voxel_provider_noise.h
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef VOXEL_PROVIDER_NOISE_H
|
||||
#define VOXEL_PROVIDER_NOISE_H
|
||||
|
||||
#include "voxel_provider.h"
|
||||
#include <modules/opensimplex/open_simplex_noise.h>
|
||||
|
||||
class VoxelProviderNoise : public VoxelProvider {
|
||||
GDCLASS(VoxelProviderNoise, VoxelProvider)
|
||||
public:
|
||||
void set_noise(Ref<OpenSimplexNoise> noise);
|
||||
Ref<OpenSimplexNoise> get_noise() const;
|
||||
|
||||
void set_height_start(real_t y);
|
||||
real_t get_height_start() const;
|
||||
|
||||
void set_height_range(real_t hrange);
|
||||
real_t get_height_range() const;
|
||||
|
||||
void emerge_block(Ref<VoxelBuffer> out_buffer, Vector3i origin_in_voxels, int lod);
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
private:
|
||||
Ref<OpenSimplexNoise> _noise;
|
||||
float _height_start = 0;
|
||||
float _height_range = 300;
|
||||
};
|
||||
|
||||
#endif // VOXEL_PROVIDER_NOISE_H
|
@ -3,6 +3,7 @@
|
||||
#include "meshers/dmc/voxel_mesher_dmc.h"
|
||||
#include "meshers/transvoxel/voxel_mesher_transvoxel.h"
|
||||
#include "providers/voxel_provider_image.h"
|
||||
#include "providers/voxel_provider_noise.h"
|
||||
#include "providers/voxel_provider_test.h"
|
||||
#include "terrain/voxel_box_mover.h"
|
||||
#include "terrain/voxel_lod_terrain.h"
|
||||
@ -30,6 +31,7 @@ void register_voxel_types() {
|
||||
ClassDB::register_class<VoxelProvider>();
|
||||
ClassDB::register_class<VoxelProviderTest>();
|
||||
ClassDB::register_class<VoxelProviderImage>();
|
||||
ClassDB::register_class<VoxelProviderNoise>();
|
||||
|
||||
// Helpers
|
||||
ClassDB::register_class<VoxelBoxMover>();
|
||||
|
Loading…
Reference in New Issue
Block a user