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