mirrored from https://projects.blender.org/blender/blender.git
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
particle_system.c
5027 lines (4213 loc) · 147 KB
/
particle_system.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2007 by Janne Karhu.
* All rights reserved.
* Adaptive time step
* Classical SPH
* Copyright 2011-2012 AutoCRC
*/
/** \file
* \ingroup bke
*/
#include <stddef.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "MEM_guardedalloc.h"
#include "DNA_anim_types.h"
#include "DNA_boid_types.h"
#include "DNA_curve_types.h"
#include "DNA_listBase.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_modifier_types.h"
#include "DNA_object_force_types.h"
#include "DNA_object_types.h"
#include "DNA_particle_types.h"
#include "DNA_scene_types.h"
#include "DNA_texture_types.h"
#include "BLI_blenlib.h"
#include "BLI_edgehash.h"
#include "BLI_kdopbvh.h"
#include "BLI_kdtree.h"
#include "BLI_linklist.h"
#include "BLI_math.h"
#include "BLI_rand.h"
#include "BLI_string_utils.h"
#include "BLI_task.h"
#include "BLI_threads.h"
#include "BLI_utildefines.h"
#include "BKE_animsys.h"
#include "BKE_boids.h"
#include "BKE_collision.h"
#include "BKE_colortools.h"
#include "BKE_effect.h"
#include "BKE_lib_id.h"
#include "BKE_lib_query.h"
#include "BKE_particle.h"
#include "BKE_bvhutils.h"
#include "BKE_cloth.h"
#include "BKE_collection.h"
#include "BKE_lattice.h"
#include "BKE_material.h"
#include "BKE_mesh.h"
#include "BKE_modifier.h"
#include "BKE_object.h"
#include "BKE_pointcache.h"
#include "BKE_scene.h"
#include "DEG_depsgraph.h"
#include "DEG_depsgraph_physics.h"
#include "DEG_depsgraph_query.h"
#include "PIL_time.h"
#include "RE_shader_ext.h"
/* FLUID sim particle import */
#ifdef WITH_FLUID
# include "DNA_fluid_types.h"
# include "manta_fluid_API.h"
#endif // WITH_FLUID
static ThreadRWMutex psys_bvhtree_rwlock = BLI_RWLOCK_INITIALIZER;
/************************************************/
/* Reacting to system events */
/************************************************/
static int particles_are_dynamic(ParticleSystem *psys)
{
if (psys->pointcache->flag & PTCACHE_BAKED) {
return 0;
}
if (psys->part->type == PART_HAIR) {
return psys->flag & PSYS_HAIR_DYNAMICS;
}
return ELEM(psys->part->phystype, PART_PHYS_NEWTON, PART_PHYS_BOIDS, PART_PHYS_FLUID);
}
float psys_get_current_display_percentage(ParticleSystem *psys, const bool use_render_params)
{
ParticleSettings *part = psys->part;
if ((use_render_params &&
!particles_are_dynamic(psys)) || /* non-dynamic particles can be rendered fully */
(part->child_nbr && part->childtype) || /* display percentage applies to children */
(psys->pointcache->flag & PTCACHE_BAKING)) /* baking is always done with full amount */
{
return 1.0f;
}
return psys->part->disp / 100.0f;
}
static int tot_particles(ParticleSystem *psys, PTCacheID *pid)
{
if (pid && psys->pointcache->flag & PTCACHE_EXTERNAL) {
return pid->cache->totpoint;
}
if (psys->part->distr == PART_DISTR_GRID && psys->part->from != PART_FROM_VERT) {
return psys->part->grid_res * psys->part->grid_res * psys->part->grid_res - psys->totunexist;
}
return psys->part->totpart - psys->totunexist;
}
void psys_reset(ParticleSystem *psys, int mode)
{
PARTICLE_P;
if (ELEM(mode, PSYS_RESET_ALL, PSYS_RESET_DEPSGRAPH)) {
if (mode == PSYS_RESET_ALL || !(psys->flag & PSYS_EDITED)) {
/* don't free if not absolutely necessary */
if (psys->totpart != tot_particles(psys, NULL)) {
psys_free_particles(psys);
psys->totpart = 0;
}
psys->totkeyed = 0;
psys->flag &= ~(PSYS_HAIR_DONE | PSYS_KEYED);
if (psys->edit && psys->free_edit) {
psys->free_edit(psys->edit);
psys->edit = NULL;
psys->free_edit = NULL;
}
}
}
else if (mode == PSYS_RESET_CACHE_MISS) {
/* set all particles to be skipped */
LOOP_PARTICLES
{
pa->flag |= PARS_NO_DISP;
}
}
/* reset children */
if (psys->child) {
MEM_freeN(psys->child);
psys->child = NULL;
}
psys->totchild = 0;
/* reset path cache */
psys_free_path_cache(psys, psys->edit);
/* reset point cache */
BKE_ptcache_invalidate(psys->pointcache);
if (psys->fluid_springs) {
MEM_freeN(psys->fluid_springs);
psys->fluid_springs = NULL;
}
psys->tot_fluidsprings = psys->alloc_fluidsprings = 0;
}
void psys_unique_name(Object *object, ParticleSystem *psys, const char *defname)
{
BLI_uniquename(&object->particlesystem,
psys,
defname,
'.',
offsetof(ParticleSystem, name),
sizeof(psys->name));
}
static void realloc_particles(ParticleSimulationData *sim, int new_totpart)
{
ParticleSystem *psys = sim->psys;
ParticleSettings *part = psys->part;
ParticleData *newpars = NULL;
BoidParticle *newboids = NULL;
PARTICLE_P;
int totpart, totsaved = 0;
if (new_totpart < 0) {
if ((part->distr == PART_DISTR_GRID) && (part->from != PART_FROM_VERT)) {
totpart = part->grid_res;
totpart *= totpart * totpart;
}
else {
totpart = part->totpart;
}
}
else {
totpart = new_totpart;
}
if (totpart != psys->totpart) {
if (psys->edit && psys->free_edit) {
psys->free_edit(psys->edit);
psys->edit = NULL;
psys->free_edit = NULL;
}
if (totpart) {
newpars = MEM_callocN(totpart * sizeof(ParticleData), "particles");
if (newpars == NULL) {
return;
}
if (psys->part->phystype == PART_PHYS_BOIDS) {
newboids = MEM_callocN(totpart * sizeof(BoidParticle), "boid particles");
if (newboids == NULL) {
/* allocation error! */
if (newpars) {
MEM_freeN(newpars);
}
return;
}
}
}
if (psys->particles) {
totsaved = MIN2(psys->totpart, totpart);
/*save old pars*/
if (totsaved) {
memcpy(newpars, psys->particles, totsaved * sizeof(ParticleData));
if (psys->particles->boid) {
memcpy(newboids, psys->particles->boid, totsaved * sizeof(BoidParticle));
}
}
if (psys->particles->keys) {
MEM_freeN(psys->particles->keys);
}
if (psys->particles->boid) {
MEM_freeN(psys->particles->boid);
}
for (p = 0, pa = newpars; p < totsaved; p++, pa++) {
if (pa->keys) {
pa->keys = NULL;
pa->totkey = 0;
}
}
for (p = totsaved, pa = psys->particles + totsaved; p < psys->totpart; p++, pa++) {
if (pa->hair) {
MEM_freeN(pa->hair);
}
}
MEM_freeN(psys->particles);
psys_free_pdd(psys);
}
psys->particles = newpars;
psys->totpart = totpart;
if (newboids) {
LOOP_PARTICLES
{
pa->boid = newboids++;
}
}
}
if (psys->child) {
MEM_freeN(psys->child);
psys->child = NULL;
psys->totchild = 0;
}
}
int psys_get_child_number(Scene *scene, ParticleSystem *psys, const bool use_render_params)
{
int nbr;
if (!psys->part->childtype) {
return 0;
}
if (use_render_params) {
nbr = psys->part->ren_child_nbr;
}
else {
nbr = psys->part->child_nbr;
}
return get_render_child_particle_number(&scene->r, nbr, use_render_params);
}
int psys_get_tot_child(Scene *scene, ParticleSystem *psys, const bool use_render_params)
{
return psys->totpart * psys_get_child_number(scene, psys, use_render_params);
}
/************************************************/
/* Distribution */
/************************************************/
void psys_calc_dmcache(Object *ob, Mesh *mesh_final, Mesh *mesh_original, ParticleSystem *psys)
{
/* use for building derived mesh mapping info:
*
* node: the allocated links - total derived mesh element count
* nodearray: the array of nodes aligned with the base mesh's elements, so
* each original elements can reference its derived elements
*/
Mesh *me = (Mesh *)ob->data;
bool use_modifier_stack = psys->part->use_modifier_stack;
PARTICLE_P;
/* CACHE LOCATIONS */
if (!mesh_final->runtime.deformed_only) {
/* Will use later to speed up subsurf/evaluated mesh. */
LinkNode *node, *nodedmelem, **nodearray;
int totdmelem, totelem, i, *origindex, *origindex_poly = NULL;
if (psys->part->from == PART_FROM_VERT) {
totdmelem = mesh_final->totvert;
if (use_modifier_stack) {
totelem = totdmelem;
origindex = NULL;
}
else {
totelem = me->totvert;
origindex = CustomData_get_layer(&mesh_final->vdata, CD_ORIGINDEX);
}
}
else { /* FROM_FACE/FROM_VOLUME */
totdmelem = mesh_final->totface;
if (use_modifier_stack) {
totelem = totdmelem;
origindex = NULL;
origindex_poly = NULL;
}
else {
totelem = mesh_original->totface;
origindex = CustomData_get_layer(&mesh_final->fdata, CD_ORIGINDEX);
/* for face lookups we need the poly origindex too */
origindex_poly = CustomData_get_layer(&mesh_final->pdata, CD_ORIGINDEX);
if (origindex_poly == NULL) {
origindex = NULL;
}
}
}
nodedmelem = MEM_callocN(sizeof(LinkNode) * totdmelem, "psys node elems");
nodearray = MEM_callocN(sizeof(LinkNode *) * totelem, "psys node array");
for (i = 0, node = nodedmelem; i < totdmelem; i++, node++) {
int origindex_final;
node->link = POINTER_FROM_INT(i);
/* may be vertex or face origindex */
if (use_modifier_stack) {
origindex_final = i;
}
else {
origindex_final = origindex ? origindex[i] : ORIGINDEX_NONE;
/* if we have a poly source, do an index lookup */
if (origindex_poly && origindex_final != ORIGINDEX_NONE) {
origindex_final = origindex_poly[origindex_final];
}
}
if (origindex_final != ORIGINDEX_NONE && origindex_final < totelem) {
if (nodearray[origindex_final]) {
/* prepend */
node->next = nodearray[origindex_final];
nodearray[origindex_final] = node;
}
else {
nodearray[origindex_final] = node;
}
}
}
/* cache the verts/faces! */
LOOP_PARTICLES
{
if (pa->num < 0) {
pa->num_dmcache = DMCACHE_NOTFOUND;
continue;
}
if (use_modifier_stack) {
if (pa->num < totelem) {
pa->num_dmcache = DMCACHE_ISCHILD;
}
else {
pa->num_dmcache = DMCACHE_NOTFOUND;
}
}
else {
if (psys->part->from == PART_FROM_VERT) {
if (pa->num < totelem && nodearray[pa->num]) {
pa->num_dmcache = POINTER_AS_INT(nodearray[pa->num]->link);
}
else {
pa->num_dmcache = DMCACHE_NOTFOUND;
}
}
else { /* FROM_FACE/FROM_VOLUME */
pa->num_dmcache = psys_particle_dm_face_lookup(
mesh_final, mesh_original, pa->num, pa->fuv, nodearray);
}
}
}
MEM_freeN(nodearray);
MEM_freeN(nodedmelem);
}
else {
/* TODO PARTICLE, make the following line unnecessary, each function
* should know to use the num or num_dmcache, set the num_dmcache to
* an invalid value, just in case */
LOOP_PARTICLES
{
pa->num_dmcache = DMCACHE_NOTFOUND;
}
}
}
/* threaded child particle distribution and path caching */
void psys_thread_context_init(ParticleThreadContext *ctx, ParticleSimulationData *sim)
{
memset(ctx, 0, sizeof(ParticleThreadContext));
ctx->sim = *sim;
ctx->mesh = ctx->sim.psmd->mesh_final;
ctx->ma = BKE_object_material_get(sim->ob, sim->psys->part->omat);
}
void psys_tasks_create(ParticleThreadContext *ctx,
int startpart,
int endpart,
ParticleTask **r_tasks,
int *r_numtasks)
{
ParticleTask *tasks;
int numtasks = min_ii(BLI_system_thread_count() * 4, endpart - startpart);
float particles_per_task = (float)(endpart - startpart) / (float)numtasks, p, pnext;
int i;
tasks = MEM_callocN(sizeof(ParticleTask) * numtasks, "ParticleThread");
*r_numtasks = numtasks;
*r_tasks = tasks;
p = (float)startpart;
for (i = 0; i < numtasks; i++, p = pnext) {
pnext = p + particles_per_task;
tasks[i].ctx = ctx;
tasks[i].begin = (int)p;
tasks[i].end = min_ii((int)pnext, endpart);
}
}
void psys_tasks_free(ParticleTask *tasks, int numtasks)
{
int i;
/* threads */
for (i = 0; i < numtasks; i++) {
if (tasks[i].rng) {
BLI_rng_free(tasks[i].rng);
}
if (tasks[i].rng_path) {
BLI_rng_free(tasks[i].rng_path);
}
}
MEM_freeN(tasks);
}
void psys_thread_context_free(ParticleThreadContext *ctx)
{
/* path caching */
if (ctx->vg_length) {
MEM_freeN(ctx->vg_length);
}
if (ctx->vg_clump) {
MEM_freeN(ctx->vg_clump);
}
if (ctx->vg_kink) {
MEM_freeN(ctx->vg_kink);
}
if (ctx->vg_rough1) {
MEM_freeN(ctx->vg_rough1);
}
if (ctx->vg_rough2) {
MEM_freeN(ctx->vg_rough2);
}
if (ctx->vg_roughe) {
MEM_freeN(ctx->vg_roughe);
}
if (ctx->vg_twist) {
MEM_freeN(ctx->vg_twist);
}
if (ctx->sim.psys->lattice_deform_data) {
BKE_lattice_deform_data_destroy(ctx->sim.psys->lattice_deform_data);
ctx->sim.psys->lattice_deform_data = NULL;
}
/* distribution */
if (ctx->jit) {
MEM_freeN(ctx->jit);
}
if (ctx->jitoff) {
MEM_freeN(ctx->jitoff);
}
if (ctx->weight) {
MEM_freeN(ctx->weight);
}
if (ctx->index) {
MEM_freeN(ctx->index);
}
if (ctx->seams) {
MEM_freeN(ctx->seams);
}
// if (ctx->vertpart) MEM_freeN(ctx->vertpart);
BLI_kdtree_3d_free(ctx->tree);
if (ctx->clumpcurve != NULL) {
BKE_curvemapping_free(ctx->clumpcurve);
}
if (ctx->roughcurve != NULL) {
BKE_curvemapping_free(ctx->roughcurve);
}
if (ctx->twistcurve != NULL) {
BKE_curvemapping_free(ctx->twistcurve);
}
}
static void init_particle_texture(ParticleSimulationData *sim, ParticleData *pa, int p)
{
ParticleSystem *psys = sim->psys;
ParticleSettings *part = psys->part;
ParticleTexture ptex;
psys_get_texture(sim, pa, &ptex, PAMAP_INIT, 0.f);
switch (part->type) {
case PART_EMITTER:
if (ptex.exist < psys_frand(psys, p + 125)) {
pa->flag |= PARS_UNEXIST;
}
pa->time = part->sta + (part->end - part->sta) * ptex.time;
break;
case PART_HAIR:
if (ptex.exist < psys_frand(psys, p + 125)) {
pa->flag |= PARS_UNEXIST;
}
pa->time = 0.f;
break;
}
}
/* set particle parameters that don't change during particle's life */
void init_particle(ParticleSimulationData *sim, ParticleData *pa)
{
ParticleSettings *part = sim->psys->part;
float birth_time = (float)(pa - sim->psys->particles) / (float)sim->psys->totpart;
pa->flag &= ~PARS_UNEXIST;
pa->time = part->sta + (part->end - part->sta) * birth_time;
pa->hair_index = 0;
/* we can't reset to -1 anymore since we've figured out correct index in distribute_particles */
/* usage other than straight after distribute has to handle this index by itself - jahka*/
// pa->num_dmcache = DMCACHE_NOTFOUND; /* assume we don't have a derived mesh face */
}
static void initialize_all_particles(ParticleSimulationData *sim)
{
ParticleSystem *psys = sim->psys;
ParticleSettings *part = psys->part;
/* Grid distributionsets UNEXIST flag, need to take care of
* it here because later this flag is being reset.
*
* We can't do it for any distribution, because it'll then
* conflict with texture influence, which does not free
* unexisting particles and only sets flag.
*
* It's not so bad, because only grid distribution sets
* UNEXIST flag.
*/
const bool emit_from_volume_grid = (part->distr == PART_DISTR_GRID) &&
(!ELEM(part->from, PART_FROM_VERT, PART_FROM_CHILD));
PARTICLE_P;
LOOP_PARTICLES
{
if (!(emit_from_volume_grid && (pa->flag & PARS_UNEXIST) != 0)) {
init_particle(sim, pa);
}
}
}
static void free_unexisting_particles(ParticleSimulationData *sim)
{
ParticleSystem *psys = sim->psys;
PARTICLE_P;
psys->totunexist = 0;
LOOP_PARTICLES
{
if (pa->flag & PARS_UNEXIST) {
psys->totunexist++;
}
}
if (psys->totpart && psys->totunexist == psys->totpart) {
if (psys->particles->boid) {
MEM_freeN(psys->particles->boid);
}
MEM_freeN(psys->particles);
psys->particles = NULL;
psys->totpart = psys->totunexist = 0;
}
if (psys->totunexist) {
int newtotpart = psys->totpart - psys->totunexist;
ParticleData *npa, *newpars;
npa = newpars = MEM_callocN(newtotpart * sizeof(ParticleData), "particles");
for (p = 0, pa = psys->particles; p < newtotpart; p++, pa++, npa++) {
while (pa->flag & PARS_UNEXIST) {
pa++;
}
memcpy(npa, pa, sizeof(ParticleData));
}
if (psys->particles->boid) {
MEM_freeN(psys->particles->boid);
}
MEM_freeN(psys->particles);
psys->particles = newpars;
psys->totpart -= psys->totunexist;
if (psys->particles->boid) {
BoidParticle *newboids = MEM_callocN(psys->totpart * sizeof(BoidParticle), "boid particles");
LOOP_PARTICLES
{
pa->boid = newboids++;
}
}
}
}
static void get_angular_velocity_vector(short avemode, ParticleKey *state, float vec[3])
{
switch (avemode) {
case PART_AVE_VELOCITY:
copy_v3_v3(vec, state->vel);
break;
case PART_AVE_HORIZONTAL: {
float zvec[3];
zvec[0] = zvec[1] = 0;
zvec[2] = 1.f;
cross_v3_v3v3(vec, state->vel, zvec);
break;
}
case PART_AVE_VERTICAL: {
float zvec[3], temp[3];
zvec[0] = zvec[1] = 0;
zvec[2] = 1.f;
cross_v3_v3v3(temp, state->vel, zvec);
cross_v3_v3v3(vec, temp, state->vel);
break;
}
case PART_AVE_GLOBAL_X:
vec[0] = 1.f;
vec[1] = vec[2] = 0;
break;
case PART_AVE_GLOBAL_Y:
vec[1] = 1.f;
vec[0] = vec[2] = 0;
break;
case PART_AVE_GLOBAL_Z:
vec[2] = 1.f;
vec[0] = vec[1] = 0;
break;
}
}
void psys_get_birth_coords(
ParticleSimulationData *sim, ParticleData *pa, ParticleKey *state, float dtime, float cfra)
{
Object *ob = sim->ob;
ParticleSystem *psys = sim->psys;
ParticleSettings *part = psys->part;
ParticleTexture ptex;
float fac, phasefac, nor[3] = {0, 0, 0}, loc[3], vel[3] = {0.0, 0.0, 0.0}, rot[4], q2[4];
float r_vel[3], r_ave[3], r_rot[4], vec[3], p_vel[3] = {0.0, 0.0, 0.0};
float x_vec[3] = {1.0, 0.0, 0.0}, utan[3] = {0.0, 1.0, 0.0}, vtan[3] = {0.0, 0.0, 1.0},
rot_vec[3] = {0.0, 0.0, 0.0};
float q_phase[4];
const bool use_boids = ((part->phystype == PART_PHYS_BOIDS) && (pa->boid != NULL));
const bool use_tangents = ((use_boids == false) &&
((part->tanfac != 0.0f) || (part->rotmode == PART_ROT_NOR_TAN)));
int p = pa - psys->particles;
/* get birth location from object */
if (use_tangents) {
psys_particle_on_emitter(sim->psmd,
part->from,
pa->num,
pa->num_dmcache,
pa->fuv,
pa->foffset,
loc,
nor,
utan,
vtan,
0);
}
else {
psys_particle_on_emitter(
sim->psmd, part->from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, loc, nor, 0, 0, 0);
}
/* get possible textural influence */
psys_get_texture(sim, pa, &ptex, PAMAP_IVEL, cfra);
/* particles live in global space so */
/* let's convert: */
/* -location */
mul_m4_v3(ob->obmat, loc);
/* -normal */
mul_mat3_m4_v3(ob->obmat, nor);
normalize_v3(nor);
/* -tangent */
if (use_tangents) {
#if 0
float phase = vg_rot ?
2.0f *
(psys_particle_value_from_verts(sim->psmd->dm, part->from, pa, vg_rot) -
0.5f) :
0.0f;
#else
float phase = 0.0f;
#endif
mul_v3_fl(vtan, -cosf((float)M_PI * (part->tanphase + phase)));
fac = -sinf((float)M_PI * (part->tanphase + phase));
madd_v3_v3fl(vtan, utan, fac);
mul_mat3_m4_v3(ob->obmat, vtan);
copy_v3_v3(utan, nor);
mul_v3_fl(utan, dot_v3v3(vtan, nor));
sub_v3_v3(vtan, utan);
normalize_v3(vtan);
}
/* -velocity (boids need this even if there's no random velocity) */
if (part->randfac != 0.0f || (part->phystype == PART_PHYS_BOIDS && pa->boid)) {
r_vel[0] = 2.0f * (psys_frand(psys, p + 10) - 0.5f);
r_vel[1] = 2.0f * (psys_frand(psys, p + 11) - 0.5f);
r_vel[2] = 2.0f * (psys_frand(psys, p + 12) - 0.5f);
mul_mat3_m4_v3(ob->obmat, r_vel);
normalize_v3(r_vel);
}
/* -angular velocity */
if (part->avemode == PART_AVE_RAND) {
r_ave[0] = 2.0f * (psys_frand(psys, p + 13) - 0.5f);
r_ave[1] = 2.0f * (psys_frand(psys, p + 14) - 0.5f);
r_ave[2] = 2.0f * (psys_frand(psys, p + 15) - 0.5f);
mul_mat3_m4_v3(ob->obmat, r_ave);
normalize_v3(r_ave);
}
/* -rotation */
if (part->randrotfac != 0.0f) {
r_rot[0] = 2.0f * (psys_frand(psys, p + 16) - 0.5f);
r_rot[1] = 2.0f * (psys_frand(psys, p + 17) - 0.5f);
r_rot[2] = 2.0f * (psys_frand(psys, p + 18) - 0.5f);
r_rot[3] = 2.0f * (psys_frand(psys, p + 19) - 0.5f);
normalize_qt(r_rot);
mat4_to_quat(rot, ob->obmat);
mul_qt_qtqt(r_rot, r_rot, rot);
}
if (use_boids) {
float dvec[3], q[4], mat[3][3];
copy_v3_v3(state->co, loc);
/* boids don't get any initial velocity */
zero_v3(state->vel);
/* boids store direction in ave */
if (fabsf(nor[2]) == 1.0f) {
sub_v3_v3v3(state->ave, loc, ob->obmat[3]);
normalize_v3(state->ave);
}
else {
copy_v3_v3(state->ave, nor);
}
/* calculate rotation matrix */
project_v3_v3v3(dvec, r_vel, state->ave);
sub_v3_v3v3(mat[0], state->ave, dvec);
normalize_v3(mat[0]);
negate_v3_v3(mat[2], r_vel);
normalize_v3(mat[2]);
cross_v3_v3v3(mat[1], mat[2], mat[0]);
/* apply rotation */
mat3_to_quat_is_ok(q, mat);
copy_qt_qt(state->rot, q);
}
else {
/* conversion done so now we apply new: */
/* -velocity from: */
/* *reactions */
if (dtime > 0.f) {
sub_v3_v3v3(vel, pa->state.vel, pa->prev_state.vel);
}
/* *emitter velocity */
if (dtime != 0.f && part->obfac != 0.f) {
sub_v3_v3v3(vel, loc, state->co);
mul_v3_fl(vel, part->obfac / dtime);
}
/* *emitter normal */
if (part->normfac != 0.f) {
madd_v3_v3fl(vel, nor, part->normfac);
}
/* *emitter tangent */
if (sim->psmd && part->tanfac != 0.f) {
madd_v3_v3fl(vel, vtan, part->tanfac);
}
/* *emitter object orientation */
if (part->ob_vel[0] != 0.f) {
normalize_v3_v3(vec, ob->obmat[0]);
madd_v3_v3fl(vel, vec, part->ob_vel[0]);
}
if (part->ob_vel[1] != 0.f) {
normalize_v3_v3(vec, ob->obmat[1]);
madd_v3_v3fl(vel, vec, part->ob_vel[1]);
}
if (part->ob_vel[2] != 0.f) {
normalize_v3_v3(vec, ob->obmat[2]);
madd_v3_v3fl(vel, vec, part->ob_vel[2]);
}
/* *texture */
/* TODO */
/* *random */
if (part->randfac != 0.f) {
madd_v3_v3fl(vel, r_vel, part->randfac);
}
/* *particle */
if (part->partfac != 0.f) {
madd_v3_v3fl(vel, p_vel, part->partfac);
}
mul_v3_v3fl(state->vel, vel, ptex.ivel);
/* -location from emitter */
copy_v3_v3(state->co, loc);
/* -rotation */
unit_qt(state->rot);
if (part->rotmode) {
bool use_global_space;
/* create vector into which rotation is aligned */
switch (part->rotmode) {
case PART_ROT_NOR:
case PART_ROT_NOR_TAN:
copy_v3_v3(rot_vec, nor);
use_global_space = false;
break;
case PART_ROT_VEL:
copy_v3_v3(rot_vec, vel);
use_global_space = true;
break;
case PART_ROT_GLOB_X:
case PART_ROT_GLOB_Y:
case PART_ROT_GLOB_Z:
rot_vec[part->rotmode - PART_ROT_GLOB_X] = 1.0f;
use_global_space = true;
break;
case PART_ROT_OB_X:
case PART_ROT_OB_Y:
case PART_ROT_OB_Z:
copy_v3_v3(rot_vec, ob->obmat[part->rotmode - PART_ROT_OB_X]);
use_global_space = false;
break;
default:
use_global_space = true;
break;
}
/* create rotation quat */
if (use_global_space) {
negate_v3(rot_vec);
vec_to_quat(q2, rot_vec, OB_POSX, OB_POSZ);
/* randomize rotation quat */
if (part->randrotfac != 0.0f) {
interp_qt_qtqt(rot, q2, r_rot, part->randrotfac);
}
else {
copy_qt_qt(rot, q2);
}
}
else {
/* calculate rotation in local-space */
float q_obmat[4];
float q_imat[4];
mat4_to_quat(q_obmat, ob->obmat);
invert_qt_qt_normalized(q_imat, q_obmat);
if (part->rotmode != PART_ROT_NOR_TAN) {
float rot_vec_local[3];
/* rot_vec */
negate_v3(rot_vec);
copy_v3_v3(rot_vec_local, rot_vec);
mul_qt_v3(q_imat, rot_vec_local);
normalize_v3(rot_vec_local);
vec_to_quat(q2, rot_vec_local, OB_POSX, OB_POSZ);
}
else {
/* (part->rotmode == PART_ROT_NOR_TAN) */
float tmat[3][3];
/* note: utan_local is not taken from 'utan', we calculate from rot_vec/vtan */
/* note: it looks like rotation phase may be applied twice (once with vtan, again below)
* however this isn't the case - campbell */
float *rot_vec_local = tmat[0];
float *vtan_local = tmat[1];
float *utan_local = tmat[2];
/* use tangents */
BLI_assert(use_tangents == true);
/* rot_vec */