d797669901154aa6e8a7719521fdfa92ca682cff
[blender.git] / source / blender / blenkernel / intern / particle_child.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) Blender Foundation
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Lukas Toenne
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/particle_child.c
29  *  \ingroup bke
30  */
31
32 #include "BLI_math.h"
33 #include "BLI_noise.h"
34
35 #include "DNA_material_types.h"
36
37 #include "BKE_colortools.h"
38 #include "BKE_particle.h"
39
40 struct Material;
41
42 void do_kink(ParticleKey *state, ParticleKey *par, float *par_rot, float time, float freq, float shape, float amplitude, float flat,
43              short type, short axis, float obmat[4][4], int smooth_start);
44 float do_clump(ParticleKey *state, ParticleKey *par, float time, float clumpfac, float clumppow, float pa_clump, CurveMapping *clumpcurve);
45 void do_child_modifiers(ParticleSimulationData *sim,
46                         ParticleTexture *ptex, ParticleKey *par, float *par_rot, ChildParticle *cpa,
47                         const float orco[3], float mat[4][4], ParticleKey *state, float t);
48
49 static void get_strand_normal(Material *ma, const float surfnor[3], float surfdist, float nor[3])
50 {
51         float cross[3], nstrand[3], vnor[3], blend;
52
53         if (!((ma->mode & MA_STR_SURFDIFF) || (ma->strand_surfnor > 0.0f)))
54                 return;
55
56         if (ma->mode & MA_STR_SURFDIFF) {
57                 cross_v3_v3v3(cross, surfnor, nor);
58                 cross_v3_v3v3(nstrand, nor, cross);
59
60                 blend = dot_v3v3(nstrand, surfnor);
61                 CLAMP(blend, 0.0f, 1.0f);
62
63                 interp_v3_v3v3(vnor, nstrand, surfnor, blend);
64                 normalize_v3(vnor);
65         }
66         else {
67                 copy_v3_v3(vnor, nor);
68         }
69         
70         if (ma->strand_surfnor > 0.0f) {
71                 if (ma->strand_surfnor > surfdist) {
72                         blend = (ma->strand_surfnor - surfdist) / ma->strand_surfnor;
73                         interp_v3_v3v3(vnor, vnor, surfnor, blend);
74                         normalize_v3(vnor);
75                 }
76         }
77
78         copy_v3_v3(nor, vnor);
79 }
80
81 /* ------------------------------------------------------------------------- */
82
83 typedef struct ParticlePathIterator {
84         ParticleCacheKey *key;
85         int index;
86         float time;
87         
88         ParticleCacheKey *parent_key;
89         float parent_rotation[4];
90 } ParticlePathIterator;
91
92 static void psys_path_iter_get(ParticlePathIterator *iter, ParticleCacheKey *keys, int totkeys, ParticleCacheKey *parent, int index)
93 {
94         BLI_assert(index >= 0 && index < totkeys);
95         
96         iter->key = keys + index;
97         iter->index = index;
98         iter->time = (float)index / (float)(totkeys - 1);
99         
100         if (parent) {
101                 iter->parent_key = parent + index;
102                 if (index > 0)
103                         mul_qt_qtqt(iter->parent_rotation, iter->parent_key->rot, parent->rot);
104                 else
105                         copy_qt_qt(iter->parent_rotation, parent->rot);
106         }
107         else {
108                 iter->parent_key = NULL;
109                 unit_qt(iter->parent_rotation);
110         }
111 }
112
113 typedef struct ParticlePathModifier {
114         struct ParticlePathModifier *next, *prev;
115         
116         void (*apply)(ParticleCacheKey *keys, int totkeys, ParticleCacheKey *parent_keys);
117 } ParticlePathModifier;
118
119 /* ------------------------------------------------------------------------- */
120
121 static bool check_path_length(int k, ParticleCacheKey *keys, ParticleCacheKey *key, float max_length, float step_length, float *cur_length, float dvec[3])
122 {
123         if (*cur_length + step_length > max_length) {
124                 sub_v3_v3v3(dvec, key->co, (key-1)->co);
125                 mul_v3_fl(dvec, (max_length - *cur_length) / step_length);
126                 add_v3_v3v3(key->co, (key-1)->co, dvec);
127                 keys->steps = k;
128                 /* something over the maximum step value */
129                 return false;
130         }
131         else {
132                 *cur_length += step_length;
133                 return true;
134         }
135 }
136
137 void psys_apply_child_modifiers(ParticleThreadContext *ctx, struct ListBase *modifiers,
138                                 ChildParticle *cpa, ParticleTexture *ptex, const float orco[3], const float ornor[3], float hairmat[4][4],
139                                 ParticleCacheKey *keys, ParticleCacheKey *parent_keys)
140 {
141         struct ParticleSettings *part = ctx->sim.psys->part;
142         struct Material *ma = ctx->ma;
143         const bool draw_col_ma = (part->draw_col == PART_DRAW_COL_MAT);
144         
145         const int totkeys = ctx->steps + 1;
146         const float step_length = 1.0f / (float)ctx->steps;
147         const float max_length = ptex->length;
148         
149         ParticlePathModifier *mod;
150         ParticleCacheKey *key;
151         int k;
152         float cur_length;
153         
154 #if 0 /* TODO for the future: use true particle modifiers that work on the whole curve */
155         for (mod = modifiers->first; mod; mod = mod->next) {
156                 mod->apply(keys, totkeys, parent_keys);
157         }
158 #else
159         (void)modifiers;
160         (void)mod;
161         
162         {
163                 ParticlePathIterator iter;
164                 for (k = 0, key = keys; k < totkeys; k++, key++) {
165                         psys_path_iter_get(&iter, keys, totkeys, parent_keys, k);
166                         
167                         /* apply different deformations to the child path */
168                         do_child_modifiers(&ctx->sim, ptex, (ParticleKey *)iter.parent_key, iter.parent_rotation, cpa, orco, hairmat, (ParticleKey *)key, iter.time);
169                 }
170         }
171 #endif
172         
173         cur_length = 0.0f;
174         /* we have to correct velocity because of kink & clump */
175         for (k = 0, key = keys; k < totkeys; ++k, ++key) {
176                 if (k >= 2) {
177                         sub_v3_v3v3((key-1)->vel, key->co, (key-2)->co);
178                         mul_v3_fl((key-1)->vel, 0.5);
179                         
180                         if (ma && draw_col_ma)
181                                 get_strand_normal(ma, ornor, cur_length, (key-1)->vel);
182                 }
183                 else if (k == totkeys-1) {
184                         /* last key */
185                         sub_v3_v3v3(key->vel, key->co, (key-1)->co);
186                 }
187                 
188                 if (k > 1) {
189                         float dvec[3];
190                         /* check if path needs to be cut before actual end of data points */
191                         if (!check_path_length(k, keys, key, max_length, step_length, &cur_length, dvec))
192                                 break;
193                 }
194                 
195                 if (ma && draw_col_ma) {
196                         copy_v3_v3(key->col, &ma->r);
197                         get_strand_normal(ma, ornor, cur_length, key->vel);
198                 }
199         }
200 }
201
202 /* ------------------------------------------------------------------------- */
203
204 void do_kink(ParticleKey *state, ParticleKey *par, float *par_rot, float time, float freq, float shape, float amplitude, float flat, short type, short axis, float obmat[4][4], int smooth_start)
205 {
206         float kink[3] = {1.f, 0.f, 0.f}, par_vec[3], q1[4] = {1.f, 0.f, 0.f, 0.f};
207         float t, dt = 1.f, result[3];
208
209         if (par == NULL || type == PART_KINK_NO)
210                 return;
211
212         CLAMP(time, 0.f, 1.f);
213
214         if (shape != 0.0f && type != PART_KINK_BRAID) {
215                 if (shape < 0.0f)
216                         time = (float)pow(time, 1.f + shape);
217                 else
218                         time = (float)pow(time, 1.f / (1.f - shape));
219         }
220
221         t = time * freq * (float)M_PI;
222         
223         if (smooth_start) {
224                 dt = fabsf(t);
225                 /* smooth the beginning of kink */
226                 CLAMP(dt, 0.f, (float)M_PI);
227                 dt = sinf(dt / 2.f);
228         }
229
230         if (type != PART_KINK_RADIAL) {
231                 float temp[3];
232
233                 kink[axis] = 1.f;
234
235                 if (obmat)
236                         mul_mat3_m4_v3(obmat, kink);
237                 
238                 if (par_rot)
239                         mul_qt_v3(par_rot, kink);
240
241                 /* make sure kink is normal to strand */
242                 project_v3_v3v3(temp, kink, par->vel);
243                 sub_v3_v3(kink, temp);
244                 normalize_v3(kink);
245         }
246
247         copy_v3_v3(result, state->co);
248         sub_v3_v3v3(par_vec, par->co, state->co);
249
250         switch (type) {
251                 case PART_KINK_CURL:
252                 {
253                         float curl_offset[3];
254                         
255                         /* rotate kink vector around strand tangent */
256                         mul_v3_v3fl(curl_offset, kink, amplitude);
257                         axis_angle_to_quat(q1, par->vel, t);
258                         mul_qt_v3(q1, curl_offset);
259                         
260                         interp_v3_v3v3(par_vec, state->co, par->co, flat);
261                         add_v3_v3v3(result, par_vec, curl_offset);
262                         break;
263                 }
264                 case PART_KINK_RADIAL:
265                 {
266                         if (flat > 0.f) {
267                                 float proj[3];
268                                 /* flatten along strand */
269                                 project_v3_v3v3(proj, par_vec, par->vel);
270                                 madd_v3_v3fl(result, proj, flat);
271                         }
272
273                         madd_v3_v3fl(result, par_vec, -amplitude * sinf(t));
274                         break;
275                 }
276                 case PART_KINK_WAVE:
277                 {
278                         madd_v3_v3fl(result, kink, amplitude * sinf(t));
279
280                         if (flat > 0.f) {
281                                 float proj[3];
282                                 /* flatten along wave */
283                                 project_v3_v3v3(proj, par_vec, kink);
284                                 madd_v3_v3fl(result, proj, flat);
285
286                                 /* flatten along strand */
287                                 project_v3_v3v3(proj, par_vec, par->vel);
288                                 madd_v3_v3fl(result, proj, flat);
289                         }
290                         break;
291                 }
292                 case PART_KINK_BRAID:
293                 {
294                         float y_vec[3] = {0.f, 1.f, 0.f};
295                         float z_vec[3] = {0.f, 0.f, 1.f};
296                         float vec_one[3], state_co[3];
297                         float inp_y, inp_z, length;
298                 
299                         if (par_rot) {
300                                 mul_qt_v3(par_rot, y_vec);
301                                 mul_qt_v3(par_rot, z_vec);
302                         }
303
304                         negate_v3(par_vec);
305                         normalize_v3_v3(vec_one, par_vec);
306
307                         inp_y = dot_v3v3(y_vec, vec_one);
308                         inp_z = dot_v3v3(z_vec, vec_one);
309
310                         if (inp_y > 0.5f) {
311                                 copy_v3_v3(state_co, y_vec);
312
313                                 mul_v3_fl(y_vec, amplitude * cosf(t));
314                                 mul_v3_fl(z_vec, amplitude / 2.f * sinf(2.f * t));
315                         }
316                         else if (inp_z > 0.0f) {
317                                 mul_v3_v3fl(state_co, z_vec, sinf((float)M_PI / 3.f));
318                                 madd_v3_v3fl(state_co, y_vec, -0.5f);
319
320                                 mul_v3_fl(y_vec, -amplitude * cosf(t + (float)M_PI / 3.f));
321                                 mul_v3_fl(z_vec, amplitude / 2.f * cosf(2.f * t + (float)M_PI / 6.f));
322                         }
323                         else {
324                                 mul_v3_v3fl(state_co, z_vec, -sinf((float)M_PI / 3.f));
325                                 madd_v3_v3fl(state_co, y_vec, -0.5f);
326
327                                 mul_v3_fl(y_vec, amplitude * -sinf(t + (float)M_PI / 6.f));
328                                 mul_v3_fl(z_vec, amplitude / 2.f * -sinf(2.f * t + (float)M_PI / 3.f));
329                         }
330
331                         mul_v3_fl(state_co, amplitude);
332                         add_v3_v3(state_co, par->co);
333                         sub_v3_v3v3(par_vec, state->co, state_co);
334
335                         length = normalize_v3(par_vec);
336                         mul_v3_fl(par_vec, MIN2(length, amplitude / 2.f));
337
338                         add_v3_v3v3(state_co, par->co, y_vec);
339                         add_v3_v3(state_co, z_vec);
340                         add_v3_v3(state_co, par_vec);
341
342                         shape = 2.f * (float)M_PI * (1.f + shape);
343
344                         if (t < shape) {
345                                 shape = t / shape;
346                                 shape = (float)sqrt((double)shape);
347                                 interp_v3_v3v3(result, result, state_co, shape);
348                         }
349                         else {
350                                 copy_v3_v3(result, state_co);
351                         }
352                         break;
353                 }
354         }
355
356         /* blend the start of the kink */
357         if (dt < 1.f)
358                 interp_v3_v3v3(state->co, state->co, result, dt);
359         else
360                 copy_v3_v3(state->co, result);
361 }
362
363 float do_clump(ParticleKey *state, ParticleKey *par, float time, float clumpfac, float clumppow, float pa_clump, CurveMapping *clumpcurve)
364 {
365         float clump = 0.f;
366
367         if (!par)
368                 return 0.0f;
369         
370         if (clumpcurve) {
371                 clump = pa_clump * (1.0f - CLAMPIS(curvemapping_evaluateF(clumpcurve, 0, time), 0.0f, 1.0f));
372                 
373                 interp_v3_v3v3(state->co, state->co, par->co, clump);
374         }
375         else if (clumpfac != 0.0f) {
376                 float cpow;
377
378                 if (clumppow < 0.0f)
379                         cpow = 1.0f + clumppow;
380                 else
381                         cpow = 1.0f + 9.0f * clumppow;
382
383                 if (clumpfac < 0.0f) /* clump roots instead of tips */
384                         clump = -clumpfac * pa_clump * (float)pow(1.0 - (double)time, (double)cpow);
385                 else
386                         clump = clumpfac * pa_clump * (float)pow((double)time, (double)cpow);
387
388                 interp_v3_v3v3(state->co, state->co, par->co, clump);
389         }
390
391         return clump;
392 }
393
394 static void do_rough(const float loc[3], float mat[4][4], float t, float fac, float size, float thres, ParticleKey *state)
395 {
396         float rough[3];
397         float rco[3];
398
399         if (thres != 0.0f) {
400                 if (fabsf((float)(-1.5f + loc[0] + loc[1] + loc[2])) < 1.5f * thres) {
401                         return;
402                 }
403         }
404
405         copy_v3_v3(rco, loc);
406         mul_v3_fl(rco, t);
407         rough[0] = -1.0f + 2.0f * BLI_gTurbulence(size, rco[0], rco[1], rco[2], 2, 0, 2);
408         rough[1] = -1.0f + 2.0f * BLI_gTurbulence(size, rco[1], rco[2], rco[0], 2, 0, 2);
409         rough[2] = -1.0f + 2.0f * BLI_gTurbulence(size, rco[2], rco[0], rco[1], 2, 0, 2);
410
411         madd_v3_v3fl(state->co, mat[0], fac * rough[0]);
412         madd_v3_v3fl(state->co, mat[1], fac * rough[1]);
413         madd_v3_v3fl(state->co, mat[2], fac * rough[2]);
414 }
415
416 static void do_rough_end(const float loc[3], float mat[4][4], float t, float fac, float shape, ParticleKey *state)
417 {
418         float rough[2];
419         float roughfac;
420
421         roughfac = fac * (float)pow((double)t, shape);
422         copy_v2_v2(rough, loc);
423         rough[0] = -1.0f + 2.0f * rough[0];
424         rough[1] = -1.0f + 2.0f * rough[1];
425         mul_v2_fl(rough, roughfac);
426
427         madd_v3_v3fl(state->co, mat[0], rough[0]);
428         madd_v3_v3fl(state->co, mat[1], rough[1]);
429 }
430
431 static void do_rough_curve(const float loc[3], float mat[4][4], float time, float fac, float size, CurveMapping *roughcurve, ParticleKey *state)
432 {
433         float rough[3];
434         float rco[3];
435         
436         if (!roughcurve)
437                 return;
438         
439         fac *= CLAMPIS(curvemapping_evaluateF(roughcurve, 0, time), 0.0f, 1.0f);
440         
441         copy_v3_v3(rco, loc);
442         mul_v3_fl(rco, time);
443         rough[0] = -1.0f + 2.0f * BLI_gTurbulence(size, rco[0], rco[1], rco[2], 2, 0, 2);
444         rough[1] = -1.0f + 2.0f * BLI_gTurbulence(size, rco[1], rco[2], rco[0], 2, 0, 2);
445         rough[2] = -1.0f + 2.0f * BLI_gTurbulence(size, rco[2], rco[0], rco[1], 2, 0, 2);
446         
447         madd_v3_v3fl(state->co, mat[0], fac * rough[0]);
448         madd_v3_v3fl(state->co, mat[1], fac * rough[1]);
449         madd_v3_v3fl(state->co, mat[2], fac * rough[2]);
450 }
451
452 void do_child_modifiers(ParticleSimulationData *sim, ParticleTexture *ptex, ParticleKey *par, float *par_rot, ChildParticle *cpa, const float orco[3], float mat[4][4], ParticleKey *state, float t)
453 {
454         ParticleSettings *part = sim->psys->part;
455         int i = cpa - sim->psys->child;
456         int guided = 0;
457
458         float kink_freq = part->kink_freq;
459         float rough1 = part->rough1;
460         float rough2 = part->rough2;
461         float rough_end = part->rough_end;
462
463         if (ptex) {
464                 kink_freq *= ptex->kink;
465                 rough1 *= ptex->rough1;
466                 rough2 *= ptex->rough2;
467                 rough_end *= ptex->roughe;
468         }
469
470         if (part->flag & PART_CHILD_EFFECT)
471                 /* state is safe to cast, since only co and vel are used */
472                 guided = do_guides(sim->psys->part, sim->psys->effectors, (ParticleKey *)state, cpa->parent, t);
473
474         if (guided == 0) {
475                 float clump = do_clump(state, par, t, part->clumpfac, part->clumppow, ptex ? ptex->clump : 1.f, part->clumpcurve);
476
477                 if (kink_freq != 0.f) {
478                         float kink_amp = part->kink_amp * (1.f - part->kink_amp_clump * clump);
479
480                         do_kink(state, par, par_rot, t, kink_freq, part->kink_shape,
481                                 kink_amp, part->kink_flat, part->kink, part->kink_axis,
482                                 sim->ob->obmat, sim->psys->part->childtype == PART_CHILD_FACES);
483                 }
484         }
485
486         if (part->roughcurve) {
487                 do_rough_curve(orco, mat, t, rough1, part->rough1_size, part->roughcurve, state);
488         }
489         else {
490                 if (rough1 > 0.f)
491                         do_rough(orco, mat, t, rough1, part->rough1_size, 0.0, state);
492         
493                 if (rough2 > 0.f) {
494                         float vec[3];
495                         psys_frand_vec(sim->psys, i + 27, vec);
496                         do_rough(vec, mat, t, rough2, part->rough2_size, part->rough2_thres, state);
497                 }
498         
499                 if (rough_end > 0.f) {
500                         float vec[3];
501                         psys_frand_vec(sim->psys, i + 27, vec);
502                         do_rough_end(vec, mat, t, rough_end, part->rough_end_shape, state);
503                 }
504         }
505 }