Brought in the step code from Terraman. Not yet finished, but it compiles.

This commit is contained in:
Relintai 2021-08-09 13:15:20 +02:00
parent 6c8deb8956
commit fca5abaa5d
2 changed files with 389 additions and 0 deletions

View File

@ -487,6 +487,383 @@ void PropInstancePropJob::_reset() {
set_build_phase_type(BUILD_PHASE_TYPE_PHYSICS_PROCESS); set_build_phase_type(BUILD_PHASE_TYPE_PHYSICS_PROCESS);
} }
void PropInstancePropJob::phase_steps() {
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
return;
}
//set up the meshes
if (should_do()) {
if (_prop_instace->mesh_get_num() == 0) {
//need to allocate the meshes
//first count how many we need
int count = 0;
for (int i = 0; i < _job_steps.size(); ++i) {
Ref<PropMesherJobStep> step = _job_steps[i];
ERR_FAIL_COND(!step.is_valid());
switch (step->get_job_type()) {
case PropMesherJobStep::TYPE_NORMAL:
++count;
break;
case PropMesherJobStep::TYPE_NORMAL_LOD:
++count;
break;
case PropMesherJobStep::TYPE_DROP_UV2:
++count;
break;
case PropMesherJobStep::TYPE_MERGE_VERTS:
++count;
break;
case PropMesherJobStep::TYPE_BAKE_TEXTURE:
++count;
break;
case PropMesherJobStep::TYPE_SIMPLIFY_MESH:
#ifdef MESH_UTILS_PRESENT
count += step->get_simplification_steps();
#endif
break;
default:
break;
}
}
//allocate
if (count > 0) {
_prop_instace->meshes_create(count);
}
} else {
//we have the meshes, just clear
int count = _prop_instace->mesh_get_num();
for (int i = 0; i < count; ++i) {
RID mesh_rid = _prop_instace->mesh_get(i);
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();) {
Ref<PropMesherJobStep> step = _job_steps[_current_job_step];
ERR_FAIL_COND(!step.is_valid());
switch (step->get_job_type()) {
case PropMesherJobStep::TYPE_NORMAL:
step_type_normal();
break;
case PropMesherJobStep::TYPE_NORMAL_LOD:
step_type_normal_lod();
break;
case PropMesherJobStep::TYPE_DROP_UV2:
step_type_drop_uv2();
break;
case PropMesherJobStep::TYPE_MERGE_VERTS:
step_type_merge_verts();
break;
case PropMesherJobStep::TYPE_BAKE_TEXTURE:
step_type_bake_texture();
break;
case PropMesherJobStep::TYPE_SIMPLIFY_MESH:
step_type_simplify_mesh();
break;
case PropMesherJobStep::TYPE_OTHER:
//do nothing
break;
}
++_current_job_step;
if (should_return()) {
return;
}
}
reset_stages();
//next_phase();
set_complete(true); //So threadpool knows it's done
}
void PropInstancePropJob::step_type_normal() {
//TODO add a lighting generation step
temp_mesh_arr = _prop_mesher->build_mesh();
RID mesh_rid = _prop_instace->mesh_get(_current_mesh);
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;
}
void PropInstancePropJob::step_type_normal_lod() {
print_error("Error: step_type_normal_lod doesn't work for TerraPropJobs!");
++_current_mesh;
}
void PropInstancePropJob::step_type_drop_uv2() {
RID mesh_rid = _prop_instace->mesh_get(_current_mesh);
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;
}
void PropInstancePropJob::step_type_merge_verts() {
Array temp_mesh_arr2 = merge_mesh_array(temp_mesh_arr);
temp_mesh_arr = temp_mesh_arr2;
RID mesh_rid = _prop_instace->mesh_get(_current_mesh);
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;
}
void PropInstancePropJob::step_type_bake_texture() {
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();
RID mesh_rid = _prop_instace->mesh_get(_current_mesh);
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;
}
void PropInstancePropJob::step_type_simplify_mesh() {
#ifdef MESH_UTILS_PRESENT
Ref<PropMesherJobStep> step = _job_steps[_current_job_step];
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();
RID mesh_rid = _prop_instace->mesh_get(_current_mesh);
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
}
Array PropInstancePropJob::merge_mesh_array(Array arr) const {
ERR_FAIL_COND_V(arr.size() != VisualServer::ARRAY_MAX, arr);
PoolVector3Array verts = arr[VisualServer::ARRAY_VERTEX];
PoolVector3Array normals = arr[VisualServer::ARRAY_NORMAL];
PoolVector2Array uvs = arr[VisualServer::ARRAY_TEX_UV];
PoolColorArray colors = arr[VisualServer::ARRAY_COLOR];
PoolIntArray indices = arr[VisualServer::ARRAY_INDEX];
bool has_normals = normals.size() > 0;
bool has_uvs = uvs.size() > 0;
bool has_colors = colors.size() > 0;
int i = 0;
while (i < verts.size()) {
Vector3 v = verts[i];
Array equals;
for (int j = i + 1; j < verts.size(); ++j) {
Vector3 vc = verts[j];
if (Math::is_equal_approx(v.x, vc.x) && Math::is_equal_approx(v.y, vc.y) && Math::is_equal_approx(v.z, vc.z))
equals.push_back(j);
}
for (int k = 0; k < equals.size(); ++k) {
int rem = equals[k];
int remk = rem - k;
verts.remove(remk);
if (has_normals)
normals.remove(remk);
if (has_uvs)
uvs.remove(remk);
if (has_colors)
colors.remove(remk);
for (int j = 0; j < indices.size(); ++j) {
int indx = indices[j];
if (indx == remk)
indices.set(j, i);
else if (indx > remk)
indices.set(j, indx - 1);
}
}
++i;
}
arr[VisualServer::ARRAY_VERTEX] = verts;
if (has_normals)
arr[VisualServer::ARRAY_NORMAL] = normals;
if (has_uvs)
arr[VisualServer::ARRAY_TEX_UV] = uvs;
if (has_colors)
arr[VisualServer::ARRAY_COLOR] = colors;
arr[VisualServer::ARRAY_INDEX] = indices;
return arr;
}
Array PropInstancePropJob::bake_mesh_array_uv(Array arr, Ref<Texture> tex, const float mul_color) const {
ERR_FAIL_COND_V(arr.size() != VisualServer::ARRAY_MAX, arr);
ERR_FAIL_COND_V(!tex.is_valid(), arr);
Ref<Image> img = tex->get_data();
ERR_FAIL_COND_V(!img.is_valid(), arr);
Vector2 imgsize = img->get_size();
PoolVector2Array uvs = arr[VisualServer::ARRAY_TEX_UV];
PoolColorArray colors = arr[VisualServer::ARRAY_COLOR];
if (colors.size() < uvs.size())
colors.resize(uvs.size());
#if !GODOT4
img->lock();
#endif
for (int i = 0; i < uvs.size(); ++i) {
Vector2 uv = uvs[i];
uv *= imgsize;
int ux = static_cast<int>(CLAMP(uv.x, 0, imgsize.x - 1));
int uy = static_cast<int>(CLAMP(uv.y, 0, imgsize.y - 1));
Color c = img->get_pixel(ux, uy);
colors.set(i, colors[i] * c * mul_color);
}
#if !GODOT4
img->unlock();
#endif
arr[VisualServer::ARRAY_COLOR] = colors;
return arr;
}
PropInstancePropJob::PropInstancePropJob() { PropInstancePropJob::PropInstancePropJob() {
set_build_phase_type(BUILD_PHASE_TYPE_PHYSICS_PROCESS); set_build_phase_type(BUILD_PHASE_TYPE_PHYSICS_PROCESS);

View File

@ -72,6 +72,18 @@ public:
void _execute_phase(); void _execute_phase();
void _reset(); void _reset();
void phase_steps();
void step_type_normal();
void step_type_normal_lod();
void step_type_drop_uv2();
void step_type_merge_verts();
void step_type_bake_texture();
void step_type_simplify_mesh();
Array merge_mesh_array(Array arr) const;
Array bake_mesh_array_uv(Array arr, Ref<Texture> tex, float mul_color = 0.7) const;
PropInstancePropJob(); PropInstancePropJob();
~PropInstancePropJob(); ~PropInstancePropJob();