6 * ***** BEGIN GPL LICENSE BLOCK *****
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23 * All rights reserved.
25 * The Original Code is: all of this file.
27 * Contributor(s): none yet.
29 * ***** END GPL LICENSE BLOCK *****
33 #include "BLI_storage.h" /* _LARGEFILE_SOURCE */
38 #include "MEM_guardedalloc.h"
40 #include "DNA_curve_types.h"
41 #include "DNA_effect_types.h"
42 #include "DNA_group_types.h"
43 #include "DNA_ipo_types.h"
44 #include "DNA_key_types.h"
45 #include "DNA_lattice_types.h"
46 #include "DNA_listBase.h"
47 #include "DNA_mesh_types.h"
48 #include "DNA_meshdata_types.h"
49 #include "DNA_material_types.h"
50 #include "DNA_object_types.h"
51 #include "DNA_object_force.h"
52 #include "DNA_particle_types.h"
53 #include "DNA_texture_types.h"
54 #include "DNA_scene_types.h"
57 #include "BLI_blenlib.h"
58 #include "BLI_jitter.h"
59 #include "BLI_listbase.h"
60 #include "BLI_noise.h"
65 #include "BKE_action.h"
66 #include "BKE_anim.h" /* needed for where_on_path */
67 #include "BKE_armature.h"
68 #include "BKE_blender.h"
69 #include "BKE_collision.h"
70 #include "BKE_constraint.h"
71 #include "BKE_deform.h"
72 #include "BKE_depsgraph.h"
73 #include "BKE_displist.h"
74 #include "BKE_DerivedMesh.h"
75 #include "BKE_cdderivedmesh.h"
76 #include "BKE_effect.h"
77 #include "BKE_global.h"
78 #include "BKE_group.h"
81 #include "BKE_lattice.h"
83 #include "BKE_material.h"
85 #include "BKE_modifier.h"
86 #include "BKE_object.h"
87 #include "BKE_particle.h"
88 #include "BKE_scene.h"
89 #include "BKE_utildefines.h"
91 #include "RE_render_ext.h"
92 #include "RE_shader_ext.h"
94 /* fluid sim particle import */
95 #ifndef DISABLE_ELBEEM
96 #include "DNA_object_fluidsim.h"
97 #include "LBM_fluidsim.h"
100 #endif // DISABLE_ELBEEM
102 //XXX #include "BIF_screen.h"
104 EffectorWeights *BKE_add_effector_weights(Group *group)
106 EffectorWeights *weights = MEM_callocN(sizeof(EffectorWeights), "EffectorWeights");
109 for(i=0; i<NUM_PFIELD_TYPES; i++)
110 weights->weight[i] = 1.0f;
112 weights->global_gravity = 1.0f;
114 weights->group = group;
118 PartDeflect *object_add_collision_fields(int type)
122 pd= MEM_callocN(sizeof(PartDeflect), "PartDeflect");
124 pd->forcefield = type;
125 pd->pdef_sbdamp = 0.1f;
126 pd->pdef_sbift = 0.2f;
127 pd->pdef_sboft = 0.02f;
128 pd->seed = ((unsigned int)(ceil(PIL_check_seconds_timer()))+1) % 128;
129 pd->f_strength = 1.0f;
132 /* set sensible defaults based on type */
135 pd->shape = PFIELD_SHAPE_PLANE;
138 pd->shape = PFIELD_SHAPE_PLANE;
139 pd->f_flow = 1.0f; /* realistic wind behavior */
145 pd->flag = PFIELD_DO_LOCATION|PFIELD_DO_ROTATION;
150 /* temporal struct, used for reading return of mesh_get_mapped_verts_nors() */
152 typedef struct VeNoCo {
156 /* ***************** PARTICLES ***************** */
158 /* deprecated, only keep this for readfile.c */
159 PartEff *give_parteff(Object *ob)
163 paf= ob->effect.first;
165 if(paf->type==EFF_PARTICLE) return paf;
171 void free_effect(Effect *eff)
175 if(eff->type==EFF_PARTICLE) {
177 if(paf->keys) MEM_freeN(paf->keys);
183 void free_effects(ListBase *lb)
189 BLI_remlink(lb, eff);
195 /* -------------------------- Effectors ------------------ */
196 void free_partdeflect(PartDeflect *pd)
210 static void precalculate_effector(EffectorCache *eff)
212 unsigned int cfra = (unsigned int)(eff->scene->r.cfra >= 0 ? eff->scene->r.cfra : -eff->scene->r.cfra);
214 eff->pd->rng = rng_new(eff->pd->seed + cfra);
216 rng_srandom(eff->pd->rng, eff->pd->seed + cfra);
218 if(eff->pd->forcefield == PFIELD_GUIDE && eff->ob->type==OB_CURVE) {
219 Curve *cu= eff->ob->data;
220 if(cu->flag & CU_PATH) {
221 if(cu->path==NULL || cu->path->data==NULL)
222 makeDispListCurveTypes(eff->scene, eff->ob, 0);
224 if(cu->path && cu->path->data) {
225 where_on_path(eff->ob, 0.0, eff->guide_loc, eff->guide_dir, NULL, &eff->guide_radius, NULL);
226 mul_m4_v3(eff->ob->obmat, eff->guide_loc);
227 mul_mat3_m4_v3(eff->ob->obmat, eff->guide_dir);
231 else if(eff->pd->shape == PFIELD_SHAPE_SURFACE) {
232 eff->surmd = (SurfaceModifierData *)modifiers_findByType ( eff->ob, eModifierType_Surface );
233 if(eff->ob->type == OB_CURVE)
234 eff->flag |= PE_USE_NORMAL_DATA;
237 psys_update_particle_tree(eff->psys, eff->scene->r.cfra);
239 static EffectorCache *new_effector_cache(Scene *scene, Object *ob, ParticleSystem *psys, PartDeflect *pd)
241 EffectorCache *eff = MEM_callocN(sizeof(EffectorCache), "EffectorCache");
248 precalculate_effector(eff);
252 static void add_object_to_effectors(ListBase **effectors, Scene *scene, EffectorWeights *weights, Object *ob, Object *ob_src)
254 EffectorCache *eff = NULL;
256 if( ob == ob_src || weights->weight[ob->pd->forcefield] == 0.0f )
259 if (ob->pd->shape == PFIELD_SHAPE_POINTS && !ob->derivedFinal )
262 if(*effectors == NULL)
263 *effectors = MEM_callocN(sizeof(ListBase), "effectors list");
265 eff = new_effector_cache(scene, ob, NULL, ob->pd);
267 /* make sure imat is up to date */
268 invert_m4_m4(ob->imat, ob->obmat);
270 BLI_addtail(*effectors, eff);
272 static void add_particles_to_effectors(ListBase **effectors, Scene *scene, EffectorWeights *weights, Object *ob, ParticleSystem *psys, ParticleSystem *psys_src)
274 ParticleSettings *part= psys->part;
276 if( !psys_check_enabled(ob, psys) )
279 if( psys == psys_src && (part->flag & PART_SELF_EFFECT) == 0)
282 if( part->pd && part->pd->forcefield && weights->weight[part->pd->forcefield] != 0.0f) {
283 if(*effectors == NULL)
284 *effectors = MEM_callocN(sizeof(ListBase), "effectors list");
286 BLI_addtail(*effectors, new_effector_cache(scene, ob, psys, part->pd));
289 if (part->pd2 && part->pd2->forcefield && weights->weight[part->pd2->forcefield] != 0.0f) {
290 if(*effectors == NULL)
291 *effectors = MEM_callocN(sizeof(ListBase), "effectors list");
293 BLI_addtail(*effectors, new_effector_cache(scene, ob, psys, part->pd2));
297 /* returns ListBase handle with objects taking part in the effecting */
298 ListBase *pdInitEffectors(Scene *scene, Object *ob_src, ParticleSystem *psys_src, EffectorWeights *weights)
301 unsigned int layer= ob_src->lay;
302 ListBase *effectors = NULL;
307 for(go= weights->group->gobject.first; go; go= go->next) {
308 if( (go->ob->lay & layer) ) {
309 if( go->ob->pd && go->ob->pd->forcefield )
310 add_object_to_effectors(&effectors, scene, weights, go->ob, ob_src);
312 if( go->ob->particlesystem.first ) {
313 ParticleSystem *psys= go->ob->particlesystem.first;
315 for( ; psys; psys=psys->next )
316 add_particles_to_effectors(&effectors, scene, weights, go->ob, psys, psys_src);
322 for(base = scene->base.first; base; base= base->next) {
323 if( (base->lay & layer) ) {
324 if( base->object->pd && base->object->pd->forcefield )
325 add_object_to_effectors(&effectors, scene, weights, base->object, ob_src);
327 if( base->object->particlesystem.first ) {
328 ParticleSystem *psys= base->object->particlesystem.first;
330 for( ; psys; psys=psys->next )
331 add_particles_to_effectors(&effectors, scene, weights, base->object, psys, psys_src);
339 void pdEndEffectors(ListBase **effectors)
342 EffectorCache *eff = (*effectors)->first;
344 for(; eff; eff=eff->next) {
346 MEM_freeN(eff->guide_data);
349 BLI_freelistN(*effectors);
350 MEM_freeN(*effectors);
356 void pd_point_from_particle(ParticleSimulationData *sim, ParticleData *pa, ParticleKey *state, EffectedPoint *point)
358 point->loc = state->co;
359 point->vel = state->vel;
360 point->index = pa - sim->psys->particles;
361 point->size = pa->size;
362 /* TODO: point->charge */
363 point->charge = 1.0f;
365 point->vel_to_sec = 1.0f;
366 point->vel_to_frame = psys_get_timestep(sim);
370 if(sim->psys->part->flag & PART_ROT_DYN) {
371 point->ave = state->ave;
372 point->rot = state->rot;
375 point->ave = point->rot = NULL;
377 point->psys = sim->psys;
380 void pd_point_from_loc(Scene *scene, float *loc, float *vel, int index, EffectedPoint *point)
384 point->index = index;
387 point->vel_to_sec = (float)scene->r.frs_sec;
388 point->vel_to_frame = 1.0f;
392 point->ave = point->rot = NULL;
395 void pd_point_from_soft(Scene *scene, float *loc, float *vel, int index, EffectedPoint *point)
399 point->index = index;
402 point->vel_to_sec = (float)scene->r.frs_sec;
403 point->vel_to_frame = 1.0f;
405 point->flag = PE_WIND_AS_SPEED;
407 point->ave = point->rot = NULL;
411 /************************************************/
413 /************************************************/
415 // triangle - ray callback function
416 static void eff_tri_ray_hit(void *UNUSED(userData), int UNUSED(index), const BVHTreeRay *UNUSED(ray), BVHTreeRayHit *hit)
418 // whenever we hit a bounding box, we don't check further
423 // get visibility of a wind ray
424 static float eff_calc_visibility(ListBase *colliders, EffectorCache *eff, EffectorData *efd, EffectedPoint *point)
426 ListBase *colls = colliders;
428 float norm[3], len = 0.0;
429 float visibility = 1.0, absorption = 0.0;
431 if(!(eff->pd->flag & PFIELD_VISIBILITY))
435 colls = get_collider_cache(eff->scene, NULL, NULL);
440 negate_v3_v3(norm, efd->vec_to_point);
441 len = normalize_v3(norm);
443 // check all collision objects
444 for(col = colls->first; col; col = col->next)
446 CollisionModifierData *collmd = col->collmd;
448 if(col->ob == eff->ob)
456 hit.dist = len + FLT_EPSILON;
458 // check if the way is blocked
459 if(BLI_bvhtree_ray_cast(collmd->bvhtree, point->loc, norm, 0.0f, &hit, eff_tri_ray_hit, NULL)>=0)
461 absorption= col->ob->pd->absorption;
463 // visibility is only between 0 and 1, calculated from 1-absorption
464 visibility *= CLAMPIS(1.0f-absorption, 0.0f, 1.0f);
466 if(visibility <= 0.0f)
473 free_collider_cache(&colls);
478 // noise function for wind e.g.
479 static float wind_func(struct RNG *rng, float strength)
481 int random = (rng_getInt(rng)+1) % 128; // max 2357
482 float force = rng_getFloat(rng) + 1.0f;
486 sign = ((float)random > 64.0) ? 1.0: -1.0; // dividing by 2 is not giving equal sign distribution
488 ret = sign*((float)random / force)*strength/128.0f;
493 /* maxdist: zero effect from this distance outwards (if usemax) */
494 /* mindist: full effect up to this distance (if usemin) */
495 /* power: falloff with formula 1/r^power */
496 static float falloff_func(float fac, int usemin, float mindist, int usemax, float maxdist, float power)
498 /* first quick checks */
499 if(usemax && fac > maxdist)
502 if(usemin && fac < mindist)
508 return pow((double)1.0+fac-mindist, (double)-power);
511 static float falloff_func_dist(PartDeflect *pd, float fac)
513 return falloff_func(fac, pd->flag&PFIELD_USEMIN, pd->mindist, pd->flag&PFIELD_USEMAX, pd->maxdist, pd->f_power);
516 static float falloff_func_rad(PartDeflect *pd, float fac)
518 return falloff_func(fac, pd->flag&PFIELD_USEMINR, pd->minrad, pd->flag&PFIELD_USEMAXR, pd->maxrad, pd->f_power_r);
521 float effector_falloff(EffectorCache *eff, EffectorData *efd, EffectedPoint *UNUSED(point), EffectorWeights *weights)
524 float falloff = weights ? weights->weight[0] * weights->weight[eff->pd->forcefield] : 1.0f;
527 fac = dot_v3v3(efd->nor, efd->vec_to_point2);
529 if(eff->pd->zdir == PFIELD_Z_POS && fac < 0.0f)
531 else if(eff->pd->zdir == PFIELD_Z_NEG && fac > 0.0f)
533 else switch(eff->pd->falloff){
534 case PFIELD_FALL_SPHERE:
535 falloff*= falloff_func_dist(eff->pd, efd->distance);
538 case PFIELD_FALL_TUBE:
539 falloff*= falloff_func_dist(eff->pd, ABS(fac));
543 VECADDFAC(temp, efd->vec_to_point, efd->nor, -fac);
545 falloff*= falloff_func_rad(eff->pd, r_fac);
547 case PFIELD_FALL_CONE:
548 falloff*= falloff_func_dist(eff->pd, ABS(fac));
552 r_fac=saacos(fac/len_v3(efd->vec_to_point))*180.0f/(float)M_PI;
553 falloff*= falloff_func_rad(eff->pd, r_fac);
561 int closest_point_on_surface(SurfaceModifierData *surmd, float *co, float *surface_co, float *surface_nor, float *surface_vel)
563 BVHTreeNearest nearest;
566 nearest.dist = FLT_MAX;
568 BLI_bvhtree_find_nearest(surmd->bvhtree->tree, co, &nearest, surmd->bvhtree->nearest_callback, surmd->bvhtree);
570 if(nearest.index != -1) {
571 VECCOPY(surface_co, nearest.co);
574 VECCOPY(surface_nor, nearest.no);
578 MFace *mface = CDDM_get_face(surmd->dm, nearest.index);
580 VECCOPY(surface_vel, surmd->v[mface->v1].co);
581 add_v3_v3(surface_vel, surmd->v[mface->v2].co);
582 add_v3_v3(surface_vel, surmd->v[mface->v3].co);
584 add_v3_v3(surface_vel, surmd->v[mface->v4].co);
586 mul_v3_fl(surface_vel, mface->v4 ? 0.25f : 0.333f);
593 int get_effector_data(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, int real_velocity)
595 float cfra = eff->scene->r.cfra;
598 if(eff->pd && eff->pd->shape==PFIELD_SHAPE_SURFACE && eff->surmd) {
599 /* closest point in the object surface is an effector */
602 /* using velocity corrected location allows for easier sliding over effector surface */
603 copy_v3_v3(vec, point->vel);
604 mul_v3_fl(vec, point->vel_to_frame);
605 add_v3_v3(vec, point->loc);
607 ret = closest_point_on_surface(eff->surmd, vec, efd->loc, efd->nor, real_velocity ? efd->vel : NULL);
611 else if(eff->pd && eff->pd->shape==PFIELD_SHAPE_POINTS) {
613 if(eff->ob->derivedFinal) {
614 DerivedMesh *dm = eff->ob->derivedFinal;
616 dm->getVertCo(dm, *efd->index, efd->loc);
617 dm->getVertNo(dm, *efd->index, efd->nor);
619 mul_m4_v3(eff->ob->obmat, efd->loc);
620 mul_mat3_m4_v3(eff->ob->obmat, efd->nor);
622 normalize_v3(efd->nor);
631 ParticleSimulationData sim = {eff->scene, eff->ob, eff->psys, NULL, NULL};
632 ParticleData *pa = eff->psys->particles + *efd->index;
635 /* exclude the particle itself for self effecting particles */
636 if(eff->psys == point->psys && *efd->index == point->index)
639 /* TODO: time from actual previous calculated frame (step might not be 1) */
640 state.time = cfra - 1.0;
641 ret = psys_get_particle_state(&sim, *efd->index, &state, 0);
644 //if(eff->pd->forcefiled == PFIELD_HARMONIC && ret==0) {
645 // if(pa->dietime < eff->psys->cfra)
646 // eff->flag |= PE_VELOCITY_TO_IMPULSE;
649 VECCOPY(efd->loc, state.co);
650 VECCOPY(efd->nor, state.vel);
652 VECCOPY(efd->vel, state.vel);
655 efd->size = pa->size;
659 /* use center of object for distance calculus */
660 Object *ob = eff->ob;
663 /* XXX this is not thread-safe, but used from multiple threads by
665 where_is_object_time(eff->scene, ob, cfra);
667 /* use z-axis as normal*/
668 normalize_v3_v3(efd->nor, ob->obmat[2]);
670 /* for vortex the shape chooses between old / new force */
671 if(eff->pd && eff->pd->shape == PFIELD_SHAPE_PLANE) {
672 /* efd->loc is closes point on effector xy-plane */
673 float temp[3], translate[3];
674 sub_v3_v3v3(temp, point->loc, ob->obmat[3]);
675 project_v3_v3v3(translate, temp, efd->nor);
676 add_v3_v3v3(efd->loc, ob->obmat[3], translate);
679 VECCOPY(efd->loc, ob->obmat[3]);
683 VECCOPY(efd->vel, ob->obmat[3]);
685 where_is_object_time(eff->scene, ob, cfra - 1.0);
687 sub_v3_v3v3(efd->vel, efd->vel, ob->obmat[3]);
698 sub_v3_v3v3(efd->vec_to_point, point->loc, efd->loc);
699 efd->distance = len_v3(efd->vec_to_point);
701 /* rest length for harmonic effector, will have to see later if this could be extended to other effectors */
702 if(eff->pd && eff->pd->forcefield == PFIELD_HARMONIC && eff->pd->f_size)
703 mul_v3_fl(efd->vec_to_point, (efd->distance-eff->pd->f_size)/efd->distance);
705 if(eff->flag & PE_USE_NORMAL_DATA) {
706 VECCOPY(efd->vec_to_point2, efd->vec_to_point);
707 VECCOPY(efd->nor2, efd->nor);
710 /* for some effectors we need the object center every time */
711 sub_v3_v3v3(efd->vec_to_point2, point->loc, eff->ob->obmat[3]);
712 normalize_v3_v3(efd->nor2, eff->ob->obmat[2]);
718 static void get_effector_tot(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, int *tot, int *p)
720 if(eff->pd->shape == PFIELD_SHAPE_POINTS) {
724 *tot = eff->ob->derivedFinal ? eff->ob->derivedFinal->numVertData : 1;
726 if(*tot && eff->pd->forcefield == PFIELD_HARMONIC && point->index >= 0) {
727 *p = point->index % *tot;
735 *tot = eff->psys->totpart;
737 if(eff->pd->forcefield == PFIELD_CHARGE) {
738 /* Only the charge of the effected particle is used for
739 interaction, not fall-offs. If the fall-offs aren't the
740 same this will be unphysical, but for animation this
741 could be the wanted behavior. If you want physical
742 correctness the fall-off should be spherical 2.0 anyways.
744 efd->charge = eff->pd->f_strength;
746 else if(eff->pd->forcefield == PFIELD_HARMONIC && (eff->pd->flag & PFIELD_MULTIPLE_SPRINGS)==0) {
747 /* every particle is mapped to only one harmonic effector particle */
748 *p= point->index % eff->psys->totpart;
757 static void do_texture_effector(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, float *total_force)
760 float tex_co[3], strength, force[3];
761 float nabla = eff->pd->tex_nabla;
763 short mode = eff->pd->tex_mode;
768 result[0].nor = result[1].nor = result[2].nor = result[3].nor = 0;
770 strength= eff->pd->f_strength * efd->falloff;
772 VECCOPY(tex_co,point->loc);
774 if(eff->pd->flag & PFIELD_TEX_2D) {
775 float fac=-dot_v3v3(tex_co, efd->nor);
776 VECADDFAC(tex_co, tex_co, efd->nor, fac);
779 if(eff->pd->flag & PFIELD_TEX_OBJECT) {
780 mul_m4_v3(eff->ob->imat, tex_co);
783 hasrgb = multitex_ext(eff->pd->tex, tex_co, NULL,NULL, 0, result);
785 if(hasrgb && mode==PFIELD_TEX_RGB) {
786 force[0] = (0.5f - result->tr) * strength;
787 force[1] = (0.5f - result->tg) * strength;
788 force[2] = (0.5f - result->tb) * strength;
794 multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+1);
798 multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+2);
802 multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+3);
804 if(mode == PFIELD_TEX_GRAD || !hasrgb) { /* if we dont have rgb fall back to grad */
805 force[0] = (result[0].tin - result[1].tin) * strength;
806 force[1] = (result[0].tin - result[2].tin) * strength;
807 force[2] = (result[0].tin - result[3].tin) * strength;
809 else { /*PFIELD_TEX_CURL*/
810 float dbdy, dgdz, drdz, dbdx, dgdx, drdy;
812 dbdy = result[2].tb - result[0].tb;
813 dgdz = result[3].tg - result[0].tg;
814 drdz = result[3].tr - result[0].tr;
815 dbdx = result[1].tb - result[0].tb;
816 dgdx = result[1].tg - result[0].tg;
817 drdy = result[2].tr - result[0].tr;
819 force[0] = (dbdy - dgdz) * strength;
820 force[1] = (drdz - dbdx) * strength;
821 force[2] = (dgdx - drdy) * strength;
825 if(eff->pd->flag & PFIELD_TEX_2D){
826 float fac = -dot_v3v3(force, efd->nor);
827 VECADDFAC(force, force, efd->nor, fac);
830 add_v3_v3(total_force, force);
832 void do_physical_effector(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, float *total_force)
834 PartDeflect *pd = eff->pd;
836 float force[3]={0,0,0};
839 float strength = pd->f_strength;
840 float damp = pd->f_damp;
841 float noise_factor = pd->f_noise;
843 if(noise_factor > 0.0f) {
844 strength += wind_func(rng, noise_factor);
846 if(ELEM(pd->forcefield, PFIELD_HARMONIC, PFIELD_DRAG))
847 damp += wind_func(rng, noise_factor);
850 VECCOPY(force, efd->vec_to_point);
852 switch(pd->forcefield){
854 VECCOPY(force, efd->nor);
855 mul_v3_fl(force, strength * efd->falloff);
859 mul_v3_fl(force, strength * efd->falloff);
862 /* old vortex force */
863 if(pd->shape == PFIELD_SHAPE_POINT) {
864 cross_v3_v3v3(force, efd->nor, efd->vec_to_point);
866 mul_v3_fl(force, strength * efd->distance * efd->falloff);
869 /* new vortex force */
870 cross_v3_v3v3(temp, efd->nor2, efd->vec_to_point2);
871 mul_v3_fl(temp, strength * efd->falloff);
873 cross_v3_v3v3(force, efd->nor2, temp);
874 mul_v3_fl(force, strength * efd->falloff);
876 VECADDFAC(temp, temp, point->vel, -point->vel_to_sec);
877 add_v3_v3(force, temp);
881 if(eff->pd->shape == PFIELD_SHAPE_POINT)
882 /* magnetic field of a moving charge */
883 cross_v3_v3v3(temp, efd->nor, efd->vec_to_point);
885 copy_v3_v3(temp, efd->nor);
888 mul_v3_fl(temp, strength * efd->falloff);
889 cross_v3_v3v3(force, point->vel, temp);
890 mul_v3_fl(force, point->vel_to_sec);
892 case PFIELD_HARMONIC:
893 mul_v3_fl(force, -strength * efd->falloff);
894 copy_v3_v3(temp, point->vel);
895 mul_v3_fl(temp, -damp * 2.0f * (float)sqrt(fabs(strength)) * point->vel_to_sec);
896 add_v3_v3(force, temp);
899 mul_v3_fl(force, point->charge * strength * efd->falloff);
901 case PFIELD_LENNARDJ:
902 fac = pow((efd->size + point->size) / efd->distance, 6.0);
904 fac = - fac * (1.0 - fac) / efd->distance;
906 /* limit the repulsive term drastically to avoid huge forces */
907 fac = ((fac>2.0) ? 2.0 : fac);
909 mul_v3_fl(force, strength * fac);
912 /* Boid field is handled completely in boids code. */
914 case PFIELD_TURBULENCE:
915 if(pd->flag & PFIELD_GLOBAL_CO) {
916 VECCOPY(temp, point->loc);
919 VECADD(temp, efd->vec_to_point2, efd->nor2);
921 force[0] = -1.0f + 2.0f * BLI_gTurbulence(pd->f_size, temp[0], temp[1], temp[2], 2,0,2);
922 force[1] = -1.0f + 2.0f * BLI_gTurbulence(pd->f_size, temp[1], temp[2], temp[0], 2,0,2);
923 force[2] = -1.0f + 2.0f * BLI_gTurbulence(pd->f_size, temp[2], temp[0], temp[1], 2,0,2);
924 mul_v3_fl(force, strength * efd->falloff);
927 VECCOPY(force, point->vel);
928 fac = normalize_v3(force) * point->vel_to_sec;
930 strength = MIN2(strength, 2.0f);
931 damp = MIN2(damp, 2.0f);
933 mul_v3_fl(force, -efd->falloff * fac * (strength * fac + damp));
937 if(pd->flag & PFIELD_DO_LOCATION) {
938 VECADDFAC(total_force, total_force, force, 1.0f/point->vel_to_sec);
940 if(ELEM(pd->forcefield, PFIELD_HARMONIC, PFIELD_DRAG)==0 && pd->f_flow != 0.0f) {
941 VECADDFAC(total_force, total_force, point->vel, -pd->f_flow * efd->falloff);
945 if(pd->flag & PFIELD_DO_ROTATION && point->ave && point->rot) {
946 float xvec[3] = {1.0f, 0.0f, 0.0f};
948 mul_qt_v3(point->rot, xvec);
949 cross_v3_v3v3(dave, xvec, force);
950 if(pd->f_flow != 0.0f) {
951 VECADDFAC(dave, dave, point->ave, -pd->f_flow * efd->falloff);
953 add_v3_v3(point->ave, dave);
957 /* -------- pdDoEffectors() --------
958 generic force/speed system, now used for particles and softbodies
959 scene = scene where it runs in, for time and stuff
960 lb = listbase with objects that take part in effecting
961 opco = global coord, as input
962 force = force accumulator
963 speed = actual current speed which can be altered
964 cur_time = "external" time in frames, is constant for static particles
965 loc_time = "local" time in frames, range <0-1> for the lifetime of particle
966 par_layer = layer the caller is in
967 flags = only used for softbody wind now
968 guide = old speed of particle
971 void pdDoEffectors(ListBase *effectors, ListBase *colliders, EffectorWeights *weights, EffectedPoint *point, float *force, float *impulse)
974 Modifies the force on a particle according to its
975 relation with the effector object
976 Different kind of effectors include:
977 Forcefields: Gravity-like attractor
978 (force power is related to the inverse of distance to the power of a falloff value)
979 Vortex fields: swirling effectors
980 (particles rotate around Z-axis of the object. otherwise, same relation as)
981 (Forcefields, but this is not done through a force/acceleration)
982 Guide: particles on a path
983 (particles are guided along a curve bezier or old nurbs)
984 (is independent of other effectors)
990 /* Cycle through collected objects, get total of (1/(gravity_strength * dist^gravity_power)) */
991 /* Check for min distance here? (yes would be cool to add that, ton) */
993 if(effectors) for(eff = effectors->first; eff; eff=eff->next) {
994 /* object effectors were fully checked to be OK to evaluate! */
996 get_effector_tot(eff, &efd, point, &tot, &p);
999 if(get_effector_data(eff, &efd, point, 0)) {
1000 efd.falloff= effector_falloff(eff, &efd, point, weights);
1002 if(efd.falloff > 0.0f)
1003 efd.falloff *= eff_calc_visibility(colliders, eff, &efd, point);
1005 if(efd.falloff <= 0.0f)
1006 ; /* don't do anything */
1007 else if(eff->pd->forcefield == PFIELD_TEXTURE)
1008 do_texture_effector(eff, &efd, point, force);
1010 float temp1[3]={0,0,0}, temp2[3];
1011 VECCOPY(temp1, force);
1013 do_physical_effector(eff, &efd, point, force);
1015 // for softbody backward compatibility
1016 if(point->flag & PE_WIND_AS_SPEED && impulse){
1017 VECSUB(temp2, force, temp1);
1018 VECSUB(impulse, impulse, temp2);
1022 else if(eff->flag & PE_VELOCITY_TO_IMPULSE && impulse) {
1023 /* special case for harmonic effector */
1024 VECADD(impulse, impulse, efd.vel);