From 98707674a49e3f0f0d9968fda91becb07e8fac18 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 5 Apr 2020 21:02:51 +0200 Subject: [PATCH] Initial implementation for the voxel world editor. --- world/voxel_world_editor.cpp | 93 ++++++++++++++++++++++++++++++++++++ world/voxel_world_editor.h | 4 ++ 2 files changed, 97 insertions(+) diff --git a/world/voxel_world_editor.cpp b/world/voxel_world_editor.cpp index dbe2f0f..6ce9eb4 100644 --- a/world/voxel_world_editor.cpp +++ b/world/voxel_world_editor.cpp @@ -32,16 +32,109 @@ SOFTWARE. #include "core/math/geometry.h" #include "core/os/keyboard.h" +#include "voxel_chunk.h" + bool VoxelWorldEditor::forward_spatial_input_event(Camera *p_camera, const Ref &p_event) { + if (!_world && !_world->get_editable()) { + return false; + } + + Ref mb = p_event; + + if (mb.is_valid()) { + + if (mb->is_pressed()) { + Ref lib = _world->get_library(); + + if (!lib.is_valid()) + return false; + + if (mb->get_button_index() == BUTTON_LEFT) { + return do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true, _seletced_type); + } else if (mb->get_button_index() == BUTTON_RIGHT) { + return do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true, 0); + } else { + return false; + } + + //return do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true); + } + } + + return false; +} + +bool VoxelWorldEditor::do_input_action(Camera *p_camera, const Point2 &p_point, bool p_click, int selected_voxel) { + + if (!spatial_editor) + return false; + + Camera *camera = p_camera; + Vector3 from = camera->project_ray_origin(p_point); + Vector3 to = from + camera->project_ray_normal(p_point) * 10000; + Transform local_xform = _world->get_global_transform().affine_inverse(); + + from = local_xform.xform(from); + to = local_xform.xform(to); + + PhysicsDirectSpaceState *ss = _world->get_world()->get_direct_space_state(); + PhysicsDirectSpaceState::RayResult res; + + if (ss->intersect_ray(from, to, res)) { + Vector3 pos = res.position; + + //_world->set_voxel(pos, data[]); + //Ref chunk = _world->get_or_spawn_chunk_at_world_pos(pos); + + int x = (pos.x - _world->get_chunk_size_x()) / _world->get_chunk_size_x() / _world->get_voxel_scale(); + int y = (pos.y - _world->get_chunk_size_y()) / _world->get_chunk_size_y() / _world->get_voxel_scale(); + int z = (pos.z - _world->get_chunk_size_z()) / _world->get_chunk_size_z() / _world->get_voxel_scale(); + + Ref chunk = _world->get_chunk(x, y, z); + + if (!chunk.is_valid()) { + chunk = _world->create_chunk(x, y, z); + } + + int bx = static_cast((pos.x - _world->get_chunk_size_x()) / _world->get_voxel_scale()) % _world->get_chunk_size_x(); + int by = static_cast((pos.y - _world->get_chunk_size_y()) / _world->get_voxel_scale()) % _world->get_chunk_size_y(); + int bz = static_cast((pos.z - _world->get_chunk_size_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(); + + chunk->set_voxel(selected_voxel, bx, by, bz, 0); + + chunk->build(); + + return true; + } + return false; } void VoxelWorldEditor::edit(VoxelWorld *p_world) { + _world = p_world; + + spatial_editor = Object::cast_to(_editor->get_editor_plugin_screen()); } VoxelWorldEditor::VoxelWorldEditor() { + _world = NULL; + _seletced_type = 1; + _editor = NULL; } VoxelWorldEditor::VoxelWorldEditor(EditorNode *p_editor) { + _world = NULL; + _seletced_type = 1; + _editor = p_editor; + spatial_editor_hb = memnew(HBoxContainer); spatial_editor_hb->set_h_size_flags(SIZE_EXPAND_FILL); spatial_editor_hb->set_alignment(BoxContainer::ALIGN_END); diff --git a/world/voxel_world_editor.h b/world/voxel_world_editor.h index 4021ab8..5efe8a5 100644 --- a/world/voxel_world_editor.h +++ b/world/voxel_world_editor.h @@ -36,6 +36,7 @@ public: bool forward_spatial_input_event(Camera *p_camera, const Ref &p_event); void edit(VoxelWorld *p_world); + bool do_input_action(Camera *p_camera, const Point2 &p_point, bool p_click, int selected_voxel); VoxelWorldEditor(); VoxelWorldEditor(EditorNode *p_editor); @@ -49,6 +50,9 @@ protected: private: VoxelWorld *_world; + int _seletced_type; + SpatialEditorPlugin *spatial_editor; + EditorNode *_editor; }; class VoxelWorldEditorPlugin : public EditorPlugin {