Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / effect.c
index b0b9372b74c5ba7871372220b06083b617b12e2c..af5089f742fe45512e11fa3e81bab27de8482842 100644 (file)
@@ -43,7 +43,6 @@
 #include "DNA_meshdata_types.h"
 #include "DNA_object_types.h"
 #include "DNA_object_force.h"
-#include "DNA_particle_types.h"
 #include "DNA_texture_types.h"
 #include "DNA_scene_types.h"
 
@@ -64,9 +63,9 @@
 #include "BKE_cdderivedmesh.h"
 #include "BKE_effect.h"
 #include "BKE_global.h"
+#include "BKE_library.h"
 #include "BKE_modifier.h"
 #include "BKE_object.h"
-#include "BKE_particle.h"
 #include "BKE_scene.h"
 #include "BKE_smoke.h"
 
@@ -139,7 +138,7 @@ void free_partdeflect(PartDeflect *pd)
                return;
 
        if (pd->tex)
-               pd->tex->id.us--;
+               id_us_min(&pd->tex->id);
 
        if (pd->rng)
                BLI_rng_free(pd->rng);
@@ -147,12 +146,11 @@ void free_partdeflect(PartDeflect *pd)
        MEM_freeN(pd);
 }
 
-static EffectorCache *new_effector_cache(Scene *scene, Object *ob, ParticleSystem *psys, PartDeflect *pd)
+static EffectorCache *new_effector_cache(Scene *scene, Object *ob, PartDeflect *pd)
 {
        EffectorCache *eff = MEM_callocN(sizeof(EffectorCache), "EffectorCache");
        eff->scene = scene;
        eff->ob = ob;
-       eff->psys = psys;
        eff->pd = pd;
        eff->frame = -1;
        return eff;
@@ -170,40 +168,16 @@ static void add_object_to_effectors(ListBase **effectors, Scene *scene, Effector
        if (*effectors == NULL)
                *effectors = MEM_callocN(sizeof(ListBase), "effectors list");
 
-       eff = new_effector_cache(scene, ob, NULL, ob->pd);
+       eff = new_effector_cache(scene, ob, ob->pd);
 
        /* make sure imat is up to date */
        invert_m4_m4(ob->imat, ob->obmat);
 
        BLI_addtail(*effectors, eff);
 }
-static void add_particles_to_effectors(ListBase **effectors, Scene *scene, EffectorWeights *weights, Object *ob, ParticleSystem *psys, ParticleSystem *psys_src)
-{
-       ParticleSettings *part= psys->part;
-
-       if ( !psys_check_enabled(ob, psys) )
-               return;
-
-       if ( psys == psys_src && (part->flag & PART_SELF_EFFECT) == 0)
-               return;
-
-       if ( part->pd && part->pd->forcefield && weights->weight[part->pd->forcefield] != 0.0f) {
-               if (*effectors == NULL)
-                       *effectors = MEM_callocN(sizeof(ListBase), "effectors list");
-
-               BLI_addtail(*effectors, new_effector_cache(scene, ob, psys, part->pd));
-       }
-
-       if (part->pd2 && part->pd2->forcefield && weights->weight[part->pd2->forcefield] != 0.0f) {
-               if (*effectors == NULL)
-                       *effectors = MEM_callocN(sizeof(ListBase), "effectors list");
-
-               BLI_addtail(*effectors, new_effector_cache(scene, ob, psys, part->pd2));
-       }
-}
 
 /* returns ListBase handle with objects taking part in the effecting */
-ListBase *pdInitEffectors(Scene *scene, Object *ob_src, ParticleSystem *psys_src,
+ListBase *pdInitEffectors(Scene *scene, Object *ob_src,
                           EffectorWeights *weights, bool precalc)
 {
        Base *base;
@@ -217,13 +191,6 @@ ListBase *pdInitEffectors(Scene *scene, Object *ob_src, ParticleSystem *psys_src
                        if ( (go->ob->lay & layer) ) {
                                if ( go->ob->pd && go->ob->pd->forcefield )
                                        add_object_to_effectors(&effectors, scene, weights, go->ob, ob_src);
-
-                               if ( go->ob->particlesystem.first ) {
-                                       ParticleSystem *psys= go->ob->particlesystem.first;
-
-                                       for ( ; psys; psys=psys->next )
-                                               add_particles_to_effectors(&effectors, scene, weights, go->ob, psys, psys_src);
-                               }
                        }
                }
        }
@@ -232,13 +199,6 @@ ListBase *pdInitEffectors(Scene *scene, Object *ob_src, ParticleSystem *psys_src
                        if ( (base->lay & layer) ) {
                                if ( base->object->pd && base->object->pd->forcefield )
                                        add_object_to_effectors(&effectors, scene, weights, base->object, ob_src);
-
-                               if ( base->object->particlesystem.first ) {
-                                       ParticleSystem *psys= base->object->particlesystem.first;
-
-                                       for ( ; psys; psys=psys->next )
-                                               add_particles_to_effectors(&effectors, scene, weights, base->object, psys, psys_src);
-                               }
                        }
                }
        }
@@ -291,8 +251,6 @@ static void precalculate_effector(EffectorCache *eff)
                if (eff->ob->type == OB_CURVE)
                        eff->flag |= PE_USE_NORMAL_DATA;
        }
