Fix T57620: display custom normals in Edit Mode.

Since it seems that CD_ORIGINDEX is not available for loops,
the only choice is to simply use the loop normals already
computed by depsgraph after evaluating modifiers.

This revealed a bug where the Auto Smooth settings would be lost
from the mesh after complex modifiers, or after edit mesh to mesh
conversion, so restoring them is needed to get correct results.
This commit is contained in:
Alexander Gavrilov 2018-12-04 19:52:30 +03:00
parent 7e5f31be41
commit 34b73cb11c
Notes: blender-bot 2023-02-14 08:06:33 +01:00
Referenced by issue #57620, Loop normals display not working if have custom split normals
2 changed files with 26 additions and 15 deletions

View File

@ -1151,6 +1151,14 @@ static void add_shapekey_layers(Mesh *me_dst, Mesh *me_src, Object *UNUSED(ob))
}
}
static void mesh_copy_autosmooth(Mesh *me, Mesh *me_orig)
{
if (me_orig->flag & ME_AUTOSMOOTH) {
me->flag |= ME_AUTOSMOOTH;
me->smoothresh = me_orig->smoothresh;
}
}
static void mesh_calc_modifiers(
struct Depsgraph *depsgraph, Scene *scene, Object *ob, float (*inputVertexCos)[3],
int useDeform,
@ -1460,6 +1468,8 @@ static void mesh_calc_modifiers(
}
deformedVerts = NULL;
}
mesh_copy_autosmooth(me, ob->data);
}
/* create an orco mesh in parallel */
@ -1693,6 +1703,7 @@ static void editbmesh_calc_modifiers(
if (r_cage && cageIndex == -1) {
*r_cage = BKE_mesh_from_editmesh_with_coords_thin_wrap(em, dataMask, NULL);
mesh_copy_autosmooth(*r_cage, ob->data);
}
md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
@ -1769,6 +1780,8 @@ static void editbmesh_calc_modifiers(
me = BKE_mesh_from_bmesh_for_eval_nomain(em->bm, 0);
ASSERT_IS_VALID_MESH(me);
mesh_copy_autosmooth(me, ob->data);
if (deformedVerts) {
BKE_mesh_apply_vert_coords(me, deformedVerts);
}
@ -1822,6 +1835,8 @@ static void editbmesh_calc_modifiers(
MEM_freeN(deformedVerts);
deformedVerts = NULL;
}
mesh_copy_autosmooth(me, ob->data);
}
me->runtime.deformed_only = false;
}
@ -1843,6 +1858,7 @@ static void editbmesh_calc_modifiers(
*r_cage = BKE_mesh_from_editmesh_with_coords_thin_wrap(
em, mask,
deformedVerts ? MEM_dupallocN(deformedVerts) : NULL);
mesh_copy_autosmooth(*r_cage, ob->data);
}
}
}
@ -1878,6 +1894,8 @@ static void editbmesh_calc_modifiers(
*r_final = BKE_mesh_from_editmesh_with_coords_thin_wrap(em, dataMask, deformedVerts);
deformedVerts = NULL;
mesh_copy_autosmooth(*r_final, ob->data);
/* In this case, we should never have weight-modifying modifiers in stack... */
if (do_init_statvis) {
editmesh_update_statvis_color(scene, ob);
@ -1916,6 +1934,10 @@ static void editbmesh_calc_modifiers(
if (!do_loop_normals) {
BKE_mesh_ensure_normals_for_display(*r_final);
if (r_cage && *r_cage && (*r_cage != *r_final)) {
BKE_mesh_ensure_normals_for_display(*r_cage);
}
/* Some modifiers, like datatransfer, may generate those data, we do not want to keep them,
* as they are used by display code when available (i.e. even if autosmooth is disabled). */
if (CustomData_has_layer(&(*r_final)->ldata, CD_NORMAL)) {

View File

@ -72,7 +72,6 @@
#include "draw_cache_impl.h" /* own include */
// #define USE_BM_MAPPED_LOOPNORMAL
static void mesh_batch_cache_clear(Mesh *me);
@ -492,7 +491,6 @@ static MeshRenderData *mesh_render_data_create_ex(
}
if (types & MR_DATATYPE_LOOP) {
int totloop = bm->totloop;
#ifdef USE_BM_MAPPED_LOOPNORMAL
if (is_auto_smooth) {
rdata->loop_normals = MEM_mallocN(sizeof(*rdata->loop_normals) * totloop, __func__);
int cd_loop_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL);
@ -500,7 +498,6 @@ static MeshRenderData *mesh_render_data_create_ex(
bm, NULL, NULL, NULL, true, split_angle, rdata->loop_normals, NULL, NULL,
cd_loop_clnors_offset, false);
}
#endif
rdata->loop_len = totloop;
bm_ensure_types |= BM_LOOP;
}
@ -1746,7 +1743,7 @@ static void add_overlay_tri(
static void add_overlay_tri_mapped(
MeshRenderData *rdata, GPUVertBuf *vbo_pos, GPUVertBuf *vbo_nor, GPUVertBuf *vbo_data, GPUIndexBufBuilder *elb,
const uint pos_id, const uint vnor_id, const uint lnor_id, const uint data_id,
BMFace *efa, const MLoopTri *mlt, const float poly_normal[3], const int base_vert_idx)
BMFace *efa, const MLoopTri *mlt, const float poly_normal[3], const float (*loop_normals)[3], const int base_vert_idx)
{
BMEditMesh *embm = rdata->edit_bmesh;
BMesh *bm = embm->bm;
@ -1791,17 +1788,8 @@ static void add_overlay_tri_mapped(
}
if (vbo_nor) {
#ifdef USE_BM_MAPPED_LOOPNORMAL
float (*lnors)[3] = rdata->loop_normals;
#endif
for (uint i = 0; i < 3; i++) {
/* We don't have 'l_origindex', so use the polygons normal. */
#ifdef USE_BM_MAPPED_LOOPNORMAL
const int l_orig = l_origindex[mlt->tri[i]];
const float *nor = (lnors && (l_orig != ORIGINDEX_NONE)) ? lnors[l_orig] : poly_normal;
#else
const float *nor = poly_normal;
#endif
const float *nor = loop_normals ? loop_normals[mlt->tri[i]] : poly_normal;
GPUPackedNormal lnor = GPU_normal_convert_i10_v3(nor);
GPU_vertbuf_attr_set(vbo_nor, lnor_id, base_vert_idx + i, &lnor);
GPUPackedNormal vnor = GPU_normal_convert_i10_s3(mvert[mloop[mlt->tri[i]].v].no);
@ -3789,6 +3777,7 @@ static void mesh_batch_cache_create_overlay_tri_buffers(
BKE_mesh_ensure_normals_for_display(me_cage);
}
const float (*polynors)[3] = CustomData_get_layer(&me_cage->pdata, CD_NORMAL);
const float (*loopnors)[3] = CustomData_get_layer(&me_cage->ldata, CD_NORMAL);
for (int i = 0; i < tri_len; i++) {
const MLoopTri *mlt = &mlooptri[i];
const int p_orig = rdata->mapped.p_origindex[mlt->poly];
@ -3798,7 +3787,7 @@ static void mesh_batch_cache_create_overlay_tri_buffers(
add_overlay_tri_mapped(
rdata, vbo_pos, vbo_nor, vbo_data, elbp,
attr_id.pos, attr_id.vnor, attr_id.lnor, attr_id.data,
efa, mlt, polynors[mlt->poly], vbo_len_used);
efa, mlt, polynors[mlt->poly], loopnors, vbo_len_used);
vbo_len_used += 3;
}
}