2016-05-08 01:09:00 +02:00
|
|
|
#include "voxel_mesher.h"
|
2016-05-01 22:20:27 +02:00
|
|
|
#include "voxel_library.h"
|
2017-08-19 21:10:31 +02:00
|
|
|
#include "cube_tables.h"
|
2018-09-25 01:54:07 +02:00
|
|
|
#include "utility.h"
|
2018-09-29 23:41:52 +02:00
|
|
|
#include <core/os/os.h>
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void raw_copy_to(PoolVector<T> &to, const Vector<T> &from) {
|
|
|
|
to.resize(from.size());
|
|
|
|
typename PoolVector<T>::Write w = to.write();
|
|
|
|
memcpy(w.ptr(), from.ptr(), from.size() * sizeof(T));
|
|
|
|
}
|
|
|
|
|
2016-05-04 14:52:23 +02:00
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
VoxelMesher::VoxelMesher()
|
2018-09-30 02:52:40 +02:00
|
|
|
: _baked_occlusion_darkness(0.8),
|
2017-08-13 01:19:39 +02:00
|
|
|
_bake_occlusion(true) {}
|
2016-05-01 15:00:02 +02:00
|
|
|
|
2016-05-08 01:09:00 +02:00
|
|
|
void VoxelMesher::set_library(Ref<VoxelLibrary> library) {
|
2017-08-13 01:19:39 +02:00
|
|
|
_library = library;
|
2016-05-01 15:00:02 +02:00
|
|
|
}
|
|
|
|
|
2016-05-08 01:09:00 +02:00
|
|
|
void VoxelMesher::set_occlusion_darkness(float darkness) {
|
2017-08-13 01:19:39 +02:00
|
|
|
_baked_occlusion_darkness = darkness;
|
|
|
|
if (_baked_occlusion_darkness < 0.0)
|
|
|
|
_baked_occlusion_darkness = 0.0;
|
|
|
|
else if (_baked_occlusion_darkness >= 1.0)
|
|
|
|
_baked_occlusion_darkness = 1.0;
|
2016-05-04 14:52:23 +02:00
|
|
|
}
|
2017-04-06 23:44:11 +02:00
|
|
|
|
2016-05-08 01:09:00 +02:00
|
|
|
void VoxelMesher::set_occlusion_enabled(bool enable) {
|
2017-08-13 01:19:39 +02:00
|
|
|
_bake_occlusion = enable;
|
2016-05-04 14:52:23 +02:00
|
|
|
}
|
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
inline Color Color_greyscale(float c) {
|
|
|
|
return Color(c, c, c);
|
|
|
|
}
|
2016-05-04 14:52:23 +02:00
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
inline bool is_face_visible(const VoxelLibrary &lib, const Voxel &vt, int other_voxel_id) {
|
|
|
|
if (other_voxel_id == 0) // air
|
|
|
|
return true;
|
|
|
|
if (lib.has_voxel(other_voxel_id)) {
|
|
|
|
const Voxel &other_vt = lib.get_voxel_const(other_voxel_id);
|
|
|
|
return other_vt.is_transparent() && vt.get_id() != other_voxel_id;
|
|
|
|
}
|
|
|
|
return true;
|
2016-05-05 02:12:23 +02:00
|
|
|
}
|
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
inline bool is_transparent(const VoxelLibrary &lib, int voxel_id) {
|
|
|
|
if (lib.has_voxel(voxel_id))
|
|
|
|
return lib.get_voxel_const(voxel_id).is_transparent();
|
|
|
|
return true;
|
2016-05-05 02:12:23 +02:00
|
|
|
}
|
|
|
|
|
2018-09-25 01:54:07 +02:00
|
|
|
Ref<ArrayMesh> VoxelMesher::build_mesh(Ref<VoxelBuffer> buffer_ref, unsigned int channel, Array materials, Ref<ArrayMesh> mesh) {
|
2017-06-24 01:20:24 +02:00
|
|
|
ERR_FAIL_COND_V(buffer_ref.is_null(), Ref<ArrayMesh>());
|
2018-09-25 01:54:07 +02:00
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
VoxelBuffer &buffer = **buffer_ref;
|
2018-09-25 01:54:07 +02:00
|
|
|
Array surfaces = build(buffer, channel, Vector3i(), buffer.get_size());
|
|
|
|
|
|
|
|
if(mesh.is_null())
|
|
|
|
mesh.instance();
|
|
|
|
|
|
|
|
int surface = mesh->get_surface_count();
|
|
|
|
for(int i = 0; i < surfaces.size(); ++i) {
|
|
|
|
|
|
|
|
Array arrays = surfaces[i];
|
|
|
|
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays);
|
|
|
|
|
|
|
|
Ref<Material> material = materials[i];
|
|
|
|
if(material.is_valid()) {
|
|
|
|
mesh->surface_set_material(surface, material);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-06 23:44:11 +02:00
|
|
|
return mesh;
|
2016-05-10 01:59:54 +02:00
|
|
|
}
|
|
|
|
|
2018-09-25 01:54:07 +02:00
|
|
|
Array VoxelMesher::build(const VoxelBuffer &buffer, unsigned int channel, Vector3i min, Vector3i max) {
|
2018-09-29 23:41:52 +02:00
|
|
|
uint64_t time_before = OS::get_singleton()->get_ticks_usec();
|
|
|
|
|
2018-09-25 01:54:07 +02:00
|
|
|
ERR_FAIL_COND_V(_library.is_null(), Array());
|
|
|
|
ERR_FAIL_COND_V(channel >= VoxelBuffer::MAX_CHANNELS, Array());
|
2016-05-01 15:00:02 +02:00
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
const VoxelLibrary &library = **_library;
|
2016-05-01 15:00:02 +02:00
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
for (unsigned int i = 0; i < MAX_MATERIALS; ++i) {
|
2017-08-19 21:10:31 +02:00
|
|
|
Arrays &a = _arrays[i];
|
|
|
|
a.positions.clear();
|
|
|
|
a.normals.clear();
|
|
|
|
a.uvs.clear();
|
|
|
|
a.colors.clear();
|
|
|
|
a.indices.clear();
|
2017-08-13 00:08:53 +02:00
|
|
|
}
|
2016-05-01 15:00:02 +02:00
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
float baked_occlusion_darkness;
|
|
|
|
if (_bake_occlusion)
|
|
|
|
baked_occlusion_darkness = _baked_occlusion_darkness / 3.0;
|
2016-05-04 14:52:23 +02:00
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
// The technique is Culled faces.
|
|
|
|
// Could be improved with greedy meshing: https://0fps.net/2012/06/30/meshing-in-a-minecraft-game/
|
|
|
|
// However I don't feel it's worth it yet:
|
|
|
|
// - Not so much gain for organic worlds with lots of texture variations
|
|
|
|
// - Works well with cubes but not with any shape
|
|
|
|
// - Slower
|
|
|
|
// => Could be implemented in a separate class?
|
2016-05-05 21:08:52 +02:00
|
|
|
|
2017-04-06 23:44:11 +02:00
|
|
|
// Data must be padded, hence the off-by-one
|
|
|
|
Vector3i::sort_min_max(min, max);
|
2017-08-13 01:19:39 +02:00
|
|
|
const Vector3i pad(1, 1, 1);
|
2017-04-06 23:44:11 +02:00
|
|
|
min.clamp_to(pad, max);
|
2017-08-13 01:19:39 +02:00
|
|
|
max.clamp_to(min, buffer.get_size() - pad);
|
2017-04-06 23:44:11 +02:00
|
|
|
|
2017-08-19 21:10:31 +02:00
|
|
|
int index_offset = 0;
|
|
|
|
|
2018-09-30 02:55:09 +02:00
|
|
|
// Iterate 3D padded data to extract voxel faces.
|
|
|
|
// This is the most intensive job in this class, so all required data should be as fit as possible.
|
|
|
|
|
|
|
|
// The buffer we receive MUST be dense (i.e not compressed, and channels allocated).
|
|
|
|
// That means we can use raw pointers to voxel data inside instead of using the higher-level getters,
|
|
|
|
// and then save a lot of time.
|
|
|
|
|
|
|
|
uint8_t *type_buffer = buffer.get_channel_raw(Voxel::CHANNEL_TYPE);
|
2018-12-14 01:22:03 +01:00
|
|
|
// _
|
|
|
|
// | \
|
|
|
|
// /\ \\
|
|
|
|
// / /|\\\
|
|
|
|
// | |\ \\\
|
|
|
|
// | \_\ \\|
|
|
|
|
// | | )
|
|
|
|
// \ | |
|
|
|
|
// \ /
|
|
|
|
CRASH_COND(type_buffer == NULL);
|
|
|
|
|
|
|
|
|
2018-09-30 02:55:09 +02:00
|
|
|
//CRASH_COND(memarr_len(type_buffer) != buffer.get_volume() * sizeof(uint8_t));
|
|
|
|
|
|
|
|
// Build lookup tables so to speed up voxel access.
|
|
|
|
// These are values to add to an address in order to get given neighbor.
|
|
|
|
|
|
|
|
int row_size = buffer.get_size().y;
|
|
|
|
int deck_size = buffer.get_size().x * row_size;
|
|
|
|
|
2018-09-30 20:37:39 +02:00
|
|
|
int side_neighbor_lut[Cube::SIDE_COUNT];
|
2018-10-06 00:51:55 +02:00
|
|
|
side_neighbor_lut[Cube::SIDE_LEFT] = row_size;
|
|
|
|
side_neighbor_lut[Cube::SIDE_RIGHT] = -row_size;
|
2018-09-30 20:37:39 +02:00
|
|
|
side_neighbor_lut[Cube::SIDE_BACK] = -deck_size;
|
|
|
|
side_neighbor_lut[Cube::SIDE_FRONT] = deck_size;
|
|
|
|
side_neighbor_lut[Cube::SIDE_BOTTOM] = -1;
|
|
|
|
side_neighbor_lut[Cube::SIDE_TOP] = 1;
|
|
|
|
|
|
|
|
int edge_neighbor_lut[Cube::EDGE_COUNT];
|
|
|
|
edge_neighbor_lut[Cube::EDGE_BOTTOM_BACK] = side_neighbor_lut[Cube::SIDE_BOTTOM] + side_neighbor_lut[Cube::SIDE_BACK];
|
|
|
|
edge_neighbor_lut[Cube::EDGE_BOTTOM_FRONT] = side_neighbor_lut[Cube::SIDE_BOTTOM] + side_neighbor_lut[Cube::SIDE_FRONT];
|
|
|
|
edge_neighbor_lut[Cube::EDGE_BOTTOM_LEFT] = side_neighbor_lut[Cube::SIDE_BOTTOM] + side_neighbor_lut[Cube::SIDE_LEFT];
|
|
|
|
edge_neighbor_lut[Cube::EDGE_BOTTOM_RIGHT] = side_neighbor_lut[Cube::SIDE_BOTTOM] + side_neighbor_lut[Cube::SIDE_RIGHT];
|
|
|
|
edge_neighbor_lut[Cube::EDGE_BACK_LEFT] = side_neighbor_lut[Cube::SIDE_BACK] + side_neighbor_lut[Cube::SIDE_LEFT];
|
|
|
|
edge_neighbor_lut[Cube::EDGE_BACK_RIGHT] = side_neighbor_lut[Cube::SIDE_BACK] + side_neighbor_lut[Cube::SIDE_RIGHT];
|
|
|
|
edge_neighbor_lut[Cube::EDGE_FRONT_LEFT] = side_neighbor_lut[Cube::SIDE_FRONT] + side_neighbor_lut[Cube::SIDE_LEFT];
|
|
|
|
edge_neighbor_lut[Cube::EDGE_FRONT_RIGHT] = side_neighbor_lut[Cube::SIDE_FRONT] + side_neighbor_lut[Cube::SIDE_RIGHT];
|
|
|
|
edge_neighbor_lut[Cube::EDGE_TOP_BACK] = side_neighbor_lut[Cube::SIDE_TOP] + side_neighbor_lut[Cube::SIDE_BACK];
|
|
|
|
edge_neighbor_lut[Cube::EDGE_TOP_FRONT] = side_neighbor_lut[Cube::SIDE_TOP] + side_neighbor_lut[Cube::SIDE_FRONT];
|
|
|
|
edge_neighbor_lut[Cube::EDGE_TOP_LEFT] = side_neighbor_lut[Cube::SIDE_TOP] + side_neighbor_lut[Cube::SIDE_LEFT];
|
|
|
|
edge_neighbor_lut[Cube::EDGE_TOP_RIGHT] = side_neighbor_lut[Cube::SIDE_TOP] + side_neighbor_lut[Cube::SIDE_RIGHT];
|
|
|
|
|
|
|
|
int corner_neighbor_lut[Cube::CORNER_COUNT];
|
|
|
|
corner_neighbor_lut[Cube::CORNER_BOTTOM_BACK_LEFT] = side_neighbor_lut[Cube::SIDE_BOTTOM] + side_neighbor_lut[Cube::SIDE_BACK] + side_neighbor_lut[Cube::SIDE_LEFT];
|
|
|
|
corner_neighbor_lut[Cube::CORNER_BOTTOM_BACK_RIGHT] = side_neighbor_lut[Cube::SIDE_BOTTOM] + side_neighbor_lut[Cube::SIDE_BACK] + side_neighbor_lut[Cube::SIDE_RIGHT];
|
|
|
|
corner_neighbor_lut[Cube::CORNER_BOTTOM_FRONT_RIGHT] = side_neighbor_lut[Cube::SIDE_BOTTOM] + side_neighbor_lut[Cube::SIDE_FRONT] + side_neighbor_lut[Cube::SIDE_RIGHT];
|
|
|
|
corner_neighbor_lut[Cube::CORNER_BOTTOM_FRONT_LEFT] = side_neighbor_lut[Cube::SIDE_BOTTOM] + side_neighbor_lut[Cube::SIDE_FRONT] + side_neighbor_lut[Cube::SIDE_LEFT];
|
|
|
|
corner_neighbor_lut[Cube::CORNER_TOP_BACK_LEFT] = side_neighbor_lut[Cube::SIDE_TOP] + side_neighbor_lut[Cube::SIDE_BACK] + side_neighbor_lut[Cube::SIDE_LEFT];
|
|
|
|
corner_neighbor_lut[Cube::CORNER_TOP_BACK_RIGHT] = side_neighbor_lut[Cube::SIDE_TOP] + side_neighbor_lut[Cube::SIDE_BACK] + side_neighbor_lut[Cube::SIDE_RIGHT];
|
|
|
|
corner_neighbor_lut[Cube::CORNER_TOP_FRONT_RIGHT] = side_neighbor_lut[Cube::SIDE_TOP] + side_neighbor_lut[Cube::SIDE_FRONT] + side_neighbor_lut[Cube::SIDE_RIGHT];
|
|
|
|
corner_neighbor_lut[Cube::CORNER_TOP_FRONT_LEFT] = side_neighbor_lut[Cube::SIDE_TOP] + side_neighbor_lut[Cube::SIDE_FRONT] + side_neighbor_lut[Cube::SIDE_LEFT];
|
2018-09-30 02:55:09 +02:00
|
|
|
|
2018-09-29 23:41:52 +02:00
|
|
|
uint64_t time_prep = OS::get_singleton()->get_ticks_usec() - time_before;
|
|
|
|
time_before = OS::get_singleton()->get_ticks_usec();
|
|
|
|
|
2017-04-06 23:44:11 +02:00
|
|
|
for (unsigned int z = min.z; z < max.z; ++z) {
|
|
|
|
for (unsigned int x = min.x; x < max.x; ++x) {
|
|
|
|
for (unsigned int y = min.y; y < max.y; ++y) {
|
2018-09-30 02:55:09 +02:00
|
|
|
// min and max are chosen such that you can visit 1 neighbor away from the current voxel without size check
|
2016-05-01 15:00:02 +02:00
|
|
|
|
2017-08-19 21:10:31 +02:00
|
|
|
// TODO In this intensive routine, there is a way to make voxel access fastest by getting a pointer to the channel,
|
|
|
|
// and using offset lookup to get neighbors rather than going through get_voxel validations
|
2018-09-30 02:55:09 +02:00
|
|
|
int voxel_index = y + x * row_size + z * deck_size;
|
|
|
|
int voxel_id = type_buffer[voxel_index];
|
2016-05-01 15:00:02 +02:00
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
if (voxel_id != 0 && library.has_voxel(voxel_id)) {
|
2016-05-01 15:00:02 +02:00
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
const Voxel &voxel = library.get_voxel_const(voxel_id);
|
2016-05-01 15:00:02 +02:00
|
|
|
|
2017-08-19 21:10:31 +02:00
|
|
|
Arrays &arrays = _arrays[voxel.get_material_id()];
|
2016-05-01 15:00:02 +02:00
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
// Hybrid approach: extract cube faces and decimate those that aren't visible,
|
|
|
|
// and still allow voxels to have geometry that is not a cube
|
2016-05-01 15:00:02 +02:00
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
// Sides
|
2018-09-30 20:37:39 +02:00
|
|
|
for (unsigned int side = 0; side < Cube::SIDE_COUNT; ++side) {
|
2016-05-01 15:00:02 +02:00
|
|
|
|
2017-08-19 21:10:31 +02:00
|
|
|
const PoolVector<Vector3> &positions = voxel.get_model_side_positions(side);
|
|
|
|
int vertex_count = positions.size();
|
2016-05-01 15:00:02 +02:00
|
|
|
|
2017-08-19 21:10:31 +02:00
|
|
|
if (vertex_count != 0) {
|
|
|
|
|
2018-09-30 02:55:09 +02:00
|
|
|
int neighbor_voxel_id = type_buffer[voxel_index + side_neighbor_lut[side]];
|
2016-05-01 15:00:02 +02:00
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
// TODO Better face visibility test
|
|
|
|
if (is_face_visible(library, voxel, neighbor_voxel_id)) {
|
|
|
|
|
|
|
|
// The face is visible
|
|
|
|
|
|
|
|
int shaded_corner[8] = { 0 };
|
|
|
|
|
|
|
|
if (_bake_occlusion) {
|
|
|
|
|
|
|
|
// Combinatory solution for https://0fps.net/2013/07/03/ambient-occlusion-for-minecraft-like-worlds/
|
|
|
|
|
|
|
|
for (unsigned int j = 0; j < 4; ++j) {
|
2018-09-30 20:37:39 +02:00
|
|
|
unsigned int edge = Cube::g_side_edges[side][j];
|
2018-09-30 02:55:09 +02:00
|
|
|
int edge_neighbor_id = type_buffer[voxel_index + edge_neighbor_lut[edge]];
|
|
|
|
if (!is_transparent(library, edge_neighbor_id)) {
|
2018-09-30 20:37:39 +02:00
|
|
|
shaded_corner[Cube::g_edge_corners[edge][0]] += 1;
|
|
|
|
shaded_corner[Cube::g_edge_corners[edge][1]] += 1;
|
2017-08-13 01:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (unsigned int j = 0; j < 4; ++j) {
|
2018-09-30 20:37:39 +02:00
|
|
|
unsigned int corner = Cube::g_side_corners[side][j];
|
2017-08-13 01:19:39 +02:00
|
|
|
if (shaded_corner[corner] == 2) {
|
|
|
|
shaded_corner[corner] = 3;
|
|
|
|
} else {
|
2018-09-30 02:55:09 +02:00
|
|
|
int corner_neigbor_id = type_buffer[voxel_index + corner_neighbor_lut[corner]];
|
|
|
|
if (!is_transparent(library, corner_neigbor_id)) {
|
2017-08-13 01:19:39 +02:00
|
|
|
shaded_corner[corner] += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-04 14:52:23 +02:00
|
|
|
|
2017-08-19 21:10:31 +02:00
|
|
|
PoolVector<Vector3>::Read rv = positions.read();
|
2017-03-25 01:23:36 +01:00
|
|
|
PoolVector<Vector2>::Read rt = voxel.get_model_side_uv(side).read();
|
2017-08-19 21:10:31 +02:00
|
|
|
|
2018-09-30 02:55:09 +02:00
|
|
|
// Subtracting 1 because the data is padded
|
2017-08-13 01:19:39 +02:00
|
|
|
Vector3 pos(x - 1, y - 1, z - 1);
|
|
|
|
|
2018-09-30 02:55:09 +02:00
|
|
|
// Append vertices of the faces in one go, don't use push_back
|
|
|
|
|
|
|
|
{
|
|
|
|
int append_index = arrays.positions.size();
|
|
|
|
arrays.positions.resize(arrays.positions.size() + vertex_count);
|
|
|
|
Vector3 *w = arrays.positions.ptrw() + append_index;
|
|
|
|
for (unsigned int i = 0; i < vertex_count; ++i) {
|
|
|
|
w[i] = rv[i] + pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int append_index = arrays.uvs.size();
|
|
|
|
arrays.uvs.resize(arrays.uvs.size() + vertex_count);
|
|
|
|
memcpy(arrays.uvs.ptrw() + append_index, rt.ptr(), vertex_count * sizeof(Vector2));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int append_index = arrays.normals.size();
|
|
|
|
arrays.normals.resize(arrays.normals.size() + vertex_count);
|
|
|
|
Vector3 *w = arrays.normals.ptrw() + append_index;
|
|
|
|
for (unsigned int i = 0; i < vertex_count; ++i) {
|
2018-09-30 20:37:39 +02:00
|
|
|
w[i] = Cube::g_side_normals[side].to_vec3();
|
2018-09-30 02:55:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_bake_occlusion) {
|
|
|
|
// Use color array
|
|
|
|
|
|
|
|
int append_index = arrays.colors.size();
|
|
|
|
arrays.colors.resize(arrays.colors.size() + vertex_count);
|
|
|
|
Color *w = arrays.colors.ptrw() + append_index;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < vertex_count; ++i) {
|
|
|
|
Vector3 v = rv[i];
|
2017-08-13 01:19:39 +02:00
|
|
|
|
|
|
|
// General purpose occlusion colouring.
|
|
|
|
// TODO Optimize for cubes
|
2018-09-30 02:55:09 +02:00
|
|
|
// TODO Fix occlusion inconsistency caused by triangles orientation? Not sure if worth it
|
2017-08-13 01:19:39 +02:00
|
|
|
float shade = 0;
|
|
|
|
for (unsigned int j = 0; j < 4; ++j) {
|
2018-09-30 20:37:39 +02:00
|
|
|
unsigned int corner = Cube::g_side_corners[side][j];
|
2017-08-13 01:19:39 +02:00
|
|
|
if (shaded_corner[corner]) {
|
|
|
|
float s = baked_occlusion_darkness * static_cast<float>(shaded_corner[corner]);
|
2018-09-30 20:37:39 +02:00
|
|
|
float k = 1.0 - Cube::g_corner_position[corner].distance_to(v);
|
2017-08-13 01:19:39 +02:00
|
|
|
if (k < 0.0)
|
|
|
|
k = 0.0;
|
|
|
|
s *= k;
|
|
|
|
if (s > shade)
|
|
|
|
shade = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
float gs = 1.0 - shade;
|
2018-09-30 02:55:09 +02:00
|
|
|
w[i] = Color(gs, gs, gs);
|
2017-08-13 01:19:39 +02:00
|
|
|
}
|
2017-08-19 21:10:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const PoolVector<int> &side_indices = voxel.get_model_side_indices(side);
|
|
|
|
PoolVector<int>::Read ri = side_indices.read();
|
|
|
|
unsigned int index_count = side_indices.size();
|
|
|
|
|
2018-09-30 02:55:09 +02:00
|
|
|
{
|
|
|
|
int i = arrays.indices.size();
|
|
|
|
arrays.indices.resize(arrays.indices.size() + index_count);
|
|
|
|
int *w = arrays.indices.ptrw();
|
|
|
|
for(unsigned int j = 0; j < index_count; ++j) {
|
|
|
|
w[i++] = index_offset + ri[j];
|
|
|
|
}
|
2017-08-13 01:19:39 +02:00
|
|
|
}
|
2017-08-19 21:10:31 +02:00
|
|
|
|
|
|
|
index_offset += vertex_count;
|
2017-08-13 01:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inside
|
2017-08-19 21:10:31 +02:00
|
|
|
if (voxel.get_model_positions().size() != 0) {
|
2018-09-30 02:55:09 +02:00
|
|
|
// TODO Get rid of push_backs
|
2017-08-13 01:19:39 +02:00
|
|
|
|
2017-08-19 21:10:31 +02:00
|
|
|
const PoolVector<Vector3> &vertices = voxel.get_model_positions();
|
|
|
|
int vertex_count = vertices.size();
|
|
|
|
|
|
|
|
PoolVector<Vector3>::Read rv = vertices.read();
|
2017-03-25 01:23:36 +01:00
|
|
|
PoolVector<Vector3>::Read rn = voxel.get_model_normals().read();
|
|
|
|
PoolVector<Vector2>::Read rt = voxel.get_model_uv().read();
|
2017-08-19 21:10:31 +02:00
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
Vector3 pos(x - 1, y - 1, z - 1);
|
|
|
|
|
2017-08-19 21:10:31 +02:00
|
|
|
for (unsigned int i = 0; i < vertex_count; ++i) {
|
|
|
|
arrays.normals.push_back(rn[i]);
|
|
|
|
arrays.uvs.push_back(rt[i]);
|
|
|
|
arrays.positions.push_back(rv[i] + pos);
|
2017-08-13 01:19:39 +02:00
|
|
|
}
|
2017-08-19 21:10:31 +02:00
|
|
|
|
|
|
|
if(_bake_occlusion) {
|
|
|
|
// TODO handle ambient occlusion on inner parts
|
|
|
|
arrays.colors.push_back(Color(1,1,1));
|
|
|
|
}
|
|
|
|
|
|
|
|
const PoolVector<int> &indices = voxel.get_model_indices();
|
|
|
|
PoolVector<int>::Read ri = indices.read();
|
|
|
|
unsigned int index_count = indices.size();
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < index_count; ++i) {
|
|
|
|
arrays.indices.push_back(index_offset + ri[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
index_offset += vertex_count;
|
2017-08-13 01:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-01 15:00:02 +02:00
|
|
|
|
2018-09-29 23:41:52 +02:00
|
|
|
uint64_t time_meshing = OS::get_singleton()->get_ticks_usec() - time_before;
|
|
|
|
time_before = OS::get_singleton()->get_ticks_usec();
|
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
// Commit mesh
|
2017-04-06 23:44:11 +02:00
|
|
|
|
2017-08-19 21:10:31 +02:00
|
|
|
// print_line(String("Made mesh v: ") + String::num(_arrays[0].positions.size())
|
|
|
|
// + String(", i: ") + String::num(_arrays[0].indices.size()));
|
2017-08-13 01:19:39 +02:00
|
|
|
|
2018-09-25 01:54:07 +02:00
|
|
|
Array surfaces;
|
|
|
|
|
2018-09-30 02:55:09 +02:00
|
|
|
// TODO We could return a single byte array and use Mesh::add_surface down the line?
|
|
|
|
|
2018-09-25 01:54:07 +02:00
|
|
|
for (int i = 0; i < MAX_MATERIALS; ++i) {
|
2017-04-01 20:10:38 +02:00
|
|
|
|
2017-08-19 21:10:31 +02:00
|
|
|
const Arrays &arrays = _arrays[i];
|
2018-09-25 01:54:07 +02:00
|
|
|
if (arrays.positions.size() != 0) {
|
2016-05-10 04:42:30 +02:00
|
|
|
|
2018-10-06 00:51:55 +02:00
|
|
|
/*print_line("Arrays:");
|
|
|
|
for(int i = 0; i < arrays.positions.size(); ++i)
|
|
|
|
print_line(String(" P {0}").format(varray(arrays.positions[i])));
|
|
|
|
for(int i = 0; i < arrays.normals.size(); ++i)
|
|
|
|
print_line(String(" N {0}").format(varray(arrays.normals[i])));
|
|
|
|
for(int i = 0; i < arrays.uvs.size(); ++i)
|
|
|
|
print_line(String(" UV {0}").format(varray(arrays.uvs[i])));*/
|
|
|
|
|
2017-08-19 21:10:31 +02:00
|
|
|
Array mesh_arrays;
|
|
|
|
mesh_arrays.resize(Mesh::ARRAY_MAX);
|
|
|
|
|
|
|
|
{
|
|
|
|
PoolVector<Vector3> positions;
|
|
|
|
PoolVector<Vector2> uvs;
|
|
|
|
PoolVector<Vector3> normals;
|
|
|
|
PoolVector<Color> colors;
|
|
|
|
PoolVector<int> indices;
|
|
|
|
|
2018-09-29 23:41:52 +02:00
|
|
|
raw_copy_to(positions, arrays.positions);
|
|
|
|
raw_copy_to(uvs, arrays.uvs);
|
|
|
|
raw_copy_to(normals, arrays.normals);
|
|
|
|
raw_copy_to(colors, arrays.colors);
|
|
|
|
raw_copy_to(indices, arrays.indices);
|
2017-08-19 21:10:31 +02:00
|
|
|
|
|
|
|
mesh_arrays[Mesh::ARRAY_VERTEX] = positions;
|
|
|
|
mesh_arrays[Mesh::ARRAY_TEX_UV] = uvs;
|
|
|
|
mesh_arrays[Mesh::ARRAY_NORMAL] = normals;
|
|
|
|
mesh_arrays[Mesh::ARRAY_COLOR] = colors;
|
|
|
|
mesh_arrays[Mesh::ARRAY_INDEX] = indices;
|
|
|
|
}
|
|
|
|
|
2018-09-25 01:54:07 +02:00
|
|
|
surfaces.append(mesh_arrays);
|
2017-08-19 21:10:31 +02:00
|
|
|
}
|
2017-08-13 01:19:39 +02:00
|
|
|
}
|
2016-05-01 15:00:02 +02:00
|
|
|
|
2018-09-29 23:41:52 +02:00
|
|
|
uint64_t time_commit = OS::get_singleton()->get_ticks_usec() - time_before;
|
|
|
|
|
|
|
|
//print_line(String("P: {0}, M: {1}, C: {2}").format(varray(time_prep, time_meshing, time_commit)));
|
|
|
|
|
2018-09-25 01:54:07 +02:00
|
|
|
return surfaces;
|
2016-05-01 15:00:02 +02:00
|
|
|
}
|
|
|
|
|
2016-05-08 01:09:00 +02:00
|
|
|
void VoxelMesher::_bind_methods() {
|
2016-05-01 15:00:02 +02:00
|
|
|
|
2017-08-13 00:08:53 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_library", "voxel_library"), &VoxelMesher::set_library);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_library"), &VoxelMesher::get_library);
|
2017-01-01 05:23:22 +01:00
|
|
|
|
2017-03-25 01:23:36 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_occlusion_enabled", "enable"), &VoxelMesher::set_occlusion_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_occlusion_enabled"), &VoxelMesher::get_occlusion_enabled);
|
2017-01-01 05:23:22 +01:00
|
|
|
|
2017-03-25 01:23:36 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_occlusion_darkness", "value"), &VoxelMesher::set_occlusion_darkness);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_occlusion_darkness"), &VoxelMesher::get_occlusion_darkness);
|
2017-01-01 05:23:22 +01:00
|
|
|
|
2018-09-25 01:54:07 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("build_mesh", "voxel_buffer", "channel", "materials", "existing_mesh"), &VoxelMesher::build_mesh);
|
2016-05-01 15:00:02 +02:00
|
|
|
|
2017-04-01 20:10:38 +02:00
|
|
|
#ifdef VOXEL_PROFILING
|
|
|
|
ClassDB::bind_method(D_METHOD("get_profiling_info"), &VoxelMesher::get_profiling_info);
|
|
|
|
#endif
|
2016-05-01 15:00:02 +02:00
|
|
|
}
|