Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve auto lod by comparing every attribute #73734

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/classes/ImporterMesh.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
<param index="0" name="normal_merge_angle" type="float" />
<param index="1" name="normal_split_angle" type="float" />
<param index="2" name="bone_transform_array" type="Array" />
<param index="3" name="minimum_index_count" type="int" />
<param index="4" name="maximum_mesh_error" type="float" />
<description>
Generates all lods for this ImporterMesh.
[param normal_merge_angle] and [param normal_split_angle] are in degrees and used in the same way as the importer settings in [code]lods[/code]. As a good default, use 25 and 60 respectively.
Expand Down
14 changes: 13 additions & 1 deletion editor/import/resource_importer_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,8 @@ void ResourceImporterScene::get_internal_import_options(InternalImportCategory p
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "generate/lods", PROPERTY_HINT_ENUM, "Default,Enable,Disable"), 0));
r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "lods/normal_split_angle", PROPERTY_HINT_RANGE, "0,180,0.1,degrees"), 25.0f));
r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "lods/normal_merge_angle", PROPERTY_HINT_RANGE, "0,180,0.1,degrees"), 60.0f));
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "lods/minimum_triangle_count", PROPERTY_HINT_RANGE, "0,65536,1,or_greater,suffix:triangles"), 3));
r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "lods/maximum_mesh_error", PROPERTY_HINT_RANGE, "0.0001,999999999,0.0001"), 999'999'999.0f));
} break;
case INTERNAL_IMPORT_CATEGORY_MATERIAL: {
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "use_external/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false));
Expand Down Expand Up @@ -1952,10 +1954,12 @@ void ResourceImporterScene::_generate_meshes(Node *p_node, const Dictionary &p_m
//do mesh processing

bool generate_lods = p_generate_lods;
float maximum_mesh_error = 999'999'999.0f;
float split_angle = 25.0f;
float merge_angle = 60.0f;
bool create_shadow_meshes = p_create_shadow_meshes;
bool bake_lightmaps = p_light_bake_mode == LIGHT_BAKE_STATIC_LIGHTMAPS;
int32_t minimum_triangle_count = 3;
String save_to_file;

String mesh_id = src_mesh_node->get_mesh()->get_meta("import_id", src_mesh_node->get_mesh()->get_name());
Expand Down Expand Up @@ -2008,6 +2012,14 @@ void ResourceImporterScene::_generate_meshes(Node *p_node, const Dictionary &p_m
merge_angle = mesh_settings["lods/normal_merge_angle"];
}

if (mesh_settings.has("lods/minimum_triangle_count")) {
minimum_triangle_count = mesh_settings["lods/minimum_triangle_count"];
}

if (mesh_settings.has("lods/maximum_mesh_error")) {
maximum_mesh_error = mesh_settings["lods/maximum_mesh_error"];
}

if (mesh_settings.has("save_to_file/enabled") && bool(mesh_settings["save_to_file/enabled"]) && mesh_settings.has("save_to_file/path")) {
save_to_file = mesh_settings["save_to_file/path"];
if (!save_to_file.is_resource_file()) {
Expand Down Expand Up @@ -2054,7 +2066,7 @@ void ResourceImporterScene::_generate_meshes(Node *p_node, const Dictionary &p_m

if (generate_lods) {
Array skin_pose_transform_array = _get_skinned_pose_transforms(src_mesh_node);
src_mesh_node->get_mesh()->generate_lods(merge_angle, split_angle, skin_pose_transform_array);
src_mesh_node->get_mesh()->generate_lods(merge_angle, split_angle, skin_pose_transform_array, minimum_triangle_count * 3, maximum_mesh_error);
}

if (create_shadow_meshes) {
Expand Down
166 changes: 139 additions & 27 deletions scene/resources/importer_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ void ImporterMesh::set_surface_material(int p_surface, const Ref<Material> &p_ma
} \
write_array[vert_idx] = transformed_vert;

void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_split_angle, Array p_bone_transform_array) {
void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_split_angle, Array p_bone_transform_array, int32_t p_minimum_index_count, float p_max_mesh_error) {
if (!SurfaceTool::simplify_scale_func) {
return;
}
Expand Down Expand Up @@ -299,6 +299,7 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli
Vector<Vector2> uv2s = surfaces[i].arrays[RS::ARRAY_TEX_UV2];
Vector<int> bones = surfaces[i].arrays[RS::ARRAY_BONES];
Vector<float> weights = surfaces[i].arrays[RS::ARRAY_WEIGHTS];
Vector<Color> colors = surfaces[i].arrays[RS::ARRAY_COLOR];

unsigned int index_count = indices.size();
unsigned int vertex_count = vertices.size();
Expand Down Expand Up @@ -350,11 +351,39 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli
LocalVector<int> vertex_remap;
LocalVector<int> vertex_inverse_remap;
LocalVector<Vector3> merged_vertices;
LocalVector<Vector3> merged_normals;
LocalVector<int> merged_normals_counts;
const int bone_weight_length = surfaces[i].flags & Mesh::ARRAY_FLAG_USE_8_BONE_WEIGHTS ? 8 : 4;
const int ATTRIBUTE_NORMAL = 3;
const int ATTRIBUTE_UV = ATTRIBUTE_NORMAL + 2;
const int ATTRIBUTE_UV2 = ATTRIBUTE_UV + 2;
const int ATTRIBUTE_COLOR = ATTRIBUTE_UV2 + 4;
const int ATTRIBUTE_BONE = ATTRIBUTE_COLOR + bone_weight_length;
const int ATTRIBUTE_WEIGHT = ATTRIBUTE_BONE + bone_weight_length;
const int ATTRIBUTE_MAX = ATTRIBUTE_WEIGHT + 1;
struct Attribute {
float normals[3] = { 0, 0, 0 };
float uvs[2] = { 0, 0 };
float uv2s[2] = { 0, 0 };
float colors[4] = { 0, 0, 0, 0 };
Vector<float> bones;
Vector<float> weights;
Attribute(int32_t p_bone_weight_length = 0) {
// Always use 8 bone weights for now.
bones.resize(p_bone_weight_length);
bones.fill(0);
weights.resize(bones.size());
weights.fill(p_bone_weight_length);
}
};
LocalVector<Attribute> merged_attributes;
LocalVector<int> merged_attributes_counts;
const Vector2 *uvs_ptr = uvs.ptr();
const Vector2 *uv2s_ptr = uv2s.ptr();

const Color *color_ptr = colors.ptr();
const int *bones_ptr = bones.ptr();
const float *weights_ptr = weights.ptr();
bool has_uv2 = uv2s.size() > 0;
bool has_color = colors.size() > 0;
bool has_bones = bones.size() > 0;
for (unsigned int j = 0; j < vertex_count; j++) {
const Vector3 &v = vertices_ptr[j];
const Vector3 &n = normals_ptr[j];
Expand All @@ -368,12 +397,31 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli
for (const Pair<int, int> &idx : close_verts) {
bool is_uvs_close = (!uvs_ptr || uvs_ptr[j].distance_squared_to(uvs_ptr[idx.second]) < CMP_EPSILON2);
bool is_uv2s_close = (!uv2s_ptr || uv2s_ptr[j].distance_squared_to(uv2s_ptr[idx.second]) < CMP_EPSILON2);
bool is_colors_close = true;
if (color_ptr) {
Vector4 color_a = Vector4(color_ptr[j].r, color_ptr[j].g, color_ptr[j].b, color_ptr[j].a);
Vector4 color_b = Vector4(color_ptr[idx.second].r, color_ptr[idx.second].g, color_ptr[idx.second].b, color_ptr[idx.second].a);
is_colors_close = color_a.distance_squared_to(color_b) < CMP_EPSILON2;
}
bool is_bones_close = true;
if (bones_ptr) {
for (int32_t k = 0; k < bone_weight_length; k++) {
Vector2 bone_a = Vector2(bones_ptr[j * bone_weight_length + k], weights_ptr[j * bone_weight_length + k]);
Vector2 bone_b = Vector2(bones_ptr[idx.second * bone_weight_length + k], weights_ptr[idx.second * bone_weight_length + k]);
is_bones_close = (bone_a.distance_squared_to(bone_b) < CMP_EPSILON2) && is_bones_close;
if (!is_bones_close) {
break;
}
}
}
ERR_FAIL_INDEX(idx.second, normals.size());
bool is_normals_close = normals[idx.second].dot(n) > normal_merge_threshold;
if (is_uvs_close && is_uv2s_close && is_normals_close) {
if (is_uvs_close && is_uv2s_close && is_normals_close && is_colors_close && is_bones_close) {
vertex_remap.push_back(idx.first);
merged_normals[idx.first] += normals[idx.second];
merged_normals_counts[idx.first]++;
for (int32_t normal_i = 0; normal_i < 3; normal_i++) {
merged_attributes[idx.first].normals[normal_i] += normals[idx.second][normal_i];
}
merged_attributes_counts[idx.first]++;
found = true;
break;
}
Expand All @@ -385,8 +433,41 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli
vertex_inverse_remap.push_back(j);
merged_vertices.push_back(v);
vertex_remap.push_back(vcount);
merged_normals.push_back(normals_ptr[j]);
merged_normals_counts.push_back(1);
Attribute attribute(bone_weight_length);
if (normals_ptr) {
for (int32_t normal_i = 0; normal_i < 3; normal_i++) {
Vector3 normal = normals_ptr[j];
attribute.normals[normal_i] = normal[normal_i];
}
}
if (uvs_ptr) {
for (int32_t uv_i = 0; uv_i < 2; uv_i++) {
Vector2 uv = uvs_ptr[j];
attribute.uvs[uv_i] = uv[uv_i];
}
}
if (has_uv2) {
for (int32_t uv_i = 0; uv_i < 2; uv_i++) {
Vector2 uv2 = uv2s_ptr[j];
attribute.uv2s[uv_i] = uv2[uv_i];
}
}
if (has_color) {
for (int32_t color_i = 0; color_i < 4; color_i++) {
Color color = color_ptr[j];
attribute.colors[color_i] = color[color_i];
}
}
if (has_bones && bones_ptr && weights_ptr) {
for (int32_t k = 0; k < bone_weight_length; k++) {
float bone = bones_ptr[j * bone_weight_length + k];
attribute.bones.write[k] = bone;
float weight = weights_ptr[j * bone_weight_length + k];
attribute.weights.write[k] = weight;
}
}
merged_attributes.push_back(attribute);
merged_attributes_counts.push_back(1);
}
} else {
int vcount = merged_vertices.size();
Expand All @@ -395,8 +476,37 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli
vertex_inverse_remap.push_back(j);
merged_vertices.push_back(v);
vertex_remap.push_back(vcount);
merged_normals.push_back(normals_ptr[j]);
merged_normals_counts.push_back(1);
Attribute attribute(bone_weight_length);
if (normals_ptr) {
for (int32_t normal_i = 0; normal_i < 3; normal_i++) {
Vector3 normal = normals_ptr[j];
attribute.normals[normal_i] = normal[normal_i];
}
}
if (uvs_ptr) {
for (int32_t uv_i = 0; uv_i < 2; uv_i++) {
Vector2 uv = uvs_ptr[j];
attribute.uvs[uv_i] = uv[uv_i];
}
}
if (has_uv2) {
for (int32_t uv_i = 0; uv_i < 2; uv_i++) {
attribute.uv2s[uv_i] = uv2s_ptr[j][uv_i];
}
}
if (has_color) {
for (int32_t color_i = 0; color_i < 4; color_i++) {
attribute.colors[color_i] = color_ptr[j][color_i];
}
}
if (has_bones && bones_ptr && weights_ptr) {
for (int32_t k = 0; k < bone_weight_length; k++) {
attribute.bones.write[k] = bones_ptr[j * bone_weight_length + k];
attribute.weights.write[k] = weights_ptr[j * bone_weight_length + k];
}
}
merged_attributes.push_back(attribute);
merged_attributes_counts.push_back(1);
}
}

Expand All @@ -411,23 +521,25 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli
const int32_t *merged_indices_ptr = merged_indices.ptr();

{
const int *counts_ptr = merged_normals_counts.ptr();
Vector3 *merged_normals_ptrw = merged_normals.ptr();
const int *counts_ptr = merged_attributes_counts.ptr();
Attribute *merged_attributes_ptrw = merged_attributes.ptr();
for (unsigned int j = 0; j < merged_vertex_count; j++) {
merged_normals_ptrw[j] /= counts_ptr[j];
for (int32_t normal_i = 0; normal_i < 3; normal_i++) {
merged_attributes_ptrw[j].normals[normal_i] /= counts_ptr[j];
}
}
}

LocalVector<float> normal_weights;
normal_weights.resize(merged_vertex_count);
LocalVector<float> attribute_weights;
attribute_weights.resize(merged_vertex_count);
for (unsigned int j = 0; j < merged_vertex_count; j++) {
normal_weights[j] = 2.0; // Give some weight to normal preservation, may be worth exposing as an import setting
attribute_weights[j] = ATTRIBUTE_MAX; // Give some weight to normal preservation, may be worth exposing as an import setting
}

Vector<float> merged_vertices_f32 = vector3_to_float32_array(merged_vertices_ptr, merged_vertex_count);
float scale = SurfaceTool::simplify_scale_func(merged_vertices_f32.ptr(), merged_vertex_count, sizeof(float) * 3);

unsigned int index_target = 12; // Start with the smallest target, 4 triangles
unsigned int index_target = p_minimum_index_count;
unsigned int last_index_count = 0;

int split_vertex_count = vertex_count;
Expand All @@ -445,14 +557,12 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli
raycaster->commit();
}

const float max_mesh_error = FLT_MAX; // We don't want to limit by error, just by index target
float max_mesh_error = p_max_mesh_error; // Use the default error from the upstream mesh optimizer.
float mesh_error = 0.0f;

while (index_target < index_count) {
PackedInt32Array new_indices;
new_indices.resize(index_count);

Vector<float> merged_normals_f32 = vector3_to_float32_array(merged_normals.ptr(), merged_normals.size());
const int simplify_options = SurfaceTool::SIMPLIFY_LOCK_BORDER;

size_t new_index_count = SurfaceTool::simplify_with_attrib_func(
Expand All @@ -464,15 +574,17 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli
max_mesh_error,
simplify_options,
&mesh_error,
merged_normals_f32.ptr(),
normal_weights.ptr(), 3);

(float *)merged_attributes.ptr(),
attribute_weights.ptr(),
ATTRIBUTE_MAX);
if (new_index_count < size_t(p_minimum_index_count)) {
break;
}
if (new_index_count < last_index_count * 1.5f) {
index_target = index_target * 1.5f;
continue;
}

if (new_index_count == 0 || (new_index_count >= (index_count * 0.75f))) {
if (new_index_count == 0 || new_index_count < size_t(p_minimum_index_count) || (new_index_count >= (index_count * 0.75f))) {
break;
}

Expand Down Expand Up @@ -1346,7 +1458,7 @@ void ImporterMesh::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_surface_name", "surface_idx", "name"), &ImporterMesh::set_surface_name);
ClassDB::bind_method(D_METHOD("set_surface_material", "surface_idx", "material"), &ImporterMesh::set_surface_material);

ClassDB::bind_method(D_METHOD("generate_lods", "normal_merge_angle", "normal_split_angle", "bone_transform_array"), &ImporterMesh::generate_lods);
ClassDB::bind_method(D_METHOD("generate_lods", "normal_merge_angle", "normal_split_angle", "bone_transform_array", "minimum_index_count", "maximum_mesh_error"), &ImporterMesh::generate_lods);
ClassDB::bind_method(D_METHOD("get_mesh", "base_mesh"), &ImporterMesh::get_mesh, DEFVAL(Ref<ArrayMesh>()));
ClassDB::bind_method(D_METHOD("clear"), &ImporterMesh::clear);

Expand Down
2 changes: 1 addition & 1 deletion scene/resources/importer_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class ImporterMesh : public Resource {

void set_surface_material(int p_surface, const Ref<Material> &p_material);

void generate_lods(float p_normal_merge_angle, float p_normal_split_angle, Array p_skin_pose_transform_array);
void generate_lods(float p_normal_merge_angle, float p_normal_split_angle, Array p_skin_pose_transform_array, int32_t p_minimum_index_count, float p_maximum_mesh_error);

void create_shadow_mesh();
Ref<ImporterMesh> get_shadow_mesh() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ index d8d4a67391..3847afc736 100644
#endif

-#define ATTRIBUTES 8
+#define ATTRIBUTES 3
+#define ATTRIBUTES 20

// This work is based on:
// Michael Garland and Paul S. Heckbert. Surface simplification using quadric error metrics. 1997
Expand Down
2 changes: 1 addition & 1 deletion thirdparty/meshoptimizer/simplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#define TRACESTATS(i) (void)0
#endif

#define ATTRIBUTES 3
#define ATTRIBUTES 20

// This work is based on:
// Michael Garland and Paul S. Heckbert. Surface simplification using quadric error metrics. 1997
Expand Down