BUILD_PHASE_TERRARIN_MESH now can also distribute it's calculations onto multiple frames.

This commit is contained in:
Relintai 2020-08-04 15:30:34 +02:00
parent 469927cc3b
commit 6af711322d
2 changed files with 228 additions and 83 deletions

View File

@ -1355,6 +1355,7 @@ void VoxelChunkDefault::_build_phase(int phase) {
return;
}
case BUILD_PHASE_TERRARIN_MESH: {
if (_job->should_do()) {
for (int i = 0; i < _meshers.size(); ++i) {
Ref<VoxelMesher> mesher = _meshers.get(i);
@ -1371,8 +1372,22 @@ void VoxelChunkDefault::_build_phase(int phase) {
mesher->set_library(_library);
}
if (_job->should_return())
return;
}
if ((_build_flags & VoxelChunkDefault::BUILD_FLAG_USE_LIGHTING) != 0) {
for (int i = 0; i < _meshers.size(); ++i) {
int starti = 0;
if (_job->has_meta("bptm_ulm")) {
starti = _job->get_meta("bptm_ulm");
}
for (int i = starti; i < _meshers.size(); ++i) {
if (_job->should_return()) {
_job->set_meta("bptm_ulm", i);
}
Ref<VoxelMesher> mesher = _meshers.get(i);
ERR_CONTINUE(!mesher.is_valid());
@ -1380,7 +1395,17 @@ void VoxelChunkDefault::_build_phase(int phase) {
mesher->bake_colors(this);
}
for (int i = 0; i < _liquid_meshers.size(); ++i) {
starti = 0;
if (_job->has_meta("bptm_ullm")) {
starti = _job->get_meta("bptm_ullm");
}
for (int i = starti; i < _liquid_meshers.size(); ++i) {
if (_job->should_return()) {
_job->set_meta("bptm_ullm", i);
}
Ref<VoxelMesher> mesher = _liquid_meshers.get(i);
ERR_CONTINUE(!mesher.is_valid());
@ -1389,8 +1414,18 @@ void VoxelChunkDefault::_build_phase(int phase) {
}
}
int starti = 0;
if (_job->has_meta("bptm_mm")) {
starti = _job->get_meta("bptm_mm");
}
Ref<VoxelMesher> mesher;
for (int i = 0; i < _meshers.size(); ++i) {
for (int i = starti; i < _meshers.size(); ++i) {
if (_job->should_return()) {
_job->set_meta("bptm_mm", i);
}
Ref<VoxelMesher> m = _meshers.get(i);
ERR_CONTINUE(!m.is_valid());
@ -1407,8 +1442,18 @@ void VoxelChunkDefault::_build_phase(int phase) {
ERR_FAIL_COND(!mesher.is_valid());
starti = 0;
if (_job->has_meta("bptm_lmm")) {
starti = _job->get_meta("bptm_lmm");
}
Ref<VoxelMesher> liquid_mesher;
for (int i = 0; i < _liquid_meshers.size(); ++i) {
for (int i = starti; i < _liquid_meshers.size(); ++i) {
if (_job->should_return()) {
_job->set_meta("bptm_lmm", i);
}
Ref<VoxelMesher> m = _liquid_meshers.get(i);
ERR_CONTINUE(!m.is_valid());
@ -1424,15 +1469,42 @@ void VoxelChunkDefault::_build_phase(int phase) {
}
if (mesher->get_vertex_count() == 0 && liquid_mesher.is_valid() && liquid_mesher->get_vertex_count() == 0) {
if (_job->has_meta("bptm_ulm")) {
_job->remove_meta("bptm_ulm");
}
if (_job->has_meta("bptm_ullm")) {
_job->remove_meta("bptm_ullm");
}
if (_job->has_meta("bptm_mm")) {
_job->remove_meta("bptm_mm");
}
if (_job->has_meta("bptm_lmm")) {
_job->remove_meta("bptm_lmm");
}
_job->reset_stages();
next_phase();
return;
}
if (mesher->get_vertex_count() != 0) {
if (_job->should_do()) {
temp_mesh_arr = mesher->build_mesh();
if (_job->should_return()) {
return;
}
}
RID mesh_rid = get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 0);
Array temp_mesh_arr = mesher->build_mesh();
if (_job->should_do()) {
if (mesh_rid == RID()) {
if ((_build_flags & BUILD_FLAG_CREATE_LODS) != 0)
create_meshes(MESH_INDEX_TERRARIN, _lod_num + 1);
@ -1449,12 +1521,24 @@ void VoxelChunkDefault::_build_phase(int phase) {
VS::get_singleton()->mesh_clear(mesh_rid);
#endif
if (_job->should_return()) {
return;
}
}
if (_job->should_do()) {
VS::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr);
if (_library->get_material(0).is_valid())
VS::get_singleton()->mesh_surface_set_material(mesh_rid, 0, _library->get_material(0)->get_rid());
if (_job->should_return()) {
return;
}
}
if ((_build_flags & BUILD_FLAG_CREATE_LODS) != 0) {
if (_job->should_do()) {
if (_lod_num >= 1) {
//for lod 1 just remove uv2
temp_mesh_arr[VisualServer::ARRAY_TEX_UV2] = Variant();
@ -1464,9 +1548,15 @@ void VoxelChunkDefault::_build_phase(int phase) {
if (get_library()->get_material(1).is_valid())
VisualServer::get_singleton()->mesh_surface_set_material(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 1), 0, get_library()->get_material(1)->get_rid());
}
if (_job->should_return()) {
return;
}
}
if (_job->should_do()) {
if (_lod_num >= 2) {
Array temp_mesh_arr2 = merge_mesh_array(temp_mesh_arr);
temp_mesh_arr = temp_mesh_arr2;
VisualServer::get_singleton()->mesh_add_surface_from_arrays(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 2), VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr2);
@ -1474,6 +1564,12 @@ void VoxelChunkDefault::_build_phase(int phase) {
VisualServer::get_singleton()->mesh_surface_set_material(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 2), 0, get_library()->get_material(2)->get_rid());
}
if (_job->should_return()) {
return;
}
}
if (_job->should_do()) {
if (_lod_num >= 3) {
Ref<ShaderMaterial> mat = get_library()->get_material(0);
Ref<SpatialMaterial> spmat = get_library()->get_material(0);
@ -1496,7 +1592,13 @@ void VoxelChunkDefault::_build_phase(int phase) {
}
}
if (_job->should_return()) {
return;
}
}
/*
if (_job->should_do()) {
if (_lod_num > 4) {
Ref<FastQuadraticMeshSimplifier> fqms;
fqms.instance();
@ -1517,15 +1619,27 @@ void VoxelChunkDefault::_build_phase(int phase) {
VisualServer::get_singleton()->mesh_surface_set_material(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, i), 0, get_library()->get_material(i)->get_rid());
}
}
if (_job->should_return()) {
return;
}
}
*/
}
}
if (liquid_mesher.is_valid() && liquid_mesher->get_vertex_count() != 0) {
if (_job->should_do()) {
temp_mesh_arr = liquid_mesher->build_mesh();
if (_job->should_return()) {
return;
}
}
RID mesh_rid = get_mesh_rid_index(MESH_INDEX_LIQUID, MESH_TYPE_INDEX_MESH, 0);
Array temp_mesh_arr = liquid_mesher->build_mesh();
if (_job->should_do()) {
if (mesh_rid == RID()) {
create_meshes(MESH_INDEX_LIQUID, 1);
@ -1539,11 +1653,40 @@ void VoxelChunkDefault::_build_phase(int phase) {
VS::get_singleton()->mesh_clear(mesh_rid);
#endif
if (_job->should_return()) {
return;
}
}
if (_job->should_do()) {
VS::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr);
if (_library->get_liquid_material(0).is_valid())
VS::get_singleton()->mesh_surface_set_material(mesh_rid, 0, _library->get_liquid_material(0)->get_rid());
if (_job->should_return()) {
return;
}
}
}
if (_job->has_meta("bptm_ulm")) {
_job->remove_meta("bptm_ulm");
}
if (_job->has_meta("bptm_ullm")) {
_job->remove_meta("bptm_ullm");
}
if (_job->has_meta("bptm_mm")) {
_job->remove_meta("bptm_mm");
}
if (_job->has_meta("bptm_lmm")) {
_job->remove_meta("bptm_lmm");
}
_job->reset_stages();
next_phase();
@ -1577,7 +1720,7 @@ void VoxelChunkDefault::_build_phase(int phase) {
if (get_prop_mesher()->get_vertex_count() != 0) {
RID mesh_rid = get_mesh_rid_index(MESH_INDEX_PROP, MESH_TYPE_INDEX_MESH, 0);
Array temp_mesh_arr = get_prop_mesher()->build_mesh();
temp_mesh_arr = get_prop_mesher()->build_mesh();
if (mesh_rid == RID()) {
if ((_build_flags & BUILD_FLAG_CREATE_LODS) != 0)

View File

@ -296,6 +296,8 @@ protected:
Vector<Ref<VoxelLight> > _lights;
Ref<VoxelJob> _job;
Array temp_mesh_arr;
};
VARIANT_ENUM_CAST(VoxelChunkDefault::DefaultChannels);