Blender V5.3
particle_child.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
11#include "BLI_math_vector_c.hh"
12#include "BLI_noise_c.hh"
13
14#include "DNA_material_types.h"
15#include "DNA_object_types.h"
16
17#include "BKE_colortools.hh"
18#include "BKE_particle.h"
19
20#include "particle_private.h"
21
22namespace blender {
23
24/* ------------------------------------------------------------------------- */
25
34
36 ParticleCacheKey *keys,
37 int totkeys,
38 ParticleCacheKey *parent,
39 int index)
40{
41 BLI_assert(index >= 0 && index < totkeys);
42
43 iter->key = keys + index;
44 iter->index = index;
45 iter->time = float(index) / float(totkeys - 1);
46
47 if (parent) {
48 iter->parent_key = parent + index;
49 if (index > 0) {
50 mul_qt_qtqt(iter->parent_rotation, iter->parent_key->rot, parent->rot);
51 }
52 else {
53 copy_qt_qt(iter->parent_rotation, parent->rot);
54 }
55 }
56 else {
57 iter->parent_key = nullptr;
59 }
60}
61
62/* ------------------------------------------------------------------------- */
63
65 const float dir[3],
66 const float kink[3],
67 float time,
68 float freq,
69 float shape,
70 float amplitude,
71 const float spiral_start[3])
72{
73 float result[3];
74
75 CLAMP(time, 0.0f, 1.0f);
76
78
79 {
80 /* Creates a logarithmic spiral:
81 * r(theta) = a * exp(b * theta)
82 *
83 * The "density" parameter b is defined by the shape parameter
84 * and goes up to the Golden Spiral for 1.0
85 * https://en.wikipedia.org/wiki/Golden_spiral
86 */
87 const float b = shape * (1.0f + sqrtf(5.0f)) / float(M_PI) * 0.25f;
88 /* angle of the spiral against the curve (rotated opposite to make a smooth transition) */
89 const float start_angle = ((b != 0.0f) ? atanf(1.0f / b) : float(-M_PI_2)) +
90 (b > 0.0f ? -float(M_PI_2) : float(M_PI_2));
91
92 float spiral_axis[3], rot[3][3];
93 float vec[3];
94
95 float theta = freq * time * 2.0f * float(M_PI);
96 float radius = amplitude * expf(b * theta);
97
98 /* a bit more intuitive than using negative frequency for this */
99 if (amplitude < 0.0f) {
100 theta = -theta;
101 }
102
103 cross_v3_v3v3(spiral_axis, dir, kink);
104 normalize_v3(spiral_axis);
105
106 mul_v3_v3fl(vec, kink, -radius);
107
108 axis_angle_normalized_to_mat3(rot, spiral_axis, theta);
109 mul_m3_v3(rot, vec);
110
111 madd_v3_v3fl(vec, kink, amplitude);
112
113 axis_angle_normalized_to_mat3(rot, spiral_axis, -start_angle);
114 mul_m3_v3(rot, vec);
115
116 add_v3_v3v3(result, spiral_start, vec);
117 }
118
119 copy_v3_v3(state->co, result);
120}
121
123 ParticleTexture *ptex,
124 const float parent_orco[3],
125 ChildParticle *cpa,
126 const float orco[3],
127 float hairmat[4][4],
128 ParticleCacheKey *keys,
129 ParticleCacheKey *parent_keys,
130 int *r_totkeys,
131 float *r_max_length)
132{
133 ParticleSettings *part = ctx->sim.psys->part;
134 const int seed = ctx->sim.psys->child_seed + int(cpa - ctx->sim.psys->child);
135 const int totkeys = ctx->segments + 1;
136 const int extrakeys = ctx->extra_segments;
137
138 float kink_amp_random = part->kink_amp_random;
139 float kink_amp = part->kink_amp *
140 (1.0f - kink_amp_random * psys_frand(ctx->sim.psys, 93541 + seed));
141 float kink_freq = part->kink_freq;
142 float kink_shape = part->kink_shape;
143 float kink_axis_random = part->kink_axis_random;
144
146 ParticleCacheKey *key;
147 int k;
148
149 float dir[3];
150 float spiral_start[3] = {0.0f, 0.0f, 0.0f};
151 float spiral_start_time = 0.0f;
152 float spiral_par_co[3] = {0.0f, 0.0f, 0.0f};
153 float spiral_par_vel[3] = {0.0f, 0.0f, 0.0f};
154 float spiral_par_rot[4] = {1.0f, 0.0f, 0.0f, 0.0f};
155 float totlen;
156 float cut_time;
157 int start_index = 0, end_index = 0;
158 float kink_base[3];
159
160 kink_amp *= ptex->kink_amp;
161 kink_freq *= ptex->kink_freq;
162
163 cut_time = (totkeys - 1) * ptex->length;
164 zero_v3(spiral_start);
165
166 for (k = 0, key = keys; k < totkeys - 1; k++, key++) {
167 if (float(k + 1) >= cut_time) {
168 float fac = cut_time - float(k);
169 ParticleCacheKey *par = parent_keys + k;
170
171 start_index = k + 1;
172 end_index = start_index + extrakeys;
173
174 spiral_start_time = (float(k) + fac) / float(totkeys - 1);
175 interp_v3_v3v3(spiral_start, key->co, (key + 1)->co, fac);
176
177 interp_v3_v3v3(spiral_par_co, par->co, (par + 1)->co, fac);
178 interp_v3_v3v3(spiral_par_vel, par->vel, (par + 1)->vel, fac);
179 interp_qt_qtqt(spiral_par_rot, par->rot, (par + 1)->rot, fac);
180
181 break;
182 }
183 }
184
185 zero_v3(dir);
186
187 zero_v3(kink_base);
188 kink_base[part->kink_axis] = 1.0f;
189 mul_mat3_m4_v3(ctx->sim.ob->object_to_world().ptr(), kink_base);
190
191 /* Fill in invariant part of modifier context. */
192 ParticleChildModifierContext modifier_ctx = {nullptr};
193 modifier_ctx.thread_ctx = ctx;
194 modifier_ctx.sim = &ctx->sim;
195 modifier_ctx.ptex = ptex;
196 modifier_ctx.cpa = cpa;
197 modifier_ctx.orco = orco;
198 modifier_ctx.parent_keys = parent_keys;
199
200 for (k = 0, key = keys; k < end_index; k++, key++) {
201 float par_time;
202 float *par_co, *par_vel, *par_rot;
203
204 psys_path_iter_get(&iter, keys, end_index, nullptr, k);
205 if (k < start_index) {
206 sub_v3_v3v3(dir, (key + 1)->co, key->co);
207 normalize_v3(dir);
208
209 par_time = float(k) / float(totkeys - 1);
210 par_co = parent_keys[k].co;
211 par_vel = parent_keys[k].vel;
212 par_rot = parent_keys[k].rot;
213 }
214 else {
215 float spiral_time = float(k - start_index) / float(extrakeys - 1);
216 float kink[3], tmp[3];
217
218 /* use same time value for every point on the spiral */
219 par_time = spiral_start_time;
220 par_co = spiral_par_co;
221 par_vel = spiral_par_vel;
222 par_rot = spiral_par_rot;
223
224 project_v3_v3v3(tmp, kink_base, dir);
225 sub_v3_v3v3(kink, kink_base, tmp);
226 normalize_v3(kink);
227
228 if (kink_axis_random > 0.0f) {
229 float a = kink_axis_random * (psys_frand(ctx->sim.psys, 7112 + seed) * 2.0f - 1.0f) *
230 float(M_PI);
231 float rot[3][3];
232
234 mul_m3_v3(rot, kink);
235 }
236
237 do_kink_spiral_deform(reinterpret_cast<ParticleKey *>(key),
238 dir,
239 kink,
240 spiral_time,
241 kink_freq,
242 kink_shape,
243 kink_amp,
244 spiral_start);
245 }
246
247 /* Fill in variant part of modifier context. */
248 modifier_ctx.par_co = par_co;
249 modifier_ctx.par_vel = par_vel;
250 modifier_ctx.par_rot = par_rot;
251 modifier_ctx.par_orco = parent_orco;
252
253 /* Apply different deformations to the child path/ */
254 do_child_modifiers(&modifier_ctx, hairmat, reinterpret_cast<ParticleKey *>(key), par_time);
255 }
256
257 totlen = 0.0f;
258 for (k = 0, key = keys; k < end_index - 1; k++, key++) {
259 totlen += len_v3v3((key + 1)->co, key->co);
260 }
261
262 *r_totkeys = end_index;
263 *r_max_length = totlen;
264}
265
266/* ------------------------------------------------------------------------- */
267
268static bool check_path_length(int k,
269 ParticleCacheKey *keys,
270 ParticleCacheKey *key,
271 float max_length,
272 float step_length,
273 float *cur_length,
274 float dvec[3])
275{
276 if (*cur_length + step_length > max_length) {
277 sub_v3_v3v3(dvec, key->co, (key - 1)->co);
278 mul_v3_fl(dvec, (max_length - *cur_length) / step_length);
279 add_v3_v3v3(key->co, (key - 1)->co, dvec);
280 keys->segments = k;
281 /* something over the maximum step value */
282 return false;
283 }
284
285 *cur_length += step_length;
286 return true;
287}
288
290 ListBaseT<ModifierData> * /*modifiers*/,
291 ChildParticle *cpa,
292 ParticleTexture *ptex,
293 const float orco[3],
294 float hairmat[4][4],
295 ParticleCacheKey *keys,
296 ParticleCacheKey *parent_keys,
297 const float parent_orco[3])
298{
299 ParticleSettings *part = ctx->sim.psys->part;
300 Material *ma = ctx->ma;
301 const bool draw_col_ma = (part->draw_col == PART_DRAW_COL_MAT);
302 const bool use_length_check = !ELEM(part->kink, PART_KINK_SPIRAL);
303
304 ParticleCacheKey *key;
305 int totkeys, k;
306 float max_length;
307
308 /* TODO: for the future: use true particle modifiers that work on the whole curve. */
309
310 if (part->kink == PART_KINK_SPIRAL) {
312 ctx, ptex, parent_orco, cpa, orco, hairmat, keys, parent_keys, &totkeys, &max_length);
313 keys->segments = totkeys - 1;
314 }
315 else {
316 /* Fill in invariant part of modifier context. */
317 ParticleChildModifierContext modifier_ctx = {nullptr};
318 modifier_ctx.thread_ctx = ctx;
319 modifier_ctx.sim = &ctx->sim;
320 modifier_ctx.ptex = ptex;
321 modifier_ctx.cpa = cpa;
322 modifier_ctx.orco = orco;
323 modifier_ctx.parent_keys = parent_keys;
324
325 totkeys = ctx->segments + 1;
326 max_length = ptex->length;
327
328 for (k = 0, key = keys; k < totkeys; k++, key++) {
330 psys_path_iter_get(&iter, keys, totkeys, parent_keys, k);
331
332 ParticleKey *par = reinterpret_cast<ParticleKey *>(iter.parent_key);
333
334 /* Fill in variant part of modifier context. */
335 modifier_ctx.par_co = par->co;
336 modifier_ctx.par_vel = par->vel;
337 modifier_ctx.par_rot = iter.parent_rotation;
338 modifier_ctx.par_orco = parent_orco;
339
340 /* Apply different deformations to the child path. */
341 do_child_modifiers(&modifier_ctx, hairmat, reinterpret_cast<ParticleKey *>(key), iter.time);
342 }
343 }
344
345 {
346 const float step_length = 1.0f / float(totkeys - 1);
347 float cur_length = 0.0f;
348
349 if (max_length <= 0.0f) {
350 keys->segments = -1;
351 totkeys = 0;
352 }
353
354 /* we have to correct velocity because of kink & clump */
355 for (k = 0, key = keys; k < totkeys; k++, key++) {
356 if (k >= 2) {
357 sub_v3_v3v3((key - 1)->vel, key->co, (key - 2)->co);
358 mul_v3_fl((key - 1)->vel, 0.5);
359 }
360
361 if (use_length_check && k > 0) {
362 float dvec[3];
363 /* check if path needs to be cut before actual end of data points */
364 if (!check_path_length(k, keys, key, max_length, step_length, &cur_length, dvec)) {
365 /* last key */
366 sub_v3_v3v3(key->vel, key->co, (key - 1)->co);
367 if (ma && draw_col_ma) {
368 copy_v3_v3(key->col, &ma->r);
369 }
370 break;
371 }
372 }
373 if (k == totkeys - 1) {
374 /* last key */
375 sub_v3_v3v3(key->vel, key->co, (key - 1)->co);
376 }
377
378 if (ma && draw_col_ma) {
379 copy_v3_v3(key->col, &ma->r);
380 }
381 }
382 }
383}
384
385/* ------------------------------------------------------------------------- */
386
388 const float par_co[3],
389 const float par_vel[3],
390 const float par_rot[4],
391 float time,
392 float freq,
393 float shape,
394 float amplitude,
395 float flat,
396 short type,
397 short axis,
398 const float obmat[4][4],
399 int smooth_start)
400{
401 float kink[3] = {1.0f, 0.0f, 0.0f}, par_vec[3];
402 float t, dt = 1.0f, result[3];
403
404 if (ELEM(type, PART_KINK_NO, PART_KINK_SPIRAL)) {
405 return;
406 }
407
408 CLAMP(time, 0.0f, 1.0f);
409
410 if (shape != 0.0f && !ELEM(type, PART_KINK_BRAID)) {
411 if (shape < 0.0f) {
412 time = pow(time, 1.0f + shape);
413 }
414 else {
415 time = pow(time, 1.0f / (1.0f - shape));
416 }
417 }
418
419 t = time * freq * float(M_PI);
420
421 if (smooth_start) {
422 dt = fabsf(t);
423 /* smooth the beginning of kink */
424 CLAMP(dt, 0.0f, float(M_PI));
425 dt = sinf(dt / 2.0f);
426 }
427
428 if (!ELEM(type, PART_KINK_RADIAL)) {
429 float temp[3];
430
431 kink[axis] = 1.0f;
432
433 if (obmat) {
434 mul_mat3_m4_v3(obmat, kink);
435 }
436
437 mul_qt_v3(par_rot, kink);
438
439 /* make sure kink is normal to strand */
440 project_v3_v3v3(temp, kink, par_vel);
441 sub_v3_v3(kink, temp);
442 normalize_v3(kink);
443 }
444
445 copy_v3_v3(result, state->co);
446 sub_v3_v3v3(par_vec, par_co, state->co);
447
448 switch (type) {
449 case PART_KINK_CURL: {
450 float curl_offset[3];
451 float q1[4] = {1.0f, 0.0f, 0.0f, 0.0f};
452
453 /* rotate kink vector around strand tangent */
454 mul_v3_v3fl(curl_offset, kink, amplitude);
455 axis_angle_to_quat(q1, par_vel, t);
456 mul_qt_v3(q1, curl_offset);
457
458 interp_v3_v3v3(par_vec, state->co, par_co, flat);
459 add_v3_v3v3(result, par_vec, curl_offset);
460 break;
461 }
462 case PART_KINK_RADIAL: {
463 if (flat > 0.0f) {
464 float proj[3];
465 /* flatten along strand */
466 project_v3_v3v3(proj, par_vec, par_vel);
467 madd_v3_v3fl(result, proj, flat);
468 }
469
470 madd_v3_v3fl(result, par_vec, -amplitude * sinf(t));
471 break;
472 }
473 case PART_KINK_WAVE: {
474 madd_v3_v3fl(result, kink, amplitude * sinf(t));
475
476 if (flat > 0.0f) {
477 float proj[3];
478 /* flatten along wave */
479 project_v3_v3v3(proj, par_vec, kink);
480 madd_v3_v3fl(result, proj, flat);
481
482 /* flatten along strand */
483 project_v3_v3v3(proj, par_vec, par_vel);
484 madd_v3_v3fl(result, proj, flat);
485 }
486 break;
487 }
488 case PART_KINK_BRAID: {
489 float y_vec[3] = {0.0f, 1.0f, 0.0f};
490 float z_vec[3] = {0.0f, 0.0f, 1.0f};
491 float vec_one[3], state_co[3];
492 float inp_y, inp_z, length;
493
494 if (par_rot) {
495 mul_qt_v3(par_rot, y_vec);
496 mul_qt_v3(par_rot, z_vec);
497 }
498
499 negate_v3(par_vec);
500 normalize_v3_v3(vec_one, par_vec);
501
502 inp_y = dot_v3v3(y_vec, vec_one);
503 inp_z = dot_v3v3(z_vec, vec_one);
504
505 if (inp_y > 0.5f) {
506 copy_v3_v3(state_co, y_vec);
507
508 mul_v3_fl(y_vec, amplitude * cosf(t));
509 mul_v3_fl(z_vec, amplitude / 2.0f * sinf(2.0f * t));
510 }
511 else if (inp_z > 0.0f) {
512 mul_v3_v3fl(state_co, z_vec, sinf(float(M_PI) / 3.0f));
513 madd_v3_v3fl(state_co, y_vec, -0.5f);
514
515 mul_v3_fl(y_vec, -amplitude * cosf(t + float(M_PI) / 3.0f));
516 mul_v3_fl(z_vec, amplitude / 2.0f * cosf(2.0f * t + float(M_PI) / 6.0f));
517 }
518 else {
519 mul_v3_v3fl(state_co, z_vec, -sinf(float(M_PI) / 3.0f));
520 madd_v3_v3fl(state_co, y_vec, -0.5f);
521
522 mul_v3_fl(y_vec, amplitude * -sinf(t + float(M_PI) / 6.0f));
523 mul_v3_fl(z_vec, amplitude / 2.0f * -sinf(2.0f * t + float(M_PI) / 3.0f));
524 }
525
526 mul_v3_fl(state_co, amplitude);
527 add_v3_v3(state_co, par_co);
528 sub_v3_v3v3(par_vec, state->co, state_co);
529
530 length = normalize_v3(par_vec);
531 mul_v3_fl(par_vec, std::min(length, amplitude / 2.0f));
532
533 add_v3_v3v3(state_co, par_co, y_vec);
534 add_v3_v3(state_co, z_vec);
535 add_v3_v3(state_co, par_vec);
536
537 shape = 2.0f * float(M_PI) * (1.0f + shape);
538
539 if (t < shape) {
540 shape = t / shape;
541 shape = float(sqrt(double(shape)));
542 interp_v3_v3v3(result, result, state_co, shape);
543 }
544 else {
545 copy_v3_v3(result, state_co);
546 }
547 break;
548 }
549 }
550
551 /* blend the start of the kink */
552 if (dt < 1.0f) {
553 interp_v3_v3v3(state->co, state->co, result, dt);
554 }
555 else {
556 copy_v3_v3(state->co, result);
557 }
558}
559
560static float do_clump_level(float result[3],
561 const float co[3],
562 const float par_co[3],
563 float time,
564 float clumpfac,
565 float clumppow,
566 float pa_clump,
567 const CurveMapping *clumpcurve)
568{
569 float clump = 0.0f;
570
571 if (clumpcurve) {
572 clump = pa_clump *
573 (1.0f - clamp_f(BKE_curvemapping_evaluateF(clumpcurve, 0, time), 0.0f, 1.0f));
574
575 interp_v3_v3v3(result, co, par_co, clump);
576 }
577 else if (clumpfac != 0.0f) {
578 float cpow;
579
580 if (clumppow < 0.0f) {
581 cpow = 1.0f + clumppow;
582 }
583 else {
584 cpow = 1.0f + 9.0f * clumppow;
585 }
586
587 if (clumpfac < 0.0f) { /* clump roots instead of tips */
588 clump = -clumpfac * pa_clump * float(pow(1.0 - double(time), double(cpow)));
589 }
590 else {
591 clump = clumpfac * pa_clump * float(pow(double(time), double(cpow)));
592 }
593
594 interp_v3_v3v3(result, co, par_co, clump);
595 }
596
597 return clump;
598}
599
601 const float par_co[3],
602 float time,
603 const float orco_offset[3],
604 float clumpfac,
605 float clumppow,
606 float pa_clump,
607 bool use_clump_noise,
608 float clump_noise_size,
609 const CurveMapping *clumpcurve)
610{
611 float clump;
612
613 if (use_clump_noise && clump_noise_size != 0.0f) {
614 float center[3], noisevec[3];
615 float da[4], pa[12];
616
617 mul_v3_v3fl(noisevec, orco_offset, 1.0f / clump_noise_size);
618 BLI_noise_voronoi(noisevec[0], noisevec[1], noisevec[2], da, pa, 1.0f, 0);
619 mul_v3_fl(&pa[0], clump_noise_size);
620 add_v3_v3v3(center, par_co, &pa[0]);
621
622 do_clump_level(state->co, state->co, center, time, clumpfac, clumppow, pa_clump, clumpcurve);
623 }
624
625 clump = do_clump_level(
626 state->co, state->co, par_co, time, clumpfac, clumppow, pa_clump, clumpcurve);
627
628 return clump;
629}
630
631static void do_rough(const float loc[3],
632 const float mat[4][4],
633 float t,
634 float fac,
635 float size,
636 float thres,
638{
639 float rough[3];
640 float rco[3];
641
642 if (thres != 0.0f) {
643 if (fabsf(float(-1.5f + loc[0] + loc[1] + loc[2])) < 1.5f * thres) {
644 return;
645 }
646 }
647
648 copy_v3_v3(rco, loc);
649 mul_v3_fl(rco, t);
650 rough[0] = -1.0f +
651 2.0f * BLI_noise_generic_turbulence(size, rco[0], rco[1], rco[2], 2, false, 2);
652 rough[1] = -1.0f +
653 2.0f * BLI_noise_generic_turbulence(size, rco[1], rco[2], rco[0], 2, false, 2);
654 rough[2] = -1.0f +
655 2.0f * BLI_noise_generic_turbulence(size, rco[2], rco[0], rco[1], 2, false, 2);
656
657 madd_v3_v3fl(state->co, mat[0], fac * rough[0]);
658 madd_v3_v3fl(state->co, mat[1], fac * rough[1]);
659 madd_v3_v3fl(state->co, mat[2], fac * rough[2]);
660}
661
662static void do_rough_end(
663 const float loc[3], const float mat[4][4], float t, float fac, float shape, ParticleKey *state)
664{
665 float rough[2];
666 float roughfac;
667
668 roughfac = fac * float(pow(double(t), shape));
669 copy_v2_v2(rough, loc);
670 rough[0] = -1.0f + 2.0f * rough[0];
671 rough[1] = -1.0f + 2.0f * rough[1];
672 mul_v2_fl(rough, roughfac);
673
674 madd_v3_v3fl(state->co, mat[0], rough[0]);
675 madd_v3_v3fl(state->co, mat[1], rough[1]);
676}
677
678static void do_rough_curve(const float loc[3],
679 const float mat[4][4],
680 float time,
681 float fac,
682 float size,
683 CurveMapping *roughcurve,
685{
686 float rough[3];
687 float rco[3];
688
689 if (!roughcurve) {
690 return;
691 }
692
693 fac *= clamp_f(BKE_curvemapping_evaluateF(roughcurve, 0, time), 0.0f, 1.0f);
694
695 copy_v3_v3(rco, loc);
696 mul_v3_fl(rco, time);
697 rough[0] = -1.0f +
698 2.0f * BLI_noise_generic_turbulence(size, rco[0], rco[1], rco[2], 2, false, 2);
699 rough[1] = -1.0f +
700 2.0f * BLI_noise_generic_turbulence(size, rco[1], rco[2], rco[0], 2, false, 2);
701 rough[2] = -1.0f +
702 2.0f * BLI_noise_generic_turbulence(size, rco[2], rco[0], rco[1], 2, false, 2);
703
704 madd_v3_v3fl(state->co, mat[0], fac * rough[0]);
705 madd_v3_v3fl(state->co, mat[1], fac * rough[1]);
706 madd_v3_v3fl(state->co, mat[2], fac * rough[2]);
707}
708
710{
711 ParticleThreadContext *thread_ctx = modifier_ctx->thread_ctx;
712 return (thread_ctx != nullptr) ? thread_ctx->segments : modifier_ctx->sim->psys->part->draw_step;
713}
714
715static void twist_get_axis(const ParticleChildModifierContext *modifier_ctx,
716 const float time,
717 float r_axis[3])
718{
719 const int num_segments = twist_num_segments(modifier_ctx);
720 const int index = clamp_i(time * num_segments, 0, num_segments);
721 if (index > 0) {
723 r_axis, modifier_ctx->parent_keys[index].co, modifier_ctx->parent_keys[index - 1].co);
724 }
725 else {
727 r_axis, modifier_ctx->parent_keys[index + 1].co, modifier_ctx->parent_keys[index].co);
728 }
729}
730
732 float start,
733 float end,
734 float step)
735{
736 float integral = 0.0f;
737 float x = start;
738 while (x < end) {
739 float y = BKE_curvemapping_evaluateF(curve, 0, x);
740 y = clamp_f(y, 0.0f, 1.0f);
741 /* TODO(sergey): Clamp last step to end. */
742 integral += y * step;
743 x += step;
744 }
745 return integral;
746}
747
748static void do_twist(const ParticleChildModifierContext *modifier_ctx,
750 const float time)
751{
752 ParticleThreadContext *thread_ctx = modifier_ctx->thread_ctx;
753 ParticleSimulationData *sim = modifier_ctx->sim;
754 ParticleTexture *ptex = modifier_ctx->ptex;
755 ParticleSettings *part = sim->psys->part;
756 /* Early output checks. */
757 if (modifier_ctx->parent_keys == nullptr) {
758 /* Cannot get axis of rotation... */
759 return;
760 }
761 if (part->childtype != PART_CHILD_PARTICLES) {
762 /* Interpolated children behave weird with twist. */
763 return;
764 }
765 if (part->twist == 0.0f) {
766 /* No twist along the strand. */
767 return;
768 }
769 /* Dependent on whether it's threaded update or not, curve comes
770 * from different places.
771 */
772 CurveMapping *twist_curve = nullptr;
774 twist_curve = (thread_ctx != nullptr) ? thread_ctx->twistcurve : part->twistcurve;
775 }
776 /* Axis of rotation. */
777 float axis[3];
778 twist_get_axis(modifier_ctx, time, axis);
779 /* Angle of rotation. */
780 float angle = part->twist;
781 if (ptex != nullptr) {
782 angle *= (ptex->twist - 0.5f) * 2.0f;
783 }
784 if (twist_curve != nullptr) {
785 const int num_segments = twist_num_segments(modifier_ctx);
786 angle *= BKE_curvemapping_integrate_clamped(twist_curve, 0.0f, time, 1.0f / num_segments);
787 }
788 else {
789 angle *= time;
790 }
791 /* Perform rotation around parent curve. */
792 float vec[3];
793 sub_v3_v3v3(vec, state->co, modifier_ctx->par_co);
794 rotate_v3_v3v3fl(state->co, vec, axis, angle * 2.0f * M_PI);
795 add_v3_v3(state->co, modifier_ctx->par_co);
796}
797
799 float mat[4][4],
801 float t)
802{
803 ParticleThreadContext *ctx = modifier_ctx->thread_ctx;
804 ParticleSimulationData *sim = modifier_ctx->sim;
805 ParticleTexture *ptex = modifier_ctx->ptex;
806 ChildParticle *cpa = modifier_ctx->cpa;
807 ParticleSettings *part = sim->psys->part;
808 CurveMapping *clumpcurve = nullptr, *roughcurve = nullptr;
809 int i = cpa - sim->psys->child;
810 int guided = 0;
811
813 clumpcurve = (ctx != nullptr) ? ctx->clumpcurve : part->clumpcurve;
814 }
816 roughcurve = (ctx != nullptr) ? ctx->roughcurve : part->roughcurve;
817 }
818
819 float kink_amp = part->kink_amp;
820 float kink_amp_clump = part->kink_amp_clump;
821 float kink_freq = part->kink_freq;
822 float rough1 = part->rough1;
823 float rough2 = part->rough2;
824 float rough_end = part->rough_end;
825 const bool smooth_start = (sim->psys->part->childtype == PART_CHILD_FACES);
826
827 if (ptex) {
828 kink_amp *= ptex->kink_amp;
829 kink_freq *= ptex->kink_freq;
830 rough1 *= ptex->rough1;
831 rough2 *= ptex->rough2;
832 rough_end *= ptex->roughe;
833 }
834
835 do_twist(modifier_ctx, state, t);
836
837 if (part->flag & PART_CHILD_EFFECT) {
838 /* state is safe to cast, since only co and vel are used */
839 guided = do_guides(
840 sim->depsgraph, sim->psys->part, sim->psys->effectors, state, cpa->parent, t);
841 }
842
843 if (guided == 0) {
844 float orco_offset[3];
845 float clump;
846
847 sub_v3_v3v3(orco_offset, modifier_ctx->orco, modifier_ctx->par_orco);
848 clump = do_clump(state,
849 modifier_ctx->par_co,
850 t,
851 orco_offset,
852 part->clumpfac,
853 part->clumppow,
854 ptex ? ptex->clump : 1.0f,
856 part->clump_noise_size,
857 clumpcurve);
858
859 if (kink_freq != 0.0f) {
860 kink_amp *= (1.0f - kink_amp_clump * clump);
861
863 modifier_ctx->par_co,
864 modifier_ctx->par_vel,
865 modifier_ctx->par_rot,
866 t,
867 kink_freq,
868 part->kink_shape,
869 kink_amp,
870 part->kink_flat,
871 part->kink,
872 part->kink_axis,
873 sim->ob->object_to_world().ptr(),
874 smooth_start);
875 }
876 }
877
878 if (roughcurve) {
879 do_rough_curve(modifier_ctx->orco, mat, t, rough1, part->rough1_size, roughcurve, state);
880 }
881 else {
882 if (rough1 > 0.0f) {
883 do_rough(modifier_ctx->orco, mat, t, rough1, part->rough1_size, 0.0, state);
884 }
885
886 if (rough2 > 0.0f) {
887 float vec[3];
888 psys_frand_vec(sim->psys, i + 27, vec);
889 do_rough(vec, mat, t, rough2, part->rough2_size, part->rough2_thres, state);
890 }
891
892 if (rough_end > 0.0f) {
893 float vec[3];
894 psys_frand_vec(sim->psys, i + 27, vec);
895 do_rough_end(vec, mat, t, rough_end, part->rough_end_shape, state);
896 }
897 }
898}
899
900} // namespace blender
#define M_PI_2
#define M_PI
Object is a sort of wrapper for general info.
#define CLAMP(a, b, c)
#define ELEM(...)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
#define expf(x)
#define rot(x, k)
#define flat
#define pow
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
float length(VecOp< float, D >) RET
const int state
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void psys_path_iter_get(ParticlePathIterator *iter, ParticleCacheKey *keys, int totkeys, ParticleCacheKey *parent, int index)
void mul_m3_v3(const float M[3][3], float r[3])
float BLI_noise_generic_turbulence(float noisesize, float x, float y, float z, int oct, bool hard, int noisebasis)
Definition noise_c.cc:1212
float do_clump(ParticleKey *state, const float par_co[3], float time, const float orco_offset[3], float clumpfac, float clumppow, float pa_clump, bool use_clump_noise, float clump_noise_size, const CurveMapping *clumpcurve)
void do_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])
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
static void do_kink_spiral_deform(ParticleKey *state, const float dir[3], const float kink[3], float time, float freq, float shape, float amplitude, const float spiral_start[3])
MINLINE void mul_v2_fl(float r[2], float f)
static void do_kink_spiral(ParticleThreadContext *ctx, ParticleTexture *ptex, const float parent_orco[3], ChildParticle *cpa, const float orco[3], float hairmat[4][4], ParticleCacheKey *keys, ParticleCacheKey *parent_keys, int *r_totkeys, float *r_max_length)
MINLINE void copy_v2_v2(float r[2], const float a[2])
static int twist_num_segments(const ParticleChildModifierContext *modifier_ctx)
void axis_angle_to_quat(float r[4], const float axis[3], float angle)
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])
static void do_rough_curve(const float loc[3], const float mat[4][4], float time, float fac, float size, CurveMapping *roughcurve, ParticleKey *state)
static void do_rough_end(const float loc[3], const float mat[4][4], float t, float fac, float shape, ParticleKey *state)
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])
MINLINE void mul_v3_fl(float r[3], float f)
static void twist_get_axis(const ParticleChildModifierContext *modifier_ctx, const float time, float r_axis[3])
static bool check_path_length(int k, ParticleCacheKey *keys, ParticleCacheKey *key, float max_length, float step_length, float *cur_length, float dvec[3])
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 mul_qt_v3(const float q[4], float r[3])
void unit_qt(float q[4])
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
static void do_rough(const float loc[3], const float mat[4][4], float t, float fac, float size, float thres, ParticleKey *state)
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
void copy_qt_qt(float q[4], const float a[4])
void BLI_noise_voronoi(float x, float y, float z, float *da, float *pa, float me, int dtype)
Definition noise_c.cc:917
void interp_qt_qtqt(float q[4], const float a[4], const float b[4], float t)
sqrt(x)+1/max(0
static void do_twist(const ParticleChildModifierContext *modifier_ctx, ParticleKey *state, const float time)
MINLINE void negate_v3(float r[3])
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
MINLINE void zero_v3(float r[3])
void mul_mat3_m4_v3(const float mat[4][4], float r[3])
MINLINE float normalize_v3_v3(float r[3], const float a[3])
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
static float BKE_curvemapping_integrate_clamped(const CurveMapping *curve, float start, float end, float step)
MINLINE float normalize_v3(float n[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
BLI_INLINE void psys_frand_vec(ParticleSystem *psys, unsigned int seed, float vec[3])
static float do_clump_level(float result[3], const float co[3], const float par_co[3], float time, float clumpfac, float clumppow, float pa_clump, const CurveMapping *clumpcurve)
bool do_guides(struct Depsgraph *depsgraph, struct ParticleSettings *part, ListBaseT< EffectorCache > *effectors, ParticleKey *state, int index, float time)
Definition particle.cc:2378
BLI_INLINE float psys_frand(ParticleSystem *psys, unsigned int seed)
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void axis_angle_normalized_to_mat3(float R[3][3], const float axis[3], float angle)
MINLINE int clamp_i(int value, int min, int max)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
void rotate_v3_v3v3fl(float r[3], const float p[3], const float axis[3], float angle)
MINLINE float clamp_f(float value, float min, float max)
#define fabsf
#define sqrtf
#define atanf
#define sinf
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct CurveMapping * twistcurve
struct CurveMapping * clumpcurve
struct CurveMapping * roughcurve
struct ParticleSystem * psys
struct ListBaseT< struct EffectorCache > * effectors
struct CurveMapping * clumpcurve
struct CurveMapping * roughcurve
struct CurveMapping * twistcurve
struct ParticleSimulationData sim