diff options
author | Hans Goudey <h.goudey@me.com> | 2022-08-31 17:09:01 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2022-08-31 17:09:01 +0300 |
commit | f1c0249f34c4171ec311b5b9882e36fed5889259 (patch) | |
tree | 7dd630c9301b8dd468c8279e8c446c62824fb6d6 /source/blender/blenkernel/intern/mesh_boolean_convert.cc | |
parent | 3e73afb5360592fe2bf43419e216035ea3c281f9 (diff) |
Mesh: Move material indices to a generic attribute
This patch moves material indices from the mesh `MPoly` struct to a
generic integer attribute. The builtin material index was already
exposed in geometry nodes, but this makes it a "proper" attribute
accessible with Python and visible in the "Attributes" panel.
The goals of the refactor are code simplification and memory and
performance improvements, mainly because the attribute doesn't have
to be stored and processed if there are no materials. However, until
4.0, material indices will still be read and written in the old
format, meaning there may be a temporary increase in memory usage.
Further notes:
* Completely removing the `MPoly.mat_nr` after 4.0 may require
changes to DNA or introducing a new `MPoly` type.
* Geometry nodes regression tests didn't look at material indices,
so the change reveals a bug in the realize instances node that I fixed.
* Access to material indices from the RNA `MeshPolygon` type is slower
with this patch. The `material_index` attribute can be used instead.
* Cycles is changed to read from the attribute instead.
* BMesh isn't changed in this patch. Theoretically it could be though,
to save 2 bytes per face when less than two materials are used.
* Eventually we could use a 16 bit integer attribute type instead.
Ref T95967
Differential Revision: https://developer.blender.org/D15675
Diffstat (limited to 'source/blender/blenkernel/intern/mesh_boolean_convert.cc')
-rw-r--r-- | source/blender/blenkernel/intern/mesh_boolean_convert.cc | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/source/blender/blenkernel/intern/mesh_boolean_convert.cc b/source/blender/blenkernel/intern/mesh_boolean_convert.cc index 903198e249a..fb4a9248d8d 100644 --- a/source/blender/blenkernel/intern/mesh_boolean_convert.cc +++ b/source/blender/blenkernel/intern/mesh_boolean_convert.cc @@ -9,6 +9,7 @@ #include "DNA_meshdata_types.h" #include "DNA_object_types.h" +#include "BKE_attribute.hh" #include "BKE_customdata.h" #include "BKE_material.h" #include "BKE_mesh.h" @@ -23,6 +24,7 @@ #include "BLI_mesh_intersect.hh" #include "BLI_span.hh" #include "BLI_task.hh" +#include "BLI_virtual_array.hh" namespace blender::meshintersect { @@ -405,13 +407,17 @@ static void copy_poly_attributes(Mesh *dest_mesh, const Mesh *orig_me, int mp_index, int index_in_orig_me, - Span<short> material_remap) + Span<short> material_remap, + MutableSpan<int> dst_material_indices) { - if (material_remap.size() > 0 && material_remap.index_range().contains(orig_mp->mat_nr)) { - mp->mat_nr = material_remap[orig_mp->mat_nr]; + const VArray<int> src_material_indices = bke::mesh_attributes(*orig_me).lookup_or_default<int>( + "material_index", ATTR_DOMAIN_FACE, 0); + const int src_index = src_material_indices[index_in_orig_me]; + if (material_remap.size() > 0 && material_remap.index_range().contains(src_index)) { + dst_material_indices[mp_index] = material_remap[src_index]; } else { - mp->mat_nr = orig_mp->mat_nr; + dst_material_indices[mp_index] = src_index; } mp->flag = orig_mp->flag; @@ -722,6 +728,9 @@ static Mesh *imesh_to_mesh(IMesh *im, MeshesToIMeshInfo &mim) /* Set the loopstart and totloop for each output poly, * and set the vertices in the appropriate loops. */ + bke::SpanAttributeWriter<int> dst_material_indices = + bke::mesh_attributes_for_write(*result).lookup_or_add_for_write_only_span<int>( + "material_index", ATTR_DOMAIN_FACE); int cur_loop_index = 0; MLoop *l = result->mloop; for (int fi : im->face_index_range()) { @@ -750,9 +759,11 @@ static Mesh *imesh_to_mesh(IMesh *im, MeshesToIMeshInfo &mim) index_in_orig_me, (mim.material_remaps.size() > 0) ? mim.material_remaps[orig_me_index].as_span() : - Span<short>()); + Span<short>(), + dst_material_indices.span); copy_or_interp_loop_attributes(result, f, mp, orig_mp, orig_me, orig_me_index, mim); } + dst_material_indices.finish(); /* BKE_mesh_calc_edges will calculate and populate all the * MEdges from the MPolys. */ |