Separated VoxelStructure into 2 classes, and made it scriptable.

This commit is contained in:
Relintai 2020-03-19 13:08:58 +01:00
parent 666695a220
commit d0578c24d4
6 changed files with 234 additions and 118 deletions

1
SCsub
View File

@ -36,6 +36,7 @@ sources = [
"world/voxel_chunk.cpp",
"world/voxel_chunk_default.cpp",
"world/voxel_structure.cpp",
"world/block_voxel_structure.cpp",
"world/environment_data.cpp",
"world/voxel_chunk_prop_data.cpp",

View File

@ -38,6 +38,7 @@ SOFTWARE.
#include "meshers/transvoxel_uv_mesher/voxel_mesher_transvoxel.h"
#include "meshers/voxel_mesher.h"
#include "world/block_voxel_structure.h"
#include "world/environment_data.h"
#include "world/voxel_chunk.h"
#include "world/voxel_chunk_default.h"
@ -95,6 +96,7 @@ void register_voxelman_types() {
ClassDB::register_class<VoxelChunk>();
ClassDB::register_class<VoxelChunkDefault>();
ClassDB::register_class<VoxelStructure>();
ClassDB::register_class<BlockVoxelStructure>();
ClassDB::register_class<EnvironmentData>();
ClassDB::register_class<VoxelChunkPropData>();

View File

@ -0,0 +1,116 @@
/*
Copyright (c) 2019-2020 Péter Magyar
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.
*/
#include "block_voxel_structure.h"
int BlockVoxelStructure::get_channel_count() const {
return _channel_count;
}
void BlockVoxelStructure::set_channel_count(const int value) {
_channel_count = value;
}
uint8_t BlockVoxelStructure::get_voxel(int p_x, int p_y, int p_z, int p_channel_index) const {
VSIntPos p;
p.x = p_x;
p.y = p_y;
p.z = p_z;
if (!_data.has(p))
return 0;
PoolByteArray arr = _data[p];
ERR_FAIL_INDEX_V(arr.size(), p_channel_index, 0);
return arr[p_channel_index];
}
void BlockVoxelStructure::set_voxel(uint8_t p_value, int p_x, int p_y, int p_z, int p_channel_index) {
VSIntPos p;
p.x = p_x;
p.y = p_y;
p.z = p_z;
PoolByteArray arr;
if (!_data.has(p)) {
arr.resize(_channel_count);
for (int i = 0; i < _channel_count; ++i) {
arr.set(i, p_value);
}
} else {
arr = _data[p];
}
_data[p] = arr;
}
PoolByteArray BlockVoxelStructure::get_voxel_data(int p_x, int p_y, int p_z) const {
VSIntPos p;
p.x = p_x;
p.y = p_y;
p.z = p_z;
if (!_data.has(p))
return PoolByteArray();
return _data[p];
}
void BlockVoxelStructure::set_voxel_data(PoolByteArray p_arr, int p_x, int p_y, int p_z) {
VSIntPos p;
p.x = p_x;
p.y = p_y;
p.z = p_z;
_data[p] = p_arr;
}
//void BlockVoxelStructure::_write_to_chunk(Node *chunk) {
//VoxelChunk *c = Object::cast_to<VoxelChunk>(chunk);
//}
void BlockVoxelStructure::clear() {
_data.clear();
}
BlockVoxelStructure::BlockVoxelStructure() {
_channel_count = 0;
}
BlockVoxelStructure::~BlockVoxelStructure() {
_data.clear();
}
void BlockVoxelStructure::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_channel_count"), &BlockVoxelStructure::get_channel_count);
ClassDB::bind_method(D_METHOD("set_channel_count", "value"), &BlockVoxelStructure::set_channel_count);
ADD_PROPERTY(PropertyInfo(Variant::INT, "channel_count"), "set_channel_count", "get_channel_count");
ClassDB::bind_method(D_METHOD("get_voxel", "x", "y", "z", "channel_index"), &BlockVoxelStructure::get_voxel, DEFVAL(0));
ClassDB::bind_method(D_METHOD("set_voxel", "value", "x", "y", "z", "channel_index"), &BlockVoxelStructure::set_voxel, DEFVAL(0));
ClassDB::bind_method(D_METHOD("get_voxel_data", "x", "y", "z"), &BlockVoxelStructure::get_voxel_data);
ClassDB::bind_method(D_METHOD("set_voxel_data", "arr", "x", "y", "z"), &BlockVoxelStructure::set_voxel_data);
//ClassDB::bind_method(D_METHOD("_write_to_chunk", "chunk"), &BlockVoxelStructure::_write_to_chunk);
}

View File

@ -0,0 +1,80 @@
/*
Copyright (c) 2019-2020 Péter Magyar
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 BLOCK_VOXEL_STRUCTURE_H
#define BLOCK_VOXEL_STRUCTURE_H
#include "voxel_structure.h"
#include "core/hash_map.h"
#include "core/pool_vector.h"
#include "voxel_chunk.h"
class BlockVoxelStructure : public VoxelStructure {
GDCLASS(BlockVoxelStructure, VoxelStructure);
public:
int get_channel_count() const;
void set_channel_count(const int value);
uint8_t get_voxel(int p_x, int p_y, int p_z, int p_channel_index) const;
void set_voxel(uint8_t p_value, int p_x, int p_y, int p_z, int p_channel_index);
PoolByteArray get_voxel_data(int p_x, int p_y, int p_z) const;
void set_voxel_data(PoolByteArray p_arr, int p_x, int p_y, int p_z);
//void _write_to_chunk(Node *chunk);
void clear();
BlockVoxelStructure();
~BlockVoxelStructure();
protected:
static void _bind_methods();
public:
struct VSIntPos {
int x;
int y;
int z;
};
struct VSIntPosHasher {
static _FORCE_INLINE_ uint32_t hash(const VSIntPos &v) {
uint32_t hash = hash_djb2_one_32(v.x);
hash = hash_djb2_one_32(v.y, hash);
return hash_djb2_one_32(v.z, hash);
}
};
private:
int _channel_count;
HashMap<VSIntPos, PoolByteArray, VSIntPosHasher> _data;
};
_FORCE_INLINE_ bool operator==(const BlockVoxelStructure::VSIntPos &a, const BlockVoxelStructure::VSIntPos &b) {
return a.x == b.x && a.y == b.y && a.z == b.z;
}
#endif

View File

@ -22,11 +22,18 @@ SOFTWARE.
#include "voxel_structure.h"
int VoxelStructure::get_channel_count() const {
return _channel_count;
bool VoxelStructure::get_use_aabb() const {
return _use_aabb;
}
void VoxelStructure::set_channel_count(const int value) {
_channel_count = value;
void VoxelStructure::set_use_aabb(const bool value) {
_use_aabb = value;
}
AABB VoxelStructure::get_aabb() const {
return _aabb;
}
void VoxelStructure::set_aabb(const AABB &value) {
_aabb = value;
}
int VoxelStructure::get_world_position_x() const {
@ -50,92 +57,33 @@ void VoxelStructure::set_world_position_z(const int value) {
_world_position_z = value;
}
uint8_t VoxelStructure::get_voxel(int p_x, int p_y, int p_z, int p_channel_index) const {
VSIntPos p;
p.x = p_x;
p.y = p_y;
p.z = p_z;
void VoxelStructure::write_to_chunk(Node *chunk) {
ERR_FAIL_COND(!ObjectDB::instance_validate(Object::cast_to<VoxelChunk>(chunk)));
if (!_data.has(p))
return 0;
PoolByteArray arr = _data[p];
ERR_FAIL_INDEX_V(arr.size(), p_channel_index, 0);
return arr[p_channel_index];
}
void VoxelStructure::set_voxel(uint8_t p_value, int p_x, int p_y, int p_z, int p_channel_index) {
VSIntPos p;
p.x = p_x;
p.y = p_y;
p.z = p_z;
PoolByteArray arr;
if (!_data.has(p)) {
arr.resize(_channel_count);
for (int i = 0; i < _channel_count; ++i) {
arr.set(i, p_value);
}
} else {
arr = _data[p];
}
_data[p] = arr;
}
PoolByteArray VoxelStructure::get_voxel_data(int p_x, int p_y, int p_z) const {
VSIntPos p;
p.x = p_x;
p.y = p_y;
p.z = p_z;
if (!_data.has(p))
return PoolByteArray();
return _data[p];
}
void VoxelStructure::set_voxel_data(PoolByteArray p_arr, int p_x, int p_y, int p_z) {
VSIntPos p;
p.x = p_x;
p.y = p_y;
p.z = p_z;
_data[p] = p_arr;
}
void VoxelStructure::write_to_chunk_bind(Node *chunk) {
VoxelChunk *c = Object::cast_to<VoxelChunk>(chunk);
ERR_FAIL_COND(!ObjectDB::instance_validate(c));
write_to_chunk(c);
}
void VoxelStructure::write_to_chunk(VoxelChunk *chunk) {
}
void VoxelStructure::clear() {
_data.clear();
if (has_method("_write_to_chunk"))
call("_write_to_chunk", chunk);
}
VoxelStructure::VoxelStructure() {
_channel_count = 0;
_use_aabb = true;
_world_position_x = 0;
_world_position_y = 0;
_world_position_z = 0;
}
VoxelStructure::~VoxelStructure() {
_data.clear();
}
void VoxelStructure::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_channel_count"), &VoxelStructure::get_channel_count);
ClassDB::bind_method(D_METHOD("set_channel_count", "value"), &VoxelStructure::set_channel_count);
ADD_PROPERTY(PropertyInfo(Variant::INT, "channel_count"), "set_channel_count", "get_channel_count");
BIND_VMETHOD(MethodInfo("_write_to_chunk", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "VoxelChunk")));
ClassDB::bind_method(D_METHOD("get_use_aabb"), &VoxelStructure::get_use_aabb);
ClassDB::bind_method(D_METHOD("set_use_aabb", "value"), &VoxelStructure::set_use_aabb);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_aabb"), "set_use_aabb", "get_use_aabb");
ClassDB::bind_method(D_METHOD("get_aabb"), &VoxelStructure::get_aabb);
ClassDB::bind_method(D_METHOD("set_aabb", "value"), &VoxelStructure::set_aabb);
ADD_PROPERTY(PropertyInfo(Variant::AABB, "aabb"), "set_aabb", "get_aabb");
ClassDB::bind_method(D_METHOD("get_world_position_x"), &VoxelStructure::get_world_position_x);
ClassDB::bind_method(D_METHOD("set_world_position_x", "value"), &VoxelStructure::set_world_position_x);
@ -149,11 +97,5 @@ void VoxelStructure::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_world_position_z", "value"), &VoxelStructure::set_world_position_z);
ADD_PROPERTY(PropertyInfo(Variant::INT, "world_position_z"), "set_world_position_z", "get_world_position_z");
ClassDB::bind_method(D_METHOD("get_voxel", "x", "y", "z", "channel_index"), &VoxelStructure::get_voxel, DEFVAL(0));
ClassDB::bind_method(D_METHOD("set_voxel", "value", "x", "y", "z", "channel_index"), &VoxelStructure::set_voxel, DEFVAL(0));
ClassDB::bind_method(D_METHOD("get_voxel_data", "x", "y", "z"), &VoxelStructure::get_voxel_data);
ClassDB::bind_method(D_METHOD("set_voxel_data", "arr", "x", "y", "z"), &VoxelStructure::set_voxel_data);
ClassDB::bind_method(D_METHOD("write_to_chunk", "chunk"), &VoxelStructure::write_to_chunk_bind);
ClassDB::bind_method(D_METHOD("write_to_chunk", "chunk"), &VoxelStructure::write_to_chunk);
}

View File

@ -26,6 +26,7 @@ SOFTWARE.
#include "core/reference.h"
#include "core/hash_map.h"
#include "core/math/aabb.h"
#include "core/pool_vector.h"
#include "voxel_chunk.h"
@ -33,8 +34,11 @@ class VoxelStructure : public Reference {
GDCLASS(VoxelStructure, Reference);
public:
int get_channel_count() const;
void set_channel_count(const int value);
bool get_use_aabb() const;
void set_use_aabb(const bool value);
AABB get_aabb() const;
void set_aabb(const AABB &value);
int get_world_position_x() const;
void set_world_position_x(const int value);
@ -45,16 +49,7 @@ public:
int get_world_position_z() const;
void set_world_position_z(const int value);
uint8_t get_voxel(int p_x, int p_y, int p_z, int p_channel_index) const;
void set_voxel(uint8_t p_value, int p_x, int p_y, int p_z, int p_channel_index);
PoolByteArray get_voxel_data(int p_x, int p_y, int p_z) const;
void set_voxel_data(PoolByteArray p_arr, int p_x, int p_y, int p_z);
void write_to_chunk_bind(Node *chunk);
void write_to_chunk(VoxelChunk *chunk);
void clear();
void write_to_chunk(Node *chunk);
VoxelStructure();
~VoxelStructure();
@ -62,33 +57,13 @@ public:
protected:
static void _bind_methods();
public:
struct VSIntPos {
int x;
int y;
int z;
};
struct VSIntPosHasher {
static _FORCE_INLINE_ uint32_t hash(const VSIntPos &v) {
uint32_t hash = hash_djb2_one_32(v.x);
hash = hash_djb2_one_32(v.y, hash);
return hash_djb2_one_32(v.z, hash);
}
};
private:
int _channel_count;
bool _use_aabb;
AABB _aabb;
int _world_position_x;
int _world_position_y;
int _world_position_z;
HashMap<VSIntPos, PoolByteArray, VSIntPosHasher> _data;
};
_FORCE_INLINE_ bool operator==(const VoxelStructure::VSIntPos &a, const VoxelStructure::VSIntPos &b) {
return a.x == b.x && a.y == b.y && a.z == b.z;
}
#endif