Added in gridmap from godot, I currently think it is a nice place to start from with the voxel world, as it pretty much already works like it, and it has a nice editor plugin as well. Also I haven't yet used godot's servers too much.

This commit is contained in:
Relintai 2019-05-31 23:03:38 +02:00
parent bc9896b695
commit 4d73c7e9c6
6 changed files with 3483 additions and 0 deletions

225
doc_classes/GridMap.xml Normal file
View File

@ -0,0 +1,225 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GridMap" inherits="Spatial" category="Core" version="3.2">
<brief_description>
Node for 3D tile-based maps.
</brief_description>
<description>
GridMap lets you place meshes on a grid interactively. It works both from the editor and can help you create in-game level editors.
GridMaps use a [MeshLibrary] which contain a list of tiles: meshes with materials plus optional collisions and extra elements.
A GridMap contains a collection of cells. Each grid cell refers to a [MeshLibrary] item. All cells in the map have the same dimensions.
A GridMap is split into a sparse collection of octants for efficient rendering and physics processing. Every octant has the same dimensions and can contain several cells.
</description>
<tutorials>
<link>https://docs.godotengine.org/en/latest/tutorials/3d/using_gridmaps.html</link>
</tutorials>
<methods>
<method name="clear">
<return type="void">
</return>
<description>
Clear all cells.
</description>
</method>
<method name="clear_baked_meshes">
<return type="void">
</return>
<description>
</description>
</method>
<method name="get_bake_mesh_instance">
<return type="RID">
</return>
<argument index="0" name="idx" type="int">
</argument>
<description>
</description>
</method>
<method name="get_bake_meshes">
<return type="Array">
</return>
<description>
</description>
</method>
<method name="get_cell_item" qualifiers="const">
<return type="int">
</return>
<argument index="0" name="x" type="int">
</argument>
<argument index="1" name="y" type="int">
</argument>
<argument index="2" name="z" type="int">
</argument>
<description>
The [MeshLibrary] item index located at the grid-based X, Y and Z coordinates. If the cell is empty, [constant INVALID_CELL_ITEM] will be returned.
</description>
</method>
<method name="get_cell_item_orientation" qualifiers="const">
<return type="int">
</return>
<argument index="0" name="x" type="int">
</argument>
<argument index="1" name="y" type="int">
</argument>
<argument index="2" name="z" type="int">
</argument>
<description>
The orientation of the cell at the grid-based X, Y and Z coordinates. -1 is returned if the cell is empty.
</description>
</method>
<method name="get_collision_layer_bit" qualifiers="const">
<return type="bool">
</return>
<argument index="0" name="bit" type="int">
</argument>
<description>
</description>
</method>
<method name="get_collision_mask_bit" qualifiers="const">
<return type="bool">
</return>
<argument index="0" name="bit" type="int">
</argument>
<description>
</description>
</method>
<method name="get_meshes">
<return type="Array">
</return>
<description>
Array of [Transform] and [Mesh] references corresponding to the non empty cells in the grid. The transforms are specified in world space.
</description>
</method>
<method name="get_used_cells" qualifiers="const">
<return type="Array">
</return>
<description>
Array of [Vector3] with the non empty cell coordinates in the grid map.
</description>
</method>
<method name="make_baked_meshes">
<return type="void">
</return>
<argument index="0" name="gen_lightmap_uv" type="bool" default="false">
</argument>
<argument index="1" name="lightmap_uv_texel_size" type="float" default="0.1">
</argument>
<description>
</description>
</method>
<method name="map_to_world" qualifiers="const">
<return type="Vector3">
</return>
<argument index="0" name="x" type="int">
</argument>
<argument index="1" name="y" type="int">
</argument>
<argument index="2" name="z" type="int">
</argument>
<description>
</description>
</method>
<method name="resource_changed">
<return type="void">
</return>
<argument index="0" name="resource" type="Resource">
</argument>
<description>
</description>
</method>
<method name="set_cell_item">
<return type="void">
</return>
<argument index="0" name="x" type="int">
</argument>
<argument index="1" name="y" type="int">
</argument>
<argument index="2" name="z" type="int">
</argument>
<argument index="3" name="item" type="int">
</argument>
<argument index="4" name="orientation" type="int" default="0">
</argument>
<description>
Set the mesh index for the cell referenced by its grid-based X, Y and Z coordinates.
A negative item index will clear the cell.
Optionally, the item's orientation can be passed.
</description>
</method>
<method name="set_clip">
<return type="void">
</return>
<argument index="0" name="enabled" type="bool">
</argument>
<argument index="1" name="clipabove" type="bool" default="true">
</argument>
<argument index="2" name="floor" type="int" default="0">
</argument>
<argument index="3" name="axis" type="int" enum="Vector3.Axis" default="0">
</argument>
<description>
</description>
</method>
<method name="set_collision_layer_bit">
<return type="void">
</return>
<argument index="0" name="bit" type="int">
</argument>
<argument index="1" name="value" type="bool">
</argument>
<description>
</description>
</method>
<method name="set_collision_mask_bit">
<return type="void">
</return>
<argument index="0" name="bit" type="int">
</argument>
<argument index="1" name="value" type="bool">
</argument>
<description>
</description>
</method>
<method name="world_to_map" qualifiers="const">
<return type="Vector3">
</return>
<argument index="0" name="pos" type="Vector3">
</argument>
<description>
</description>
</method>
</methods>
<members>
<member name="cell_center_x" type="bool" setter="set_center_x" getter="get_center_x">
If [code]true[/code], grid items are centered on the X axis.
</member>
<member name="cell_center_y" type="bool" setter="set_center_y" getter="get_center_y">
If [code]true[/code], grid items are centered on the Y axis.
</member>
<member name="cell_center_z" type="bool" setter="set_center_z" getter="get_center_z">
If [code]true[/code], grid items are centered on the Z axis.
</member>
<member name="cell_octant_size" type="int" setter="set_octant_size" getter="get_octant_size">
The size of each octant measured in number of cells. This applies to all three axis.
</member>
<member name="cell_scale" type="float" setter="set_cell_scale" getter="get_cell_scale">
</member>
<member name="cell_size" type="Vector3" setter="set_cell_size" getter="get_cell_size">
The dimensions of the grid's cells.
</member>
<member name="collision_layer" type="int" setter="set_collision_layer" getter="get_collision_layer">
</member>
<member name="collision_mask" type="int" setter="set_collision_mask" getter="get_collision_mask">
</member>
<member name="mesh_library" type="MeshLibrary" setter="set_mesh_library" getter="get_mesh_library">
The assigned [MeshLibrary].
</member>
<member name="theme" type="MeshLibrary" setter="set_theme" getter="get_theme">
Deprecated, use [member mesh_library] instead.
</member>
</members>
<constants>
<constant name="INVALID_CELL_ITEM" value="-1">
Invalid cell item that can be used in [method set_cell_item] to clear cells (or represent an empty cell in [method get_cell_item]).
</constant>
</constants>
</class>

