diff --git a/world/default/voxel_chunk_default.cpp b/world/default/voxel_chunk_default.cpp index 6097e3d..882e5ff 100644 --- a/world/default/voxel_chunk_default.cpp +++ b/world/default/voxel_chunk_default.cpp @@ -39,9 +39,9 @@ typedef class PhysicsServer3D PhysicsServer; typedef class StandardMaterial3D SpatialMaterial; #endif -#include "../voxel_world.h" -#include "../../meshers/default/voxel_mesher_default.h" #include "../../../opensimplex/open_simplex_noise.h" +#include "../../meshers/default/voxel_mesher_default.h" +#include "../voxel_world.h" const String VoxelChunkDefault::BINDING_STRING_ACTIVE_BUILD_PHASE_TYPE = "Normal,Process,Physics Process"; const String VoxelChunkDefault::BINDING_STRING_BUILD_FLAGS = "Use Isolevel,Use Lighting,Use AO,Use RAO,Generate AO,Generate RAO,Bake Lights,Create Collider,Create Lods"; diff --git a/world/voxel_world_editor.cpp b/world/voxel_world_editor.cpp index 5e0bcda..4f543c9 100644 --- a/world/voxel_world_editor.cpp +++ b/world/voxel_world_editor.cpp @@ -24,8 +24,6 @@ SOFTWARE. #include "core/version.h" - - #include "editor/editor_scale.h" #include "editor/editor_settings.h" @@ -103,40 +101,71 @@ bool VoxelWorldEditor::do_input_action(Camera *p_camera, const Point2 &p_point, PhysicsDirectSpaceState::RayResult res; if (ss->intersect_ray(from, to, res)) { - Vector3 pos = res.position; + Vector3 pos = (res.position + (Vector3(0.1, 0.1, 0.1) * res.normal)) / _world->get_voxel_scale(); + //todo add these?: //_world->set_voxel(pos, data[]); //Ref chunk = _world->get_or_spawn_chunk_at_world_pos(pos); - float fx = pos.x / _world->get_chunk_size_x() / _world->get_voxel_scale(); - float fy = pos.y / _world->get_chunk_size_y() / _world->get_voxel_scale(); - float fz = pos.z / _world->get_chunk_size_z() / _world->get_voxel_scale(); + //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 x = Math::floor(fx); - int y = Math::floor(fy); - int z = Math::floor(fz); + 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(); - Ref chunk = _world->get_chunk(x, y, z); - - if (!chunk.is_valid()) { - chunk = _world->create_chunk(x, y, z); + if (bx < 0) { + bx += _world->get_chunk_size_x(); } - int bx = static_cast(Math::round(pos.x / _world->get_voxel_scale())) % _world->get_chunk_size_x(); - int by = static_cast(Math::round(pos.y / _world->get_voxel_scale())) % _world->get_chunk_size_y(); - int bz = static_cast(Math::round(pos.z / _world->get_voxel_scale())) % _world->get_chunk_size_z(); - - if (bx < 0) - bx += _world->get_chunk_size_x(); - - if (by < 0) + if (by < 0) { by += _world->get_chunk_size_y(); + } - if (bz < 0) + if (bz < 0) { bz += _world->get_chunk_size_z(); + } + 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 (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(); return true;