2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/effect.c
38 #include "MEM_guardedalloc.h"
40 #include "DNA_curve_types.h"
41 #include "DNA_group_types.h"
42 #include "DNA_listBase.h"
43 #include "DNA_meshdata_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_object_force.h"
46 #include "DNA_texture_types.h"
47 #include "DNA_scene_types.h"
50 #include "BLI_blenlib.h"
51 #include "BLI_noise.h"
53 #include "BLI_utildefines.h"
54 #include "BLI_ghash.h"
58 #include "BKE_anim.h" /* needed for where_on_path */
59 #include "BKE_collision.h"
60 #include "BKE_curve.h"
61 #include "BKE_displist.h"
62 #include "BKE_DerivedMesh.h"
63 #include "BKE_cdderivedmesh.h"
64 #include "BKE_effect.h"
65 #include "BKE_global.h"
66 #include "BKE_library.h"
67 #include "BKE_modifier.h"
68 #include "BKE_object.h"
69 #include "BKE_scene.h"
70 #include "BKE_smoke.h"
73 #include "RE_render_ext.h"
74 #include "RE_shader_ext.h"
76 /* fluid sim particle import */
78 #include "LBM_fluidsim.h"
81 #endif // WITH_MOD_FLUID
83 EffectorWeights *BKE_add_effector_weights(Group *group)
85 EffectorWeights *weights = MEM_callocN(sizeof(EffectorWeights), "EffectorWeights");
88 for (i=0; i<NUM_PFIELD_TYPES; i++)
89 weights->weight[i] = 1.0f;
91 weights->global_gravity = 1.0f;
93 weights->group = group;
97 PartDeflect *object_add_collision_fields(int type)
101 pd= MEM_callocN(sizeof(PartDeflect), "PartDeflect");
103 pd->forcefield = type;
104 pd->pdef_sbdamp = 0.1f;
105 pd->pdef_sbift = 0.2f;
106 pd->pdef_sboft = 0.02f;
107 pd->seed = ((unsigned int)(ceil(PIL_check_seconds_timer()))+1) % 128;
108 pd->f_strength = 1.0f;
111 /* set sensible defaults based on type */
114 pd->shape = PFIELD_SHAPE_PLANE;
117 pd->shape = PFIELD_SHAPE_PLANE;
118 pd->f_flow = 1.0f; /* realistic wind behavior */
123 case PFIELD_SMOKEFLOW:
127 pd->flag = PFIELD_DO_LOCATION|PFIELD_DO_ROTATION;
132 /* ***************** PARTICLES ***************** */
134 /* -------------------------- Effectors ------------------ */
135 void free_partdeflect(PartDeflect *pd)
141 id_us_min(&pd->tex->id);
144 BLI_rng_free(pd->rng);
149 static EffectorCache *new_effector_cache(Scene *scene, Object *ob, PartDeflect *pd)
151 EffectorCache *eff = MEM_callocN(sizeof(EffectorCache), "EffectorCache");
158 static void add_object_to_effectors(ListBase **effectors, Scene *scene, EffectorWeights *weights, Object *ob, Object *ob_src)
160 EffectorCache *eff = NULL;
162 if ( ob == ob_src || weights->weight[ob->pd->forcefield] == 0.0f )
165 if (ob->pd->shape == PFIELD_SHAPE_POINTS && !ob->derivedFinal )
168 if (*effectors == NULL)
169 *effectors = MEM_callocN(sizeof(ListBase), "effectors list");
171 eff = new_effector_cache(scene, ob, ob->pd);
173 /* make sure imat is up to date */
174 invert_m4_m4(ob->imat, ob->obmat);
176 BLI_addtail(*effectors, eff);
179 /* returns ListBase handle with objects taking part in the effecting */
180 ListBase *pdInitEffectors(Scene *scene, Object *ob_src,
181 EffectorWeights *weights, bool precalc)
184 unsigned int layer= ob_src->lay;
185 ListBase *effectors = NULL;
187 if (weights->group) {
190 for (go= weights->group->gobject.first; go; go= go->next) {
191 if ( (go->ob->lay & layer) ) {
192 if ( go->ob->pd && go->ob->pd->forcefield )
193 add_object_to_effectors(&effectors, scene, weights, go->ob, ob_src);
198 for (base = scene->base.first; base; base= base->next) {
199 if ( (base->lay & layer) ) {
200 if ( base->object->pd && base->object->pd->forcefield )
201 add_object_to_effectors(&effectors, scene, weights, base->object, ob_src);
207 pdPrecalculateEffectors(effectors);
212 void pdEndEffectors(ListBase **effectors)
215 EffectorCache *eff = (*effectors)->first;
217 for (; eff; eff=eff->next) {
219 MEM_freeN(eff->guide_data);
222 BLI_freelistN(*effectors);
223 MEM_freeN(*effectors);
228 static void precalculate_effector(EffectorCache *eff)
230 unsigned int cfra = (unsigned int)(eff->scene->r.cfra >= 0 ? eff->scene->r.cfra : -eff->scene->r.cfra);
232 eff->pd->rng = BLI_rng_new(eff->pd->seed + cfra);
234 BLI_rng_srandom(eff->pd->rng, eff->pd->seed + cfra);
236 if (eff->pd->forcefield == PFIELD_GUIDE && eff->ob->type==OB_CURVE) {
237 Curve *cu= eff->ob->data;
238 if (cu->flag & CU_PATH) {
239 if (eff->ob->curve_cache == NULL || eff->ob->curve_cache->path==NULL || eff->ob->curve_cache->path->data==NULL)
240 BKE_displist_make_curveTypes(eff->scene, eff->ob, 0);
242 if (eff->ob->curve_cache->path && eff->ob->curve_cache->path->data) {
243 where_on_path(eff->ob, 0.0, eff->guide_loc, eff->guide_dir, NULL, &eff->guide_radius, NULL);
244 mul_m4_v3(eff->ob->obmat, eff->guide_loc);
245 mul_mat3_m4_v3(eff->ob->obmat, eff->guide_dir);
249 else if (eff->pd->shape == PFIELD_SHAPE_SURFACE) {
250 eff->surmd = (SurfaceModifierData *)modifiers_findByType( eff->ob, eModifierType_Surface );
251 if (eff->ob->type == OB_CURVE)
252 eff->flag |= PE_USE_NORMAL_DATA;
255 /* Store object velocity */
259 BKE_object_where_is_calc_time(eff->scene, eff->ob, cfra - 1.0f);
260 copy_v3_v3(old_vel, eff->ob->obmat[3]);
261 BKE_object_where_is_calc_time(eff->scene, eff->ob, cfra);
262 sub_v3_v3v3(eff->velocity, eff->ob->obmat[3], old_vel);
266 void pdPrecalculateEffectors(ListBase *effectors)
269 EffectorCache *eff = effectors->first;
270 for (; eff; eff=eff->next)
271 precalculate_effector(eff);
276 void pd_point_from_loc(Scene *scene, float *loc, float *vel, int index, EffectedPoint *point)
280 point->index = index;
283 point->vel_to_sec = (float)scene->r.frs_sec;
284 point->vel_to_frame = 1.0f;
288 point->ave = point->rot = NULL;
290 void pd_point_from_soft(Scene *scene, float *loc, float *vel, int index, EffectedPoint *point)
294 point->index = index;
297 point->vel_to_sec = (float)scene->r.frs_sec;
298 point->vel_to_frame = 1.0f;
300 point->flag = PE_WIND_AS_SPEED;
302 point->ave = point->rot = NULL;
304 /************************************************/
306 /************************************************/
308 // triangle - ray callback function
309 static void eff_tri_ray_hit(void *UNUSED(userData), int UNUSED(index), const BVHTreeRay *UNUSED(ray), BVHTreeRayHit *hit)
311 /* whenever we hit a bounding box, we don't check further */
316 // get visibility of a wind ray
317 static float eff_calc_visibility(ListBase *colliders, EffectorCache *eff, EffectorData *efd, EffectedPoint *point)
319 const int raycast_flag = BVH_RAYCAST_DEFAULT & ~(BVH_RAYCAST_WATERTIGHT);
320 ListBase *colls = colliders;
322 float norm[3], len = 0.0;
323 float visibility = 1.0, absorption = 0.0;
325 if (!(eff->pd->flag & PFIELD_VISIBILITY))
329 colls = get_collider_cache(eff->scene, eff->ob, NULL);
334 negate_v3_v3(norm, efd->vec_to_point);
335 len = normalize_v3(norm);
337 /* check all collision objects */
338 for (col = colls->first; col; col = col->next) {
339 CollisionModifierData *collmd = col->collmd;
341 if (col->ob == eff->ob)
344 if (collmd->bvhtree) {
348 hit.dist = len + FLT_EPSILON;
350 /* check if the way is blocked */
351 if (BLI_bvhtree_ray_cast_ex(
352 collmd->bvhtree, point->loc, norm, 0.0f, &hit,
353 eff_tri_ray_hit, NULL, raycast_flag) != -1)
355 absorption= col->ob->pd->absorption;
357 /* visibility is only between 0 and 1, calculated from 1-absorption */
358 visibility *= CLAMPIS(1.0f-absorption, 0.0f, 1.0f);
360 if (visibility <= 0.0f)
367 free_collider_cache(&colls);
372 // noise function for wind e.g.
373 static float wind_func(struct RNG *rng, float strength)
375 int random = (BLI_rng_get_int(rng)+1) % 128; // max 2357
376 float force = BLI_rng_get_float(rng) + 1.0f;
380 sign = ((float)random > 64.0f) ? 1.0f: -1.0f; // dividing by 2 is not giving equal sign distribution
382 ret = sign*((float)random / force)*strength/128.0f;
387 /* maxdist: zero effect from this distance outwards (if usemax) */
388 /* mindist: full effect up to this distance (if usemin) */
389 /* power: falloff with formula 1/r^power */
390 static float falloff_func(float fac, int usemin, float mindist, int usemax, float maxdist, float power)
392 /* first quick checks */
393 if (usemax && fac > maxdist)
396 if (usemin && fac < mindist)
402 return pow((double)(1.0f+fac-mindist), (double)(-power));
405 static float falloff_func_dist(PartDeflect *pd, float fac)
407 return falloff_func(fac, pd->flag&PFIELD_USEMIN, pd->mindist, pd->flag&PFIELD_USEMAX, pd->maxdist, pd->f_power);
410 static float falloff_func_rad(PartDeflect *pd, float fac)
412 return falloff_func(fac, pd->flag&PFIELD_USEMINR, pd->minrad, pd->flag&PFIELD_USEMAXR, pd->maxrad, pd->f_power_r);
415 static float effector_falloff(EffectorCache *eff, EffectorData *efd, EffectedPoint *UNUSED(point), EffectorWeights *weights)
418 float falloff = weights ? weights->weight[0] * weights->weight[eff->pd->forcefield] : 1.0f;
421 fac = dot_v3v3(efd->nor, efd->vec_to_point2);
423 if (eff->pd->zdir == PFIELD_Z_POS && fac < 0.0f)
425 else if (eff->pd->zdir == PFIELD_Z_NEG && fac > 0.0f)
428 switch (eff->pd->falloff) {
429 case PFIELD_FALL_SPHERE:
430 falloff*= falloff_func_dist(eff->pd, efd->distance);
433 case PFIELD_FALL_TUBE:
434 falloff*= falloff_func_dist(eff->pd, ABS(fac));
438 madd_v3_v3v3fl(temp, efd->vec_to_point2, efd->nor, -fac);
440 falloff*= falloff_func_rad(eff->pd, r_fac);
442 case PFIELD_FALL_CONE:
443 falloff*= falloff_func_dist(eff->pd, ABS(fac));
447 r_fac= RAD2DEGF(saacos(fac/len_v3(efd->vec_to_point)));
448 falloff*= falloff_func_rad(eff->pd, r_fac);
457 int closest_point_on_surface(SurfaceModifierData *surmd, const float co[3], float surface_co[3], float surface_nor[3], float surface_vel[3])
459 BVHTreeNearest nearest;
462 nearest.dist_sq = FLT_MAX;
464 BLI_bvhtree_find_nearest(surmd->bvhtree->tree, co, &nearest, surmd->bvhtree->nearest_callback, surmd->bvhtree);
466 if (nearest.index != -1) {
467 copy_v3_v3(surface_co, nearest.co);
470 copy_v3_v3(surface_nor, nearest.no);
474 const MLoop *mloop = surmd->bvhtree->loop;
475 const MLoopTri *lt = &surmd->bvhtree->looptri[nearest.index];
477 copy_v3_v3(surface_vel, surmd->v[mloop[lt->tri[0]].v].co);
478 add_v3_v3(surface_vel, surmd->v[mloop[lt->tri[1]].v].co);
479 add_v3_v3(surface_vel, surmd->v[mloop[lt->tri[2]].v].co);
481 mul_v3_fl(surface_vel, (1.0f / 3.0f));
488 int get_effector_data(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, int real_velocity)
492 /* In case surface object is in Edit mode when loading the .blend, surface modifier is never executed
493 * and bvhtree never built, see T48415. */
494 if (eff->pd && eff->pd->shape==PFIELD_SHAPE_SURFACE && eff->surmd && eff->surmd->bvhtree) {
495 /* closest point in the object surface is an effector */
498 /* using velocity corrected location allows for easier sliding over effector surface */
499 copy_v3_v3(vec, point->vel);
500 mul_v3_fl(vec, point->vel_to_frame);
501 add_v3_v3(vec, point->loc);
503 ret = closest_point_on_surface(eff->surmd, vec, efd->loc, efd->nor, real_velocity ? efd->vel : NULL);
507 else if (eff->pd && eff->pd->shape==PFIELD_SHAPE_POINTS) {
509 if (eff->ob->derivedFinal) {
510 DerivedMesh *dm = eff->ob->derivedFinal;
512 dm->getVertCo(dm, *efd->index, efd->loc);
513 dm->getVertNo(dm, *efd->index, efd->nor);
515 mul_m4_v3(eff->ob->obmat, efd->loc);
516 mul_mat3_m4_v3(eff->ob->obmat, efd->nor);
518 normalize_v3(efd->nor);
527 /* use center of object for distance calculus */
528 const Object *ob = eff->ob;
530 /* use z-axis as normal*/
531 normalize_v3_v3(efd->nor, ob->obmat[2]);
533 if (eff->pd && eff->pd->shape == PFIELD_SHAPE_PLANE) {
534 float temp[3], translate[3];
535 sub_v3_v3v3(temp, point->loc, ob->obmat[3]);
536 project_v3_v3v3(translate, temp, efd->nor);
538 /* for vortex the shape chooses between old / new force */
539 if (eff->pd->forcefield == PFIELD_VORTEX)
540 add_v3_v3v3(efd->loc, ob->obmat[3], translate);
541 else /* normally efd->loc is closest point on effector xy-plane */
542 sub_v3_v3v3(efd->loc, point->loc, translate);
545 copy_v3_v3(efd->loc, ob->obmat[3]);
549 copy_v3_v3(efd->vel, eff->velocity);
557 sub_v3_v3v3(efd->vec_to_point, point->loc, efd->loc);
558 efd->distance = len_v3(efd->vec_to_point);
560 /* rest length for harmonic effector, will have to see later if this could be extended to other effectors */
561 if (eff->pd && eff->pd->forcefield == PFIELD_HARMONIC && eff->pd->f_size)
562 mul_v3_fl(efd->vec_to_point, (efd->distance-eff->pd->f_size)/efd->distance);
564 if (eff->flag & PE_USE_NORMAL_DATA) {
565 copy_v3_v3(efd->vec_to_point2, efd->vec_to_point);
566 copy_v3_v3(efd->nor2, efd->nor);
569 /* for some effectors we need the object center every time */
570 sub_v3_v3v3(efd->vec_to_point2, point->loc, eff->ob->obmat[3]);
571 normalize_v3_v3(efd->nor2, eff->ob->obmat[2]);
577 static void get_effector_tot(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, int *tot, int *p)
582 if (eff->pd->shape == PFIELD_SHAPE_POINTS) {
583 *tot = eff->ob->derivedFinal ? eff->ob->derivedFinal->numVertData : 1;
585 if (*tot && eff->pd->forcefield == PFIELD_HARMONIC && point->index >= 0) {
586 *p = point->index % *tot;
594 static void do_texture_effector(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, float *total_force)
597 float tex_co[3], strength, force[3];
598 float nabla = eff->pd->tex_nabla;
600 short mode = eff->pd->tex_mode;
601 bool scene_color_manage;
606 result[0].nor = result[1].nor = result[2].nor = result[3].nor = NULL;
608 strength= eff->pd->f_strength * efd->falloff;
610 copy_v3_v3(tex_co, point->loc);
612 if (eff->pd->flag & PFIELD_TEX_OBJECT) {
613 mul_m4_v3(eff->ob->imat, tex_co);
615 if (eff->pd->flag & PFIELD_TEX_2D)
618 else if (eff->pd->flag & PFIELD_TEX_2D) {
619 float fac=-dot_v3v3(tex_co, efd->nor);
620 madd_v3_v3fl(tex_co, efd->nor, fac);
623 scene_color_manage = BKE_scene_check_color_management_enabled(eff->scene);
625 hasrgb = multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result, 0, NULL, scene_color_manage, false);
627 if (hasrgb && mode==PFIELD_TEX_RGB) {
628 force[0] = (0.5f - result->tr) * strength;
629 force[1] = (0.5f - result->tg) * strength;
630 force[2] = (0.5f - result->tb) * strength;
636 multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+1, 0, NULL, scene_color_manage, false);
640 multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+2, 0, NULL, scene_color_manage, false);
644 multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+3, 0, NULL, scene_color_manage, false);
646 if (mode == PFIELD_TEX_GRAD || !hasrgb) { /* if we don't have rgb fall back to grad */
647 /* generate intensity if texture only has rgb value */
648 if (hasrgb & TEX_RGB) {
651 result[i].tin = (1.0f / 3.0f) * (result[i].tr + result[i].tg + result[i].tb);
653 force[0] = (result[0].tin - result[1].tin) * strength;
654 force[1] = (result[0].tin - result[2].tin) * strength;
655 force[2] = (result[0].tin - result[3].tin) * strength;
657 else { /*PFIELD_TEX_CURL*/
658 float dbdy, dgdz, drdz, dbdx, dgdx, drdy;
660 dbdy = result[2].tb - result[0].tb;
661 dgdz = result[3].tg - result[0].tg;
662 drdz = result[3].tr - result[0].tr;
663 dbdx = result[1].tb - result[0].tb;
664 dgdx = result[1].tg - result[0].tg;
665 drdy = result[2].tr - result[0].tr;
667 force[0] = (dbdy - dgdz) * strength;
668 force[1] = (drdz - dbdx) * strength;
669 force[2] = (dgdx - drdy) * strength;
673 if (eff->pd->flag & PFIELD_TEX_2D) {
674 float fac = -dot_v3v3(force, efd->nor);
675 madd_v3_v3fl(force, efd->nor, fac);
678 add_v3_v3(total_force, force);
680 static void do_physical_effector(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, float *total_force)
682 PartDeflect *pd = eff->pd;
684 float force[3] = {0, 0, 0};
687 float strength = pd->f_strength;
688 float damp = pd->f_damp;
689 float noise_factor = pd->f_noise;
691 if (noise_factor > 0.0f) {
692 strength += wind_func(rng, noise_factor);
694 if (ELEM(pd->forcefield, PFIELD_HARMONIC, PFIELD_DRAG))
695 damp += wind_func(rng, noise_factor);
698 copy_v3_v3(force, efd->vec_to_point);
700 switch (pd->forcefield) {
702 copy_v3_v3(force, efd->nor);
703 mul_v3_fl(force, strength * efd->falloff);
707 mul_v3_fl(force, strength * efd->falloff);
710 /* old vortex force */
711 if (pd->shape == PFIELD_SHAPE_POINT) {
712 cross_v3_v3v3(force, efd->nor, efd->vec_to_point);
714 mul_v3_fl(force, strength * efd->distance * efd->falloff);
717 /* new vortex force */
718 cross_v3_v3v3(temp, efd->nor2, efd->vec_to_point2);
719 mul_v3_fl(temp, strength * efd->falloff);
721 cross_v3_v3v3(force, efd->nor2, temp);
722 mul_v3_fl(force, strength * efd->falloff);
724 madd_v3_v3fl(temp, point->vel, -point->vel_to_sec);
725 add_v3_v3(force, temp);
729 if (eff->pd->shape == PFIELD_SHAPE_POINT)
730 /* magnetic field of a moving charge */
731 cross_v3_v3v3(temp, efd->nor, efd->vec_to_point);
733 copy_v3_v3(temp, efd->nor);
736 mul_v3_fl(temp, strength * efd->falloff);
737 cross_v3_v3v3(force, point->vel, temp);
738 mul_v3_fl(force, point->vel_to_sec);
740 case PFIELD_HARMONIC:
741 mul_v3_fl(force, -strength * efd->falloff);
742 copy_v3_v3(temp, point->vel);
743 mul_v3_fl(temp, -damp * 2.0f * sqrtf(fabsf(strength)) * point->vel_to_sec);
744 add_v3_v3(force, temp);
747 mul_v3_fl(force, point->charge * strength * efd->falloff);
749 case PFIELD_LENNARDJ:
750 fac = pow((efd->size + point->size) / efd->distance, 6.0);
752 fac = - fac * (1.0f - fac) / efd->distance;
754 /* limit the repulsive term drastically to avoid huge forces */
755 fac = ((fac>2.0f) ? 2.0f : fac);
757 mul_v3_fl(force, strength * fac);
760 /* Boid field is handled completely in boids code. */
762 case PFIELD_TURBULENCE:
763 if (pd->flag & PFIELD_GLOBAL_CO) {
764 copy_v3_v3(temp, point->loc);
767 add_v3_v3v3(temp, efd->vec_to_point2, efd->nor2);
769 force[0] = -1.0f + 2.0f * BLI_gTurbulence(pd->f_size, temp[0], temp[1], temp[2], 2, 0, 2);
770 force[1] = -1.0f + 2.0f * BLI_gTurbulence(pd->f_size, temp[1], temp[2], temp[0], 2, 0, 2);
771 force[2] = -1.0f + 2.0f * BLI_gTurbulence(pd->f_size, temp[2], temp[0], temp[1], 2, 0, 2);
772 mul_v3_fl(force, strength * efd->falloff);
775 copy_v3_v3(force, point->vel);
776 fac = normalize_v3(force) * point->vel_to_sec;
778 strength = MIN2(strength, 2.0f);
779 damp = MIN2(damp, 2.0f);
781 mul_v3_fl(force, -efd->falloff * fac * (strength * fac + damp));
783 case PFIELD_SMOKEFLOW:
787 if ((density = smoke_get_velocity_at(pd->f_source, point->loc, force)) >= 0.0f) {
788 float influence = strength * efd->falloff;
789 if (pd->flag & PFIELD_SMOKE_DENSITY)
790 influence *= density;
791 mul_v3_fl(force, influence);
793 madd_v3_v3fl(total_force, point->vel, -pd->f_flow * influence);
800 if (pd->flag & PFIELD_DO_LOCATION) {
801 madd_v3_v3fl(total_force, force, 1.0f/point->vel_to_sec);
803 if (ELEM(pd->forcefield, PFIELD_HARMONIC, PFIELD_DRAG, PFIELD_SMOKEFLOW)==0 && pd->f_flow != 0.0f) {
804 madd_v3_v3fl(total_force, point->vel, -pd->f_flow * efd->falloff);
810 if (pd->flag & PFIELD_DO_ROTATION && point->ave && point->rot) {
811 float xvec[3] = {1.0f, 0.0f, 0.0f};
813 mul_qt_v3(point->rot, xvec);
814 cross_v3_v3v3(dave, xvec, force);
815 if (pd->f_flow != 0.0f) {
816 madd_v3_v3fl(dave, point->ave, -pd->f_flow * efd->falloff);
818 add_v3_v3(point->ave, dave);
822 /* -------- pdDoEffectors() --------
823 * generic force/speed system, now used for particles and softbodies
824 * scene = scene where it runs in, for time and stuff
825 * lb = listbase with objects that take part in effecting
826 * opco = global coord, as input
827 * force = force accumulator
828 * speed = actual current speed which can be altered
829 * cur_time = "external" time in frames, is constant for static particles
830 * loc_time = "local" time in frames, range <0-1> for the lifetime of particle
831 * par_layer = layer the caller is in
832 * flags = only used for softbody wind now
833 * guide = old speed of particle
835 void pdDoEffectors(ListBase *effectors, ListBase *colliders, EffectorWeights *weights, EffectedPoint *point, float *force, float *impulse)
838 * Modifies the force on a particle according to its
839 * relation with the effector object
840 * Different kind of effectors include:
841 * Forcefields: Gravity-like attractor
842 * (force power is related to the inverse of distance to the power of a falloff value)
843 * Vortex fields: swirling effectors
844 * (particles rotate around Z-axis of the object. otherwise, same relation as)
845 * (Forcefields, but this is not done through a force/acceleration)
846 * Guide: particles on a path
847 * (particles are guided along a curve bezier or old nurbs)
848 * (is independent of other effectors)
854 /* Cycle through collected objects, get total of (1/(gravity_strength * dist^gravity_power)) */
855 /* Check for min distance here? (yes would be cool to add that, ton) */
857 if (effectors) for (eff = effectors->first; eff; eff=eff->next) {
858 /* object effectors were fully checked to be OK to evaluate! */
860 get_effector_tot(eff, &efd, point, &tot, &p);
863 if (get_effector_data(eff, &efd, point, 0)) {
864 efd.falloff= effector_falloff(eff, &efd, point, weights);
866 if (efd.falloff > 0.0f)
867 efd.falloff *= eff_calc_visibility(colliders, eff, &efd, point);
869 if (efd.falloff <= 0.0f) {
870 /* don't do anything */
872 else if (eff->pd->forcefield == PFIELD_TEXTURE) {
873 do_texture_effector(eff, &efd, point, force);
876 float temp1[3] = {0, 0, 0}, temp2[3];
877 copy_v3_v3(temp1, force);
879 do_physical_effector(eff, &efd, point, force);
881 /* for softbody backward compatibility */
882 if (point->flag & PE_WIND_AS_SPEED && impulse) {
883 sub_v3_v3v3(temp2, force, temp1);
884 sub_v3_v3v3(impulse, impulse, temp2);
888 else if (eff->flag & PE_VELOCITY_TO_IMPULSE && impulse) {
889 /* special case for harmonic effector */
890 add_v3_v3v3(impulse, impulse, efd.vel);
896 /* ======== Simulation Debugging ======== */
898 SimDebugData *_sim_debug_data = NULL;
900 unsigned int BKE_sim_debug_data_hash(int i)
902 return BLI_ghashutil_uinthash((unsigned int)i);
905 unsigned int BKE_sim_debug_data_hash_combine(unsigned int kx, unsigned int ky)
907 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
909 unsigned int a, b, c;
911 a = b = c = 0xdeadbeef + (2 << 2) + 13;
915 c ^= b; c -= rot(b,14);
916 a ^= c; a -= rot(c,11);
917 b ^= a; b -= rot(a,25);
918 c ^= b; c -= rot(b,16);
919 a ^= c; a -= rot(c,4);
920 b ^= a; b -= rot(a,14);
921 c ^= b; c -= rot(b,24);
928 static unsigned int debug_element_hash(const void *key)
930 const SimDebugElement *elem = key;
934 static bool debug_element_compare(const void *a, const void *b)
936 const SimDebugElement *elem1 = a;
937 const SimDebugElement *elem2 = b;
939 if (elem1->hash == elem2->hash) {
945 static void debug_element_free(void *val)
947 SimDebugElement *elem = val;
951 void BKE_sim_debug_data_set_enabled(bool enable)
954 if (!_sim_debug_data) {
955 _sim_debug_data = MEM_callocN(sizeof(SimDebugData), "sim debug data");
956 _sim_debug_data->gh = BLI_ghash_new(debug_element_hash, debug_element_compare, "sim debug element hash");
960 BKE_sim_debug_data_free();
964 bool BKE_sim_debug_data_get_enabled(void)
966 return _sim_debug_data != NULL;
969 void BKE_sim_debug_data_free(void)
971 if (_sim_debug_data) {
972 if (_sim_debug_data->gh)
973 BLI_ghash_free(_sim_debug_data->gh, NULL, debug_element_free);
974 MEM_freeN(_sim_debug_data);
978 static void debug_data_insert(SimDebugData *debug_data, SimDebugElement *elem)
980 SimDebugElement *old_elem = BLI_ghash_lookup(debug_data->gh, elem);
986 BLI_ghash_insert(debug_data->gh, elem, elem);
989 void BKE_sim_debug_data_add_element(int type, const float v1[3], const float v2[3], float r, float g, float b, const char *category, unsigned int hash)
991 unsigned int category_hash = BLI_ghashutil_strhash_p(category);
992 SimDebugElement *elem;
994 if (!_sim_debug_data) {
995 if (G.debug & G_DEBUG_SIMDATA)
996 BKE_sim_debug_data_set_enabled(true);
1001 elem = MEM_callocN(sizeof(SimDebugElement), "sim debug data element");
1003 elem->category_hash = category_hash;
1008 copy_v3_v3(elem->v1, v1);
1009 copy_v3_v3(elem->v2, v2);
1011 debug_data_insert(_sim_debug_data, elem);
1014 void BKE_sim_debug_data_remove_element(unsigned int hash)
1016 SimDebugElement dummy;
1017 if (!_sim_debug_data)
1021 BLI_ghash_remove(_sim_debug_data->gh, &dummy, NULL, debug_element_free);
1024 void BKE_sim_debug_data_clear(void)
1026 if (!_sim_debug_data)
1029 if (_sim_debug_data->gh)
1030 BLI_ghash_clear(_sim_debug_data->gh, NULL, debug_element_free);
1033 void BKE_sim_debug_data_clear_category(const char *category)
1035 int category_hash = (int)BLI_ghashutil_strhash_p(category);
1037 if (!_sim_debug_data)
1040 if (_sim_debug_data->gh) {
1042 BLI_ghashIterator_init(&iter, _sim_debug_data->gh);
1043 while (!BLI_ghashIterator_done(&iter)) {
1044 const SimDebugElement *elem = BLI_ghashIterator_getValue(&iter);
1045 BLI_ghashIterator_step(&iter); /* removing invalidates the current iterator, so step before removing */
1047 if (elem->category_hash == category_hash)
1048 BLI_ghash_remove(_sim_debug_data->gh, elem, NULL, debug_element_free);