1
doc_classes/README.md Normal file
View File

@ -0,0 +1 @@
Doc classes will appear here when generating

1147
world/grid_map.cpp Normal file

File diff suppressed because it is too large Load Diff

279
world/grid_map.h Normal file
View File

@ -0,0 +1,279 @@
/*************************************************************************/
/* grid_map.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef GRID_MAP_H
#define GRID_MAP_H
#include "scene/3d/navigation.h"
#include "scene/3d/spatial.h"
#include "scene/resources/mesh_library.h"
#include "scene/resources/multimesh.h"
//heh heh, godotsphir!! this shares no code and the design is completely different with previous projects i've done..
//should scale better with hardware that supports instancing
class GridMap : public Spatial {
GDCLASS(GridMap, Spatial);
enum {
MAP_DIRTY_TRANSFORMS = 1,
MAP_DIRTY_INSTANCES = 2,
};
union IndexKey {
struct {
int16_t x;
int16_t y;
int16_t z;
};
uint64_t key;
_FORCE_INLINE_ bool operator<(const IndexKey &p_key) const {
return key < p_key.key;
}
IndexKey() { key = 0; }
};
/**
* @brief A Cell is a single cell in the cube map space; it is defined by its coordinates and the populating Item, identified by int id.
*/
union Cell {
struct {
unsigned int item : 16;
unsigned int rot : 5;
unsigned int layer : 8;
};
uint32_t cell;
Cell() {
item = 0;
rot = 0;
layer = 0;
}
};
/**
* @brief An Octant is a prism containing Cells, and possibly belonging to an Area.
* A GridMap can have multiple Octants.
*/
struct Octant {
struct NavMesh {
int id;
Transform xform;
};
struct MultimeshInstance {
RID instance;
RID multimesh;
struct Item {
int index;
Transform transform;
IndexKey key;
};
Vector<Item> items; //tools only, for changing visibility
};
Vector<MultimeshInstance> multimesh_instances;
Set<IndexKey> cells;
RID collision_debug;
RID collision_debug_instance;
bool dirty;
RID static_body;
Map<IndexKey, NavMesh> navmesh_ids;
};
union OctantKey {
struct {
int16_t x;
int16_t y;
int16_t z;
int16_t empty;
};
uint64_t key;
_FORCE_INLINE_ bool operator<(const OctantKey &p_key) const {
return key < p_key.key;
}
//OctantKey(const IndexKey& p_k, int p_item) { indexkey=p_k.key; item=p_item; }
OctantKey() { key = 0; }
};
uint32_t collision_layer;
uint32_t collision_mask;
Transform last_transform;
bool _in_tree;
Vector3 cell_size;
int octant_size;
bool center_x, center_y, center_z;
float cell_scale;
Navigation *navigation;
bool clip;
bool clip_above;
int clip_floor;
bool recreating_octants;
Vector3::Axis clip_axis;
Ref<MeshLibrary> mesh_library;
Map<OctantKey, Octant *> octant_map;
Map<IndexKey, Cell> cell_map;
void _recreate_octant_data();
struct BakeLight {
VS::LightType type;
Vector3 pos;
Vector3 dir;
float param[VS::LIGHT_PARAM_MAX];
};
_FORCE_INLINE_ Vector3 _octant_get_offset(const OctantKey &p_key) const {
return Vector3(p_key.x, p_key.y, p_key.z) * cell_size * octant_size;
}
void _reset_physic_bodies_collision_filters();
void _octant_enter_world(const OctantKey &p_key);
void _octant_exit_world(const OctantKey &p_key);
bool _octant_update(const OctantKey &p_key);
void _octant_clean_up(const OctantKey &p_key);
void _octant_transform(const OctantKey &p_key);
bool awaiting_update;
void _queue_octants_dirty();
void _update_octants_callback();
void resource_changed(const RES &p_res);
void _clear_internal();
Vector3 _get_offset() const;
struct BakedMesh {
Ref<Mesh> mesh;
RID instance;
};
Vector<BakedMesh> baked_meshes;
protected:
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;
void _get_property_list(List<PropertyInfo> *p_list) const;
void _notification(int p_what);
void _update_visibility();
static void _bind_methods();
public:
enum {
INVALID_CELL_ITEM = -1
};
void set_collision_layer(uint32_t p_layer);
uint32_t get_collision_layer() const;
void set_collision_mask(uint32_t p_mask);
uint32_t get_collision_mask() const;
void set_collision_layer_bit(int p_bit, bool p_value);
bool get_collision_layer_bit(int p_bit) const;
void set_collision_mask_bit(int p_bit, bool p_value);
bool get_collision_mask_bit(int p_bit) const;
#ifndef DISABLE_DEPRECATED
void set_theme(const Ref<MeshLibrary> &p_theme);
Ref<MeshLibrary> get_theme() const;
#endif // DISABLE_DEPRECATED
void set_mesh_library(const Ref<MeshLibrary> &p_mesh_library);
Ref<MeshLibrary> get_mesh_library() const;
void set_cell_size(const Vector3 &p_size);
Vector3 get_cell_size() const;
void set_octant_size(int p_size);
int get_octant_size() const;
void set_center_x(bool p_enable);
bool get_center_x() const;
void set_center_y(bool p_enable);
bool get_center_y() const;
void set_center_z(bool p_enable);
bool get_center_z() const;
void set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot = 0);
int get_cell_item(int p_x, int p_y, int p_z) const;
int get_cell_item_orientation(int p_x, int p_y, int p_z) const;
Vector3 world_to_map(const Vector3 &p_world_pos) const;
Vector3 map_to_world(int p_x, int p_y, int p_z) const;
void set_clip(bool p_enabled, bool p_clip_above = true, int p_floor = 0, Vector3::Axis p_axis = Vector3::AXIS_X);
void set_cell_scale(float p_scale);
float get_cell_scale() const;
Array get_used_cells() const;
Array get_meshes();
void clear_baked_meshes();
void make_baked_meshes(bool p_gen_lightmap_uv = false, float p_lightmap_uv_texel_size = 0.1);
void clear();
Array get_bake_meshes();
RID get_bake_mesh_instance(int p_idx);
GridMap();
~GridMap();
};
#endif // GRID_MAP_H

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,274 @@
/*************************************************************************/
/* grid_map_editor_plugin.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef GRID_MAP_EDITOR_PLUGIN_H
#define GRID_MAP_EDITOR_PLUGIN_H
#include "editor/editor_node.h"
#include "editor/editor_plugin.h"
#include "editor/pane_drag.h"
#include "grid_map.h"
/**
@author Juan Linietsky <reduzio@gmail.com>
*/
class SpatialEditorPlugin;
class GridMapEditor : public VBoxContainer {
GDCLASS(GridMapEditor, VBoxContainer);
enum {
GRID_CURSOR_SIZE = 50
};
enum InputAction {
INPUT_NONE,
INPUT_PAINT,
INPUT_ERASE,
INPUT_PICK,
INPUT_SELECT,
INPUT_PASTE,
};
enum ClipMode {
CLIP_DISABLED,
CLIP_ABOVE,
CLIP_BELOW
};
enum DisplayMode {
DISPLAY_THUMBNAIL,
DISPLAY_LIST
};
UndoRedo *undo_redo;
InputAction input_action;
Panel *panel;
MenuButton *options;
SpinBox *floor;
double accumulated_floor_delta;
ToolButton *mode_thumbnail;
ToolButton *mode_list;
LineEdit *search_box;
HSlider *size_slider;
HBoxContainer *spatial_editor_hb;
ConfirmationDialog *settings_dialog;
VBoxContainer *settings_vbc;
SpinBox *settings_pick_distance;
Label *spin_box_label;
struct SetItem {
Vector3 pos;
int new_value;
int new_orientation;
int old_value;
int old_orientation;
};
List<SetItem> set_items;
GridMap *node;
MeshLibrary *last_mesh_library;
ClipMode clip_mode;
bool lock_view;
Transform grid_xform;
Transform edit_grid_xform;
Vector3::Axis edit_axis;
int edit_floor[3];
Vector3 grid_ofs;
RID grid[3];
RID grid_instance[3];
RID cursor_instance;
RID selection_mesh;
RID selection_instance;
RID selection_level_mesh[3];
RID selection_level_instance[3];
RID paste_mesh;
RID paste_instance;
struct ClipboardItem {
int cell_item;
Vector3 grid_offset;
int orientation;
RID instance;
};
List<ClipboardItem> clipboard_items;
Ref<SpatialMaterial> indicator_mat;
Ref<SpatialMaterial> inner_mat;
Ref<SpatialMaterial> outer_mat;
Ref<SpatialMaterial> selection_floor_mat;
bool updating;
struct Selection {
Vector3 click;
Vector3 current;
Vector3 begin;
Vector3 end;
bool active;
} selection;
Selection last_selection;
struct PasteIndicator {
Vector3 click;
Vector3 current;
Vector3 begin;
Vector3 end;
int orientation;
};
PasteIndicator paste_indicator;
bool cursor_visible;
Transform cursor_transform;
Vector3 cursor_origin;
Vector3 last_mouseover;
int display_mode;
int selected_palette;
int cursor_rot;
enum Menu {
MENU_OPTION_NEXT_LEVEL,
MENU_OPTION_PREV_LEVEL,
MENU_OPTION_LOCK_VIEW,
MENU_OPTION_CLIP_DISABLED,
MENU_OPTION_CLIP_ABOVE,
MENU_OPTION_CLIP_BELOW,
MENU_OPTION_X_AXIS,
MENU_OPTION_Y_AXIS,
MENU_OPTION_Z_AXIS,
MENU_OPTION_CURSOR_ROTATE_Y,
MENU_OPTION_CURSOR_ROTATE_X,
MENU_OPTION_CURSOR_ROTATE_Z,
MENU_OPTION_CURSOR_BACK_ROTATE_Y,
MENU_OPTION_CURSOR_BACK_ROTATE_X,
MENU_OPTION_CURSOR_BACK_ROTATE_Z,
MENU_OPTION_CURSOR_CLEAR_ROTATION,
MENU_OPTION_PASTE_SELECTS,
MENU_OPTION_SELECTION_DUPLICATE,
MENU_OPTION_SELECTION_CUT,
MENU_OPTION_SELECTION_CLEAR,
MENU_OPTION_SELECTION_FILL,
MENU_OPTION_GRIDMAP_SETTINGS
};
SpatialEditorPlugin *spatial_editor;
struct AreaDisplay {
RID mesh;
RID instance;
};
void update_grid();
void _configure();
void _menu_option(int);
void update_palette();
void _set_display_mode(int p_mode);
ItemList *mesh_library_palette;
void _item_selected_cbk(int idx);
void _update_cursor_transform();
void _update_cursor_instance();
void _update_clip();
void _text_changed(const String &p_text);
void _sbox_input(const Ref<InputEvent> &p_ie);
void _icon_size_changed(float p_value);
void _clear_clipboard_data();
void _set_clipboard_data();
void _update_paste_indicator();
void _do_paste();
void _update_selection_transform();
void _validate_selection();
void _set_selection(bool p_active, const Vector3 p_begin = Vector3(), const Vector3 p_end = Vector3());
void _floor_changed(float p_value);
void _floor_mouse_exited();
void _delete_selection();
void _fill_selection();
EditorNode *editor;
bool do_input_action(Camera *p_camera, const Point2 &p_point, bool p_click);
friend class GridMapEditorPlugin;
protected:
void _notification(int p_what);
void _node_removed(Node *p_node);
static void _bind_methods();
public:
bool forward_spatial_input_event(Camera *p_camera, const Ref<InputEvent> &p_event);
void edit(GridMap *p_gridmap);
GridMapEditor() {}
GridMapEditor(EditorNode *p_editor);
~GridMapEditor();
};
class GridMapEditorPlugin : public EditorPlugin {
GDCLASS(GridMapEditorPlugin, EditorPlugin);
GridMapEditor *grid_map_editor;
EditorNode *editor;
protected:
void _notification(int p_what);
public:
virtual bool forward_spatial_gui_input(Camera *p_camera, const Ref<InputEvent> &p_event) { return grid_map_editor->forward_spatial_input_event(p_camera, p_event); }
virtual String get_name() const { return "GridMap"; }
bool has_main_screen() const { return false; }
virtual void edit(Object *p_object);
virtual bool handles(Object *p_object) const;
virtual void make_visible(bool p_visible);
GridMapEditorPlugin(EditorNode *p_node);
~GridMapEditorPlugin();
};
#endif // CUBE_GRID_MAP_EDITOR_PLUGIN_H