diff --git a/voxel_isosurface_tool.cpp b/voxel_isosurface_tool.cpp index b2d4a94..58f8d4d 100644 --- a/voxel_isosurface_tool.cpp +++ b/voxel_isosurface_tool.cpp @@ -114,12 +114,44 @@ void VoxelIsoSurfaceTool::do_cube(Transform transform, Vector3 extents, Operatio for (int y = 0; y < buffer.get_size().y; ++y) { Vector3 pos = inv_transform.xform(Vector3(x, y, z)); - do_op(buffer, x, y, z, sdf_cube(pos, extents), op); + do_op(buffer, x, y, z, sdf_cube(pos, extents) * _iso_scale, op); } } } } +void VoxelIsoSurfaceTool::do_heightmap(Ref heightmap, Vector3 offset, real_t vertical_scale, Operation op) { + + ERR_FAIL_COND(_buffer.is_null()); + VoxelBuffer &buffer = **_buffer; + + ERR_FAIL_COND(heightmap.is_null()); + Image &im = **heightmap; + + offset += _offset; + + im.lock(); + + for (int z = 0; z < buffer.get_size().z; ++z) { + for (int x = 0; x < buffer.get_size().x; ++x) { + + int px = int(Math::fmod(x + offset.x, im.get_width())); + int py = int(Math::fmod(z + offset.z, im.get_height())); + + float h = im.get_pixel(px, py).r * vertical_scale + offset.y; + + for (int y = 0; y < buffer.get_size().y; ++y) { + + // Not a true distance to heightmap, but might be enough + float d = (y - h) * _iso_scale; + do_op(buffer, x, y, z, d, op); + } + } + } + + im.unlock(); +} + void VoxelIsoSurfaceTool::_bind_methods() { ClassDB::bind_method(D_METHOD("set_buffer", "voxel_buffer"), &VoxelIsoSurfaceTool::set_buffer); @@ -134,6 +166,7 @@ void VoxelIsoSurfaceTool::_bind_methods() { ClassDB::bind_method(D_METHOD("do_sphere", "center", "radius", "op"), &VoxelIsoSurfaceTool::do_sphere, DEFVAL(OP_ADD)); ClassDB::bind_method(D_METHOD("do_plane", "plane", "op"), &VoxelIsoSurfaceTool::do_plane, DEFVAL(OP_ADD)); ClassDB::bind_method(D_METHOD("do_cube", "transform", "extents", "op"), &VoxelIsoSurfaceTool::do_cube, DEFVAL(OP_ADD)); + ClassDB::bind_method(D_METHOD("do_heightmap", "heightmap", "offset", "vertical_scale", "op"), &VoxelIsoSurfaceTool::do_cube, DEFVAL(OP_ADD)); BIND_ENUM_CONSTANT(OP_ADD); BIND_ENUM_CONSTANT(OP_SUBTRACT); diff --git a/voxel_isosurface_tool.h b/voxel_isosurface_tool.h index 02c5afc..e1b4f1e 100644 --- a/voxel_isosurface_tool.h +++ b/voxel_isosurface_tool.h @@ -2,6 +2,7 @@ #define VOXEL_ISOSURFACE_TOOL_H #include "voxel_buffer.h" +#include class VoxelIsoSurfaceTool : public Reference { GDCLASS(VoxelIsoSurfaceTool, Reference) @@ -26,6 +27,7 @@ public: void do_sphere(Vector3 center, real_t radius, Operation op); void do_plane(Plane plane, Operation op); void do_cube(Transform transform, Vector3 extents, Operation op); + void do_heightmap(Ref heightmap, Vector3 offset, real_t vertical_scale, Operation op); protected: static void _bind_methods();