Moved the chunk position calculations from the world editor into VoxelWorld.

This commit is contained in:
Relintai 2020-04-16 14:44:22 +02:00
parent 06d3b0ae32
commit 135b34c8a8
3 changed files with 77 additions and 70 deletions

View File

@ -557,6 +557,75 @@ void VoxelWorld::set_lights(const Vector<Variant> &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<int>(Math::floor(pos.x / get_chunk_size_x() / get_voxel_scale()));
int y = static_cast<int>(Math::floor(pos.y / get_chunk_size_y() / get_voxel_scale()));
int z = static_cast<int>(Math::floor(pos.z / get_chunk_size_z() / get_voxel_scale()));
int bx = static_cast<int>(Math::floor(pos.x / get_voxel_scale())) % get_chunk_size_x();
int by = static_cast<int>(Math::floor(pos.y / get_voxel_scale())) % get_chunk_size_y();
int bz = static_cast<int>(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<VoxelChunk> 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<VoxelChunk> 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<VoxelChunk> 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<VoxelChunk> 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<VoxelChunk> 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<VoxelChunk> chunk = get_or_create_chunk(x, y, z + 1);
chunk->set_voxel(data, bx, by, -1, channel_index);
chunk->build();
}
}
Ref<VoxelChunk> 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);
}

View File

@ -169,6 +169,9 @@ public:
Vector<Variant> get_lights();
void set_lights(const Vector<Variant> &chunks);
//Helpers
void set_voxel_at_world_position(const Vector3 &world_position, const uint8_t data, const int channel_index);
VoxelWorld();
~VoxelWorld();

View File

@ -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<VoxelChunk> chunk = _world->get_or_spawn_chunk_at_world_pos(pos);
//Note: floor is needed to handle negative numbers proiberly
int x = static_cast<int>(Math::floor(pos.x / _world->get_chunk_size_x() / _world->get_voxel_scale()));
int y = static_cast<int>(Math::floor(pos.y / _world->get_chunk_size_y() / _world->get_voxel_scale()));
int z = static_cast<int>(Math::floor(pos.z / _world->get_chunk_size_z() / _world->get_voxel_scale()));
int bx = static_cast<int>(Math::floor(pos.x / _world->get_voxel_scale())) % _world->get_chunk_size_x();
int by = static_cast<int>(Math::floor(pos.y / _world->get_voxel_scale())) % _world->get_chunk_size_y();
int bz = static_cast<int>(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<VoxelChunk> 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<VoxelChunk> 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<VoxelChunk> 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<VoxelChunk> 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<VoxelChunk> 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<VoxelChunk> chunk = _world->get_or_create_chunk(x, y, z + 1);
chunk->set_voxel(selected_voxel, bx, by, -1, 0);
chunk->build();
}
}
Ref<VoxelChunk> 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;
}