2021-11-22 22:00:45 +01:00
|
|
|
/*
|
2022-02-22 10:33:06 +01:00
|
|
|
Copyright (c) 2019-2022 Péter Magyar
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2022-02-22 11:09:36 +01:00
|
|
|
#include "terrain_2d_prop_job.h"
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
#include "../../defines.h"
|
|
|
|
|
2022-02-22 11:09:36 +01:00
|
|
|
#include "../../library/terrain_2d_material_cache.h"
|
|
|
|
#include "../../library/terrain_2d_surface.h"
|
|
|
|
#include "../../library/terrain_2d_library.h"
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:09:36 +01:00
|
|
|
#include "../../meshers/default/terrain_2d_mesher_default.h"
|
|
|
|
#include "../../meshers/terrain_2d_mesher.h"
|
|
|
|
#include "../default/terrain_2d_chunk_default.h"
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
#ifdef MESH_DATA_RESOURCE_PRESENT
|
|
|
|
#include "../../../mesh_data_resource/mesh_data_resource.h"
|
|
|
|
#endif
|
|
|
|
|
2022-02-22 11:09:36 +01:00
|
|
|
#include "../../world/default/terrain_2d_world_default.h"
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
#ifdef MESH_UTILS_PRESENT
|
|
|
|
#include "../../../mesh_utils/fast_quadratic_mesh_simplifier.h"
|
|
|
|
#endif
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
Ref<Terrain2DMesher> Terrain2DPropJob::get_prop_mesher() const {
|
2021-11-22 22:00:45 +01:00
|
|
|
return _prop_mesher;
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DPropJob::set_prop_mesher(const Ref<Terrain2DMesher> &mesher) {
|
2021-11-22 22:00:45 +01:00
|
|
|
_prop_mesher = mesher;
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
Ref<Terrain2DMesherJobStep> Terrain2DPropJob::get_jobs_step(int index) const {
|
|
|
|
ERR_FAIL_INDEX_V(index, _job_steps.size(), Ref<Terrain2DMesherJobStep>());
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
return _job_steps.get(index);
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DPropJob::set_jobs_step(int index, const Ref<Terrain2DMesherJobStep> &step) {
|
2021-11-22 22:00:45 +01:00
|
|
|
ERR_FAIL_INDEX(index, _job_steps.size());
|
|
|
|
|
|
|
|
_job_steps.set(index, step);
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DPropJob::remove_jobs_step(const int index) {
|
2021-11-22 22:00:45 +01:00
|
|
|
ERR_FAIL_INDEX(index, _job_steps.size());
|
|
|
|
|
|
|
|
_job_steps.remove(index);
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DPropJob::add_jobs_step(const Ref<Terrain2DMesherJobStep> &step) {
|
2021-11-22 22:00:45 +01:00
|
|
|
_job_steps.push_back(step);
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
int Terrain2DPropJob::get_jobs_step_count() const {
|
2021-11-22 22:00:45 +01:00
|
|
|
return _job_steps.size();
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DPropJob::phase_physics_process() {
|
|
|
|
Ref<Terrain2DChunkDefault> chunk = _chunk;
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
//TODO this should only update the differences
|
|
|
|
for (int i = 0; i < chunk->collider_get_count(); ++i) {
|
|
|
|
PhysicsServer::get_singleton()->free(chunk->collider_get_body(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
chunk->colliders_clear();
|
|
|
|
|
|
|
|
#ifdef MESH_DATA_RESOURCE_PRESENT
|
|
|
|
for (int i = 0; i < chunk->mesh_data_resource_get_count(); ++i) {
|
|
|
|
Ref<MeshDataResource> mdr = chunk->mesh_data_resource_get(i);
|
|
|
|
|
|
|
|
for (int j = 0; j < mdr->get_collision_shape_count(); ++j) {
|
|
|
|
Ref<Shape> shape = mdr->get_collision_shape(j);
|
|
|
|
Transform offset = mdr->get_collision_shape_offset(j);
|
|
|
|
|
|
|
|
if (!shape.is_valid()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RID body = PhysicsServer::get_singleton()->body_create(PhysicsServer::BODY_MODE_STATIC);
|
|
|
|
|
|
|
|
Transform transform = chunk->mesh_data_resource_get_transform(i);
|
|
|
|
transform *= offset;
|
|
|
|
|
|
|
|
PhysicsServer::get_singleton()->body_add_shape(body, shape->get_rid());
|
|
|
|
|
|
|
|
//TODO store the layer mask somewhere
|
|
|
|
PhysicsServer::get_singleton()->body_set_collision_layer(body, 1);
|
|
|
|
PhysicsServer::get_singleton()->body_set_collision_mask(body, 1);
|
|
|
|
|
|
|
|
if (chunk->get_voxel_world()->is_inside_tree() && chunk->get_voxel_world()->is_inside_world()) {
|
|
|
|
Ref<World> world = chunk->get_voxel_world()->GET_WORLD();
|
|
|
|
|
|
|
|
if (world.is_valid() && world->get_space() != RID()) {
|
|
|
|
PhysicsServer::get_singleton()->body_set_space(body, world->get_space());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsServer::get_singleton()->body_set_state(body, PhysicsServer::BODY_STATE_TRANSFORM, chunk->get_transform() * transform);
|
|
|
|
|
|
|
|
chunk->collider_add(transform, shape, shape->get_rid(), body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if TOOLS_ENABLED
|
|
|
|
if (SceneTree::get_singleton()->is_debugging_collisions_hint() && chunk->collider_get_count() > 0) {
|
|
|
|
chunk->draw_debug_mdr_colliders();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
set_build_phase_type(BUILD_PHASE_TYPE_NORMAL);
|
|
|
|
next_phase();
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DPropJob::phase_prop() {
|
2021-11-22 22:00:45 +01:00
|
|
|
#ifdef MESH_DATA_RESOURCE_PRESENT
|
2022-02-22 11:42:28 +01:00
|
|
|
Ref<Terrain2DChunkDefault> chunk = _chunk;
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
if (!get_prop_mesher().is_valid()) {
|
|
|
|
set_complete(true); //So threadpool knows it's done
|
|
|
|
next_job();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (should_do()) {
|
|
|
|
if (chunk->mesh_data_resource_get_count() == 0) {
|
|
|
|
reset_stages();
|
|
|
|
set_complete(true); //So threadpool knows it's done
|
|
|
|
next_job();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < chunk->mesh_data_resource_get_count(); ++i) {
|
|
|
|
if (chunk->mesh_data_resource_get_is_inside(i)) {
|
|
|
|
get_prop_mesher()->add_mesh_data_resource_transform(chunk->mesh_data_resource_get(i), chunk->mesh_data_resource_get_transform(i), chunk->mesh_data_resource_get_uv_rect(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (should_return()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (should_do()) {
|
2022-02-22 11:42:28 +01:00
|
|
|
if ((chunk->get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_LIGHTING) != 0) {
|
2021-11-22 22:00:45 +01:00
|
|
|
get_prop_mesher()->bake_colors(_chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (should_return()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (should_do()) {
|
2022-02-22 11:42:28 +01:00
|
|
|
if ((chunk->get_build_flags() & Terrain2DChunkDefault::BUILD_FLAG_USE_LIGHTING) != 0) {
|
|
|
|
Terrain2DWorldDefault *world = Object::cast_to<Terrain2DWorldDefault>(chunk->get_voxel_world());
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
if (world) {
|
|
|
|
for (int i = 0; i < chunk->mesh_data_resource_get_count(); ++i) {
|
|
|
|
if (!chunk->mesh_data_resource_get_is_inside(i)) {
|
|
|
|
Ref<MeshDataResource> mdr = chunk->mesh_data_resource_get(i);
|
|
|
|
|
|
|
|
ERR_CONTINUE(!mdr.is_valid());
|
|
|
|
|
|
|
|
Transform trf = chunk->mesh_data_resource_get_transform(i);
|
|
|
|
|
|
|
|
Array arr = mdr->get_array();
|
|
|
|
|
|
|
|
if (arr.size() <= Mesh::ARRAY_VERTEX) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolVector3Array varr = arr[Mesh::ARRAY_VERTEX];
|
|
|
|
|
|
|
|
if (varr.size() == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolColorArray carr = world->get_vertex_colors(trf, varr);
|
|
|
|
|
|
|
|
get_prop_mesher()->add_mesh_data_resource_transform_colored(mdr, trf, carr, chunk->mesh_data_resource_get_uv_rect(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_prop_mesher()->get_vertex_count() == 0) {
|
|
|
|
reset_stages();
|
|
|
|
|
|
|
|
set_complete(true); //So threadpool knows it's done
|
|
|
|
next_job();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
reset_stages();
|
|
|
|
next_phase();
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DPropJob::_physics_process(float delta) {
|
2021-11-22 22:00:45 +01:00
|
|
|
if (_phase == 0)
|
|
|
|
phase_physics_process();
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DPropJob::_execute_phase() {
|
2021-11-22 22:00:45 +01:00
|
|
|
ERR_FAIL_COND(!_chunk.is_valid());
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
Ref<Terrain2DLibrary> library = _chunk->get_library();
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!library.is_valid());
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
Ref<Terrain2DChunkDefault> chunk = _chunk;
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
if (!chunk.is_valid()
|
|
|
|
#ifdef MESH_DATA_RESOURCE_PRESENT
|
|
|
|
|| chunk->mesh_data_resource_get_count() == 0
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
set_complete(true);
|
|
|
|
next_job();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_phase == 1) {
|
|
|
|
phase_setup();
|
|
|
|
} else if (_phase == 2) {
|
|
|
|
phase_prop();
|
|
|
|
} else if (_phase == 3) {
|
|
|
|
phase_steps();
|
|
|
|
} else if (_phase > 3) {
|
|
|
|
set_complete(true); //So threadpool knows it's done
|
|
|
|
next_job();
|
2022-02-22 11:42:28 +01:00
|
|
|
ERR_FAIL_MSG("Terrain2DPropJob: _phase is too high!");
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DPropJob::_reset() {
|
|
|
|
Terrain2DJob::_reset();
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
_build_done = false;
|
|
|
|
_phase = 0;
|
|
|
|
|
|
|
|
_current_job_step = 0;
|
|
|
|
_current_mesh = 0;
|
|
|
|
|
|
|
|
if (get_prop_mesher().is_valid()) {
|
|
|
|
get_prop_mesher()->reset();
|
|
|
|
get_prop_mesher()->set_library(_chunk->get_library());
|
|
|
|
}
|
|
|
|
|
|
|
|
set_build_phase_type(BUILD_PHASE_TYPE_PHYSICS_PROCESS);
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DPropJob::phase_setup() {
|
|
|
|
Ref<Terrain2DLibrary> library = _chunk->get_library();
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
if (!library->supports_caching()) {
|
|
|
|
next_phase();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (library->supports_caching()) {
|
|
|
|
if (!_chunk->prop_material_cache_key_has()) {
|
|
|
|
library->prop_material_cache_get_key(_chunk);
|
|
|
|
|
|
|
|
if (!_chunk->prop_material_cache_key_has()) {
|
|
|
|
//chunk does not need a key
|
|
|
|
next_phase();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
Ref<Terrain2DMaterialCache> cache = library->prop_material_cache_get(_chunk->prop_material_cache_key_get());
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
//Note: without threadpool and threading none of this can happen, as cache will get initialized the first time a thread requests it!
|
|
|
|
while (!cache->get_initialized()) {
|
|
|
|
//Means it's currently merging the atlases on a different thread.
|
|
|
|
//Let's just wait
|
|
|
|
OS::get_singleton()->delay_usec(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if MESH_DATA_RESOURCE_PRESENT
|
|
|
|
for (int i = 0; i < _chunk->mesh_data_resource_get_count(); ++i) {
|
|
|
|
Ref<Texture> tex = _chunk->mesh_data_resource_get_texture(i);
|
|
|
|
|
|
|
|
if (!tex.is_valid())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Rect2 r = cache->additional_texture_get_uv_rect(tex);
|
|
|
|
|
|
|
|
_chunk->mesh_data_resource_set_uv_rect(i, r);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
next_phase();
|
|
|
|
|
|
|
|
if (should_return()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DPropJob::phase_steps() {
|
|
|
|
Ref<Terrain2DChunkDefault> chunk = _chunk;
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!_prop_mesher.is_valid());
|
|
|
|
|
|
|
|
if (should_return()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_prop_mesher->get_vertex_count() == 0) {
|
|
|
|
reset_stages();
|
|
|
|
//next_phase();
|
|
|
|
set_complete(true); //So threadpool knows it's done
|
|
|
|
next_job();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//set up the meshes
|
|
|
|
if (should_do()) {
|
2022-02-22 11:42:28 +01:00
|
|
|
RID mesh_rid = chunk->mesh_rid_get_index(Terrain2DChunkDefault::MESH_INDEX_PROP, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH, 0);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
if (mesh_rid == RID()) {
|
|
|
|
//need to allocate the meshes
|
|
|
|
|
|
|
|
//first count how many we need
|
|
|
|
int count = 0;
|
|
|
|
for (int i = 0; i < _job_steps.size(); ++i) {
|
2022-02-22 11:42:28 +01:00
|
|
|
Ref<Terrain2DMesherJobStep> step = _job_steps[i];
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!step.is_valid());
|
|
|
|
|
|
|
|
switch (step->get_job_type()) {
|
2022-02-22 11:42:28 +01:00
|
|
|
case Terrain2DMesherJobStep::TYPE_NORMAL:
|
2021-11-22 22:00:45 +01:00
|
|
|
++count;
|
|
|
|
break;
|
2022-02-22 11:42:28 +01:00
|
|
|
case Terrain2DMesherJobStep::TYPE_NORMAL_LOD:
|
2021-11-22 22:00:45 +01:00
|
|
|
++count;
|
|
|
|
break;
|
2022-02-22 11:42:28 +01:00
|
|
|
case Terrain2DMesherJobStep::TYPE_DROP_UV2:
|
2021-11-22 22:00:45 +01:00
|
|
|
++count;
|
|
|
|
break;
|
2022-02-22 11:42:28 +01:00
|
|
|
case Terrain2DMesherJobStep::TYPE_MERGE_VERTS:
|
2021-11-22 22:00:45 +01:00
|
|
|
++count;
|
|
|
|
break;
|
2022-02-22 11:42:28 +01:00
|
|
|
case Terrain2DMesherJobStep::TYPE_BAKE_TEXTURE:
|
2021-11-22 22:00:45 +01:00
|
|
|
++count;
|
|
|
|
break;
|
2022-02-22 11:42:28 +01:00
|
|
|
case Terrain2DMesherJobStep::TYPE_SIMPLIFY_MESH:
|
2021-11-22 22:00:45 +01:00
|
|
|
#ifdef MESH_UTILS_PRESENT
|
|
|
|
count += step->get_simplification_steps();
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//allocate
|
|
|
|
if (count > 0)
|
2022-02-22 11:42:28 +01:00
|
|
|
chunk->meshes_create(Terrain2DChunkDefault::MESH_INDEX_PROP, count);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
//we have the meshes, just clear
|
2022-02-22 11:42:28 +01:00
|
|
|
int count = chunk->mesh_rid_get_count(Terrain2DChunkDefault::MESH_INDEX_PROP, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < count; ++i) {
|
2022-02-22 11:42:28 +01:00
|
|
|
mesh_rid = chunk->mesh_rid_get_index(Terrain2DChunkDefault::MESH_INDEX_PROP, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH, i);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
if (VS::get_singleton()->mesh_get_surface_count(mesh_rid) > 0)
|
|
|
|
#if !GODOT4
|
|
|
|
VS::get_singleton()->mesh_remove_surface(mesh_rid, 0);
|
|
|
|
#else
|
|
|
|
VS::get_singleton()->mesh_clear(mesh_rid);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; _current_job_step < _job_steps.size();) {
|
2022-02-22 11:42:28 +01:00
|
|
|
Ref<Terrain2DMesherJobStep> step = _job_steps[_current_job_step];
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!step.is_valid());
|
|
|
|
|
|
|
|
switch (step->get_job_type()) {
|
2022-02-22 11:42:28 +01:00
|
|
|
case Terrain2DMesherJobStep::TYPE_NORMAL:
|
2021-11-22 22:00:45 +01:00
|
|
|
step_type_normal();
|
|
|
|
break;
|
2022-02-22 11:42:28 +01:00
|
|
|
case Terrain2DMesherJobStep::TYPE_NORMAL_LOD:
|
2021-11-22 22:00:45 +01:00
|
|
|
step_type_normal_lod();
|
|
|
|
break;
|
2022-02-22 11:42:28 +01:00
|
|
|
case Terrain2DMesherJobStep::TYPE_DROP_UV2:
|
2021-11-22 22:00:45 +01:00
|
|
|
step_type_drop_uv2();
|
|
|
|
break;
|
2022-02-22 11:42:28 +01:00
|
|
|
case Terrain2DMesherJobStep::TYPE_MERGE_VERTS:
|
2021-11-22 22:00:45 +01:00
|
|
|
step_type_merge_verts();
|
|
|
|
break;
|
2022-02-22 11:42:28 +01:00
|
|
|
case Terrain2DMesherJobStep::TYPE_BAKE_TEXTURE:
|
2021-11-22 22:00:45 +01:00
|
|
|
step_type_bake_texture();
|
|
|
|
break;
|
2022-02-22 11:42:28 +01:00
|
|
|
case Terrain2DMesherJobStep::TYPE_SIMPLIFY_MESH:
|
2021-11-22 22:00:45 +01:00
|
|
|
step_type_simplify_mesh();
|
|
|
|
break;
|
2022-02-22 11:42:28 +01:00
|
|
|
case Terrain2DMesherJobStep::TYPE_OTHER:
|
2021-11-22 22:00:45 +01:00
|
|
|
//do nothing
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++_current_job_step;
|
|
|
|
|
|
|
|
if (should_return()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reset_stages();
|
|
|
|
//next_phase();
|
|
|
|
|
|
|
|
set_complete(true); //So threadpool knows it's done
|
|
|
|
next_job();
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DPropJob::step_type_normal() {
|
2021-11-22 22:00:45 +01:00
|
|
|
//TODO add a lighting generation step
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
Ref<Terrain2DChunkDefault> chunk = _chunk;
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
temp_mesh_arr = _prop_mesher->build_mesh();
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
RID mesh_rid = chunk->mesh_rid_get_index(Terrain2DChunkDefault::MESH_INDEX_PROP, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH, _current_mesh);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
VS::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr);
|
|
|
|
|
|
|
|
Ref<Material> lmat;
|
|
|
|
|
|
|
|
if (chunk->prop_material_cache_key_has()) {
|
|
|
|
lmat = chunk->get_library()->prop_material_cache_get(_chunk->prop_material_cache_key_get())->material_lod_get(_current_mesh);
|
|
|
|
} else {
|
|
|
|
lmat = chunk->get_library()->prop_material_lod_get(_current_mesh);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lmat.is_valid()) {
|
|
|
|
VisualServer::get_singleton()->mesh_surface_set_material(mesh_rid, 0, lmat->get_rid());
|
|
|
|
}
|
|
|
|
|
|
|
|
++_current_mesh;
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DPropJob::step_type_normal_lod() {
|
|
|
|
print_error("Error: step_type_normal_lod doesn't work for Terrain2DPropJobs!");
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
++_current_mesh;
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DPropJob::step_type_drop_uv2() {
|
|
|
|
Ref<Terrain2DChunkDefault> chunk = _chunk;
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
RID mesh_rid = chunk->mesh_rid_get_index(Terrain2DChunkDefault::MESH_INDEX_PROP, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH, _current_mesh);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
temp_mesh_arr[VisualServer::ARRAY_TEX_UV2] = Variant();
|
|
|
|
|
|
|
|
VisualServer::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr);
|
|
|
|
|
|
|
|
Ref<Material> lmat;
|
|
|
|
|
|
|
|
if (chunk->prop_material_cache_key_has()) {
|
|
|
|
lmat = chunk->get_library()->prop_material_cache_get(_chunk->prop_material_cache_key_get())->material_lod_get(_current_mesh);
|
|
|
|
} else {
|
|
|
|
lmat = chunk->get_library()->prop_material_lod_get(_current_mesh);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lmat.is_valid()) {
|
|
|
|
VisualServer::get_singleton()->mesh_surface_set_material(mesh_rid, 0, lmat->get_rid());
|
|
|
|
}
|
|
|
|
|
|
|
|
++_current_mesh;
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DPropJob::step_type_merge_verts() {
|
2021-11-22 22:00:45 +01:00
|
|
|
Array temp_mesh_arr2 = merge_mesh_array(temp_mesh_arr);
|
|
|
|
temp_mesh_arr = temp_mesh_arr2;
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
Ref<Terrain2DChunkDefault> chunk = _chunk;
|
|
|
|
RID mesh_rid = chunk->mesh_rid_get_index(Terrain2DChunkDefault::MESH_INDEX_PROP, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH, _current_mesh);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
VisualServer::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr);
|
|
|
|
|
|
|
|
Ref<Material> lmat;
|
|
|
|
|
|
|
|
if (chunk->prop_material_cache_key_has()) {
|
|
|
|
lmat = chunk->get_library()->prop_material_cache_get(_chunk->prop_material_cache_key_get())->material_lod_get(_current_mesh);
|
|
|
|
} else {
|
|
|
|
lmat = chunk->get_library()->prop_material_lod_get(_current_mesh);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lmat.is_valid()) {
|
|
|
|
VisualServer::get_singleton()->mesh_surface_set_material(mesh_rid, 0, lmat->get_rid());
|
|
|
|
}
|
|
|
|
|
|
|
|
++_current_mesh;
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DPropJob::step_type_bake_texture() {
|
|
|
|
Ref<Terrain2DChunkDefault> chunk = _chunk;
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
Ref<ShaderMaterial> mat = chunk->get_library()->material_lod_get(0);
|
|
|
|
Ref<SpatialMaterial> spmat = chunk->get_library()->material_lod_get(0);
|
|
|
|
Ref<Texture> tex;
|
|
|
|
|
|
|
|
if (mat.is_valid()) {
|
|
|
|
tex = mat->get_shader_param("texture_albedo");
|
|
|
|
} else if (spmat.is_valid()) {
|
|
|
|
tex = spmat->get_texture(SpatialMaterial::TEXTURE_ALBEDO);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tex.is_valid()) {
|
|
|
|
temp_mesh_arr = bake_mesh_array_uv(temp_mesh_arr, tex);
|
|
|
|
temp_mesh_arr[VisualServer::ARRAY_TEX_UV] = Variant();
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
RID mesh_rid = chunk->mesh_rid_get_index(Terrain2DChunkDefault::MESH_INDEX_PROP, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH, _current_mesh);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
VisualServer::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr);
|
|
|
|
|
|
|
|
Ref<Material> lmat;
|
|
|
|
|
|
|
|
if (chunk->prop_material_cache_key_has()) {
|
|
|
|
lmat = chunk->get_library()->prop_material_cache_get(_chunk->prop_material_cache_key_get())->material_lod_get(_current_mesh);
|
|
|
|
} else {
|
|
|
|
lmat = chunk->get_library()->prop_material_lod_get(_current_mesh);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lmat.is_valid()) {
|
|
|
|
VisualServer::get_singleton()->mesh_surface_set_material(mesh_rid, 0, lmat->get_rid());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
++_current_mesh;
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DPropJob::step_type_simplify_mesh() {
|
2021-11-22 22:00:45 +01:00
|
|
|
#ifdef MESH_UTILS_PRESENT
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
Ref<Terrain2DChunkDefault> chunk = _chunk;
|
|
|
|
Ref<Terrain2DMesherJobStep> step = _job_steps[_current_job_step];
|
2021-11-22 22:00:45 +01:00
|
|
|
ERR_FAIL_COND(!step.is_valid());
|
|
|
|
Ref<FastQuadraticMeshSimplifier> fqms = step->get_fqms();
|
|
|
|
ERR_FAIL_COND(!fqms.is_valid());
|
|
|
|
|
|
|
|
fqms->initialize(temp_mesh_arr);
|
|
|
|
|
|
|
|
for (int i = 0; i < step->get_simplification_steps(); ++i) {
|
|
|
|
fqms->simplify_mesh(temp_mesh_arr.size() * step->get_simplification_step_ratio(), step->get_simplification_agressiveness());
|
|
|
|
temp_mesh_arr = fqms->get_arrays();
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
RID mesh_rid = chunk->mesh_rid_get_index(Terrain2DChunkDefault::MESH_INDEX_PROP, Terrain2DChunkDefault::MESH_TYPE_INDEX_MESH, _current_mesh);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
VisualServer::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr);
|
|
|
|
|
|
|
|
Ref<Material> lmat;
|
|
|
|
|
|
|
|
if (chunk->prop_material_cache_key_has()) {
|
|
|
|
lmat = chunk->get_library()->prop_material_cache_get(_chunk->prop_material_cache_key_get())->material_lod_get(_current_mesh);
|
|
|
|
} else {
|
|
|
|
lmat = chunk->get_library()->prop_material_lod_get(_current_mesh);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lmat.is_valid()) {
|
|
|
|
VisualServer::get_singleton()->mesh_surface_set_material(mesh_rid, 0, lmat->get_rid());
|
|
|
|
}
|
|
|
|
|
|
|
|
++_current_mesh;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
Terrain2DPropJob::Terrain2DPropJob() {
|
2021-11-22 22:00:45 +01:00
|
|
|
set_build_phase_type(BUILD_PHASE_TYPE_PHYSICS_PROCESS);
|
|
|
|
|
|
|
|
_current_job_step = 0;
|
|
|
|
_current_mesh = 0;
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
Terrain2DPropJob::~Terrain2DPropJob() {
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DPropJob::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_prop_mesher"), &Terrain2DPropJob::get_prop_mesher);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_prop_mesher", "mesher"), &Terrain2DPropJob::set_prop_mesher);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "prop_mesher", PROPERTY_HINT_RESOURCE_TYPE, "Terrain2DMesher", 0), "set_prop_mesher", "get_prop_mesher");
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_jobs_step", "index"), &Terrain2DPropJob::get_jobs_step);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_jobs_step", "index", "mesher"), &Terrain2DPropJob::set_jobs_step);
|
|
|
|
ClassDB::bind_method(D_METHOD("remove_jobs_step", "index"), &Terrain2DPropJob::remove_jobs_step);
|
|
|
|
ClassDB::bind_method(D_METHOD("add_jobs_step", "mesher"), &Terrain2DPropJob::add_jobs_step);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_jobs_step_count"), &Terrain2DPropJob::get_jobs_step_count);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_physics_process", "delta"), &Terrain2DPropJob::_physics_process);
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|