Voxel Remesher: Option to preserver material slots

This commit is contained in:
Pablo Dobarro 2021-03-08 17:37:47 +01:00
parent 11613f8c7a
commit 5234e23c2c
8 changed files with 47 additions and 4 deletions

View File

@ -523,6 +523,7 @@ class DATA_PT_remesh(MeshButtonsPanel, Panel):
col.prop(mesh, "use_remesh_preserve_volume", text="Volume")
col.prop(mesh, "use_remesh_preserve_paint_mask", text="Paint Mask")
col.prop(mesh, "use_remesh_preserve_sculpt_face_sets", text="Face Sets")
col.prop(mesh, "use_remesh_preserve_materials", text="Materials")
if context.preferences.experimental.use_sculpt_vertex_colors:
col.prop(mesh, "use_remesh_preserve_vertex_colors", text="Vertex Colors")

View File

@ -829,6 +829,7 @@ class VIEW3D_PT_sculpt_voxel_remesh(Panel, View3DPaintPanel):
col.prop(mesh, "use_remesh_preserve_volume", text="Volume")
col.prop(mesh, "use_remesh_preserve_paint_mask", text="Paint Mask")
col.prop(mesh, "use_remesh_preserve_sculpt_face_sets", text="Face Sets")
col.prop(mesh, "use_remesh_preserve_materials", text="Materials")
if context.preferences.experimental.use_sculpt_vertex_colors:
col.prop(mesh, "use_remesh_preserve_vertex_colors", text="Vertex Colors")

View File

@ -61,6 +61,7 @@ struct Mesh *BKE_mesh_remesh_quadriflow_to_mesh_nomain(struct Mesh *mesh,
void BKE_mesh_remesh_reproject_paint_mask(struct Mesh *target, struct Mesh *source);
void BKE_remesh_reproject_vertex_paint(struct Mesh *target, struct Mesh *source);
void BKE_remesh_reproject_sculpt_face_sets(struct Mesh *target, struct Mesh *source);
void BKE_remesh_reproject_materials(struct Mesh *target, struct Mesh *source);
#ifdef __cplusplus
}

View File

@ -403,6 +403,34 @@ void BKE_remesh_reproject_sculpt_face_sets(Mesh *target, Mesh *source)
free_bvhtree_from_mesh(&bvhtree);
}
void BKE_remesh_reproject_materials(Mesh *target, Mesh *source)
{
BVHTreeFromMesh bvhtree = {
.nearest_callback = NULL,
};
const MPoly *target_polys = CustomData_get_layer(&target->pdata, CD_MPOLY);
const MVert *target_verts = CustomData_get_layer(&target->vdata, CD_MVERT);
const MLoop *target_loops = CustomData_get_layer(&target->ldata, CD_MLOOP);
const MLoopTri *looptri = BKE_mesh_runtime_looptri_ensure(source);
BKE_bvhtree_from_mesh_get(&bvhtree, source, BVHTREE_FROM_LOOPTRI, 2);
for (int i = 0; i < target->totpoly; i++) {
float from_co[3];
BVHTreeNearest nearest;
nearest.index = -1;
nearest.dist_sq = FLT_MAX;
const MPoly *mpoly = &target_polys[i];
BKE_mesh_calc_poly_center(mpoly, &target_loops[mpoly->loopstart], target_verts, from_co);
BLI_bvhtree_find_nearest(bvhtree.tree, from_co, &nearest, bvhtree.nearest_callback, &bvhtree);
if (nearest.index != -1) {
target->mpoly[i].mat_nr = source->mpoly[looptri[nearest.index].poly].mat_nr;
}
}
free_bvhtree_from_mesh(&bvhtree);
}
void BKE_remesh_reproject_vertex_paint(Mesh *target, Mesh *source)
{
BVHTreeFromMesh bvhtree = {

View File

@ -3501,8 +3501,9 @@ void blo_do_versions_280(FileData *fd, Library *UNUSED(lib), Main *bmain)
}
for (Mesh *me = bmain->meshes.first; me; me = me->id.next) {
me->flag &= ~(ME_FLAG_UNUSED_0 | ME_FLAG_UNUSED_1 | ME_FLAG_UNUSED_3 | ME_FLAG_UNUSED_4 |
ME_FLAG_UNUSED_6 | ME_FLAG_UNUSED_7 | ME_REMESH_REPROJECT_VERTEX_COLORS);
me->flag &= ~(ME_REMESH_REPROJECT_MATERIALS | ME_FLAG_UNUSED_1 | ME_FLAG_UNUSED_3 |
ME_FLAG_UNUSED_4 | ME_FLAG_UNUSED_6 | ME_REMESH_REPROJECT_MATERIALS |
ME_REMESH_REPROJECT_VERTEX_COLORS);
}
for (Material *mat = bmain->materials.first; mat; mat = mat->id.next) {

View File

@ -162,7 +162,8 @@ static int voxel_remesh_exec(bContext *C, wmOperator *op)
}
if (mesh->flag & ME_REMESH_REPROJECT_VOLUME || mesh->flag & ME_REMESH_REPROJECT_PAINT_MASK ||
mesh->flag & ME_REMESH_REPROJECT_SCULPT_FACE_SETS) {
mesh->flag & ME_REMESH_REPROJECT_SCULPT_FACE_SETS ||
mesh->flag & ME_REMESH_REPROJECT_MATERIALS) {
BKE_mesh_runtime_clear_geometry(mesh);
}
@ -178,6 +179,10 @@ static int voxel_remesh_exec(bContext *C, wmOperator *op)
BKE_remesh_reproject_sculpt_face_sets(new_mesh, mesh);
}
if (mesh->flag & ME_REMESH_REPROJECT_MATERIALS) {
BKE_remesh_reproject_materials(new_mesh, mesh);
}
if (mesh->flag & ME_REMESH_REPROJECT_VERTEX_COLORS) {
BKE_mesh_runtime_clear_geometry(mesh);
BKE_remesh_reproject_vertex_paint(new_mesh, mesh);

View File

@ -288,7 +288,7 @@ enum {
ME_FLAG_UNUSED_4 = 1 << 4, /* cleared */
ME_AUTOSMOOTH = 1 << 5,
ME_FLAG_UNUSED_6 = 1 << 6, /* cleared */
ME_FLAG_UNUSED_7 = 1 << 7, /* cleared */
ME_REMESH_REPROJECT_MATERIALS = 1 << 7,
ME_REMESH_REPROJECT_VERTEX_COLORS = 1 << 8,
ME_DS_EXPAND = 1 << 9,
ME_SCULPT_DYNAMIC_TOPOLOGY = 1 << 10,

View File

@ -3265,6 +3265,12 @@ static void rna_def_mesh(BlenderRNA *brna)
prop, "Preserve Vertex Colors", "Keep the current vertex colors on the new mesh");
RNA_def_property_update(prop, 0, "rna_Mesh_update_draw");
prop = RNA_def_property(srna, "use_remesh_preserve_materials", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, NULL, "flag", ME_REMESH_REPROJECT_MATERIALS);
RNA_def_property_boolean_default(prop, false);
RNA_def_property_ui_text(prop, "Preserve Materials", "Keep the current material slots");
RNA_def_property_update(prop, 0, "rna_Mesh_update_draw");
prop = RNA_def_property(srna, "remesh_mode", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_sdna(prop, NULL, "remesh_mode");
RNA_def_property_enum_items(prop, rna_enum_mesh_remesh_mode_items);