From f1c0249f34c4171ec311b5b9882e36fed5889259 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Wed, 31 Aug 2022 09:09:01 -0500 Subject: 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 --- .../freestyle/intern/blender_interface/BlenderFileLoader.cpp | 7 ++++++- .../freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp | 6 ++++-- 2 files changed, 10 insertions(+), 3 deletions(-) (limited to 'source/blender/freestyle/intern/blender_interface') diff --git a/source/blender/freestyle/intern/blender_interface/BlenderFileLoader.cpp b/source/blender/freestyle/intern/blender_interface/BlenderFileLoader.cpp index ab357890096..f82d6f6164b 100644 --- a/source/blender/freestyle/intern/blender_interface/BlenderFileLoader.cpp +++ b/source/blender/freestyle/intern/blender_interface/BlenderFileLoader.cpp @@ -8,6 +8,7 @@ #include "BLI_utildefines.h" +#include "BKE_attribute.hh" #include "BKE_global.h" #include "BKE_object.h" @@ -497,12 +498,16 @@ void BlenderFileLoader::insertShapeNode(Object *ob, Mesh *me, int id) FrsMaterial tmpMat; + const blender::VArray material_indices = + blender::bke::mesh_attributes(*me).lookup_or_default( + "material_index", ATTR_DOMAIN_FACE, 0); + // We parse the vlak nodes again and import meshes while applying the clipping // by the near and far view planes. for (int a = 0; a < tottri; a++) { const MLoopTri *lt = &mlooptri[a]; const MPoly *mp = &mpoly[lt->poly]; - Material *mat = BKE_object_material_get(ob, mp->mat_nr + 1); + Material *mat = BKE_object_material_get(ob, material_indices[lt->poly] + 1); copy_v3_v3(v1, mvert[mloop[lt->tri[0]].v].co); copy_v3_v3(v2, mvert[mloop[lt->tri[1]].v].co); diff --git a/source/blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp b/source/blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp index 3df0d723aec..6365dfe26a7 100644 --- a/source/blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp +++ b/source/blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp @@ -584,7 +584,8 @@ void BlenderStrokeRenderer::GenerateStrokeMesh(StrokeGroup *group, bool hasTex) &mesh->pdata, CD_MPOLY, CD_SET_DEFAULT, nullptr, mesh->totpoly); mesh->mloop = (MLoop *)CustomData_add_layer( &mesh->ldata, CD_MLOOP, CD_SET_DEFAULT, nullptr, mesh->totloop); - + int *material_indices = (int *)CustomData_add_layer_named( + &mesh->pdata, CD_PROP_INT32, CD_SET_DEFAULT, nullptr, mesh->totpoly, "material_index"); MVert *vertices = mesh->mvert; MEdge *edges = mesh->medge; MPoly *polys = mesh->mpoly; @@ -714,7 +715,8 @@ void BlenderStrokeRenderer::GenerateStrokeMesh(StrokeGroup *group, bool hasTex) // poly polys->loopstart = loop_index; polys->totloop = 3; - polys->mat_nr = matnr; + *material_indices = matnr; + ++material_indices; ++polys; // Even and odd loops connect triangles vertices differently -- cgit v1.2.3