2018-09-27 17:11:28 -07:00
|
|
|
#include "voxel_provider_image.h"
|
|
|
|
|
2019-04-24 16:54:14 -07:00
|
|
|
VoxelProviderImage::VoxelProviderImage() :
|
|
|
|
_channel(0) {
|
2018-09-27 17:11:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelProviderImage::set_image(Ref<Image> im) {
|
|
|
|
_image = im;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Image> VoxelProviderImage::get_image() const {
|
|
|
|
return _image;
|
|
|
|
}
|
|
|
|
|
2019-04-24 16:54:14 -07:00
|
|
|
void VoxelProviderImage::set_channel(VoxelBuffer::ChannelId channel) {
|
2018-09-27 17:11:28 -07:00
|
|
|
_channel = channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VoxelProviderImage::get_channel() const {
|
|
|
|
return _channel;
|
|
|
|
}
|
|
|
|
|
2019-04-26 18:08:12 -07:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
inline int umod(int a, int b) {
|
|
|
|
return ((unsigned int)a - (a < 0)) % (unsigned int)b;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline float get_height_repeat(Image &im, int x, int y) {
|
|
|
|
return im.get_pixel(umod(x, im.get_width()), umod(y, im.get_height())).r;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline float get_height_blurred(Image &im, int x, int y) {
|
|
|
|
float h = get_height_repeat(im, x, y);
|
|
|
|
h += get_height_repeat(im, x + 1, y);
|
|
|
|
h += get_height_repeat(im, x - 1, y);
|
|
|
|
h += get_height_repeat(im, x, y + 1);
|
|
|
|
h += get_height_repeat(im, x, y - 1);
|
|
|
|
return h * 0.2f;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-04-29 13:57:39 -07:00
|
|
|
void VoxelProviderImage::emerge_block(Ref<VoxelBuffer> p_out_buffer, Vector3i origin_in_voxels, int lod) {
|
|
|
|
|
2018-09-27 17:11:28 -07:00
|
|
|
int ox = origin_in_voxels.x;
|
|
|
|
int oy = origin_in_voxels.y;
|
|
|
|
int oz = origin_in_voxels.z;
|
|
|
|
|
|
|
|
Image &image = **_image;
|
|
|
|
VoxelBuffer &out_buffer = **p_out_buffer;
|
|
|
|
|
|
|
|
image.lock();
|
|
|
|
|
|
|
|
int x = 0;
|
|
|
|
int z = 0;
|
|
|
|
|
|
|
|
int bs = out_buffer.get_size().x;
|
|
|
|
|
|
|
|
int dirt = 1;
|
|
|
|
|
2019-05-03 15:55:52 -07:00
|
|
|
float hbase = 50.0;
|
|
|
|
float hspan = 200.0;
|
|
|
|
|
2018-09-27 17:11:28 -07:00
|
|
|
while (z < bs) {
|
|
|
|
while (x < bs) {
|
|
|
|
|
2019-05-03 15:55:52 -07:00
|
|
|
int lx = x << lod;
|
|
|
|
int lz = z << lod;
|
|
|
|
|
2019-04-24 16:54:14 -07:00
|
|
|
if (_channel == VoxelBuffer::CHANNEL_ISOLEVEL) {
|
|
|
|
|
2019-05-03 15:55:52 -07:00
|
|
|
float h = get_height_blurred(image, ox + lx, oz + lz) * hspan - hbase;
|
2019-04-26 18:08:12 -07:00
|
|
|
|
2019-04-24 16:54:14 -07:00
|
|
|
for (int y = 0; y < bs; ++y) {
|
2019-05-03 15:55:52 -07:00
|
|
|
int ly = y << lod;
|
|
|
|
out_buffer.set_voxel_f((oy + ly) - h, x, y, z, _channel);
|
2019-04-24 16:54:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2019-05-03 15:55:52 -07:00
|
|
|
float h = get_height_repeat(image, ox + lx, oz + lz) * hspan - hbase;
|
2019-04-24 16:54:14 -07:00
|
|
|
h -= oy;
|
|
|
|
int ih = int(h);
|
|
|
|
if (ih > 0) {
|
|
|
|
if (ih > bs) {
|
|
|
|
ih = bs;
|
|
|
|
}
|
|
|
|
out_buffer.fill_area(dirt, Vector3(x, 0, z), Vector3(x + 1, ih, z + 1), _channel);
|
2018-09-27 17:11:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
x += 1;
|
|
|
|
}
|
|
|
|
z += 1;
|
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
image.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelProviderImage::_bind_methods() {
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_image", "image"), &VoxelProviderImage::set_image);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_image"), &VoxelProviderImage::get_image);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_channel", "channel"), &VoxelProviderImage::set_channel);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_channel"), &VoxelProviderImage::get_channel);
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "image", PROPERTY_HINT_RESOURCE_TYPE, "Image"), "set_image", "get_image");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "channel"), "set_channel", "get_channel");
|
|
|
|
}
|