mirror of
https://github.com/Relintai/godot_voxel.git
synced 2025-01-27 08:29:19 +01:00
Clang-format
This commit is contained in:
parent
bc75ec863b
commit
51596fef95
16
vector3i.h
16
vector3i.h
@ -1,8 +1,8 @@
|
||||
#ifndef VOXEL_VECTOR3I_H
|
||||
#define VOXEL_VECTOR3I_H
|
||||
|
||||
#include <core/math/vector3.h>
|
||||
#include <core/hashfuncs.h>
|
||||
#include <core/math/vector3.h>
|
||||
|
||||
struct Vector3i {
|
||||
|
||||
@ -15,9 +15,11 @@ struct Vector3i {
|
||||
int coords[3];
|
||||
};
|
||||
|
||||
_FORCE_INLINE_ Vector3i() : x(0), y(0), z(0) {}
|
||||
_FORCE_INLINE_ Vector3i()
|
||||
: x(0), y(0), z(0) {}
|
||||
|
||||
_FORCE_INLINE_ Vector3i(int px, int py, int pz) : x(px), y(py), z(pz) {}
|
||||
_FORCE_INLINE_ Vector3i(int px, int py, int pz)
|
||||
: x(px), y(py), z(pz) {}
|
||||
|
||||
_FORCE_INLINE_ Vector3i(const Vector3i &other) {
|
||||
*this = other;
|
||||
@ -81,16 +83,14 @@ struct Vector3i {
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ bool is_contained_in(const Vector3i &min, const Vector3i &max) {
|
||||
return x >= min.x && y >= min.y && z >= min.z
|
||||
&& x < max.x && y < max.y && z < max.z;
|
||||
return x >= min.x && y >= min.y && z >= min.z && x < max.x && y < max.y && z < max.z;
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ Vector3i wrap(const Vector3i &size) {
|
||||
return Vector3i(
|
||||
x % size.x,
|
||||
y % size.y,
|
||||
z % size.z
|
||||
);
|
||||
z % size.z);
|
||||
}
|
||||
|
||||
static void sort_min_max(Vector3i &a, Vector3i &b) {
|
||||
@ -107,7 +107,6 @@ private:
|
||||
b = temp;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
_FORCE_INLINE_ Vector3i operator+(const Vector3i a, const Vector3i &b) {
|
||||
@ -151,4 +150,3 @@ struct Vector3iHasher {
|
||||
};
|
||||
|
||||
#endif // VOXEL_VECTOR3I_H
|
||||
|
||||
|
44
voxel.cpp
44
voxel.cpp
@ -2,13 +2,13 @@
|
||||
#include "voxel_library.h"
|
||||
#include "voxel_mesher.h"
|
||||
|
||||
Voxel::Voxel() : Reference(),
|
||||
Voxel::Voxel()
|
||||
: Reference(),
|
||||
_id(-1),
|
||||
_material_id(0),
|
||||
_is_transparent(false),
|
||||
_library(NULL),
|
||||
_color(1.f, 1.f, 1.f)
|
||||
{}
|
||||
_color(1.f, 1.f, 1.f) {}
|
||||
|
||||
Ref<Voxel> Voxel::set_name(String name) {
|
||||
_name = name;
|
||||
@ -41,60 +41,48 @@ Ref<Voxel> Voxel::set_transparent(bool t) {
|
||||
|
||||
Ref<Voxel> Voxel::set_cube_geometry(float sy) {
|
||||
const Vector3 vertices[SIDE_COUNT][6] = {
|
||||
{
|
||||
// LEFT
|
||||
{ // LEFT
|
||||
Vector3(0, 0, 0),
|
||||
Vector3(0, sy, 0),
|
||||
Vector3(0, sy, 1),
|
||||
Vector3(0, 0, 0),
|
||||
Vector3(0, sy, 1),
|
||||
Vector3(0, 0, 1),
|
||||
},
|
||||
{
|
||||
// RIGHT
|
||||
Vector3(0, 0, 1) },
|
||||
{ // RIGHT
|
||||
Vector3(1, 0, 0),
|
||||
Vector3(1, sy, 1),
|
||||
Vector3(1, sy, 0),
|
||||
Vector3(1, 0, 0),
|
||||
Vector3(1, 0, 1),
|
||||
Vector3(1, sy, 1)
|
||||
},
|
||||
{
|
||||
// BOTTOM
|
||||
Vector3(1, sy, 1) },
|
||||
{ // BOTTOM
|
||||
Vector3(0, 0, 0),
|
||||
Vector3(1, 0, 1),
|
||||
Vector3(1, 0, 0),
|
||||
Vector3(0, 0, 0),
|
||||
Vector3(0, 0, 1),
|
||||
Vector3(1, 0, 1)
|
||||
},
|
||||
{
|
||||
// TOP
|
||||
Vector3(1, 0, 1) },
|
||||
{ // TOP
|
||||
Vector3(0, sy, 0),
|
||||
Vector3(1, sy, 0),
|
||||
Vector3(1, sy, 1),
|
||||
Vector3(0, sy, 0),
|
||||
Vector3(1, sy, 1),
|
||||
Vector3(0, sy, 1)
|
||||
},
|
||||
{
|
||||
// BACK
|
||||
Vector3(0, sy, 1) },
|
||||
{ // BACK
|
||||
Vector3(0, 0, 0),
|
||||
Vector3(1, 0, 0),
|
||||
Vector3(1, sy, 0),
|
||||
Vector3(0, 0, 0),
|
||||
Vector3(1, sy, 0),
|
||||
Vector3(0, sy, 0),
|
||||
},
|
||||
{
|
||||
// FRONT
|
||||
Vector3(0, sy, 0) },
|
||||
{ // FRONT
|
||||
Vector3(1, 0, 1),
|
||||
Vector3(0, 0, 1),
|
||||
Vector3(1, sy, 1),
|
||||
Vector3(0, 0, 1),
|
||||
Vector3(0, sy, 1),
|
||||
Vector3(1, sy, 1)
|
||||
}
|
||||
Vector3(1, sy, 1) }
|
||||
};
|
||||
|
||||
for (unsigned int side = 0; side < SIDE_COUNT; ++side) {
|
||||
@ -200,6 +188,4 @@ void Voxel::_bind_methods() {
|
||||
BIND_CONSTANT(CHANNEL_TYPE)
|
||||
BIND_CONSTANT(CHANNEL_ISOLEVEL)
|
||||
BIND_CONSTANT(CHANNEL_DATA)
|
||||
|
||||
}
|
||||
|
||||
|
3
voxel.h
3
voxel.h
@ -93,11 +93,8 @@ private:
|
||||
PoolVector<Vector2> _model_side_uv[SIDE_COUNT];
|
||||
|
||||
// TODO Child voxel types
|
||||
|
||||
};
|
||||
|
||||
VARIANT_ENUM_CAST(Voxel::ChannelMode)
|
||||
|
||||
|
||||
#endif // VOXEL_TYPE_H
|
||||
|
||||
|
@ -1,10 +1,8 @@
|
||||
#include "voxel_buffer.h"
|
||||
#include <string.h>
|
||||
#include <math_funcs.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
VoxelBuffer::VoxelBuffer() {
|
||||
|
||||
}
|
||||
|
||||
VoxelBuffer::~VoxelBuffer() {
|
||||
@ -58,8 +56,7 @@ int VoxelBuffer::get_voxel(int x, int y, int z, unsigned int channel_index) cons
|
||||
|
||||
if (validate_pos(x, y, z) && channel.data) {
|
||||
return channel.data[index(x, y, z)];
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return channel.defval;
|
||||
}
|
||||
}
|
||||
@ -75,8 +72,7 @@ void VoxelBuffer::set_voxel(int value, int x, int y, int z, unsigned int channel
|
||||
create_channel(channel_index, _size);
|
||||
channel.data[index(x, y, z)] = value;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
channel.data[index(x, y, z)] = value;
|
||||
}
|
||||
}
|
||||
@ -162,8 +158,7 @@ void VoxelBuffer::copy_from(const VoxelBuffer & other, unsigned int channel_inde
|
||||
create_channel_noinit(channel_index, _size);
|
||||
}
|
||||
memcpy(channel.data, other_channel.data, get_volume() * sizeof(uint8_t));
|
||||
}
|
||||
else if(channel.data) {
|
||||
} else if (channel.data) {
|
||||
delete_channel(channel_index);
|
||||
}
|
||||
|
||||
@ -188,8 +183,7 @@ void VoxelBuffer::copy_from(const VoxelBuffer & other, Vector3i src_min, Vector3
|
||||
|
||||
if (area_size == _size) {
|
||||
copy_from(other, channel_index);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (other_channel.data) {
|
||||
if (channel.data == NULL) {
|
||||
create_channel(channel_index, _size);
|
||||
@ -204,8 +198,7 @@ void VoxelBuffer::copy_from(const VoxelBuffer & other, Vector3i src_min, Vector3
|
||||
memcpy(&channel.data[dst_ri], &other_channel.data[src_ri], area_size.y * sizeof(uint8_t));
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (channel.defval != other_channel.defval) {
|
||||
} else if (channel.defval != other_channel.defval) {
|
||||
if (channel.data == NULL) {
|
||||
create_channel(channel_index, _size);
|
||||
}
|
||||
@ -261,7 +254,6 @@ void VoxelBuffer::_bind_methods() {
|
||||
|
||||
ClassDB::bind_method(D_METHOD("is_uniform", "channel"), &VoxelBuffer::is_uniform, DEFVAL(0));
|
||||
ClassDB::bind_method(D_METHOD("optimize"), &VoxelBuffer::optimize);
|
||||
|
||||
}
|
||||
|
||||
void VoxelBuffer::_copy_from_binding(Ref<VoxelBuffer> other, unsigned int channel) {
|
||||
|
@ -1,9 +1,9 @@
|
||||
#ifndef VOXEL_BUFFER_H
|
||||
#define VOXEL_BUFFER_H
|
||||
|
||||
#include "vector3i.h"
|
||||
#include <reference.h>
|
||||
#include <vector.h>
|
||||
#include "vector3i.h"
|
||||
|
||||
// Dense voxels data storage.
|
||||
// Organized in 8-bit channels like images, all optional.
|
||||
@ -61,9 +61,7 @@ public:
|
||||
void copy_from(const VoxelBuffer &other, Vector3i src_min, Vector3i src_max, Vector3i dst_min, unsigned int channel_index = 0);
|
||||
|
||||
_FORCE_INLINE_ bool validate_pos(unsigned int x, unsigned int y, unsigned int z) const {
|
||||
return x < _size.x
|
||||
&& y < _size.y
|
||||
&& z < _size.x;
|
||||
return x < _size.x && y < _size.y && z < _size.x;
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ unsigned int index(unsigned int x, unsigned int y, unsigned int z) const {
|
||||
@ -106,7 +104,8 @@ private:
|
||||
// Default value when data is null
|
||||
uint8_t defval;
|
||||
|
||||
Channel() : data(NULL), defval(0) {}
|
||||
Channel()
|
||||
: data(NULL), defval(0) {}
|
||||
};
|
||||
|
||||
// Each channel can store arbitary data.
|
||||
@ -115,8 +114,6 @@ private:
|
||||
|
||||
// How many voxels are there in the three directions. All populated channels have the same size.
|
||||
Vector3i _size;
|
||||
|
||||
};
|
||||
|
||||
#endif // VOXEL_BUFFER_H
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "voxel_library.h"
|
||||
|
||||
VoxelLibrary::VoxelLibrary() : Reference(), _atlas_size(1) {
|
||||
VoxelLibrary::VoxelLibrary()
|
||||
: Reference(), _atlas_size(1) {
|
||||
// Defaults
|
||||
create_voxel(0, "air")->set_transparent(true);
|
||||
create_voxel(1, "solid")->set_transparent(false)->set_cube_geometry();
|
||||
@ -40,6 +41,4 @@ void VoxelLibrary::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("get_voxel", "id"), &VoxelLibrary::_get_voxel_bind);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("set_atlas_size", "square_size"), &VoxelLibrary::set_atlas_size);
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
#ifndef VOXEL_LIBRARY_H
|
||||
#define VOXEL_LIBRARY_H
|
||||
|
||||
#include <reference.h>
|
||||
#include "voxel.h"
|
||||
#include <reference.h>
|
||||
|
||||
class VoxelLibrary : public Reference {
|
||||
GDCLASS(VoxelLibrary, Reference)
|
||||
@ -32,8 +32,6 @@ protected:
|
||||
private:
|
||||
Ref<Voxel> _voxel_types[MAX_VOXEL_TYPES];
|
||||
int _atlas_size;
|
||||
|
||||
};
|
||||
|
||||
#endif // VOXEL_LIBRARY_H
|
||||
|
||||
|
@ -28,14 +28,16 @@ VoxelBlock * VoxelBlock::create(Vector3i bpos, Ref<VoxelBuffer> buffer) {
|
||||
return block;
|
||||
}
|
||||
|
||||
VoxelBlock::VoxelBlock(): voxels(NULL) {
|
||||
VoxelBlock::VoxelBlock()
|
||||
: voxels(NULL) {
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// VoxelMap
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
VoxelMap::VoxelMap() : _last_accessed_block(NULL) {
|
||||
VoxelMap::VoxelMap()
|
||||
: _last_accessed_block(NULL) {
|
||||
for (unsigned int i = 0; i < VoxelBuffer::MAX_CHANNELS; ++i) {
|
||||
_default_voxel[i] = 0;
|
||||
}
|
||||
@ -109,8 +111,7 @@ void VoxelMap::set_block_buffer(Vector3i bpos, Ref<VoxelBuffer> buffer) {
|
||||
if (block == NULL) {
|
||||
block = VoxelBlock::create(bpos, *buffer);
|
||||
set_block(bpos, block);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
block->voxels = buffer;
|
||||
}
|
||||
}
|
||||
@ -187,20 +188,16 @@ void VoxelMap::get_buffer_copy(Vector3i min_pos, VoxelBuffer & dst_buffer, unsig
|
||||
Vector3i offset = block_to_voxel(bpos);
|
||||
// Note: copy_from takes care of clamping the area if it's on an edge
|
||||
dst_buffer.copy_from(src_buffer, min_pos - offset, max_pos - offset, offset - min_pos, channel);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
Vector3i offset = block_to_voxel(bpos);
|
||||
dst_buffer.fill_area(
|
||||
_default_voxel[channel],
|
||||
offset - min_pos,
|
||||
offset - min_pos + Vector3i(VoxelBlock::SIZE,VoxelBlock::SIZE, VoxelBlock::SIZE)
|
||||
);
|
||||
}
|
||||
|
||||
offset - min_pos + Vector3i(VoxelBlock::SIZE, VoxelBlock::SIZE, VoxelBlock::SIZE));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -246,7 +243,6 @@ void VoxelMap::clear() {
|
||||
_last_accessed_block = NULL;
|
||||
}
|
||||
|
||||
|
||||
void VoxelMap::_bind_methods() {
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_voxel", "x", "y", "z", "c"), &VoxelMap::_get_voxel_binding, DEFVAL(0));
|
||||
@ -263,12 +259,9 @@ void VoxelMap::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("get_block_size"), &VoxelMap::get_block_size);
|
||||
|
||||
//ADD_PROPERTY(PropertyInfo(Variant::INT, "iterations"), _SCS("set_iterations"), _SCS("get_iterations"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
void VoxelMap::_get_buffer_copy_binding(Vector3 pos, Ref<VoxelBuffer> dst_buffer_ref, unsigned int channel) {
|
||||
ERR_FAIL_COND(dst_buffer_ref.is_null());
|
||||
get_buffer_copy(Vector3i(pos), **dst_buffer_ref, channel);
|
||||
}
|
||||
|
||||
|
16
voxel_map.h
16
voxel_map.h
@ -1,12 +1,11 @@
|
||||
#ifndef VOXEL_MAP_H
|
||||
#define VOXEL_MAP_H
|
||||
|
||||
#include <scene/main/node.h>
|
||||
#include "voxel_buffer.h"
|
||||
#include <core/hash_map.h>
|
||||
#include <scene/3d/mesh_instance.h>
|
||||
#include <scene/3d/physics_body.h>
|
||||
#include "voxel_buffer.h"
|
||||
|
||||
#include <scene/main/node.h>
|
||||
|
||||
// Fixed-size voxel container used in VoxelMap. Used internally.
|
||||
class VoxelBlock {
|
||||
@ -25,10 +24,8 @@ public:
|
||||
|
||||
private:
|
||||
VoxelBlock();
|
||||
|
||||
};
|
||||
|
||||
|
||||
// Infinite voxel storage by means of octants like Gridmap
|
||||
class VoxelMap : public Reference {
|
||||
GDCLASS(VoxelMap, Reference)
|
||||
@ -38,16 +35,14 @@ public:
|
||||
return Vector3i(
|
||||
pos.x >> VoxelBlock::SIZE_POW2,
|
||||
pos.y >> VoxelBlock::SIZE_POW2,
|
||||
pos.z >> VoxelBlock::SIZE_POW2
|
||||
);
|
||||
pos.z >> VoxelBlock::SIZE_POW2);
|
||||
}
|
||||
|
||||
static _FORCE_INLINE_ Vector3i to_local(Vector3i pos) {
|
||||
return Vector3i(
|
||||
pos.x & VoxelBlock::SIZE_MASK,
|
||||
pos.y & VoxelBlock::SIZE_MASK,
|
||||
pos.z & VoxelBlock::SIZE_MASK
|
||||
);
|
||||
pos.z & VoxelBlock::SIZE_MASK);
|
||||
}
|
||||
|
||||
// Converts block coodinates into voxel coordinates
|
||||
@ -79,7 +74,6 @@ public:
|
||||
|
||||
void clear();
|
||||
|
||||
|
||||
private:
|
||||
void set_block(Vector3i bpos, VoxelBlock *block);
|
||||
|
||||
@ -108,8 +102,6 @@ private:
|
||||
// Voxel access will most frequently be in contiguous areas, so the same blocks are accessed.
|
||||
// To prevent too much hashing, this reference is checked before.
|
||||
VoxelBlock *_last_accessed_block;
|
||||
|
||||
};
|
||||
|
||||
#endif // VOXEL_MAP_H
|
||||
|
||||
|
@ -118,11 +118,9 @@ static const unsigned int g_edge_corners[EDGE_COUNT][2] = {
|
||||
{ 4, 5 }, { 5, 6 }, { 6, 7 }, { 7, 4 }
|
||||
};
|
||||
|
||||
|
||||
VoxelMesher::VoxelMesher():
|
||||
_baked_occlusion_darkness(0.75),
|
||||
_bake_occlusion(true)
|
||||
{}
|
||||
VoxelMesher::VoxelMesher()
|
||||
: _baked_occlusion_darkness(0.75),
|
||||
_bake_occlusion(true) {}
|
||||
|
||||
void VoxelMesher::set_library(Ref<VoxelLibrary> library) {
|
||||
ERR_FAIL_COND(library.is_null());
|
||||
@ -151,7 +149,9 @@ void VoxelMesher::set_occlusion_enabled(bool enable) {
|
||||
_bake_occlusion = enable;
|
||||
}
|
||||
|
||||
inline Color Color_greyscale(float c) { return Color(c, c, c); }
|
||||
inline Color Color_greyscale(float c) {
|
||||
return Color(c, c, c);
|
||||
}
|
||||
|
||||
inline bool is_face_visible(const VoxelLibrary &lib, const Voxel &vt, int other_voxel_id) {
|
||||
if (other_voxel_id == 0) // air
|
||||
@ -262,8 +262,7 @@ Ref<ArrayMesh> VoxelMesher::build(const VoxelBuffer & buffer, unsigned int chann
|
||||
unsigned int corner = g_side_corners[side][j];
|
||||
if (shaded_corner[corner] == 2) {
|
||||
shaded_corner[corner] = 3;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
Vector3i corner_normal = g_corner_inormals[corner];
|
||||
unsigned int cx = x + corner_normal.x;
|
||||
unsigned int cy = y + corner_normal.y;
|
||||
@ -326,9 +325,7 @@ Ref<ArrayMesh> VoxelMesher::build(const VoxelBuffer & buffer, unsigned int chann
|
||||
st.add_vertex(rv[i] + pos);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,13 @@
|
||||
#ifndef VOXEL_MESHER
|
||||
#define VOXEL_MESHER
|
||||
|
||||
#include <reference.h>
|
||||
#include <scene/resources/mesh.h>
|
||||
#include <scene/resources/surface_tool.h>
|
||||
#include "voxel.h"
|
||||
#include "voxel_buffer.h"
|
||||
#include "voxel_library.h"
|
||||
#include "zprofiling.h"
|
||||
|
||||
#include <reference.h>
|
||||
#include <scene/resources/mesh.h>
|
||||
#include <scene/resources/surface_tool.h>
|
||||
|
||||
// TODO Should be renamed VoxelMesherCubic or something like that
|
||||
class VoxelMesher : public Reference {
|
||||
@ -50,5 +49,4 @@ private:
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
#endif // VOXEL_MESHER
|
||||
|
@ -3,7 +3,6 @@
|
||||
#include "transvoxel_tables.cpp"
|
||||
#include <os/os.h>
|
||||
|
||||
|
||||
inline float tof(int8_t v) {
|
||||
return static_cast<float>(v) / 256.f;
|
||||
}
|
||||
@ -44,8 +43,7 @@ inline Vector3i dir_to_prev_vec(uint8_t dir) {
|
||||
return Vector3i(
|
||||
-(dir & 1),
|
||||
-((dir >> 1) & 1),
|
||||
-((dir >> 2) & 1)
|
||||
);
|
||||
-((dir >> 2) & 1));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@ -168,15 +166,14 @@ void VoxelMesherSmooth::build_mesh(const VoxelBuffer & voxels, unsigned int chan
|
||||
|
||||
// Concatenate the sign of cell values to obtain the case code.
|
||||
// Index 0 is the less significant bit, and index 7 is the most significant bit.
|
||||
uint8_t case_code =
|
||||
sign(cell_samples[0])
|
||||
| (sign(cell_samples[1]) << 1)
|
||||
| (sign(cell_samples[2]) << 2)
|
||||
| (sign(cell_samples[3]) << 3)
|
||||
| (sign(cell_samples[4]) << 4)
|
||||
| (sign(cell_samples[5]) << 5)
|
||||
| (sign(cell_samples[6]) << 6)
|
||||
| (sign(cell_samples[7]) << 7);
|
||||
uint8_t case_code = sign(cell_samples[0]);
|
||||
case_code |= (sign(cell_samples[1]) << 1);
|
||||
case_code |= (sign(cell_samples[2]) << 2);
|
||||
case_code |= (sign(cell_samples[3]) << 3);
|
||||
case_code |= (sign(cell_samples[4]) << 4);
|
||||
case_code |= (sign(cell_samples[5]) << 5);
|
||||
case_code |= (sign(cell_samples[6]) << 6);
|
||||
case_code |= (sign(cell_samples[7]) << 7);
|
||||
|
||||
{
|
||||
ReuseCell &rc = get_reuse_cell(pos);
|
||||
@ -195,14 +192,9 @@ void VoxelMesherSmooth::build_mesh(const VoxelBuffer & voxels, unsigned int chan
|
||||
|
||||
Vector3i p = pos + g_corner_dirs[i];
|
||||
|
||||
float nx = tof(tos(voxels.get_voxel(p - Vector3i(1,0,0), channel)))
|
||||
- tof(tos(voxels.get_voxel(p + Vector3i(1,0,0), channel)));
|
||||
|
||||
float ny = tof(tos(voxels.get_voxel(p - Vector3i(0,1,0), channel)))
|
||||
- tof(tos(voxels.get_voxel(p + Vector3i(0,1,0), channel)));
|
||||
|
||||
float nz = tof(tos(voxels.get_voxel(p - Vector3i(0,0,1), channel)))
|
||||
- tof(tos(voxels.get_voxel(p + Vector3i(0,0,1), channel)));
|
||||
float nx = tof(tos(voxels.get_voxel(p - Vector3i(1, 0, 0), channel))) - tof(tos(voxels.get_voxel(p + Vector3i(1, 0, 0), channel)));
|
||||
float ny = tof(tos(voxels.get_voxel(p - Vector3i(0, 1, 0), channel))) - tof(tos(voxels.get_voxel(p + Vector3i(0, 1, 0), channel)));
|
||||
float nz = tof(tos(voxels.get_voxel(p - Vector3i(0, 0, 1), channel))) - tof(tos(voxels.get_voxel(p + Vector3i(0, 0, 1), channel)));
|
||||
|
||||
corner_normals[i] = Vector3(nx, ny, nz);
|
||||
corner_normals[i].normalize();
|
||||
@ -214,9 +206,7 @@ void VoxelMesherSmooth::build_mesh(const VoxelBuffer & voxels, unsigned int chan
|
||||
// While iterating through the cells in a block, a 3-bit mask is maintained whose bits indicate
|
||||
// whether corresponding bits in a direction code are valid
|
||||
uint8_t direction_validity_mask =
|
||||
(pos.x > 1 ? 1 : 0)
|
||||
| ((pos.y > 1 ? 1 : 0) << 1)
|
||||
| ((pos.z > 1 ? 1 : 0) << 2);
|
||||
(pos.x > 1 ? 1 : 0) | ((pos.y > 1 ? 1 : 0) << 1) | ((pos.z > 1 ? 1 : 0) << 2);
|
||||
|
||||
uint8_t regular_cell_class_index = Transvoxel::regularCellClass[case_code];
|
||||
Transvoxel::RegularCellData regular_cell_class = Transvoxel::regularCellData[regular_cell_class_index];
|
||||
@ -377,23 +367,18 @@ void VoxelMesherSmooth::build_mesh(const VoxelBuffer & voxels, unsigned int chan
|
||||
//OS::get_singleton()->print("\n");
|
||||
}
|
||||
|
||||
|
||||
VoxelMesherSmooth::ReuseCell &VoxelMesherSmooth::get_reuse_cell(Vector3i pos) {
|
||||
int j = pos.z & 1;
|
||||
int i = pos.y * m_block_size.y + pos.x;
|
||||
return m_cache[j][i];
|
||||
}
|
||||
|
||||
|
||||
void VoxelMesherSmooth::emit_vertex(Vector3 primary, Vector3 normal) {
|
||||
m_output_vertices.push_back(primary - PAD.to_vec3());
|
||||
m_output_normals.push_back(normal);
|
||||
}
|
||||
|
||||
|
||||
void VoxelMesherSmooth::_bind_methods() {
|
||||
|
||||
ClassDB::bind_method(D_METHOD("build", "voxels", "channel", "existing_mesh"), &VoxelMesherSmooth::build_ref, DEFVAL(Variant()));
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
#include "voxel_provider.h"
|
||||
#include "voxel_map.h"
|
||||
|
||||
|
||||
void VoxelProvider::emerge_block(Ref<VoxelBuffer> out_buffer, Vector3i block_pos) {
|
||||
ERR_FAIL_COND(out_buffer.is_null());
|
||||
ScriptInstance *script = get_script_instance();
|
||||
@ -40,7 +39,4 @@ void VoxelProvider::_bind_methods() {
|
||||
|
||||
ClassDB::bind_method(D_METHOD("emerge_block", "out_buffer", "block_pos"), &VoxelProvider::_emerge_block);
|
||||
ClassDB::bind_method(D_METHOD("immerge_block", "buffer", "block_pos"), &VoxelProvider::_immerge_block);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -4,7 +4,6 @@
|
||||
#include "reference.h"
|
||||
#include "voxel_buffer.h"
|
||||
|
||||
|
||||
class VoxelProvider : public Reference {
|
||||
GDCLASS(VoxelProvider, Reference)
|
||||
public:
|
||||
@ -18,5 +17,4 @@ protected:
|
||||
void _immerge_block(Ref<VoxelBuffer> buffer, Vector3 block_pos);
|
||||
};
|
||||
|
||||
|
||||
#endif // VOXEL_PROVIDER_H
|
||||
|
@ -3,7 +3,6 @@
|
||||
|
||||
VARIANT_ENUM_CAST(VoxelProviderTest::Mode)
|
||||
|
||||
|
||||
VoxelProviderTest::VoxelProviderTest() {
|
||||
_mode = MODE_FLAT;
|
||||
_voxel_type = 1;
|
||||
@ -108,4 +107,3 @@ void VoxelProviderTest::_bind_methods() {
|
||||
BIND_CONSTANT(MODE_FLAT);
|
||||
BIND_CONSTANT(MODE_WAVES);
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,6 @@
|
||||
|
||||
#include "voxel_provider.h"
|
||||
|
||||
|
||||
class VoxelProviderTest : public VoxelProvider {
|
||||
GDCLASS(VoxelProviderTest, VoxelProvider)
|
||||
|
||||
@ -48,6 +47,4 @@ private:
|
||||
Vector3i _pattern_size;
|
||||
};
|
||||
|
||||
|
||||
#endif // VOXEL_PROVIDER_TEST_H
|
||||
|
||||
|
@ -10,8 +10,7 @@ bool voxel_raycast(
|
||||
void *predicate_context,
|
||||
real_t max_distance,
|
||||
Vector3i &out_hit_pos,
|
||||
Vector3i & out_prev_pos
|
||||
){
|
||||
Vector3i &out_prev_pos) {
|
||||
// Equation : p + v*t
|
||||
// p : ray start position (ray.pos)
|
||||
// v : ray orientation vector (ray.dir)
|
||||
@ -31,8 +30,7 @@ bool voxel_raycast(
|
||||
Vector3i hit_pos(
|
||||
Math::floor(ray_origin.x),
|
||||
Math::floor(ray_origin.y),
|
||||
Math::floor(ray_origin.z)
|
||||
);
|
||||
Math::floor(ray_origin.z));
|
||||
Vector3i hit_prev_pos = hit_pos;
|
||||
|
||||
// Voxel step
|
||||
@ -51,56 +49,45 @@ bool voxel_raycast(
|
||||
real_t tcross_z; // At which value of T we will cross a depth line?
|
||||
|
||||
// X initialization
|
||||
if(xi_step != 0)
|
||||
{
|
||||
if (xi_step != 0) {
|
||||
if (xi_step == 1)
|
||||
tcross_x = (Math::ceil(ray_origin.x) - ray_origin.x) * tdelta_x;
|
||||
else
|
||||
tcross_x = (ray_origin.x - Math::floor(ray_origin.x)) * tdelta_x;
|
||||
}
|
||||
else
|
||||
} else
|
||||
tcross_x = g_infinite; // Will never cross on X
|
||||
|
||||
// Y initialization
|
||||
if(yi_step != 0)
|
||||
{
|
||||
if (yi_step != 0) {
|
||||
if (yi_step == 1)
|
||||
tcross_y = (Math::ceil(ray_origin.y) - ray_origin.y) * tdelta_y;
|
||||
else
|
||||
tcross_y = (ray_origin.y - Math::floor(ray_origin.y)) * tdelta_y;
|
||||
}
|
||||
else
|
||||
} else
|
||||
tcross_y = g_infinite; // Will never cross on X
|
||||
|
||||
// Z initialization
|
||||
if(zi_step != 0)
|
||||
{
|
||||
if (zi_step != 0) {
|
||||
if (zi_step == 1)
|
||||
tcross_z = (Math::ceil(ray_origin.z) - ray_origin.z) * tdelta_z;
|
||||
else
|
||||
tcross_z = (ray_origin.z - Math::floor(ray_origin.z)) * tdelta_z;
|
||||
}
|
||||
else
|
||||
} else
|
||||
tcross_z = g_infinite; // Will never cross on X
|
||||
|
||||
/* Iteration */
|
||||
|
||||
do
|
||||
{
|
||||
do {
|
||||
hit_prev_pos = hit_pos;
|
||||
if(tcross_x < tcross_y)
|
||||
{
|
||||
if(tcross_x < tcross_z)
|
||||
{
|
||||
if (tcross_x < tcross_y) {
|
||||
if (tcross_x < tcross_z) {
|
||||
// X collision
|
||||
//hit.prevPos.x = hit.pos.x;
|
||||
hit_pos.x += xi_step;
|
||||
if (tcross_x > max_distance)
|
||||
return false;
|
||||
tcross_x += tdelta_x;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Z collision (duplicate code)
|
||||
//hit.prevPos.z = hit.pos.z;
|
||||
hit_pos.z += zi_step;
|
||||
@ -108,20 +95,15 @@ bool voxel_raycast(
|
||||
return false;
|
||||
tcross_z += tdelta_z;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(tcross_y < tcross_z)
|
||||
{
|
||||
} else {
|
||||
if (tcross_y < tcross_z) {
|
||||
// Y collision
|
||||
//hit.prevPos.y = hit.pos.y;
|
||||
hit_pos.y += yi_step;
|
||||
if (tcross_y > max_distance)
|
||||
return false;
|
||||
tcross_y += tdelta_y;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Z collision (duplicate code)
|
||||
//hit.prevPos.z = hit.pos.z;
|
||||
hit_pos.z += zi_step;
|
||||
@ -138,4 +120,3 @@ bool voxel_raycast(
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include <vector3.h>
|
||||
#include "vector3i.h"
|
||||
#include <vector3.h>
|
||||
|
||||
// TODO Having a C++11 lambda would be nice...
|
||||
// TODO that could be a template function
|
||||
// pos: voxel position
|
||||
// context: arguments to carry (as a lamdbda capture)
|
||||
typedef bool (*VoxelPredicate)(Vector3i pos, void *context);
|
||||
@ -13,6 +13,4 @@ bool voxel_raycast(
|
||||
void *predicate_context, // Handle that one with care
|
||||
real_t max_distance,
|
||||
Vector3i &out_hit_pos,
|
||||
Vector3i & out_prev_pos
|
||||
);
|
||||
|
||||
Vector3i &out_prev_pos);
|
||||
|
@ -1,9 +1,10 @@
|
||||
#include "voxel_terrain.h"
|
||||
#include <scene/3d/mesh_instance.h>
|
||||
#include <os/os.h>
|
||||
#include "voxel_raycast.h"
|
||||
#include <os/os.h>
|
||||
#include <scene/3d/mesh_instance.h>
|
||||
|
||||
VoxelTerrain::VoxelTerrain(): Node(), _generate_collisions(true) {
|
||||
VoxelTerrain::VoxelTerrain()
|
||||
: Node(), _generate_collisions(true) {
|
||||
|
||||
_map = Ref<VoxelMap>(memnew(VoxelMap));
|
||||
_mesher = Ref<VoxelMesher>(memnew(VoxelMesher));
|
||||
@ -105,20 +106,17 @@ void VoxelTerrain::make_voxel_dirty(Vector3i pos) {
|
||||
|
||||
if (rpos.x == 0)
|
||||
make_block_dirty(bpos - Vector3i(1, 0, 0));
|
||||
else
|
||||
if(rpos.x == max)
|
||||
else if (rpos.x == max)
|
||||
make_block_dirty(bpos + Vector3i(1, 0, 0));
|
||||
|
||||
if (rpos.y == 0)
|
||||
make_block_dirty(bpos - Vector3i(0, 1, 0));
|
||||
else
|
||||
if(rpos.y == max)
|
||||
else if (rpos.y == max)
|
||||
make_block_dirty(bpos + Vector3i(0, 1, 0));
|
||||
|
||||
if (rpos.z == 0)
|
||||
make_block_dirty(bpos - Vector3i(0, 0, 1));
|
||||
else
|
||||
if(rpos.z == max)
|
||||
else if (rpos.z == max)
|
||||
make_block_dirty(bpos + Vector3i(0, 0, 1));
|
||||
|
||||
// We might want to update blocks in corners in order to update ambient occlusion
|
||||
@ -182,9 +180,7 @@ void VoxelTerrain::make_voxel_dirty(Vector3i pos) {
|
||||
{ 24, 15, 21, 25 }, { 25 }, { 26, 17, 23, 25 }
|
||||
};
|
||||
|
||||
int m = get_border_index(rpos.x, max)
|
||||
+ 3*get_border_index(rpos.z, max)
|
||||
+ 9*get_border_index(rpos.y, max);
|
||||
int m = get_border_index(rpos.x, max) + 3 * get_border_index(rpos.z, max) + 9 * get_border_index(rpos.y, max);
|
||||
|
||||
const int *ce_indexes = ce_indexes_lut[m];
|
||||
int ce_count = ce_counts[m];
|
||||
@ -305,8 +301,7 @@ void VoxelTerrain::update_blocks() {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// Only update the block, neighbors will probably follow if needed
|
||||
update_block_mesh(block_pos);
|
||||
//OS::get_singleton()->print("Update (%i, %i, %i)\n", block_pos.x, block_pos.y, block_pos.z);
|
||||
@ -318,7 +313,6 @@ void VoxelTerrain::update_blocks() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static inline bool is_mesh_empty(Ref<Mesh> mesh_ref) {
|
||||
if (mesh_ref.is_null())
|
||||
return true;
|
||||
@ -331,7 +325,6 @@ static inline bool is_mesh_empty(Ref<Mesh> mesh_ref) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void VoxelTerrain::update_block_mesh(Vector3i block_pos) {
|
||||
|
||||
VoxelBlock *block = _map->get_block(block_pos);
|
||||
@ -367,8 +360,7 @@ void VoxelTerrain::update_block_mesh(Vector3i block_pos) {
|
||||
if (mesh_instance) {
|
||||
mesh_instance->set_mesh(Ref<Mesh>());
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// The mesh exist and it has vertices
|
||||
|
||||
// TODO Don't use nodes! Use servers directly, it's faster
|
||||
@ -379,8 +371,7 @@ void VoxelTerrain::update_block_mesh(Vector3i block_pos) {
|
||||
mesh_instance->set_translation(block_node_pos);
|
||||
add_child(mesh_instance);
|
||||
block->mesh_instance_path = mesh_instance->get_path();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// Update mesh
|
||||
VOXEL_PROFILE_BEGIN("mesh_instance_set_mesh")
|
||||
mesh_instance->set_mesh(mesh);
|
||||
@ -449,8 +440,7 @@ Variant VoxelTerrain::_raycast_binding(Vector3 origin, Vector3 direction, real_t
|
||||
hit["position"] = hit_pos.to_vec3();
|
||||
hit["prev_position"] = prev_pos.to_vec3();
|
||||
return hit;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return Variant(); // Null dictionary, no alloc
|
||||
}
|
||||
}
|
||||
@ -484,6 +474,4 @@ void VoxelTerrain::_bind_methods() {
|
||||
#ifdef VOXEL_PROFILING
|
||||
ClassDB::bind_method(D_METHOD("get_profiling_info"), &VoxelTerrain::get_profiling_info);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,12 @@
|
||||
#ifndef VOXEL_TERRAIN_H
|
||||
#define VOXEL_TERRAIN_H
|
||||
|
||||
#include <scene/main/node.h>
|
||||
#include "voxel_map.h"
|
||||
#include "voxel_mesher.h"
|
||||
#include "voxel_mesher_smooth.h"
|
||||
#include "voxel_provider.h"
|
||||
#include "zprofiling.h"
|
||||
#include <scene/main/node.h>
|
||||
|
||||
// Infinite static terrain made of voxels.
|
||||
// It is loaded around VoxelTerrainStreamers.
|
||||
@ -90,8 +90,6 @@ private:
|
||||
ZProfiler _zprofiler;
|
||||
Dictionary get_profiling_info() { return _zprofiler.get_all_serialized_info(); }
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
#endif // VOXEL_TERRAIN_H
|
||||
|
||||
|
@ -52,13 +52,10 @@ void ZProfileVar::begin(uint64_t time) {
|
||||
void ZProfileVar::end(uint64_t time) {
|
||||
instant_time = time - _begin_time;
|
||||
|
||||
if(hits == 0)
|
||||
{
|
||||
if (hits == 0) {
|
||||
min_time = instant_time;
|
||||
max_time = instant_time;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
if (instant_time < min_time)
|
||||
min_time = instant_time;
|
||||
|
||||
@ -116,8 +113,7 @@ ZProfileVar * ZProfiler::get_var(String key) {
|
||||
if (pv == NULL) {
|
||||
v = memnew(ZProfileVar);
|
||||
_vars[key] = v;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
v = *pv;
|
||||
}
|
||||
return v;
|
||||
@ -144,6 +140,4 @@ Dictionary ZProfiler::get_all_serialized_info() const {
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
#endif // VOXEL_PROFILING
|
||||
|
||||
|
@ -5,9 +5,9 @@
|
||||
|
||||
#ifdef VOXEL_PROFILING
|
||||
|
||||
#include <ustring.h>
|
||||
#include <dictionary.h>
|
||||
#include <hash_map.h>
|
||||
#include <ustring.h>
|
||||
|
||||
#define VOXEL_PROFILE_BEGIN(_key) _zprofiler.begin(_key);
|
||||
#define VOXEL_PROFILE_END(_key) _zprofiler.end(_key);
|
||||
|
Loading…
Reference in New Issue
Block a user