Update everything
This commit is contained in:
@@ -1,3 +1,8 @@
|
||||
#undef NDEBUG
|
||||
#include <cassert>
|
||||
#include <core/io/resource_loader.h>
|
||||
#include <core/os/file_access.h>
|
||||
#include <core/io/compression.h>
|
||||
#include "voxel_generator_imgmapper.h"
|
||||
#include <modules/voxel/util/fixed_array.h>
|
||||
#include "modules/voxel/util/span.h"
|
||||
@@ -45,6 +50,7 @@ namespace
|
||||
|
||||
} // namespace
|
||||
|
||||
#if 0
|
||||
std::map<int, struct EditBrush *> EditBrushList::brushes;
|
||||
std::map<int, String> EditBrushList::brush_names;
|
||||
struct Brush0 : EditBrush
|
||||
@@ -267,18 +273,21 @@ T *RegBrush(Args &&...args)
|
||||
EditBrushList::register_brush(&data);
|
||||
return &data;
|
||||
}
|
||||
#endif
|
||||
|
||||
VoxelGeneratorImgMapper::VoxelGeneratorImgMapper()
|
||||
: VoxelGeneratorHeightmap(),
|
||||
world_size(10240),
|
||||
grid_size(0)
|
||||
{
|
||||
#if 0
|
||||
RegBrush<Brush0>(_image_draw);
|
||||
RegBrush<Brush1>(_image_draw);
|
||||
RegBrush<Brush2>(_image_draw, curve1);
|
||||
RegBrush<Brush3>(_image_draw, curve1, this);
|
||||
RegBrush<Brush4>(_image_draw, curve2, this);
|
||||
RegBrush<Brush5>(_image_draw, this);
|
||||
#endif
|
||||
}
|
||||
|
||||
VoxelGeneratorImgMapper::~VoxelGeneratorImgMapper()
|
||||
@@ -384,7 +393,7 @@ VoxelGenerator::Result VoxelGeneratorImgMapper::generate_block(VoxelBlockRequest
|
||||
result = VoxelGeneratorHeightmap::generate(
|
||||
out_buffer,
|
||||
[this](int x, int z)
|
||||
{ return get_height_linear(Vector3(x, 0, z)); },
|
||||
{ return get_height_linear2(Vector3(x, 0, z)); },
|
||||
input.origin_in_voxels, input.lod);
|
||||
}
|
||||
|
||||
@@ -412,11 +421,12 @@ float VoxelGeneratorImgMapper::get_height(const Vector3 &v)
|
||||
else
|
||||
return get_height_repeat(**_parameters.image, v.x, v.z);
|
||||
#endif
|
||||
return get_height_linear(v);
|
||||
return get_height_linear2(v);
|
||||
}
|
||||
|
||||
float VoxelGeneratorImgMapper::get_height_linear(const Vector3 &v)
|
||||
{
|
||||
update_heightmaps(v);
|
||||
int px = (int)(v.x / grid_size);
|
||||
int pz = (int)(v.z / grid_size);
|
||||
float mx = (float)px * grid_size;
|
||||
@@ -439,11 +449,37 @@ float VoxelGeneratorImgMapper::get_height_linear(const Vector3 &v)
|
||||
return result;
|
||||
}
|
||||
|
||||
float VoxelGeneratorImgMapper::get_height_linear2(const Vector3 &v)
|
||||
{
|
||||
update_heightmaps(v);
|
||||
int px = (int)v.x + world_size / 2;
|
||||
int pz = (int)v.z + world_size / 2;
|
||||
float mx = (float)px;
|
||||
float mz = (float)pz;
|
||||
// float Mx = mx + (float)grid_size;
|
||||
// float Mz = mz + (float)grid_size;
|
||||
float x_weight = 0.0f;
|
||||
float z_weight = 0.0f;
|
||||
// float d0x = (v.x - mx) / (float)grid_size;
|
||||
// float d0z = (v.z - mz) / (float)grid_size;
|
||||
// float d1x = (mx + (float)grid_size - v.x) / (float)grid_size;
|
||||
// float d1z = (mz + (float)grid_size - v.z) / (float)grid_size;
|
||||
float p0 = get_world_pixel(px, pz);
|
||||
float p1 = get_world_pixel(px + 1, pz);
|
||||
float p2 = get_world_pixel(px, pz + 1);
|
||||
float p3 = get_world_pixel(px + 1, pz + 1);
|
||||
float a = (1.0f - x_weight);
|
||||
float b = (1.0f - z_weight);
|
||||
float result = p0 * a * b + p1 * x_weight * b + p2 * z_weight * a + p3 * x_weight * z_weight;
|
||||
return result;
|
||||
}
|
||||
|
||||
float VoxelGeneratorImgMapper::get_height_full(const Vector3 &v)
|
||||
{
|
||||
return get_height(v) * 200.0f - 100.0f;
|
||||
}
|
||||
|
||||
#if 0
|
||||
void VoxelGeneratorImgMapper::draw_brush(const Vector3 &v, float r, float s, int id)
|
||||
{
|
||||
print_line("draw_brush " + (v.operator String()) + " " +
|
||||
@@ -492,191 +528,335 @@ void VoxelGeneratorImgMapper::draw_brush(const Vector3 &v, float r, float s, int
|
||||
float base_h = get_height_full(v);
|
||||
float scale = s / 100.0f;
|
||||
EditBrushList::draw_brush(v, r, s, id);
|
||||
#if 0
|
||||
switch (id)
|
||||
}
|
||||
#endif
|
||||
|
||||
static inline Vector2 world2img(Ref<Image> img, const Vector3 &v)
|
||||
{
|
||||
|
||||
Vector3 pos(v.x, v.y, v.z);
|
||||
Vector2 pt = Vector2(pos.x * 0.1f + img->get_width() / 2,
|
||||
pos.z * 0.1f + img->get_height() / 2);
|
||||
pt.x = CLAMP(pt.x, 1, img->get_width() - 1);
|
||||
pt.y = CLAMP(pt.y, 1, img->get_height() - 1);
|
||||
return pt;
|
||||
}
|
||||
|
||||
uint64_t VoxelGeneratorImgMapper::heightmap_index(const Vector3 &v)
|
||||
{
|
||||
int heightmap_size = 1024;
|
||||
int world_offset = world_size / 2;
|
||||
int wx = (int)v.x + world_offset;
|
||||
int wz = (int)v.z + world_offset;
|
||||
uint64_t heightmap_index = ((wz / heightmap_size) << 16) + wx / heightmap_size;
|
||||
return heightmap_index;
|
||||
}
|
||||
|
||||
static Set<uint64_t> loaded_tiles;
|
||||
static Set<uint64_t> dirty_tiles;
|
||||
static HashMap<uint64_t, uint8_t *> tiles;
|
||||
void VoxelGeneratorImgMapper::get_or_create_tile(uint64_t tile)
|
||||
{
|
||||
int heightmap_size = 1024;
|
||||
Error err = OK;
|
||||
String path = "res://terrain/" + String::hex_encode_buffer((uint8_t *)&tile, sizeof(tile)) + ".bin";
|
||||
Vector<uint8_t> buffer;
|
||||
if (loaded_tiles.has(tile))
|
||||
return;
|
||||
print_line("Loading: " + path + ": " + itos(loaded_tiles.size()));
|
||||
buffer = FileAccess::get_file_as_array(path, &err);
|
||||
if (err == OK)
|
||||
{
|
||||
case 0:
|
||||
/* flat stuff */
|
||||
for (i = -xs; i < xs + 1; i++)
|
||||
for (j = -xs; j < xs + 1; j++)
|
||||
{
|
||||
float xr = Vector2(i, j).length();
|
||||
if (xr < r)
|
||||
{
|
||||
Vector3 pos(v.x + i, v.y, v.z + j);
|
||||
Vector2 pt = Vector2(pos.x * 0.1f + _image_draw->get_width() / 2,
|
||||
pos.z * 0.1f + _image_draw->get_height() / 2);
|
||||
pt.x = CLAMP(pt.x, 1, _image_draw->get_width() - 1);
|
||||
pt.y = CLAMP(pt.y, 1, _image_draw->get_height() - 1);
|
||||
_image_draw->fill_rect(Rect2(pt - Vector2(1, 1), Vector2(2, 2)), Color(c, 0, 0, 1));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
/* erase stuff */
|
||||
for (i = -xs; i < xs + 1; i++)
|
||||
for (j = -xs; j < xs + 1; j++)
|
||||
{
|
||||
float xr = Vector2(i, j).length();
|
||||
if (xr < r)
|
||||
{
|
||||
Vector3 pos(v.x + i, v.y, v.z + j);
|
||||
Vector2 pt = Vector2(pos.x * 0.1f + _image_draw->get_width() / 2,
|
||||
pos.z * 0.1f + _image_draw->get_height() / 2);
|
||||
pt.x = CLAMP(pt.x, 1, _image_draw->get_width() - 1);
|
||||
pt.y = CLAMP(pt.y, 1, _image_draw->get_height() - 1);
|
||||
_image_draw->fill_rect(Rect2(pt - Vector2(1, 1), Vector2(2, 2)), Color(c, 0, 0, 0));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
/* abs draw curve */
|
||||
ERR_FAIL_COND(!curve1.is_valid());
|
||||
for (i = -xs; i < xs + 1; i++)
|
||||
for (j = -xs; j < xs + 1; j++)
|
||||
{
|
||||
float xr = Vector2(i, j).length();
|
||||
if (xr < r)
|
||||
{
|
||||
float h = curve1->interpolate_baked(xr / r) * scale;
|
||||
h = CLAMP(h, -100.0, 100.0f);
|
||||
c = (h + 100.0f) / 200.0;
|
||||
int i;
|
||||
uint8_t *buffer_dst = memnew_arr(uint8_t, heightmap_size * heightmap_size);
|
||||
memset(buffer_dst, 127, heightmap_size * heightmap_size);
|
||||
Compression::decompress(buffer_dst, heightmap_size * heightmap_size, buffer.ptr() + 8, buffer.size() - 8, Compression::MODE_FASTLZ);
|
||||
tiles[tile] = buffer_dst;
|
||||
loaded_tiles.insert(tile);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint8_t *buffer_dst = memnew_arr(uint8_t, heightmap_size * heightmap_size);
|
||||
memset(buffer_dst, 127, heightmap_size * heightmap_size);
|
||||
tiles[tile] = buffer_dst;
|
||||
loaded_tiles.insert(tile);
|
||||
dirty_tiles.insert(tile);
|
||||
}
|
||||
}
|
||||
|
||||
Vector3 pos(v.x + i, v.y, v.z + j);
|
||||
Vector2 pt = Vector2(pos.x * 0.1f + _image_draw->get_width() / 2,
|
||||
pos.z * 0.1f + _image_draw->get_height() / 2);
|
||||
pt.x = CLAMP(pt.x, 1, _image_draw->get_width() - 1);
|
||||
pt.y = CLAMP(pt.y, 1, _image_draw->get_height() - 1);
|
||||
_image_draw->fill_rect(Rect2(pt - Vector2(1, 1), Vector2(2, 2)), Color(c, 0, 0, 1));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
/* rel draw curve1 */
|
||||
ERR_FAIL_COND(!curve1.is_valid());
|
||||
for (i = -xs; i < xs + 1; i++)
|
||||
for (j = -xs; j < xs + 1; j++)
|
||||
{
|
||||
float xr = Vector2(i, j).length();
|
||||
if (xr < r)
|
||||
{
|
||||
float h = curve1->interpolate_baked(xr / r) * scale;
|
||||
if (h < -2.0f || h > 2.0f)
|
||||
{
|
||||
h += get_height_full(v + Vector3(i, 0.0f, j));
|
||||
h = CLAMP(h, -100.0, 100.0f);
|
||||
c = (h + 100.0f) / 200.0;
|
||||
float VoxelGeneratorImgMapper::get_world_pixel(int wx, int wy)
|
||||
{
|
||||
int heightmap_size = 1024;
|
||||
uint64_t tile_x = (wx / heightmap_size) & 0xFFFF;
|
||||
uint64_t tile_z = (wy / heightmap_size) & 0xFFFF;
|
||||
uint64_t tile = tile_x | (tile_z << 16);
|
||||
assert(loaded_tiles.has(tile));
|
||||
if (loaded_tiles.has(tile))
|
||||
{
|
||||
int px = (wx % heightmap_size);
|
||||
int py = (wy % heightmap_size);
|
||||
assert(loaded_tiles.has(tile));
|
||||
assert(tiles[tile]);
|
||||
int raw_pixel = tiles[tile][px + py * heightmap_size];
|
||||
float ret = (float)(raw_pixel) / 255.0f;
|
||||
return ret;
|
||||
}
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
Vector3 pos(v.x + i, v.y, v.z + j);
|
||||
Vector2 pt = Vector2(pos.x * 0.1f + _image_draw->get_width() / 2,
|
||||
pos.z * 0.1f + _image_draw->get_height() / 2);
|
||||
pt.x = CLAMP(pt.x, 1, _image_draw->get_width() - 1);
|
||||
pt.y = CLAMP(pt.y, 1, _image_draw->get_height() - 1);
|
||||
_image_draw->fill_rect(Rect2(pt - Vector2(1, 1), Vector2(2, 2)), Color(c, 0, 0, 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
/* rel draw curve2 */
|
||||
ERR_FAIL_COND(!curve2.is_valid());
|
||||
for (i = -xs; i < xs + 1; i++)
|
||||
for (j = -xs; j < xs + 1; j++)
|
||||
{
|
||||
float xr = Vector2(i, j).length();
|
||||
if (xr < r)
|
||||
{
|
||||
float h = curve2->interpolate_baked(xr / r) * scale;
|
||||
if (h < -2.0f || h > 2.0f)
|
||||
{
|
||||
h += get_height_full(v + Vector3(i, 0.0f, j));
|
||||
h = CLAMP(h, -100.0, 100.0f);
|
||||
c = (h + 100.0f) / 200.0;
|
||||
void VoxelGeneratorImgMapper::set_world_pixel(int wx, int wy, float color)
|
||||
{
|
||||
int heightmap_size = 1024;
|
||||
uint64_t tile_x = (wx / heightmap_size) & 0xFFFF;
|
||||
uint64_t tile_z = (wy / heightmap_size) & 0xFFFF;
|
||||
uint64_t tile = tile_x | (tile_z << 16);
|
||||
// print_line("world pixel: " + itos(wx) + " " + itos(wy) + String::num(color));
|
||||
assert(loaded_tiles.has(tile));
|
||||
assert(tiles[tile]);
|
||||
if (loaded_tiles.has(tile))
|
||||
{
|
||||
int px = (wx % heightmap_size);
|
||||
int py = (wy % heightmap_size);
|
||||
int raw_pixels = (int)(color * 255.0f);
|
||||
tiles[tile][px + py * heightmap_size] = raw_pixels;
|
||||
dirty_tiles.insert(tile);
|
||||
// print_line("dirty tile: " + String::num_uint64(tile, 16));
|
||||
}
|
||||
}
|
||||
|
||||
Vector3 pos(v.x + i, v.y, v.z + j);
|
||||
Vector2 pt = Vector2(pos.x * 0.1f + _image_draw->get_width() / 2,
|
||||
pos.z * 0.1f + _image_draw->get_height() / 2);
|
||||
pt.x = CLAMP(pt.x, 1, _image_draw->get_width() - 1);
|
||||
pt.y = CLAMP(pt.y, 1, _image_draw->get_height() - 1);
|
||||
_image_draw->fill_rect(Rect2(pt - Vector2(1, 1), Vector2(2, 2)), Color(c, 0, 0, 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
#if 0
|
||||
void VoxelGeneratorImgMapper::save_dirty_tiles()
|
||||
{
|
||||
Set<uint64_t>::Element *e = dirty_tiles.front();
|
||||
int heightmap_size = 1024;
|
||||
print_line("Save Dirty Tiles");
|
||||
while (e)
|
||||
{
|
||||
uint64_t tile = e->get();
|
||||
Vector<uint8_t> buffer;
|
||||
buffer.resize(heightmap_size * heightmap_size + 8);
|
||||
buffer.write[0] = (uint8_t)(heightmap_size % 256);
|
||||
buffer.write[1] = (uint8_t)(heightmap_size >> 8);
|
||||
buffer.write[2] = (uint8_t)(heightmap_size % 256);
|
||||
buffer.write[3] = (uint8_t)(heightmap_size >> 8);
|
||||
buffer.write[4] = 0;
|
||||
buffer.write[5] = 0;
|
||||
buffer.write[6] = 0;
|
||||
buffer.write[7] = 0;
|
||||
assert(loaded_tiles.has(tile));
|
||||
assert(tiles[tile]);
|
||||
int size = Compression::compress(buffer.ptrw() + 8, tiles[tile], heightmap_size * heightmap_size, Compression::MODE_FASTLZ);
|
||||
Error err = OK;
|
||||
String path = "res://terrain/" + String::hex_encode_buffer((uint8_t *)&tile, sizeof(tile)) + ".bin";
|
||||
print_line("Saving: " + path + ": " + itos(loaded_tiles.size()));
|
||||
FileAccess *pfd = FileAccess::open(path, FileAccess::WRITE, &err);
|
||||
if (err == OK && pfd)
|
||||
{
|
||||
Ref<Curve> curve;
|
||||
if (id == 3)
|
||||
curve = curve1;
|
||||
else if (id == 4)
|
||||
curve = curve2;
|
||||
ERR_FAIL_COND(!curve.is_valid());
|
||||
Ref<Image> img;
|
||||
img.instance();
|
||||
img->create(xs * 2, xs * 2, false, Image::FORMAT_RGBA8);
|
||||
img->fill(Color(0, 0, 0, 0));
|
||||
for (i = -xs ; i < xs + 1; i++)
|
||||
for (j = -xs ; j < xs + 1; j++) {
|
||||
float xr = Vector2(i, j).length();
|
||||
if (xr < r) {
|
||||
float h = curve->interpolate_baked(xr / r);
|
||||
if (h < -2.0f || h > 2.0f) {
|
||||
h += get_height_full(v + Vector3(i, 0.0f, j));
|
||||
h = CLAMP(h, -100.0, 100.0f);
|
||||
c = (h + 100.0f) / 200.0;
|
||||
pfd->store_buffer(buffer.ptr(), size + 8);
|
||||
pfd->close();
|
||||
}
|
||||
e = e->next();
|
||||
}
|
||||
dirty_tiles.clear();
|
||||
}
|
||||
|
||||
img->fill_rect(Rect2(Vector2(i + xs, j + xs), Vector2(1, 1)), Color(c, 0, 0, 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
Vector3 pos(v.x, v.y, v.z);
|
||||
Vector2 pt = Vector2(pos.x * 0.1f + _image_draw->get_width() / 2,
|
||||
pos.z * 0.1f + _image_draw->get_height() / 2);
|
||||
pt.x = CLAMP(pt.x, 1, _image_draw->get_width() - 1);
|
||||
pt.y = CLAMP(pt.y, 1, _image_draw->get_height() - 1);
|
||||
_image_draw->blend_rect(img,
|
||||
Rect2(Vector2(), Vector2(img->get_width(), img->get_height())),
|
||||
Vector2(pt.x - xs, pt.y - xs));
|
||||
|
||||
} break;
|
||||
#endif
|
||||
case 5:
|
||||
void VoxelGeneratorImgMapper::draw_brush(const Vector3 &v, Ref<Curve> curve, float radius, float strength, int flags)
|
||||
{
|
||||
float min_x = v.x - radius;
|
||||
float min_z = v.z - radius;
|
||||
float max_x = v.x + radius;
|
||||
float max_z = v.z + radius;
|
||||
float step = grid_size;
|
||||
Vector3 min_pos(min_x, v.y, min_z), max_pos(max_x, v.y, max_z);
|
||||
int x, z;
|
||||
update_heightmaps(v);
|
||||
if (radius < 1.0f || strength <= 0.0f)
|
||||
return;
|
||||
if (!_image_draw.is_valid())
|
||||
return;
|
||||
if (!brush_canvas.is_valid())
|
||||
brush_canvas = _image_draw->duplicate();
|
||||
brush_canvas->blit_rect(_image_draw, Rect2(0, 0, _image_draw->get_width(), _image_draw->get_height()), Vector2());
|
||||
int xpos_min = min_pos.x / grid_size + _image_draw->get_width() / 2;
|
||||
int xpos_max = max_pos.x / grid_size + _image_draw->get_width() / 2;
|
||||
int zpos_min = min_pos.z / grid_size + _image_draw->get_height() / 2;
|
||||
int zpos_max = max_pos.z / grid_size + _image_draw->get_height() / 2;
|
||||
int radius_int = Math::ceil(radius / grid_size);
|
||||
int center_x = v.x / grid_size + _image_draw->get_width() / 2;
|
||||
int center_z = v.z / grid_size + _image_draw->get_height() / 2;
|
||||
int wx_min = world_size / 2 + (int)min_pos.x;
|
||||
int wx_max = world_size / 2 + (int)max_pos.x;
|
||||
int wz_min = world_size / 2 + (int)min_pos.z;
|
||||
int wz_max = world_size / 2 + (int)max_pos.z;
|
||||
int wx_center = world_size / 2 + (int)v.x;
|
||||
int wz_center = world_size / 2 + (int)v.z;
|
||||
gen.set_seed(wx_center + world_size * wz_center);
|
||||
// abs + offt (1 << 0)
|
||||
// abs (1 << 1)
|
||||
// rnd (1 << 2)
|
||||
// flatten (1 << 3)
|
||||
// smooth (1 << 4)
|
||||
// no increments (1 << 5)
|
||||
float avg = 0.0f;
|
||||
float minv = Math_INF;
|
||||
float maxv = -Math_INF;
|
||||
if (flags & 16)
|
||||
{
|
||||
float h = get_height_full(v) + 2.0f;
|
||||
h = CLAMP(h, -100.0, 100.0f);
|
||||
for (i = -8; i < 8 + 1; i++)
|
||||
for (j = -8; j < 8 + 1; j++)
|
||||
int count = 0;
|
||||
for (z = wz_min; z <= wz_max; z++)
|
||||
{
|
||||
for (x = wx_min; x <= wx_max; x++)
|
||||
{
|
||||
Vector3 pos(v.x + i, v.y, v.z + j);
|
||||
h = MAX(h, get_height_full(pos) + 2.0f);
|
||||
float r = Vector2((x - wx_center), (z - wz_center)).length();
|
||||
if (r >= radius)
|
||||
continue;
|
||||
float h = get_world_pixel(x, z);
|
||||
if (minv > h)
|
||||
minv = h;
|
||||
if (maxv < h)
|
||||
maxv = h;
|
||||
avg += h;
|
||||
count++;
|
||||
}
|
||||
c = (h + 100.0f) / 200.0;
|
||||
for (i = -xs; i < xs + 1; i++)
|
||||
for (j = -xs; j < xs + 1; j++)
|
||||
}
|
||||
avg /= (float)count;
|
||||
}
|
||||
print_line("AVG: " + String::num(avg));
|
||||
for (z = wz_min; z <= wz_max; z++)
|
||||
{
|
||||
for (x = wx_min; x <= wx_max; x++)
|
||||
{
|
||||
float r = Vector2((x - wx_center), (z - wz_center)).length();
|
||||
if (r >= radius)
|
||||
continue;
|
||||
float offset = r / radius;
|
||||
float value = curve->interpolate_baked(offset) * strength;
|
||||
float nvalue = value * 0.5f; // -1:1 range
|
||||
float mvalue = nvalue * 0.5f + 0.5f; // 0:1 range
|
||||
float nh = CLAMP((v.y + 100.0f) / 200.0f, 0.0f, 1.0f);
|
||||
float h = get_world_pixel(x, z);
|
||||
float new_h = h + nvalue;
|
||||
float c;
|
||||
if (flags & 1) // absolute with offset
|
||||
new_h = nh + nvalue;
|
||||
else if (flags & 2) // absolute
|
||||
new_h = nvalue;
|
||||
if (flags & 8)
|
||||
new_h = nh;
|
||||
else if (flags & 16)
|
||||
{
|
||||
float xr = Vector2(i, j).length();
|
||||
if (xr < r)
|
||||
int ox, oz;
|
||||
float hu = maxv - avg;
|
||||
float hb = avg - minv;
|
||||
new_h = avg + (h - avg) * 0.9f;
|
||||
if (z > 0 && z < world_size - 1 && x > 0 && x < world_size - 1)
|
||||
{
|
||||
Vector3 pos(v.x + i, v.y, v.z + j);
|
||||
Vector2 pt = Vector2(pos.x * 0.1f + _image_draw->get_width() / 2,
|
||||
pos.z * 0.1f + _image_draw->get_height() / 2);
|
||||
pt.x = CLAMP(pt.x, 1, _image_draw->get_width() - 1);
|
||||
pt.y = CLAMP(pt.y, 1, _image_draw->get_height() - 1);
|
||||
_image_draw->fill_rect(Rect2(pt - Vector2(1, 1), Vector2(2, 2)), Color(c, 0, 0, 1));
|
||||
float smooth_h = 0.0f;
|
||||
int smooth_count = 0;
|
||||
for (oz = 0; oz < 5; oz++)
|
||||
for (ox = 0; ox < 5; ox++)
|
||||
{
|
||||
if (ox == 0 && oz == 0)
|
||||
continue;
|
||||
smooth_h += get_world_pixel(x + ox - 2, z + oz - 2);
|
||||
smooth_count++;
|
||||
}
|
||||
new_h = new_h * 0.5f + smooth_h / (float)smooth_count * 0.5f;
|
||||
}
|
||||
}
|
||||
if (flags & 4)
|
||||
new_h = 0.95f * new_h + 0.05f * (gen.randf() - 0.5f);
|
||||
// relative
|
||||
if (flags & 32)
|
||||
c = CLAMP(new_h, 0.0f, 1.0f);
|
||||
else
|
||||
{
|
||||
if (offset > 0.9f)
|
||||
c = CLAMP(h * 0.95f + new_h * 0.05f, 0.0f, 1.0f);
|
||||
else
|
||||
c = CLAMP(h * 0.8f + new_h * 0.2f, 0.0f, 1.0f);
|
||||
}
|
||||
set_world_pixel(x, z, c);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
#if 0
|
||||
int wz = world_size / 2 + (int)v.z;
|
||||
brush_canvas->lock();
|
||||
for (z = zpos_min; z <= zpos_max; z++)
|
||||
{
|
||||
for (x = xpos_min; x <= xpos_max; x++)
|
||||
{
|
||||
float r = Vector2((x - center_x) * grid_size, (z - center_z) * grid_size).length();
|
||||
if (r >= radius)
|
||||
continue;
|
||||
float offset = r / radius;
|
||||
float value = curve->interpolate_baked(offset) * strength;
|
||||
float nvalue = value * 0.5f; // -1:1 range
|
||||
float nh = CLAMP((v.y + 100.0f) / 200.0f, 0.0f, 1.0f);
|
||||
int xx = CLAMP(x, 0, _image_draw->get_width() - 1);
|
||||
int zz = CLAMP(z, 0, _image_draw->get_height() - 1);
|
||||
// height in 0-1 range where 0.5 is zero
|
||||
float h = brush_canvas->get_pixel(xx, zz).r;
|
||||
// print_line("h: " + String::num(h));
|
||||
float new_h;
|
||||
float c;
|
||||
new_h = h + nvalue;
|
||||
if (flags & 1) // absolute
|
||||
new_h = nh + nvalue;
|
||||
// relative
|
||||
if (offset > 0.9f)
|
||||
c = CLAMP(h * 0.95f + new_h * 0.05f, 0.0f, 1.0f);
|
||||
else
|
||||
c = CLAMP(h * 0.8f + new_h * 0.2f, 0.0f, 1.0f);
|
||||
// print_line("c: " + String::num(c));
|
||||
brush_canvas->set_pixel(xx, zz, Color(c, 0, 0, 1));
|
||||
}
|
||||
}
|
||||
brush_canvas->unlock();
|
||||
_image_draw->blit_rect(brush_canvas, Rect2(0, 0, brush_canvas->get_width(), brush_canvas->get_height()), Vector2());
|
||||
#endif
|
||||
save_dirty_tiles();
|
||||
}
|
||||
static Mutex update_heightmaps_lock;
|
||||
void VoxelGeneratorImgMapper::update_heightmaps(const Vector3 &v)
|
||||
{
|
||||
int i;
|
||||
int heightmap_size = 1024;
|
||||
uint64_t pos_x = (int)v.x + (world_size / 2);
|
||||
uint64_t pos_z = (int)v.z + (world_size / 2);
|
||||
uint64_t tile_x = (pos_x / heightmap_size) & 0xFFFF;
|
||||
uint64_t tile_z = (pos_z / heightmap_size) & 0xFFFF;
|
||||
Vector<uint64_t> tiles;
|
||||
tiles.resize(9);
|
||||
for (i = 0; i < tiles.size(); i++)
|
||||
{
|
||||
int m_x = (i % 3) - 1;
|
||||
int m_z = (i / 3) - 1;
|
||||
tiles.write[i] = (tile_z << 16) | tile_x;
|
||||
if (tile_x == 0 && m_x < 0)
|
||||
continue;
|
||||
if (tile_z == 0 && m_z < 0)
|
||||
continue;
|
||||
tiles.write[i] = ((tile_z + m_x) << 16) | (tile_x + m_z);
|
||||
}
|
||||
Vector3 pos = v;
|
||||
pos.y = 0;
|
||||
update_heightmaps_lock.lock();
|
||||
if (current_position.is_equal_approx(pos))
|
||||
goto out_unlock;
|
||||
for (i = 0; i < tiles.size(); i++)
|
||||
{
|
||||
if (!loaded_tiles.has(tiles[i]))
|
||||
get_or_create_tile(tiles[i]);
|
||||
}
|
||||
current_position = pos;
|
||||
out_unlock:
|
||||
update_heightmaps_lock.unlock();
|
||||
}
|
||||
|
||||
void VoxelGeneratorImgMapper::compose()
|
||||
{
|
||||
Ref<Image> copy;
|
||||
print_line("compose");
|
||||
// print_line("compose");
|
||||
ERR_FAIL_COND(!_image_bg.is_valid());
|
||||
ERR_FAIL_COND(!_image_overlay.is_valid());
|
||||
ERR_FAIL_COND(!_image_draw.is_valid());
|
||||
@@ -723,6 +903,7 @@ void VoxelGeneratorImgMapper::save_png()
|
||||
_image_overlay->save_png("res://terrain/terrain_edit.png");
|
||||
}
|
||||
|
||||
#if 0
|
||||
void VoxelGeneratorImgMapper::set_curve1(const Ref<Curve> &curve)
|
||||
{
|
||||
ERR_FAIL_COND(!curve.is_valid());
|
||||
@@ -770,6 +951,7 @@ Ref<Curve> VoxelGeneratorImgMapper::get_curve4() const
|
||||
{
|
||||
return curve4;
|
||||
}
|
||||
#endif
|
||||
|
||||
void VoxelGeneratorImgMapper::set_grid_size(int value)
|
||||
{
|
||||
@@ -791,10 +973,12 @@ int VoxelGeneratorImgMapper::get_world_size() const
|
||||
return world_size;
|
||||
}
|
||||
|
||||
#if 0
|
||||
std::map<int, String> VoxelGeneratorImgMapper::get_brush_names() const
|
||||
{
|
||||
return EditBrushList::get_brush_names();
|
||||
}
|
||||
#endif
|
||||
|
||||
void VoxelGeneratorImgMapper::_bind_methods()
|
||||
{
|
||||
@@ -807,6 +991,7 @@ void VoxelGeneratorImgMapper::_bind_methods()
|
||||
ClassDB::bind_method(D_METHOD("set_image_draw", "image"), &VoxelGeneratorImgMapper::set_image_draw);
|
||||
ClassDB::bind_method(D_METHOD("get_image_draw"), &VoxelGeneratorImgMapper::get_image_draw);
|
||||
|
||||
#if 0
|
||||
ClassDB::bind_method(D_METHOD("set_curve1", "curve"), &VoxelGeneratorImgMapper::set_curve1);
|
||||
ClassDB::bind_method(D_METHOD("get_curve1"), &VoxelGeneratorImgMapper::get_curve1);
|
||||
ClassDB::bind_method(D_METHOD("set_curve2", "curve"), &VoxelGeneratorImgMapper::set_curve2);
|
||||
@@ -815,9 +1000,9 @@ void VoxelGeneratorImgMapper::_bind_methods()
|
||||
ClassDB::bind_method(D_METHOD("get_curve3"), &VoxelGeneratorImgMapper::get_curve3);
|
||||
ClassDB::bind_method(D_METHOD("set_curve4", "curve"), &VoxelGeneratorImgMapper::set_curve4);
|
||||
ClassDB::bind_method(D_METHOD("get_curve4"), &VoxelGeneratorImgMapper::get_curve4);
|
||||
#endif
|
||||
|
||||
ClassDB::bind_method(D_METHOD("set_height", "v", "height"), &VoxelGeneratorImgMapper::set_height);
|
||||
ClassDB::bind_method(D_METHOD("draw_brush", "v", "r", "s", "id"), &VoxelGeneratorImgMapper::draw_brush);
|
||||
ClassDB::bind_method(D_METHOD("get_height", "v"), &VoxelGeneratorImgMapper::get_height);
|
||||
ClassDB::bind_method(D_METHOD("get_height_full", "v"), &VoxelGeneratorImgMapper::get_height_full);
|
||||
ClassDB::bind_method(D_METHOD("compose"), &VoxelGeneratorImgMapper::compose);
|
||||
@@ -832,9 +1017,11 @@ void VoxelGeneratorImgMapper::_bind_methods()
|
||||
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "image_bg", PROPERTY_HINT_RESOURCE_TYPE, "Image"), "set_image_bg", "get_image_bg");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "image_overlay", PROPERTY_HINT_RESOURCE_TYPE, "Image"), "set_image_overlay", "get_image_overlay");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "image_draw", PROPERTY_HINT_RESOURCE_TYPE, "Image"), "set_image_draw", "get_image_draw");
|
||||
#if 0
|
||||
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve1", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve1", "get_curve1");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve2", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve2", "get_curve2");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve3", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve3", "get_curve3");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve4", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve4", "get_curve4");
|
||||
#endif
|
||||
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "blur_enabled"), "set_blur_enabled", "is_blur_enabled");
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#undef NDEBUG
|
||||
#include <cassert>
|
||||
#include <map>
|
||||
#include <core/math/random_number_generator.h>
|
||||
#include <modules/voxel/generators/simple/voxel_generator_heightmap.h>
|
||||
#include <core/image.h>
|
||||
#include <scene/resources/curve.h>
|
||||
@@ -21,6 +22,7 @@ struct EditBrush
|
||||
}
|
||||
};
|
||||
|
||||
#if 0
|
||||
struct EditBrushList
|
||||
{
|
||||
static std::map<int, struct EditBrush *> brushes;
|
||||
@@ -46,6 +48,7 @@ public:
|
||||
return brush_names;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
// Provides infinite tiling heightmap based on an image
|
||||
class VoxelGeneratorImgMapper : public VoxelGeneratorHeightmap
|
||||
@@ -74,14 +77,25 @@ public:
|
||||
Result generate_block(VoxelBlockRequest &input) override;
|
||||
|
||||
void set_height(const Vector3 &v, float height);
|
||||
#if 0
|
||||
void draw_brush(const Vector3 &v, float r, float s, int id);
|
||||
#endif
|
||||
void draw_brush(const Vector3 &v, Ref<Curve> curve, float radius, float strength, int flags);
|
||||
void update_heightmaps(const Vector3 &v);
|
||||
uint64_t heightmap_index(const Vector3 &v);
|
||||
void get_or_create_tile(uint64_t tile);
|
||||
float get_world_pixel(int wx, int wy);
|
||||
void set_world_pixel(int wx, int wy, float color);
|
||||
void save_dirty_tiles();
|
||||
float get_height(const Vector3 &v);
|
||||
float get_height_linear(const Vector3 &v);
|
||||
float get_height_linear2(const Vector3 &v);
|
||||
float get_height_full(const Vector3 &v);
|
||||
float get_height_full_linear(const Vector3 &v);
|
||||
void compose();
|
||||
void save_png();
|
||||
|
||||
#if 0
|
||||
void set_curve1(const Ref<Curve> &curve);
|
||||
Ref<Curve> get_curve1() const;
|
||||
|
||||
@@ -93,10 +107,13 @@ public:
|
||||
|
||||
void set_curve4(const Ref<Curve> &curve);
|
||||
Ref<Curve> get_curve4() const;
|
||||
#endif
|
||||
|
||||
void set_world_size(int value);
|
||||
int get_world_size() const;
|
||||
#if 0
|
||||
std::map<int, String> get_brush_names() const;
|
||||
#endif
|
||||
|
||||
private:
|
||||
static void _bind_methods();
|
||||
@@ -107,6 +124,9 @@ private:
|
||||
Ref<Image> _image_bg;
|
||||
Ref<Image> _image_overlay, _image_buildings;
|
||||
Ref<Image> _image_draw;
|
||||
Ref<Image> brush_canvas;
|
||||
Vector3 current_position;
|
||||
RandomNumberGenerator gen;
|
||||
|
||||
Ref<Curve> curve1, curve2, curve3, curve4;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user