Fix face culling issue with transparent voxels of different IDs

master
Marc Gilleron 2020-07-30 18:00:12 +01:00
parent 905dbee563
commit 1310112f25
3 changed files with 13 additions and 8 deletions

View File

@ -132,6 +132,8 @@ void Voxel::clear_geometry() {
_model_side_uvs[side].clear();
_model_side_indices[side].clear();
}
_empty = true;
}
void Voxel::set_geometry_type(GeometryType type) {
@ -214,6 +216,8 @@ void Voxel::set_custom_mesh_from_arrays(Array arrays) {
PoolVector3Array normals = arrays[Mesh::ARRAY_NORMAL];
PoolVector2Array uvs = arrays[Mesh::ARRAY_TEX_UV];
_empty = positions.size() == 0;
ERR_FAIL_COND(normals.size() == 0);
struct L {
@ -349,6 +353,7 @@ Ref<Voxel> Voxel::set_cube_geometry(float sy) {
_collision_aabbs.clear();
_collision_aabbs.push_back(AABB(Vector3(0, 0, 0), Vector3(1, 1, 1)));
_empty = false;
return Ref<Voxel>(this);
}
@ -464,6 +469,8 @@ void Voxel::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_collision_aabbs", "aabbs"), &Voxel::_b_set_collision_aabbs);
ClassDB::bind_method(D_METHOD("get_collision_aabbs"), &Voxel::_b_get_collision_aabbs);
ClassDB::bind_method(D_METHOD("is_empty()"), &Voxel::is_empty);
// TODO Update to StringName in Godot 4
ADD_PROPERTY(PropertyInfo(Variant::STRING, "voxel_name"), "set_voxel_name", "get_voxel_name");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");

View File

@ -7,7 +7,7 @@
class VoxelLibrary;
// TODO Rename VoxelBlockyType?
// TODO Rename VoxelModel?
// Definition of one type of voxel.
// A voxel can be a simple coloured cube, or a more complex model.
// Important: it is recommended that you create voxels from a library rather than using new().
@ -63,6 +63,8 @@ public:
void set_geometry_type(GeometryType type);
GeometryType get_geometry_type() const;
inline bool is_empty() const { return _empty; }
Ref<Resource> duplicate(bool p_subresources) const override;
//------------------------------------------
@ -125,6 +127,7 @@ private:
std::vector<AABB> _collision_aabbs;
bool _contributes_to_ao = false;
bool _random_tickable = false;
bool _empty = true;
FixedArray<uint32_t, Cube::SIDE_COUNT> _side_pattern_index;

View File

@ -25,7 +25,8 @@ const int g_opposite_side[6] = {
inline bool is_face_visible(const VoxelLibrary &lib, const Voxel &vt, int other_voxel_id, int side) {
if (lib.has_voxel(other_voxel_id)) {
const Voxel &other_vt = lib.get_voxel_const(other_voxel_id);
if (other_vt.is_transparent() && vt.get_id() != other_voxel_id) {
// TODO Might test against material somehow, but not only
if (other_vt.is_empty() || (other_vt.is_transparent() && !vt.is_transparent())) {
return true;
} else {
const unsigned int ai = vt.get_side_pattern_index(side);
@ -143,7 +144,6 @@ static void generate_blocky_mesh(
const int voxel_id = type_buffer[voxel_index];
if (voxel_id != 0 && library.has_voxel(voxel_id)) {
const Voxel &voxel = library.get_voxel_const(voxel_id);
VoxelMesherBlocky::Arrays &arrays = out_arrays_per_material[voxel.get_material_id()];
@ -154,7 +154,6 @@ static void generate_blocky_mesh(
// Sides
for (unsigned int side = 0; side < Cube::SIDE_COUNT; ++side) {
const std::vector<Vector3> &side_positions = voxel.get_model_side_positions(side);
const unsigned int vertex_count = side_positions.size();
@ -173,7 +172,6 @@ static void generate_blocky_mesh(
int shaded_corner[8] = { 0 };
if (bake_occlusion) {
// Combinatory solution for https://0fps.net/2013/07/03/ambient-occlusion-for-minecraft-like-worlds/
// (inverted)
// function vertexAO(side1, side2, corner) {
@ -350,7 +348,6 @@ void VoxelMesherBlocky::set_occlusion_enabled(bool enable) {
}
void VoxelMesherBlocky::build(VoxelMesher::Output &output, const VoxelMesher::Input &input) {
const int channel = VoxelBuffer::CHANNEL_TYPE;
ERR_FAIL_COND(_library.is_null());
@ -446,7 +443,6 @@ void VoxelMesherBlocky::build(VoxelMesher::Output &output, const VoxelMesher::In
// TODO We could return a single byte array and use Mesh::add_surface down the line?
for (unsigned int i = 0; i < MAX_MATERIALS; ++i) {
const Arrays &arrays = _arrays_per_material[i];
if (arrays.positions.size() != 0) {
@ -493,7 +489,6 @@ VoxelMesher *VoxelMesherBlocky::clone() {
}
void VoxelMesherBlocky::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_library", "voxel_library"), &VoxelMesherBlocky::set_library);
ClassDB::bind_method(D_METHOD("get_library"), &VoxelMesherBlocky::get_library);