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) 2007 by Janne Karhu.
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/particle.c
37 #include "MEM_guardedalloc.h"
39 #include "DNA_curve_types.h"
40 #include "DNA_group_types.h"
41 #include "DNA_key_types.h"
42 #include "DNA_material_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meshdata_types.h"
45 #include "DNA_particle_types.h"
46 #include "DNA_smoke_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_dynamicpaint_types.h"
50 #include "BLI_blenlib.h"
51 #include "BLI_noise.h"
53 #include "BLI_utildefines.h"
54 #include "BLI_kdtree.h"
57 #include "BLI_threads.h"
58 #include "BLI_linklist.h"
60 #include "BLT_translation.h"
63 #include "BKE_animsys.h"
65 #include "BKE_boids.h"
66 #include "BKE_cloth.h"
67 #include "BKE_colortools.h"
68 #include "BKE_effect.h"
69 #include "BKE_global.h"
70 #include "BKE_group.h"
72 #include "BKE_lattice.h"
74 #include "BKE_displist.h"
75 #include "BKE_particle.h"
76 #include "BKE_material.h"
78 #include "BKE_library.h"
79 #include "BKE_library_query.h"
80 #include "BKE_library_remap.h"
81 #include "BKE_depsgraph.h"
82 #include "BKE_modifier.h"
84 #include "BKE_cdderivedmesh.h"
85 #include "BKE_pointcache.h"
86 #include "BKE_scene.h"
87 #include "BKE_deform.h"
89 #include "RE_render_ext.h"
91 #include "particle_private.h"
93 unsigned int PSYS_FRAND_SEED_OFFSET[PSYS_FRAND_COUNT];
94 unsigned int PSYS_FRAND_SEED_MULTIPLIER[PSYS_FRAND_COUNT];
95 float PSYS_FRAND_BASE[PSYS_FRAND_COUNT];
97 void psys_init_rng(void)
100 BLI_srandom(5831); /* arbitrary */
101 for (i = 0; i < PSYS_FRAND_COUNT; ++i) {
102 PSYS_FRAND_BASE[i] = BLI_frand();
103 PSYS_FRAND_SEED_OFFSET[i] = (unsigned int)BLI_rand();
104 PSYS_FRAND_SEED_MULTIPLIER[i] = (unsigned int)BLI_rand();
108 static void get_child_modifier_parameters(ParticleSettings *part, ParticleThreadContext *ctx,
109 ChildParticle *cpa, short cpa_from, int cpa_num, float *cpa_fuv, float *orco, ParticleTexture *ptex);
110 static void get_cpa_texture(DerivedMesh *dm, ParticleSystem *psys, ParticleSettings *part, ParticleData *par,
111 int child_index, int face_index, const float fw[4], float *orco, ParticleTexture *ptex, int event, float cfra);
113 /* few helpers for countall etc. */
114 int count_particles(ParticleSystem *psys)
116 ParticleSettings *part = psys->part;
120 LOOP_SHOWN_PARTICLES {
121 if (pa->alive == PARS_UNBORN && (part->flag & PART_UNBORN) == 0) {}
122 else if (pa->alive == PARS_DEAD && (part->flag & PART_DIED) == 0) {}
127 int count_particles_mod(ParticleSystem *psys, int totgr, int cur)
129 ParticleSettings *part = psys->part;
133 LOOP_SHOWN_PARTICLES {
134 if (pa->alive == PARS_UNBORN && (part->flag & PART_UNBORN) == 0) {}
135 else if (pa->alive == PARS_DEAD && (part->flag & PART_DIED) == 0) {}
136 else if (p % totgr == cur) tot++;
140 /* we allocate path cache memory in chunks instead of a big contiguous
141 * chunk, windows' memory allocater fails to find big blocks of memory often */
143 #define PATH_CACHE_BUF_SIZE 1024
145 static ParticleCacheKey *pcache_key_segment_endpoint_safe(ParticleCacheKey *key)
147 return (key->segments > 0) ? (key + (key->segments - 1)) : key;
150 static ParticleCacheKey **psys_alloc_path_cache_buffers(ListBase *bufs, int tot, int totkeys)
153 ParticleCacheKey **cache;
154 int i, totkey, totbufkey;
158 cache = MEM_callocN(tot * sizeof(void *), "PathCacheArray");
160 while (totkey < tot) {
161 totbufkey = MIN2(tot - totkey, PATH_CACHE_BUF_SIZE);
162 buf = MEM_callocN(sizeof(LinkData), "PathCacheLinkData");
163 buf->data = MEM_callocN(sizeof(ParticleCacheKey) * totbufkey * totkeys, "ParticleCacheKey");
165 for (i = 0; i < totbufkey; i++)
166 cache[totkey + i] = ((ParticleCacheKey *)buf->data) + i * totkeys;
169 BLI_addtail(bufs, buf);
175 static void psys_free_path_cache_buffers(ParticleCacheKey **cache, ListBase *bufs)
182 for (buf = bufs->first; buf; buf = buf->next)
183 MEM_freeN(buf->data);
187 /************************************************/
189 /************************************************/
190 /* get object's active particle system safely */
191 ParticleSystem *psys_get_current(Object *ob)
193 ParticleSystem *psys;
194 if (ob == NULL) return NULL;
196 for (psys = ob->particlesystem.first; psys; psys = psys->next) {
197 if (psys->flag & PSYS_CURRENT)
203 short psys_get_current_num(Object *ob)
205 ParticleSystem *psys;
208 if (ob == NULL) return 0;
210 for (psys = ob->particlesystem.first, i = 0; psys; psys = psys->next, i++)
211 if (psys->flag & PSYS_CURRENT)
216 void psys_set_current_num(Object *ob, int index)
218 ParticleSystem *psys;
221 if (ob == NULL) return;
223 for (psys = ob->particlesystem.first, i = 0; psys; psys = psys->next, i++) {
225 psys->flag |= PSYS_CURRENT;
227 psys->flag &= ~PSYS_CURRENT;
232 Object *psys_find_object(Scene *scene, ParticleSystem *psys)
235 ParticleSystem *tpsys;
237 for (base = scene->base.first; base; base = base->next) {
238 for (tpsys = base->object->particlesystem.first; psys; psys = psys->next) {
248 struct LatticeDeformData *psys_create_lattice_deform_data(ParticleSimulationData *sim)
250 struct LatticeDeformData *lattice_deform_data = NULL;
252 if (psys_in_edit_mode(sim->scene, sim->psys) == 0) {
253 Object *lattice = NULL;
254 ModifierData *md = (ModifierData *)psys_get_modifier(sim->ob, sim->psys);
255 int mode = G.is_rendering ? eModifierMode_Render : eModifierMode_Realtime;
257 for (; md; md = md->next) {
258 if (md->type == eModifierType_Lattice) {
259 if (md->mode & mode) {
260 LatticeModifierData *lmd = (LatticeModifierData *)md;
261 lattice = lmd->object;
262 sim->psys->lattice_strength = lmd->strength;
269 lattice_deform_data = init_latt_deform(lattice, NULL);
272 return lattice_deform_data;
274 void psys_disable_all(Object *ob)
276 ParticleSystem *psys = ob->particlesystem.first;
278 for (; psys; psys = psys->next)
279 psys->flag |= PSYS_DISABLED;
281 void psys_enable_all(Object *ob)
283 ParticleSystem *psys = ob->particlesystem.first;
285 for (; psys; psys = psys->next)
286 psys->flag &= ~PSYS_DISABLED;
288 bool psys_in_edit_mode(Scene *scene, ParticleSystem *psys)
290 return (scene->basact && (scene->basact->object->mode & OB_MODE_PARTICLE_EDIT) && psys == psys_get_current((scene->basact)->object) && (psys->edit || psys->pointcache->edit) && !psys->renderdata);
292 bool psys_check_enabled(Object *ob, ParticleSystem *psys, const bool use_render_params)
294 ParticleSystemModifierData *psmd;
296 if (psys->flag & PSYS_DISABLED || psys->flag & PSYS_DELETE || !psys->part)
299 psmd = psys_get_modifier(ob, psys);
300 if (psys->renderdata || use_render_params) {
301 if (!(psmd->modifier.mode & eModifierMode_Render))
304 else if (!(psmd->modifier.mode & eModifierMode_Realtime))
310 bool psys_check_edited(ParticleSystem *psys)
312 if (psys->part && psys->part->type == PART_HAIR)
313 return (psys->flag & PSYS_EDITED || (psys->edit && psys->edit->edited));
315 return (psys->pointcache->edit && psys->pointcache->edit->edited);
318 void psys_check_group_weights(ParticleSettings *part)
320 ParticleDupliWeight *dw, *tdw;
324 if (part->ren_as == PART_DRAW_GR && part->dup_group && part->dup_group->gobject.first) {
325 /* First try to find NULL objects from their index,
326 * and remove all weights that don't have an object in the group. */
327 dw = part->dupliweights.first;
329 if (dw->ob == NULL || !BKE_group_object_exists(part->dup_group, dw->ob)) {
330 go = (GroupObject *)BLI_findlink(&part->dup_group->gobject, dw->index);
336 BLI_freelinkN(&part->dupliweights, dw);
345 /* then add objects in the group to new list */
346 go = part->dup_group->gobject.first;
348 dw = part->dupliweights.first;
349 while (dw && dw->ob != go->ob)
353 dw = MEM_callocN(sizeof(ParticleDupliWeight), "ParticleDupliWeight");
356 BLI_addtail(&part->dupliweights, dw);
362 dw = part->dupliweights.first;
363 for (; dw; dw = dw->next) {
364 if (dw->flag & PART_DUPLIW_CURRENT) {
371 dw = part->dupliweights.first;
373 dw->flag |= PART_DUPLIW_CURRENT;
377 BLI_freelistN(&part->dupliweights);
380 int psys_uses_gravity(ParticleSimulationData *sim)
382 return sim->scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY && sim->psys->part && sim->psys->part->effector_weights->global_gravity != 0.0f;
384 /************************************************/
386 /************************************************/
387 static void fluid_free_settings(SPHFluidSettings *fluid)
393 /** Free (or release) any data used by this particle settings (does not free the partsett itself). */
394 void BKE_particlesettings_free(ParticleSettings *part)
398 BKE_animdata_free((ID *)part, false);
400 for (a = 0; a < MAX_MTEX; a++) {
401 MEM_SAFE_FREE(part->mtex[a]);
404 if (part->clumpcurve)
405 curvemapping_free(part->clumpcurve);
406 if (part->roughcurve)
407 curvemapping_free(part->roughcurve);
408 if (part->twistcurve)
409 curvemapping_free(part->twistcurve);
411 free_partdeflect(part->pd);
412 free_partdeflect(part->pd2);
414 MEM_SAFE_FREE(part->effector_weights);
416 BLI_freelistN(&part->dupliweights);
418 boid_free_settings(part->boids);
419 fluid_free_settings(part->fluid);
422 void free_hair(Object *UNUSED(ob), ParticleSystem *psys, int dynamics)
433 psys->flag &= ~PSYS_HAIR_DONE;
437 BKE_ptcache_free_list(&psys->ptcaches);
438 psys->pointcache = NULL;
440 modifier_free((ModifierData *)psys->clmd);
443 psys->pointcache = BKE_ptcache_add(&psys->ptcaches);
446 cloth_free_modifier(psys->clmd);
450 if (psys->hair_in_dm)
451 psys->hair_in_dm->release(psys->hair_in_dm);
452 psys->hair_in_dm = NULL;
454 if (psys->hair_out_dm)
455 psys->hair_out_dm->release(psys->hair_out_dm);
456 psys->hair_out_dm = NULL;
458 void free_keyed_keys(ParticleSystem *psys)
462 if (psys->part->type == PART_HAIR)
465 if (psys->particles && psys->particles->keys) {
466 MEM_freeN(psys->particles->keys);
476 static void free_child_path_cache(ParticleSystem *psys)
478 psys_free_path_cache_buffers(psys->childcache, &psys->childcachebufs);
479 psys->childcache = NULL;
480 psys->totchildcache = 0;
482 void psys_free_path_cache(ParticleSystem *psys, PTCacheEdit *edit)
485 psys_free_path_cache_buffers(edit->pathcache, &edit->pathcachebufs);
486 edit->pathcache = NULL;
490 psys_free_path_cache_buffers(psys->pathcache, &psys->pathcachebufs);
491 psys->pathcache = NULL;
494 free_child_path_cache(psys);
497 void psys_free_children(ParticleSystem *psys)
500 MEM_freeN(psys->child);
505 free_child_path_cache(psys);
507 void psys_free_particles(ParticleSystem *psys)
511 if (psys->particles) {
512 /* Even though psys->part should never be NULL, this can happen as an exception during deletion.
513 * See ID_REMAP_SKIP/FORCE/FLAG_NEVER_NULL_USAGE in BKE_library_remap. */
514 if (psys->part && psys->part->type == PART_HAIR) {
521 if (psys->particles->keys)
522 MEM_freeN(psys->particles->keys);
524 if (psys->particles->boid)
525 MEM_freeN(psys->particles->boid);
527 MEM_freeN(psys->particles);
528 psys->particles = NULL;
532 void psys_free_pdd(ParticleSystem *psys)
535 if (psys->pdd->cdata)
536 MEM_freeN(psys->pdd->cdata);
537 psys->pdd->cdata = NULL;
539 if (psys->pdd->vdata)
540 MEM_freeN(psys->pdd->vdata);
541 psys->pdd->vdata = NULL;
543 if (psys->pdd->ndata)
544 MEM_freeN(psys->pdd->ndata);
545 psys->pdd->ndata = NULL;
547 if (psys->pdd->vedata)
548 MEM_freeN(psys->pdd->vedata);
549 psys->pdd->vedata = NULL;
551 psys->pdd->totpoint = 0;
552 psys->pdd->totpart = 0;
553 psys->pdd->partsize = 0;
556 /* free everything */
557 void psys_free(Object *ob, ParticleSystem *psys)
561 ParticleSystem *tpsys;
563 psys_free_path_cache(psys, NULL);
565 free_hair(ob, psys, 1);
567 psys_free_particles(psys);
569 if (psys->edit && psys->free_edit)
570 psys->free_edit(psys->edit);
573 MEM_freeN(psys->child);
578 /* check if we are last non-visible particle system */
579 for (tpsys = ob->particlesystem.first; tpsys; tpsys = tpsys->next) {
581 if (ELEM(tpsys->part->ren_as, PART_DRAW_OB, PART_DRAW_GR)) {
587 /* clear do-not-draw-flag */
589 ob->transflag &= ~OB_DUPLIPARTS;
593 BKE_ptcache_free_list(&psys->ptcaches);
594 psys->pointcache = NULL;
596 BLI_freelistN(&psys->targets);
598 BLI_bvhtree_free(psys->bvhtree);
599 BLI_kdtree_free(psys->tree);
601 if (psys->fluid_springs)
602 MEM_freeN(psys->fluid_springs);
604 pdEndEffectors(&psys->effectors);
608 MEM_freeN(psys->pdd);
615 /************************************************/
617 /************************************************/
618 /* these functions move away particle data and bring it back after
619 * rendering, to make different render settings possible without
620 * removing the previous data. this should be solved properly once */
622 void psys_render_set(Object *ob, ParticleSystem *psys, float viewmat[4][4], float winmat[4][4], int winx, int winy, int timeoffset)
624 ParticleRenderData *data;
625 ParticleSystemModifierData *psmd = psys_get_modifier(ob, psys);
627 if (psys->renderdata)
630 data = MEM_callocN(sizeof(ParticleRenderData), "ParticleRenderData");
632 data->child = psys->child;
633 data->totchild = psys->totchild;
634 data->pathcache = psys->pathcache;
635 data->pathcachebufs.first = psys->pathcachebufs.first;
636 data->pathcachebufs.last = psys->pathcachebufs.last;
637 data->totcached = psys->totcached;
638 data->childcache = psys->childcache;
639 data->childcachebufs.first = psys->childcachebufs.first;
640 data->childcachebufs.last = psys->childcachebufs.last;
641 data->totchildcache = psys->totchildcache;
643 if (psmd->dm_final) {
644 data->dm = CDDM_copy_with_tessface(psmd->dm_final);
646 data->totdmvert = psmd->totdmvert;
647 data->totdmedge = psmd->totdmedge;
648 data->totdmface = psmd->totdmface;
651 psys->pathcache = NULL;
652 psys->childcache = NULL;
653 psys->totchild = psys->totcached = psys->totchildcache = 0;
654 BLI_listbase_clear(&psys->pathcachebufs);
655 BLI_listbase_clear(&psys->childcachebufs);
657 copy_m4_m4(data->winmat, winmat);
658 mul_m4_m4m4(data->viewmat, viewmat, ob->obmat);
659 mul_m4_m4m4(data->mat, winmat, data->viewmat);
663 data->timeoffset = timeoffset;
665 psys->renderdata = data;
667 /* Hair can and has to be recalculated if everything isn't displayed. */
668 if (psys->part->disp != 100 && ELEM(psys->part->type, PART_HAIR, PART_FLUID)) {
669 psys->recalc |= PSYS_RECALC_RESET;
673 void psys_render_restore(Object *ob, ParticleSystem *psys)
675 ParticleRenderData *data;
676 ParticleSystemModifierData *psmd = psys_get_modifier(ob, psys);
677 float render_disp = psys_get_current_display_percentage(psys);
680 data = psys->renderdata;
685 MEM_freeN(data->elems);
687 if (psmd->dm_final) {
688 psmd->dm_final->needsFree = 1;
689 psmd->dm_final->release(psmd->dm_final);
691 if (psmd->dm_deformed) {
692 psmd->dm_deformed->needsFree = 1;
693 psmd->dm_deformed->release(psmd->dm_deformed);
694 psmd->dm_deformed = NULL;
697 psys_free_path_cache(psys, NULL);
700 MEM_freeN(psys->child);
705 psys->child = data->child;
706 psys->totchild = data->totchild;
707 psys->pathcache = data->pathcache;
708 psys->pathcachebufs.first = data->pathcachebufs.first;
709 psys->pathcachebufs.last = data->pathcachebufs.last;
710 psys->totcached = data->totcached;
711 psys->childcache = data->childcache;
712 psys->childcachebufs.first = data->childcachebufs.first;
713 psys->childcachebufs.last = data->childcachebufs.last;
714 psys->totchildcache = data->totchildcache;
716 psmd->dm_final = data->dm;
717 psmd->totdmvert = data->totdmvert;
718 psmd->totdmedge = data->totdmedge;
719 psmd->totdmface = data->totdmface;
720 psmd->flag &= ~eParticleSystemFlag_psys_updated;
722 if (psmd->dm_final) {
723 if (!psmd->dm_final->deformedOnly) {
724 if (ob->derivedDeform) {
725 psmd->dm_deformed = CDDM_copy(ob->derivedDeform);
728 psmd->dm_deformed = CDDM_from_mesh((Mesh *)ob->data);
730 DM_ensure_tessface(psmd->dm_deformed);
732 psys_calc_dmcache(ob, psmd->dm_final, psmd->dm_deformed, psys);
736 psys->renderdata = NULL;
738 /* restore particle display percentage */
739 disp = psys_get_current_display_percentage(psys);
741 if (disp != render_disp) {
742 /* Hair can and has to be recalculated if everything isn't displayed. */
743 if (ELEM(psys->part->type, PART_HAIR, PART_FLUID)) {
744 psys->recalc |= PSYS_RECALC_RESET;
750 if (psys_frand(psys, p) > disp)
751 pa->flag |= PARS_NO_DISP;
753 pa->flag &= ~PARS_NO_DISP;
759 bool psys_render_simplify_params(ParticleSystem *psys, ChildParticle *cpa, float *params)
761 ParticleRenderData *data;
762 ParticleRenderElem *elem;
763 float x, w, scale, alpha, lambda, t, scalemin, scalemax;
766 if (!(psys->renderdata && (psys->part->simplify_flag & PART_SIMPLIFY_ENABLE)))
769 data = psys->renderdata;
770 if (!data->do_simplify)
772 b = (data->index_mf_to_mpoly) ? DM_origindex_mface_mpoly(data->index_mf_to_mpoly, data->index_mp_to_orig, cpa->num) : cpa->num;
773 if (b == ORIGINDEX_NONE) {
777 elem = &data->elems[b];
779 lambda = elem->lambda;
781 scalemin = elem->scalemin;
782 scalemax = elem->scalemax;
789 x = (elem->curchild + 0.5f) / elem->totchild;
790 if (x < lambda - t) {
794 else if (x >= lambda + t) {
799 w = (lambda + t - x) / (2.0f * t);
800 scale = scalemin + (scalemax - scalemin) * w;
813 /************************************************/
815 /************************************************/
816 static float interpolate_particle_value(float v1, float v2, float v3, float v4, const float w[4], int four)
820 value = w[0] * v1 + w[1] * v2 + w[2] * v3;
824 CLAMP(value, 0.f, 1.f);
829 void psys_interpolate_particle(short type, ParticleKey keys[4], float dt, ParticleKey *result, bool velocity)
834 interp_cubic_v3(result->co, result->vel, keys[1].co, keys[1].vel, keys[2].co, keys[2].vel, dt);
837 key_curve_position_weights(dt, t, type);
839 interp_v3_v3v3v3v3(result->co, keys[0].co, keys[1].co, keys[2].co, keys[3].co, t);
845 key_curve_position_weights(dt - 0.001f, t, type);
846 interp_v3_v3v3v3v3(temp, keys[0].co, keys[1].co, keys[2].co, keys[3].co, t);
847 sub_v3_v3v3(result->vel, result->co, temp);
850 key_curve_position_weights(dt + 0.001f, t, type);
851 interp_v3_v3v3v3v3(temp, keys[0].co, keys[1].co, keys[2].co, keys[3].co, t);
852 sub_v3_v3v3(result->vel, temp, result->co);
859 typedef struct ParticleInterpolationData {
866 ParticleKey *kkey[2];
871 PTCacheEditPoint *epoint;
872 PTCacheEditKey *ekey[2];
874 float birthtime, dietime;
876 } ParticleInterpolationData;
877 /* Assumes pointcache->mem_cache exists, so for disk cached particles call psys_make_temp_pointcache() before use */
878 /* It uses ParticleInterpolationData->pm to store the current memory cache frame so it's thread safe. */
879 static void get_pointcache_keys_for_time(Object *UNUSED(ob), PointCache *cache, PTCacheMem **cur, int index, float t, ParticleKey *key1, ParticleKey *key2)
881 static PTCacheMem *pm = NULL;
884 if (index < 0) { /* initialize */
885 *cur = cache->mem_cache.first;
892 while (*cur && (*cur)->next && (float)(*cur)->frame < t)
897 index2 = BKE_ptcache_mem_index_find(pm, index);
898 index1 = BKE_ptcache_mem_index_find(pm->prev, index);
903 BKE_ptcache_make_particle_key(key2, index2, pm->data, (float)pm->frame);
905 copy_particle_key(key1, key2, 1);
907 BKE_ptcache_make_particle_key(key1, index1, pm->prev->data, (float)pm->prev->frame);
909 else if (cache->mem_cache.first) {
910 pm = cache->mem_cache.first;
911 index2 = BKE_ptcache_mem_index_find(pm, index);
915 BKE_ptcache_make_particle_key(key2, index2, pm->data, (float)pm->frame);
916 copy_particle_key(key1, key2, 1);
920 static int get_pointcache_times_for_particle(PointCache *cache, int index, float *start, float *end)
925 for (pm = cache->mem_cache.first; pm; pm = pm->next) {
926 if (BKE_ptcache_mem_index_find(pm, index) >= 0) {
933 for (pm = cache->mem_cache.last; pm; pm = pm->prev) {
934 if (BKE_ptcache_mem_index_find(pm, index) >= 0) {
944 float psys_get_dietime_from_cache(PointCache *cache, int index)
947 int dietime = 10000000; /* some max value so that we can default to pa->time+lifetime */
949 for (pm = cache->mem_cache.last; pm; pm = pm->prev) {
950 if (BKE_ptcache_mem_index_find(pm, index) >= 0)
951 return (float)pm->frame;
954 return (float)dietime;
957 static void init_particle_interpolation(Object *ob, ParticleSystem *psys, ParticleData *pa, ParticleInterpolationData *pind)
961 PTCacheEditPoint *point = pind->epoint;
963 pind->ekey[0] = point->keys;
964 pind->ekey[1] = point->totkey > 1 ? point->keys + 1 : NULL;
966 pind->birthtime = *(point->keys->time);
967 pind->dietime = *((point->keys + point->totkey - 1)->time);
969 else if (pind->keyed) {
970 ParticleKey *key = pa->keys;
972 pind->kkey[1] = pa->totkey > 1 ? key + 1 : NULL;
974 pind->birthtime = key->time;
975 pind->dietime = (key + pa->totkey - 1)->time;
977 else if (pind->cache) {
978 float start = 0.0f, end = 0.0f;
979 get_pointcache_keys_for_time(ob, pind->cache, &pind->pm, -1, 0.0f, NULL, NULL);
980 pind->birthtime = pa ? pa->time : pind->cache->startframe;
981 pind->dietime = pa ? pa->dietime : pind->cache->endframe;
983 if (get_pointcache_times_for_particle(pind->cache, pa - psys->particles, &start, &end)) {
984 pind->birthtime = MAX2(pind->birthtime, start);
985 pind->dietime = MIN2(pind->dietime, end);
989 HairKey *key = pa->hair;
991 pind->hkey[1] = key + 1;
993 pind->birthtime = key->time;
994 pind->dietime = (key + pa->totkey - 1)->time;
997 pind->mvert[0] = CDDM_get_vert(pind->dm, pa->hair_index);
998 pind->mvert[1] = pind->mvert[0] + 1;
1002 static void edit_to_particle(ParticleKey *key, PTCacheEditKey *ekey)
1004 copy_v3_v3(key->co, ekey->co);
1006 copy_v3_v3(key->vel, ekey->vel);
1008 key->time = *(ekey->time);
1010 static void hair_to_particle(ParticleKey *key, HairKey *hkey)
1012 copy_v3_v3(key->co, hkey->co);
1013 key->time = hkey->time;
1016 static void mvert_to_particle(ParticleKey *key, MVert *mvert, HairKey *hkey)
1018 copy_v3_v3(key->co, mvert->co);
1019 key->time = hkey->time;
1022 static void do_particle_interpolation(ParticleSystem *psys, int p, ParticleData *pa, float t, ParticleInterpolationData *pind, ParticleKey *result)
1024 PTCacheEditPoint *point = pind->epoint;
1025 ParticleKey keys[4];
1026 int point_vel = (point && point->keys->vel);
1027 float real_t, dfra, keytime, invdt = 1.f;
1029 /* billboards wont fill in all of these, so start cleared */
1030 memset(keys, 0, sizeof(keys));
1032 /* interpret timing and find keys */
1034 if (result->time < 0.0f)
1035 real_t = -result->time;
1037 real_t = *(pind->ekey[0]->time) + t * (*(pind->ekey[0][point->totkey - 1].time) - *(pind->ekey[0]->time));
1039 while (*(pind->ekey[1]->time) < real_t)
1042 pind->ekey[0] = pind->ekey[1] - 1;
1044 else if (pind->keyed) {
1045 /* we have only one key, so let's use that */
1046 if (pind->kkey[1] == NULL) {
1047 copy_particle_key(result, pind->kkey[0], 1);
1051 if (result->time < 0.0f)
1052 real_t = -result->time;
1054 real_t = pind->kkey[0]->time + t * (pind->kkey[0][pa->totkey - 1].time - pind->kkey[0]->time);
1056 if (psys->part->phystype == PART_PHYS_KEYED && psys->flag & PSYS_KEYED_TIMING) {
1057 ParticleTarget *pt = psys->targets.first;
1061 while (pt && pa->time + pt->time < real_t)
1067 if (pa->time + pt->time + pt->duration > real_t)
1068 real_t = pa->time + pt->time;
1071 real_t = pa->time + ((ParticleTarget *)psys->targets.last)->time;
1074 CLAMP(real_t, pa->time, pa->dietime);
1076 while (pind->kkey[1]->time < real_t)
1079 pind->kkey[0] = pind->kkey[1] - 1;
1081 else if (pind->cache) {
1082 if (result->time < 0.0f) /* flag for time in frames */
1083 real_t = -result->time;
1085 real_t = pa->time + t * (pa->dietime - pa->time);
1088 if (result->time < 0.0f)
1089 real_t = -result->time;
1091 real_t = pind->hkey[0]->time + t * (pind->hkey[0][pa->totkey - 1].time - pind->hkey[0]->time);
1093 while (pind->hkey[1]->time < real_t) {
1098 pind->hkey[0] = pind->hkey[1] - 1;
1101 /* set actual interpolation keys */
1103 edit_to_particle(keys + 1, pind->ekey[0]);
1104 edit_to_particle(keys + 2, pind->ekey[1]);
1106 else if (pind->dm) {
1107 pind->mvert[0] = pind->mvert[1] - 1;
1108 mvert_to_particle(keys + 1, pind->mvert[0], pind->hkey[0]);
1109 mvert_to_particle(keys + 2, pind->mvert[1], pind->hkey[1]);
1111 else if (pind->keyed) {
1112 memcpy(keys + 1, pind->kkey[0], sizeof(ParticleKey));
1113 memcpy(keys + 2, pind->kkey[1], sizeof(ParticleKey));
1115 else if (pind->cache) {
1116 get_pointcache_keys_for_time(NULL, pind->cache, &pind->pm, p, real_t, keys + 1, keys + 2);
1119 hair_to_particle(keys + 1, pind->hkey[0]);
1120 hair_to_particle(keys + 2, pind->hkey[1]);
1123 /* set secondary interpolation keys for hair */
1124 if (!pind->keyed && !pind->cache && !point_vel) {
1126 if (pind->ekey[0] != point->keys)
1127 edit_to_particle(keys, pind->ekey[0] - 1);
1129 edit_to_particle(keys, pind->ekey[0]);
1131 else if (pind->dm) {
1132 if (pind->hkey[0] != pa->hair)
1133 mvert_to_particle(keys, pind->mvert[0] - 1, pind->hkey[0] - 1);
1135 mvert_to_particle(keys, pind->mvert[0], pind->hkey[0]);
1138 if (pind->hkey[0] != pa->hair)
1139 hair_to_particle(keys, pind->hkey[0] - 1);
1141 hair_to_particle(keys, pind->hkey[0]);
1145 if (pind->ekey[1] != point->keys + point->totkey - 1)
1146 edit_to_particle(keys + 3, pind->ekey[1] + 1);
1148 edit_to_particle(keys + 3, pind->ekey[1]);
1150 else if (pind->dm) {
1151 if (pind->hkey[1] != pa->hair + pa->totkey - 1)
1152 mvert_to_particle(keys + 3, pind->mvert[1] + 1, pind->hkey[1] + 1);
1154 mvert_to_particle(keys + 3, pind->mvert[1], pind->hkey[1]);
1157 if (pind->hkey[1] != pa->hair + pa->totkey - 1)
1158 hair_to_particle(keys + 3, pind->hkey[1] + 1);
1160 hair_to_particle(keys + 3, pind->hkey[1]);
1164 dfra = keys[2].time - keys[1].time;
1165 keytime = (real_t - keys[1].time) / dfra;
1167 /* convert velocity to timestep size */
1168 if (pind->keyed || pind->cache || point_vel) {
1169 invdt = dfra * 0.04f * (psys ? psys->part->timetweak : 1.f);
1170 mul_v3_fl(keys[1].vel, invdt);
1171 mul_v3_fl(keys[2].vel, invdt);
1172 interp_qt_qtqt(result->rot, keys[1].rot, keys[2].rot, keytime);
1175 /* now we should have in chronologiacl order k1<=k2<=t<=k3<=k4 with keytime between [0, 1]->[k2, k3] (k1 & k4 used for cardinal & bspline interpolation)*/
1176 psys_interpolate_particle((pind->keyed || pind->cache || point_vel) ? -1 /* signal for cubic interpolation */
1177 : (pind->bspline ? KEY_BSPLINE : KEY_CARDINAL),
1178 keys, keytime, result, 1);
1180 /* the velocity needs to be converted back from cubic interpolation */
1181 if (pind->keyed || pind->cache || point_vel)
1182 mul_v3_fl(result->vel, 1.f / invdt);
1185 static void interpolate_pathcache(ParticleCacheKey *first, float t, ParticleCacheKey *result)
1188 ParticleCacheKey *cur = first;
1190 /* scale the requested time to fit the entire path even if the path is cut early */
1191 t *= (first + first->segments)->time;
1193 while (i < first->segments && cur->time < t)
1199 float dt = (t - (cur - 1)->time) / (cur->time - (cur - 1)->time);
1200 interp_v3_v3v3(result->co, (cur - 1)->co, cur->co, dt);
1201 interp_v3_v3v3(result->vel, (cur - 1)->vel, cur->vel, dt);
1202 interp_qt_qtqt(result->rot, (cur - 1)->rot, cur->rot, dt);
1206 /* first is actual base rotation, others are incremental from first */
1207 if (cur == first || cur - 1 == first)
1208 copy_qt_qt(result->rot, first->rot);
1210 mul_qt_qtqt(result->rot, first->rot, result->rot);
1213 /************************************************/
1214 /* Particles on a dm */
1215 /************************************************/
1216 /* interpolate a location on a face based on face coordinates */
1217 void psys_interpolate_face(MVert *mvert, MFace *mface, MTFace *tface, float (*orcodata)[3],
1218 float w[4], float vec[3], float nor[3], float utan[3], float vtan[3],
1219 float orco[3], float ornor[3])
1221 float *v1 = 0, *v2 = 0, *v3 = 0, *v4 = 0;
1222 float e1[3], e2[3], s1, s2, t1, t2;
1223 float *uv1, *uv2, *uv3, *uv4;
1224 float n1[3], n2[3], n3[3], n4[3];
1226 float *o1, *o2, *o3, *o4;
1228 v1 = mvert[mface->v1].co;
1229 v2 = mvert[mface->v2].co;
1230 v3 = mvert[mface->v3].co;
1232 normal_short_to_float_v3(n1, mvert[mface->v1].no);
1233 normal_short_to_float_v3(n2, mvert[mface->v2].no);
1234 normal_short_to_float_v3(n3, mvert[mface->v3].no);
1237 v4 = mvert[mface->v4].co;
1238 normal_short_to_float_v3(n4, mvert[mface->v4].no);
1240 interp_v3_v3v3v3v3(vec, v1, v2, v3, v4, w);
1243 if (mface->flag & ME_SMOOTH)
1244 interp_v3_v3v3v3v3(nor, n1, n2, n3, n4, w);
1246 normal_quad_v3(nor, v1, v2, v3, v4);
1250 interp_v3_v3v3v3(vec, v1, v2, v3, w);
1253 if (mface->flag & ME_SMOOTH)
1254 interp_v3_v3v3v3(nor, n1, n2, n3, w);
1256 normal_tri_v3(nor, v1, v2, v3);
1260 /* calculate tangent vectors */
1269 uv1 = tuv[0]; uv2 = tuv[1]; uv3 = tuv[2]; uv4 = tuv[3];
1270 map_to_sphere(uv1, uv1 + 1, v1[0], v1[1], v1[2]);
1271 map_to_sphere(uv2, uv2 + 1, v2[0], v2[1], v2[2]);
1272 map_to_sphere(uv3, uv3 + 1, v3[0], v3[1], v3[2]);
1274 map_to_sphere(uv4, uv4 + 1, v4[0], v4[1], v4[2]);
1278 s1 = uv3[0] - uv1[0];
1279 s2 = uv4[0] - uv1[0];
1281 t1 = uv3[1] - uv1[1];
1282 t2 = uv4[1] - uv1[1];
1284 sub_v3_v3v3(e1, v3, v1);
1285 sub_v3_v3v3(e2, v4, v1);
1288 s1 = uv2[0] - uv1[0];
1289 s2 = uv3[0] - uv1[0];
1291 t1 = uv2[1] - uv1[1];
1292 t2 = uv3[1] - uv1[1];
1294 sub_v3_v3v3(e1, v2, v1);
1295 sub_v3_v3v3(e2, v3, v1);
1298 vtan[0] = (s1 * e2[0] - s2 * e1[0]);
1299 vtan[1] = (s1 * e2[1] - s2 * e1[1]);
1300 vtan[2] = (s1 * e2[2] - s2 * e1[2]);
1302 utan[0] = (t1 * e2[0] - t2 * e1[0]);
1303 utan[1] = (t1 * e2[1] - t2 * e1[1]);
1304 utan[2] = (t1 * e2[2] - t2 * e1[2]);
1309 o1 = orcodata[mface->v1];
1310 o2 = orcodata[mface->v2];
1311 o3 = orcodata[mface->v3];
1314 o4 = orcodata[mface->v4];
1316 interp_v3_v3v3v3v3(orco, o1, o2, o3, o4, w);
1319 normal_quad_v3(ornor, o1, o2, o3, o4);
1322 interp_v3_v3v3v3(orco, o1, o2, o3, w);
1325 normal_tri_v3(ornor, o1, o2, o3);
1329 copy_v3_v3(orco, vec);
1331 copy_v3_v3(ornor, nor);
1335 void psys_interpolate_uvs(const MTFace *tface, int quad, const float w[4], float uvco[2])
1337 float v10 = tface->uv[0][0];
1338 float v11 = tface->uv[0][1];
1339 float v20 = tface->uv[1][0];
1340 float v21 = tface->uv[1][1];
1341 float v30 = tface->uv[2][0];
1342 float v31 = tface->uv[2][1];
1346 v40 = tface->uv[3][0];
1347 v41 = tface->uv[3][1];
1349 uvco[0] = w[0] * v10 + w[1] * v20 + w[2] * v30 + w[3] * v40;
1350 uvco[1] = w[0] * v11 + w[1] * v21 + w[2] * v31 + w[3] * v41;
1353 uvco[0] = w[0] * v10 + w[1] * v20 + w[2] * v30;
1354 uvco[1] = w[0] * v11 + w[1] * v21 + w[2] * v31;
1358 void psys_interpolate_mcol(const MCol *mcol, int quad, const float w[4], MCol *mc)
1360 const char *cp1, *cp2, *cp3, *cp4;
1364 cp1 = (const char *)&mcol[0];
1365 cp2 = (const char *)&mcol[1];
1366 cp3 = (const char *)&mcol[2];
1369 cp4 = (char *)&mcol[3];
1371 cp[0] = (int)(w[0] * cp1[0] + w[1] * cp2[0] + w[2] * cp3[0] + w[3] * cp4[0]);
1372 cp[1] = (int)(w[0] * cp1[1] + w[1] * cp2[1] + w[2] * cp3[1] + w[3] * cp4[1]);
1373 cp[2] = (int)(w[0] * cp1[2] + w[1] * cp2[2] + w[2] * cp3[2] + w[3] * cp4[2]);
1374 cp[3] = (int)(w[0] * cp1[3] + w[1] * cp2[3] + w[2] * cp3[3] + w[3] * cp4[3]);
1377 cp[0] = (int)(w[0] * cp1[0] + w[1] * cp2[0] + w[2] * cp3[0]);
1378 cp[1] = (int)(w[0] * cp1[1] + w[1] * cp2[1] + w[2] * cp3[1]);
1379 cp[2] = (int)(w[0] * cp1[2] + w[1] * cp2[2] + w[2] * cp3[2]);
1380 cp[3] = (int)(w[0] * cp1[3] + w[1] * cp2[3] + w[2] * cp3[3]);
1384 static float psys_interpolate_value_from_verts(DerivedMesh *dm, short from, int index, const float fw[4], const float *values)
1386 if (values == 0 || index == -1)
1390 case PART_FROM_VERT:
1391 return values[index];
1392 case PART_FROM_FACE:
1393 case PART_FROM_VOLUME:
1395 MFace *mf = dm->getTessFaceData(dm, index, CD_MFACE);
1396 return interpolate_particle_value(values[mf->v1], values[mf->v2], values[mf->v3], values[mf->v4], fw, mf->v4);
1403 /* conversion of pa->fw to origspace layer coordinates */
1404 static void psys_w_to_origspace(const float w[4], float uv[2])
1406 uv[0] = w[1] + w[2];
1407 uv[1] = w[2] + w[3];
1410 /* conversion of pa->fw to weights in face from origspace */
1411 static void psys_origspace_to_w(OrigSpaceFace *osface, int quad, const float w[4], float neww[4])
1413 float v[4][3], co[3];
1415 v[0][0] = osface->uv[0][0]; v[0][1] = osface->uv[0][1]; v[0][2] = 0.0f;
1416 v[1][0] = osface->uv[1][0]; v[1][1] = osface->uv[1][1]; v[1][2] = 0.0f;
1417 v[2][0] = osface->uv[2][0]; v[2][1] = osface->uv[2][1]; v[2][2] = 0.0f;
1419 psys_w_to_origspace(w, co);
1423 v[3][0] = osface->uv[3][0]; v[3][1] = osface->uv[3][1]; v[3][2] = 0.0f;
1424 interp_weights_poly_v3(neww, v, 4, co);
1427 interp_weights_poly_v3(neww, v, 3, co);
1433 * Find the final derived mesh tessface for a particle, from its original tessface index.
1434 * This is slow and can be optimized but only for many lookups.
1436 * \param dm_final final DM, it may not have the same topology as original mesh.
1437 * \param dm_deformed deformed-only DM, it has the exact same topology as original mesh.
1438 * \param findex_orig the input tessface index.
1439 * \param fw face weights (position of the particle inside the \a findex_orig tessface).
1440 * \param poly_nodes may be NULL, otherwise an array of linked list, one for each final DM polygon, containing all
1441 * its tessfaces indices.
1442 * \return the DM tessface index.
1444 int psys_particle_dm_face_lookup(
1445 DerivedMesh *dm_final, DerivedMesh *dm_deformed,
1446 int findex_orig, const float fw[4], struct LinkNode **poly_nodes)
1448 MFace *mtessface_final;
1449 OrigSpaceFace *osface_final;
1451 float uv[2], (*faceuv)[2];
1453 const int *index_mf_to_mpoly_deformed = NULL;
1454 const int *index_mf_to_mpoly = NULL;
1455 const int *index_mp_to_orig = NULL;
1457 const int totface_final = dm_final->getNumTessFaces(dm_final);
1458 const int totface_deformed = dm_deformed ? dm_deformed->getNumTessFaces(dm_deformed) : totface_final;
1460 if (ELEM(0, totface_final, totface_deformed)) {
1461 return DMCACHE_NOTFOUND;
1464 index_mf_to_mpoly = dm_final->getTessFaceDataArray(dm_final, CD_ORIGINDEX);
1465 index_mp_to_orig = dm_final->getPolyDataArray(dm_final, CD_ORIGINDEX);
1466 BLI_assert(index_mf_to_mpoly);
1469 index_mf_to_mpoly_deformed = dm_deformed->getTessFaceDataArray(dm_deformed, CD_ORIGINDEX);
1472 BLI_assert(dm_final->deformedOnly);
1473 index_mf_to_mpoly_deformed = index_mf_to_mpoly;
1475 BLI_assert(index_mf_to_mpoly_deformed);
1477 pindex_orig = index_mf_to_mpoly_deformed[findex_orig];
1479 if (dm_deformed == NULL) {
1480 dm_deformed = dm_final;
1483 index_mf_to_mpoly_deformed = NULL;
1485 mtessface_final = dm_final->getTessFaceArray(dm_final);
1486 osface_final = dm_final->getTessFaceDataArray(dm_final, CD_ORIGSPACE);
1488 if (osface_final == NULL) {
1489 /* Assume we don't need osface_final data, and we get a direct 1-1 mapping... */
1490 if (findex_orig < totface_final) {
1491 //printf("\tNO CD_ORIGSPACE, assuming not needed\n");
1495 printf("\tNO CD_ORIGSPACE, error out of range\n");
1496 return DMCACHE_NOTFOUND;
1499 else if (findex_orig >= dm_deformed->getNumTessFaces(dm_deformed)) {
1500 return DMCACHE_NOTFOUND; /* index not in the original mesh */
1503 psys_w_to_origspace(fw, uv);
1506 /* we can have a restricted linked list of faces to check, faster! */
1507 LinkNode *tessface_node = poly_nodes[pindex_orig];
1509 for (; tessface_node; tessface_node = tessface_node->next) {
1510 int findex_dst = GET_INT_FROM_POINTER(tessface_node->link);
1511 faceuv = osface_final[findex_dst].uv;
1513 /* check that this intersects - Its possible this misses :/ -
1514 * could also check its not between */
1515 if (mtessface_final[findex_dst].v4) {
1516 if (isect_point_quad_v2(uv, faceuv[0], faceuv[1], faceuv[2], faceuv[3])) {
1520 else if (isect_point_tri_v2(uv, faceuv[0], faceuv[1], faceuv[2])) {
1525 else { /* if we have no node, try every face */
1526 for (int findex_dst = 0; findex_dst < totface_final; findex_dst++) {
1527 /* If current tessface from 'final' DM and orig tessface (given by index) map to the same orig poly... */
1528 if (DM_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, findex_dst) == pindex_orig) {
1529 faceuv = osface_final[findex_dst].uv;
1531 /* check that this intersects - Its possible this misses :/ -
1532 * could also check its not between */
1533 if (mtessface_final[findex_dst].v4) {
1534 if (isect_point_quad_v2(uv, faceuv[0], faceuv[1], faceuv[2], faceuv[3])) {
1538 else if (isect_point_tri_v2(uv, faceuv[0], faceuv[1], faceuv[2])) {
1545 return DMCACHE_NOTFOUND;
1548 static int psys_map_index_on_dm(DerivedMesh *dm, int from, int index, int index_dmcache, const float fw[4], float UNUSED(foffset), int *mapindex, float mapfw[4])
1553 if (dm->deformedOnly || index_dmcache == DMCACHE_ISCHILD) {
1554 /* for meshes that are either only deformed or for child particles, the
1555 * index and fw do not require any mapping, so we can directly use it */
1556 if (from == PART_FROM_VERT) {
1557 if (index >= dm->getNumVerts(dm))
1562 else { /* FROM_FACE/FROM_VOLUME */
1563 if (index >= dm->getNumTessFaces(dm))
1567 copy_v4_v4(mapfw, fw);
1571 /* for other meshes that have been modified, we try to map the particle
1572 * to their new location, which means a different index, and for faces
1573 * also a new face interpolation weights */
1574 if (from == PART_FROM_VERT) {
1575 if (index_dmcache == DMCACHE_NOTFOUND || index_dmcache > dm->getNumVerts(dm))
1578 *mapindex = index_dmcache;
1580 else { /* FROM_FACE/FROM_VOLUME */
1581 /* find a face on the derived mesh that uses this face */
1583 OrigSpaceFace *osface;
1588 if (i == DMCACHE_NOTFOUND || i >= dm->getNumTessFaces(dm))
1593 /* modify the original weights to become
1594 * weights for the derived mesh face */
1595 osface = dm->getTessFaceDataArray(dm, CD_ORIGSPACE);
1596 mface = dm->getTessFaceData(dm, i, CD_MFACE);
1599 mapfw[0] = mapfw[1] = mapfw[2] = mapfw[3] = 0.0f;
1601 psys_origspace_to_w(&osface[i], mface->v4, fw, mapfw);
1608 /* interprets particle data to get a point on a mesh in object space */
1609 void psys_particle_on_dm(DerivedMesh *dm_final, int from, int index, int index_dmcache,
1610 const float fw[4], float foffset, float vec[3], float nor[3], float utan[3], float vtan[3],
1611 float orco[3], float ornor[3])
1613 float tmpnor[3], mapfw[4];
1614 float (*orcodata)[3];
1617 if (!psys_map_index_on_dm(dm_final, from, index, index_dmcache, fw, foffset, &mapindex, mapfw)) {
1618 if (vec) { vec[0] = vec[1] = vec[2] = 0.0; }
1619 if (nor) { nor[0] = nor[1] = 0.0; nor[2] = 1.0; }
1620 if (orco) { orco[0] = orco[1] = orco[2] = 0.0; }
1621 if (ornor) { ornor[0] = ornor[1] = 0.0; ornor[2] = 1.0; }
1622 if (utan) { utan[0] = utan[1] = utan[2] = 0.0; }
1623 if (vtan) { vtan[0] = vtan[1] = vtan[2] = 0.0; }
1628 orcodata = dm_final->getVertDataArray(dm_final, CD_ORCO);
1630 if (from == PART_FROM_VERT) {
1631 dm_final->getVertCo(dm_final, mapindex, vec);
1634 dm_final->getVertNo(dm_final, mapindex, nor);
1640 copy_v3_v3(orco, orcodata[mapindex]);
1643 copy_v3_v3(orco, vec);
1648 dm_final->getVertNo(dm_final, mapindex, ornor);
1649 normalize_v3(ornor);
1653 utan[0] = utan[1] = utan[2] = 0.0f;
1654 vtan[0] = vtan[1] = vtan[2] = 0.0f;
1657 else { /* PART_FROM_FACE / PART_FROM_VOLUME */
1662 mface = dm_final->getTessFaceData(dm_final, mapindex, CD_MFACE);
1663 mvert = dm_final->getVertDataArray(dm_final, CD_MVERT);
1664 mtface = CustomData_get_layer(&dm_final->faceData, CD_MTFACE);
1669 if (from == PART_FROM_VOLUME) {
1670 psys_interpolate_face(mvert, mface, mtface, orcodata, mapfw, vec, tmpnor, utan, vtan, orco, ornor);
1672 copy_v3_v3(nor, tmpnor);
1674 normalize_v3(tmpnor); /* XXX Why not normalize tmpnor before copying it into nor??? -- mont29 */
1675 mul_v3_fl(tmpnor, -foffset);
1676 add_v3_v3(vec, tmpnor);
1679 psys_interpolate_face(mvert, mface, mtface, orcodata, mapfw, vec, nor, utan, vtan, orco, ornor);
1683 float psys_particle_value_from_verts(DerivedMesh *dm, short from, ParticleData *pa, float *values)
1688 if (!psys_map_index_on_dm(dm, from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, &mapindex, mapfw))
1691 return psys_interpolate_value_from_verts(dm, from, mapindex, mapfw, values);
1694 ParticleSystemModifierData *psys_get_modifier(Object *ob, ParticleSystem *psys)
1697 ParticleSystemModifierData *psmd;
1699 for (md = ob->modifiers.first; md; md = md->next) {
1700 if (md->type == eModifierType_ParticleSystem) {
1701 psmd = (ParticleSystemModifierData *) md;
1702 if (psmd->psys == psys) {
1709 /************************************************/
1710 /* Particles on a shape */
1711 /************************************************/
1712 /* ready for future use */
1713 static void psys_particle_on_shape(int UNUSED(distr), int UNUSED(index),
1714 float *UNUSED(fuv), float vec[3], float nor[3], float utan[3], float vtan[3],
1715 float orco[3], float ornor[3])
1718 float zerovec[3] = {0.0f, 0.0f, 0.0f};
1720 copy_v3_v3(vec, zerovec);
1723 copy_v3_v3(nor, zerovec);
1726 copy_v3_v3(utan, zerovec);
1729 copy_v3_v3(vtan, zerovec);
1732 copy_v3_v3(orco, zerovec);
1735 copy_v3_v3(ornor, zerovec);
1738 /************************************************/
1739 /* Particles on emitter */
1740 /************************************************/
1742 CustomDataMask psys_emitter_customdata_mask(ParticleSystem *psys)
1744 CustomDataMask dataMask = 0;
1751 for (i = 0; i < MAX_MTEX; i++) {
1752 mtex = psys->part->mtex[i];
1753 if (mtex && mtex->mapto && (mtex->texco & TEXCO_UV))
1754 dataMask |= CD_MASK_MTFACE;
1757 if (psys->part->tanfac != 0.0f)
1758 dataMask |= CD_MASK_MTFACE;
1760 /* ask for vertexgroups if we need them */
1761 for (i = 0; i < PSYS_TOT_VG; i++) {
1762 if (psys->vgroup[i]) {
1763 dataMask |= CD_MASK_MDEFORMVERT;
1768 /* particles only need this if they are after a non deform modifier, and
1769 * the modifier stack will only create them in that case. */
1770 dataMask |= CD_MASK_ORIGSPACE_MLOOP | CD_MASK_ORIGINDEX;
1772 dataMask |= CD_MASK_ORCO;
1777 void psys_particle_on_emitter(ParticleSystemModifierData *psmd, int from, int index, int index_dmcache,
1778 float fuv[4], float foffset, float vec[3], float nor[3], float utan[3], float vtan[3],
1779 float orco[3], float ornor[3])
1781 if (psmd && psmd->dm_final) {
1782 if (psmd->psys->part->distr == PART_DISTR_GRID && psmd->psys->part->from != PART_FROM_VERT) {
1784 copy_v3_v3(vec, fuv);
1787 copy_v3_v3(orco, fuv);
1790 /* we cant use the num_dmcache */
1791 psys_particle_on_dm(psmd->dm_final, from, index, index_dmcache, fuv, foffset, vec, nor, utan, vtan, orco, ornor);
1794 psys_particle_on_shape(from, index, fuv, vec, nor, utan, vtan, orco, ornor);
1797 /************************************************/
1799 /************************************************/
1801 void precalc_guides(ParticleSimulationData *sim, ListBase *effectors)
1803 EffectedPoint point;
1807 ParticleSystem *psys = sim->psys;
1808 EffectorWeights *weights = sim->psys->part->effector_weights;
1809 GuideEffectorData *data;
1816 psys_particle_on_emitter(sim->psmd, sim->psys->part->from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, state.co, 0, 0, 0, 0, 0);
1818 mul_m4_v3(sim->ob->obmat, state.co);
1819 mul_mat3_m4_v3(sim->ob->obmat, state.vel);
1821 pd_point_from_particle(sim, pa, &state, &point);
1823 for (eff = effectors->first; eff; eff = eff->next) {
1824 if (eff->pd->forcefield != PFIELD_GUIDE)
1827 if (!eff->guide_data)
1828 eff->guide_data = MEM_callocN(sizeof(GuideEffectorData) * psys->totpart, "GuideEffectorData");
1830 data = eff->guide_data + p;
1832 sub_v3_v3v3(efd.vec_to_point, state.co, eff->guide_loc);
1833 copy_v3_v3(efd.nor, eff->guide_dir);
1834 efd.distance = len_v3(efd.vec_to_point);
1836 copy_v3_v3(data->vec_to_point, efd.vec_to_point);
1837 data->strength = effector_falloff(eff, &efd, &point, weights);
1842 int do_guides(ParticleSettings *part, ListBase *effectors, ParticleKey *state, int index, float time)
1844 CurveMapping *clumpcurve = (part->child_flag & PART_CHILD_USE_CLUMP_CURVE) ? part->clumpcurve : NULL;
1845 CurveMapping *roughcurve = (part->child_flag & PART_CHILD_USE_ROUGH_CURVE) ? part->roughcurve : NULL;
1849 GuideEffectorData *data;
1851 float effect[3] = {0.0f, 0.0f, 0.0f}, veffect[3] = {0.0f, 0.0f, 0.0f};
1852 float guidevec[4], guidedir[3], rot2[4], temp[3];
1853 float guidetime, radius, weight, angle, totstrength = 0.0f;
1854 float vec_to_point[3];
1856 if (effectors) for (eff = effectors->first; eff; eff = eff->next) {
1859 if (pd->forcefield != PFIELD_GUIDE)
1862 data = eff->guide_data + index;
1864 if (data->strength <= 0.0f)
1867 guidetime = time / (1.0f - pd->free_end);
1869 if (guidetime > 1.0f)
1872 cu = (Curve *)eff->ob->data;
1874 if (pd->flag & PFIELD_GUIDE_PATH_ADD) {
1875 if (where_on_path(eff->ob, data->strength * guidetime, guidevec, guidedir, NULL, &radius, &weight) == 0)
1879 if (where_on_path(eff->ob, guidetime, guidevec, guidedir, NULL, &radius, &weight) == 0)
1883 mul_m4_v3(eff->ob->obmat, guidevec);
1884 mul_mat3_m4_v3(eff->ob->obmat, guidedir);
1886 normalize_v3(guidedir);
1888 copy_v3_v3(vec_to_point, data->vec_to_point);
1890 if (guidetime != 0.0f) {
1891 /* curve direction */
1892 cross_v3_v3v3(temp, eff->guide_dir, guidedir);
1893 angle = dot_v3v3(eff->guide_dir, guidedir) / (len_v3(eff->guide_dir));
1894 angle = saacos(angle);
1895 axis_angle_to_quat(rot2, temp, angle);
1896 mul_qt_v3(rot2, vec_to_point);
1899 axis_angle_to_quat(rot2, guidedir, guidevec[3] - eff->guide_loc[3]);
1900 mul_qt_v3(rot2, vec_to_point);
1905 mul_v3_fl(vec_to_point, BKE_displist_calc_taper(eff->scene, cu->taperobj, (int)(data->strength * guidetime * 100.0f), 100));
1907 else { /* curve size*/
1908 if (cu->flag & CU_PATH_RADIUS) {
1909 mul_v3_fl(vec_to_point, radius);
1914 curvemapping_changed_all(clumpcurve);
1916 curvemapping_changed_all(roughcurve);
1920 float par_co[3] = {0.0f, 0.0f, 0.0f};
1921 float par_vel[3] = {0.0f, 0.0f, 0.0f};
1922 float par_rot[4] = {1.0f, 0.0f, 0.0f, 0.0f};
1923 float orco_offset[3] = {0.0f, 0.0f, 0.0f};
1925 copy_v3_v3(key.co, vec_to_point);
1926 do_kink(&key, par_co, par_vel, par_rot, guidetime, pd->kink_freq, pd->kink_shape, pd->kink_amp, 0.f, pd->kink, pd->kink_axis, 0, 0);
1927 do_clump(&key, par_co, guidetime, orco_offset, pd->clump_fac, pd->clump_pow, 1.0f,
1928 part->child_flag & PART_CHILD_USE_CLUMP_NOISE, part->clump_noise_size, clumpcurve);
1929 copy_v3_v3(vec_to_point, key.co);
1932 add_v3_v3(vec_to_point, guidevec);
1934 //sub_v3_v3v3(pa_loc, pa_loc, pa_zero);
1935 madd_v3_v3fl(effect, vec_to_point, data->strength);
1936 madd_v3_v3fl(veffect, guidedir, data->strength);
1937 totstrength += data->strength;
1939 if (pd->flag & PFIELD_GUIDE_PATH_WEIGHT)
1940 totstrength *= weight;
1943 if (totstrength != 0.0f) {
1944 if (totstrength > 1.0f)
1945 mul_v3_fl(effect, 1.0f / totstrength);
1946 CLAMP(totstrength, 0.0f, 1.0f);
1947 //add_v3_v3(effect, pa_zero);
1948 interp_v3_v3v3(state->co, state->co, effect, totstrength);
1950 normalize_v3(veffect);
1951 mul_v3_fl(veffect, len_v3(state->vel));
1952 copy_v3_v3(state->vel, veffect);
1958 static void do_path_effectors(ParticleSimulationData *sim, int i, ParticleCacheKey *ca, int k, int steps, float *UNUSED(rootco), float effector, float UNUSED(dfra), float UNUSED(cfra), float *length, float *vec)
1960 float force[3] = {0.0f, 0.0f, 0.0f};
1961 ParticleKey eff_key;
1962 EffectedPoint epoint;
1964 /* Don't apply effectors for dynamic hair, otherwise the effectors don't get applied twice. */
1965 if (sim->psys->flag & PSYS_HAIR_DYNAMICS)
1968 copy_v3_v3(eff_key.co, (ca - 1)->co);
1969 copy_v3_v3(eff_key.vel, (ca - 1)->vel);
1970 copy_qt_qt(eff_key.rot, (ca - 1)->rot);
1972 pd_point_from_particle(sim, sim->psys->particles + i, &eff_key, &epoint);
1973 pdDoEffectors(sim->psys->effectors, sim->colliders, sim->psys->part->effector_weights, &epoint, force, NULL);
1975 mul_v3_fl(force, effector * powf((float)k / (float)steps, 100.0f * sim->psys->part->eff_hair) / (float)steps);
1977 add_v3_v3(force, vec);
1979 normalize_v3(force);
1982 sub_v3_v3v3(vec, (ca + 1)->co, ca->co);
1984 madd_v3_v3v3fl(ca->co, (ca - 1)->co, force, *length);
1987 *length = len_v3(vec);
1989 static void offset_child(ChildParticle *cpa, ParticleKey *par, float *par_rot, ParticleKey *child, float flat, float radius)
1991 copy_v3_v3(child->co, cpa->fuv);
1992 mul_v3_fl(child->co, radius);
1994 child->co[0] *= flat;
1996 copy_v3_v3(child->vel, par->vel);
1999 mul_qt_v3(par_rot, child->co);
2000 copy_qt_qt(child->rot, par_rot);
2003 unit_qt(child->rot);
2005 add_v3_v3(child->co, par->co);
2007 float *psys_cache_vgroup(DerivedMesh *dm, ParticleSystem *psys, int vgroup)
2012 /* hair dynamics pinning vgroup */
2015 else if (psys->vgroup[vgroup]) {
2016 MDeformVert *dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
2018 int totvert = dm->getNumVerts(dm), i;
2019 vg = MEM_callocN(sizeof(float) * totvert, "vg_cache");
2020 if (psys->vg_neg & (1 << vgroup)) {
2021 for (i = 0; i < totvert; i++)
2022 vg[i] = 1.0f - defvert_find_weight(&dvert[i], psys->vgroup[vgroup] - 1);
2025 for (i = 0; i < totvert; i++)
2026 vg[i] = defvert_find_weight(&dvert[i], psys->vgroup[vgroup] - 1);
2032 void psys_find_parents(ParticleSimulationData *sim, const bool use_render_params)
2034 ParticleSystem *psys = sim->psys;
2035 ParticleSettings *part = sim->psys->part;
2038 ParticleTexture ptex;
2039 int p, totparent, totchild = sim->psys->totchild;
2040 float co[3], orco[3];
2041 int from = PART_FROM_FACE;
2042 totparent = (int)(totchild * part->parents * 0.3f);
2044 if ((sim->psys->renderdata || use_render_params) && part->child_nbr && part->ren_child_nbr)
2045 totparent *= (float)part->child_nbr / (float)part->ren_child_nbr;
2047 /* hard limit, workaround for it being ignored above */
2048 if (sim->psys->totpart < totparent) {
2049 totparent = sim->psys->totpart;
2052 tree = BLI_kdtree_new(totparent);
2054 for (p = 0, cpa = sim->psys->child; p < totparent; p++, cpa++) {
2055 psys_particle_on_emitter(sim->psmd, from, cpa->num, DMCACHE_ISCHILD, cpa->fuv, cpa->foffset, co, 0, 0, 0, orco, 0);
2057 /* Check if particle doesn't exist because of texture influence. Insert only existing particles into kdtree. */
2058 get_cpa_texture(sim->psmd->dm_final, psys, part, psys->particles + cpa->pa[0], p, cpa->num, cpa->fuv, orco, &ptex, PAMAP_DENS | PAMAP_CHILD, psys->cfra);
2060 if (ptex.exist >= psys_frand(psys, p + 24)) {
2061 BLI_kdtree_insert(tree, p, orco);
2065 BLI_kdtree_balance(tree);
2067 for (; p < totchild; p++, cpa++) {
2068 psys_particle_on_emitter(sim->psmd, from, cpa->num, DMCACHE_ISCHILD, cpa->fuv, cpa->foffset, co, 0, 0, 0, orco, 0);
2069 cpa->parent = BLI_kdtree_find_nearest(tree, orco, NULL);
2072 BLI_kdtree_free(tree);
2075 static bool psys_thread_context_init_path(
2076 ParticleThreadContext *ctx, ParticleSimulationData *sim, Scene *scene,
2077 float cfra, const bool editupdate, const bool use_render_params)
2079 ParticleSystem *psys = sim->psys;
2080 ParticleSettings *part = psys->part;
2081 int totparent = 0, between = 0;
2082 int segments = 1 << part->draw_step;
2083 int totchild = psys->totchild;
2085 psys_thread_context_init(ctx, sim);
2087 /*---start figuring out what is actually wanted---*/
2088 if (psys_in_edit_mode(scene, psys)) {
2089 ParticleEditSettings *pset = &scene->toolsettings->particle;
2091 if ((psys->renderdata == 0 && use_render_params == 0) && (psys->edit == NULL || pset->flag & PE_DRAW_PART) == 0)
2094 segments = 1 << pset->draw_step;
2097 if (totchild && part->childtype == PART_CHILD_FACES) {
2098 totparent = (int)(totchild * part->parents * 0.3f);
2100 if ((psys->renderdata || use_render_params) && part->child_nbr && part->ren_child_nbr)
2101 totparent *= (float)part->child_nbr / (float)part->ren_child_nbr;
2103 /* part->parents could still be 0 so we can't test with totparent */
2107 if (psys->renderdata || use_render_params)
2108 segments = 1 << part->ren_step;
2110 totchild = (int)((float)totchild * (float)part->disp / 100.0f);
2111 totparent = MIN2(totparent, totchild);
2117 /* fill context values */
2118 ctx->between = between;
2119 ctx->segments = segments;
2120 if (ELEM(part->kink, PART_KINK_SPIRAL))
2121 ctx->extra_segments = max_ii(part->kink_extra_steps, 1);
2123 ctx->extra_segments = 0;
2124 ctx->totchild = totchild;
2125 ctx->totparent = totparent;
2126 ctx->parent_pass = 0;
2128 ctx->editupdate = editupdate;
2130 psys->lattice_deform_data = psys_create_lattice_deform_data(&ctx->sim);
2132 /* cache all relevant vertex groups if they exist */
2133 ctx->vg_length = psys_cache_vgroup(ctx->dm, psys, PSYS_VG_LENGTH);
2134 ctx->vg_clump = psys_cache_vgroup(ctx->dm, psys, PSYS_VG_CLUMP);
2135 ctx->vg_kink = psys_cache_vgroup(ctx->dm, psys, PSYS_VG_KINK);
2136 ctx->vg_rough1 = psys_cache_vgroup(ctx->dm, psys, PSYS_VG_ROUGH1);
2137 ctx->vg_rough2 = psys_cache_vgroup(ctx->dm, psys, PSYS_VG_ROUGH2);
2138 ctx->vg_roughe = psys_cache_vgroup(ctx->dm, psys, PSYS_VG_ROUGHE);
2139 ctx->vg_twist = psys_cache_vgroup(ctx->dm, psys, PSYS_VG_TWIST);
2140 if (psys->part->flag & PART_CHILD_EFFECT)
2141 ctx->vg_effector = psys_cache_vgroup(ctx->dm, psys, PSYS_VG_EFFECTOR);
2143 /* prepare curvemapping tables */
2144 if ((part->child_flag & PART_CHILD_USE_CLUMP_CURVE) && part->clumpcurve) {
2145 ctx->clumpcurve = curvemapping_copy(part->clumpcurve);
2146 curvemapping_changed_all(ctx->clumpcurve);
2149 ctx->clumpcurve = NULL;
2151 if ((part->child_flag & PART_CHILD_USE_ROUGH_CURVE) && part->roughcurve) {
2152 ctx->roughcurve = curvemapping_copy(part->roughcurve);
2153 curvemapping_changed_all(ctx->roughcurve);
2156 ctx->roughcurve = NULL;
2158 if ((part->child_flag & PART_CHILD_USE_TWIST_CURVE) && part->twistcurve) {
2159 ctx->twistcurve = curvemapping_copy(part->twistcurve);
2160 curvemapping_changed_all(ctx->twistcurve);
2163 ctx->twistcurve = NULL;
2169 static void psys_task_init_path(ParticleTask *task, ParticleSimulationData *sim)
2171 /* init random number generator */
2172 int seed = 31415926 + sim->psys->seed;
2174 task->rng_path = BLI_rng_new(seed);
2177 /* note: this function must be thread safe, except for branching! */
2178 static void psys_thread_create_path(ParticleTask *task, struct ChildParticle *cpa, ParticleCacheKey *child_keys, int i)
2180 ParticleThreadContext *ctx = task->ctx;
2181 Object *ob = ctx->sim.ob;
2182 ParticleSystem *psys = ctx->sim.psys;
2183 ParticleSettings *part = psys->part;
2184 ParticleCacheKey **cache = psys->childcache;
2185 ParticleCacheKey **pcache = psys_in_edit_mode(ctx->sim.scene, psys) && psys->edit ? psys->edit->pathcache : psys->pathcache;
2186 ParticleCacheKey *child, *key[4];
2187 ParticleTexture ptex;
2188 float *cpa_fuv = 0, *par_rot = 0, rot[4];
2189 float orco[3], ornor[3], hairmat[4][4], dvec[3], off1[4][3], off2[4][3];
2190 float eff_length, eff_vec[3], weight[4];
2198 ParticleData *pa = psys->particles + cpa->pa[0];
2200 float foffset, wsum = 0.f;
2202 float p_min = part->parting_min;
2203 float p_max = part->parting_max;
2204 /* Virtual parents don't work nicely with parting. */
2205 float p_fac = part->parents > 0.f ? 0.f : part->parting_fac;
2207 if (ctx->editupdate) {
2210 while (w < 4 && cpa->pa[w] >= 0) {
2211 if (psys->edit->points[cpa->pa[w]].flag & PEP_EDIT_RECALC) {
2221 memset(child_keys, 0, sizeof(*child_keys) * (ctx->segments + 1));
2224 /* get parent paths */
2225 for (w = 0; w < 4; w++) {
2226 if (cpa->pa[w] >= 0) {
2227 key[w] = pcache[cpa->pa[w]];
2228 weight[w] = cpa->w[w];
2236 /* modify weights to create parting */
2238 const ParticleCacheKey *key_0_last = pcache_key_segment_endpoint_safe(key[0]);
2239 for (w = 0; w < 4; w++) {
2240 if (w && (weight[w] > 0.f)) {
2241 const ParticleCacheKey *key_w_last = pcache_key_segment_endpoint_safe(key[w]);
2243 if (part->flag & PART_CHILD_LONG_HAIR) {
2244 /* For long hair use tip distance/root distance as parting factor instead of root to tip angle. */
2245 float d1 = len_v3v3(key[0]->co, key[w]->co);
2246 float d2 = len_v3v3(key_0_last->co, key_w_last->co);
2248 d = d1 > 0.f ? d2 / d1 - 1.f : 10000.f;
2252 sub_v3_v3v3(v1, key_0_last->co, key[0]->co);
2253 sub_v3_v3v3(v2, key_w_last->co, key[w]->co);
2257 d = RAD2DEGF(saacos(dot_v3v3(v1, v2)));
2261 d = (d - p_min) / (p_max - p_min);
2263 d = (d - p_min) <= 0.f ? 0.f : 1.f;
2268 weight[w] *= (1.f - d);
2272 for (w = 0; w < 4; w++)
2275 interp_v4_v4v4(weight, cpa->w, weight, p_fac);
2278 /* get the original coordinates (orco) for texture usage */
2281 foffset = cpa->foffset;
2283 cpa_from = PART_FROM_FACE;
2285 psys_particle_on_emitter(ctx->sim.psmd, cpa_from, cpa_num, DMCACHE_ISCHILD, cpa->fuv, foffset, co, ornor, 0, 0, orco, 0);
2287 mul_m4_v3(ob->obmat, co);
2289 for (w = 0; w < 4; w++)
2290 sub_v3_v3v3(off1[w], co, key[w]->co);
2292 psys_mat_hair_to_global(ob, ctx->sim.psmd->dm_final, psys->part->from, pa, hairmat);
2295 ParticleData *pa = psys->particles + cpa->parent;
2297 if (ctx->editupdate) {
2298 if (!(psys->edit->points[cpa->parent].flag & PEP_EDIT_RECALC))
2301 memset(child_keys, 0, sizeof(*child_keys) * (ctx->segments + 1));
2304 /* get the parent path */
2305 key[0] = pcache[cpa->parent];
2307 /* get the original coordinates (orco) for texture usage */
2308 cpa_from = part->from;
2311 * NOTE: Should in theory be the same as:
2312 cpa_num = psys_particle_dm_face_lookup(
2313 ctx->sim.psmd->dm_final,
2314 ctx->sim.psmd->dm_deformed,
2318 cpa_num = (ELEM(pa->num_dmcache, DMCACHE_ISCHILD, DMCACHE_NOTFOUND))
2322 /* XXX hack to avoid messed up particle num and subsequent crash (#40733) */
2323 if (cpa_num > ctx->sim.psmd->dm_final->getNumTessFaces(ctx->sim.psmd->dm_final))
2327 psys_particle_on_emitter(ctx->sim.psmd, cpa_from, cpa_num, DMCACHE_ISCHILD, cpa_fuv, pa->foffset, co, ornor, 0, 0, orco, 0);
2329 psys_mat_hair_to_global(ob, ctx->sim.psmd->dm_final, psys->part->from, pa, hairmat);
2332 child_keys->segments = ctx->segments;
2334 /* get different child parameters from textures & vgroups */
2335 get_child_modifier_parameters(part, ctx, cpa, cpa_from, cpa_num, cpa_fuv, orco, &ptex);
2337 if (ptex.exist < psys_frand(psys, i + 24)) {
2338 child_keys->segments = -1;
2342 /* create the child path */
2343 for (k = 0, child = child_keys; k <= ctx->segments; k++, child++) {
2348 zero_v3(child->vel);
2349 unit_qt(child->rot);
2351 for (w = 0; w < 4; w++) {
2352 copy_v3_v3(off2[w], off1[w]);
2354 if (part->flag & PART_CHILD_LONG_HAIR) {
2355 /* Use parent rotation (in addition to emission location) to determine child offset. */
2357 mul_qt_v3((key[w] + k)->rot, off2[w]);
2359 /* Fade the effect of rotation for even lengths in the end */
2360 project_v3_v3v3(dvec, off2[w], (key[w] + k)->vel);
2361 madd_v3_v3fl(off2[w], dvec, -(float)k / (float)ctx->segments);
2364 add_v3_v3(off2[w], (key[w] + k)->co);
2367 /* child position is the weighted sum of parent positions */
2368 interp_v3_v3v3v3v3(child->co, off2[0], off2[1], off2[2], off2[3], weight);
2369 interp_v3_v3v3v3v3(child->vel, (key[0] + k)->vel, (key[1] + k)->vel, (key[2] + k)->vel, (key[3] + k)->vel, weight);
2371 copy_qt_qt(child->rot, (key[0] + k)->rot);
2375 mul_qt_qtqt(rot, (key[0] + k)->rot, key[0]->rot);
2379 par_rot = key[0]->rot;
2381 /* offset the child from the parent position */
2382 offset_child(cpa, (ParticleKey *)(key[0] + k), par_rot, (ParticleKey *)child, part->childflat, part->childrad);
2385 child->time = (float)k / (float)ctx->segments;
2388 /* apply effectors */
2389 if (part->flag & PART_CHILD_EFFECT) {
2390 for (k = 0, child = child_keys; k <= ctx->segments; k++, child++) {
2392 do_path_effectors(&ctx->sim, cpa->pa[0], child, k, ctx->segments, child_keys->co, ptex.effector, 0.0f, ctx->cfra, &eff_length, eff_vec);
2395 sub_v3_v3v3(eff_vec, (child + 1)->co, child->co);
2396 eff_length = len_v3(eff_vec);
2402 ParticleData *pa = NULL;
2403 ParticleCacheKey *par = NULL;
2407 if (ctx->totparent) {
2408 if (i >= ctx->totparent) {
2409 pa = &psys->particles[cpa->parent];
2410 /* this is now threadsafe, virtual parents are calculated before rest of children */
2411 BLI_assert(cpa->parent < psys->totchildcache);
2412 par = cache[cpa->parent];
2415 else if (cpa->parent >= 0) {
2416 pa = &psys->particles[cpa->parent];
2417 par = pcache[cpa->parent];
2419 /* If particle is unexisting, try to pick a viable parent from particles used for interpolation. */
2420 for (k = 0; k < 4 && pa && (pa->flag & PARS_UNEXIST); k++) {
2421 if (cpa->pa[k] >= 0) {
2422 pa = &psys->particles[cpa->pa[k]];
2423 par = pcache[cpa->pa[k]];
2427 if (pa->flag & PARS_UNEXIST) pa = NULL;
2432 BLI_listbase_clear(&modifiers);
2434 psys_particle_on_emitter(ctx->sim.psmd, part->from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset,
2435 par_co, NULL, NULL, NULL, par_orco, NULL);
2437 psys_apply_child_modifiers(ctx, &modifiers, cpa, &ptex, orco, ornor, hairmat, child_keys, par, par_orco);
2443 /* Hide virtual parents */
2444 if (i < ctx->totparent)
2445 child_keys->segments = -1;
2448 static void exec_child_path_cache(TaskPool * __restrict UNUSED(pool), void *taskdata, int UNUSED(threadid))
2450 ParticleTask *task = taskdata;
2451 ParticleThreadContext *ctx = task->ctx;
2452 ParticleSystem *psys = ctx->sim.psys;
2453 ParticleCacheKey **cache = psys->childcache;
2457 cpa = psys->child + task->begin;
2458 for (i = task->begin; i < task->end; ++i, ++cpa) {
2459 BLI_assert(i < psys->totchildcache);
2460 psys_thread_create_path(task, cpa, cache[i], i);
2464 void psys_cache_child_paths(
2465 ParticleSimulationData *sim, float cfra,
2466 const bool editupdate, const bool use_render_params)
2468 TaskScheduler *task_scheduler;
2469 TaskPool *task_pool;
2470 ParticleThreadContext ctx;
2471 ParticleTask *tasks_parent, *tasks_child;
2472 int numtasks_parent, numtasks_child;
2473 int i, totchild, totparent;
2475 if (sim->psys->flag & PSYS_GLOBAL_HAIR)
2478 /* create a task pool for child path tasks */
2479 if (!psys_thread_context_init_path(&ctx, sim, sim->scene, cfra, editupdate, use_render_params))
2482 task_scheduler = BLI_task_scheduler_get();
2483 task_pool = BLI_task_pool_create(task_scheduler, &ctx);
2484 totchild = ctx.totchild;
2485 totparent = ctx.totparent;
2487 if (editupdate && sim->psys->childcache && totchild == sim->psys->totchildcache) {
2488 ; /* just overwrite the existing cache */
2491 /* clear out old and create new empty path cache */
2492 free_child_path_cache(sim->psys);
2494 sim->psys->childcache = psys_alloc_path_cache_buffers(&sim->psys->childcachebufs, totchild, ctx.segments + ctx.extra_segments + 1);
2495 sim->psys->totchildcache = totchild;
2498 /* cache parent paths */
2499 ctx.parent_pass = 1;
2500 psys_tasks_create(&ctx, 0, totparent, &tasks_parent, &numtasks_parent);
2501 for (i = 0; i < numtasks_parent; ++i) {
2502 ParticleTask *task = &tasks_parent[i];
2504 psys_task_init_path(task, sim);
2505 BLI_task_pool_push(task_pool, exec_child_path_cache, task, false, TASK_PRIORITY_LOW);
2507 BLI_task_pool_work_and_wait(task_pool);
2509 /* cache child paths */
2510 ctx.parent_pass = 0;
2511 psys_tasks_create(&ctx, totparent, totchild, &tasks_child, &numtasks_child);
2512 for (i = 0; i < numtasks_child; ++i) {
2513 ParticleTask *task = &tasks_child[i];
2515 psys_task_init_path(task, sim);
2516 BLI_task_pool_push(task_pool, exec_child_path_cache, task, false, TASK_PRIORITY_LOW);
2518 BLI_task_pool_work_and_wait(task_pool);
2520 BLI_task_pool_free(task_pool);
2522 psys_tasks_free(tasks_parent, numtasks_parent);
2523 psys_tasks_free(tasks_child, numtasks_child);
2525 psys_thread_context_free(&ctx);
2528 /* figure out incremental rotations along path starting from unit quat */
2529 static void cache_key_incremental_rotation(ParticleCacheKey *key0, ParticleCacheKey *key1, ParticleCacheKey *key2, float *prev_tangent, int i)
2531 float cosangle, angle, tangent[3], normal[3], q[4];
2535 /* start from second key */
2538 /* calculate initial tangent for incremental rotations */
2539 sub_v3_v3v3(prev_tangent, key0->co, key1->co);
2540 normalize_v3(prev_tangent);
2544 sub_v3_v3v3(tangent, key0->co, key1->co);
2545 normalize_v3(tangent);
2547 cosangle = dot_v3v3(tangent, prev_tangent);
2549 /* note we do the comparison on cosangle instead of
2550 * angle, since floating point accuracy makes it give
2551 * different results across platforms */
2552 if (cosangle > 0.999999f) {
2553 copy_v4_v4(key1->rot, key2->rot);
2556 angle = saacos(cosangle);
2557 cross_v3_v3v3(normal, prev_tangent, tangent);
2558 axis_angle_to_quat(q, normal, angle);
2559 mul_qt_qtqt(key1->rot, q, key2->rot);
2562 copy_v3_v3(prev_tangent, tangent);
2567 * Calculates paths ready for drawing/rendering
2568 * - Useful for making use of opengl vertex arrays for super fast strand drawing.
2569 * - Makes child strands possible and creates them too into the cache.
2570 * - Cached path data is also used to determine cut position for the editmode tool. */
2571 void psys_cache_paths(ParticleSimulationData *sim, float cfra, const bool use_render_params)
2574 ParticleEditSettings *pset = &sim->scene->toolsettings->particle;
2575 ParticleSystem *psys = sim->psys;
2576 ParticleSettings *part = psys->part;
2577 ParticleCacheKey *ca, **cache;
2579 DerivedMesh *hair_dm = (psys->part->type == PART_HAIR && psys->flag & PSYS_HAIR_DYNAMICS) ? psys->hair_out_dm : NULL;
2584 ParticleInterpolationData pind;
2585 ParticleTexture ptex;
2589 float birthtime = 0.0, dietime = 0.0;
2590 float t, time = 0.0, dfra = 1.0 /* , frs_sec = sim->scene->r.frs_sec*/ /*UNUSED*/;
2591 float col[4] = {0.5f, 0.5f, 0.5f, 1.0f};
2592 float prev_tangent[3] = {0.0f, 0.0f, 0.0f}, hairmat[4][4];
2595 int segments = (int)pow(2.0, (double)((psys->renderdata || use_render_params) ? part->ren_step : part->draw_step));
2596 int totpart = psys->totpart;
2597 float length, vec[3];
2598 float *vg_effector = NULL;
2599 float *vg_length = NULL, pa_length = 1.0f;
2602 /* we don't have anything valid to create paths from so let's quit here */
2603 if ((psys->flag & PSYS_HAIR_DONE || psys->flag & PSYS_KEYED || psys->pointcache) == 0)
2606 if (psys_in_edit_mode(sim->scene, psys))
2607 if (psys->renderdata == 0 && (psys->edit == NULL || pset->flag & PE_DRAW_PART) == 0)
2610 keyed = psys->flag & PSYS_KEYED;
2611 baked = psys->pointcache->mem_cache.first && psys->part->type != PART_HAIR;
2613 /* clear out old and create new empty path cache */
2614 psys_free_path_cache(psys, psys->edit);
2615 cache = psys->pathcache = psys_alloc_path_cache_buffers(&psys->pathcachebufs, totpart, segments + 1);
2617 psys->lattice_deform_data = psys_create_lattice_deform_data(sim);
2618 ma = give_current_material(sim->ob, psys->part->omat);
2619 if (ma && (psys->part->draw_col == PART_DRAW_COL_MAT))
2620 copy_v3_v3(col, &ma->r);
2622 if ((psys->flag & PSYS_GLOBAL_HAIR) == 0) {
2623 if ((psys->part->flag & PART_CHILD_EFFECT) == 0)
2624 vg_effector = psys_cache_vgroup(psmd->dm_final, psys, PSYS_VG_EFFECTOR);
2626 if (!psys->totchild)
2627 vg_length = psys_cache_vgroup(psmd->dm_final, psys, PSYS_VG_LENGTH);
2630 /* ensure we have tessfaces to be used for mapping */
2631 if (part->from != PART_FROM_VERT) {
2632 DM_ensure_tessface(psmd->dm_final);
2635 /*---first main loop: create all actual particles' paths---*/
2637 if (!psys->totchild) {
2638 psys_get_texture(sim, pa, &ptex, PAMAP_LENGTH, 0.f);
2639 pa_length = ptex.length * (1.0f - part->randlength * psys_frand(psys, psys->seed + p));
2641 pa_length *= psys_particle_value_from_verts(psmd->dm_final, part->from, pa, vg_length);
2645 pind.cache = baked ? psys->pointcache : NULL;
2647 pind.bspline = (psys->part->flag & PART_HAIR_BSPLINE);
2650 memset(cache[p], 0, sizeof(*cache[p]) * (segments + 1));
2652 cache[p]->segments = segments;
2654 /*--get the first data points--*/
2655 init_particle_interpolation(sim->ob, sim->psys, pa, &pind);
2657 /* hairmat is needed for for non-hair particle too so we get proper rotations */
2658 psys_mat_hair_to_global(sim->ob, psmd->dm_final, psys->part->from, pa, hairmat);
2659 copy_v3_v3(rotmat[0], hairmat[2]);
2660 copy_v3_v3(rotmat[1], hairmat[1]);
2661 copy_v3_v3(rotmat[2], hairmat[0]);
2663 if (part->draw & PART_ABS_PATH_TIME) {
2664 birthtime = MAX2(pind.birthtime, part->path_start);
2665 dietime = MIN2(pind.dietime, part->path_end);
2668 float tb = pind.birthtime;
2669 birthtime = tb + part->path_start * (pind.dietime - tb);
2670 dietime = tb + part->path_end * (pind.dietime - tb);
2673 if (birthtime >= dietime) {
2674 cache[p]->segments = -1;
2678 dietime = birthtime + pa_length * (dietime - birthtime);
2680 /*--interpolate actual path from data points--*/
2681 for (k = 0, ca = cache[p]; k <= segments; k++, ca++) {
2682 time = (float)k / (float)segments;
2683 t = birthtime + time * (dietime - birthtime);
2685 do_particle_interpolation(psys, p, pa, t, &pind, &result);
2686 copy_v3_v3(ca->co, result.co);
2688 /* dynamic hair is in object space */
2689 /* keyed and baked are already in global space */
2691 mul_m4_v3(sim->ob->obmat, ca->co);
2692 else if (!keyed && !baked && !(psys->flag & PSYS_GLOBAL_HAIR))
2693 mul_m4_v3(hairmat, ca->co);
2695 copy_v3_v3(ca->col, col);
2698 if (part->type == PART_HAIR) {
2701 for (k = 0, hkey = pa->hair; k < pa->totkey; ++k, ++hkey) {
2702 mul_v3_m4v3(hkey->world_co, hairmat, hkey->co);
2706 /*--modify paths and calculate rotation & velocity--*/
2708 if (!(psys->flag & PSYS_GLOBAL_HAIR)) {
2709 /* apply effectors */
2710 if ((psys->part->flag & PART_CHILD_EFFECT) == 0) {
2711 float effector = 1.0f;
2713 effector *= psys_particle_value_from_verts(psmd->dm_final, psys->part->from, pa, vg_effector);
2715 sub_v3_v3v3(vec, (cache[p] + 1)->co, cache[p]->co);
2716 length = len_v3(vec);
2718 for (k = 1, ca = cache[p] + 1; k <= segments; k++, ca++)
2719 do_path_effectors(sim, p, ca, k, segments, cache[p]->co, effector, dfra, cfra, &length, vec);
2722 /* apply guide curves to path data */
2723 if (sim->psys->effectors && (psys->part->flag & PART_CHILD_EFFECT) == 0) {
2724 for (k = 0, ca = cache[p]; k <= segments; k++, ca++)
2725 /* ca is safe to cast, since only co and vel are used */
2726 do_guides(sim->psys->part, sim->psys->effectors, (ParticleKey *)ca, p, (float)k / (float)segments);
2729 /* lattices have to be calculated separately to avoid mixups between effector calculations */
2730 if (psys->lattice_deform_data) {
2731 for (k = 0, ca = cache[p]; k <= segments; k++, ca++)
2732 calc_latt_deform(psys->lattice_deform_data, ca->co, psys->lattice_strength);
2736 /* finally do rotation & velocity */
2737 for (k = 1, ca = cache[p] + 1; k <= segments; k++, ca++) {
2738 cache_key_incremental_rotation(ca, ca - 1, ca - 2, prev_tangent, k);
2741 copy_qt_qt(ca->rot, (ca - 1)->rot);
2744 sub_v3_v3v3(ca->vel, ca->co, (ca - 1)->co);
2747 copy_v3_v3((ca - 1)->vel, ca->vel);
2749 ca->time = (float)k / (float)segments;
2751 /* First rotation is based on emitting face orientation.
2752 * This is way better than having flipping rotations resulting
2753 * from using a global axis as a rotation pole (vec_to_quat()).
2754 * It's not an ideal solution though since it disregards the
2755 * initial tangent, but taking that in to account will allow
2756 * the possibility of flipping again. -jahka
2758 mat3_to_quat_is_ok(cache[p]->rot, rotmat);
2761 psys->totcached = totpart;
2763 if (psys->lattice_deform_data) {
2764 end_latt_deform(psys->lattice_deform_data);
2765 psys->lattice_deform_data = NULL;
2769 MEM_freeN(vg_effector);
2772 MEM_freeN(vg_length);
2774 void psys_cache_edit_paths(Scene *scene, Object *ob, PTCacheEdit *edit, float cfra, const bool use_render_params)
2776 ParticleCacheKey *ca, **cache = edit->pathcache;
2777 ParticleEditSettings *pset = &scene->toolsettings->particle;
2779 PTCacheEditPoint *point = NULL;
2780 PTCacheEditKey *ekey = NULL;
2782 ParticleSystem *psys = edit->psys;
2783 ParticleSystemModifierData *psmd = psys_get_modifier(ob, psys);
2784 ParticleData *pa = psys ? psys->particles : NULL;
2786 ParticleInterpolationData pind;
2789 float birthtime = 0.0f, dietime = 0.0f;
2790 float t, time = 0.0f, keytime = 0.0f /*, frs_sec */;
2791 float hairmat[4][4], rotmat[3][3], prev_tangent[3] = {0.0f, 0.0f, 0.0f};
2793 int segments = 1 << pset->draw_step;
2794 int totpart = edit->totpoint, recalc_set = 0;
2798 segments = MAX2(segments, 4);
2800 if (!cache || edit->totpoint != edit->totcached) {
2801 /* clear out old and create new empty path cache */
2802 psys_free_path_cache(edit->psys, edit);
2803 cache = edit->pathcache = psys_alloc_path_cache_buffers(&edit->pathcachebufs, totpart, segments + 1);
2805 /* set flag for update (child particles check this too) */
2806 for (i = 0, point = edit->points; i < totpart; i++, point++)
2807 point->flag |= PEP_EDIT_RECALC;
2811 /* frs_sec = (psys || edit->pid.flag & PTCACHE_VEL_PER_SEC) ? 25.0f : 1.0f; */ /* UNUSED */
2813 const bool use_weight = (pset->brushtype == PE_BRUSH_WEIGHT) && (psys != NULL) && (psys->particles != NULL);
2816 ; /* use weight painting colors now... */
2819 sel_col[0] = (float)edit->sel_col[0] / 255.0f;
2820 sel_col[1] = (float)edit->sel_col[1] / 255.0f;
2821 sel_col[2] = (float)edit->sel_col[2] / 255.0f;
2822 nosel_col[0] = (float)edit->nosel_col[0] / 255.0f;
2823 nosel_col[1] = (float)edit->nosel_col[1] / 255.0f;
2824 nosel_col[2] = (float)edit->nosel_col[2] / 255.0f;
2827 /*---first main loop: create all actual particles' paths---*/
2828 for (i = 0, point = edit->points; i < totpart; i++, pa += pa ? 1 : 0, point++) {
2829 if (edit->totcached && !(point->flag & PEP_EDIT_RECALC))
2832 if (point->totkey == 0)
2839 pind.epoint = point;
2840 pind.bspline = psys ? (psys->part->flag & PART_HAIR_BSPLINE) : 0;
2844 /* should init_particle_interpolation set this ? */
2846 pind.hkey[0] = NULL;
2847 /* pa != NULL since the weight brush is only available for hair */
2848 pind.hkey[0] = pa->hair;
2849 pind.hkey[1] = pa->hair + 1;
2853 memset(cache[i], 0, sizeof(*cache[i]) * (segments + 1));
2855 cache[i]->segments = segments;
2857 /*--get the first data points--*/
2858 init_particle_interpolation(ob, psys, pa, &pind);
2861 psys_mat_hair_to_global(ob, psmd->dm_final, psys->part->from, pa, hairmat);
2862 copy_v3_v3(rotmat[0], hairmat[2]);
2863 copy_v3_v3(rotmat[1], hairmat[1]);
2864 copy_v3_v3(rotmat[2], hairmat[0]);
2867 birthtime = pind.birthtime;
2868 dietime = pind.dietime;
2870 if (birthtime >= dietime) {
2871 cache[i]->segments = -1;
2875 /*--interpolate actual path from data points--*/
2876 for (k = 0, ca = cache[i]; k <= segments; k++, ca++) {
2877 time = (float)k / (float)segments;
2878 t = birthtime + time * (dietime - birthtime);
2880 do_particle_interpolation(psys, i, pa, t, &pind, &result);
2881 copy_v3_v3(ca->co, result.co);
2883 /* non-hair points are already in global space */
2884 if (psys && !(psys->flag & PSYS_GLOBAL_HAIR)) {
2885 mul_m4_v3(hairmat, ca->co);
2888 cache_key_incremental_rotation(ca, ca - 1, ca - 2, prev_tangent, k);
2891 copy_qt_qt(ca->rot, (ca - 1)->rot);
2894 sub_v3_v3v3(ca->vel, ca->co, (ca - 1)->co);
2897 copy_v3_v3((ca - 1)->vel, ca->vel);
2901 ca->vel[0] = ca->vel[1] = 0.0f;
2905 /* selection coloring in edit mode */
2908 weight_to_rgb(ca->col, pind.hkey[1]->weight);
2911 /* warning: copied from 'do_particle_interpolation' (without 'mvert' array stepping) */
2913 if (result.time < 0.0f) {
2914 real_t = -result.time;
2917 real_t = pind.hkey[0]->time + t * (pind.hkey[0][pa->totkey - 1].time - pind.hkey[0]->time);
2920 while (pind.hkey[1]->time < real_t) {
2923 pind.hkey[0] = pind.hkey[1] - 1;
2928 keytime = (t - (*pind.ekey[0]->time)) / ((*pind.ekey[1]->time) - (*pind.ekey[0]->time));
2930 weight_to_rgb(w1, pind.hkey[0]->weight);
2931 weight_to_rgb(w2, pind.hkey[1]->weight);
2933 interp_v3_v3v3(ca->col, w1, w2, keytime);
2937 if ((ekey + (pind.ekey[0] - point->keys))->flag & PEK_SELECT) {
2938 if ((ekey + (pind.ekey[1] - point->keys))->flag & PEK_SELECT) {
2939 copy_v3_v3(ca->col, sel_col);
2942 keytime = (t - (*pind.ekey[0]->time)) / ((*pind.ekey[1]->time) - (*pind.ekey[0]->time));
2943 interp_v3_v3v3(ca->col, sel_col, nosel_col, keytime);
2947 if ((ekey + (pind.ekey[1] - point->keys))->flag & PEK_SELECT) {
2948 keytime = (t - (*pind.ekey[0]->time)) / ((*pind.ekey[1]->time) - (*pind.ekey[0]->time));
2949 interp_v3_v3v3(ca->col, nosel_col, sel_col, keytime);
2952 copy_v3_v3(ca->col, nosel_col);
2959 if (psys && !(psys->flag & PSYS_GLOBAL_HAIR)) {
2960 /* First rotation is based on emitting face orientation.
2961 * This is way better than having flipping rotations resulting
2962 * from using a global axis as a rotation pole (vec_to_quat()).
2963 * It's not an ideal solution though since it disregards the
2964 * initial tangent, but taking that in to account will allow
2965 * the possibility of flipping again. -jahka
2967 mat3_to_quat_is_ok(cache[i]->rot, rotmat);
2971 edit->totcached = totpart;
2974 ParticleSimulationData sim = {0};
2978 sim.psmd = psys_get_modifier(ob, psys);
2980 psys_cache_child_paths(&sim, cfra, true, use_render_params);
2983 /* clear recalc flag if set here */
2985 for (i = 0, point = edit->points; i < totpart; i++, point++)
2986 point->flag &= ~PEP_EDIT_RECALC;
2989 /************************************************/
2990 /* Particle Key handling */
2991 /************************************************/
2992 void copy_particle_key(ParticleKey *to, ParticleKey *from, int time)
2995 memcpy(to, from, sizeof(ParticleKey));
2998 float to_time = to->time;
2999 memcpy(to, from, sizeof(ParticleKey));
3003 void psys_get_from_key(ParticleKey *key, float loc[3], float vel[3], float rot[4], float *time)
3005 if (loc) copy_v3_v3(loc, key->co);
3006 if (vel) copy_v3_v3(vel, key->vel);
3007 if (rot) copy_qt_qt(rot, key->rot);
3008 if (time) *time = key->time;
3010 /*-------changing particle keys from space to another-------*/
3012 static void key_from_object(Object *ob, ParticleKey *key)
3016 add_v3_v3(key->vel, key->co);
3018 mul_m4_v3(ob->obmat, key->co);
3019 mul_m4_v3(ob->obmat, key->vel);
3020 mat4_to_quat(q, ob->obmat);
3022 sub_v3_v3v3(key->vel, key->vel, key->co);
3023 mul_qt_qtqt(key->rot, q, key->rot);
3027 static void triatomat(float *v1, float *v2, float *v3, float (*uv)[2], float mat[4][4])
3029 float det, w1, w2, d1[2], d2[2];
3031 memset(mat, 0, sizeof(float) * 4 * 4);
3034 /* first axis is the normal */
3035 normal_tri_v3(mat[2], v1, v2, v3);
3037 /* second axis along (1, 0) in uv space */
3039 d1[0] = uv[1][0] - uv[0][0];
3040 d1[1] = uv[1][1] - uv[0][1];
3041 d2[0] = uv[2][0] - uv[0][0];
3042 d2[1] = uv[2][1] - uv[0][1];
3044 det = d2[0] * d1[1] - d2[1] * d1[0];
3051 mat[1][0] = w1 * (v2[0] - v1[0]) + w2 * (v3[0] - v1[0]);
3052 mat[1][1] = w1 * (v2[1] - v1[1]) + w2 * (v3[1] - v1[1]);
3053 mat[1][2] = w1 * (v2[2] - v1[2]) + w2 * (v3[2] - v1[2]);
3054 normalize_v3(mat[1]);
3057 mat[1][0] = mat[1][1] = mat[1][2] = 0.0f;
3060 sub_v3_v3v3(mat[1], v2, v1);
3061 normalize_v3(mat[1]);
3064 /* third as a cross product */
3065 cross_v3_v3v3(mat[0], mat[1], mat[2]);
3068 static void psys_face_mat(Object *ob, DerivedMesh *dm, ParticleData *pa, float mat[4][4], int orco)
3072 OrigSpaceFace *osface;
3073 float (*orcodata)[3];
3075 int i = (ELEM(pa->num_dmcache, DMCACHE_ISCHILD, DMCACHE_NOTFOUND)) ? pa->num : pa->num_dmcache;
3076 if (i == -1 || i >= dm->getNumTessFaces(dm)) { unit_m4(mat); return; }
3078 mface = dm->getTessFaceData(dm, i, CD_MFACE);
3079 osface = dm->getTessFaceData(dm, i, CD_ORIGSPACE);
3081 if (orco && (orcodata = dm->getVertDataArray(dm, CD_ORCO))) {
3082 copy_v3_v3(v[0], orcodata[mface->v1]);
3083 copy_v3_v3(v[1], orcodata[mface->v2]);
3084 copy_v3_v3(v[2], orcodata[mface->v3]);
3086 /* ugly hack to use non-transformed orcos, since only those
3087 * give symmetric results for mirroring in particle mode */
3088 if (DM_get_vert_data_layer(dm, CD_ORIGINDEX))
3089 BKE_mesh_orco_verts_transform(ob->data, v, 3, 1);
3092 dm->getVertCo(dm, mface->v1, v[0]);
3093 dm->getVertCo(dm, mface->v2, v[1]);
3094 dm->getVertCo(dm, mface->v3, v[2]);
3097 triatomat(v[0], v[1], v[2], (osface) ? osface->uv : NULL, mat);
3100 void psys_mat_hair_to_object(Object *UNUSED(ob), DerivedMesh *dm, short from, ParticleData *pa, float hairmat[4][4])
3104 /* can happen when called from a different object's modifier */
3110 psys_face_mat(0, dm, pa, hairmat, 0);
3111 psys_particle_on_dm(dm, from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, vec, 0, 0, 0, 0, 0);
3112 copy_v3_v3(hairmat[3], vec);