Blender V4.5
particle.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2007 by Janne Karhu. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9/* Allow using deprecated functionality for .blend file I/O. */
10#define DNA_DEPRECATED_ALLOW
11
12#include <algorithm>
13#include <cmath>
14#include <cstdlib>
15#include <cstring>
16#include <optional>
17
18#include "MEM_guardedalloc.h"
19
20#include "DNA_defaults.h"
21
22#include "DNA_cloth_types.h"
24#include "DNA_curve_types.h"
26#include "DNA_fluid_types.h"
27#include "DNA_key_types.h"
28#include "DNA_material_types.h"
29#include "DNA_mesh_types.h"
30#include "DNA_meshdata_types.h"
32#include "DNA_particle_types.h"
33#include "DNA_scene_types.h"
34#include "DNA_texture_types.h"
35
36#include "BLI_kdopbvh.hh"
37#include "BLI_kdtree.h"
38#include "BLI_linklist.h"
39#include "BLI_listbase.h"
40#include "BLI_math_base_safe.h"
41#include "BLI_math_geom.h"
42#include "BLI_math_matrix.h"
43#include "BLI_math_rotation.h"
44#include "BLI_math_vector.h"
45#include "BLI_rand.h"
46#include "BLI_string.h"
47#include "BLI_task.h"
48#include "BLI_threads.h"
49#include "BLI_utildefines.h"
50
51#include "BLT_translation.hh"
52
53#include "BKE_anim_path.h"
54#include "BKE_boids.h"
55#include "BKE_cloth.hh"
56#include "BKE_collection.hh"
57#include "BKE_colortools.hh"
58#include "BKE_customdata.hh"
59#include "BKE_deform.hh"
60#include "BKE_displist.h"
61#include "BKE_effect.h"
62#include "BKE_idtype.hh"
63#include "BKE_key.hh"
64#include "BKE_lattice.hh"
65#include "BKE_layer.hh"
66#include "BKE_lib_id.hh"
67#include "BKE_lib_query.hh"
68#include "BKE_material.hh"
69#include "BKE_mesh.hh"
71#include "BKE_modifier.hh"
72#include "BKE_object.hh"
73#include "BKE_particle.h"
74#include "BKE_pointcache.h"
75#include "BKE_scene.hh"
76#include "BKE_texture.h"
77
78#include "DEG_depsgraph.hh"
81
82#include "RE_texture.h"
83
84#include "BLO_read_write.hh"
85
86#include "particle_private.h"
87
88using blender::float3;
89
90static void fluid_free_settings(SPHFluidSettings *fluid);
91
92static void particle_settings_init(ID *id)
93{
94 ParticleSettings *particle_settings = (ParticleSettings *)id;
95 BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(particle_settings, id));
96
98
99 particle_settings->effector_weights = BKE_effector_add_weights(nullptr);
100 particle_settings->pd = BKE_partdeflect_new(PFIELD_NULL);
101 particle_settings->pd2 = BKE_partdeflect_new(PFIELD_NULL);
102}
103
104static void particle_settings_copy_data(Main * /*bmain*/,
105 std::optional<Library *> /*owner_library*/,
106 ID *id_dst,
107 const ID *id_src,
108 const int /*flag*/)
109{
110 ParticleSettings *particle_settings_dst = (ParticleSettings *)id_dst;
111 const ParticleSettings *partticle_settings_src = (const ParticleSettings *)id_src;
112
113 particle_settings_dst->pd = BKE_partdeflect_copy(partticle_settings_src->pd);
114 particle_settings_dst->pd2 = BKE_partdeflect_copy(partticle_settings_src->pd2);
115 particle_settings_dst->effector_weights = static_cast<EffectorWeights *>(
116 MEM_dupallocN(partticle_settings_src->effector_weights));
117 particle_settings_dst->fluid = static_cast<SPHFluidSettings *>(
118 MEM_dupallocN(partticle_settings_src->fluid));
119
120 if (partticle_settings_src->clumpcurve) {
121 particle_settings_dst->clumpcurve = BKE_curvemapping_copy(partticle_settings_src->clumpcurve);
122 }
123 if (partticle_settings_src->roughcurve) {
124 particle_settings_dst->roughcurve = BKE_curvemapping_copy(partticle_settings_src->roughcurve);
125 }
126 if (partticle_settings_src->twistcurve) {
127 particle_settings_dst->twistcurve = BKE_curvemapping_copy(partticle_settings_src->twistcurve);
128 }
129
130 particle_settings_dst->boids = boid_copy_settings(partticle_settings_src->boids);
131
132 for (int a = 0; a < MAX_MTEX; a++) {
133 if (partticle_settings_src->mtex[a]) {
134 particle_settings_dst->mtex[a] = static_cast<MTex *>(
135 MEM_dupallocN(partticle_settings_src->mtex[a]));
136 }
137 }
138
139 BLI_duplicatelist(&particle_settings_dst->instance_weights,
140 &partticle_settings_src->instance_weights);
141}
142
144{
145 ParticleSettings *particle_settings = (ParticleSettings *)id;
146
147 for (int a = 0; a < MAX_MTEX; a++) {
148 MEM_SAFE_FREE(particle_settings->mtex[a]);
149 }
150
151 if (particle_settings->clumpcurve) {
152 BKE_curvemapping_free(particle_settings->clumpcurve);
153 }
154 if (particle_settings->roughcurve) {
155 BKE_curvemapping_free(particle_settings->roughcurve);
156 }
157 if (particle_settings->twistcurve) {
158 BKE_curvemapping_free(particle_settings->twistcurve);
159 }
160
161 BKE_partdeflect_free(particle_settings->pd);
162 BKE_partdeflect_free(particle_settings->pd2);
163
164 MEM_SAFE_FREE(particle_settings->effector_weights);
165
166 BLI_freelistN(&particle_settings->instance_weights);
167
168 boid_free_settings(particle_settings->boids);
169 fluid_free_settings(particle_settings->fluid);
170}
171
173{
174 ParticleSettings *psett = reinterpret_cast<ParticleSettings *>(id);
176
181
182 for (int i = 0; i < MAX_MTEX; i++) {
183 if (psett->mtex[i]) {
186 }
187 }
188
189 if (psett->effector_weights) {
191 }
192
193 if (psett->pd) {
196 }
197 if (psett->pd2) {
200 }
201
202 if (psett->boids) {
204 LISTBASE_FOREACH (BoidRule *, rule, &state->rules) {
205 if (ELEM(rule->type, eBoidRuleType_Avoid, eBoidRuleType_Goal)) {
206 BoidRuleGoalAvoid *gabr = (BoidRuleGoalAvoid *)rule;
208 }
209 else if (rule->type == eBoidRuleType_FollowLeader) {
212 }
213 }
214 }
215 }
216
219 }
220
223 }
224}
225
227{
229
230 LISTBASE_FOREACH (BoidRule *, rule, &state->rules) {
231 switch (rule->type) {
234 BLO_write_struct(writer, BoidRuleGoalAvoid, rule);
235 break;
238 break;
241 break;
244 break;
246 BLO_write_struct(writer, BoidRuleFight, rule);
247 break;
248 default:
249 BLO_write_struct(writer, BoidRule, rule);
250 break;
251 }
252 }
253#if 0
254 BoidCondition *cond = state->conditions.first;
255 for (; cond; cond = cond->next) {
256 BLO_write_struct(writer, BoidCondition, cond);
257 }
258#endif
259}
260
261static void particle_settings_blend_write(BlendWriter *writer, ID *id, const void *id_address)
262{
264
265 /* write LibData */
266 BLO_write_id_struct(writer, ParticleSettings, id_address, &part->id);
267 BKE_id_blend_write(writer, &part->id);
268
269 BLO_write_struct(writer, PartDeflect, part->pd);
270 BLO_write_struct(writer, PartDeflect, part->pd2);
272
273 if (part->clumpcurve) {
275 }
276 if (part->roughcurve) {
278 }
279 if (part->twistcurve) {
281 }
282
284 /* update indices, but only if dw->ob is set (can be nullptr after loading e.g.) */
285 if (dw->ob != nullptr) {
286 dw->index = 0;
287 if (part->instance_collection) { /* can be nullptr if lining fails or set to None */
289 if (object == dw->ob) {
290 break;
291 }
292 dw->index++;
293 }
295 }
296 }
298 }
299
300 if (part->boids && part->phystype == PART_PHYS_BOIDS) {
301 BLO_write_struct(writer, BoidSettings, part->boids);
302
304 write_boid_state(writer, state);
305 }
306 }
307 if (part->fluid && part->phystype == PART_PHYS_FLUID) {
309 }
310
311 for (int a = 0; a < MAX_MTEX; a++) {
312 if (part->mtex[a]) {
313 BLO_write_struct(writer, MTex, part->mtex[a]);
314 }
315 }
316}
317
321
323static size_t boid_rule_struct_size(const eBoidRuleType type)
324{
325 switch (type) {
329 return sizeof(BoidRule);
332 return sizeof(BoidRuleGoalAvoid);
334 return sizeof(BoidRuleAvoidCollision);
336 return sizeof(BoidRuleFollowLeader);
338 return sizeof(BoidRuleAverageSpeed);
340 return sizeof(BoidRuleFight);
341 }
342 return 0;
343}
344
346{
348
349 BLO_read_struct(reader, PartDeflect, &part->pd);
350 BLO_read_struct(reader, PartDeflect, &part->pd2);
353
354 BLO_read_struct(reader, CurveMapping, &part->clumpcurve);
355 if (part->clumpcurve) {
357 }
358 BLO_read_struct(reader, CurveMapping, &part->roughcurve);
359 if (part->roughcurve) {
361 }
362 BLO_read_struct(reader, CurveMapping, &part->twistcurve);
363 if (part->twistcurve) {
365 }
366
368 if (!part->effector_weights) {
369 part->effector_weights = BKE_effector_add_weights(part->force_group);
370 }
371
373
374 BLO_read_struct(reader, BoidSettings, &part->boids);
375 BLO_read_struct(reader, SPHFluidSettings, &part->fluid);
376
377 if (part->boids) {
378 BLO_read_struct_list(reader, BoidState, &part->boids->states);
379
382 reader, &state->rules, [](const BoidRule &rule) {
383 return std::max(boid_rule_struct_size(eBoidRuleType(rule.type)), sizeof(BoidRule));
384 });
385
386 /* Disable rules of unknown type, for upward compatibility. */
387 LISTBASE_FOREACH (BoidRule *, rule, &state->rules) {
388 if (boid_rule_struct_size(eBoidRuleType(rule->type)) == 0) {
389 rule->type = eBoidRuleType_None;
390 }
391 }
392#if 0
393 /* Not implemented yet. */
394 BLO_read_struct_list(reader, BoidCondition, &state->conditions);
395 BLO_read_struct_list(reader, BoidAction, &state->actions);
396#endif
397 }
398 }
399 for (int a = 0; a < MAX_MTEX; a++) {
400 BLO_read_struct(reader, MTex, &part->mtex[a]);
401 }
402
403 /* Protect against integer overflow vulnerability. */
404 CLAMP(part->trail_count, 1, 100000);
405}
406
408{
409 ParticleSettings *part = reinterpret_cast<ParticleSettings *>(id);
410
411 if (part->instance_weights.first && !part->instance_collection) {
413 }
414}
415
417 /*id_code*/ ParticleSettings::id_type,
418 /*id_filter*/ FILTER_ID_PA,
419 /*dependencies_id_types*/ FILTER_ID_OB | FILTER_ID_GR | FILTER_ID_TE,
420 /*main_listbase_index*/ INDEX_ID_PA,
421 /*struct_size*/ sizeof(ParticleSettings),
422 /*name*/ "ParticleSettings",
423 /*name_plural*/ N_("particles"),
424 /*translation_context*/ BLT_I18NCONTEXT_ID_PARTICLESETTINGS,
425 /*flags*/ 0,
426 /*asset_type_info*/ nullptr,
427
428 /*init_data*/ particle_settings_init,
429 /*copy_data*/ particle_settings_copy_data,
430 /*free_data*/ particle_settings_free_data,
431 /*make_local*/ nullptr,
432 /*foreach_id*/ particle_settings_foreach_id,
433 /*foreach_cache*/ nullptr,
434 /*foreach_path*/ nullptr,
435 /*owner_pointer_get*/ nullptr,
436
437 /*blend_write*/ particle_settings_blend_write,
438 /*blend_read_data*/ particle_settings_blend_read_data,
439 /*blend_read_after_liblink*/ particle_settings_blend_read_after_liblink,
440
441 /*blend_read_undo_preserve*/ nullptr,
442
443 /*lib_override_apply_post*/ nullptr,
444};
445
449
451{
452 RNG *rng = BLI_rng_new_srandom(5831); /* arbitrary */
453 for (int i = 0; i < PSYS_FRAND_COUNT; i++) {
457 }
458 BLI_rng_free(rng);
459}
460
463 ChildParticle *cpa,
464 short cpa_from,
465 int cpa_num,
466 float *cpa_fuv,
467 float *orco,
468 ParticleTexture *ptex);
469static void get_cpa_texture(Mesh *mesh,
470 ParticleSystem *psys,
471 ParticleSettings *part,
472 ParticleData *par,
473 int child_index,
474 int face_index,
475 const float fw[4],
476 float *orco,
477 ParticleTexture *ptex,
478 int event,
479 float cfra);
480
482{
483 ParticleSettings *part = psys->part;
485 int tot = 0;
486
488 {
489 if (pa->alive == PARS_UNBORN && (part->flag & PART_UNBORN) == 0) {
490 }
491 else if (pa->alive == PARS_DEAD && (part->flag & PART_DIED) == 0) {
492 }
493 else {
494 tot++;
495 }
496 }
497 return tot;
498}
499int count_particles_mod(ParticleSystem *psys, int totgr, int cur)
500{
501 ParticleSettings *part = psys->part;
503 int tot = 0;
504
506 {
507 if (pa->alive == PARS_UNBORN && (part->flag & PART_UNBORN) == 0) {
508 }
509 else if (pa->alive == PARS_DEAD && (part->flag & PART_DIED) == 0) {
510 }
511 else if (p % totgr == cur) {
512 tot++;
513 }
514 }
515 return tot;
516}
517/* We allocate path cache memory in chunks instead of a big contiguous
518 * chunk, windows' memory allocator fails to find big blocks of memory often. */
519
520#define PATH_CACHE_BUF_SIZE 1024
521
523{
524 return (key->segments > 0) ? (key + (key->segments - 1)) : key;
525}
526
527static ParticleCacheKey **psys_alloc_path_cache_buffers(ListBase *bufs, int tot, int totkeys)
528{
529 LinkData *buf;
530 ParticleCacheKey **cache;
531 int i, totkey, totbufkey;
532
533 tot = std::max(tot, 1);
534 totkey = 0;
535 cache = MEM_calloc_arrayN<ParticleCacheKey *>(tot, "PathCacheArray");
536
537 while (totkey < tot) {
538 totbufkey = std::min(tot - totkey, PATH_CACHE_BUF_SIZE);
539 buf = MEM_callocN<LinkData>("PathCacheLinkData");
540 buf->data = MEM_calloc_arrayN<ParticleCacheKey>(size_t(totbufkey) * size_t(totkeys),
541 "ParticleCacheKey");
542
543 for (i = 0; i < totbufkey; i++) {
544 cache[totkey + i] = ((ParticleCacheKey *)buf->data) + i * totkeys;
545 }
546
547 totkey += totbufkey;
548 BLI_addtail(bufs, buf);
549 }
550
551 return cache;
552}
553
555{
556 if (cache) {
557 MEM_freeN(cache);
558 }
559
560 LISTBASE_FOREACH (LinkData *, buf, bufs) {
561 MEM_freeN((ParticleCacheKey *)buf->data);
562 }
563 BLI_freelistN(bufs);
564}
565
566/************************************************/
567/* Getting stuff */
568/************************************************/
569
571{
572 if (ob == nullptr) {
573 return nullptr;
574 }
575
577 if (psys->flag & PSYS_CURRENT) {
578 return psys;
579 }
580 }
581
582 return nullptr;
583}
585{
586 ParticleSystem *psys;
587 short i;
588
589 if (ob == nullptr) {
590 return 0;
591 }
592
593 for (psys = static_cast<ParticleSystem *>(ob->particlesystem.first), i = 0; psys;
594 psys = psys->next, i++)
595 {
596 if (psys->flag & PSYS_CURRENT) {
597 return i;
598 }
599 }
600
601 return i;
602}
603void psys_set_current_num(Object *ob, int index)
604{
605 ParticleSystem *psys;
606 short i;
607
608 if (ob == nullptr) {
609 return;
610 }
611
612 for (psys = static_cast<ParticleSystem *>(ob->particlesystem.first), i = 0; psys;
613 psys = psys->next, i++)
614 {
615 if (i == index) {
616 psys->flag |= PSYS_CURRENT;
617 }
618 else {
619 psys->flag &= ~PSYS_CURRENT;
620 }
621 }
622}
623
625{
626 ParticleSystem *psys = sim->psys;
627 ParticleSettings *part = psys->part;
628
629 /* Prepare lattice deform. */
630 psys->lattice_deform_data = nullptr;
631 if (psys_in_edit_mode(sim->depsgraph, sim->psys) == 0) {
632 Object *lattice = nullptr;
634 bool for_render = DEG_get_mode(sim->depsgraph) == DAG_EVAL_RENDER;
635 int mode = for_render ? eModifierMode_Render : eModifierMode_Realtime;
636
637 for (; md; md = md->next) {
638 if (md->type == eModifierType_Lattice) {
639 if (md->mode & mode) {
641 lattice = lmd->object;
642 psys->lattice_strength = lmd->strength;
643 }
644
645 break;
646 }
647 }
648 if (lattice) {
649 psys->lattice_deform_data = BKE_lattice_deform_data_create(lattice, nullptr);
650 }
651 }
652
653 /* Prepare curvemapping tables. */
654 if ((part->child_flag & PART_CHILD_USE_CLUMP_CURVE) && part->clumpcurve) {
656 }
657 if ((part->child_flag & PART_CHILD_USE_ROUGH_CURVE) && part->roughcurve) {
659 }
660 if ((part->child_flag & PART_CHILD_USE_TWIST_CURVE) && part->twistcurve) {
662 }
663}
664
666{
667 ParticleSystem *psys = sim->psys;
668
669 if (psys->lattice_deform_data) {
671 psys->lattice_deform_data = nullptr;
672 }
673}
674
676{
677 ParticleSystem *psys = static_cast<ParticleSystem *>(ob->particlesystem.first);
678
679 for (; psys; psys = psys->next) {
680 psys->flag |= PSYS_DISABLED;
681 }
682}
684{
685 ParticleSystem *psys = static_cast<ParticleSystem *>(ob->particlesystem.first);
686
687 for (; psys; psys = psys->next) {
688 psys->flag &= ~PSYS_DISABLED;
689 }
690}
691
693{
694 if (psys->orig_psys == nullptr) {
695 return psys;
696 }
697 return psys->orig_psys;
698}
699
700ParticleSystem *psys_eval_get(Depsgraph *depsgraph, Object *object, ParticleSystem *psys)
701{
702 Object *object_eval = DEG_get_evaluated(depsgraph, object);
703 if (object_eval == object) {
704 return psys;
705 }
706 ParticleSystem *psys_eval = static_cast<ParticleSystem *>(object_eval->particlesystem.first);
707 while (psys_eval != nullptr) {
708 if (psys_eval->orig_psys == psys) {
709 return psys_eval;
710 }
711 psys_eval = psys_eval->next;
712 }
713 return psys_eval;
714}
715
717{
718 if (psys->orig_psys == nullptr) {
719 return psys->edit;
720 }
721 return psys->orig_psys->edit;
722}
723
724bool psys_in_edit_mode(Depsgraph *depsgraph, const ParticleSystem *psys)
725{
726 const Scene *scene = DEG_get_input_scene(depsgraph);
727 ViewLayer *view_layer = DEG_get_input_view_layer(depsgraph);
728 BKE_view_layer_synced_ensure(scene, view_layer);
729 const Object *object = BKE_view_layer_active_object_get(view_layer);
730 if (object == nullptr) {
731 /* TODO(sergey): Needs double-check with multi-object edit. */
732 return false;
733 }
734 const bool use_render_params = (DEG_get_mode(depsgraph) == DAG_EVAL_RENDER);
735 if (object->mode != OB_MODE_PARTICLE_EDIT) {
736 return false;
737 }
738 const ParticleSystem *psys_orig = psys_orig_get((ParticleSystem *)psys);
739 return (psys_orig->edit || psys->pointcache->edit) && (use_render_params == false);
740}
741
742bool psys_check_enabled(Object *ob, ParticleSystem *psys, const bool use_render_params)
743{
745
746 if (psys->flag & PSYS_DISABLED || psys->flag & PSYS_DELETE || !psys->part) {
747 return false;
748 }
749
750 psmd = psys_get_modifier(ob, psys);
751
752 if (!psmd) {
753 return false;
754 }
755
756 if (use_render_params) {
757 if (!(psmd->modifier.mode & eModifierMode_Render)) {
758 return false;
759 }
760 }
761 else if (!(psmd->modifier.mode & eModifierMode_Realtime)) {
762 return false;
763 }
764
765 return true;
766}
767
769{
770 if (psys->part && psys->part->type == PART_HAIR) {
771 return (psys->flag & PSYS_EDITED || (psys->edit && psys->edit->edited));
772 }
773
774 return (psys->pointcache->edit && psys->pointcache->edit->edited);
775}
776
778{
779 /* Find object pointers based on index. If the collection is linked from
780 * another library linking may not have the object pointers available on
781 * file load, so we have to retrieve them later. See #49273. */
782 ListBase instance_collection_objects = {nullptr, nullptr};
783
784 if (part->instance_collection) {
785 instance_collection_objects = BKE_collection_object_cache_get(part->instance_collection);
786 }
787
789 if (dw->ob == nullptr) {
790 Base *base = static_cast<Base *>(BLI_findlink(&instance_collection_objects, dw->index));
791 if (base != nullptr) {
792 dw->ob = base->object;
793 }
794 }
795 }
796}
797
799{
800 ParticleDupliWeight *dw, *tdw;
801
802 if (part->ren_as != PART_DRAW_GR || !part->instance_collection) {
804 return;
805 }
806
807 /* Find object pointers. */
809
810 /* Remove nullptr objects, that were removed from the collection. */
811 dw = static_cast<ParticleDupliWeight *>(part->instance_weights.first);
812 while (dw) {
813 if (dw->ob == nullptr ||
815 {
816 tdw = dw->next;
818 dw = tdw;
819 }
820 else {
821 dw = dw->next;
822 }
823 }
824
825 /* Add new objects in the collection. */
826 int index = 0;
828 dw = static_cast<ParticleDupliWeight *>(part->instance_weights.first);
829 while (dw && dw->ob != object) {
830 dw = dw->next;
831 }
832
833 if (!dw) {
834 dw = MEM_callocN<ParticleDupliWeight>("ParticleDupliWeight");
835 dw->ob = object;
836 dw->count = 1;
837 BLI_addtail(&part->instance_weights, dw);
838 }
839
840 dw->index = index++;
841 }
843
844 /* Ensure there is an element marked as current. */
845 int current = 0;
847 if (dw->flag & PART_DUPLIW_CURRENT) {
848 current = 1;
849 break;
850 }
851 }
852
853 if (!current) {
854 dw = static_cast<ParticleDupliWeight *>(part->instance_weights.first);
855 if (dw) {
857 }
858 }
859}
860
866
867/************************************************/
868/* Freeing stuff */
869/************************************************/
870
872{
873 if (fluid) {
874 MEM_freeN(fluid);
875 }
876}
877
878void free_hair(Object *object, ParticleSystem *psys, int dynamics)
879{
881
883 {
884 MEM_SAFE_FREE(pa->hair);
885 pa->totkey = 0;
886 }
887
888 psys->flag &= ~PSYS_HAIR_DONE;
889
890 if (psys->clmd) {
891 if (dynamics) {
893 psys->clmd = nullptr;
894 PTCacheID pid;
895 BKE_ptcache_id_from_particles(&pid, object, psys);
897 }
898 else {
900 }
901 }
902
903 if (psys->hair_in_mesh) {
904 BKE_id_free(nullptr, psys->hair_in_mesh);
905 }
906 psys->hair_in_mesh = nullptr;
907
908 if (psys->hair_out_mesh) {
909 BKE_id_free(nullptr, psys->hair_out_mesh);
910 }
911 psys->hair_out_mesh = nullptr;
912}
914{
916
917 if (psys->part->type == PART_HAIR) {
918 return;
919 }
920
921 if (psys->particles && psys->particles->keys) {
922 MEM_freeN(psys->particles->keys);
923
925 {
926 if (pa->keys) {
927 pa->keys = nullptr;
928 pa->totkey = 0;
929 }
930 }
931 }
932}
934{
936 psys->childcache = nullptr;
937 psys->totchildcache = 0;
938}
940{
941 if (edit) {
943 edit->pathcache = nullptr;
944 edit->totcached = 0;
945 }
946 if (psys) {
948 psys->pathcache = nullptr;
949 psys->totcached = 0;
950
952 }
953}
955{
956 if (psys->child) {
957 MEM_freeN(psys->child);
958 psys->child = nullptr;
959 psys->totchild = 0;
960 }
961
963}
965{
967
968 if (psys->particles) {
969 /* Even though psys->part should never be nullptr,
970 * this can happen as an exception during deletion.
971 * See ID_REMAP_SKIP/FORCE/FLAG_NEVER_NULL_USAGE in BKE_library_remap. */
972 if (psys->part && psys->part->type == PART_HAIR) {
974 {
975 if (pa->hair) {
976 MEM_freeN(pa->hair);
977 }
978 }
979 }
980
981 if (psys->particles->keys) {
982 MEM_freeN(psys->particles->keys);
983 }
984
985 if (psys->particles->boid) {
986 MEM_freeN(psys->particles->boid);
987 }
988
989 MEM_freeN(psys->particles);
990 psys->particles = nullptr;
991 psys->totpart = 0;
992 }
993}
995{
996 if (psys->pdd) {
997 MEM_SAFE_FREE(psys->pdd->cdata);
998
999 MEM_SAFE_FREE(psys->pdd->vdata);
1000
1001 MEM_SAFE_FREE(psys->pdd->ndata);
1002
1003 MEM_SAFE_FREE(psys->pdd->vedata);
1004
1005 psys->pdd->totpoint = 0;
1006 psys->pdd->totpart = 0;
1007 psys->pdd->partsize = 0;
1008 }
1009}
1011{
1012 if (psys) {
1013 int nr = 0;
1014
1015 psys_free_path_cache(psys, nullptr);
1016
1017 /* NOTE: We pass dynamics=0 to free_hair() to prevent it from doing an
1018 * unneeded clear of the cache. But for historical reason that code path
1019 * was only clearing cloth part of modifier data.
1020 *
1021 * Part of the story there is that particle evaluation is trying to not
1022 * re-allocate thew ModifierData itself, and limits all allocations to
1023 * the cloth part of it.
1024 *
1025 * Why evaluation is relying on hair_free() and in some specific code
1026 * paths there is beyond me.
1027 */
1028 free_hair(ob, psys, 0);
1029 if (psys->clmd != nullptr) {
1031 }
1032
1033 psys_free_particles(psys);
1034
1035 if (psys->edit && psys->free_edit) {
1036 psys->free_edit(psys->edit);
1037 }
1038
1039 if (psys->child) {
1040 MEM_freeN(psys->child);
1041 psys->child = nullptr;
1042 psys->totchild = 0;
1043 }
1044
1045 /* check if we are last non-visible particle system */
1047 if (tpsys->part) {
1048 if (ELEM(tpsys->part->ren_as, PART_DRAW_OB, PART_DRAW_GR)) {
1049 nr++;
1050 break;
1051 }
1052 }
1053 }
1054 /* clear do-not-draw-flag */
1055 if (!nr) {
1057 }
1058
1059 psys->part = nullptr;
1060
1061 if ((psys->flag & PSYS_SHARED_CACHES) == 0) {
1063 }
1064 psys->pointcache = nullptr;
1065
1066 BLI_freelistN(&psys->targets);
1067
1069 BLI_kdtree_3d_free(psys->tree);
1070
1071 if (psys->fluid_springs) {
1072 MEM_freeN(psys->fluid_springs);
1073 }
1074
1076
1077 if (psys->pdd) {
1078 psys_free_pdd(psys);
1079 MEM_freeN(psys->pdd);
1080 }
1081
1083
1084 MEM_freeN(psys);
1085 }
1086}
1087
1089{
1090 /* Free existing particles. */
1091 if (psys_dst->particles != psys_src->particles) {
1092 psys_free_particles(psys_dst);
1093 }
1094 if (psys_dst->child != psys_src->child) {
1095 psys_free_children(psys_dst);
1096 }
1097 /* Restore counters. */
1098 psys_dst->totpart = psys_src->totpart;
1099 psys_dst->totchild = psys_src->totchild;
1100 /* Copy particles and children. */
1101 psys_dst->particles = static_cast<ParticleData *>(MEM_dupallocN(psys_src->particles));
1102 psys_dst->child = static_cast<ChildParticle *>(MEM_dupallocN(psys_src->child));
1103
1104 /* Ideally this should only be performed if `(psys_dst->part->type == PART_HAIR)`.
1105 *
1106 * But #ParticleData (`psys_dst`) is some sub-data of the #Object ID, while #ParticleSettings
1107 * (`psys_dst->part`) is another ID. In case the particle settings is a linked ID that gets
1108 * missing, it will be replaced (in readfile code) by a place-holder, which defaults to a
1109 * `PART_EMITTER` type of particle settings.
1110 *
1111 * This leads to a situation where each particle of `psys_dst` still has a valid allocated `hair`
1112 * data, which should still be preserved in case the missing particle settings ID becomes valid
1113 * again.
1114 *
1115 * Furthermore, #free_hair() always frees `pa->hair` if it's not nullptr, regardless of the
1116 * particle type. So *not* copying here would cause a double free (or more), e.g. freeing the
1117 * copy-on-evaluation copy and the original data will crash Blender.
1118 * In any case, sharing pointers between `psys_src` and `psys_dst` should be forbidden.
1119 *
1120 * So while we could in theory 'sanitize' the situation by setting `pa->hair` to nullptr in the
1121 * new copy (in case of non-`PART_HAIR` type), it is probably safer for now to systematically
1122 * duplicate the `hair` data if available. */
1123 {
1124 ParticleData *pa;
1125 int p;
1126 for (p = 0, pa = psys_dst->particles; p < psys_dst->totpart; p++, pa++) {
1127 pa->hair = static_cast<HairKey *>(MEM_dupallocN(pa->hair));
1128 }
1129 }
1130 if (psys_dst->particles && (psys_dst->particles->keys || psys_dst->particles->boid)) {
1131 ParticleKey *key = psys_dst->particles->keys;
1132 BoidParticle *boid = psys_dst->particles->boid;
1133 ParticleData *pa;
1134 int p;
1135 if (key != nullptr) {
1136 key = static_cast<ParticleKey *>(MEM_dupallocN(key));
1137 }
1138 if (boid != nullptr) {
1139 boid = static_cast<BoidParticle *>(MEM_dupallocN(boid));
1140 }
1141 for (p = 0, pa = psys_dst->particles; p < psys_dst->totpart; p++, pa++) {
1142 if (boid != nullptr) {
1143 pa->boid = boid++;
1144 }
1145 if (key != nullptr) {
1146 pa->keys = key;
1147 key += pa->totkey;
1148 }
1149 }
1150 }
1151}
1152
1153/************************************************/
1154/* Interpolation */
1155/************************************************/
1156
1158 float v1, float v2, float v3, float v4, const float w[4], int four)
1159{
1160 float value;
1161
1162 value = w[0] * v1 + w[1] * v2 + w[2] * v3;
1163 if (four) {
1164 value += w[3] * v4;
1165 }
1166
1167 CLAMP(value, 0.0f, 1.0f);
1168
1169 return value;
1170}
1171
1173 short type, ParticleKey keys[4], float dt, ParticleKey *result, bool velocity)
1174{
1175 float t[4];
1176
1177 if (type < 0) {
1178 interp_cubic_v3(result->co, result->vel, keys[1].co, keys[1].vel, keys[2].co, keys[2].vel, dt);
1179 }
1180 else {
1182
1183 interp_v3_v3v3v3v3(result->co, keys[0].co, keys[1].co, keys[2].co, keys[3].co, t);
1184
1185 if (velocity) {
1186 float temp[3];
1187
1188 if (dt > 0.999f) {
1189 key_curve_position_weights(dt - 0.001f, t, KeyInterpolationType(type));
1190 interp_v3_v3v3v3v3(temp, keys[0].co, keys[1].co, keys[2].co, keys[3].co, t);
1191 sub_v3_v3v3(result->vel, result->co, temp);
1192 }
1193 else {
1194 key_curve_position_weights(dt + 0.001f, t, KeyInterpolationType(type));
1195 interp_v3_v3v3v3v3(temp, keys[0].co, keys[1].co, keys[2].co, keys[3].co, t);
1196 sub_v3_v3v3(result->vel, temp, result->co);
1197 }
1198 }
1199 }
1200}
1201
1222
1229 PointCache *cache,
1230 PTCacheMem **cur,
1231 int index,
1232 float t,
1233 ParticleKey *key1,
1234 ParticleKey *key2)
1235{
1236 static PTCacheMem *pm = nullptr;
1237 int index1, index2;
1238
1239 if (index < 0) { /* initialize */
1240 *cur = static_cast<PTCacheMem *>(cache->mem_cache.first);
1241
1242 if (*cur) {
1243 *cur = (*cur)->next;
1244 }
1245 }
1246 else {
1247 if (*cur) {
1248 while (*cur && (*cur)->next && float((*cur)->frame) < t) {
1249 *cur = (*cur)->next;
1250 }
1251
1252 pm = *cur;
1253
1254 index2 = BKE_ptcache_mem_index_find(pm, index);
1255 index1 = BKE_ptcache_mem_index_find(pm->prev, index);
1256 if (index2 < 0) {
1257 return;
1258 }
1259
1260 BKE_ptcache_make_particle_key(key2, index2, pm->data, float(pm->frame));
1261 if (index1 < 0) {
1262 copy_particle_key(key1, key2, 1);
1263 }
1264 else {
1265 BKE_ptcache_make_particle_key(key1, index1, pm->prev->data, float(pm->prev->frame));
1266 }
1267 }
1268 else if (cache->mem_cache.first) {
1269 pm = static_cast<PTCacheMem *>(cache->mem_cache.first);
1270 index2 = BKE_ptcache_mem_index_find(pm, index);
1271 if (index2 < 0) {
1272 return;
1273 }
1274 BKE_ptcache_make_particle_key(key2, index2, pm->data, float(pm->frame));
1275 copy_particle_key(key1, key2, 1);
1276 }
1277 }
1278}
1280 int index,
1281 float *r_start,
1282 float *r_dietime)
1283{
1284 int ret = 0;
1285
1286 LISTBASE_FOREACH (PTCacheMem *, pm, &cache->mem_cache) {
1287 if (BKE_ptcache_mem_index_find(pm, index) >= 0) {
1288 *r_start = pm->frame;
1289 ret++;
1290 break;
1291 }
1292 }
1293
1295 if (BKE_ptcache_mem_index_find(pm, index) >= 0) {
1296 /* Die *after* the last available frame. */
1297 *r_dietime = pm->frame + 1;
1298 ret++;
1299 break;
1300 }
1301 }
1302
1303 return ret == 2;
1304}
1305
1307{
1308 int dietime = 10000000; /* some max value so that we can default to pa->time+lifetime */
1309
1311 if (BKE_ptcache_mem_index_find(pm, index) >= 0) {
1312 /* Die *after* the last available frame. */
1313 dietime = pm->frame + 1;
1314 break;
1315 }
1316 }
1317
1318 return float(dietime);
1319}
1320
1322 ParticleSystem *psys,
1323 ParticleData *pa,
1325{
1326
1327 if (pind->epoint) {
1328 PTCacheEditPoint *point = pind->epoint;
1329
1330 pind->ekey[0] = point->keys;
1331 pind->ekey[1] = point->totkey > 1 ? point->keys + 1 : nullptr;
1332
1333 pind->birthtime = *(point->keys->time);
1334 pind->dietime = *((point->keys + point->totkey - 1)->time);
1335 }
1336 else if (pind->keyed) {
1337 ParticleKey *key = pa->keys;
1338 pind->kkey[0] = key;
1339 pind->kkey[1] = pa->totkey > 1 ? key + 1 : nullptr;
1340
1341 pind->birthtime = key->time;
1342 pind->dietime = (key + pa->totkey - 1)->time;
1343 }
1344 else if (pind->cache) {
1345 float start = 0.0f, dietime = 0.0f;
1346 get_pointcache_keys_for_time(ob, pind->cache, &pind->pm, -1, 0.0f, nullptr, nullptr);
1347 pind->birthtime = pa ? pa->time : pind->cache->startframe;
1348 pind->dietime = pa ? pa->dietime : (pind->cache->endframe + 1);
1349
1350 if (get_pointcache_times_for_particle(pind->cache, pa - psys->particles, &start, &dietime)) {
1351 pind->birthtime = std::max(pind->birthtime, start);
1352 pind->dietime = std::min(pind->dietime, dietime);
1353 }
1354 }
1355 else {
1356 HairKey *key = pa->hair;
1357 pind->hkey[0] = key;
1358 pind->hkey[1] = key + 1;
1359
1360 pind->birthtime = key->time;
1361 pind->dietime = (key + pa->totkey - 1)->time;
1362
1363 if (pind->mesh) {
1364 float3 *positions = pind->mesh->vert_positions_for_write().data();
1365 pind->vert_positions[0] = &positions[pa->hair_index];
1366 pind->vert_positions[1] = pind->vert_positions[0] + 1;
1367 }
1368 }
1369}
1371{
1372 copy_v3_v3(key->co, ekey->co);
1373 if (ekey->vel) {
1374 copy_v3_v3(key->vel, ekey->vel);
1375 }
1376 key->time = *(ekey->time);
1377}
1378static void hair_to_particle(ParticleKey *key, HairKey *hkey)
1379{
1380 copy_v3_v3(key->co, hkey->co);
1381 key->time = hkey->time;
1382}
1383
1384static void mvert_to_particle(ParticleKey *key, float3 *position, HairKey *hkey)
1385{
1386 copy_v3_v3(key->co, *position);
1387 key->time = hkey->time;
1388}
1389
1391 int p,
1392 ParticleData *pa,
1393 float t,
1396{
1397 PTCacheEditPoint *point = pind->epoint;
1398 int point_vel = (point && point->keys->vel);
1399 float real_t, dfra, keytime, invdt = 1.0f;
1400
1401 /* billboards won't fill in all of these, so start cleared */
1402 ParticleKey keys[4] = {};
1403
1404 /* interpret timing and find keys */
1405 if (point) {
1406 if (result->time < 0.0f) {
1407 real_t = -result->time;
1408 }
1409 else {
1410 real_t = *(pind->ekey[0]->time) +
1411 t * (*(pind->ekey[0][point->totkey - 1].time) - *(pind->ekey[0]->time));
1412 }
1413
1414 while (*(pind->ekey[1]->time) < real_t) {
1415 pind->ekey[1]++;
1416 }
1417
1418 pind->ekey[0] = pind->ekey[1] - 1;
1419 }
1420 else if (pind->keyed) {
1421 /* we have only one key, so let's use that */
1422 if (pind->kkey[1] == nullptr) {
1423 copy_particle_key(result, pind->kkey[0], 1);
1424 return;
1425 }
1426
1427 if (result->time < 0.0f) {
1428 real_t = -result->time;
1429 }
1430 else {
1431 real_t = pind->kkey[0]->time +
1432 t * (pind->kkey[0][pa->totkey - 1].time - pind->kkey[0]->time);
1433 }
1434
1435 if (psys->part->phystype == PART_PHYS_KEYED && psys->flag & PSYS_KEYED_TIMING) {
1436 ParticleTarget *pt = static_cast<ParticleTarget *>(psys->targets.first);
1437
1438 pt = pt->next;
1439
1440 while (pt && pa->time + pt->time < real_t) {
1441 pt = pt->next;
1442 }
1443
1444 if (pt) {
1445 pt = pt->prev;
1446
1447 if (pa->time + pt->time + pt->duration > real_t) {
1448 real_t = pa->time + pt->time;
1449 }
1450 }
1451 else {
1452 real_t = pa->time + ((ParticleTarget *)psys->targets.last)->time;
1453 }
1454 }
1455
1456 CLAMP(real_t, pa->time, pa->dietime);
1457
1458 while (pind->kkey[1]->time < real_t) {
1459 pind->kkey[1]++;
1460 }
1461
1462 pind->kkey[0] = pind->kkey[1] - 1;
1463 }
1464 else if (pind->cache) {
1465 if (result->time < 0.0f) { /* flag for time in frames */
1466 real_t = -result->time;
1467 }
1468 else {
1469 real_t = pa->time + t * (pa->dietime - pa->time);
1470 }
1471 }
1472 else {
1473 if (result->time < 0.0f) {
1474 real_t = -result->time;
1475 }
1476 else {
1477 real_t = pind->hkey[0]->time +
1478 t * (pind->hkey[0][pa->totkey - 1].time - pind->hkey[0]->time);
1479 }
1480
1481 while (pind->hkey[1]->time < real_t) {
1482 pind->hkey[1]++;
1483 if (pind->mesh) {
1484 pind->vert_positions[1]++;
1485 }
1486 }
1487
1488 pind->hkey[0] = pind->hkey[1] - 1;
1489 }
1490
1491 /* set actual interpolation keys */
1492 if (point) {
1493 edit_to_particle(keys + 1, pind->ekey[0]);
1494 edit_to_particle(keys + 2, pind->ekey[1]);
1495 }
1496 else if (pind->mesh) {
1497 pind->vert_positions[0] = pind->vert_positions[1] - 1;
1498 mvert_to_particle(keys + 1, pind->vert_positions[0], pind->hkey[0]);
1499 mvert_to_particle(keys + 2, pind->vert_positions[1], pind->hkey[1]);
1500 }
1501 else if (pind->keyed) {
1502 memcpy(keys + 1, pind->kkey[0], sizeof(ParticleKey));
1503 memcpy(keys + 2, pind->kkey[1], sizeof(ParticleKey));
1504 }
1505 else if (pind->cache) {
1506 get_pointcache_keys_for_time(nullptr, pind->cache, &pind->pm, p, real_t, keys + 1, keys + 2);
1507 }
1508 else {
1509 hair_to_particle(keys + 1, pind->hkey[0]);
1510 hair_to_particle(keys + 2, pind->hkey[1]);
1511 }
1512
1513 /* set secondary interpolation keys for hair */
1514 if (!pind->keyed && !pind->cache && !point_vel) {
1515 if (point) {
1516 if (pind->ekey[0] != point->keys) {
1517 edit_to_particle(keys, pind->ekey[0] - 1);
1518 }
1519 else {
1520 edit_to_particle(keys, pind->ekey[0]);
1521 }
1522 }
1523 else if (pind->mesh) {
1524 if (pind->hkey[0] != pa->hair) {
1525 mvert_to_particle(keys, pind->vert_positions[0] - 1, pind->hkey[0] - 1);
1526 }
1527 else {
1528 mvert_to_particle(keys, pind->vert_positions[0], pind->hkey[0]);
1529 }
1530 }
1531 else {
1532 if (pind->hkey[0] != pa->hair) {
1533 hair_to_particle(keys, pind->hkey[0] - 1);
1534 }
1535 else {
1536 hair_to_particle(keys, pind->hkey[0]);
1537 }
1538 }
1539
1540 if (point) {
1541 if (pind->ekey[1] != point->keys + point->totkey - 1) {
1542 edit_to_particle(keys + 3, pind->ekey[1] + 1);
1543 }
1544 else {
1545 edit_to_particle(keys + 3, pind->ekey[1]);
1546 }
1547 }
1548 else if (pind->mesh) {
1549 if (pind->hkey[1] != pa->hair + pa->totkey - 1) {
1550 mvert_to_particle(keys + 3, pind->vert_positions[1] + 1, pind->hkey[1] + 1);
1551 }
1552 else {
1553 mvert_to_particle(keys + 3, pind->vert_positions[1], pind->hkey[1]);
1554 }
1555 }
1556 else {
1557 if (pind->hkey[1] != pa->hair + pa->totkey - 1) {
1558 hair_to_particle(keys + 3, pind->hkey[1] + 1);
1559 }
1560 else {
1561 hair_to_particle(keys + 3, pind->hkey[1]);
1562 }
1563 }
1564 }
1565
1566 dfra = keys[2].time - keys[1].time;
1567 keytime = (real_t - keys[1].time) / dfra;
1568
1569 /* Convert velocity to time-step size. */
1570 if (pind->keyed || pind->cache || point_vel) {
1571 invdt = dfra * 0.04f * (psys ? psys->part->timetweak : 1.0f);
1572 mul_v3_fl(keys[1].vel, invdt);
1573 mul_v3_fl(keys[2].vel, invdt);
1574 interp_qt_qtqt(result->rot, keys[1].rot, keys[2].rot, keytime);
1575 }
1576
1577 /* Now we should have in chronological order k1<=k2<=t<=k3<=k4 with key-time between
1578 * [0, 1]->[k2, k3] (k1 & k4 used for cardinal & b-spline interpolation). */
1579 psys_interpolate_particle((pind->keyed || pind->cache || point_vel) ?
1580 -1 /* signal for cubic interpolation */
1581 :
1582 (pind->bspline ? KEY_BSPLINE : KEY_CARDINAL),
1583 keys,
1584 keytime,
1585 result,
1586 true);
1587
1588 /* the velocity needs to be converted back from cubic interpolation */
1589 if (pind->keyed || pind->cache || point_vel) {
1590 mul_v3_fl(result->vel, 1.0f / invdt);
1591 }
1592}
1593
1595{
1596 int i = 0;
1597 ParticleCacheKey *cur = first;
1598
1599 /* scale the requested time to fit the entire path even if the path is cut early */
1600 t *= (first + first->segments)->time;
1601
1602 while (i < first->segments && cur->time < t) {
1603 cur++;
1604 }
1605
1606 if (cur->time == t) {
1607 *result = *cur;
1608 }
1609 else {
1610 float dt = (t - (cur - 1)->time) / (cur->time - (cur - 1)->time);
1611 interp_v3_v3v3(result->co, (cur - 1)->co, cur->co, dt);
1612 interp_v3_v3v3(result->vel, (cur - 1)->vel, cur->vel, dt);
1613 interp_qt_qtqt(result->rot, (cur - 1)->rot, cur->rot, dt);
1614 result->time = t;
1615 }
1616
1617 /* first is actual base rotation, others are incremental from first */
1618 if (cur == first || cur - 1 == first) {
1619 copy_qt_qt(result->rot, first->rot);
1620 }
1621 else {
1622 mul_qt_qtqt(result->rot, first->rot, result->rot);
1623 }
1624}
1625
1626/************************************************/
1627/* Particles on a dm */
1628/************************************************/
1629
1631 const float (*vert_positions)[3],
1632 const float (*vert_normals)[3],
1633 const MFace *mface,
1634 MTFace *tface,
1635 const float (*orcodata)[3],
1636 float w[4],
1637 float vec[3],
1638 float nor[3],
1639 float utan[3],
1640 float vtan[3],
1641 float orco[3])
1642{
1643 const float *v1 = nullptr, *v2 = nullptr, *v3 = nullptr, *v4 = nullptr;
1644 float e1[3], e2[3], s1, s2, t1, t2;
1645 float *uv1, *uv2, *uv3, *uv4;
1646 float n1[3], n2[3], n3[3], n4[3];
1647 float tuv[4][2];
1648 const float *o1, *o2, *o3, *o4;
1649
1650 v1 = vert_positions[mface->v1];
1651 v2 = vert_positions[mface->v2];
1652 v3 = vert_positions[mface->v3];
1653
1654 copy_v3_v3(n1, vert_normals[mface->v1]);
1655 copy_v3_v3(n2, vert_normals[mface->v2]);
1656 copy_v3_v3(n3, vert_normals[mface->v3]);
1657
1658 if (mface->v4) {
1659 v4 = vert_positions[mface->v4];
1660 copy_v3_v3(n4, vert_normals[mface->v4]);
1661
1662 interp_v3_v3v3v3v3(vec, v1, v2, v3, v4, w);
1663
1664 if (nor) {
1665 if (mface->flag & ME_SMOOTH) {
1666 interp_v3_v3v3v3v3(nor, n1, n2, n3, n4, w);
1667 }
1668 else {
1669 normal_quad_v3(nor, v1, v2, v3, v4);
1670 }
1671 }
1672 }
1673 else {
1674 interp_v3_v3v3v3(vec, v1, v2, v3, w);
1675
1676 if (nor) {
1677 if (mface->flag & ME_SMOOTH) {
1678 interp_v3_v3v3v3(nor, n1, n2, n3, w);
1679 }
1680 else {
1681 normal_tri_v3(nor, v1, v2, v3);
1682 }
1683 }
1684 }
1685
1686 /* calculate tangent vectors */
1687 if (utan && vtan) {
1688 if (tface) {
1689 uv1 = tface->uv[0];
1690 uv2 = tface->uv[1];
1691 uv3 = tface->uv[2];
1692 uv4 = tface->uv[3];
1693 }
1694 else {
1695 uv1 = tuv[0];
1696 uv2 = tuv[1];
1697 uv3 = tuv[2];
1698 uv4 = tuv[3];
1699 map_to_sphere(uv1, uv1 + 1, v1[0], v1[1], v1[2]);
1700 map_to_sphere(uv2, uv2 + 1, v2[0], v2[1], v2[2]);
1701 map_to_sphere(uv3, uv3 + 1, v3[0], v3[1], v3[2]);
1702 if (v4) {
1703 map_to_sphere(uv4, uv4 + 1, v4[0], v4[1], v4[2]);
1704 }
1705 }
1706
1707 if (v4) {
1708 s1 = uv3[0] - uv1[0];
1709 s2 = uv4[0] - uv1[0];
1710
1711 t1 = uv3[1] - uv1[1];
1712 t2 = uv4[1] - uv1[1];
1713
1714 sub_v3_v3v3(e1, v3, v1);
1715 sub_v3_v3v3(e2, v4, v1);
1716 }
1717 else {
1718 s1 = uv2[0] - uv1[0];
1719 s2 = uv3[0] - uv1[0];
1720
1721 t1 = uv2[1] - uv1[1];
1722 t2 = uv3[1] - uv1[1];
1723
1724 sub_v3_v3v3(e1, v2, v1);
1725 sub_v3_v3v3(e2, v3, v1);
1726 }
1727
1728 vtan[0] = (s1 * e2[0] - s2 * e1[0]);
1729 vtan[1] = (s1 * e2[1] - s2 * e1[1]);
1730 vtan[2] = (s1 * e2[2] - s2 * e1[2]);
1731
1732 utan[0] = (t1 * e2[0] - t2 * e1[0]);
1733 utan[1] = (t1 * e2[1] - t2 * e1[1]);
1734 utan[2] = (t1 * e2[2] - t2 * e1[2]);
1735 }
1736
1737 if (orco) {
1738 if (orcodata) {
1739 o1 = orcodata[mface->v1];
1740 o2 = orcodata[mface->v2];
1741 o3 = orcodata[mface->v3];
1742
1743 if (mface->v4) {
1744 o4 = orcodata[mface->v4];
1745
1746 interp_v3_v3v3v3v3(orco, o1, o2, o3, o4, w);
1747 }
1748 else {
1749 interp_v3_v3v3v3(orco, o1, o2, o3, w);
1750 }
1751 BKE_mesh_orco_verts_transform(mesh, (float(*)[3])orco, 1, true);
1752 }
1753 else {
1754 copy_v3_v3(orco, vec);
1755 }
1756 }
1757}
1758void psys_interpolate_uvs(const MTFace *tface, int quad, const float w[4], float uvco[2])
1759{
1760 float v10 = tface->uv[0][0];
1761 float v11 = tface->uv[0][1];
1762 float v20 = tface->uv[1][0];
1763 float v21 = tface->uv[1][1];
1764 float v30 = tface->uv[2][0];
1765 float v31 = tface->uv[2][1];
1766 float v40, v41;
1767
1768 if (quad) {
1769 v40 = tface->uv[3][0];
1770 v41 = tface->uv[3][1];
1771
1772 uvco[0] = w[0] * v10 + w[1] * v20 + w[2] * v30 + w[3] * v40;
1773 uvco[1] = w[0] * v11 + w[1] * v21 + w[2] * v31 + w[3] * v41;
1774 }
1775 else {
1776 uvco[0] = w[0] * v10 + w[1] * v20 + w[2] * v30;
1777 uvco[1] = w[0] * v11 + w[1] * v21 + w[2] * v31;
1778 }
1779}
1780
1781void psys_interpolate_mcol(const MCol *mcol, int quad, const float w[4], MCol *mc)
1782{
1783 const char *cp1, *cp2, *cp3, *cp4;
1784 char *cp;
1785
1786 cp = (char *)mc;
1787 cp1 = (const char *)&mcol[0];
1788 cp2 = (const char *)&mcol[1];
1789 cp3 = (const char *)&mcol[2];
1790
1791 if (quad) {
1792 cp4 = (char *)&mcol[3];
1793
1794 cp[0] = int(w[0] * cp1[0] + w[1] * cp2[0] + w[2] * cp3[0] + w[3] * cp4[0]);
1795 cp[1] = int(w[0] * cp1[1] + w[1] * cp2[1] + w[2] * cp3[1] + w[3] * cp4[1]);
1796 cp[2] = int(w[0] * cp1[2] + w[1] * cp2[2] + w[2] * cp3[2] + w[3] * cp4[2]);
1797 cp[3] = int(w[0] * cp1[3] + w[1] * cp2[3] + w[2] * cp3[3] + w[3] * cp4[3]);
1798 }
1799 else {
1800 cp[0] = int(w[0] * cp1[0] + w[1] * cp2[0] + w[2] * cp3[0]);
1801 cp[1] = int(w[0] * cp1[1] + w[1] * cp2[1] + w[2] * cp3[1]);
1802 cp[2] = int(w[0] * cp1[2] + w[1] * cp2[2] + w[2] * cp3[2]);
1803 cp[3] = int(w[0] * cp1[3] + w[1] * cp2[3] + w[2] * cp3[3]);
1804 }
1805}
1806
1808 Mesh *mesh, short from, int index, const float fw[4], const float *values)
1809{
1810 if (values == nullptr || index == -1) {
1811 return 0.0;
1812 }
1813
1814 switch (from) {
1815 case PART_FROM_VERT:
1816 return values[index];
1817 case PART_FROM_FACE:
1818 case PART_FROM_VOLUME: {
1819 MFace *mfaces = static_cast<MFace *>(
1821 MFace *mf = &mfaces[index];
1823 values[mf->v1], values[mf->v2], values[mf->v3], values[mf->v4], fw, mf->v4);
1824 }
1825 }
1826 return 0.0f;
1827}
1828
1829/* conversion of pa->fw to origspace layer coordinates */
1830static void psys_w_to_origspace(const float w[4], float uv[2])
1831{
1832 uv[0] = w[1] + w[2];
1833 uv[1] = w[2] + w[3];
1834}
1835
1836/* conversion of pa->fw to weights in face from origspace */
1837static void psys_origspace_to_w(OrigSpaceFace *osface, int quad, const float w[4], float neww[4])
1838{
1839 float v[4][3], co[3];
1840
1841 v[0][0] = osface->uv[0][0];
1842 v[0][1] = osface->uv[0][1];
1843 v[0][2] = 0.0f;
1844 v[1][0] = osface->uv[1][0];
1845 v[1][1] = osface->uv[1][1];
1846 v[1][2] = 0.0f;
1847 v[2][0] = osface->uv[2][0];
1848 v[2][1] = osface->uv[2][1];
1849 v[2][2] = 0.0f;
1850
1852 co[2] = 0.0f;
1853
1854 if (quad) {
1855 v[3][0] = osface->uv[3][0];
1856 v[3][1] = osface->uv[3][1];
1857 v[3][2] = 0.0f;
1858 interp_weights_poly_v3(neww, v, 4, co);
1859 }
1860 else {
1861 interp_weights_poly_v3(neww, v, 3, co);
1862 neww[3] = 0.0f;
1863 }
1864}
1865
1867 Mesh *mesh_original,
1868 int findex_orig,
1869 const float fw[4],
1870 LinkNode **poly_nodes)
1871{
1872 MFace *mtessface_final;
1873 const OrigSpaceFace *osface_final;
1874 int pindex_orig;
1875 float uv[2];
1876 const float(*faceuv)[2];
1877
1878 const int *index_mf_to_mpoly_deformed = nullptr;
1879 const int *index_mf_to_mpoly = nullptr;
1880 const int *index_mp_to_orig = nullptr;
1881
1882 const int totface_final = mesh_final->totface_legacy;
1883 const int totface_deformed = mesh_original ? mesh_original->totface_legacy : totface_final;
1884
1885 if (ELEM(0, totface_final, totface_deformed)) {
1886 return DMCACHE_NOTFOUND;
1887 }
1888
1889 index_mf_to_mpoly = static_cast<const int *>(
1891 index_mp_to_orig = static_cast<const int *>(
1893 BLI_assert(index_mf_to_mpoly);
1894
1895 if (mesh_original) {
1896 index_mf_to_mpoly_deformed = static_cast<const int *>(
1898 }
1899 else {
1900 BLI_assert(mesh_final->runtime->deformed_only);
1901 index_mf_to_mpoly_deformed = index_mf_to_mpoly;
1902 }
1903 BLI_assert(index_mf_to_mpoly_deformed);
1904
1905 pindex_orig = index_mf_to_mpoly_deformed[findex_orig];
1906
1907 if (mesh_original == nullptr) {
1908 mesh_original = mesh_final;
1909 }
1910
1911 index_mf_to_mpoly_deformed = nullptr;
1912
1913 mtessface_final = static_cast<MFace *>(CustomData_get_layer_for_write(
1914 &mesh_final->fdata_legacy, CD_MFACE, mesh_final->totface_legacy));
1915 osface_final = static_cast<OrigSpaceFace *>(CustomData_get_layer_for_write(
1916 &mesh_final->fdata_legacy, CD_ORIGSPACE, mesh_final->totface_legacy));
1917
1918 if (osface_final == nullptr) {
1919 /* Assume we don't need osface_final data, and we get a direct 1-1 mapping... */
1920 if (findex_orig < totface_final) {
1921 // printf("\tNO CD_ORIGSPACE, assuming not needed\n");
1922 return findex_orig;
1923 }
1924
1925 printf("\tNO CD_ORIGSPACE, error out of range\n");
1926 return DMCACHE_NOTFOUND;
1927 }
1928 if (findex_orig >= mesh_original->totface_legacy) {
1929 return DMCACHE_NOTFOUND; /* index not in the original mesh */
1930 }
1931
1932 psys_w_to_origspace(fw, uv);
1933
1934 if (poly_nodes) {
1935 /* we can have a restricted linked list of faces to check, faster! */
1936 LinkNode *tessface_node = poly_nodes[pindex_orig];
1937
1938 for (; tessface_node; tessface_node = tessface_node->next) {
1939 int findex_dst = POINTER_AS_INT(tessface_node->link);
1940 faceuv = osface_final[findex_dst].uv;
1941
1942 /* check that this intersects - Its possible this misses :/ -
1943 * could also check its not between */
1944 if (mtessface_final[findex_dst].v4) {
1945 if (isect_point_quad_v2(uv, faceuv[0], faceuv[1], faceuv[2], faceuv[3])) {
1946 return findex_dst;
1947 }
1948 }
1949 else if (isect_point_tri_v2(uv, faceuv[0], faceuv[1], faceuv[2])) {
1950 return findex_dst;
1951 }
1952 }
1953 }
1954 else { /* if we have no node, try every face */
1955 for (int findex_dst = 0; findex_dst < totface_final; findex_dst++) {
1956 /* If current tessface from 'final' DM and orig tessface (given by index)
1957 * map to the same orig poly. */
1958 if (BKE_mesh_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, findex_dst) ==
1959 pindex_orig)
1960 {
1961 faceuv = osface_final[findex_dst].uv;
1962
1963 /* check that this intersects - Its possible this misses :/ -
1964 * could also check its not between */
1965 if (mtessface_final[findex_dst].v4) {
1966 if (isect_point_quad_v2(uv, faceuv[0], faceuv[1], faceuv[2], faceuv[3])) {
1967 return findex_dst;
1968 }
1969 }
1970 else if (isect_point_tri_v2(uv, faceuv[0], faceuv[1], faceuv[2])) {
1971 return findex_dst;
1972 }
1973 }
1974 }
1975 }
1976
1977 return DMCACHE_NOTFOUND;
1978}
1979
1981 int from,
1982 int index,
1983 int index_dmcache,
1984 const float fw[4],
1985 float /*foffset*/,
1986 int *mapindex,
1987 float mapfw[4])
1988{
1989 if (index < 0) {
1990 return 0;
1991 }
1992
1993 if (mesh->runtime->deformed_only || index_dmcache == DMCACHE_ISCHILD) {
1994 /* for meshes that are either only deformed or for child particles, the
1995 * index and fw do not require any mapping, so we can directly use it */
1996 if (from == PART_FROM_VERT) {
1997 if (index >= mesh->verts_num) {
1998 return 0;
1999 }
2000
2001 *mapindex = index;
2002 }
2003 else { /* FROM_FACE/FROM_VOLUME */
2004 if (index >= mesh->totface_legacy) {
2005 return 0;
2006 }
2007
2008 *mapindex = index;
2009 copy_v4_v4(mapfw, fw);
2010 }
2011 }
2012 else {
2013 /* for other meshes that have been modified, we try to map the particle
2014 * to their new location, which means a different index, and for faces
2015 * also a new face interpolation weights */
2016 if (from == PART_FROM_VERT) {
2017 if (index_dmcache == DMCACHE_NOTFOUND || index_dmcache >= mesh->verts_num) {
2018 return 0;
2019 }
2020
2021 *mapindex = index_dmcache;
2022 }
2023 else { /* FROM_FACE/FROM_VOLUME */
2024 /* find a face on the derived mesh that uses this face */
2025 int i = index_dmcache;
2026
2027 if (i == DMCACHE_NOTFOUND || i >= mesh->totface_legacy) {
2028 return 0;
2029 }
2030
2031 *mapindex = i;
2032
2033 /* modify the original weights to become
2034 * weights for the derived mesh face */
2035 OrigSpaceFace *osface = static_cast<OrigSpaceFace *>(
2037 const MFace *mfaces = static_cast<const MFace *>(
2039 const MFace *mface = &mfaces[i];
2040
2041 if (osface == nullptr) {
2042 mapfw[0] = mapfw[1] = mapfw[2] = mapfw[3] = 0.0f;
2043 }
2044 else {
2045 psys_origspace_to_w(&osface[i], mface->v4, fw, mapfw);
2046 }
2047 }
2048 }
2049
2050 return 1;
2051}
2052
2053void psys_particle_on_dm(Mesh *mesh_final,
2054 int from,
2055 int index,
2056 int index_dmcache,
2057 const float fw[4],
2058 float foffset,
2059 float vec[3],
2060 float nor[3],
2061 float utan[3],
2062 float vtan[3],
2063 float orco[3])
2064{
2065 float tmpnor[3], mapfw[4];
2066 const float(*orcodata)[3];
2067 int mapindex;
2068
2069 if (!psys_map_index_on_dm(mesh_final, from, index, index_dmcache, fw, foffset, &mapindex, mapfw))
2070 {
2071 if (vec) {
2072 vec[0] = vec[1] = vec[2] = 0.0;
2073 }
2074 if (nor) {
2075 nor[0] = nor[1] = 0.0;
2076 nor[2] = 1.0;
2077 }
2078 if (orco) {
2079 orco[0] = orco[1] = orco[2] = 0.0;
2080 }
2081 if (utan) {
2082 utan[0] = utan[1] = utan[2] = 0.0;
2083 }
2084 if (vtan) {
2085 vtan[0] = vtan[1] = vtan[2] = 0.0;
2086 }
2087
2088 return;
2089 }
2090
2091 orcodata = static_cast<const float(*)[3]>(CustomData_get_layer(&mesh_final->vert_data, CD_ORCO));
2092 const blender::Span<blender::float3> vert_normals = mesh_final->vert_normals();
2093
2094 if (from == PART_FROM_VERT) {
2095 const blender::Span<blender::float3> vert_positions = mesh_final->vert_positions();
2096 copy_v3_v3(vec, vert_positions[mapindex]);
2097
2098 if (nor) {
2099 copy_v3_v3(nor, vert_normals[mapindex]);
2100 }
2101
2102 if (orco) {
2103 if (orcodata) {
2104 copy_v3_v3(orco, orcodata[mapindex]);
2105 BKE_mesh_orco_verts_transform(mesh_final, (float(*)[3])orco, 1, true);
2106 }
2107 else {
2108 copy_v3_v3(orco, vec);
2109 }
2110 }
2111
2112 if (utan && vtan) {
2113 utan[0] = utan[1] = utan[2] = 0.0f;
2114 vtan[0] = vtan[1] = vtan[2] = 0.0f;
2115 }
2116 }
2117 else { /* PART_FROM_FACE / PART_FROM_VOLUME */
2118 MFace *mface;
2119 MTFace *mtface;
2120
2121 MFace *mfaces = static_cast<MFace *>(CustomData_get_layer_for_write(
2122 &mesh_final->fdata_legacy, CD_MFACE, mesh_final->totface_legacy));
2123 mface = &mfaces[mapindex];
2124 const blender::Span<blender::float3> vert_positions = mesh_final->vert_positions();
2125 mtface = static_cast<MTFace *>(CustomData_get_layer_for_write(
2126 &mesh_final->fdata_legacy, CD_MTFACE, mesh_final->totface_legacy));
2127
2128 if (mtface) {
2129 mtface += mapindex;
2130 }
2131
2132 if (from == PART_FROM_VOLUME) {
2133 psys_interpolate_face(mesh_final,
2134 reinterpret_cast<const float(*)[3]>(vert_positions.data()),
2135 reinterpret_cast<const float(*)[3]>(vert_normals.data()),
2136 mface,
2137 mtface,
2138 orcodata,
2139 mapfw,
2140 vec,
2141 tmpnor,
2142 utan,
2143 vtan,
2144 orco);
2145 if (nor) {
2146 copy_v3_v3(nor, tmpnor);
2147 }
2148
2149 /* XXX Why not normalize tmpnor before copying it into nor??? -- mont29 */
2150 normalize_v3(tmpnor);
2151
2152 mul_v3_fl(tmpnor, -foffset);
2153 add_v3_v3(vec, tmpnor);
2154 }
2155 else {
2156 psys_interpolate_face(mesh_final,
2157 reinterpret_cast<const float(*)[3]>(vert_positions.data()),
2158 reinterpret_cast<const float(*)[3]>(vert_normals.data()),
2159 mface,
2160 mtface,
2161 orcodata,
2162 mapfw,
2163 vec,
2164 nor,
2165 utan,
2166 vtan,
2167 orco);
2168 }
2169 }
2170}
2171
2172float psys_particle_value_from_verts(Mesh *mesh, short from, ParticleData *pa, float *values)
2173{
2174 float mapfw[4];
2175 int mapindex;
2176
2178 mesh, from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, &mapindex, mapfw))
2179 {
2180 return 0.0f;
2181 }
2182
2183 return psys_interpolate_value_from_verts(mesh, from, mapindex, mapfw, values);
2184}
2185
2187{
2189
2191 if (md->type == eModifierType_ParticleSystem) {
2192 psmd = (ParticleSystemModifierData *)md;
2193 if (psmd->psys == psys) {
2194 return psmd;
2195 }
2196 }
2197 }
2198 return nullptr;
2199}
2200
2201/************************************************/
2202/* Particles on a shape */
2203/************************************************/
2204
2205/* ready for future use */
2206static void psys_particle_on_shape(int /*distr*/,
2207 int /*index*/,
2208 float * /*fuv*/,
2209 float vec[3],
2210 float nor[3],
2211 float utan[3],
2212 float vtan[3],
2213 float orco[3])
2214{
2215 /* TODO */
2216 const float zerovec[3] = {0.0f, 0.0f, 0.0f};
2217 if (vec) {
2218 copy_v3_v3(vec, zerovec);
2219 }
2220 if (nor) {
2221 copy_v3_v3(nor, zerovec);
2222 }
2223 if (utan) {
2224 copy_v3_v3(utan, zerovec);
2225 }
2226 if (vtan) {
2227 copy_v3_v3(vtan, zerovec);
2228 }
2229 if (orco) {
2230 copy_v3_v3(orco, zerovec);
2231 }
2232}
2233
2234/************************************************/
2235/* Particles on emitter */
2236/************************************************/
2237
2239{
2240 MTex *mtex;
2241 int i;
2242
2243 if (!psys->part) {
2244 return;
2245 }
2246
2247 for (i = 0; i < MAX_MTEX; i++) {
2248 mtex = psys->part->mtex[i];
2249 if (mtex && mtex->mapto && (mtex->texco & TEXCO_UV)) {
2250 r_cddata_masks->fmask |= CD_MASK_MTFACE;
2251 }
2252 }
2253
2254 if (psys->part->tanfac != 0.0f) {
2255 r_cddata_masks->fmask |= CD_MASK_MTFACE;
2256 }
2257
2258 /* Ask for vertex-groups if we need them. */
2259 for (i = 0; i < PSYS_TOT_VG; i++) {
2260 if (psys->vgroup[i]) {
2261 r_cddata_masks->vmask |= CD_MASK_MDEFORMVERT;
2262 break;
2263 }
2264 }
2265
2266 /* particles only need this if they are after a non deform modifier, and
2267 * the modifier stack will only create them in that case. */
2268 r_cddata_masks->lmask |= CD_MASK_ORIGSPACE_MLOOP;
2269 /* XXX Check we do need all those? */
2270 r_cddata_masks->vmask |= CD_MASK_ORIGINDEX;
2271 r_cddata_masks->emask |= CD_MASK_ORIGINDEX;
2272 r_cddata_masks->pmask |= CD_MASK_ORIGINDEX;
2273
2274 r_cddata_masks->vmask |= CD_MASK_ORCO;
2275}
2276
2278 int from,
2279 int index,
2280 int index_dmcache,
2281 float fuv[4],
2282 float foffset,
2283 float vec[3],
2284 float nor[3],
2285 float utan[3],
2286 float vtan[3],
2287 float orco[3])
2288{
2289 if (psmd && psmd->mesh_final) {
2290 if (psmd->psys->part->distr == PART_DISTR_GRID && psmd->psys->part->from != PART_FROM_VERT) {
2291 if (vec) {
2292 copy_v3_v3(vec, fuv);
2293 }
2294
2295 if (orco) {
2296 copy_v3_v3(orco, fuv);
2297 }
2298 return;
2299 }
2300 /* we can't use the num_dmcache */
2302 psmd->mesh_final, from, index, index_dmcache, fuv, foffset, vec, nor, utan, vtan, orco);
2303 }
2304 else {
2305 psys_particle_on_shape(from, index, fuv, vec, nor, utan, vtan, orco);
2306 }
2307}
2308
2309/************************************************/
2310/* Path Cache */
2311/************************************************/
2312
2314{
2315 EffectedPoint point;
2317 EffectorData efd;
2318 ParticleSystem *psys = sim->psys;
2319 EffectorWeights *weights = sim->psys->part->effector_weights;
2321 PARTICLE_P;
2322
2323 if (!effectors) {
2324 return;
2325 }
2326
2328 {
2330 sim->psys->part->from,
2331 pa->num,
2332 pa->num_dmcache,
2333 pa->fuv,
2334 pa->foffset,
2335 state.co,
2336 nullptr,
2337 nullptr,
2338 nullptr,
2339 nullptr);
2340
2341 mul_m4_v3(sim->ob->object_to_world().ptr(), state.co);
2342 mul_mat3_m4_v3(sim->ob->object_to_world().ptr(), state.vel);
2343
2344 pd_point_from_particle(sim, pa, &state, &point);
2345
2346 LISTBASE_FOREACH (EffectorCache *, eff, effectors) {
2347 if (eff->pd->forcefield != PFIELD_GUIDE) {
2348 continue;
2349 }
2350
2351 if (!eff->guide_data) {
2352 eff->guide_data = MEM_calloc_arrayN<GuideEffectorData>(psys->totpart, "GuideEffectorData");
2353 }
2354
2355 data = eff->guide_data + p;
2356
2357 sub_v3_v3v3(efd.vec_to_point, state.co, eff->guide_loc);
2358 copy_v3_v3(efd.nor, eff->guide_dir);
2359 efd.distance = len_v3(efd.vec_to_point);
2360
2361 copy_v3_v3(data->vec_to_point, efd.vec_to_point);
2362 data->strength = effector_falloff(eff, &efd, &point, weights);
2363 }
2364 }
2365}
2366
2367bool do_guides(Depsgraph *depsgraph,
2368 ParticleSettings *part,
2369 ListBase *effectors,
2371 int index,
2372 float time)
2373{
2374 CurveMapping *clumpcurve = (part->child_flag & PART_CHILD_USE_CLUMP_CURVE) ? part->clumpcurve :
2375 nullptr;
2376 CurveMapping *roughcurve = (part->child_flag & PART_CHILD_USE_ROUGH_CURVE) ? part->roughcurve :
2377 nullptr;
2378 PartDeflect *pd;
2379 Curve *cu;
2381
2382 float effect[3] = {0.0f, 0.0f, 0.0f}, veffect[3] = {0.0f, 0.0f, 0.0f};
2383 float guidevec[4], guidedir[3], rot2[4], temp[3];
2384 float guidetime, radius, weight, angle, totstrength = 0.0f;
2385 float vec_to_point[3];
2386
2387 if (effectors) {
2388 LISTBASE_FOREACH (EffectorCache *, eff, effectors) {
2389 pd = eff->pd;
2390
2391 if (pd->forcefield != PFIELD_GUIDE) {
2392 continue;
2393 }
2394
2395 data = eff->guide_data + index;
2396
2397 if (data->strength <= 0.0f) {
2398 continue;
2399 }
2400
2401 guidetime = time / (1.0f - pd->free_end);
2402
2403 if (guidetime > 1.0f) {
2404 continue;
2405 }
2406
2407 cu = (Curve *)eff->ob->data;
2408
2409 if (pd->flag & PFIELD_GUIDE_PATH_ADD) {
2410 if (BKE_where_on_path(eff->ob,
2411 data->strength * guidetime,
2412 guidevec,
2413 guidedir,
2414 nullptr,
2415 &radius,
2416 &weight) == 0)
2417 {
2418 return false;
2419 }
2420 }
2421 else {
2422 if (BKE_where_on_path(eff->ob, guidetime, guidevec, guidedir, nullptr, &radius, &weight) ==
2423 0)
2424 {
2425 return false;
2426 }
2427 }
2428
2429 mul_m4_v3(eff->ob->object_to_world().ptr(), guidevec);
2430 mul_mat3_m4_v3(eff->ob->object_to_world().ptr(), guidedir);
2431
2432 normalize_v3(guidedir);
2433
2434 copy_v3_v3(vec_to_point, data->vec_to_point);
2435
2436 if (guidetime != 0.0f) {
2437 /* curve direction */
2438 cross_v3_v3v3(temp, eff->guide_dir, guidedir);
2439 angle = dot_v3v3(eff->guide_dir, guidedir) / len_v3(eff->guide_dir);
2441 axis_angle_to_quat(rot2, temp, angle);
2442 mul_qt_v3(rot2, vec_to_point);
2443
2444 /* curve tilt */
2445 axis_angle_to_quat(rot2, guidedir, guidevec[3] - eff->guide_loc[3]);
2446 mul_qt_v3(rot2, vec_to_point);
2447 }
2448
2449 /* curve taper */
2450 if (cu->taperobj) {
2451 mul_v3_fl(vec_to_point,
2452 BKE_displist_calc_taper(depsgraph,
2453 eff->scene,
2454 cu->taperobj,
2455 int(data->strength * guidetime * 100.0f),
2456 100));
2457 }
2458 else { /* Curve size. */
2459 if (cu->flag & CU_PATH_RADIUS) {
2460 mul_v3_fl(vec_to_point, radius);
2461 }
2462 }
2463
2464 if (clumpcurve) {
2465 BKE_curvemapping_changed_all(clumpcurve);
2466 }
2467 if (roughcurve) {
2468 BKE_curvemapping_changed_all(roughcurve);
2469 }
2470
2471 {
2472 ParticleKey key;
2473 const float par_co[3] = {0.0f, 0.0f, 0.0f};
2474 const float par_vel[3] = {0.0f, 0.0f, 0.0f};
2475 const float par_rot[4] = {1.0f, 0.0f, 0.0f, 0.0f};
2476 const float orco_offset[3] = {0.0f, 0.0f, 0.0f};
2477
2478 copy_v3_v3(key.co, vec_to_point);
2479 do_kink(&key,
2480 par_co,
2481 par_vel,
2482 par_rot,
2483 guidetime,
2484 pd->kink_freq,
2485 pd->kink_shape,
2486 pd->kink_amp,
2487 0.0f,
2488 pd->kink,
2489 pd->kink_axis,
2490 nullptr,
2491 0);
2492 do_clump(&key,
2493 par_co,
2494 guidetime,
2495 orco_offset,
2496 pd->clump_fac,
2497 pd->clump_pow,
2498 1.0f,
2500 part->clump_noise_size,
2501 clumpcurve);
2502 copy_v3_v3(vec_to_point, key.co);
2503 }
2504
2505 add_v3_v3(vec_to_point, guidevec);
2506
2507 // sub_v3_v3v3(pa_loc, pa_loc, pa_zero);
2508 madd_v3_v3fl(effect, vec_to_point, data->strength);
2509 madd_v3_v3fl(veffect, guidedir, data->strength);
2510 totstrength += data->strength;
2511
2512 if (pd->flag & PFIELD_GUIDE_PATH_WEIGHT) {
2513 totstrength *= weight;
2514 }
2515 }
2516 }
2517
2518 if (totstrength != 0.0f) {
2519 if (totstrength > 1.0f) {
2520 mul_v3_fl(effect, 1.0f / totstrength);
2521 }
2522 CLAMP(totstrength, 0.0f, 1.0f);
2523 // add_v3_v3(effect, pa_zero);
2524 interp_v3_v3v3(state->co, state->co, effect, totstrength);
2525
2526 normalize_v3(veffect);
2527 mul_v3_fl(veffect, len_v3(state->vel));
2528 copy_v3_v3(state->vel, veffect);
2529 return true;
2530 }
2531 return false;
2532}
2533
2535 int i,
2536 ParticleCacheKey *ca,
2537 int k,
2538 int steps,
2539 float * /*rootco*/,
2540 float effector,
2541 float /*dfra*/,
2542 float /*cfra*/,
2543 float *length,
2544 float *vec)
2545{
2546 float force[3] = {0.0f, 0.0f, 0.0f};
2547 ParticleKey eff_key;
2548 EffectedPoint epoint;
2549
2550 /* Don't apply effectors for dynamic hair, otherwise the effectors don't get applied twice. */
2551 if (sim->psys->flag & PSYS_HAIR_DYNAMICS) {
2552 return;
2553 }
2554
2555 copy_v3_v3(eff_key.co, (ca - 1)->co);
2556 copy_v3_v3(eff_key.vel, (ca - 1)->vel);
2557 copy_qt_qt(eff_key.rot, (ca - 1)->rot);
2558
2559 pd_point_from_particle(sim, sim->psys->particles + i, &eff_key, &epoint);
2561 sim->colliders,
2562 sim->psys->part->effector_weights,
2563 &epoint,
2564 force,
2565 nullptr,
2566 nullptr);
2567
2568 mul_v3_fl(force,
2569 effector * powf(float(k) / float(steps), 100.0f * sim->psys->part->eff_hair) /
2570 float(steps));
2571
2572 add_v3_v3(force, vec);
2573
2574 normalize_v3(force);
2575
2576 if (k < steps) {
2577 sub_v3_v3v3(vec, (ca + 1)->co, ca->co);
2578 }
2579
2580 madd_v3_v3v3fl(ca->co, (ca - 1)->co, force, *length);
2581
2582 if (k < steps) {
2583 *length = len_v3(vec);
2584 }
2585}
2587 ParticleKey *par,
2588 float *par_rot,
2589 ParticleKey *child,
2590 float flat,
2591 float radius)
2592{
2593 copy_v3_v3(child->co, cpa->fuv);
2594 mul_v3_fl(child->co, radius);
2595
2596 child->co[0] *= flat;
2597
2598 copy_v3_v3(child->vel, par->vel);
2599
2600 if (par_rot) {
2601 mul_qt_v3(par_rot, child->co);
2602 copy_qt_qt(child->rot, par_rot);
2603 }
2604 else {
2605 unit_qt(child->rot);
2606 }
2607
2608 add_v3_v3(child->co, par->co);
2609}
2610float *psys_cache_vgroup(Mesh *mesh, ParticleSystem *psys, int vgroup)
2611{
2612 float *vg = nullptr;
2613
2614 if (vgroup < 0) {
2615 /* hair dynamics pinning vgroup */
2616 }
2617 else if (psys->vgroup[vgroup]) {
2618 const MDeformVert *dvert = mesh->deform_verts().data();
2619 if (dvert) {
2620 int totvert = mesh->verts_num, i;
2621 vg = MEM_calloc_arrayN<float>(totvert, "vg_cache");
2622 if (psys->vg_neg & (1 << vgroup)) {
2623 for (i = 0; i < totvert; i++) {
2624 vg[i] = 1.0f - BKE_defvert_find_weight(&dvert[i], psys->vgroup[vgroup] - 1);
2625 }
2626 }
2627 else {
2628 for (i = 0; i < totvert; i++) {
2629 vg[i] = BKE_defvert_find_weight(&dvert[i], psys->vgroup[vgroup] - 1);
2630 }
2631 }
2632 }
2633 }
2634 return vg;
2635}
2636void psys_find_parents(ParticleSimulationData *sim, const bool use_render_params)
2637{
2638 ParticleSystem *psys = sim->psys;
2639 ParticleSettings *part = sim->psys->part;
2640 KDTree_3d *tree;
2641 ChildParticle *cpa;
2642 ParticleTexture ptex;
2643 int p, totparent, totchild = sim->psys->totchild;
2644 float co[3], orco[3];
2645 int from = PART_FROM_FACE;
2646 totparent = int(totchild * part->parents * 0.3f);
2647
2648 if (use_render_params && part->child_percent && part->child_render_percent) {
2649 totparent *= float(part->child_percent) / float(part->child_render_percent);
2650 }
2651
2652 /* hard limit, workaround for it being ignored above */
2653 totparent = std::min(sim->psys->totpart, totparent);
2654
2655 tree = BLI_kdtree_3d_new(totparent);
2656
2657 for (p = 0, cpa = sim->psys->child; p < totparent; p++, cpa++) {
2659 from,
2660 cpa->num,
2662 cpa->fuv,
2663 cpa->foffset,
2664 co,
2665 nullptr,
2666 nullptr,
2667 nullptr,
2668 orco);
2669
2670 /* Check if particle doesn't exist because of texture influence.
2671 * Insert only existing particles into kdtree. */
2673 psys,
2674 part,
2675 psys->particles + cpa->pa[0],
2676 p,
2677 cpa->num,
2678 cpa->fuv,
2679 orco,
2680 &ptex,
2682 psys->cfra);
2683
2684 if (ptex.exist >= psys_frand(psys, p + 24)) {
2685 BLI_kdtree_3d_insert(tree, p, orco);
2686 }
2687 }
2688
2689 BLI_kdtree_3d_balance(tree);
2690
2691 for (; p < totchild; p++, cpa++) {
2693 from,
2694 cpa->num,
2696 cpa->fuv,
2697 cpa->foffset,
2698 co,
2699 nullptr,
2700 nullptr,
2701 nullptr,
2702 orco);
2703 cpa->parent = BLI_kdtree_3d_find_nearest(tree, orco, nullptr);
2704 }
2705
2706 BLI_kdtree_3d_free(tree);
2707}
2708
2711 Scene *scene,
2712 float cfra,
2713 const bool editupdate,
2714 const bool use_render_params)
2715{
2716 ParticleSystem *psys = sim->psys;
2717 ParticleSettings *part = psys->part;
2718 int totparent = 0, between = 0;
2719 int segments = 1 << part->draw_step;
2720 int totchild = psys->totchild;
2721
2722 psys_thread_context_init(ctx, sim);
2723
2724 /*---start figuring out what is actually wanted---*/
2725 if (psys_in_edit_mode(sim->depsgraph, psys)) {
2727
2728 if ((use_render_params == 0) &&
2729 (psys_orig_edit_get(psys) == nullptr || pset->flag & PE_DRAW_PART) == 0)
2730 {
2731 totchild = 0;
2732 }
2733
2734 segments = 1 << pset->draw_step;
2735 }
2736
2737 if (totchild && part->childtype == PART_CHILD_FACES) {
2738 totparent = int(totchild * part->parents * 0.3f);
2739
2740 if (use_render_params && part->child_percent && part->child_render_percent) {
2741 totparent *= float(part->child_percent) / float(part->child_render_percent);
2742 }
2743
2744 /* part->parents could still be 0 so we can't test with totparent */
2745 between = 1;
2746 }
2747
2748 if (use_render_params) {
2749 segments = 1 << part->ren_step;
2750 }
2751 else {
2752 totchild = int(float(totchild) * float(part->disp) / 100.0f);
2753 }
2754
2755 totparent = std::min(totparent, totchild);
2756
2757 if (totchild == 0) {
2758 return false;
2759 }
2760
2761 /* fill context values */
2762 ctx->between = between;
2763 ctx->segments = segments;
2764 if (ELEM(part->kink, PART_KINK_SPIRAL)) {
2765 ctx->extra_segments = max_ii(part->kink_extra_steps, 1);
2766 }
2767 else {
2768 ctx->extra_segments = 0;
2769 }
2770 ctx->totchild = totchild;
2771 ctx->totparent = totparent;
2772 ctx->parent_pass = 0;
2773 ctx->cfra = cfra;
2774 ctx->editupdate = editupdate;
2775
2776 psys_sim_data_init(&ctx->sim);
2777
2778 /* cache all relevant vertex groups if they exist */
2779 ctx->vg_length = psys_cache_vgroup(ctx->mesh, psys, PSYS_VG_LENGTH);
2780 ctx->vg_clump = psys_cache_vgroup(ctx->mesh, psys, PSYS_VG_CLUMP);
2781 ctx->vg_kink = psys_cache_vgroup(ctx->mesh, psys, PSYS_VG_KINK);
2782 ctx->vg_rough1 = psys_cache_vgroup(ctx->mesh, psys, PSYS_VG_ROUGH1);
2783 ctx->vg_rough2 = psys_cache_vgroup(ctx->mesh, psys, PSYS_VG_ROUGH2);
2784 ctx->vg_roughe = psys_cache_vgroup(ctx->mesh, psys, PSYS_VG_ROUGHE);
2785 ctx->vg_twist = psys_cache_vgroup(ctx->mesh, psys, PSYS_VG_TWIST);
2786 if (psys->part->flag & PART_CHILD_EFFECT) {
2788 }
2789
2790 /* prepare curvemapping tables */
2791 if ((part->child_flag & PART_CHILD_USE_CLUMP_CURVE) && part->clumpcurve) {
2794 }
2795 else {
2796 ctx->clumpcurve = nullptr;
2797 }
2798 if ((part->child_flag & PART_CHILD_USE_ROUGH_CURVE) && part->roughcurve) {
2801 }
2802 else {
2803 ctx->roughcurve = nullptr;
2804 }
2805 if ((part->child_flag & PART_CHILD_USE_TWIST_CURVE) && part->twistcurve) {
2808 }
2809 else {
2810 ctx->twistcurve = nullptr;
2811 }
2812
2813 return true;
2814}
2815
2817{
2818 /* init random number generator */
2819 int seed = 31415926 + sim->psys->seed;
2820
2821 task->rng_path = BLI_rng_new(seed);
2822}
2823
2824/* NOTE: this function must be thread safe, except for branching! */
2826 ChildParticle *cpa,
2827 ParticleCacheKey *child_keys,
2828 int i)
2829{
2830 ParticleThreadContext *ctx = task->ctx;
2831 Object *ob = ctx->sim.ob;
2832 ParticleSystem *psys = ctx->sim.psys;
2833 ParticleSettings *part = psys->part;
2834 ParticleCacheKey **cache = psys->childcache;
2835 PTCacheEdit *edit = psys_orig_edit_get(psys);
2836 ParticleCacheKey **pcache = psys_in_edit_mode(ctx->sim.depsgraph, psys) && edit ?
2837 edit->pathcache :
2838 psys->pathcache;
2839 ParticleCacheKey *child, *key[4];
2840 ParticleTexture ptex;
2841 float *cpa_fuv = nullptr, *par_rot = nullptr, rot[4];
2842 float orco[3], hairmat[4][4], dvec[3], off1[4][3], off2[4][3];
2843 float eff_length, eff_vec[3], weight[4];
2844 int k, cpa_num;
2845 short cpa_from;
2846
2847 if (!pcache) {
2848 return;
2849 }
2850
2851 if (ctx->between) {
2852 ParticleData *pa = psys->particles + cpa->pa[0];
2853 int w, needupdate;
2854 float foffset, wsum = 0.0f;
2855 float co[3];
2856 float p_min = part->parting_min;
2857 float p_max = part->parting_max;
2858 /* Virtual parents don't work nicely with parting. */
2859 float p_fac = part->parents > 0.0f ? 0.0f : part->parting_fac;
2860
2861 if (ctx->editupdate) {
2862 needupdate = 0;
2863 w = 0;
2864 while (w < 4 && cpa->pa[w] >= 0) {
2865 if (edit->points[cpa->pa[w]].flag & PEP_EDIT_RECALC) {
2866 needupdate = 1;
2867 break;
2868 }
2869 w++;
2870 }
2871
2872 if (!needupdate) {
2873 return;
2874 }
2875
2876 memset(child_keys, 0, sizeof(*child_keys) * (ctx->segments + 1));
2877 }
2878
2879 /* get parent paths */
2880 for (w = 0; w < 4; w++) {
2881 if (cpa->pa[w] >= 0) {
2882 key[w] = pcache[cpa->pa[w]];
2883 weight[w] = cpa->w[w];
2884 }
2885 else {
2886 key[w] = pcache[0];
2887 weight[w] = 0.0f;
2888 }
2889 }
2890
2891 /* modify weights to create parting */
2892 if (p_fac > 0.0f) {
2893 const ParticleCacheKey *key_0_last = pcache_key_segment_endpoint_safe(key[0]);
2894 for (w = 0; w < 4; w++) {
2895 if (w && (weight[w] > 0.0f)) {
2896 const ParticleCacheKey *key_w_last = pcache_key_segment_endpoint_safe(key[w]);
2897 float d;
2898 if (part->flag & PART_CHILD_LONG_HAIR) {
2899 /* For long hair use tip distance/root distance as parting
2900 * factor instead of root to tip angle. */
2901 float d1 = len_v3v3(key[0]->co, key[w]->co);
2902 float d2 = len_v3v3(key_0_last->co, key_w_last->co);
2903
2904 d = d1 > 0.0f ? d2 / d1 - 1.0f : 10000.0f;
2905 }
2906 else {
2907 float v1[3], v2[3];
2908 sub_v3_v3v3(v1, key_0_last->co, key[0]->co);
2909 sub_v3_v3v3(v2, key_w_last->co, key[w]->co);
2910 normalize_v3(v1);
2912
2913 d = RAD2DEGF(safe_acosf(dot_v3v3(v1, v2)));
2914 }
2915
2916 if (p_max > p_min) {
2917 d = (d - p_min) / (p_max - p_min);
2918 }
2919 else {
2920 d = (d - p_min) <= 0.0f ? 0.0f : 1.0f;
2921 }
2922
2923 CLAMP(d, 0.0f, 1.0f);
2924
2925 if (d > 0.0f) {
2926 weight[w] *= (1.0f - d);
2927 }
2928 }
2929 wsum += weight[w];
2930 }
2931 for (w = 0; w < 4; w++) {
2932 weight[w] /= wsum;
2933 }
2934
2935 interp_v4_v4v4(weight, cpa->w, weight, p_fac);
2936 }
2937
2938 /* get the original coordinates (orco) for texture usage */
2939 cpa_num = cpa->num;
2940
2941 foffset = cpa->foffset;
2942 cpa_fuv = cpa->fuv;
2943 cpa_from = PART_FROM_FACE;
2944
2946 cpa_from,
2947 cpa_num,
2949 cpa->fuv,
2950 foffset,
2951 co,
2952 nullptr,
2953 nullptr,
2954 nullptr,
2955 orco);
2956
2957 mul_m4_v3(ob->object_to_world().ptr(), co);
2958
2959 for (w = 0; w < 4; w++) {
2960 sub_v3_v3v3(off1[w], co, key[w]->co);
2961 }
2962
2963 psys_mat_hair_to_global(ob, ctx->sim.psmd->mesh_final, psys->part->from, pa, hairmat);
2964 }
2965 else {
2966 ParticleData *pa = psys->particles + cpa->parent;
2967 float co[3];
2968 if (ctx->editupdate) {
2969 if (!(edit->points[cpa->parent].flag & PEP_EDIT_RECALC)) {
2970 return;
2971 }
2972
2973 memset(child_keys, 0, sizeof(*child_keys) * (ctx->segments + 1));
2974 }
2975
2976 /* get the parent path */
2977 key[0] = pcache[cpa->parent];
2978
2979 /* get the original coordinates (orco) for texture usage */
2980 cpa_from = part->from;
2981
2982 /*
2983 * NOTE: Should in theory be the same as:
2984 * cpa_num = psys_particle_dm_face_lookup(
2985 * ctx->sim.psmd->dm_final,
2986 * ctx->sim.psmd->dm_deformed,
2987 * pa->num, pa->fuv,
2988 * nullptr);
2989 */
2990 cpa_num = ELEM(pa->num_dmcache, DMCACHE_ISCHILD, DMCACHE_NOTFOUND) ? pa->num : pa->num_dmcache;
2991
2992 /* XXX hack to avoid messed up particle num and subsequent crash (#40733) */
2993 if (cpa_num > ctx->sim.psmd->mesh_final->totface_legacy) {
2994 cpa_num = 0;
2995 }
2996 cpa_fuv = pa->fuv;
2997
2999 cpa_from,
3000 cpa_num,
3002 cpa_fuv,
3003 pa->foffset,
3004 co,
3005 nullptr,
3006 nullptr,
3007 nullptr,
3008 orco);
3009
3010 psys_mat_hair_to_global(ob, ctx->sim.psmd->mesh_final, psys->part->from, pa, hairmat);
3011 }
3012
3013 child_keys->segments = ctx->segments;
3014
3015 /* get different child parameters from textures & vgroups */
3016 get_child_modifier_parameters(part, ctx, cpa, cpa_from, cpa_num, cpa_fuv, orco, &ptex);
3017
3018 if (ptex.exist < psys_frand(psys, i + 24)) {
3019 child_keys->segments = -1;
3020 return;
3021 }
3022
3023 /* create the child path */
3024 for (k = 0, child = child_keys; k <= ctx->segments; k++, child++) {
3025 if (ctx->between) {
3026 int w = 0;
3027
3028 zero_v3(child->co);
3029 zero_v3(child->vel);
3030 unit_qt(child->rot);
3031
3032 for (w = 0; w < 4; w++) {
3033 copy_v3_v3(off2[w], off1[w]);
3034
3035 if (part->flag & PART_CHILD_LONG_HAIR) {
3036 /* Use parent rotation (in addition to emission location) to determine child offset. */
3037 if (k) {
3038 mul_qt_v3((key[w] + k)->rot, off2[w]);
3039 }
3040
3041 /* Fade the effect of rotation for even lengths in the end */
3042 project_v3_v3v3(dvec, off2[w], (key[w] + k)->vel);
3043 madd_v3_v3fl(off2[w], dvec, -float(k) / float(ctx->segments));
3044 }
3045
3046 add_v3_v3(off2[w], (key[w] + k)->co);
3047 }
3048
3049 /* child position is the weighted sum of parent positions */
3050 interp_v3_v3v3v3v3(child->co, off2[0], off2[1], off2[2], off2[3], weight);
3051 interp_v3_v3v3v3v3(child->vel,
3052 (key[0] + k)->vel,
3053 (key[1] + k)->vel,
3054 (key[2] + k)->vel,
3055 (key[3] + k)->vel,
3056 weight);
3057
3058 copy_qt_qt(child->rot, (key[0] + k)->rot);
3059 }
3060 else {
3061 if (k) {
3062 mul_qt_qtqt(rot, (key[0] + k)->rot, key[0]->rot);
3063 par_rot = rot;
3064 }
3065 else {
3066 par_rot = key[0]->rot;
3067 }
3068 /* offset the child from the parent position */
3069 offset_child(cpa,
3070 (ParticleKey *)(key[0] + k),
3071 par_rot,
3072 (ParticleKey *)child,
3073 part->childflat,
3074 part->childrad);
3075 }
3076
3077 child->time = float(k) / float(ctx->segments);
3078 }
3079
3080 /* apply effectors */
3081 if (part->flag & PART_CHILD_EFFECT) {
3082 for (k = 0, child = child_keys; k <= ctx->segments; k++, child++) {
3083 if (k) {
3084 do_path_effectors(&ctx->sim,
3085 cpa->pa[0],
3086 child,
3087 k,
3088 ctx->segments,
3089 child_keys->co,
3090 ptex.effector,
3091 0.0f,
3092 ctx->cfra,
3093 &eff_length,
3094 eff_vec);
3095 }
3096 else {
3097 sub_v3_v3v3(eff_vec, (child + 1)->co, child->co);
3098 eff_length = len_v3(eff_vec);
3099 }
3100 }
3101 }
3102
3103 {
3104 ParticleData *pa = nullptr;
3105 ParticleCacheKey *par = nullptr;
3106 float par_co[3];
3107 float par_orco[3];
3108
3109 if (ctx->totparent) {
3110 if (i >= ctx->totparent) {
3111 pa = &psys->particles[cpa->parent];
3112 /* this is now threadsafe, virtual parents are calculated before rest of children */
3113 BLI_assert(cpa->parent < psys->totchildcache);
3114 par = cache[cpa->parent];
3115 }
3116 }
3117 else if (cpa->parent >= 0) {
3118 pa = &psys->particles[cpa->parent];
3119 par = pcache[cpa->parent];
3120
3121 /* If particle is non-existing, try to pick a viable parent from particles
3122 * used for interpolation. */
3123 for (k = 0; k < 4 && pa && (pa->flag & PARS_UNEXIST); k++) {
3124 if (cpa->pa[k] >= 0) {
3125 pa = &psys->particles[cpa->pa[k]];
3126 par = pcache[cpa->pa[k]];
3127 }
3128 }
3129
3130 if (pa->flag & PARS_UNEXIST) {
3131 pa = nullptr;
3132 }
3133 }
3134
3135 if (pa) {
3136 ListBase modifiers;
3137 BLI_listbase_clear(&modifiers);
3138
3140 part->from,
3141 pa->num,
3142 pa->num_dmcache,
3143 pa->fuv,
3144 pa->foffset,
3145 par_co,
3146 nullptr,
3147 nullptr,
3148 nullptr,
3149 par_orco);
3150
3152 ctx, &modifiers, cpa, &ptex, orco, hairmat, child_keys, par, par_orco);
3153 }
3154 else {
3155 zero_v3(par_orco);
3156 }
3157 }
3158
3159 /* Hide virtual parents */
3160 if (i < ctx->totparent) {
3161 child_keys->segments = -1;
3162 }
3163}
3164
3165static void exec_child_path_cache(TaskPool *__restrict /*pool*/, void *taskdata)
3166{
3167 ParticleTask *task = static_cast<ParticleTask *>(taskdata);
3168 ParticleThreadContext *ctx = task->ctx;
3169 ParticleSystem *psys = ctx->sim.psys;
3170 ParticleCacheKey **cache = psys->childcache;
3171 ChildParticle *cpa;
3172 int i;
3173
3174 cpa = psys->child + task->begin;
3175 for (i = task->begin; i < task->end; i++, cpa++) {
3176 BLI_assert(i < psys->totchildcache);
3177 psys_thread_create_path(task, cpa, cache[i], i);
3178 }
3179}
3180
3182 float cfra,
3183 const bool editupdate,
3184 const bool use_render_params)
3185{
3186 if (sim->psys->flag & PSYS_GLOBAL_HAIR) {
3187 return;
3188 }
3189
3190 /* create a task pool for child path tasks */
3192 if (!psys_thread_context_init_path(&ctx, sim, sim->scene, cfra, editupdate, use_render_params)) {
3193 return;
3194 }
3195
3197 const int totchild = ctx.totchild;
3198 const int totparent = ctx.totparent;
3199
3200 if (editupdate && sim->psys->childcache && totchild == sim->psys->totchildcache) {
3201 /* just overwrite the existing cache */
3202 }
3203 else {
3204 /* clear out old and create new empty path cache */
3206
3208 &sim->psys->childcachebufs, totchild, ctx.segments + ctx.extra_segments + 1);
3209 sim->psys->totchildcache = totchild;
3210 }
3211
3212 /* cache parent paths */
3213 ctx.parent_pass = 1;
3214 blender::Vector<ParticleTask> tasks_parent = psys_tasks_create(&ctx, 0, totparent);
3215 for (ParticleTask &task : tasks_parent) {
3216 psys_task_init_path(&task, sim);
3217 BLI_task_pool_push(task_pool, exec_child_path_cache, &task, false, nullptr);
3218 }
3219 BLI_task_pool_work_and_wait(task_pool);
3220
3221 /* cache child paths */
3222 ctx.parent_pass = 0;
3223 blender::Vector<ParticleTask> tasks_child = psys_tasks_create(&ctx, totparent, totchild);
3224 for (ParticleTask &task : tasks_child) {
3225 psys_task_init_path(&task, sim);
3226 BLI_task_pool_push(task_pool, exec_child_path_cache, &task, false, nullptr);
3227 }
3228 BLI_task_pool_work_and_wait(task_pool);
3229
3230 BLI_task_pool_free(task_pool);
3231
3232 psys_tasks_free(tasks_parent);
3233 psys_tasks_free(tasks_child);
3234
3236}
3237
3238/* figure out incremental rotations along path starting from unit quat */
3240 ParticleCacheKey *key1,
3241 ParticleCacheKey *key2,
3242 float *prev_tangent,
3243 int i)
3244{
3245 float cosangle, angle, tangent[3], normal[3], q[4];
3246
3247 switch (i) {
3248 case 0:
3249 /* start from second key */
3250 break;
3251 case 1:
3252 /* calculate initial tangent for incremental rotations */
3253 sub_v3_v3v3(prev_tangent, key0->co, key1->co);
3254 normalize_v3(prev_tangent);
3255 unit_qt(key1->rot);
3256 break;
3257 default:
3258 sub_v3_v3v3(tangent, key0->co, key1->co);
3259 normalize_v3(tangent);
3260
3261 cosangle = dot_v3v3(tangent, prev_tangent);
3262
3263 /* note we do the comparison on cosangle instead of
3264 * angle, since floating point accuracy makes it give
3265 * different results across platforms */
3266 if (cosangle > 0.999999f) {
3267 copy_v4_v4(key1->rot, key2->rot);
3268 }
3269 else {
3270 angle = safe_acosf(cosangle);
3271 cross_v3_v3v3(normal, prev_tangent, tangent);
3272 axis_angle_to_quat(q, normal, angle);
3273 mul_qt_qtqt(key1->rot, q, key2->rot);
3274 }
3275
3276 copy_v3_v3(prev_tangent, tangent);
3277 }
3278}
3279
3280void psys_cache_paths(ParticleSimulationData *sim, float cfra, const bool use_render_params)
3281{
3284 ParticleSystem *psys = sim->psys;
3285 ParticleSettings *part = psys->part;
3286 ParticleCacheKey *ca, **cache;
3287
3288 Mesh *hair_mesh = (psys->part->type == PART_HAIR && psys->flag & PSYS_HAIR_DYNAMICS) ?
3289 psys->hair_out_mesh :
3290 nullptr;
3291
3293
3294 Material *ma;
3296 ParticleTexture ptex;
3297
3298 PARTICLE_P;
3299
3300 float birthtime = 0.0, dietime = 0.0;
3301 float t, time = 0.0, dfra = 1.0;
3302 float col[4] = {0.5f, 0.5f, 0.5f, 1.0f};
3303 float prev_tangent[3] = {0.0f, 0.0f, 0.0f}, hairmat[4][4];
3304 float rotmat[3][3];
3305 int k;
3306 int segments = int(pow(2.0, double((use_render_params) ? part->ren_step : part->draw_step)));
3307 int totpart = psys->totpart;
3308 float length, vec[3];
3309 float *vg_effector = nullptr;
3310 float *vg_length = nullptr, pa_length = 1.0f;
3311 int keyed, baked;
3312
3313 /* we don't have anything valid to create paths from so let's quit here */
3314 if ((psys->flag & PSYS_HAIR_DONE || psys->flag & PSYS_KEYED || psys->pointcache) == 0) {
3315 return;
3316 }
3317
3318 if (psys_in_edit_mode(sim->depsgraph, psys)) {
3319 if ((psys->edit == nullptr || pset->flag & PE_DRAW_PART) == 0) {
3320 return;
3321 }
3322 }
3323
3324 keyed = psys->flag & PSYS_KEYED;
3325 baked = psys->pointcache->mem_cache.first && psys->part->type != PART_HAIR;
3326
3327 /* clear out old and create new empty path cache */
3328 psys_free_path_cache(psys, psys->edit);
3330 &psys->pathcachebufs, totpart, segments + 1);
3331
3332 psys_sim_data_init(sim);
3333 ma = BKE_object_material_get(sim->ob, psys->part->omat);
3334 if (ma && (psys->part->draw_col == PART_DRAW_COL_MAT)) {
3335 copy_v3_v3(col, &ma->r);
3336 }
3337
3338 if ((psys->flag & PSYS_GLOBAL_HAIR) == 0) {
3339 if ((psys->part->flag & PART_CHILD_EFFECT) == 0) {
3340 vg_effector = psys_cache_vgroup(psmd->mesh_final, psys, PSYS_VG_EFFECTOR);
3341 }
3342
3343 if (!psys->totchild) {
3344 vg_length = psys_cache_vgroup(psmd->mesh_final, psys, PSYS_VG_LENGTH);
3345 }
3346 }
3347
3348 /* ensure we have tessfaces to be used for mapping */
3349 if (part->from != PART_FROM_VERT) {
3350 BKE_mesh_tessface_ensure(psmd->mesh_final);
3351 }
3352
3353 /*---first main loop: create all actual particles' paths---*/
3355 {
3356 if (!psys->totchild) {
3357 psys_get_texture(sim, pa, &ptex, PAMAP_LENGTH, 0.0f);
3358 pa_length = ptex.length * (1.0f - part->randlength * psys_frand(psys, psys->seed + p));
3359 if (vg_length) {
3360 pa_length *= psys_particle_value_from_verts(psmd->mesh_final, part->from, pa, vg_length);
3361 }
3362 }
3363
3364 pind.keyed = keyed;
3365 pind.cache = baked ? psys->pointcache : nullptr;
3366 pind.epoint = nullptr;
3367 pind.bspline = (psys->part->flag & PART_HAIR_BSPLINE);
3368 pind.mesh = hair_mesh;
3369
3370 memset(cache[p], 0, sizeof(*cache[p]) * (segments + 1));
3371
3372 cache[p]->segments = segments;
3373
3374 /*--get the first data points--*/
3375 init_particle_interpolation(sim->ob, sim->psys, pa, &pind);
3376
3377 /* 'hairmat' is needed for non-hair particle too so we get proper rotations. */
3378 psys_mat_hair_to_global(sim->ob, psmd->mesh_final, psys->part->from, pa, hairmat);
3379 copy_v3_v3(rotmat[0], hairmat[2]);
3380 copy_v3_v3(rotmat[1], hairmat[1]);
3381 copy_v3_v3(rotmat[2], hairmat[0]);
3382
3383 if (part->draw & PART_ABS_PATH_TIME) {
3384 birthtime = std::max(pind.birthtime, part->path_start);
3385 dietime = std::min(pind.dietime, part->path_end);
3386 }
3387 else {
3388 float tb = pind.birthtime;
3389 birthtime = tb + part->path_start * (pind.dietime - tb);
3390 dietime = tb + part->path_end * (pind.dietime - tb);
3391 }
3392
3393 if (birthtime >= dietime) {
3394 cache[p]->segments = -1;
3395 continue;
3396 }
3397
3398 dietime = birthtime + pa_length * (dietime - birthtime);
3399
3400 /*--interpolate actual path from data points--*/
3401 for (k = 0, ca = cache[p]; k <= segments; k++, ca++) {
3402 time = float(k) / float(segments);
3403 t = birthtime + time * (dietime - birthtime);
3404 result.time = -t;
3405 do_particle_interpolation(psys, p, pa, t, &pind, &result);
3406 copy_v3_v3(ca->co, result.co);
3407
3408 /* dynamic hair is in object space */
3409 /* keyed and baked are already in global space */
3410 if (hair_mesh) {
3411 mul_m4_v3(sim->ob->object_to_world().ptr(), ca->co);
3412 }
3413 else if (!keyed && !baked && !(psys->flag & PSYS_GLOBAL_HAIR)) {
3414 mul_m4_v3(hairmat, ca->co);
3415 }
3416
3417 copy_v3_v3(ca->col, col);
3418 }
3419
3420 if (part->type == PART_HAIR) {
3421 HairKey *hkey;
3422
3423 for (k = 0, hkey = pa->hair; k < pa->totkey; k++, hkey++) {
3424 mul_v3_m4v3(hkey->world_co, hairmat, hkey->co);
3425 }
3426 }
3427
3428 /*--modify paths and calculate rotation & velocity--*/
3429
3430 if (!(psys->flag & PSYS_GLOBAL_HAIR)) {
3431 /* apply effectors */
3432 if ((psys->part->flag & PART_CHILD_EFFECT) == 0) {
3433 float effector = 1.0f;
3434 if (vg_effector) {
3436 psmd->mesh_final, psys->part->from, pa, vg_effector);
3437 }
3438
3439 sub_v3_v3v3(vec, (cache[p] + 1)->co, cache[p]->co);
3440 length = len_v3(vec);
3441
3442 for (k = 1, ca = cache[p] + 1; k <= segments; k++, ca++) {
3444 sim, p, ca, k, segments, cache[p]->co, effector, dfra, cfra, &length, vec);
3445 }
3446 }
3447
3448 /* apply guide curves to path data */
3449 if (sim->psys->effectors && (psys->part->flag & PART_CHILD_EFFECT) == 0) {
3450 for (k = 0, ca = cache[p]; k <= segments; k++, ca++) {
3451 /* ca is safe to cast, since only co and vel are used */
3452 do_guides(sim->depsgraph,
3453 sim->psys->part,
3454 sim->psys->effectors,
3455 (ParticleKey *)ca,
3456 p,
3457 float(k) / float(segments));
3458 }
3459 }
3460
3461 /* Lattices have to be calculated separately to avoid mix-ups between effector calculations.
3462 */
3463 if (psys->lattice_deform_data) {
3464 for (k = 0, ca = cache[p]; k <= segments; k++, ca++) {
3466 psys->lattice_deform_data, ca->co, psys->lattice_strength);
3467 }
3468 }
3469 }
3470
3471 /* finally do rotation & velocity */
3472 for (k = 1, ca = cache[p] + 1; k <= segments; k++, ca++) {
3473 cache_key_incremental_rotation(ca, ca - 1, ca - 2, prev_tangent, k);
3474
3475 if (k == segments) {
3476 copy_qt_qt(ca->rot, (ca - 1)->rot);
3477 }
3478
3479 /* set velocity */
3480 sub_v3_v3v3(ca->vel, ca->co, (ca - 1)->co);
3481
3482 if (k == 1) {
3483 copy_v3_v3((ca - 1)->vel, ca->vel);
3484 }
3485
3486 ca->time = float(k) / float(segments);
3487 }
3488 /* First rotation is based on emitting face orientation.
3489 * This is way better than having flipping rotations resulting
3490 * from using a global axis as a rotation pole (vec_to_quat()).
3491 * It's not an ideal solution though since it disregards the
3492 * initial tangent, but taking that in to account will allow
3493 * the possibility of flipping again. -jahka
3494 */
3495 mat3_to_quat_legacy(cache[p]->rot, rotmat);
3496 }
3497
3498 psys->totcached = totpart;
3499
3500 psys_sim_data_free(sim);
3501
3502 if (vg_effector) {
3503 MEM_freeN(vg_effector);
3504 }
3505
3506 if (vg_length) {
3507 MEM_freeN(vg_length);
3508 }
3509}
3510
3519
3520static void psys_cache_edit_paths_iter(void *__restrict iter_data_v,
3521 const int iter,
3522 const TaskParallelTLS *__restrict /*tls*/)
3523{
3524 CacheEditrPathsIterData *iter_data = (CacheEditrPathsIterData *)iter_data_v;
3525 PTCacheEdit *edit = iter_data->edit;
3526 PTCacheEditPoint *point = &edit->points[iter];
3527 if (edit->totcached && !(point->flag & PEP_EDIT_RECALC)) {
3528 return;
3529 }
3530 if (point->totkey == 0) {
3531 return;
3532 }
3533 Object *ob = iter_data->object;
3534 ParticleSystem *psys = edit->psys;
3535 ParticleCacheKey **cache = edit->pathcache;
3536 ParticleSystemModifierData *psmd = iter_data->psmd;
3537 ParticleData *pa = iter_data->pa ? iter_data->pa + iter : nullptr;
3538 PTCacheEditKey *ekey = point->keys;
3539 const int segments = iter_data->segments;
3540 const bool use_weight = iter_data->use_weight;
3541
3542 float birthtime = 0.0f, dietime = 0.0f;
3543 float hairmat[4][4], rotmat[3][3], prev_tangent[3] = {0.0f, 0.0f, 0.0f};
3544
3546 pind.keyed = 0;
3547 pind.cache = nullptr;
3548 pind.epoint = point;
3549 pind.bspline = psys ? (psys->part->flag & PART_HAIR_BSPLINE) : 0;
3550 pind.mesh = nullptr;
3551
3552 /* should init_particle_interpolation set this ? */
3553 if (use_weight) {
3554 pind.hkey[0] = nullptr;
3555 /* pa != nullptr since the weight brush is only available for hair */
3556 pind.hkey[0] = pa->hair;
3557 pind.hkey[1] = pa->hair + 1;
3558 }
3559
3560 memset(cache[iter], 0, sizeof(*cache[iter]) * (segments + 1));
3561
3562 cache[iter]->segments = segments;
3563
3564 /*--get the first data points--*/
3565 init_particle_interpolation(ob, psys, pa, &pind);
3566
3567 if (psys) {
3568 psys_mat_hair_to_global(ob, psmd->mesh_final, psys->part->from, pa, hairmat);
3569 copy_v3_v3(rotmat[0], hairmat[2]);
3570 copy_v3_v3(rotmat[1], hairmat[1]);
3571 copy_v3_v3(rotmat[2], hairmat[0]);
3572 }
3573
3574 birthtime = pind.birthtime;
3575 dietime = pind.dietime;
3576
3577 if (birthtime >= dietime) {
3578 cache[iter]->segments = -1;
3579 return;
3580 }
3581
3582 /*--interpolate actual path from data points--*/
3583 ParticleCacheKey *ca;
3584 int k;
3585 float t, time = 0.0f, keytime = 0.0f;
3586 for (k = 0, ca = cache[iter]; k <= segments; k++, ca++) {
3587 time = float(k) / float(segments);
3588 t = birthtime + time * (dietime - birthtime);
3590 result.time = -t;
3591 do_particle_interpolation(psys, iter, pa, t, &pind, &result);
3592 copy_v3_v3(ca->co, result.co);
3593
3594 /* non-hair points are already in global space */
3595 if (psys && !(psys->flag & PSYS_GLOBAL_HAIR)) {
3596 mul_m4_v3(hairmat, ca->co);
3597
3598 if (k) {
3599 cache_key_incremental_rotation(ca, ca - 1, ca - 2, prev_tangent, k);
3600
3601 if (k == segments) {
3602 copy_qt_qt(ca->rot, (ca - 1)->rot);
3603 }
3604
3605 /* set velocity */
3606 sub_v3_v3v3(ca->vel, ca->co, (ca - 1)->co);
3607
3608 if (k == 1) {
3609 copy_v3_v3((ca - 1)->vel, ca->vel);
3610 }
3611 }
3612 }
3613 else {
3614 ca->vel[0] = ca->vel[1] = 0.0f;
3615 ca->vel[2] = 1.0f;
3616 }
3617
3618 /* selection coloring in edit mode */
3619 if (use_weight) {
3620 if (k == 0) {
3622 }
3623 else {
3624 /* WARNING: copied from 'do_particle_interpolation' (without 'vertex' array stepping) */
3625 float real_t;
3626 if (result.time < 0.0f) {
3627 real_t = -result.time;
3628 }
3629 else {
3630 real_t = pind.hkey[0]->time +
3631 t * (pind.hkey[0][pa->totkey - 1].time - pind.hkey[0]->time);
3632 }
3633
3634 while (pind.hkey[1]->time < real_t) {
3635 pind.hkey[1]++;
3636 }
3637 pind.hkey[0] = pind.hkey[1] - 1;
3638 /* end copy */
3639
3640 float w1[3], w2[3];
3641 keytime = (t - (*pind.ekey[0]->time)) / ((*pind.ekey[1]->time) - (*pind.ekey[0]->time));
3642
3645
3646 interp_v3_v3v3(ca->col, w1, w2, keytime);
3647 }
3648 }
3649 else {
3650 /* HACK(fclem): Instead of setting the color we pass the select state in the red channel.
3651 * This is then picked up in DRW and the gpu shader will do the color interpolation. */
3652 if ((ekey + (pind.ekey[0] - point->keys))->flag & PEK_SELECT) {
3653 if ((ekey + (pind.ekey[1] - point->keys))->flag & PEK_SELECT) {
3654 ca->col[0] = 1.0f;
3655 }
3656 else {
3657 keytime = (t - (*pind.ekey[0]->time)) / ((*pind.ekey[1]->time) - (*pind.ekey[0]->time));
3658 ca->col[0] = 1.0f - keytime;
3659 }
3660 }
3661 else {
3662 if ((ekey + (pind.ekey[1] - point->keys))->flag & PEK_SELECT) {
3663 keytime = (t - (*pind.ekey[0]->time)) / ((*pind.ekey[1]->time) - (*pind.ekey[0]->time));
3664 ca->col[0] = keytime;
3665 }
3666 else {
3667 ca->col[0] = 0.0f;
3668 }
3669 }
3670 }
3671
3672 ca->time = t;
3673 }
3674 if (psys && !(psys->flag & PSYS_GLOBAL_HAIR)) {
3675 /* First rotation is based on emitting face orientation.
3676 * This is way better than having flipping rotations resulting
3677 * from using a global axis as a rotation pole (vec_to_quat()).
3678 * It's not an ideal solution though since it disregards the
3679 * initial tangent, but taking that in to account will allow
3680 * the possibility of flipping again. -jahka
3681 */
3682 mat3_to_quat_legacy(cache[iter]->rot, rotmat);
3683 }
3684}
3685
3686void psys_cache_edit_paths(Depsgraph *depsgraph,
3687 Scene *scene,
3688 Object *ob,
3689 PTCacheEdit *edit,
3690 float cfra,
3691 const bool use_render_params)
3692{
3693 ParticleCacheKey **cache = edit->pathcache;
3695
3696 ParticleSystem *psys = edit->psys;
3697
3698 ParticleData *pa = psys ? psys->particles : nullptr;
3699
3700 int segments = 1 << pset->draw_step;
3701 int totpart = edit->totpoint, recalc_set = 0;
3702
3703 if (edit->psmd_eval == nullptr) {
3704 return;
3705 }
3706
3707 segments = std::max(segments, 4);
3708
3709 if (!cache || edit->totpoint != edit->totcached) {
3710 /* Clear out old and create new empty path cache. */
3711 psys_free_path_cache(edit->psys, edit);
3713 &edit->pathcachebufs, totpart, segments + 1);
3714 /* Set flag for update (child particles check this too). */
3715 int i;
3716 PTCacheEditPoint *point;
3717 for (i = 0, point = edit->points; i < totpart; i++, point++) {
3718 point->flag |= PEP_EDIT_RECALC;
3719 }
3720 recalc_set = 1;
3721 }
3722
3723 const bool use_weight = (pset->brushtype == PE_BRUSH_WEIGHT) && (psys != nullptr) &&
3724 (psys->particles != nullptr);
3725
3726 CacheEditrPathsIterData iter_data;
3727 iter_data.object = ob;
3728 iter_data.edit = edit;
3729 iter_data.psmd = edit->psmd_eval;
3730 iter_data.pa = pa;
3731 iter_data.segments = segments;
3732 iter_data.use_weight = use_weight;
3733
3734 TaskParallelSettings settings;
3736 BLI_task_parallel_range(0, edit->totpoint, &iter_data, psys_cache_edit_paths_iter, &settings);
3737
3738 edit->totcached = totpart;
3739
3740 if (psys) {
3741 ParticleSimulationData sim = {nullptr};
3742 sim.depsgraph = depsgraph;
3743 sim.scene = scene;
3744 sim.ob = ob;
3745 sim.psys = psys;
3746 sim.psmd = edit->psmd_eval;
3747
3748 psys_cache_child_paths(&sim, cfra, true, use_render_params);
3749 }
3750
3751 /* clear recalc flag if set here */
3752 if (recalc_set) {
3753 PTCacheEditPoint *point;
3754 int i;
3755 for (i = 0, point = edit->points; i < totpart; i++, point++) {
3756 point->flag &= ~PEP_EDIT_RECALC;
3757 }
3758 }
3759}
3760
3761/************************************************/
3762/* Particle Key handling */
3763/************************************************/
3764
3766{
3767 if (time) {
3768 memcpy(to, from, sizeof(ParticleKey));
3769 }
3770 else {
3771 float to_time = to->time;
3772 memcpy(to, from, sizeof(ParticleKey));
3773 to->time = to_time;
3774 }
3775}
3776void psys_get_from_key(ParticleKey *key, float loc[3], float vel[3], float rot[4], float *time)
3777{
3778 if (loc) {
3779 copy_v3_v3(loc, key->co);
3780 }
3781 if (vel) {
3782 copy_v3_v3(vel, key->vel);
3783 }
3784 if (rot) {
3785 copy_qt_qt(rot, key->rot);
3786 }
3787 if (time) {
3788 *time = key->time;
3789 }
3790}
3791
3792static void triatomat(float *v1, float *v2, float *v3, const float (*uv)[2], float mat[4][4])
3793{
3794 float det, w1, w2, d1[2], d2[2];
3795
3796 memset(mat, 0, sizeof(float[4][4]));
3797 mat[3][3] = 1.0f;
3798
3799 /* first axis is the normal */
3800 normal_tri_v3(mat[2], v1, v2, v3);
3801
3802 /* second axis along (1, 0) in uv space */
3803 if (uv) {
3804 d1[0] = uv[1][0] - uv[0][0];
3805 d1[1] = uv[1][1] - uv[0][1];
3806 d2[0] = uv[2][0] - uv[0][0];
3807 d2[1] = uv[2][1] - uv[0][1];
3808
3809 det = d2[0] * d1[1] - d2[1] * d1[0];
3810
3811 if (det != 0.0f) {
3812 det = 1.0f / det;
3813 w1 = -d2[1] * det;
3814 w2 = d1[1] * det;
3815
3816 mat[1][0] = w1 * (v2[0] - v1[0]) + w2 * (v3[0] - v1[0]);
3817 mat[1][1] = w1 * (v2[1] - v1[1]) + w2 * (v3[1] - v1[1]);
3818 mat[1][2] = w1 * (v2[2] - v1[2]) + w2 * (v3[2] - v1[2]);
3819 normalize_v3(mat[1]);
3820 }
3821 else {
3822 mat[1][0] = mat[1][1] = mat[1][2] = 0.0f;
3823 }
3824 }
3825 else {
3826 sub_v3_v3v3(mat[1], v2, v1);
3827 normalize_v3(mat[1]);
3828 }
3829
3830 /* third as a cross product */
3831 cross_v3_v3v3(mat[0], mat[1], mat[2]);
3832}
3833
3834static void psys_face_mat(Object *ob, Mesh *mesh, ParticleData *pa, float mat[4][4], int orco)
3835{
3836 float v[3][3];
3837 MFace *mface;
3838 const float(*orcodata)[3];
3839
3841 if (i == -1 || i >= mesh->totface_legacy) {
3842 unit_m4(mat);
3843 return;
3844 }
3845
3846 MFace *mfaces = static_cast<MFace *>(
3848 mface = &mfaces[i];
3849 const OrigSpaceFace *osface = static_cast<const OrigSpaceFace *>(
3851
3852 if (orco &&
3853 (orcodata = static_cast<const float(*)[3]>(CustomData_get_layer(&mesh->vert_data, CD_ORCO))))
3854 {
3855 copy_v3_v3(v[0], orcodata[mface->v1]);
3856 copy_v3_v3(v[1], orcodata[mface->v2]);
3857 copy_v3_v3(v[2], orcodata[mface->v3]);
3858
3859 /* ugly hack to use non-transformed orcos, since only those
3860 * give symmetric results for mirroring in particle mode */
3862 BKE_mesh_orco_verts_transform(static_cast<Mesh *>(ob->data), v, 3, true);
3863 }
3864 }
3865 else {
3866 const blender::Span<blender::float3> vert_positions = mesh->vert_positions();
3867 copy_v3_v3(v[0], vert_positions[mface->v1]);
3868 copy_v3_v3(v[1], vert_positions[mface->v2]);
3869 copy_v3_v3(v[2], vert_positions[mface->v3]);
3870 }
3871
3872 triatomat(v[0], v[1], v[2], (osface) ? osface->uv : nullptr, mat);
3873}
3874
3876 Object * /*ob*/, Mesh *mesh, short from, ParticleData *pa, float hairmat[4][4])
3877{
3878 float vec[3];
3879
3880 /* can happen when called from a different object's modifier */
3881 if (!mesh) {
3882 unit_m4(hairmat);
3883 return;
3884 }
3885
3886 psys_face_mat(nullptr, mesh, pa, hairmat, 0);
3888 from,
3889 pa->num,
3890 pa->num_dmcache,
3891 pa->fuv,
3892 pa->foffset,
3893 vec,
3894 nullptr,
3895 nullptr,
3896 nullptr,
3897 nullptr);
3898 copy_v3_v3(hairmat[3], vec);
3899}
3900
3902 Object *ob, Mesh *mesh, short from, ParticleData *pa, float hairmat[4][4])
3903{
3904 float vec[3], orco[3];
3905
3906 psys_face_mat(ob, mesh, pa, hairmat, 1);
3908 from,
3909 pa->num,
3910 pa->num_dmcache,
3911 pa->fuv,
3912 pa->foffset,
3913 vec,
3914 nullptr,
3915 nullptr,
3916 nullptr,
3917 orco);
3918
3919 copy_v3_v3(hairmat[3], orco);
3920}
3921
3922void psys_vec_rot_to_face(Mesh *mesh, ParticleData *pa, float vec[3])
3923{
3924 float mat[4][4];
3925
3926 psys_face_mat(nullptr, mesh, pa, mat, 0);
3927 transpose_m4(mat); /* cheap inverse for rotation matrix */
3928 mul_mat3_m4_v3(mat, vec);
3929}
3930
3932 Object *ob, Mesh *mesh, short from, ParticleData *pa, float hairmat[4][4])
3933{
3934 float facemat[4][4];
3935
3936 psys_mat_hair_to_object(ob, mesh, from, pa, facemat);
3937
3938 mul_m4_m4m4(hairmat, ob->object_to_world().ptr(), facemat);
3939}
3940
3941/************************************************/
3942/* ParticleSettings handling */
3943/************************************************/
3944
3946 Main *bmain, const Scene *scene, Object *ob, const char *name, const ParticleSystem *psys_orig)
3947{
3948 ParticleSystem *psys;
3949 ModifierData *md;
3951
3952 if (!ob || ob->type != OB_MESH) {
3953 return nullptr;
3954 }
3955
3956 if (name == nullptr) {
3957 name = (psys_orig != nullptr) ? psys_orig->name : DATA_("ParticleSystem");
3958 }
3959
3960 psys = static_cast<ParticleSystem *>(ob->particlesystem.first);
3961 for (; psys; psys = psys->next) {
3962 psys->flag &= ~PSYS_CURRENT;
3963 }
3964
3965 psys = MEM_callocN<ParticleSystem>("particle_system");
3966 psys->pointcache = BKE_ptcache_add(&psys->ptcaches);
3967 BLI_addtail(&ob->particlesystem, psys);
3968 psys_unique_name(ob, psys, name);
3969
3970 if (psys_orig != nullptr) {
3971 psys->part = psys_orig->part;
3972 id_us_plus(&psys->part->id);
3973 }
3974 else {
3975 psys->part = BKE_particlesettings_add(bmain, DATA_("ParticleSettings"));
3976 }
3978 STRNCPY(md->name, psys->name);
3980
3981 psmd = (ParticleSystemModifierData *)md;
3982 psmd->psys = psys;
3983 BLI_addtail(&ob->modifiers, md);
3986
3987 psys->totpart = 0;
3988 psys->flag = PSYS_CURRENT;
3989 if (scene != nullptr) {
3990 psys->cfra = BKE_scene_frame_to_ctime(scene, scene->r.cfra + 1);
3991 }
3992
3995
3996 return md;
3997}
3998
4000 const Scene *scene,
4001 Object *ob,
4002 const char *name)
4003{
4004 return object_add_or_copy_particle_system(bmain, scene, ob, name, nullptr);
4005}
4006
4008 const Scene *scene,
4009 Object *ob,
4010 const ParticleSystem *psys_orig)
4011{
4012 return object_add_or_copy_particle_system(bmain, scene, ob, nullptr, psys_orig);
4013}
4014
4016 Scene * /*scene*/,
4017 Object *ob,
4018 ParticleSystem *psys)
4019{
4020 if (!ob || !psys) {
4021 return;
4022 }
4023
4026
4027 /* Clear particle system in fluid modifier. */
4028 if (md) {
4030
4031 /* Clear particle system pointer in flow settings. */
4032 if ((fmd->type == MOD_FLUID_TYPE_FLOW) && fmd->flow && fmd->flow->psys) {
4033 if (fmd->flow->psys == psys) {
4034 fmd->flow->psys = nullptr;
4035 }
4036 }
4037 /* Clear particle flag in domain settings when removing particle system manually. */
4038 if (fmd->type == MOD_FLUID_TYPE_DOMAIN) {
4039 if (psys->part->type == PART_FLUID_FLIP) {
4041 }
4042 if (ELEM(psys->part->type,
4047 {
4049 }
4050 if (ELEM(psys->part->type,
4055 {
4057 }
4058 if (ELEM(psys->part->type,
4063 {
4065 }
4066 if (psys->part->type == PART_FLUID_TRACER) {
4068 }
4069
4070 /* Disable combined export if combined particle system was deleted. */
4071 if (ELEM(psys->part->type,
4076 {
4078 }
4079 }
4080 }
4081
4084 if (pmd->brush && pmd->brush->psys) {
4085 if (pmd->brush->psys == psys) {
4086 pmd->brush->psys = nullptr;
4087 }
4088 }
4089 }
4090
4091 /* Clear modifier, skip empty ones. */
4092 psmd = psys_get_modifier(ob, psys);
4093 if (psmd) {
4096 }
4097
4098 /* Clear particle system. */
4099 BLI_remlink(&ob->particlesystem, psys);
4100 if (psys->part) {
4101 id_us_min(&psys->part->id);
4102 }
4103 psys_free(ob, psys);
4104
4105 if (ob->particlesystem.first) {
4107 }
4108 else {
4110 }
4111
4114
4115 /* Flush object mode. */
4117}
4118
4120{
4121 ParticleSettings *part;
4122
4123 part = BKE_id_new<ParticleSettings>(bmain, name);
4124
4125 return part;
4126}
4127
4129{
4130 CurveMapping *cumap = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
4131
4132 cumap->cm[0].curve[0].x = 0.0f;
4133 cumap->cm[0].curve[0].y = 1.0f;
4134 cumap->cm[0].curve[1].x = 1.0f;
4135 cumap->cm[0].curve[1].y = 1.0f;
4136
4137 BKE_curvemapping_init(cumap);
4138
4139 part->clumpcurve = cumap;
4140}
4141
4143{
4144 CurveMapping *cumap = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
4145
4146 cumap->cm[0].curve[0].x = 0.0f;
4147 cumap->cm[0].curve[0].y = 1.0f;
4148 cumap->cm[0].curve[1].x = 1.0f;
4149 cumap->cm[0].curve[1].y = 1.0f;
4150
4151 BKE_curvemapping_init(cumap);
4152
4153 part->roughcurve = cumap;
4154}
4155
4157{
4158 CurveMapping *cumap = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
4159
4160 cumap->cm[0].curve[0].x = 0.0f;
4161 cumap->cm[0].curve[0].y = 1.0f;
4162 cumap->cm[0].curve[1].x = 1.0f;
4163 cumap->cm[0].curve[1].y = 1.0f;
4164
4165 BKE_curvemapping_init(cumap);
4166
4167 part->twistcurve = cumap;
4168}
4169
4170/************************************************/
4171/* Textures */
4172/************************************************/
4173
4174static int get_particle_uv(Mesh *mesh,
4175 ParticleData *pa,
4176 int index,
4177 const float fuv[4],
4178 const char *name,
4179 float *texco,
4180 bool from_vert)
4181{
4183 &mesh->fdata_legacy, CD_MFACE, mesh->totface_legacy);
4184 MFace *mf;
4185 const MTFace *tf;
4186 int i;
4187
4188 tf = static_cast<const MTFace *>(
4190 if (tf == nullptr) {
4191 tf = static_cast<const MTFace *>(CustomData_get_layer(&mesh->fdata_legacy, CD_MTFACE));
4192 }
4193 if (tf == nullptr) {
4194 return 0;
4195 }
4196
4197 if (pa) {
4199 if ((!from_vert && i >= mesh->totface_legacy) || (from_vert && i >= mesh->verts_num)) {
4200 i = -1;
4201 }
4202 }
4203 else {
4204 i = index;
4205 }
4206
4207 if (i == -1) {
4208 texco[0] = 0.0f;
4209 texco[1] = 0.0f;
4210 texco[2] = 0.0f;
4211 }
4212 else {
4213 if (from_vert) {
4214 mf = mfaces;
4215
4216 /* This finds the first face to contain the emitting vertex,
4217 * this is not ideal, but is mostly fine as UV seams generally
4218 * map to equal-colored parts of a texture */
4219 for (int j = 0; j < mesh->totface_legacy; j++, mf++) {
4220 if (ELEM(i, mf->v1, mf->v2, mf->v3, mf->v4)) {
4221 i = j;
4222 break;
4223 }
4224 }
4225 }
4226 else {
4227 mf = &mfaces[i];
4228 }
4229
4230 psys_interpolate_uvs(&tf[i], mf->v4, fuv, texco);
4231
4232 texco[0] = texco[0] * 2.0f - 1.0f;
4233 texco[1] = texco[1] * 2.0f - 1.0f;
4234 texco[2] = 0.0f;
4235 }
4236
4237 return 1;
4238}
4239
4240#define SET_PARTICLE_TEXTURE(type, pvalue, texfac) \
4241 if ((event & mtex->mapto) & type) { \
4242 pvalue = texture_value_blend(def, pvalue, value, texfac, blend); \
4243 } \
4244 (void)0
4245
4246#define CLAMP_PARTICLE_TEXTURE_POS(type, pvalue) \
4247 if (event & type) { \
4248 CLAMP(pvalue, 0.0f, 1.0f); \
4249 } \
4250 (void)0
4251
4252#define CLAMP_WARP_PARTICLE_TEXTURE_POS(type, pvalue) \
4253 if (event & type) { \
4254 if (pvalue < 0.0f) { \
4255 pvalue = 1.0f + pvalue; \
4256 } \
4257 CLAMP(pvalue, 0.0f, 1.0f); \
4258 } \
4259 (void)0
4260
4261#define CLAMP_PARTICLE_TEXTURE_POSNEG(type, pvalue) \
4262 if (event & type) { \
4263 CLAMP(pvalue, -1.0f, 1.0f); \
4264 } \
4265 (void)0
4266
4267static void get_cpa_texture(Mesh *mesh,
4268 ParticleSystem *psys,
4269 ParticleSettings *part,
4270 ParticleData *par,
4271 int child_index,
4272 int face_index,
4273 const float fw[4],
4274 float *orco,
4275 ParticleTexture *ptex,
4276 int event,
4277 float cfra)
4278{
4279 MTex *mtex, **mtexp = part->mtex;
4280 int m;
4281 float value, rgba[4], texvec[3];
4282
4283 ptex->ivel = ptex->life = ptex->exist = ptex->size = ptex->damp = ptex->gravity = ptex->field =
4284 ptex->time = ptex->clump = ptex->kink_freq = ptex->kink_amp = ptex->effector = ptex->rough1 =
4285 ptex->rough2 = ptex->roughe = 1.0f;
4286 ptex->twist = 1.0f;
4287
4288 ptex->length = 1.0f - part->randlength * psys_frand(psys, child_index + 26);
4289 ptex->length *= part->clength_thres < psys_frand(psys, child_index + 27) ? part->clength : 1.0f;
4290
4291 for (m = 0; m < MAX_MTEX; m++, mtexp++) {
4292 mtex = *mtexp;
4293 if (mtex && mtex->tex && mtex->mapto) {
4294 float def = mtex->def_var;
4295 short blend = mtex->blendtype;
4296 short texco = mtex->texco;
4297
4298 if (ELEM(texco, TEXCO_UV, TEXCO_ORCO) &&
4299 (ELEM(part->from, PART_FROM_FACE, PART_FROM_VOLUME) == 0 ||
4300 part->distr == PART_DISTR_GRID))
4301 {
4302 texco = TEXCO_GLOB;
4303 }
4304
4305 switch (texco) {
4306 case TEXCO_GLOB:
4307 copy_v3_v3(texvec, par->state.co);
4308 break;
4309 case TEXCO_OBJECT:
4310 copy_v3_v3(texvec, par->state.co);
4311 if (mtex->object) {
4312 mul_m4_v3(mtex->object->world_to_object().ptr(), texvec);
4313 }
4314 break;
4315 case TEXCO_UV:
4316 if (fw && get_particle_uv(mesh,
4317 nullptr,
4318 face_index,
4319 fw,
4320 mtex->uvname,
4321 texvec,
4322 (part->from == PART_FROM_VERT)))
4323 {
4324 break;
4325 }
4326 /* no break, failed to get uv's, so let's try orco's */
4328 case TEXCO_ORCO:
4329 copy_v3_v3(texvec, orco);
4330 break;
4331 case TEXCO_PARTICLE:
4332 /* texture coordinates in range [-1, 1] */
4333 texvec[0] = 2.0f * (cfra - par->time) / (par->dietime - par->time) - 1.0f;
4334 texvec[1] = 0.0f;
4335 texvec[2] = 0.0f;
4336 break;
4337 }
4338
4339 RE_texture_evaluate(mtex, texvec, 0, nullptr, false, false, &value, rgba);
4340
4341 if ((event & mtex->mapto) & PAMAP_ROUGH) {
4342 ptex->rough1 = ptex->rough2 = ptex->roughe = texture_value_blend(
4343 def, ptex->rough1, value, mtex->roughfac, blend);
4344 }
4345
4352 }
4353 }
4354
4361}
4363 ParticleSimulationData *sim, ParticleData *pa, ParticleTexture *ptex, int event, float cfra)
4364{
4365 Object *ob = sim->ob;
4366 Mesh *mesh = (Mesh *)ob->data;
4367 ParticleSettings *part = sim->psys->part;
4368 MTex **mtexp = part->mtex;
4369 MTex *mtex;
4370 int m;
4371 float value, rgba[4], co[3], texvec[3];
4372 int setvars = 0;
4373
4374 /* initialize ptex */
4375 ptex->ivel = ptex->life = ptex->exist = ptex->size = ptex->damp = ptex->gravity = ptex->field =
4376 ptex->length = ptex->clump = ptex->kink_freq = ptex->kink_amp = ptex->effector =
4377 ptex->rough1 = ptex->rough2 = ptex->roughe = 1.0f;
4378 ptex->twist = 1.0f;
4379
4380 ptex->time = float(pa - sim->psys->particles) / float(sim->psys->totpart);
4381
4382 for (m = 0; m < MAX_MTEX; m++, mtexp++) {
4383 mtex = *mtexp;
4384 if (mtex && mtex->tex && mtex->mapto) {
4385 float def = mtex->def_var;
4386 short blend = mtex->blendtype;
4387 short texco = mtex->texco;
4388
4389 if (texco == TEXCO_UV && (ELEM(part->from, PART_FROM_FACE, PART_FROM_VOLUME) == 0 ||
4390 part->distr == PART_DISTR_GRID))
4391 {
4392 texco = TEXCO_GLOB;
4393 }
4394
4395 switch (texco) {
4396 case TEXCO_GLOB:
4397 copy_v3_v3(texvec, pa->state.co);
4398 break;
4399 case TEXCO_OBJECT:
4400 copy_v3_v3(texvec, pa->state.co);
4401 if (mtex->object) {
4402 mul_m4_v3(mtex->object->world_to_object().ptr(), texvec);
4403 }
4404 break;
4405 case TEXCO_UV:
4407 pa,
4408 0,
4409 pa->fuv,
4410 mtex->uvname,
4411 texvec,
4412 (part->from == PART_FROM_VERT)))
4413 {
4414 break;
4415 }
4416 /* no break, failed to get uv's, so let's try orco's */
4418 case TEXCO_ORCO:
4420 sim->psys->part->from,
4421 pa->num,
4422 pa->num_dmcache,
4423 pa->fuv,
4424 pa->foffset,
4425 co,
4426 nullptr,
4427 nullptr,
4428 nullptr,
4429 texvec);
4430
4432 sub_v3_v3(texvec, mesh->texspace_location);
4433 if (mesh->texspace_size[0] != 0.0f) {
4434 texvec[0] /= mesh->texspace_size[0];
4435 }
4436 if (mesh->texspace_size[1] != 0.0f) {
4437 texvec[1] /= mesh->texspace_size[1];
4438 }
4439 if (mesh->texspace_size[2] != 0.0f) {
4440 texvec[2] /= mesh->texspace_size[2];
4441 }
4442 break;
4443 case TEXCO_PARTICLE:
4444 /* texture coordinates in range [-1, 1] */
4445 texvec[0] = 2.0f * (cfra - pa->time) / (pa->dietime - pa->time) - 1.0f;
4446 if (sim->psys->totpart > 0) {
4447 texvec[1] = 2.0f * float(pa - sim->psys->particles) / float(sim->psys->totpart) - 1.0f;
4448 }
4449 else {
4450 texvec[1] = 0.0f;
4451 }
4452 texvec[2] = 0.0f;
4453 break;
4454 }
4455
4456 RE_texture_evaluate(mtex, texvec, 0, nullptr, false, false, &value, rgba);
4457
4458 if ((event & mtex->mapto) & PAMAP_TIME) {
4459 /* the first time has to set the base value for time regardless of blend mode */
4460 if ((setvars & PAMAP_TIME) == 0) {
4461 int flip = (mtex->timefac < 0.0f);
4462 float timefac = fabsf(mtex->timefac);
4463 ptex->time *= 1.0f - timefac;
4464 ptex->time += timefac * ((flip) ? 1.0f - value : value);
4465 setvars |= PAMAP_TIME;
4466 }
4467 else {
4468 ptex->time = texture_value_blend(def, ptex->time, value, mtex->timefac, blend);
4469 }
4470 }
4480 }
4481 }
4482
4492}
4493
4494/************************************************/
4495/* Particle State */
4496/************************************************/
4497
4499{
4500 return 0.04f * sim->psys->part->timetweak;
4501}
4503 ParticleSystem *psys, ChildParticle *cpa, float cfra, float *birthtime, float *dietime)
4504{
4505 ParticleSettings *part = psys->part;
4506 float time, life;
4507
4508 if (part->childtype == PART_CHILD_FACES) {
4509 int w = 0;
4510 time = 0.0;
4511 while (w < 4 && cpa->pa[w] >= 0) {
4512 time += cpa->w[w] * (psys->particles + cpa->pa[w])->time;
4513 w++;
4514 }
4515
4516 life = part->lifetime * (1.0f - part->randlife * psys_frand(psys, cpa - psys->child + 25));
4517 }
4518 else {
4519 ParticleData *pa = psys->particles + cpa->parent;
4520
4521 time = pa->time;
4522 life = pa->lifetime;
4523 }
4524
4525 if (birthtime) {
4526 *birthtime = time;
4527 }
4528 if (dietime) {
4529 *dietime = time + life;
4530 }
4531
4532 return (cfra - time) / life;
4533}
4535 ChildParticle *cpa,
4536 float /*cfra*/,
4537 float * /*pa_time*/)
4538{
4539 ParticleSettings *part = psys->part;
4540 float size; /* time XXX */
4541
4542 if (part->childtype == PART_CHILD_FACES) {
4543 int w = 0;
4544 size = 0.0;
4545 while (w < 4 && cpa->pa[w] >= 0) {
4546 size += cpa->w[w] * (psys->particles + cpa->pa[w])->size;
4547 w++;
4548 }
4549 }
4550 else {
4551 size = psys->particles[cpa->parent].size;
4552 }
4553
4554 size *= part->childsize;
4555
4556 if (part->childrandsize != 0.0f) {
4557 size *= 1.0f - part->childrandsize * psys_frand(psys, cpa - psys->child + 26);
4558 }
4559
4560 return size;
4561}
4564 ChildParticle *cpa,
4565 short cpa_from,
4566 int cpa_num,
4567 float *cpa_fuv,
4568 float *orco,
4569 ParticleTexture *ptex)
4570{
4571 ParticleSystem *psys = ctx->sim.psys;
4572 int i = cpa - psys->child;
4573
4574 get_cpa_texture(ctx->mesh,
4575 psys,
4576 part,
4577 psys->particles + cpa->pa[0],
4578 i,
4579 cpa_num,
4580 cpa_fuv,
4581 orco,
4582 ptex,
4584 psys->cfra);
4585
4586 if (ptex->exist < psys_frand(psys, i + 24)) {
4587 return;
4588 }
4589
4590 if (ctx->vg_length) {
4592 ctx->mesh, cpa_from, cpa_num, cpa_fuv, ctx->vg_length);
4593 }
4594 if (ctx->vg_clump) {
4596 ctx->mesh, cpa_from, cpa_num, cpa_fuv, ctx->vg_clump);
4597 }
4598 if (ctx->vg_kink) {
4600 ctx->mesh, cpa_from, cpa_num, cpa_fuv, ctx->vg_kink);
4601 }
4602 if (ctx->vg_rough1) {
4604 ctx->mesh, cpa_from, cpa_num, cpa_fuv, ctx->vg_rough1);
4605 }
4606 if (ctx->vg_rough2) {
4608 ctx->mesh, cpa_from, cpa_num, cpa_fuv, ctx->vg_rough2);
4609 }
4610 if (ctx->vg_roughe) {
4612 ctx->mesh, cpa_from, cpa_num, cpa_fuv, ctx->vg_roughe);
4613 }
4614 if (ctx->vg_effector) {
4616 ctx->mesh, cpa_from, cpa_num, cpa_fuv, ctx->vg_effector);
4617 }
4618 if (ctx->vg_twist) {
4620 ctx->mesh, cpa_from, cpa_num, cpa_fuv, ctx->vg_twist);
4621 }
4622}
4624 int p,
4626 const bool vel)
4627{
4629 ParticleSystem *psys = sim->psys;
4630 ParticleSettings *part = sim->psys->part;
4631 Material *ma = BKE_object_material_get(sim->ob, part->omat);
4632 ParticleData *pa;
4633 ChildParticle *cpa;
4634 ParticleTexture ptex;
4635 ParticleKey *par = nullptr, tstate;
4636 ParticleThreadContext ctx; /* fake thread context for child modifiers */
4638
4639 float t;
4640 float co[3], orco[3];
4641 float hairmat[4][4];
4642 int totpart = psys->totpart;
4643 int totchild = psys->totchild;
4644 short between = 0, edit = 0;
4645
4646 int keyed = part->phystype & PART_PHYS_KEYED && psys->flag & PSYS_KEYED;
4647 int cached = !keyed && part->type != PART_HAIR;
4648
4649 float *cpa_fuv;
4650 int cpa_num;
4651 short cpa_from;
4652
4653 /* initialize keys to zero */
4654 ParticleKey keys[4] = {};
4655
4656 t = state->time;
4657 CLAMP(t, 0.0f, 1.0f);
4658
4659 if (p < totpart) {
4660 /* interpolate pathcache directly if it exist */
4661 if (psys->pathcache) {
4663 interpolate_pathcache(psys->pathcache[p], t, &result);
4664 copy_v3_v3(state->co, result.co);
4665 copy_v3_v3(state->vel, result.vel);
4666 copy_qt_qt(state->rot, result.rot);
4667 }
4668 /* otherwise interpolate with other means */
4669 else {
4670 pa = psys->particles + p;
4671
4672 pind.keyed = keyed;
4673 pind.cache = cached ? psys->pointcache : nullptr;
4674 pind.epoint = nullptr;
4675 pind.bspline = (psys->part->flag & PART_HAIR_BSPLINE);
4676 /* `pind.dm` disabled in edit-mode means we don't get effectors taken into
4677 * account when subdividing for instance. */
4678 pind.mesh = psys_in_edit_mode(sim->depsgraph, psys) ?
4679 nullptr :
4680 psys->hair_out_mesh; /* XXX(@sybren): EEK. */
4681 init_particle_interpolation(sim->ob, psys, pa, &pind);
4682 do_particle_interpolation(psys, p, pa, t, &pind, state);
4683
4684 if (pind.mesh) {
4685 mul_m4_v3(sim->ob->object_to_world().ptr(), state->co);
4686 mul_mat3_m4_v3(sim->ob->object_to_world().ptr(), state->vel);
4687 }
4688 else if (!keyed && !cached && !(psys->flag & PSYS_GLOBAL_HAIR)) {
4689 if ((pa->flag & PARS_REKEY) == 0) {
4690 psys_mat_hair_to_global(sim->ob, sim->psmd->mesh_final, part->from, pa, hairmat);
4691 mul_m4_v3(hairmat, state->co);
4692 mul_mat3_m4_v3(hairmat, state->vel);
4693
4694 if (sim->psys->effectors && (part->flag & PART_CHILD_GUIDE) == 0) {
4695 do_guides(
4696 sim->depsgraph, sim->psys->part, sim->psys->effectors, state, p, state->time);
4697 /* TODO: proper velocity handling */
4698 }
4699
4700 if (psys->lattice_deform_data && edit == 0) {
4702 psys->lattice_deform_data, state->co, psys->lattice_strength);
4703 }
4704 }
4705 }
4706 }
4707 }
4708 else if (totchild) {
4709 // invert_m4_m4(imat, ob->object_to_world().ptr());
4710
4711 /* interpolate childcache directly if it exists */
4712 if (psys->childcache) {
4714 interpolate_pathcache(psys->childcache[p - totpart], t, &result);
4715 copy_v3_v3(state->co, result.co);
4716 copy_v3_v3(state->vel, result.vel);
4717 copy_qt_qt(state->rot, result.rot);
4718 }
4719 else {
4720 float par_co[3], par_orco[3];
4721
4722 cpa = psys->child + p - totpart;
4723
4724 if (state->time < 0.0f) {
4725 t = psys_get_child_time(psys, cpa, -state->time, nullptr, nullptr);
4726 }
4727
4728 if (part->childtype == PART_CHILD_FACES) {
4729 /* part->parents could still be 0 so we can't test with totparent */
4730 between = 1;
4731 }
4732 if (between) {
4733 int w = 0;
4734 float foffset;
4735
4736 /* get parent states */
4737 while (w < 4 && cpa->pa[w] >= 0) {
4738 keys[w].time = state->time;
4739 psys_get_particle_on_path(sim, cpa->pa[w], keys + w, true);
4740 w++;
4741 }
4742
4743 /* get the original coordinates (orco) for texture usage */
4744 cpa_num = cpa->num;
4745
4746 foffset = cpa->foffset;
4747 cpa_fuv = cpa->fuv;
4748 cpa_from = PART_FROM_FACE;
4749
4751 cpa_from,
4752 cpa_num,
4754 cpa->fuv,
4755 foffset,
4756 co,
4757 nullptr,
4758 nullptr,
4759 nullptr,
4760 orco);
4761
4762 /* We need to save the actual root position of the child for
4763 * positioning it accurately to the surface of the emitter. */
4764 // copy_v3_v3(cpa_1st, co);
4765
4766 // mul_m4_v3(ob->object_to_world().ptr(), cpa_1st);
4767
4768 pa = psys->particles + cpa->parent;
4769
4771 part->from,
4772 pa->num,
4773 pa->num_dmcache,
4774 pa->fuv,
4775 pa->foffset,
4776 par_co,
4777 nullptr,
4778 nullptr,
4779 nullptr,
4780 par_orco);
4781 if (part->type == PART_HAIR) {
4782 psys_mat_hair_to_global(sim->ob, sim->psmd->mesh_final, psys->part->from, pa, hairmat);
4783 }
4784 else {
4785 unit_m4(hairmat);
4786 }
4787
4788 pa = nullptr;
4789 }
4790 else {
4791 /* get the parent state */
4792 keys->time = state->time;
4793 psys_get_particle_on_path(sim, cpa->parent, keys, true);
4794
4795 /* get the original coordinates (orco) for texture usage */
4796 pa = psys->particles + cpa->parent;
4797
4798 cpa_from = part->from;
4799 cpa_num = pa->num;
4800 cpa_fuv = pa->fuv;
4801
4803 part->from,
4804 pa->num,
4805 pa->num_dmcache,
4806 pa->fuv,
4807 pa->foffset,
4808 par_co,
4809 nullptr,
4810 nullptr,
4811 nullptr,
4812 par_orco);
4813 if (part->type == PART_HAIR) {
4815 cpa_from,
4816 cpa_num,
4818 cpa_fuv,
4819 pa->foffset,
4820 co,
4821 nullptr,
4822 nullptr,
4823 nullptr,
4824 orco);
4825 psys_mat_hair_to_global(sim->ob, sim->psmd->mesh_final, psys->part->from, pa, hairmat);
4826 }
4827 else {
4828 copy_v3_v3(orco, cpa->fuv);
4829 unit_m4(hairmat);
4830 }
4831 }
4832
4833 /* get different child parameters from textures & vgroups */
4834 memset(&ctx, 0, sizeof(ParticleThreadContext));
4835 ctx.sim = *sim;
4836 ctx.mesh = psmd->mesh_final;
4837 ctx.ma = ma;
4838 /* TODO: assign vertex groups */
4839 get_child_modifier_parameters(part, &ctx, cpa, cpa_from, cpa_num, cpa_fuv, orco, &ptex);
4840
4841 if (between) {
4842 int w = 0;
4843
4844 state->co[0] = state->co[1] = state->co[2] = 0.0f;
4845 state->vel[0] = state->vel[1] = state->vel[2] = 0.0f;
4846
4847 /* child position is the weighted sum of parent positions */
4848 while (w < 4 && cpa->pa[w] >= 0) {
4849 state->co[0] += cpa->w[w] * keys[w].co[0];
4850 state->co[1] += cpa->w[w] * keys[w].co[1];
4851 state->co[2] += cpa->w[w] * keys[w].co[2];
4852
4853 state->vel[0] += cpa->w[w] * keys[w].vel[0];
4854 state->vel[1] += cpa->w[w] * keys[w].vel[1];
4855 state->vel[2] += cpa->w[w] * keys[w].vel[2];
4856 w++;
4857 }
4858 /* apply offset for correct positioning */
4859 // add_v3_v3(state->co, cpa_1st);
4860 }
4861 else {
4862 /* offset the child from the parent position */
4863 offset_child(cpa, keys, keys->rot, state, part->childflat, part->childrad);
4864 }
4865
4866 par = keys;
4867
4868 if (vel) {
4869 copy_particle_key(&tstate, state, 1);
4870 }
4871
4872 /* apply different deformations to the child path */
4873 ParticleChildModifierContext modifier_ctx = {nullptr};
4874 modifier_ctx.thread_ctx = nullptr;
4875 modifier_ctx.sim = sim;
4876 modifier_ctx.ptex = &ptex;
4877 modifier_ctx.cpa = cpa;
4878 modifier_ctx.orco = orco;
4879 modifier_ctx.par_co = par->co;
4880 modifier_ctx.par_vel = par->vel;
4881 modifier_ctx.par_rot = par->rot;
4882 modifier_ctx.par_orco = par_orco;
4883 modifier_ctx.parent_keys = psys->childcache ? psys->childcache[p - totpart] : nullptr;
4884 do_child_modifiers(&modifier_ctx, hairmat, state, t);
4885
4886 /* try to estimate correct velocity */
4887 if (vel) {
4888 ParticleKey tstate_tmp;
4889 float length = len_v3(state->vel);
4890
4891 if (t >= 0.001f) {
4892 tstate_tmp.time = t - 0.001f;
4893 psys_get_particle_on_path(sim, p, &tstate_tmp, false);
4894 sub_v3_v3v3(state->vel, state->co, tstate_tmp.co);
4895 normalize_v3(state->vel);
4896 }
4897 else {
4898 tstate_tmp.time = t + 0.001f;
4899 psys_get_particle_on_path(sim, p, &tstate_tmp, false);
4900 sub_v3_v3v3(state->vel, tstate_tmp.co, state->co);
4901 normalize_v3(state->vel);
4902 }
4903
4904 mul_v3_fl(state->vel, length);
4905 }
4906 }
4907 }
4908}
4909
4911 int p,
4913 const bool always)
4914{
4915 ParticleSystem *psys = sim->psys;
4916 ParticleSettings *part = psys->part;
4917 ParticleData *pa = nullptr;
4918 ChildParticle *cpa = nullptr;
4919 float cfra;
4920 int totpart = psys->totpart;
4921 float timestep = psys_get_timestep(sim);
4922
4923 /* negative time means "use current time" */
4924 cfra = state->time > 0 ? state->time : DEG_get_ctime(sim->depsgraph);
4925
4926 if (p >= totpart) {
4927 if (!psys->totchild) {
4928 return false;
4929 }
4930
4931 if (part->childtype == PART_CHILD_FACES) {
4932 if (!(psys->flag & PSYS_KEYED)) {
4933 return false;
4934 }
4935
4936 cpa = psys->child + p - totpart;
4937
4938 state->time = psys_get_child_time(psys, cpa, cfra, nullptr, nullptr);
4939
4940 if (!always) {
4941 if ((state->time < 0.0f && !(part->flag & PART_UNBORN)) ||
4942 (state->time > 1.0f && !(part->flag & PART_DIED)))
4943 {
4944 return false;
4945 }
4946 }
4947
4948 state->time = (cfra - (part->sta + (part->end - part->sta) * psys_frand(psys, p + 23))) /
4949 (part->lifetime * psys_frand(psys, p + 24));
4950
4951 psys_get_particle_on_path(sim, p, state, true);
4952 return true;
4953 }
4954
4955 cpa = sim->psys->child + p - totpart;
4956 pa = sim->psys->particles + cpa->parent;
4957 }
4958 else {
4959 pa = sim->psys->particles + p;
4960 }
4961
4962 if (pa) {
4963 if (!always) {
4964 if ((cfra < pa->time && (part->flag & PART_UNBORN) == 0) ||
4965 (cfra >= pa->dietime && (part->flag & PART_DIED) == 0))
4966 {
4967 return false;
4968 }
4969 }
4970
4971 cfra = std::min(cfra, pa->dietime);
4972 }
4973
4974 if (sim->psys->flag & PSYS_KEYED) {
4975 state->time = -cfra;
4976 psys_get_particle_on_path(sim, p, state, true);
4977 return true;
4978 }
4979
4980 if (cpa) {
4981 float mat[4][4];
4982 ParticleKey *key1;
4983 float t = (cfra - pa->time) / pa->lifetime;
4984 const float par_orco[3] = {0.0f, 0.0f, 0.0f};
4985
4986 key1 = &pa->state;
4987 offset_child(cpa, key1, key1->rot, state, part->childflat, part->childrad);
4988
4989 CLAMP(t, 0.0f, 1.0f);
4990
4991 unit_m4(mat);
4992 ParticleChildModifierContext modifier_ctx = {nullptr};
4993 modifier_ctx.thread_ctx = nullptr;
4994 modifier_ctx.sim = sim;
4995 modifier_ctx.ptex = nullptr;
4996 modifier_ctx.cpa = cpa;
4997 modifier_ctx.orco = cpa->fuv;
4998 modifier_ctx.par_co = key1->co;
4999 modifier_ctx.par_vel = key1->vel;
5000 modifier_ctx.par_rot = key1->rot;
5001 modifier_ctx.par_orco = par_orco;
5002 modifier_ctx.parent_keys = psys->childcache ? psys->childcache[p - totpart] : nullptr;
5003
5004 do_child_modifiers(&modifier_ctx, mat, state, t);
5005
5006 if (psys->lattice_deform_data) {
5008 psys->lattice_deform_data, state->co, psys->lattice_strength);
5009 }
5010 }
5011 else {
5012 if (pa->state.time == cfra || ELEM(part->phystype, PART_PHYS_NO, PART_PHYS_KEYED)) {
5013 copy_particle_key(state, &pa->state, 1);
5014 }
5015 else if (pa->prev_state.time == cfra) {
5017 }
5018 else {
5019 float dfra, frs_sec = sim->scene->r.frs_sec;
5020 /* let's interpolate to try to be as accurate as possible */
5021 if (pa->state.time + 2.0f >= state->time && pa->prev_state.time - 2.0f <= state->time) {
5022 if (pa->prev_state.time >= pa->state.time || pa->prev_state.time < 0.0f) {
5023 /* prev_state is wrong so let's not use it,
5024 * this can happen at frames 1, 0 or particle birth. */
5025 dfra = state->time - pa->state.time;
5026
5027 copy_particle_key(state, &pa->state, 1);
5028
5029 madd_v3_v3v3fl(state->co, state->co, state->vel, dfra / frs_sec);
5030 }
5031 else {
5032 ParticleKey keys[4];
5033 float keytime;
5034
5035 copy_particle_key(keys + 1, &pa->prev_state, 1);
5036 copy_particle_key(keys + 2, &pa->state, 1);
5037
5038 dfra = keys[2].time - keys[1].time;
5039
5040 keytime = (state->time - keys[1].time) / dfra;
5041
5042 /* convert velocity to timestep size */
5043 mul_v3_fl(keys[1].vel, dfra * timestep);
5044 mul_v3_fl(keys[2].vel, dfra * timestep);
5045
5046 psys_interpolate_particle(-1, keys, keytime, state, true);
5047
5048 /* convert back to real velocity */
5049 mul_v3_fl(state->vel, 1.0f / (dfra * timestep));
5050
5051 interp_v3_v3v3(state->ave, keys[1].ave, keys[2].ave, keytime);
5052 interp_qt_qtqt(state->rot, keys[1].rot, keys[2].rot, keytime);
5053 }
5054 }
5055 else if (pa->state.time + 1.0f >= state->time && pa->state.time - 1.0f <= state->time) {
5056 /* linear interpolation using only pa->state */
5057
5058 dfra = state->time - pa->state.time;
5059
5060 copy_particle_key(state, &pa->state, 1);
5061
5062 madd_v3_v3v3fl(state->co, state->co, state->vel, dfra / frs_sec);
5063 }
5064 else {
5065 /* Extrapolating over big ranges is not accurate
5066 * so let's just give something close to reasonable back. */
5067 copy_particle_key(state, &pa->state, 0);
5068 }
5069 }
5070
5071 if (sim->psys->lattice_deform_data) {
5074 }
5075 }
5076
5077 return true;
5078}
5079
5081 ParticleSettings *part,
5083 ParticleData *pa,
5084 ChildParticle *cpa,
5085 float uv[2],
5086 float orco[3])
5087{
5088 float loc[3];
5089 int num;
5090
5091 /* XXX: on checking '(psmd->dm != nullptr)'
5092 * This is incorrect but needed for meta-ball evaluation.
5093 * Ideally this would be calculated via the depsgraph, however with meta-balls,
5094 * the entire scenes dupli's are scanned, which also looks into uncalculated data.
5095 *
5096 * For now just include this workaround as an alternative to crashing,
5097 * but longer term meta-balls should behave in a more manageable way, see: #46622. */
5098
5099 uv[0] = uv[1] = 0.0f;
5100
5101 /* Grid distribution doesn't support UV or emit from vertex mode */
5102 bool is_grid = (part->distr == PART_DISTR_GRID && part->from != PART_FROM_VERT);
5103
5104 if (cpa) {
5105 if ((part->childtype == PART_CHILD_FACES) && (psmd->mesh_final != nullptr)) {
5106 if (!is_grid) {
5107 CustomData *mtf_data = &psmd->mesh_final->fdata_legacy;
5108 const int uv_idx = CustomData_get_render_layer(mtf_data, CD_MTFACE);
5109
5110 if (uv_idx >= 0) {
5111 const MTFace *mtface = static_cast<const MTFace *>(
5112 CustomData_get_layer_n(mtf_data, CD_MTFACE, uv_idx));
5113 if (mtface != nullptr) {
5114 const MFace *mface = static_cast<const MFace *>(
5116 cpa->num,
5117 CD_MFACE,
5118 psmd->mesh_final->totface_legacy));
5119 mtface += cpa->num;
5120 psys_interpolate_uvs(mtface, mface->v4, cpa->fuv, uv);
5121 }
5122 }
5123 }
5124
5127 cpa->num,
5129 cpa->fuv,
5130 cpa->foffset,
5131 loc,
5132 nullptr,
5133 nullptr,
5134 nullptr,
5135 orco);
5136 return;
5137 }
5138
5139 pa = psys->particles + cpa->pa[0];
5140 }
5141
5142 if ((part->from == PART_FROM_FACE) && (psmd->mesh_final != nullptr) && !is_grid) {
5143 num = pa->num_dmcache;
5144
5145 if (num == DMCACHE_NOTFOUND) {
5146 num = pa->num;
5147 }
5148
5149 if (num >= psmd->mesh_final->totface_legacy) {
5150 /* happens when simplify is enabled
5151 * gives invalid coords but would crash otherwise */
5153 }
5154
5156 CustomData *mtf_data = &psmd->mesh_final->fdata_legacy;
5157 const int uv_idx = CustomData_get_render_layer(mtf_data, CD_MTFACE);
5158
5159 if (uv_idx >= 0) {
5160 const MTFace *mtface = static_cast<const MTFace *>(
5161 CustomData_get_layer_n(mtf_data, CD_MTFACE, uv_idx));
5162 const MFace *mface = static_cast<const MFace *>(CustomData_get_for_write(
5164 mtface += num;
5165 psys_interpolate_uvs(mtface, mface->v4, pa->fuv, uv);
5166 }
5167 }
5168 }
5169
5171 part->from,
5172 pa->num,
5173 pa->num_dmcache,
5174 pa->fuv,
5175 pa->foffset,
5176 loc,
5177 nullptr,
5178 nullptr,
5179 nullptr,
5180 orco);
5181}
5182
5184 ParticleData *pa,
5185 ChildParticle *cpa,
5186 ParticleCacheKey *cache,
5187 float mat[4][4],
5188 float *scale)
5189{
5190 Object *ob = sim->ob;
5191 ParticleSystem *psys = sim->psys;
5192 ParticleSystemModifierData *psmd = sim->psmd;
5193 float loc[3], nor[3], vec[3], side[3], len;
5194 float xvec[3] = {-1.0, 0.0, 0.0}, nmat[3][3];
5195
5196 sub_v3_v3v3(vec, (cache + cache->segments)->co, cache->co);
5197 len = normalize_v3(vec);
5198
5199 if (pa == nullptr && psys->part->childflat != PART_CHILD_FACES) {
5200 pa = psys->particles + cpa->pa[0];
5201 }
5202
5203 if (pa) {
5205 sim->psys->part->from,
5206 pa->num,
5207 pa->num_dmcache,
5208 pa->fuv,
5209 pa->foffset,
5210 loc,
5211 nor,
5212 nullptr,
5213 nullptr,
5214 nullptr);
5215 }
5216 else {
5219 cpa->num,
5221 cpa->fuv,
5222 cpa->foffset,
5223 loc,
5224 nor,
5225 nullptr,
5226 nullptr,
5227 nullptr);
5228 }
5229
5230 if (psys->part->rotmode == PART_ROT_VEL) {
5231 transpose_m3_m4(nmat, ob->world_to_object().ptr());
5232 mul_m3_v3(nmat, nor);
5233 normalize_v3(nor);
5234
5235 /* make sure that we get a proper side vector */
5236 if (fabsf(dot_v3v3(nor, vec)) > 0.999999f) {
5237 if (fabsf(dot_v3v3(nor, xvec)) > 0.999999f) {
5238 nor[0] = 0.0f;
5239 nor[1] = 1.0f;
5240 nor[2] = 0.0f;
5241 }
5242 else {
5243 nor[0] = 1.0f;
5244 nor[1] = 0.0f;
5245 nor[2] = 0.0f;
5246 }
5247 }
5248 cross_v3_v3v3(side, nor, vec);
5249 normalize_v3(side);
5250
5251 /* rotate side vector around vec */
5252 if (psys->part->phasefac != 0) {
5253 float q_phase[4];
5254 float phasefac = psys->part->phasefac;
5255 if (psys->part->randphasefac != 0.0f) {
5256 phasefac += psys->part->randphasefac * psys_frand(psys, (pa - psys->particles) + 20);
5257 }
5258 axis_angle_to_quat(q_phase, vec, phasefac * float(M_PI));
5259
5260 mul_qt_v3(q_phase, side);
5261 }
5262
5263 cross_v3_v3v3(nor, vec, side);
5264
5265 unit_m4(mat);
5266 copy_v3_v3(mat[0], vec);
5267 copy_v3_v3(mat[1], side);
5268 copy_v3_v3(mat[2], nor);
5269 }
5270 else {
5271 quat_to_mat4(mat, pa->state.rot);
5272 }
5273
5274 *scale = len;
5275}
5276
5277void psys_apply_hair_lattice(Depsgraph *depsgraph, Scene *scene, Object *ob, ParticleSystem *psys)
5278{
5279 ParticleSimulationData sim = {nullptr};
5280 sim.depsgraph = depsgraph;
5281 sim.scene = scene;
5282 sim.ob = ob;
5283 sim.psys = psys;
5284 sim.psmd = psys_get_modifier(ob, psys);
5285
5286 psys_sim_data_init(&sim);
5287
5288 if (psys->lattice_deform_data) {
5289 ParticleData *pa = psys->particles;
5290 HairKey *hkey;
5291 int p, h;
5292 float hairmat[4][4], imat[4][4];
5293
5294 for (p = 0; p < psys->totpart; p++, pa++) {
5295 psys_mat_hair_to_global(sim.ob, sim.psmd->mesh_final, psys->part->from, pa, hairmat);
5296 invert_m4_m4(imat, hairmat);
5297
5298 hkey = pa->hair;
5299 for (h = 0; h < pa->totkey; h++, hkey++) {
5300 mul_m4_v3(hairmat, hkey->co);
5302 psys->lattice_deform_data, hkey->co, psys->lattice_strength);
5303 mul_m4_v3(imat, hkey->co);
5304 }
5305 }
5306
5307 /* protect the applied shape */
5308 psys->flag |= PSYS_EDITED;
5309 }
5310
5311 psys_sim_data_free(&sim);
5312}
5313
5314/* Draw Engine */
5315
5318
5320{
5321 if (psys->batch_cache) {
5323 }
5324}
5326{
5327 if (psys->batch_cache) {
5329 }
5330}
5331
5333{
5334 LISTBASE_FOREACH (ParticleSystem *, psys, particles) {
5335 BLO_write_struct(writer, ParticleSystem, psys);
5336
5337 if (psys->particles) {
5339
5340 if (psys->particles->hair) {
5341 ParticleData *pa = psys->particles;
5342
5343 for (int a = 0; a < psys->totpart; a++, pa++) {
5344 BLO_write_struct_array(writer, HairKey, pa->totkey, pa->hair);
5345 }
5346 }
5347
5348 if (psys->particles->boid && (psys->part->phystype == PART_PHYS_BOIDS)) {
5350 }
5351
5352 if (psys->part->fluid && (psys->part->phystype == PART_PHYS_FLUID) &&
5354 {
5356 writer, ParticleSpring, psys->tot_fluidsprings, psys->fluid_springs);
5357 }
5358 }
5359 LISTBASE_FOREACH (ParticleTarget *, pt, &psys->targets) {
5360 BLO_write_struct(writer, ParticleTarget, pt);
5361 }
5362
5363 if (psys->child) {
5364 BLO_write_struct_array(writer, ChildParticle, psys->totchild, psys->child);
5365 }
5366
5367 if (psys->clmd) {
5368 BLO_write_struct(writer, ClothModifierData, psys->clmd);
5371 }
5372
5373 BKE_ptcache_blend_write(writer, &psys->ptcaches);
5374 }
5375}
5376
5378{
5379 ParticleData *pa;
5380 int a;
5381
5382 LISTBASE_FOREACH (ParticleSystem *, psys, particles) {
5383 BLO_read_struct_array(reader, ParticleData, psys->totpart, &psys->particles);
5384
5385 if (psys->particles && psys->particles->hair) {
5386 for (a = 0, pa = psys->particles; a < psys->totpart; a++, pa++) {
5387 BLO_read_struct_array(reader, HairKey, pa->totkey, &pa->hair);
5388 }
5389 }
5390
5391 if (psys->particles && psys->particles->keys) {
5392 for (a = 0, pa = psys->particles; a < psys->totpart; a++, pa++) {
5393 pa->keys = nullptr;
5394 pa->totkey = 0;
5395 }
5396
5397 psys->flag &= ~PSYS_KEYED;
5398 }
5399
5400 if (psys->particles && psys->particles->boid) {
5401 pa = psys->particles;
5402 BLO_read_struct_array(reader, BoidParticle, psys->totpart, &pa->boid);
5403
5404 /* This is purely runtime data, but still can be an issue if left dangling. */
5405 pa->boid->ground = nullptr;
5406
5407 for (a = 1, pa++; a < psys->totpart; a++, pa++) {
5408 pa->boid = (pa - 1)->boid + 1;
5409 pa->boid->ground = nullptr;
5410 }
5411 }
5412 else if (psys->particles) {
5413 for (a = 0, pa = psys->particles; a < psys->totpart; a++, pa++) {
5414 pa->boid = nullptr;
5415 }
5416 }
5417
5419
5420 BLO_read_struct_array(reader, ChildParticle, psys->totchild, &psys->child);
5421 psys->effectors = nullptr;
5422
5424
5425 psys->edit = nullptr;
5426 psys->free_edit = nullptr;
5427 psys->pathcache = nullptr;
5428 psys->childcache = nullptr;
5431 psys->pdd = nullptr;
5432
5433 if (psys->clmd) {
5434 BLO_read_struct(reader, ClothModifierData, &psys->clmd);
5435 psys->clmd->clothObject = nullptr;
5436 psys->clmd->hairdata = nullptr;
5437
5440
5441 if (psys->clmd->sim_parms) {
5442 psys->clmd->sim_parms->effector_weights = nullptr;
5443 if (psys->clmd->sim_parms->presets > 10) {
5444 psys->clmd->sim_parms->presets = 0;
5445 }
5446 }
5447
5448 psys->hair_in_mesh = psys->hair_out_mesh = nullptr;
5449 psys->clmd->solver_result = nullptr;
5450 }
5451
5452 BKE_ptcache_blend_read_data(reader, &psys->ptcaches, &psys->pointcache, 0);
5453 if (psys->clmd) {
5454 psys->clmd->point_cache = psys->pointcache;
5455 }
5456
5457 psys->tree = nullptr;
5458 psys->bvhtree = nullptr;
5459
5460 psys->orig_psys = nullptr;
5461 psys->batch_cache = nullptr;
5462 }
5463}
5464
5466 Object *ob,
5467 ID * /*id*/,
5468 ListBase *particles)
5469{
5470 LISTBASE_FOREACH_MUTABLE (ParticleSystem *, psys, particles) {
5471 if (psys->part) {
5472 if (psys->clmd) {
5473 /* XXX(@ideasman42): from reading existing code this seems correct but intended usage
5474 * of point-cache with cloth should be added in #ParticleSystem. */
5475 psys->clmd->point_cache = psys->pointcache;
5476 psys->clmd->ptcaches.first = psys->clmd->ptcaches.last = nullptr;
5477 psys->clmd->modifier.error = nullptr;
5478 }
5479 }
5480 else {
5481 /* Particle modifier must be removed before particle system. */
5485
5486 BLI_remlink(particles, psys);
5487 MEM_freeN(psys);
5488 }
5489 }
5490}
bool BKE_where_on_path(const struct Object *ob, float ctime, float r_vec[4], float r_dir[3], float r_quat[4], float *r_radius, float *r_weight)
struct BoidSettings * boid_copy_settings(const struct BoidSettings *boids)
void boid_free_settings(struct BoidSettings *boids)
Definition boids.cc:1695
void cloth_free_modifier(ClothModifierData *clmd)
Definition cloth.cc:432
bool BKE_collection_has_object_recursive(Collection *collection, Object *ob)
#define FOREACH_COLLECTION_OBJECT_RECURSIVE_END
#define FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(_collection, _object)
ListBase BKE_collection_object_cache_get(Collection *collection)
void BKE_curvemapping_changed_all(CurveMapping *cumap)
void BKE_curvemapping_blend_read(BlendDataReader *reader, CurveMapping *cumap)
CurveMapping * BKE_curvemapping_copy(const CurveMapping *cumap)
void BKE_curvemapping_init(CurveMapping *cumap)
CurveMapping * BKE_curvemapping_add(int tot, float minx, float miny, float maxx, float maxy)
Definition colortools.cc:89
void BKE_curvemapping_free(CurveMapping *cumap)
void BKE_curvemapping_blend_write(BlendWriter *writer, const CurveMapping *cumap)
CustomData interface, see also DNA_customdata_types.h.
const void * CustomData_get_layer_n(const CustomData *data, eCustomDataType type, int n)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
const void * CustomData_get_layer_named(const CustomData *data, eCustomDataType type, blender::StringRef name)
void * CustomData_get_layer_for_write(CustomData *data, eCustomDataType type, int totelem)
int CustomData_get_render_layer(const CustomData *data, eCustomDataType type)
void * CustomData_get_for_write(CustomData *data, int index, eCustomDataType type, int totelem)
support for deformation groups and hooks.
void BKE_defvert_weight_to_rgb(float r_rgb[3], float weight)
Definition deform.cc:1593
float BKE_defvert_find_weight(const MDeformVert *dvert, int defgroup)
Definition deform.cc:763
display list (or rather multi purpose list) stuff.
float BKE_displist_calc_taper(struct Depsgraph *depsgraph, const struct Scene *scene, struct Object *taperobj, int cur, int tot)
void BKE_partdeflect_free(struct PartDeflect *pd)
Definition effect.cc:119
struct PartDeflect * BKE_partdeflect_new(int type)
Definition effect.cc:71
struct EffectorWeights * BKE_effector_add_weights(struct Collection *collection)
Definition effect.cc:58
float effector_falloff(struct EffectorCache *eff, struct EffectorData *efd, struct EffectedPoint *point, struct EffectorWeights *weights)
Definition effect.cc:586
void BKE_effectors_free(struct ListBase *lb)
Definition effect.cc:361
void BKE_effectors_apply(struct ListBase *effectors, struct ListBase *colliders, struct EffectorWeights *weights, struct EffectedPoint *point, float *force, float *wind_force, float *impulse)
Definition effect.cc:1107
struct PartDeflect * BKE_partdeflect_copy(const struct PartDeflect *pd_src)
void pd_point_from_particle(struct ParticleSimulationData *sim, struct ParticleData *pa, struct ParticleKey *state, struct EffectedPoint *point)
Definition effect.cc:378
IDTypeInfo IDType_ID_PA
Definition particle.cc:416
void key_curve_position_weights(float t, float data[4], KeyInterpolationType type)
Definition key.cc:341
LatticeDeformData * BKE_lattice_deform_data_create(const Object *oblatt, const Object *ob) ATTR_WARN_UNUSED_RESULT
void BKE_lattice_deform_data_destroy(LatticeDeformData *lattice_deform_data)
void BKE_lattice_deform_data_eval_co(LatticeDeformData *lattice_deform_data, float co[3], float weight)
void BKE_view_layer_synced_ensure(const Scene *scene, ViewLayer *view_layer)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
void BKE_id_free(Main *bmain, void *idv)
void id_us_plus(ID *id)
Definition lib_id.cc:353
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1495
void id_us_min(ID *id)
Definition lib_id.cc:361
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2611
#define BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL(data_, func_call_)
#define BKE_LIB_FOREACHID_PROCESS_IDSUPER(data_, id_super_, cb_flag_)
@ IDWALK_CB_USER
@ IDWALK_CB_NOP
LibraryForeachIDFlag BKE_lib_query_foreachid_process_flags_get(const LibraryForeachIDData *data)
Definition lib_query.cc:129
@ IDWALK_DO_DEPRECATED_POINTERS
General operations, lookup, etc. for materials.
Material * BKE_object_material_get(Object *ob, short act)
void BKE_mesh_orco_verts_transform(Mesh *mesh, blender::MutableSpan< blender::float3 > orco, bool invert)
void BKE_mesh_texspace_ensure(Mesh *mesh)
BLI_INLINE int BKE_mesh_origindex_mface_mpoly(const int *index_mf_to_mpoly, const int *index_mp_to_orig, const int i)
void BKE_mesh_tessface_ensure(Mesh *mesh)
ModifierData * BKE_modifiers_findby_type(const Object *ob, ModifierType type)
void BKE_modifiers_persistent_uid_init(const Object &object, ModifierData &md)
void BKE_modifier_unique_name(ListBase *modifiers, ModifierData *md)
void BKE_modifier_free(ModifierData *md)
void BKE_modifier_remove_from_list(Object *ob, ModifierData *md)
ModifierData * BKE_modifier_new(int type)
General operations, lookup, etc. for blender objects.
void BKE_object_modifier_set_active(Object *ob, ModifierData *md)
void psys_get_particle_on_path(struct ParticleSimulationData *sim, int pa_num, struct ParticleKey *state, bool vel)
Definition particle.cc:4623
void(*) BKE_particle_batch_cache_free_cb(struct ParticleSystem *psys)
Definition particle.cc:5317
#define PSYS_FRAND_COUNT
struct ModifierData * object_add_particle_system(struct Main *bmain, const struct Scene *scene, struct Object *ob, const char *name)
void psys_get_dupli_path_transform(struct ParticleSimulationData *sim, struct ParticleData *pa, struct ChildParticle *cpa, struct ParticleCacheKey *cache, float mat[4][4], float *scale)
Definition particle.cc:5183
void psys_get_from_key(struct ParticleKey *key, float loc[3], float vel[3], float rot[4], float *time)
Definition particle.cc:3776
bool do_guides(struct Depsgraph *depsgraph, struct ParticleSettings *part, struct ListBase *effectors, ParticleKey *state, int index, float time)
Definition particle.cc:2367
struct ParticleSystemModifierData * psys_get_modifier(struct Object *ob, struct ParticleSystem *psys)
Definition particle.cc:2186
void psys_thread_context_init(struct ParticleThreadContext *ctx, struct ParticleSimulationData *sim)
void psys_vec_rot_to_face(struct Mesh *mesh, struct ParticleData *pa, float vec[3])
Definition particle.cc:3922
void psys_get_dupli_texture(struct ParticleSystem *psys, struct ParticleSettings *part, struct ParticleSystemModifierData *psmd, struct ParticleData *pa, struct ChildParticle *cpa, float uv[2], float orco[3])
Definition particle.cc:5080
#define DMCACHE_NOTFOUND
void precalc_guides(struct ParticleSimulationData *sim, struct ListBase *effectors)
Definition particle.cc:2313
float * psys_cache_vgroup(struct Mesh *mesh, struct ParticleSystem *psys, int vgroup)
Definition particle.cc:2610
void psys_mat_hair_to_orco(struct Object *ob, struct Mesh *mesh, short from, struct ParticleData *pa, float hairmat[4][4])
Definition particle.cc:3901
struct ParticleSystem * psys_orig_get(struct ParticleSystem *psys)
Definition particle.cc:692
void BKE_particlesettings_rough_curve_init(struct ParticleSettings *part)
Definition particle.cc:4142
#define LOOP_SHOWN_PARTICLES
void psys_unique_name(struct Object *object, struct ParticleSystem *psys, const char *defname) ATTR_NONNULL(1
void psys_apply_child_modifiers(struct ParticleThreadContext *ctx, struct ListBase *modifiers, struct ChildParticle *cpa, struct ParticleTexture *ptex, const float orco[3], float hairmat[4][4], struct ParticleCacheKey *keys, struct ParticleCacheKey *parent_keys, const float parent_orco[3])
void psys_disable_all(struct Object *ob)
Definition particle.cc:675
void psys_mat_hair_to_global(struct Object *ob, struct Mesh *mesh, short from, struct ParticleData *pa, float hairmat[4][4])
Definition particle.cc:3931
float PSYS_FRAND_BASE[PSYS_FRAND_COUNT]
Definition particle.cc:448
bool psys_check_enabled(struct Object *ob, struct ParticleSystem *psys, bool use_render_params)
Definition particle.cc:742
void(*) BKE_particle_batch_cache_dirty_tag_cb(struct ParticleSystem *psys, int mode)
Definition particle.cc:5316
void psys_cache_edit_paths(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, struct PTCacheEdit *edit, float cfra, bool use_render_params)
Definition particle.cc:3686
void psys_set_current_num(struct Object *ob, int index)
Definition particle.cc:603
bool psys_check_edited(struct ParticleSystem *psys)
Definition particle.cc:768
void copy_particle_key(struct ParticleKey *to, struct ParticleKey *from, int time)
Definition particle.cc:3765
short psys_get_current_num(struct Object *ob)
Definition particle.cc:584
void BKE_particlesettings_twist_curve_init(struct ParticleSettings *part)
Definition particle.cc:4156
void psys_interpolate_uvs(const struct MTFace *tface, int quad, const float w[4], float uvco[2])
void psys_check_group_weights(struct ParticleSettings *part)
Definition particle.cc:798
void psys_interpolate_particle(short type, struct ParticleKey keys[4], float dt, struct ParticleKey *result, bool velocity)
Definition particle.cc:1172
void psys_thread_context_free(struct ParticleThreadContext *ctx)
void BKE_particlesettings_clump_curve_init(struct ParticleSettings *part)
Definition particle.cc:4128
void psys_mat_hair_to_object(struct Object *ob, struct Mesh *mesh, short from, struct ParticleData *pa, float hairmat[4][4])
Definition particle.cc:3875
bool psys_in_edit_mode(struct Depsgraph *depsgraph, const struct ParticleSystem *psys)
float psys_get_timestep(struct ParticleSimulationData *sim)
Definition particle.cc:4498
void psys_particle_on_emitter(struct ParticleSystemModifierData *psmd, int from, int index, int index_dmcache, float fuv[4], float foffset, float vec[3], float nor[3], float utan[3], float vtan[3], float orco[3])
Definition particle.cc:2277
void BKE_particle_system_blend_read_data(struct BlendDataReader *reader, struct ListBase *particles)
Definition particle.cc:5377
void psys_particle_on_dm(struct Mesh *mesh_final, int from, int index, int index_dmcache, const float fw[4], float foffset, float vec[3], float nor[3], float utan[3], float vtan[3], float orco[3])
Definition particle.cc:2053
float psys_get_dietime_from_cache(struct PointCache *cache, int index)
Definition particle.cc:1306
void psys_free_particles(struct ParticleSystem *psys)
Definition particle.cc:964
unsigned int PSYS_FRAND_SEED_MULTIPLIER[PSYS_FRAND_COUNT]
Definition particle.cc:447
float psys_get_child_time(struct ParticleSystem *psys, struct ChildParticle *cpa, float cfra, float *birthtime, float *dietime)
Definition particle.cc:4502
void psys_interpolate_face(struct Mesh *mesh, const float(*vert_positions)[3], const float(*vert_normals)[3], const struct MFace *mface, struct MTFace *tface, const float(*orcodata)[3], float w[4], float vec[3], float nor[3], float utan[3], float vtan[3], float orco[3])
void psys_free_children(struct ParticleSystem *psys)
Definition particle.cc:954
void free_keyed_keys(struct ParticleSystem *psys)
Definition particle.cc:913
void void psys_cache_paths(struct ParticleSimulationData *sim, float cfra, bool use_render_params)
Definition particle.cc:3280
void BKE_particle_system_blend_read_after_liblink(struct BlendLibReader *reader, struct Object *ob, struct ID *id, struct ListBase *particles)
Definition particle.cc:5465
int psys_uses_gravity(struct ParticleSimulationData *sim)
Definition particle.cc:861
void psys_enable_all(struct Object *ob)
Definition particle.cc:683
void psys_emitter_customdata_mask(struct ParticleSystem *psys, struct CustomData_MeshMasks *r_cddata_masks)
Definition particle.cc:2238
float psys_particle_value_from_verts(struct Mesh *mesh, short from, struct ParticleData *pa, float *values)
Definition particle.cc:2172
int count_particles_mod(struct ParticleSystem *psys, int totgr, int cur)
Definition particle.cc:499
#define LOOP_PARTICLES
void psys_sim_data_free(struct ParticleSimulationData *sim)
Definition particle.cc:665
void object_remove_particle_system(struct Main *bmain, struct Scene *scene, struct Object *ob, struct ParticleSystem *psys)
Definition particle.cc:4015
unsigned int PSYS_FRAND_SEED_OFFSET[PSYS_FRAND_COUNT]
Definition particle.cc:446
void BKE_particle_init_rng(void)
Definition particle.cc:450
struct ParticleSystem * psys_get_current(struct Object *ob)
Definition particle.cc:570
struct ParticleSystem * psys_eval_get(struct Depsgraph *depsgraph, struct Object *object, struct ParticleSystem *psys)
Definition particle.cc:700
void psys_free_path_cache(struct ParticleSystem *psys, struct PTCacheEdit *edit)
Definition particle.cc:939
blender::Vector< ParticleTask > psys_tasks_create(struct ParticleThreadContext *ctx, int startpart, int endpart)
void psys_free_pdd(struct ParticleSystem *psys)
Definition particle.cc:994
void psys_find_group_weights(struct ParticleSettings *part)
Definition particle.cc:777
#define DMCACHE_ISCHILD
int psys_particle_dm_face_lookup(struct Mesh *mesh_final, struct Mesh *mesh_original, int findex_orig, const float fw[4], struct LinkNode **poly_nodes)
Definition particle.cc:1866
int count_particles(struct ParticleSystem *psys)
Definition particle.cc:481
float psys_get_child_size(struct ParticleSystem *psys, struct ChildParticle *cpa, float cfra, float *pa_time)
Definition particle.cc:4534
void psys_get_texture(struct ParticleSimulationData *sim, struct ParticleData *pa, struct ParticleTexture *ptex, int event, float cfra)
Definition particle.cc:4362
void free_hair(struct Object *ob, struct ParticleSystem *psys, int dynamics)
Definition particle.cc:878
void BKE_particle_batch_cache_dirty_tag(struct ParticleSystem *psys, int mode)
Definition particle.cc:5319
void BKE_particle_partdeflect_blend_read_data(struct BlendDataReader *reader, struct PartDeflect *pd)
Definition particle.cc:318
#define PARTICLE_PSMD
void BKE_particle_system_blend_write(struct BlendWriter *writer, struct ListBase *particles)
Definition particle.cc:5332
void psys_free(struct Object *ob, struct ParticleSystem *psys)
Definition particle.cc:1010
void psys_tasks_free(blender::Vector< ParticleTask > &tasks)
void BKE_particle_batch_cache_free(struct ParticleSystem *psys)
Definition particle.cc:5325
void psys_apply_hair_lattice(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, struct ParticleSystem *psys)
Definition particle.cc:5277
void psys_interpolate_mcol(const struct MCol *mcol, int quad, const float w[4], struct MCol *mc)
void psys_find_parents(struct ParticleSimulationData *sim, bool use_render_params)
Definition particle.cc:2636
void psys_cache_child_paths(struct ParticleSimulationData *sim, float cfra, bool editupdate, bool use_render_params)
Definition particle.cc:3181
struct ModifierData * object_copy_particle_system(struct Main *bmain, const struct Scene *scene, struct Object *ob, const struct ParticleSystem *psys_orig)
struct ParticleSettings * BKE_particlesettings_add(struct Main *bmain, const char *name)
Definition particle.cc:4119
void psys_sim_data_init(struct ParticleSimulationData *sim)
Definition particle.cc:624
void psys_copy_particles(struct ParticleSystem *psys_dst, struct ParticleSystem *psys_src)
Definition particle.cc:1088
#define PARTICLE_P
BLI_INLINE float psys_frand(ParticleSystem *psys, unsigned int seed)
bool psys_get_particle_state(struct ParticleSimulationData *sim, int p, struct ParticleKey *state, bool always)
Definition particle.cc:4910
#define PEP_EDIT_RECALC
void BKE_ptcache_id_from_particles(PTCacheID *pid, struct Object *ob, struct ParticleSystem *psys)
struct PointCache * BKE_ptcache_add(struct ListBase *ptcaches)
#define PTCACHE_CLEAR_ALL
void BKE_ptcache_blend_read_data(struct BlendDataReader *reader, struct ListBase *ptcaches, struct PointCache **ocache, int force_disk)
int BKE_ptcache_mem_index_find(struct PTCacheMem *pm, unsigned int index)
void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
void BKE_ptcache_make_particle_key(struct ParticleKey *key, int index, void **data, float time)
void BKE_ptcache_free_list(struct ListBase *ptcaches)
#define PEK_SELECT
void BKE_ptcache_blend_write(struct BlendWriter *writer, struct ListBase *ptcaches)
float BKE_scene_frame_to_ctime(const Scene *scene, int frame)
Definition scene.cc:2372
void BKE_texture_mtex_foreach_id(struct LibraryForeachIDData *data, struct MTex *mtex)
Definition texture.cc:221
#define BLI_assert(a)
Definition BLI_assert.h:46
#define ATTR_FALLTHROUGH
void BLI_bvhtree_free(BVHTree *tree)
A KD-tree for nearest neighbor search.
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:534
#define LISTBASE_FOREACH(type, var, list)
BLI_INLINE void BLI_listbase_clear(ListBase *lb)
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:270
#define LISTBASE_FOREACH_MUTABLE(type, var, list)
#define LISTBASE_FOREACH_BACKWARD(type, var, list)
void void BLI_freelistN(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:497
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:111
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:131
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
MINLINE int max_ii(int a, int b)
MINLINE float safe_acosf(float a)
#define RAD2DEGF(_rad)
#define M_PI
int isect_point_quad_v2(const float p[2], const float v1[2], const float v2[2], const float v3[2], const float v4[2])
float normal_quad_v3(float n[3], const float v1[3], const float v2[3], const float v3[3], const float v4[3])
Definition math_geom.cc:58
void interp_cubic_v3(float x[3], float v[3], const float x1[3], const float v1[3], const float x2[3], const float v2[3], float t)
int isect_point_tri_v2(const float pt[2], const float v1[2], const float v2[2], const float v3[2])
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:41
void interp_weights_poly_v3(float w[], float v[][3], int n, const float co[3])
bool map_to_sphere(float *r_u, float *r_v, float x, float y, float z)
void mul_m3_v3(const float M[3][3], float r[3])
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void mul_m4_v3(const float M[4][4], float r[3])
void transpose_m3_m4(float R[3][3], const float M[4][4])
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void transpose_m4(float R[4][4])
void mul_mat3_m4_v3(const float mat[4][4], float r[3])
void unit_m4(float m[4][4])
void interp_qt_qtqt(float q[4], const float a[4], const float b[4], float t)
void axis_angle_to_quat(float r[4], const float axis[3], float angle)
void quat_to_mat4(float m[4][4], const float q[4])
void mul_qt_v3(const float q[4], float r[3])
void unit_qt(float q[4])
void mat3_to_quat_legacy(float q[4], const float wmat[3][3])
void mul_qt_qtqt(float q[4], const float a[4], const float b[4])
void copy_qt_qt(float q[4], const float a[4])
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void sub_v3_v3(float r[3], const float a[3])
void interp_v3_v3v3v3(float p[3], const float v1[3], const float v2[3], const float v3[3], const float w[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void interp_v4_v4v4(float r[4], const float a[4], const float b[4], float t)
void project_v3_v3v3(float out[3], const float p[3], const float v_proj[3])
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
void interp_v3_v3v3v3v3(float p[3], const float v1[3], const float v2[3], const float v3[3], const float v4[3], const float w[4])
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
MINLINE void zero_v3(float r[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE float normalize_v3(float n[3])
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
ATTR_WARN_UNUSED_RESULT const size_t num
Random number functions.
void int BLI_rng_get_int(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition rand.cc:73
struct RNG * BLI_rng_new(unsigned int seed)
Definition rand.cc:39
void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1)
Definition rand.cc:53
struct RNG * BLI_rng_new_srandom(unsigned int seed)
Definition rand.cc:46
float BLI_rng_get_float(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition rand.cc:88
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:688
unsigned int uint
@ TASK_PRIORITY_HIGH
Definition BLI_task.h:53
void BLI_task_pool_work_and_wait(TaskPool *pool)
Definition task_pool.cc:531
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
Definition task_range.cc:99
TaskPool * BLI_task_pool_create(void *userdata, eTaskPriority priority)
Definition task_pool.cc:480
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
Definition BLI_task.h:221
void BLI_task_pool_free(TaskPool *pool)
Definition task_pool.cc:517
void BLI_task_pool_push(TaskPool *pool, TaskRunFunction run, void *taskdata, bool free_taskdata, TaskFreeFunction freedata)
Definition task_pool.cc:522
#define CLAMP(a, b, c)
#define POINTER_AS_INT(i)
#define ELEM(...)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define BLO_write_id_struct(writer, struct_name, id_address, id)
#define BLO_write_struct(writer, struct_name, data_ptr)
void BLO_read_struct_list_with_type_size(BlendDataReader *reader, size_t base_size, ListBase *list, blender::FunctionRef< size_t(const void *data)> type_size_fn)
Definition readfile.cc:5647
#define BLO_write_struct_array(writer, struct_name, array_size, data_ptr)
#define BLO_read_struct_list(reader, struct_name, list)
#define BLO_read_struct_array(reader, struct_name, array_size, ptr_p)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define BLT_I18NCONTEXT_ID_PARTICLESETTINGS
#define DATA_(msgid)
void DEG_id_tag_update(ID *id, unsigned int flags)
@ DAG_EVAL_RENDER
void DEG_relations_tag_update(Main *bmain)
float DEG_get_ctime(const Depsgraph *graph)
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
ViewLayer * DEG_get_input_view_layer(const Depsgraph *graph)
Scene * DEG_get_input_scene(const Depsgraph *graph)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1026
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:982
@ INDEX_ID_PA
Definition DNA_ID.h:1227
eBoidRuleType
@ eBoidRuleType_Flock
@ eBoidRuleType_Separate
@ eBoidRuleType_Goal
@ eBoidRuleType_None
@ eBoidRuleType_Fight
@ eBoidRuleType_Avoid
@ eBoidRuleType_FollowLeader
@ eBoidRuleType_AvoidCollision
@ eBoidRuleType_AverageSpeed
Object groups, one object can be in many groups at once.
@ CU_PATH_RADIUS
#define DNA_struct_default_get(struct_name)
@ FLUID_DOMAIN_PARTICLE_SPRAY
@ FLUID_DOMAIN_PARTICLE_FOAM
@ FLUID_DOMAIN_PARTICLE_TRACER
@ FLUID_DOMAIN_PARTICLE_FLIP
@ FLUID_DOMAIN_PARTICLE_BUBBLE
@ SNDPARTICLE_COMBINED_EXPORT_OFF
KeyInterpolationType
@ KEY_CARDINAL
@ KEY_BSPLINE
@ TEXCO_ORCO
@ TEXCO_PARTICLE
@ TEXCO_GLOB
@ TEXCO_OBJECT
@ MOD_FLUID_TYPE_DOMAIN
@ MOD_FLUID_TYPE_FLOW
@ eModifierMode_Render
@ eModifierMode_Realtime
@ eModifierType_ParticleSystem
@ eModifierType_Lattice
@ eModifierType_Fluid
@ eModifierType_DynamicPaint
@ OB_MODE_PARTICLE_EDIT
@ PFIELD_GUIDE_PATH_ADD
@ PFIELD_GUIDE_PATH_WEIGHT
@ OB_DUPLIPARTS
@ OB_MESH
@ PART_UNBORN
@ PART_CHILD_LONG_HAIR
@ PART_CHILD_GUIDE
@ PART_CHILD_EFFECT
@ PART_HAIR_BSPLINE
@ PART_DIED
@ PART_ROT_VEL
@ PART_DUPLIW_CURRENT
@ PART_PHYS_FLUID
@ PART_PHYS_KEYED
@ PART_PHYS_BOIDS
@ PART_PHYS_NO
@ PART_DISTR_GRID
@ PARS_DEAD
@ PARS_UNBORN
@ PART_DRAW_COL_MAT
@ PART_ABS_PATH_TIME
@ PSYS_CURRENT
@ PSYS_DISABLED
@ PSYS_HAIR_DYNAMICS
@ PSYS_EDITED
@ PSYS_DELETE
@ PSYS_HAIR_DONE
@ PSYS_GLOBAL_HAIR
@ PSYS_KEYED_TIMING
@ PSYS_SHARED_CACHES
@ PSYS_KEYED
@ PARS_REKEY
@ PARS_UNEXIST
@ PART_FROM_VOLUME
@ PART_FROM_VERT
@ PART_FROM_FACE
@ PAMAP_DENS
@ PAMAP_FIELD
@ PAMAP_CHILD
@ PAMAP_KINK_FREQ
@ PAMAP_TWIST
@ PAMAP_DAMP
@ PAMAP_SIZE
@ PAMAP_IVEL
@ PAMAP_GRAVITY
@ PAMAP_LIFE
@ PAMAP_KINK_AMP
@ PAMAP_TIME
@ PAMAP_CLUMP
@ PAMAP_LENGTH
@ PAMAP_ROUGH
@ PART_KINK_SPIRAL
@ PART_FLUID_FLIP
@ PART_FLUID_BUBBLE
@ PART_FLUID_SPRAYBUBBLE
@ PART_FLUID_TRACER
@ PART_FLUID_FOAM
@ PART_FLUID_SPRAYFOAMBUBBLE
@ PART_FLUID_SPRAYFOAM
@ PART_HAIR
@ PART_FLUID_SPRAY
@ PART_FLUID_FOAMBUBBLE
@ PSYS_TOT_VG
@ PSYS_VG_ROUGHE
@ PSYS_VG_LENGTH
@ PSYS_VG_ROUGH1
@ PSYS_VG_CLUMP
@ PSYS_VG_KINK
@ PSYS_VG_EFFECTOR
@ PSYS_VG_ROUGH2
@ PSYS_VG_TWIST
@ PART_DRAW_GR
@ PART_DRAW_OB
@ PART_CHILD_FACES
@ PART_CHILD_USE_TWIST_CURVE
@ PART_CHILD_USE_CLUMP_CURVE
@ PART_CHILD_USE_CLUMP_NOISE
@ PART_CHILD_USE_ROUGH_CURVE
@ SPH_VISCOELASTIC_SPRINGS
@ PE_DRAW_PART
@ PHYS_GLOBAL_GRAVITY
@ PE_BRUSH_WEIGHT
Read Guarded memory(de)allocation.
#define MAX_MTEX
Definition Stroke.h:31
BMesh const char void * data
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
constexpr const T * data() const
Definition BLI_span.hh:215
#define powf(x, y)
#define fabsf(x)
#define rot(x, k)
#define pow
#define printf(...)
float length(VecOp< float, D >) RET
#define FILTER_ID_OB
#define CD_MASK_ORIGINDEX
#define CD_MASK_ORCO
#define CD_MASK_MDEFORMVERT
#define MEM_SAFE_FREE(v)
#define FILTER_ID_GR
#define FILTER_ID_TE
#define CD_MASK_MTFACE
#define FILTER_ID_PA
#define CD_MASK_ORIGSPACE_MLOOP
const int state
void * MEM_calloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:123
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void * MEM_dupallocN(const void *vmemh)
Definition mallocn.cc:143
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
ccl_device_inline float len(const float2 a)
VecBase< float, 3 > float3
static int psys_map_index_on_dm(Mesh *mesh, int from, int index, int index_dmcache, const float fw[4], float, int *mapindex, float mapfw[4])
Definition particle.cc:1980
static void psys_free_path_cache_buffers(ParticleCacheKey **cache, ListBase *bufs)
Definition particle.cc:554
static void particle_settings_blend_read_after_liblink(BlendLibReader *, ID *id)
Definition particle.cc:407
static void psys_w_to_origspace(const float w[4], float uv[2])
Definition particle.cc:1830
static void particle_settings_blend_read_data(BlendDataReader *reader, ID *id)
Definition particle.cc:345
#define CLAMP_PARTICLE_TEXTURE_POSNEG(type, pvalue)
Definition particle.cc:4261
static void particle_settings_init(ID *id)
Definition particle.cc:92
static ParticleCacheKey ** psys_alloc_path_cache_buffers(ListBase *bufs, int tot, int totkeys)
Definition particle.cc:527
static int get_pointcache_times_for_particle(PointCache *cache, int index, float *r_start, float *r_dietime)
Definition particle.cc:1279
static int get_particle_uv(Mesh *mesh, ParticleData *pa, int index, const float fuv[4], const char *name, float *texco, bool from_vert)
Definition particle.cc:4174
static void psys_cache_edit_paths_iter(void *__restrict iter_data_v, const int iter, const TaskParallelTLS *__restrict)
Definition particle.cc:3520
static float interpolate_particle_value(float v1, float v2, float v3, float v4, const float w[4], int four)
Definition particle.cc:1157
static void get_pointcache_keys_for_time(Object *, PointCache *cache, PTCacheMem **cur, int index, float t, ParticleKey *key1, ParticleKey *key2)
Definition particle.cc:1228
static void init_particle_interpolation(Object *ob, ParticleSystem *psys, ParticleData *pa, ParticleInterpolationData *pind)
Definition particle.cc:1321
static PTCacheEdit * psys_orig_edit_get(ParticleSystem *psys)
Definition particle.cc:716
static void edit_to_particle(ParticleKey *key, PTCacheEditKey *ekey)
Definition particle.cc:1370
static void offset_child(ChildParticle *cpa, ParticleKey *par, float *par_rot, ParticleKey *child, float flat, float radius)
Definition particle.cc:2586
static void get_cpa_texture(Mesh *mesh, ParticleSystem *psys, ParticleSettings *part, ParticleData *par, int child_index, int face_index, const float fw[4], float *orco, ParticleTexture *ptex, int event, float cfra)
Definition particle.cc:4267
#define CLAMP_WARP_PARTICLE_TEXTURE_POS(type, pvalue)
Definition particle.cc:4252
static void psys_thread_create_path(ParticleTask *task, ChildParticle *cpa, ParticleCacheKey *child_keys, int i)
Definition particle.cc:2825
static ParticleCacheKey * pcache_key_segment_endpoint_safe(ParticleCacheKey *key)
Definition particle.cc:522
static void interpolate_pathcache(ParticleCacheKey *first, float t, ParticleCacheKey *result)
Definition particle.cc:1594
static float psys_interpolate_value_from_verts(Mesh *mesh, short from, int index, const float fw[4], const float *values)
Definition particle.cc:1807
static void hair_to_particle(ParticleKey *key, HairKey *hkey)
Definition particle.cc:1378
static void do_particle_interpolation(ParticleSystem *psys, int p, ParticleData *pa, float t, ParticleInterpolationData *pind, ParticleKey *result)
Definition particle.cc:1390
static void triatomat(float *v1, float *v2, float *v3, const float(*uv)[2], float mat[4][4])
Definition particle.cc:3792
static void particle_settings_copy_data(Main *, std::optional< Library * >, ID *id_dst, const ID *id_src, const int)
Definition particle.cc:104
static void exec_child_path_cache(TaskPool *__restrict, void *taskdata)
Definition particle.cc:3165
static void free_child_path_cache(ParticleSystem *psys)
Definition particle.cc:933
static void fluid_free_settings(SPHFluidSettings *fluid)
Definition particle.cc:871
static void psys_origspace_to_w(OrigSpaceFace *osface, int quad, const float w[4], float neww[4])
Definition particle.cc:1837
static void cache_key_incremental_rotation(ParticleCacheKey *key0, ParticleCacheKey *key1, ParticleCacheKey *key2, float *prev_tangent, int i)
Definition particle.cc:3239
static void particle_settings_free_data(ID *id)
Definition particle.cc:143
static size_t boid_rule_struct_size(const eBoidRuleType type)
Definition particle.cc:323
#define CLAMP_PARTICLE_TEXTURE_POS(type, pvalue)
Definition particle.cc:4246
static void particle_settings_blend_write(BlendWriter *writer, ID *id, const void *id_address)
Definition particle.cc:261
static void get_child_modifier_parameters(ParticleSettings *part, ParticleThreadContext *ctx, ChildParticle *cpa, short cpa_from, int cpa_num, float *cpa_fuv, float *orco, ParticleTexture *ptex)
Definition particle.cc:4562
#define PATH_CACHE_BUF_SIZE
Definition particle.cc:520
static void do_path_effectors(ParticleSimulationData *sim, int i, ParticleCacheKey *ca, int k, int steps, float *, float effector, float, float, float *length, float *vec)
Definition particle.cc:2534
static bool psys_thread_context_init_path(ParticleThreadContext *ctx, ParticleSimulationData *sim, Scene *scene, float cfra, const bool editupdate, const bool use_render_params)
Definition particle.cc:2709
static void write_boid_state(BlendWriter *writer, BoidState *state)
Definition particle.cc:226
static void mvert_to_particle(ParticleKey *key, float3 *position, HairKey *hkey)
Definition particle.cc:1384
static ModifierData * object_add_or_copy_particle_system(Main *bmain, const Scene *scene, Object *ob, const char *name, const ParticleSystem *psys_orig)
Definition particle.cc:3945
static void psys_face_mat(Object *ob, Mesh *mesh, ParticleData *pa, float mat[4][4], int orco)
Definition particle.cc:3834
static void particle_settings_foreach_id(ID *id, LibraryForeachIDData *data)
Definition particle.cc:172
static void psys_particle_on_shape(int, int, float *, float vec[3], float nor[3], float utan[3], float vtan[3], float orco[3])
Definition particle.cc:2206
#define SET_PARTICLE_TEXTURE(type, pvalue, texfac)
Definition particle.cc:4240
static void psys_task_init_path(ParticleTask *task, ParticleSimulationData *sim)
Definition particle.cc:2816
float do_clump(ParticleKey *state, const float par_co[3], float time, const float orco_offset[3], float clumpfac, float clumppow, float pa_clump, bool use_clump_noise, float clump_noise_size, const CurveMapping *clumpcurve)
void do_kink(ParticleKey *state, const float par_co[3], const float par_vel[3], const float par_rot[4], float time, float freq, float shape, float amplitude, float flat, short type, short axis, const float obmat[4][4], int smooth_start)
void do_child_modifiers(const ParticleChildModifierContext *modifier_ctx, float mat[4][4], ParticleKey *state, float t)
return ret
struct Object * object
struct Object * ground
struct Object * ob
struct Object * ob
struct ListBase states
ParticleSystemModifierData * psmd
Definition particle.cc:3514
struct ListBase ptcaches
struct ClothSolverResult * solver_result
struct ClothHairData * hairdata
struct Cloth * clothObject
struct PointCache * point_cache
struct ClothSimSettings * sim_parms
struct ClothCollSettings * coll_parms
struct EffectorWeights * effector_weights
CurveMapPoint * curve
CurveMap cm[4]
struct Object * taperobj
struct DynamicPaintBrushSettings * brush
float distance
Definition BKE_effect.h:53
float vec_to_point[3]
Definition BKE_effect.h:52
float nor[3]
Definition BKE_effect.h:49
struct Collection * group
struct ParticleSystem * psys
struct FluidDomainSettings * domain
struct FluidFlowSettings * flow
float world_co[3]
Definition DNA_ID.h:404
void * data
void * link
struct LinkNode * next
void * last
void * first
unsigned int v2
unsigned int v1
unsigned int v4
unsigned int v3
float uv[4][2]
short texco
float fieldfac
float roughfac
float kinkampfac
float lifefac
float padensfac
short blendtype
float kinkfac
float dampfac
float def_var
float clumpfac
float ivelfac
short mapto
struct Object * object
float timefac
float lengthfac
float gravityfac
char uvname[68]
float twistfac
float sizefac
struct Tex * tex
float texspace_size[3]
MeshRuntimeHandle * runtime
CustomData face_data
CustomData vert_data
CustomData fdata_legacy
int totface_legacy
float texspace_location[3]
int verts_num
struct ModifierData * next
ListBase particlesystem
short transflag
ListBase modifiers
struct PTCacheEditKey * keys
ListBase pathcachebufs
struct ParticleCacheKey ** pathcache
PTCacheEditPoint * points
struct ParticleSystem * psys
struct ParticleSystemModifierData * psmd_eval
unsigned int frame
struct PTCacheMem * next
struct PTCacheMem * prev
struct Object * f_source
ParticleThreadContext * thread_ctx
ParticleSimulationData * sim
BoidParticle * boid
ParticleKey state
ParticleKey prev_state
ParticleKey * keys
struct ParticleDupliWeight * next
ParticleKey * kkey[2]
Definition particle.cc:1209
PTCacheEditKey * ekey[2]
Definition particle.cc:1215
PTCacheEditPoint * epoint
Definition particle.cc:1214
struct CurveMapping * clumpcurve
struct PartDeflect * pd2
struct Collection * collision_group
struct Collection * instance_collection
struct Object * bb_ob
struct CurveMapping * roughcurve
struct CurveMapping * twistcurve
struct BoidSettings * boids
struct EffectorWeights * effector_weights
struct MTex * mtex[18]
struct PartDeflect * pd
struct ListBase instance_weights
struct Object * instance_object
struct SPHFluidSettings * fluid
struct Depsgraph * depsgraph
struct ParticleSystemModifierData * psmd
struct Scene * scene
struct ParticleSystem * psys
struct Object * ob
struct ListBase * colliders
struct ParticleSystem * psys
ParticleSpring * fluid_springs
ChildParticle * child
struct PTCacheEdit * edit
struct ListBase ptcaches
ParticleData * particles
struct ListBase targets
ParticleSettings * part
void(*) free_edit(struct PTCacheEdit *edit)
struct ListBase * effectors
struct ParticleSystem * next
struct PointCache * pointcache
struct BVHTree * bvhtree
struct ClothModifierData * clmd
struct LatticeDeformData * lattice_deform_data
struct ParticleCacheKey ** childcache
struct Mesh * hair_in_mesh
struct KDTree_3d * tree
struct ParticleSystem * orig_psys
struct Mesh * hair_out_mesh
struct ParticleDrawData * pdd
struct ParticleCacheKey ** pathcache
struct ParticleTarget * prev
struct ParticleTarget * next
struct RNG * rng_path
ParticleThreadContext * ctx
struct CurveMapping * roughcurve
struct Material * ma
struct CurveMapping * twistcurve
struct CurveMapping * clumpcurve
struct ParticleSimulationData sim
struct ListBase mem_cache
struct PTCacheEdit * edit
Definition rand.cc:33
struct PhysicsSettings physics_settings
struct ToolSettings * toolsettings
struct RenderData r
struct ParticleEditSettings particle
i
Definition text_draw.cc:230
bool RE_texture_evaluate(const MTex *mtex, const float vec[3], const int thread, ImagePool *pool, const bool skip_load_image, const bool texnode_preview, float *r_intensity, float r_rgba[4])
float texture_value_blend(float tex, float out, float fact, float facg, int blendtype)
#define N_(msgid)