Fix T51296: UVs not working for hair emitted from vertices
authorLuca Rood <dev@lucarood.com>
Wed, 5 Jul 2017 10:23:42 +0000 (12:23 +0200)
committerLuca Rood <dev@lucarood.com>
Wed, 5 Jul 2017 13:48:54 +0000 (15:48 +0200)
source/blender/blenkernel/intern/particle.c
source/blender/blenkernel/intern/particle_distribute.c
source/blender/makesrna/intern/rna_particle.c

index 5cc8bc3fca56cbf4173d7413540b3cd20ac0ed35..e101bd649317475f411eee0bd6db84ef16fe8947 100644 (file)
@@ -3374,7 +3374,8 @@ void BKE_particlesettings_make_local(Main *bmain, ParticleSettings *part, const
 /*                     Textures                                                        */
 /************************************************/
 
-static int get_particle_uv(DerivedMesh *dm, ParticleData *pa, int face_index, const float fuv[4], char *name, float *texco)
+static int get_particle_uv(DerivedMesh *dm, ParticleData *pa, int index, const float fuv[4],
+                           char *name, float *texco, bool from_vert)
 {
        MFace *mf;
        MTFace *tf;
@@ -3390,11 +3391,15 @@ static int get_particle_uv(DerivedMesh *dm, ParticleData *pa, int face_index, co
 
        if (pa) {
                i = ELEM(pa->num_dmcache, DMCACHE_NOTFOUND, DMCACHE_ISCHILD) ? pa->num : pa->num_dmcache;
-               if (i >= dm->getNumTessFaces(dm))
+               if ((!from_vert && i >= dm->getNumTessFaces(dm)) ||
+                   (from_vert && i >= dm->getNumVerts(dm)))
+               {
                        i = -1;
+               }
+       }
+       else {
+               i = index;
        }
-       else
-               i = face_index;
 
        if (i == -1) {
                texco[0] = 0.0f;
@@ -3402,7 +3407,19 @@ static int get_particle_uv(DerivedMesh *dm, ParticleData *pa, int face_index, co
                texco[2] = 0.0f;
        }
        else {
-               mf = dm->getTessFaceData(dm, i, CD_MFACE);
+               if (from_vert) {
+                       mf = dm->getTessFaceDataArray(dm, CD_MFACE);
+
+                       for (int j = 0; j < dm->getNumTessFaces(dm); j++, mf++) {
+                               if (ELEM(i, mf->v1, mf->v2, mf->v3, mf->v4)) {
+                                       i = j;
+                                       break;
+                               }
+                       }
+               }
+               else {
+                       mf = dm->getTessFaceData(dm, i, CD_MFACE);
+               }
 
                psys_interpolate_uvs(&tf[i], mf->v4, fuv, texco);
 
@@ -3469,8 +3486,11 @@ static void get_cpa_texture(DerivedMesh *dm, ParticleSystem *psys, ParticleSetti
                                                mul_m4_v3(mtex->object->imat, texvec);
                                        break;
                                case TEXCO_UV:
-                                       if (fw && get_particle_uv(dm, NULL, face_index, fw, mtex->uvname, texvec))
+                                       if (fw && get_particle_uv(dm, NULL, face_index, fw, mtex->uvname,
+                                                                 texvec, (part->from == PART_FROM_VERT)))
+                                       {
                                                break;
+                                       }
                                        /* no break, failed to get uv's, so let's try orco's */
                                        ATTR_FALLTHROUGH;
                                case TEXCO_ORCO:
@@ -3542,8 +3562,11 @@ void psys_get_texture(ParticleSimulationData *sim, ParticleData *pa, ParticleTex
                                                mul_m4_v3(mtex->object->imat, texvec);
                                        break;
                                case TEXCO_UV:
-                                       if (get_particle_uv(sim->psmd->dm_final, pa, 0, pa->fuv, mtex->uvname, texvec))
+                                       if (get_particle_uv(sim->psmd->dm_final, pa, 0, pa->fuv, mtex->uvname,
+                                                           texvec, (part->from == PART_FROM_VERT)))
+                                       {
                                                break;
+                                       }
                                        /* no break, failed to get uv's, so let's try orco's */
                                        ATTR_FALLTHROUGH;
                                case TEXCO_ORCO:
index 6603521c42f87fccb8e7d09337f9385150e298e5..09da5fe72456bfb6e845e0137434062cdeb93e54 100644 (file)
@@ -427,12 +427,34 @@ static int distribute_binary_search(float *sum, int n, float value)
 static void distribute_from_verts_exec(ParticleTask *thread, ParticleData *pa, int p)
 {
        ParticleThreadContext *ctx= thread->ctx;
-       int rng_skip_tot= PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */
+       MFace *mface;
+
+       DM_ensure_tessface(ctx->dm);
+       mface = ctx->dm->getTessFaceDataArray(ctx->dm, CD_MFACE);
+
+       int rng_skip_tot = PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */
 
        /* TODO_PARTICLE - use original index */
-       pa->num= ctx->index[p];
-       pa->fuv[0] = 1.0f;
-       pa->fuv[1] = pa->fuv[2] = pa->fuv[3] = 0.0;
+       pa->num = ctx->index[p];
+
+       zero_v4(pa->fuv);
+
+       if (pa->num != DMCACHE_NOTFOUND && pa->num < ctx->dm->getNumVerts(ctx->dm)) {
+               for (int i = 0; i < ctx->dm->getNumTessFaces(ctx->dm); i++, mface++) {
+                       if (ELEM(pa->num, mface->v1, mface->v2, mface->v3, mface->v4)) {
+                               unsigned int *vert = &mface->v1;
+
+                               for (int j = 0; j < 4; j++, vert++) {
+                                       if (*vert == pa->num) {
+                                               pa->fuv[j] = 1.0f;
+                                               break;
+                                       }
+                               }
+
+                               break;
+                       }
+               }
+       }
        
 #if ONLY_WORKING_WITH_PA_VERTS
        if (ctx->tree) {
index a11b755c3c4a6165e74b263db19c7fea0b141b13..0dac797824d35b3fedbcc6a37791d40651ec12b0 100644 (file)
@@ -446,10 +446,12 @@ static int rna_ParticleSystem_tessfaceidx_on_emitter(ParticleSystem *particlesys
        int totpart;
        int totchild = 0;
        int totface;
+       int totvert;
        int num = -1;
 
        DM_ensure_tessface(modifier->dm_final); /* BMESH - UNTIL MODIFIER IS UPDATED FOR MPoly */
        totface = modifier->dm_final->getNumTessFaces(modifier->dm_final);
+       totvert = modifier->dm_final->getNumVerts(modifier->dm_final);
 
        /* 1. check that everything is ok & updated */
        if (!particlesystem || !totface) {
@@ -484,13 +486,26 @@ static int rna_ParticleSystem_tessfaceidx_on_emitter(ParticleSystem *particlesys
                                return num;
                        }
                }
+               else if (part->from == PART_FROM_VERT) {
+                       if (num != DMCACHE_NOTFOUND && num < totvert) {
+                               MFace *mface = modifier->dm_final->getTessFaceDataArray(modifier->dm_final, CD_MFACE);
+
+                               *r_fuv = &particle->fuv;
+
+                               for (int i = 0; i < totface; i++, mface++) {
+                                       if (ELEM(num, mface->v1, mface->v2, mface->v3, mface->v4)) {
+                                               return i;
+                                       }
+                               }
+                       }
+               }
        }
        else {
                ChildParticle *cpa = particlesystem->child + particle_no - totpart;
                num = cpa->num;
 
                if (part->childtype == PART_CHILD_FACES) {
-                       if (ELEM(part->from, PART_FROM_FACE, PART_FROM_VOLUME)) {
+                       if (ELEM(part->from, PART_FROM_FACE, PART_FROM_VOLUME, PART_FROM_VERT)) {
                                if (num != DMCACHE_NOTFOUND && num < totface) {
                                        *r_fuv = &cpa->fuv;
                                        return num;
@@ -510,6 +525,19 @@ static int rna_ParticleSystem_tessfaceidx_on_emitter(ParticleSystem *particlesys
                                        return num;
                                }
                        }
+                       else if (part->from == PART_FROM_VERT) {
+                               if (num != DMCACHE_NOTFOUND && num < totvert) {
+                                       MFace *mface = modifier->dm_final->getTessFaceDataArray(modifier->dm_final, CD_MFACE);
+
+                                       *r_fuv = &parent->fuv;
+
+                                       for (int i = 0; i < totface; i++, mface++) {
+                                               if (ELEM(num, mface->v1, mface->v2, mface->v3, mface->v4)) {
+                                                       return i;
+                                               }
+                                       }
+                               }
+                       }
                }
        }