Fix voxel index calculations in the world editor.

This commit is contained in:
Relintai 2020-04-10 13:40:07 +02:00
parent 03f9041eb8
commit 17dfbb4cab
2 changed files with 54 additions and 25 deletions

View File

@ -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";

View File

@ -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<VoxelChunk> 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<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 x = Math::floor(fx);
int y = Math::floor(fy);
int z = Math::floor(fz);
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();
Ref<VoxelChunk> 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<int>(Math::round(pos.x / _world->get_voxel_scale())) % _world->get_chunk_size_x();
int by = static_cast<int>(Math::round(pos.y / _world->get_voxel_scale())) % _world->get_chunk_size_y();
int bz = static_cast<int>(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<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 (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();
return true;