-       else if (eff->psys)
-               psys_update_particle_tree(eff->psys, eff->scene->r.cfra);
 
        /* Store object velocity */
        if (eff->ob) {
@@ -315,36 +273,6 @@ void pdPrecalculateEffectors(ListBase *effectors)
 }
 
 
-void pd_point_from_particle(ParticleSimulationData *sim, ParticleData *pa, ParticleKey *state, EffectedPoint *point)
-{
-       ParticleSettings *part = sim->psys->part;
-       point->loc = state->co;
-       point->vel = state->vel;
-       point->index = pa - sim->psys->particles;
-       point->size = pa->size;
-       point->charge = 0.0f;
-       
-       if (part->pd && part->pd->forcefield == PFIELD_CHARGE)
-               point->charge += part->pd->f_strength;
-
-       if (part->pd2 && part->pd2->forcefield == PFIELD_CHARGE)
-               point->charge += part->pd2->f_strength;
-
-       point->vel_to_sec = 1.0f;
-       point->vel_to_frame = psys_get_timestep(sim);
-
-       point->flag = 0;
-
-       if (sim->psys->part->flag & PART_ROT_DYN) {
-               point->ave = state->ave;
-               point->rot = state->rot;
-       }
-       else
-               point->ave = point->rot = NULL;
-
-       point->psys = sim->psys;
-}
-
 void pd_point_from_loc(Scene *scene, float *loc, float *vel, int index, EffectedPoint *point)
 {
        point->loc = loc;
@@ -358,7 +286,6 @@ void pd_point_from_loc(Scene *scene, float *loc, float *vel, int index, Effected
        point->flag = 0;
 
        point->ave = point->rot = NULL;
-       point->psys = NULL;
 }
 void pd_point_from_soft(Scene *scene, float *loc, float *vel, int index, EffectedPoint *point)
 {
@@ -373,8 +300,6 @@ void pd_point_from_soft(Scene *scene, float *loc, float *vel, int index, Effecte
        point->flag = PE_WIND_AS_SPEED;
 
        point->ave = point->rot = NULL;
-
-       point->psys = NULL;
 }
 /************************************************/
 /*                     Effectors               */
@@ -391,6 +316,7 @@ static void eff_tri_ray_hit(void *UNUSED(userData), int UNUSED(index), const BVH
 // get visibility of a wind ray
 static float eff_calc_visibility(ListBase *colliders, EffectorCache *eff, EffectorData *efd, EffectedPoint *point)
 {
+       const int raycast_flag = BVH_RAYCAST_DEFAULT & ~(BVH_RAYCAST_WATERTIGHT);
        ListBase *colls = colliders;
        ColliderCache *col;
        float norm[3], len = 0.0;
@@ -422,7 +348,10 @@ static float eff_calc_visibility(ListBase *colliders, EffectorCache *eff, Effect
                        hit.dist = len + FLT_EPSILON;
 
                        /* check if the way is blocked */
-                       if (BLI_bvhtree_ray_cast(collmd->bvhtree, point->loc, norm, 0.0f, &hit, eff_tri_ray_hit, NULL)>=0) {
+                       if (BLI_bvhtree_ray_cast_ex(
+                               collmd->bvhtree, point->loc, norm, 0.0f, &hit,
+                               eff_tri_ray_hit, NULL, raycast_flag) != -1)
+                       {
                                absorption= col->ob->pd->absorption;
 
                                /* visibility is only between 0 and 1, calculated from 1-absorption */
@@ -483,7 +412,7 @@ static float falloff_func_rad(PartDeflect *pd, float fac)
        return falloff_func(fac, pd->flag&PFIELD_USEMINR, pd->minrad, pd->flag&PFIELD_USEMAXR, pd->maxrad, pd->f_power_r);
 }
 
-float effector_falloff(EffectorCache *eff, EffectorData *efd, EffectedPoint *UNUSED(point), EffectorWeights *weights)
+static float effector_falloff(EffectorCache *eff, EffectorData *efd, EffectedPoint *UNUSED(point), EffectorWeights *weights)
 {
        float temp[3];
        float falloff = weights ? weights->weight[0] * weights->weight[eff->pd->forcefield] : 1.0f;
@@ -506,7 +435,7 @@ float effector_falloff(EffectorCache *eff, EffectorData *efd, EffectedPoint *UNU
                        if (falloff == 0.0f)
                                break;
 
-                       madd_v3_v3v3fl(temp, efd->vec_to_point, efd->nor, -fac);
+                       madd_v3_v3v3fl(temp, efd->vec_to_point2, efd->nor, -fac);
                        r_fac= len_v3(temp);
                        falloff*= falloff_func_rad(eff->pd, r_fac);
                        break;
@@ -558,7 +487,6 @@ int closest_point_on_surface(SurfaceModifierData *surmd, const float co[3], floa
 }
 int get_effector_data(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, int real_velocity)
 {
-       float cfra = eff->scene->r.cfra;
        int ret = 0;
 
        if (eff->pd && eff->pd->shape==PFIELD_SHAPE_SURFACE && eff->surmd) {
@@ -593,43 +521,6 @@ int get_effector_data(EffectorCache *eff, EffectorData *efd, EffectedPoint *poin
                        ret = 1;
                }
        }
-       else if (eff->psys) {
-               ParticleData *pa = eff->psys->particles + *efd->index;
-               ParticleKey state;
-
-               /* exclude the particle itself for self effecting particles */
-               if (eff->psys == point->psys && *efd->index == point->index) {
-                       /* pass */
-               }
-               else {
-                       ParticleSimulationData sim= {NULL};
-                       sim.scene= eff->scene;
-                       sim.ob= eff->ob;
-                       sim.psys= eff->psys;
-
-                       /* TODO: time from actual previous calculated frame (step might not be 1) */
-                       state.time = cfra - 1.0f;
-                       ret = psys_get_particle_state(&sim, *efd->index, &state, 0);
-
-                       /* TODO */
-                       //if (eff->pd->forcefiled == PFIELD_HARMONIC && ret==0) {
-                       //      if (pa->dietime < eff->psys->cfra)
-                       //              eff->flag |= PE_VELOCITY_TO_IMPULSE;
-                       //}
-
-                       copy_v3_v3(efd->loc, state.co);
-
-                       /* rather than use the velocity use rotated x-axis (defaults to velocity) */
-                       efd->nor[0] = 1.f;
-                       efd->nor[1] = efd->nor[2] = 0.f;
-                       mul_qt_v3(state.rot, efd->nor);
-               
-                       if (real_velocity)
-                               copy_v3_v3(efd->vel, state.vel);
-
-                       efd->size = pa->size;
-               }
-       }
        else {
                /* use center of object for distance calculus */
                const Object *ob = eff->ob;
@@ -681,12 +572,12 @@ int get_effector_data(EffectorCache *eff, EffectorData *efd, EffectedPoint *poin
 
        return ret;
 }
-static void get_effector_tot(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, int *tot, int *p, int *step)
+static void get_effector_tot(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, int *tot, int *p)
 {
-       if (eff->pd->shape == PFIELD_SHAPE_POINTS) {
-               efd->index = p;
+       *p = 0;
+       efd->index = p;
 
-               *p = 0;
+       if (eff->pd->shape == PFIELD_SHAPE_POINTS) {
                *tot = eff->ob->derivedFinal ? eff->ob->derivedFinal->numVertData : 1;
 
                if (*tot && eff->pd->forcefield == PFIELD_HARMONIC && point->index >= 0) {
@@ -694,36 +585,7 @@ static void get_effector_tot(EffectorCache *eff, EffectorData *efd, EffectedPoin
                        *tot = *p+1;
                }
        }
-       else if (eff->psys) {
-               efd->index = p;
-
-               *p = 0;
-               *tot = eff->psys->totpart;
-               
-               if (eff->pd->forcefield == PFIELD_CHARGE) {
-                       /* Only the charge of the effected particle is used for 
-                        * interaction, not fall-offs. If the fall-offs aren't the
-                        * same this will be unphysical, but for animation this
-                        * could be the wanted behavior. If you want physical
-                        * correctness the fall-off should be spherical 2.0 anyways.
-                        */
-                       efd->charge = eff->pd->f_strength;
-               }
-               else if (eff->pd->forcefield == PFIELD_HARMONIC && (eff->pd->flag & PFIELD_MULTIPLE_SPRINGS)==0) {
-                       /* every particle is mapped to only one harmonic effector particle */
-                       *p= point->index % eff->psys->totpart;
-                       *tot= *p + 1;
-               }
-
-               if (eff->psys->part->effector_amount) {
-                       int totpart = eff->psys->totpart;
-                       int amount = eff->psys->part->effector_amount;
-
-                       *step = (totpart > amount) ? totpart/amount : 1;
-               }
-       }
        else {
-               *p = 0;
                *tot = 1;
        }
 }
@@ -745,18 +607,20 @@ static void do_texture_effector(EffectorCache *eff, EffectorData *efd, EffectedP
 
        copy_v3_v3(tex_co, point->loc);
 
-       if (eff->pd->flag & PFIELD_TEX_2D) {
-               float fac=-dot_v3v3(tex_co, efd->nor);
-               madd_v3_v3fl(tex_co, efd->nor, fac);
-       }
-
        if (eff->pd->flag & PFIELD_TEX_OBJECT) {
                mul_m4_v3(eff->ob->imat, tex_co);
+
+               if (eff->pd->flag & PFIELD_TEX_2D)
+                       tex_co[2] = 0.0f;
+       }
+       else if (eff->pd->flag & PFIELD_TEX_2D) {
+               float fac=-dot_v3v3(tex_co, efd->nor);
+               madd_v3_v3fl(tex_co, efd->nor, fac);
        }
 
        scene_color_manage = BKE_scene_check_color_management_enabled(eff->scene);
 
-       hasrgb = multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result, NULL, scene_color_manage, false);
+       hasrgb = multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result, 0, NULL, scene_color_manage, false);
 
        if (hasrgb && mode==PFIELD_TEX_RGB) {
                force[0] = (0.5f - result->tr) * strength;
@@ -767,15 +631,15 @@ static void do_texture_effector(EffectorCache *eff, EffectorData *efd, EffectedP
                strength/=nabla;
 
                tex_co[0] += nabla;
-               multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+1, NULL, scene_color_manage, false);
+               multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+1, 0, NULL, scene_color_manage, false);
 
                tex_co[0] -= nabla;
                tex_co[1] += nabla;
-               multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+2, NULL, scene_color_manage, false);
+               multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+2, 0, NULL, scene_color_manage, false);
 
                tex_co[1] -= nabla;
                tex_co[2] += nabla;
-               multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+3, NULL, scene_color_manage, false);
+               multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+3, 0, NULL, scene_color_manage, false);
 
                if (mode == PFIELD_TEX_GRAD || !hasrgb) { /* if we don't have rgb fall back to grad */
                        /* generate intensity if texture only has rgb value */
@@ -983,7 +847,7 @@ void pdDoEffectors(ListBase *effectors, ListBase *colliders, EffectorWeights *we
  */
        EffectorCache *eff;
        EffectorData efd;
-       int p=0, tot = 1, step = 1;
+       int p=0, tot = 1;
 
        /* Cycle through collected objects, get total of (1/(gravity_strength * dist^gravity_power)) */
        /* Check for min distance here? (yes would be cool to add that, ton) */
@@ -991,9 +855,9 @@ void pdDoEffectors(ListBase *effectors, ListBase *colliders, EffectorWeights *we
        if (effectors) for (eff = effectors->first; eff; eff=eff->next) {
                /* object effectors were fully checked to be OK to evaluate! */
 
-               get_effector_tot(eff, &efd, point, &tot, &p, &step);
+               get_effector_tot(eff, &efd, point, &tot, &p);
 
-               for (; p<tot; p+=step) {
+               for (; p<tot; p++) {
                        if (get_effector_data(eff, &efd, point, 0)) {
                                efd.falloff= effector_falloff(eff, &efd, point, weights);