Page MenuHome
Paste P1032

Fix T66645 - picking vertex color through the CPU
ActivePublic

Authored by Germano Cavalcante (mano-wii) on Jul 10 2019, 3:42 PM.
diff --git a/source/blender/editors/sculpt_paint/paint_utils.c b/source/blender/editors/sculpt_paint/paint_utils.c
index c8ad1b5781d..16b36b934ab 100644
--- a/source/blender/editors/sculpt_paint/paint_utils.c
+++ b/source/blender/editors/sculpt_paint/paint_utils.c
@@ -484,7 +484,7 @@ void paint_sample_color(
palette->active_color = BLI_listbase_count(&palette->colors) - 1;
}
- if (CTX_wm_view3d(C) && texpaint_proj) {
+ if (CTX_wm_view3d(C)) {
/* first try getting a color directly from the mesh faces if possible */
ViewLayer *view_layer = CTX_data_view_layer(C);
Object *ob = OBACT(view_layer);
@@ -504,12 +504,68 @@ void paint_sample_color(
unsigned int faceindex;
unsigned int totpoly = me->totpoly;
- if (CustomData_has_layer(&me_eval->ldata, CD_MLOOPUV)) {
- ED_view3d_viewcontext_init(C, &vc);
-
- view3d_operator_needs_opengl(C);
-
- if (imapaint_pick_face(&vc, mval, &faceindex, totpoly)) {
+ ED_view3d_viewcontext_init(C, &vc);
+ view3d_operator_needs_opengl(C);
+
+ if (imapaint_pick_face(&vc, mval, &faceindex, totpoly)) {
+ if (!texpaint_proj && CustomData_has_layer(&me_eval->ldata, CD_MLOOPCOL)) {
+ float matrix[4][4], proj[4][4];
+ GLint view[4];
+
+ const MLoopTri *looptri = BKE_mesh_runtime_looptri_ensure(me_eval);
+ const int tottri = me_eval->runtime.looptris.len;
+
+ const MVert *mvert = me_eval->mvert;
+ const MPoly *mpoly = me_eval->mpoly;
+ const MLoop *mloop = me_eval->mloop;
+ struct MLoopCol *mloopcol = me_eval->mloopcol;
+
+ /* get the needed opengl matrices */
+ GPU_viewport_size_get_i(view);
+ GPU_matrix_model_view_get(matrix);
+ GPU_matrix_projection_get(proj);
+ view[0] = view[1] = 0;
+ mul_m4_m4m4(matrix, matrix, ob_eval->obmat);
+ mul_m4_m4m4(matrix, proj, matrix);
+
+ /* loop over all looptri's for a given polygon: i */
+ MPoly *mp = &mpoly[faceindex];
+ MLoopTri *lt = &looptri[poly_to_tri_count(faceindex, mp->loopstart)];
+ int j, lt_tot = ME_POLY_TRI_TOT(mp);
+
+ float p[2], w[3], absw, minabsw = 1e10;
+ for (j = 0; j < lt_tot; j++, lt++) {
+ unsigned int vtri[3] = {
+ mloop[lt->tri[0]].v,
+ mloop[lt->tri[1]].v,
+ mloop[lt->tri[2]].v,
+ };
+ float tri_co[3][3];
+ copy_v3_v3(tri_co[0], mvert[vtri[0]].co);
+ copy_v3_v3(tri_co[1], mvert[vtri[1]].co);
+ copy_v3_v3(tri_co[2], mvert[vtri[2]].co);
+
+ p[0] = mval[0];
+ p[1] = mval[1];
+ imapaint_tri_weights(matrix, view, UNPACK3(tri_co), p, w);
+ absw = fabsf(w[0]) + fabsf(w[1]) + fabsf(w[2]);
+ if (absw < minabsw) {
+ float col1[4], col2[4], col3[4];
+ rgba_uchar_to_float(col1, (char *)&mloopcol[lt->tri[0]]);
+ rgba_uchar_to_float(col2, (char *)&mloopcol[lt->tri[1]]);
+ rgba_uchar_to_float(col3, (char *)&mloopcol[lt->tri[2]]);
+ mul_v4_fl(col1, w[0]);
+ mul_v4_fl(col2, w[1]);
+ mul_v4_fl(col3, w[2]);
+ add_v4_v4(col1, col2);
+ add_v4_v4(col1, col3);
+ rgba_float_to_uchar((char *)&col, col1);
+
+ minabsw = absw;
+ }
+ };
+ }
+ else if (CustomData_has_layer(&me_eval->ldata, CD_MLOOPUV)) {
Image *image;
if (use_material) {