diff options
author | Brecht Van Lommel <brecht@blender.org> | 2022-11-09 18:59:16 +0300 |
---|---|---|
committer | Brecht Van Lommel <brecht@blender.org> | 2022-11-09 20:13:03 +0300 |
commit | 1fbb1d8cf6ea9badd3361e4c533625f569d14734 (patch) | |
tree | 60e461e1c61309b28041c38af8499f693bc39340 /source/blender/render/intern/bake.c | |
parent | 11f6c65e61a22ea9423962d6165f54ec29e221a8 (diff) |
Fix T102214: inconsistenty between bake and render with invalid material index
When the materal slot index on mesh faces exceeds the number of slots, rendering
would use the last material slot while other operations like baking would fall
back to the default material.
Now consistently use the last material slot in such cases, since preserving
backwards compatibility for rendering seems most important. And if there is
one material slot, it's more useful to use that one rather than falling back
to the default material.
Diffstat (limited to 'source/blender/render/intern/bake.c')
-rw-r--r-- | source/blender/render/intern/bake.c | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/source/blender/render/intern/bake.c b/source/blender/render/intern/bake.c index 9b56e18bb26..d9f7f9fa0af 100644 --- a/source/blender/render/intern/bake.c +++ b/source/blender/render/intern/bake.c @@ -747,6 +747,7 @@ void RE_bake_pixels_populate(Mesh *me, BKE_mesh_recalc_looptri(loops, polys, verts, me->totloop, me->totpoly, looptri); const int *material_indices = BKE_mesh_material_indices(me); + const int materials_num = targets->materials_num; for (int i = 0; i < tottri; i++) { const MLoopTri *lt = &looptri[i]; @@ -754,7 +755,10 @@ void RE_bake_pixels_populate(Mesh *me, bd.primitive_id = i; /* Find images matching this material. */ - Image *image = targets->material_to_image[material_indices ? material_indices[lt->poly] : 0]; + const int material_index = (material_indices && materials_num) ? + clamp_i(material_indices[lt->poly], 0, materials_num - 1) : + 0; + Image *image = targets->material_to_image[material_index]; for (int image_id = 0; image_id < targets->images_num; image_id++) { BakeImage *bk_image = &targets->images[image_id]; if (bk_image->image != image) { |