Skip to content

Commit

Permalink
Revert avoiding air voxels contribution to texturing in Transvoxel
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed Aug 27, 2024
1 parent 9d3e396 commit 5dcc1d8
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 29 deletions.
1 change: 1 addition & 0 deletions doc/source/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Primarily developped with Godot 4.3.
- Fixes
- `VoxelInstanceLibrary`: Editor: reworked the way items are exposed as a Blender-style list. Now removing an item while the library is open as a sub-inspector is no longer problematic
- `VoxelGeneratorGraph`: fix wrong values when using `OutputWeight` with optimized execution map enabled, when weights are determined to be locally constant
- `VoxelMesherTransvoxel`: revert texturing logic that attempted to prevent air voxels from contributing, but was lowering quality. It is now optional as an experimental property.

- Breaking changes
- `VoxelInstanceLibrary`: Items should no longer be accessed using generated properties (`item1`, `item2` etc). Use `get_item` instead.
Expand Down
64 changes: 41 additions & 23 deletions meshers/transvoxel/transvoxel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ struct CellTextureDatas {
template <unsigned int NVoxels, typename WeightSampler_T>
CellTextureDatas<NVoxels> select_textures_4_per_voxel(
const FixedArray<unsigned int, NVoxels> &voxel_indices,
Span<const uint16_t> indices_data,
const Span<const uint16_t> indices_data,
const WeightSampler_T &weights_sampler,
unsigned int case_code
const unsigned int case_code
) {
// TODO Optimization: this function takes almost half of the time when polygonizing non-empty cells.
// I wonder how it can be optimized further?
Expand Down Expand Up @@ -284,7 +284,8 @@ inline void get_cell_texture_data(
const TextureIndicesData &texture_indices_data,
const FixedArray<unsigned int, NVoxels> &voxel_indices,
const WeightSampler_T &weights_data,
unsigned int case_code
// Used for rejecting air voxels. Can be set to 0 so all corners are always used.
const unsigned int case_code
) {
if (texture_indices_data.buffer.size() == 0) {
// Indices are known for the whole block, just read weights directly
Expand All @@ -293,6 +294,9 @@ inline void get_cell_texture_data(
for (unsigned int ci = 0; ci < voxel_indices.size(); ++ci) {
if ((case_code & (1 << ci)) != 0) {
// Force air voxels to not contribute
// TODO This is not great, because every Transvoxel vertex interpolates between a matter and air corner.
// This approach means we would always interpolate towards 0 as a result.
// Maybe we'll have to use a different approach and remove this option in the future.
fill(cell_textures.weights[ci], uint8_t(0));
} else {
const unsigned int wi = voxel_indices[ci];
Expand Down Expand Up @@ -366,7 +370,8 @@ void build_regular_mesh(
MeshArrays &output,
const IDeepSDFSampler *deep_sdf_sampler,
StdVector<CellInfo> *cell_info,
const float edge_clamp_margin
const float edge_clamp_margin,
const bool textures_skip_air_voxels
) {
ZN_PROFILE_SCOPE();

Expand Down Expand Up @@ -492,7 +497,11 @@ void build_regular_mesh(
CellTextureDatas<8> cell_textures;
if (texturing_mode == TEXTURES_BLEND_4_OVER_16) {
get_cell_texture_data(
cell_textures, texture_indices_data, corner_data_indices, weights_sampler, case_code
cell_textures,
texture_indices_data,
corner_data_indices,
weights_sampler,
textures_skip_air_voxels ? case_code : 0
);
current_reuse_cell.packed_texture_indices = cell_textures.packed_indices;
}
Expand Down Expand Up @@ -919,7 +928,8 @@ void build_transition_mesh(
TexturingMode texturing_mode,
Cache &cache,
MeshArrays &output,
const float edge_clamp_margin
const float edge_clamp_margin,
const bool textures_skip_air_voxels
) {
// From this point, we expect the buffer to contain allocated data.
// This function has some comments as quotes from the Transvoxel paper.
Expand Down Expand Up @@ -1076,15 +1086,15 @@ void build_transition_mesh(
CellTextureDatas<13> cell_textures;
if (texturing_mode == TEXTURES_BLEND_4_OVER_16) {
// Re-making an ordered case code...
// |436785210|
const uint16_t alt_case_code = 0 //
| ((case_code & 0b000000111)) // 210
| ((case_code & 0b110000000) >> 4) // 43
| ((case_code & 0b000001000) << 2) // 5
| ((case_code & 0b001000000)) // 6
| ((case_code & 0b000100000) << 2) // 7
| ((case_code & 0b000010000) << 4) // 8
;
// |436785210|
const uint16_t alt_case_code = textures_skip_air_voxels ? 0 //
| ((case_code & 0b000000111)) // 210
| ((case_code & 0b110000000) >> 4) // 43
| ((case_code & 0b000001000) << 2) // 5
| ((case_code & 0b001000000)) // 6
| ((case_code & 0b000100000) << 2) // 7
| ((case_code & 0b000010000) << 4) // 8
: 0;

// uint16_t alt_case_code = sign_f(cell_samples[0]);
// alt_case_code |= (sign_f(cell_samples[1]) << 1);
Expand Down Expand Up @@ -1493,7 +1503,8 @@ DefaultTextureIndicesData build_regular_mesh(
MeshArrays &output,
const IDeepSDFSampler *deep_sdf_sampler,
StdVector<CellInfo> *cell_infos,
const float edge_clamp_margin
const float edge_clamp_margin,
const bool textures_ignore_air_voxels
) {
ZN_PROFILE_SCOPE();
// From this point, we expect the buffer to contain allocated data in the relevant channels.
Expand Down Expand Up @@ -1550,7 +1561,8 @@ DefaultTextureIndicesData build_regular_mesh(
output,
deep_sdf_sampler,
cell_infos,
edge_clamp_margin
edge_clamp_margin,
textures_ignore_air_voxels
);
} break;

Expand All @@ -1567,7 +1579,8 @@ DefaultTextureIndicesData build_regular_mesh(
output,
deep_sdf_sampler,
cell_infos,
edge_clamp_margin
edge_clamp_margin,
textures_ignore_air_voxels
);
} break;

Expand All @@ -1587,7 +1600,8 @@ DefaultTextureIndicesData build_regular_mesh(
output,
deep_sdf_sampler,
cell_infos,
edge_clamp_margin
edge_clamp_margin,
textures_ignore_air_voxels
);
} break;

Expand All @@ -1613,7 +1627,8 @@ void build_transition_mesh(
Cache &cache,
MeshArrays &output,
DefaultTextureIndicesData default_texture_indices_data,
const float edge_clamp_margin
const float edge_clamp_margin,
const bool textures_ignore_air_voxels
) {
ZN_PROFILE_SCOPE();
// From this point, we expect the buffer to contain allocated data in the relevant channels.
Expand Down Expand Up @@ -1678,7 +1693,8 @@ void build_transition_mesh(
texturing_mode,
cache,
output,
edge_clamp_margin
edge_clamp_margin,
textures_ignore_air_voxels
);
} break;

Expand All @@ -1694,7 +1710,8 @@ void build_transition_mesh(
texturing_mode,
cache,
output,
edge_clamp_margin
edge_clamp_margin,
textures_ignore_air_voxels
);
} break;

Expand All @@ -1710,7 +1727,8 @@ void build_transition_mesh(
texturing_mode,
cache,
output,
edge_clamp_margin
edge_clamp_margin,
textures_ignore_air_voxels
);
} break;

Expand Down
6 changes: 4 additions & 2 deletions meshers/transvoxel/transvoxel.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ DefaultTextureIndicesData build_regular_mesh(
MeshArrays &output,
const IDeepSDFSampler *deep_sdf_sampler,
StdVector<CellInfo> *cell_infos,
const float edge_clamp_margin
const float edge_clamp_margin,
const bool textures_ignore_air_voxels
);

void build_transition_mesh(
Expand All @@ -185,7 +186,8 @@ void build_transition_mesh(
Cache &cache,
MeshArrays &output,
DefaultTextureIndicesData default_texture_indices_data,
const float edge_clamp_margin
const float edge_clamp_margin,
const bool textures_ignore_air_voxels
);

} // namespace zylann::voxel::transvoxel
Expand Down
31 changes: 27 additions & 4 deletions meshers/transvoxel/voxel_mesher_transvoxel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ void VoxelMesherTransvoxel::build(VoxelMesher::Output &output, const VoxelMesher
mesh_arrays,
&ds,
cell_infos,
_edge_clamp_margin
_edge_clamp_margin,
_textures_ignore_air_voxels
);
} else {
default_texture_indices_data = transvoxel::build_regular_mesh(
Expand All @@ -302,7 +303,8 @@ void VoxelMesherTransvoxel::build(VoxelMesher::Output &output, const VoxelMesher
mesh_arrays,
nullptr,
cell_infos,
_edge_clamp_margin
_edge_clamp_margin,
_textures_ignore_air_voxels
);
}

Expand Down Expand Up @@ -350,7 +352,8 @@ void VoxelMesherTransvoxel::build(VoxelMesher::Output &output, const VoxelMesher
tls_cache,
*combined_mesh_arrays,
default_texture_indices_data,
_edge_clamp_margin
_edge_clamp_margin,
_textures_ignore_air_voxels
);
}
}
Expand Down Expand Up @@ -398,7 +401,8 @@ Ref<ArrayMesh> VoxelMesherTransvoxel::build_transition_mesh(Ref<godot::VoxelBuff
s_cache,
s_mesh_arrays,
default_texture_indices_data,
_edge_clamp_margin
_edge_clamp_margin,
_textures_ignore_air_voxels
);

Ref<ArrayMesh> mesh;
Expand All @@ -425,6 +429,14 @@ VoxelMesherTransvoxel::TexturingMode VoxelMesherTransvoxel::get_texturing_mode()
return _texture_mode;
}

void VoxelMesherTransvoxel::set_textures_ignore_air_voxels(const bool enable) {
_textures_ignore_air_voxels = enable;
}

bool VoxelMesherTransvoxel::get_textures_ignore_air_voxels() const {
return _textures_ignore_air_voxels;
}

void VoxelMesherTransvoxel::set_mesh_optimization_enabled(bool enabled) {
_mesh_optimization_params.enabled = enabled;
}
Expand Down Expand Up @@ -485,6 +497,9 @@ void VoxelMesherTransvoxel::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_texturing_mode", "mode"), &Self::set_texturing_mode);
ClassDB::bind_method(D_METHOD("get_texturing_mode"), &Self::get_texturing_mode);

ClassDB::bind_method(D_METHOD("set_textures_ignore_air_voxels", "enabled"), &Self::set_textures_ignore_air_voxels);
ClassDB::bind_method(D_METHOD("get_textures_ignore_air_voxels"), &Self::get_textures_ignore_air_voxels);

ClassDB::bind_method(D_METHOD("set_mesh_optimization_enabled", "enabled"), &Self::set_mesh_optimization_enabled);
ClassDB::bind_method(D_METHOD("is_mesh_optimization_enabled"), &Self::is_mesh_optimization_enabled);

Expand All @@ -509,12 +524,20 @@ void VoxelMesherTransvoxel::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_edge_clamp_margin"), &Self::get_edge_clamp_margin);
ClassDB::bind_method(D_METHOD("set_edge_clamp_margin", "margin"), &Self::set_edge_clamp_margin);

ADD_GROUP("Materials", "");

ADD_PROPERTY(
PropertyInfo(Variant::INT, "texturing_mode", PROPERTY_HINT_ENUM, "None,4-blend over 16 textures (4 bits)"),
"set_texturing_mode",
"get_texturing_mode"
);

ADD_PROPERTY(
PropertyInfo(Variant::BOOL, "textures_ignore_air_voxels"),
"set_textures_ignore_air_voxels",
"get_textures_ignore_air_voxels"
);

ADD_GROUP("Mesh optimization", "mesh_optimization_");

ADD_PROPERTY(
Expand Down
5 changes: 5 additions & 0 deletions meshers/transvoxel/voxel_mesher_transvoxel.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class VoxelMesherTransvoxel : public VoxelMesher {
void set_texturing_mode(TexturingMode mode);
TexturingMode get_texturing_mode() const;

void set_textures_ignore_air_voxels(const bool enable);
bool get_textures_ignore_air_voxels() const;

void set_mesh_optimization_enabled(bool enabled);
bool is_mesh_optimization_enabled() const;

Expand Down Expand Up @@ -103,6 +106,8 @@ class VoxelMesherTransvoxel : public VoxelMesher {
float _edge_clamp_margin = 0.02f;

bool _transitions_enabled = true;

bool _textures_ignore_air_voxels = false;
};

} // namespace zylann::voxel
Expand Down

0 comments on commit 5dcc1d8

Please sign in to comment.