diff --git a/world/voxel_world.cpp b/world/voxel_world.cpp index 9f3774f..c6794de 100644 --- a/world/voxel_world.cpp +++ b/world/voxel_world.cpp @@ -557,6 +557,75 @@ void VoxelWorld::set_lights(const Vector &chunks) { } } +void VoxelWorld::set_voxel_at_world_position(const Vector3 &world_position, const uint8_t data, const int channel_index) { + Vector3 pos = world_position / get_voxel_scale(); + + //Note: floor is needed to handle negative numbers proiberly + int x = static_cast(Math::floor(pos.x / get_chunk_size_x() / get_voxel_scale())); + int y = static_cast(Math::floor(pos.y / get_chunk_size_y() / get_voxel_scale())); + int z = static_cast(Math::floor(pos.z / get_chunk_size_z() / get_voxel_scale())); + + int bx = static_cast(Math::floor(pos.x / get_voxel_scale())) % get_chunk_size_x(); + int by = static_cast(Math::floor(pos.y / get_voxel_scale())) % get_chunk_size_y(); + int bz = static_cast(Math::floor(pos.z / get_voxel_scale())) % get_chunk_size_z(); + + if (bx < 0) { + bx += get_chunk_size_x(); + } + + if (by < 0) { + by += get_chunk_size_y(); + } + + if (bz < 0) { + bz += get_chunk_size_z(); + } + + if (get_data_margin_end() > 0) { + if (bx == 0) { + Ref chunk = get_or_create_chunk(x - 1, y, z); + chunk->set_voxel(data, get_chunk_size_x(), by, bz, channel_index); + chunk->build(); + } + + if (by == 0) { + Ref chunk = get_or_create_chunk(x, y - 1, z); + chunk->set_voxel(data, bx, get_chunk_size_y(), bz, channel_index); + chunk->build(); + } + + if (bz == 0) { + Ref chunk = get_or_create_chunk(x, y, z - 1); + chunk->set_voxel(data, bx, by, get_chunk_size_z(), channel_index); + chunk->build(); + } + } + + if (get_data_margin_start() > 0) { + if (bx == get_chunk_size_x() - 1) { + Ref chunk = get_or_create_chunk(x + 1, y, z); + chunk->set_voxel(data, -1, by, bz, channel_index); + chunk->build(); + } + + if (by == get_chunk_size_y() - 1) { + Ref chunk = get_or_create_chunk(x, y + 1, z); + chunk->set_voxel(data, bx, -1, bz, channel_index); + chunk->build(); + } + + if (bz == get_chunk_size_z() - 1) { + Ref chunk = get_or_create_chunk(x, y, z + 1); + chunk->set_voxel(data, bx, by, -1, channel_index); + chunk->build(); + } + } + + Ref chunk = get_or_create_chunk(x, y, z); + chunk->set_voxel(data, bx, by, bz, channel_index); + chunk->build(); +} + VoxelWorld::VoxelWorld() { _editable = false; @@ -873,4 +942,6 @@ void VoxelWorld::_bind_methods() { ClassDB::bind_method(D_METHOD("get_lights"), &VoxelWorld::get_lights); ClassDB::bind_method(D_METHOD("set_lights", "chunks"), &VoxelWorld::set_lights); + + ClassDB::bind_method(D_METHOD("set_voxel_at_world_position", "world_position", "data", "channel_index"), &VoxelWorld::set_voxel_at_world_position); } diff --git a/world/voxel_world.h b/world/voxel_world.h index 5da9b6c..165df0c 100644 --- a/world/voxel_world.h +++ b/world/voxel_world.h @@ -169,6 +169,9 @@ public: Vector get_lights(); void set_lights(const Vector &chunks); + //Helpers + void set_voxel_at_world_position(const Vector3 &world_position, const uint8_t data, const int channel_index); + VoxelWorld(); ~VoxelWorld(); diff --git a/world/voxel_world_editor.cpp b/world/voxel_world_editor.cpp index 73d927c..523930c 100644 --- a/world/voxel_world_editor.cpp +++ b/world/voxel_world_editor.cpp @@ -103,81 +103,14 @@ bool VoxelWorldEditor::do_input_action(Camera *p_camera, const Point2 &p_point, int selected_voxel = 0; if (_tool_mode == TOOL_MODE_ADD) { - pos = (res.position + (Vector3(0.1, 0.1, 0.1) * res.normal)) / _world->get_voxel_scale(); + pos = (res.position + (Vector3(0.1, 0.1, 0.1) * res.normal)); selected_voxel = _seletced_type; } else if (_tool_mode == TOOL_MODE_REMOVE) { - pos = (res.position + (Vector3(0.1, 0.1, 0.1) * -res.normal)) / _world->get_voxel_scale(); + pos = (res.position + (Vector3(0.1, 0.1, 0.1) * -res.normal)); selected_voxel = 0; } - //todo add these?: - //_world->set_voxel(pos, data[]); - //Ref chunk = _world->get_or_spawn_chunk_at_world_pos(pos); - - //Note: floor is needed to handle negative numbers proiberly - int x = static_cast(Math::floor(pos.x / _world->get_chunk_size_x() / _world->get_voxel_scale())); - int y = static_cast(Math::floor(pos.y / _world->get_chunk_size_y() / _world->get_voxel_scale())); - int z = static_cast(Math::floor(pos.z / _world->get_chunk_size_z() / _world->get_voxel_scale())); - - int bx = static_cast(Math::floor(pos.x / _world->get_voxel_scale())) % _world->get_chunk_size_x(); - int by = static_cast(Math::floor(pos.y / _world->get_voxel_scale())) % _world->get_chunk_size_y(); - int bz = static_cast(Math::floor(pos.z / _world->get_voxel_scale())) % _world->get_chunk_size_z(); - - if (bx < 0) { - bx += _world->get_chunk_size_x(); - } - - if (by < 0) { - by += _world->get_chunk_size_y(); - } - - if (bz < 0) { - bz += _world->get_chunk_size_z(); - } - - if (_world->get_data_margin_end() > 0) { - if (bx == 0) { - Ref chunk = _world->get_or_create_chunk(x - 1, y, z); - chunk->set_voxel(selected_voxel, _world->get_chunk_size_x(), by, bz, 0); - chunk->build(); - } - - if (by == 0) { - Ref chunk = _world->get_or_create_chunk(x, y - 1, z); - chunk->set_voxel(selected_voxel, bx, _world->get_chunk_size_y(), bz, 0); - chunk->build(); - } - - if (bz == 0) { - Ref chunk = _world->get_or_create_chunk(x, y, z - 1); - chunk->set_voxel(selected_voxel, bx, by, _world->get_chunk_size_z(), 0); - chunk->build(); - } - } - - if (_world->get_data_margin_start() > 0) { - if (bx == _world->get_chunk_size_x() - 1) { - Ref chunk = _world->get_or_create_chunk(x + 1, y, z); - chunk->set_voxel(selected_voxel, -1, by, bz, 0); - chunk->build(); - } - - if (by == _world->get_chunk_size_y() - 1) { - Ref chunk = _world->get_or_create_chunk(x, y + 1, z); - chunk->set_voxel(selected_voxel, bx, -1, bz, 0); - chunk->build(); - } - - if (bz == _world->get_chunk_size_z() - 1) { - Ref chunk = _world->get_or_create_chunk(x, y, z + 1); - chunk->set_voxel(selected_voxel, bx, by, -1, 0); - chunk->build(); - } - } - - Ref chunk = _world->get_or_create_chunk(x, y, z); - chunk->set_voxel(selected_voxel, bx, by, bz, 0); - chunk->build(); + _world->set_voxel_at_world_position(pos, selected_voxel, 0); return true; }