1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 *
16 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17 * All rights reserved.
18 */
19
20 /** \file
21 * \ingroup bke
22 */
23
24 #include <limits.h>
25 #include <stddef.h>
26 #include <stdlib.h>
27
28 #include "MEM_guardedalloc.h"
29
30 #include "BLI_listbase.h"
31 #include "BLI_string_utf8.h"
32
33 #include "BLI_alloca.h"
34 #include "BLI_math.h"
35 #include "BLI_rand.h"
36
37 #include "DNA_anim_types.h"
38 #include "DNA_collection_types.h"
39 #include "DNA_mesh_types.h"
40 #include "DNA_meshdata_types.h"
41 #include "DNA_pointcloud_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_vfont_types.h"
44
45 #include "BKE_collection.h"
46 #include "BKE_duplilist.h"
47 #include "BKE_editmesh.h"
48 #include "BKE_editmesh_cache.h"
49 #include "BKE_font.h"
50 #include "BKE_global.h"
51 #include "BKE_idprop.h"
52 #include "BKE_lattice.h"
53 #include "BKE_main.h"
54 #include "BKE_mesh.h"
55 #include "BKE_mesh_iterators.h"
56 #include "BKE_mesh_runtime.h"
57 #include "BKE_object.h"
58 #include "BKE_particle.h"
59 #include "BKE_scene.h"
60
61 #include "DEG_depsgraph.h"
62 #include "DEG_depsgraph_query.h"
63
64 #include "BLI_hash.h"
65 #include "BLI_strict_flags.h"
66
67 /* -------------------------------------------------------------------- */
68 /** \name Internal Duplicate Context
69 * \{ */
70
71 typedef struct DupliContext {
72 Depsgraph *depsgraph;
73 /** XXX child objects are selected from this group if set, could be nicer. */
74 Collection *collection;
75 /** Only to check if the object is in edit-mode. */
76 Object *obedit;
77
78 Scene *scene;
79 ViewLayer *view_layer;
80 Object *object;
81 float space_mat[4][4];
82
83 int persistent_id[MAX_DUPLI_RECUR];
84 int level;
85
86 const struct DupliGenerator *gen;
87
88 /** Result containers. */
89 ListBase *duplilist; /* Legacy doubly-linked list. */
90 } DupliContext;
91
92 typedef struct DupliGenerator {
93 short type; /* Dupli Type, see members of #OB_DUPLI. */
94 void (*make_duplis)(const DupliContext *ctx);
95 } DupliGenerator;
96
97 static const DupliGenerator *get_dupli_generator(const DupliContext *ctx);
98
99 /**
100 * Create initial context for root object.
101 */
init_context(DupliContext * r_ctx,Depsgraph * depsgraph,Scene * scene,Object * ob,const float space_mat[4][4])102 static void init_context(DupliContext *r_ctx,
103 Depsgraph *depsgraph,
104 Scene *scene,
105 Object *ob,
106 const float space_mat[4][4])
107 {
108 r_ctx->depsgraph = depsgraph;
109 r_ctx->scene = scene;
110 r_ctx->view_layer = DEG_get_evaluated_view_layer(depsgraph);
111 r_ctx->collection = NULL;
112
113 r_ctx->object = ob;
114 r_ctx->obedit = OBEDIT_FROM_OBACT(ob);
115 if (space_mat) {
116 copy_m4_m4(r_ctx->space_mat, space_mat);
117 }
118 else {
119 unit_m4(r_ctx->space_mat);
120 }
121 r_ctx->level = 0;
122
123 r_ctx->gen = get_dupli_generator(r_ctx);
124
125 r_ctx->duplilist = NULL;
126 }
127
128 /**
129 * Create sub-context for recursive duplis.
130 */
copy_dupli_context(DupliContext * r_ctx,const DupliContext * ctx,Object * ob,const float mat[4][4],int index)131 static void copy_dupli_context(
132 DupliContext *r_ctx, const DupliContext *ctx, Object *ob, const float mat[4][4], int index)
133 {
134 *r_ctx = *ctx;
135
136 /* XXX annoying, previously was done by passing an ID* argument,
137 * this at least is more explicit. */
138 if (ctx->gen->type == OB_DUPLICOLLECTION) {
139 r_ctx->collection = ctx->object->instance_collection;
140 }
141
142 r_ctx->object = ob;
143 if (mat) {
144 mul_m4_m4m4(r_ctx->space_mat, (float(*)[4])ctx->space_mat, mat);
145 }
146 r_ctx->persistent_id[r_ctx->level] = index;
147 ++r_ctx->level;
148
149 r_ctx->gen = get_dupli_generator(r_ctx);
150 }
151
152 /**
153 * Generate a dupli instance.
154 *
155 * \param mat: is transform of the object relative to current context (including #Object.obmat).
156 */
make_dupli(const DupliContext * ctx,Object * ob,const float mat[4][4],int index)157 static DupliObject *make_dupli(const DupliContext *ctx,
158 Object *ob,
159 const float mat[4][4],
160 int index)
161 {
162 DupliObject *dob;
163 int i;
164
165 /* Add a #DupliObject instance to the result container. */
166 if (ctx->duplilist) {
167 dob = MEM_callocN(sizeof(DupliObject), "dupli object");
168 BLI_addtail(ctx->duplilist, dob);
169 }
170 else {
171 return NULL;
172 }
173
174 dob->ob = ob;
175 mul_m4_m4m4(dob->mat, (float(*)[4])ctx->space_mat, mat);
176 dob->type = ctx->gen->type;
177
178 /* Set persistent id, which is an array with a persistent index for each level
179 * (particle number, vertex number, ..). by comparing this we can find the same
180 * dupli-object between frames, which is needed for motion blur.
181 * The last level is ordered first in the array. */
182 dob->persistent_id[0] = index;
183 for (i = 1; i < ctx->level + 1; i++) {
184 dob->persistent_id[i] = ctx->persistent_id[ctx->level - i];
185 }
186 /* Fill rest of values with #INT_MAX which index will never have as value. */
187 for (; i < MAX_DUPLI_RECUR; i++) {
188 dob->persistent_id[i] = INT_MAX;
189 }
190
191 /* Meta-balls never draw in duplis, they are instead merged into one by the basis
192 * meta-ball outside of the group. this does mean that if that meta-ball is not in the
193 * scene, they will not show up at all, limitation that should be solved once. */
194 if (ob->type == OB_MBALL) {
195 dob->no_draw = true;
196 }
197
198 /* Random number.
199 * The logic here is designed to match Cycles. */
200 dob->random_id = BLI_hash_string(dob->ob->id.name + 2);
201
202 if (dob->persistent_id[0] != INT_MAX) {
203 for (i = 0; i < MAX_DUPLI_RECUR; i++) {
204 dob->random_id = BLI_hash_int_2d(dob->random_id, (unsigned int)dob->persistent_id[i]);
205 }
206 }
207 else {
208 dob->random_id = BLI_hash_int_2d(dob->random_id, 0);
209 }
210
211 if (ctx->object != ob) {
212 dob->random_id ^= BLI_hash_int(BLI_hash_string(ctx->object->id.name + 2));
213 }
214
215 return dob;
216 }
217
218 /**
219 * Recursive dupli-objects.
220 *
221 * \param space_mat: is the local dupli-space (excluding dupli #Object.obmat).
222 */
make_recursive_duplis(const DupliContext * ctx,Object * ob,const float space_mat[4][4],int index)223 static void make_recursive_duplis(const DupliContext *ctx,
224 Object *ob,
225 const float space_mat[4][4],
226 int index)
227 {
228 /* Simple preventing of too deep nested collections with #MAX_DUPLI_RECUR. */
229 if (ctx->level < MAX_DUPLI_RECUR) {
230 DupliContext rctx;
231 copy_dupli_context(&rctx, ctx, ob, space_mat, index);
232 if (rctx.gen) {
233 rctx.gen->make_duplis(&rctx);
234 }
235 }
236 }
237
238 /** \} */
239
240 /* -------------------------------------------------------------------- */
241 /** \name Internal Child Duplicates (Used by Other Functions)
242 * \{ */
243
244 typedef void (*MakeChildDuplisFunc)(const DupliContext *ctx, void *userdata, Object *child);
245
is_child(const Object * ob,const Object * parent)246 static bool is_child(const Object *ob, const Object *parent)
247 {
248 const Object *ob_parent = ob->parent;
249 while (ob_parent) {
250 if (ob_parent == parent) {
251 return true;
252 }
253 ob_parent = ob_parent->parent;
254 }
255 return false;
256 }
257
258 /**
259 * Create duplis from every child in scene or collection.
260 */
make_child_duplis(const DupliContext * ctx,void * userdata,MakeChildDuplisFunc make_child_duplis_cb)261 static void make_child_duplis(const DupliContext *ctx,
262 void *userdata,
263 MakeChildDuplisFunc make_child_duplis_cb)
264 {
265 Object *parent = ctx->object;
266
267 if (ctx->collection) {
268 eEvaluationMode mode = DEG_get_mode(ctx->depsgraph);
269 FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_BEGIN (ctx->collection, ob, mode) {
270 if ((ob != ctx->obedit) && is_child(ob, parent)) {
271 DupliContext pctx;
272 copy_dupli_context(&pctx, ctx, ctx->object, NULL, _base_id);
273
274 /* Meta-balls have a different dupli handling. */
275 if (ob->type != OB_MBALL) {
276 ob->flag |= OB_DONE; /* Doesn't render. */
277 }
278 make_child_duplis_cb(&pctx, userdata, ob);
279 }
280 }
281 FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_END;
282 }
283 else {
284 int baseid = 0;
285 ViewLayer *view_layer = ctx->view_layer;
286 for (Base *base = view_layer->object_bases.first; base; base = base->next, baseid++) {
287 Object *ob = base->object;
288 if ((ob != ctx->obedit) && is_child(ob, parent)) {
289 DupliContext pctx;
290 copy_dupli_context(&pctx, ctx, ctx->object, NULL, baseid);
291
292 /* Meta-balls have a different dupli-handling. */
293 if (ob->type != OB_MBALL) {
294 ob->flag |= OB_DONE; /* Doesn't render. */
295 }
296
297 make_child_duplis_cb(&pctx, userdata, ob);
298 }
299 }
300 }
301 }
302
303 /** \} */
304
305 /* -------------------------------------------------------------------- */
306 /** \name Internal Data Access Utilities
307 * \{ */
308
mesh_data_from_duplicator_object(Object * ob,BMEditMesh ** r_em,const float (** r_vert_coords)[3],const float (** r_vert_normals)[3])309 static Mesh *mesh_data_from_duplicator_object(Object *ob,
310 BMEditMesh **r_em,
311 const float (**r_vert_coords)[3],
312 const float (**r_vert_normals)[3])
313 {
314 /* Gather mesh info. */
315 BMEditMesh *em = BKE_editmesh_from_object(ob);
316 Mesh *me_eval;
317
318 *r_em = NULL;
319 *r_vert_coords = NULL;
320 if (r_vert_normals != NULL) {
321 *r_vert_normals = NULL;
322 }
323
324 /* We do not need any render-specific handling anymore, depsgraph takes care of that. */
325 /* NOTE: Do direct access to the evaluated mesh: this function is used
326 * during meta balls evaluation. But even without those all the objects
327 * which are needed for correct instancing are already evaluated. */
328 if (em != NULL) {
329 /* Note that this will only show deformation if #eModifierMode_OnCage is enabled.
330 * We could change this but it matches 2.7x behavior. */
331 me_eval = em->mesh_eval_cage;
332 if ((me_eval == NULL) || (me_eval->runtime.wrapper_type == ME_WRAPPER_TYPE_BMESH)) {
333 EditMeshData *emd = me_eval ? me_eval->runtime.edit_data : NULL;
334
335 /* Only assign edit-mesh in the case we can't use `me_eval`. */
336 *r_em = em;
337 me_eval = NULL;
338
339 if ((emd != NULL) && (emd->vertexCos != NULL)) {
340 *r_vert_coords = emd->vertexCos;
341 if (r_vert_normals != NULL) {
342 BKE_editmesh_cache_ensure_vert_normals(em, emd);
343 *r_vert_normals = emd->vertexNos;
344 }
345 }
346 }
347 }
348 else {
349 me_eval = BKE_object_get_evaluated_mesh(ob);
350 }
351 return me_eval;
352 }
353
354 /** \} */
355
356 /* -------------------------------------------------------------------- */
357 /** \name Dupli-Collection Implementation (#OB_DUPLICOLLECTION)
358 * \{ */
359
make_duplis_collection(const DupliContext * ctx)360 static void make_duplis_collection(const DupliContext *ctx)
361 {
362 Object *ob = ctx->object;
363 Collection *collection;
364 float collection_mat[4][4];
365
366 if (ob->instance_collection == NULL) {
367 return;
368 }
369 collection = ob->instance_collection;
370
371 /* Combine collection offset and `obmat`. */
372 unit_m4(collection_mat);
373 sub_v3_v3(collection_mat[3], collection->instance_offset);
374 mul_m4_m4m4(collection_mat, ob->obmat, collection_mat);
375 /* Don't access 'ob->obmat' from now on. */
376
377 eEvaluationMode mode = DEG_get_mode(ctx->depsgraph);
378 FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_BEGIN (collection, cob, mode) {
379 if (cob != ob) {
380 float mat[4][4];
381
382 /* Collection dupli-offset, should apply after everything else. */
383 mul_m4_m4m4(mat, collection_mat, cob->obmat);
384
385 make_dupli(ctx, cob, mat, _base_id);
386
387 /* Recursion. */
388 make_recursive_duplis(ctx, cob, collection_mat, _base_id);
389 }
390 }
391 FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_END;
392 }
393
394 static const DupliGenerator gen_dupli_collection = {
395 OB_DUPLICOLLECTION, /* type */
396 make_duplis_collection /* make_duplis */
397 };
398
399 /** \} */
400
401 /* -------------------------------------------------------------------- */
402 /** \name Dupli-Vertices Implementation (#OB_DUPLIVERTS for Geometry)
403 * \{ */
404
405 /** Values shared between different mesh types. */
406 typedef struct VertexDupliData_Params {
407 /**
408 * It's important we use this context instead of the `ctx` passed into #make_child_duplis
409 * since these won't match in the case of recursion.
410 */
411 const DupliContext *ctx;
412
413 bool use_rotation;
414 } VertexDupliData_Params;
415
416 typedef struct VertexDupliData_Mesh {
417 VertexDupliData_Params params;
418
419 int totvert;
420 const MVert *mvert;
421
422 const float (*orco)[3];
423 } VertexDupliData_Mesh;
424
425 typedef struct VertexDupliData_EditMesh {
426 VertexDupliData_Params params;
427
428 BMEditMesh *em;
429
430 /* Can be NULL. */
431 const float (*vert_coords)[3];
432 const float (*vert_normals)[3];
433
434 /**
435 * \note The edit-mesh may assign #DupliObject.orco in cases when a regular mesh wouldn't.
436 * For edit-meshes we only check for deformation, for regular meshes we check if #CD_ORCO exists.
437 *
438 * At the moment this isn't a meaningful difference since requesting #CD_ORCO causes the
439 * edit-mesh to be converted into a mesh.
440 */
441 bool has_orco;
442 } VertexDupliData_EditMesh;
443
444 /**
445 * \param no: The direction,
446 * currently this is copied from a `short[3]` normal without division.
447 * Can be null when \a use_rotation is false.
448 */
get_duplivert_transform(const float co[3],const float no[3],const bool use_rotation,const short axis,const short upflag,float r_mat[4][4])449 static void get_duplivert_transform(const float co[3],
450 const float no[3],
451 const bool use_rotation,
452 const short axis,
453 const short upflag,
454 float r_mat[4][4])
455 {
456 float quat[4];
457 const float size[3] = {1.0f, 1.0f, 1.0f};
458
459 if (use_rotation) {
460 /* Construct rotation matrix from normals. */
461 float no_flip[3];
462 negate_v3_v3(no_flip, no);
463 vec_to_quat(quat, no_flip, axis, upflag);
464 }
465 else {
466 unit_qt(quat);
467 }
468
469 loc_quat_size_to_mat4(r_mat, co, quat, size);
470 }
471
vertex_dupli(const DupliContext * ctx,Object * inst_ob,const float child_imat[4][4],int index,const float co[3],const float no[3],const bool use_rotation)472 static DupliObject *vertex_dupli(const DupliContext *ctx,
473 Object *inst_ob,
474 const float child_imat[4][4],
475 int index,
476 const float co[3],
477 const float no[3],
478 const bool use_rotation)
479 {
480 /* `obmat` is transform to vertex. */
481 float obmat[4][4];
482 get_duplivert_transform(co, no, use_rotation, inst_ob->trackflag, inst_ob->upflag, obmat);
483
484 float space_mat[4][4];
485
486 /* Make offset relative to inst_ob using relative child transform. */
487 mul_mat3_m4_v3(child_imat, obmat[3]);
488 /* Apply `obmat` _after_ the local vertex transform. */
489 mul_m4_m4m4(obmat, inst_ob->obmat, obmat);
490
491 /* Space matrix is constructed by removing `obmat` transform,
492 * this yields the world-space transform for recursive duplis. */
493 mul_m4_m4m4(space_mat, obmat, inst_ob->imat);
494
495 DupliObject *dob = make_dupli(ctx, inst_ob, obmat, index);
496
497 /* Recursion. */
498 make_recursive_duplis(ctx, inst_ob, space_mat, index);
499
500 return dob;
501 }
502
make_child_duplis_verts_from_mesh(const DupliContext * ctx,void * userdata,Object * inst_ob)503 static void make_child_duplis_verts_from_mesh(const DupliContext *ctx,
504 void *userdata,
505 Object *inst_ob)
506 {
507 VertexDupliData_Mesh *vdd = userdata;
508 const bool use_rotation = vdd->params.use_rotation;
509
510 const MVert *mvert = vdd->mvert;
511 const int totvert = vdd->totvert;
512
513 invert_m4_m4(inst_ob->imat, inst_ob->obmat);
514 /* Relative transform from parent to child space. */
515 float child_imat[4][4];
516 mul_m4_m4m4(child_imat, inst_ob->imat, ctx->object->obmat);
517
518 const MVert *mv = mvert;
519 for (int i = 0; i < totvert; i++, mv++) {
520 const float *co = mv->co;
521 const float no[3] = {UNPACK3(mv->no)};
522 DupliObject *dob = vertex_dupli(vdd->params.ctx, inst_ob, child_imat, i, co, no, use_rotation);
523 if (vdd->orco) {
524 copy_v3_v3(dob->orco, vdd->orco[i]);
525 }
526 }
527 }
528
make_child_duplis_verts_from_editmesh(const DupliContext * ctx,void * userdata,Object * inst_ob)529 static void make_child_duplis_verts_from_editmesh(const DupliContext *ctx,
530 void *userdata,
531 Object *inst_ob)
532 {
533 VertexDupliData_EditMesh *vdd = userdata;
534 BMEditMesh *em = vdd->em;
535 const bool use_rotation = vdd->params.use_rotation;
536
537 invert_m4_m4(inst_ob->imat, inst_ob->obmat);
538 /* Relative transform from parent to child space. */
539 float child_imat[4][4];
540 mul_m4_m4m4(child_imat, inst_ob->imat, ctx->object->obmat);
541
542 BMVert *v;
543 BMIter iter;
544 int i;
545
546 const float(*vert_coords)[3] = vdd->vert_coords;
547 const float(*vert_normals)[3] = vdd->vert_normals;
548
549 BM_ITER_MESH_INDEX (v, &iter, em->bm, BM_VERTS_OF_MESH, i) {
550 const float *co, *no;
551 if (vert_coords != NULL) {
552 co = vert_coords[i];
553 no = vert_normals ? vert_normals[i] : NULL;
554 }
555 else {
556 co = v->co;
557 no = v->no;
558 }
559
560 DupliObject *dob = vertex_dupli(vdd->params.ctx, inst_ob, child_imat, i, co, no, use_rotation);
561 if (vdd->has_orco) {
562 copy_v3_v3(dob->orco, v->co);
563 }
564 }
565 }
566
make_duplis_verts(const DupliContext * ctx)567 static void make_duplis_verts(const DupliContext *ctx)
568 {
569 Object *parent = ctx->object;
570 const bool use_rotation = parent->transflag & OB_DUPLIROT;
571
572 /* Gather mesh info. */
573 BMEditMesh *em = NULL;
574 const float(*vert_coords)[3] = NULL;
575 const float(*vert_normals)[3] = NULL;
576 Mesh *me_eval = mesh_data_from_duplicator_object(
577 parent, &em, &vert_coords, use_rotation ? &vert_normals : NULL);
578 if (em == NULL && me_eval == NULL) {
579 return;
580 }
581
582 VertexDupliData_Params vdd_params = {
583 .ctx = ctx,
584 .use_rotation = use_rotation,
585 };
586
587 if (em != NULL) {
588 VertexDupliData_EditMesh vdd = {
589 .params = vdd_params,
590 .em = em,
591 .vert_coords = vert_coords,
592 .vert_normals = vert_normals,
593 .has_orco = (vert_coords != NULL),
594 };
595 make_child_duplis(ctx, &vdd, make_child_duplis_verts_from_editmesh);
596 }
597 else {
598 VertexDupliData_Mesh vdd = {
599 .params = vdd_params,
600 .totvert = me_eval->totvert,
601 .mvert = me_eval->mvert,
602 .orco = CustomData_get_layer(&me_eval->vdata, CD_ORCO),
603 };
604 make_child_duplis(ctx, &vdd, make_child_duplis_verts_from_mesh);
605 }
606 }
607
608 static const DupliGenerator gen_dupli_verts = {
609 OB_DUPLIVERTS, /* type */
610 make_duplis_verts /* make_duplis */
611 };
612
613 /** \} */
614
615 /* -------------------------------------------------------------------- */
616 /** \name Dupli-Vertices Implementation (#OB_DUPLIVERTS for 3D Text)
617 * \{ */
618
find_family_object(Main * bmain,const char * family,size_t family_len,unsigned int ch,GHash * family_gh)619 static Object *find_family_object(
620 Main *bmain, const char *family, size_t family_len, unsigned int ch, GHash *family_gh)
621 {
622 Object **ob_pt;
623 Object *ob;
624 void *ch_key = POINTER_FROM_UINT(ch);
625
626 if ((ob_pt = (Object **)BLI_ghash_lookup_p(family_gh, ch_key))) {
627 ob = *ob_pt;
628 }
629 else {
630 char ch_utf8[7];
631 size_t ch_utf8_len;
632
633 ch_utf8_len = BLI_str_utf8_from_unicode(ch, ch_utf8);
634 ch_utf8[ch_utf8_len] = '\0';
635 ch_utf8_len += 1; /* Compare with null terminator. */
636
637 for (ob = bmain->objects.first; ob; ob = ob->id.next) {
638 if (STREQLEN(ob->id.name + 2 + family_len, ch_utf8, ch_utf8_len)) {
639 if (STREQLEN(ob->id.name + 2, family, family_len)) {
640 break;
641 }
642 }
643 }
644
645 /* Inserted value can be NULL, just to save searches in future. */
646 BLI_ghash_insert(family_gh, ch_key, ob);
647 }
648
649 return ob;
650 }
651
make_duplis_font(const DupliContext * ctx)652 static void make_duplis_font(const DupliContext *ctx)
653 {
654 Object *par = ctx->object;
655 GHash *family_gh;
656 Object *ob;
657 Curve *cu;
658 struct CharTrans *ct, *chartransdata = NULL;
659 float vec[3], obmat[4][4], pmat[4][4], fsize, xof, yof;
660 int text_len, a;
661 size_t family_len;
662 const char32_t *text = NULL;
663 bool text_free = false;
664
665 /* Font dupli-verts not supported inside collections. */
666 if (ctx->collection) {
667 return;
668 }
669
670 copy_m4_m4(pmat, par->obmat);
671
672 /* In `par` the family name is stored, use this to find the other objects. */
673
674 BKE_vfont_to_curve_ex(
675 par, par->data, FO_DUPLI, NULL, &text, &text_len, &text_free, &chartransdata);
676
677 if (text == NULL || chartransdata == NULL) {
678 return;
679 }
680
681 cu = par->data;
682 fsize = cu->fsize;
683 xof = cu->xof;
684 yof = cu->yof;
685
686 ct = chartransdata;
687
688 /* Cache result. */
689 family_len = strlen(cu->family);
690 family_gh = BLI_ghash_int_new_ex(__func__, 256);
691
692 /* Safety check even if it might fail badly when called for original object. */
693 const bool is_eval_curve = DEG_is_evaluated_id(&cu->id);
694
695 /* Advance matching BLI_str_utf8_as_utf32. */
696 for (a = 0; a < text_len; a++, ct++) {
697
698 /* XXX That G.main is *really* ugly, but not sure what to do here.
699 * Definitively don't think it would be safe to put back `Main *bmain` pointer
700 * in #DupliContext as done in 2.7x? */
701 ob = find_family_object(G.main, cu->family, family_len, (unsigned int)text[a], family_gh);
702
703 if (is_eval_curve) {
704 /* Workaround for the above hack. */
705 ob = DEG_get_evaluated_object(ctx->depsgraph, ob);
706 }
707
708 if (ob) {
709 vec[0] = fsize * (ct->xof - xof);
710 vec[1] = fsize * (ct->yof - yof);
711 vec[2] = 0.0;
712
713 mul_m4_v3(pmat, vec);
714
715 copy_m4_m4(obmat, par->obmat);
716
717 if (UNLIKELY(ct->rot != 0.0f)) {
718 float rmat[4][4];
719
720 zero_v3(obmat[3]);
721 axis_angle_to_mat4_single(rmat, 'Z', -ct->rot);
722 mul_m4_m4m4(obmat, obmat, rmat);
723 }
724
725 copy_v3_v3(obmat[3], vec);
726
727 make_dupli(ctx, ob, obmat, a);
728 }
729 }
730
731 if (text_free) {
732 MEM_freeN((void *)text);
733 }
734
735 BLI_ghash_free(family_gh, NULL, NULL);
736
737 MEM_freeN(chartransdata);
738 }
739
740 static const DupliGenerator gen_dupli_verts_font = {
741 OB_DUPLIVERTS, /* type */
742 make_duplis_font /* make_duplis */
743 };
744
745 /** \} */
746
747 /* -------------------------------------------------------------------- */
748 /** \name Dupli-Vertices Implementation (#OB_DUPLIVERTS for #PointCloud)
749 * \{ */
750
make_child_duplis_pointcloud(const DupliContext * ctx,void * UNUSED (userdata),Object * child)751 static void make_child_duplis_pointcloud(const DupliContext *ctx,
752 void *UNUSED(userdata),
753 Object *child)
754 {
755 const Object *parent = ctx->object;
756 const PointCloud *pointcloud = parent->data;
757 const float(*co)[3] = pointcloud->co;
758 const float *radius = pointcloud->radius;
759 const float(*rotation)[4] = NULL; /* TODO: add optional rotation attribute. */
760 const float(*orco)[3] = NULL; /* TODO: add optional texture coordinate attribute. */
761
762 /* Relative transform from parent to child space. */
763 float child_imat[4][4];
764 mul_m4_m4m4(child_imat, child->imat, parent->obmat);
765
766 for (int i = 0; i < pointcloud->totpoint; i++) {
767 /* Transform matrix from point position, radius and rotation. */
768 float quat[4] = {1.0f, 0.0f, 0.0f, 0.0f};
769 float size[3] = {1.0f, 1.0f, 1.0f};
770 if (radius) {
771 copy_v3_fl(size, radius[i]);
772 }
773 if (rotation) {
774 copy_v4_v4(quat, rotation[i]);
775 }
776
777 float space_mat[4][4];
778 loc_quat_size_to_mat4(space_mat, co[i], quat, size);
779
780 /* Make offset relative to child object using relative child transform,
781 * and apply object matrix after local vertex transform. */
782 mul_mat3_m4_v3(child_imat, space_mat[3]);
783
784 /* Create dupli object. */
785 float obmat[4][4];
786 mul_m4_m4m4(obmat, child->obmat, space_mat);
787 DupliObject *dob = make_dupli(ctx, child, obmat, i);
788 if (orco) {
789 copy_v3_v3(dob->orco, orco[i]);
790 }
791
792 /* Recursion. */
793 make_recursive_duplis(ctx, child, space_mat, i);
794 }
795 }
796
make_duplis_pointcloud(const DupliContext * ctx)797 static void make_duplis_pointcloud(const DupliContext *ctx)
798 {
799 make_child_duplis(ctx, NULL, make_child_duplis_pointcloud);
800 }
801
802 static const DupliGenerator gen_dupli_verts_pointcloud = {
803 OB_DUPLIVERTS, /* type */
804 make_duplis_pointcloud /* make_duplis */
805 };
806
807 /** \} */
808
809 /* -------------------------------------------------------------------- */
810 /** \name Dupli-Faces Implementation (#OB_DUPLIFACES)
811 * \{ */
812
813 /** Values shared between different mesh types. */
814 typedef struct FaceDupliData_Params {
815 /**
816 * It's important we use this context instead of the `ctx` passed into #make_child_duplis
817 * since these won't match in the case of recursion.
818 */
819 const DupliContext *ctx;
820
821 bool use_scale;
822 } FaceDupliData_Params;
823
824 typedef struct FaceDupliData_Mesh {
825 FaceDupliData_Params params;
826
827 int totface;
828 const MPoly *mpoly;
829 const MLoop *mloop;
830 const MVert *mvert;
831 const float (*orco)[3];
832 const MLoopUV *mloopuv;
833 } FaceDupliData_Mesh;
834
835 typedef struct FaceDupliData_EditMesh {
836 FaceDupliData_Params params;
837
838 BMEditMesh *em;
839
840 bool has_orco, has_uvs;
841 int cd_loop_uv_offset;
842 /* Can be NULL. */
843 const float (*vert_coords)[3];
844 } FaceDupliData_EditMesh;
845
get_dupliface_transform_from_coords(const float coords[][3],const int coords_len,const bool use_scale,const float scale_fac,float r_mat[4][4])846 static void get_dupliface_transform_from_coords(const float coords[][3],
847 const int coords_len,
848 const bool use_scale,
849 const float scale_fac,
850 float r_mat[4][4])
851 {
852 float loc[3], quat[4], scale, size[3];
853
854 /* Location. */
855 {
856 const float w = 1.0f / (float)coords_len;
857 zero_v3(loc);
858 for (int i = 0; i < coords_len; i++) {
859 madd_v3_v3fl(loc, coords[i], w);
860 }
861 }
862 /* Rotation. */
863 {
864 float f_no[3];
865 cross_poly_v3(f_no, coords, (uint)coords_len);
866 normalize_v3(f_no);
867 tri_to_quat_ex(quat, coords[0], coords[1], coords[2], f_no);
868 }
869 /* Scale. */
870 if (use_scale) {
871 const float area = area_poly_v3(coords, (uint)coords_len);
872 scale = sqrtf(area) * scale_fac;
873 }
874 else {
875 scale = 1.0f;
876 }
877 size[0] = size[1] = size[2] = scale;
878
879 loc_quat_size_to_mat4(r_mat, loc, quat, size);
880 }
881
face_dupli(const DupliContext * ctx,Object * inst_ob,const float child_imat[4][4],const int index,const bool use_scale,const float scale_fac,const float (* coords)[3],const int coords_len)882 static DupliObject *face_dupli(const DupliContext *ctx,
883 Object *inst_ob,
884 const float child_imat[4][4],
885 const int index,
886 const bool use_scale,
887 const float scale_fac,
888 const float (*coords)[3],
889 const int coords_len)
890 {
891 float obmat[4][4];
892 float space_mat[4][4];
893
894 /* `obmat` is transform to face. */
895 get_dupliface_transform_from_coords(coords, coords_len, use_scale, scale_fac, obmat);
896
897 /* Make offset relative to inst_ob using relative child transform. */
898 mul_mat3_m4_v3(child_imat, obmat[3]);
899
900 /* XXX ugly hack to ensure same behavior as in master.
901 * This should not be needed, #Object.parentinv is not consistent outside of parenting. */
902 {
903 float imat[3][3];
904 copy_m3_m4(imat, inst_ob->parentinv);
905 mul_m4_m3m4(obmat, imat, obmat);
906 }
907
908 /* Apply `obmat` _after_ the local face transform. */
909 mul_m4_m4m4(obmat, inst_ob->obmat, obmat);
910
911 /* Space matrix is constructed by removing `obmat` transform,
912 * this yields the world-space transform for recursive duplis. */
913 mul_m4_m4m4(space_mat, obmat, inst_ob->imat);
914
915 DupliObject *dob = make_dupli(ctx, inst_ob, obmat, index);
916
917 /* Recursion. */
918 make_recursive_duplis(ctx, inst_ob, space_mat, index);
919
920 return dob;
921 }
922
923 /** Wrap #face_dupli, needed since we can't #alloca in a loop. */
face_dupli_from_mesh(const DupliContext * ctx,Object * inst_ob,const float child_imat[4][4],const int index,const bool use_scale,const float scale_fac,const MPoly * mpoly,const MLoop * mloopstart,const MVert * mvert)924 static DupliObject *face_dupli_from_mesh(const DupliContext *ctx,
925 Object *inst_ob,
926 const float child_imat[4][4],
927 const int index,
928 const bool use_scale,
929 const float scale_fac,
930
931 /* Mesh variables. */
932 const MPoly *mpoly,
933 const MLoop *mloopstart,
934 const MVert *mvert)
935 {
936 const int coords_len = mpoly->totloop;
937 float(*coords)[3] = BLI_array_alloca(coords, (size_t)coords_len);
938
939 const MLoop *ml = mloopstart;
940 for (int i = 0; i < coords_len; i++, ml++) {
941 copy_v3_v3(coords[i], mvert[ml->v].co);
942 }
943
944 return face_dupli(ctx, inst_ob, child_imat, index, use_scale, scale_fac, coords, coords_len);
945 }
946
947 /** Wrap #face_dupli, needed since we can't #alloca in a loop. */
face_dupli_from_editmesh(const DupliContext * ctx,Object * inst_ob,const float child_imat[4][4],const int index,const bool use_scale,const float scale_fac,BMFace * f,const float (* vert_coords)[3])948 static DupliObject *face_dupli_from_editmesh(const DupliContext *ctx,
949 Object *inst_ob,
950 const float child_imat[4][4],
951 const int index,
952 const bool use_scale,
953 const float scale_fac,
954
955 /* Mesh variables. */
956 BMFace *f,
957 const float (*vert_coords)[3])
958 {
959 const int coords_len = f->len;
960 float(*coords)[3] = BLI_array_alloca(coords, (size_t)coords_len);
961
962 BMLoop *l_first, *l_iter;
963 int i = 0;
964 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
965 if (vert_coords != NULL) {
966 do {
967 copy_v3_v3(coords[i++], vert_coords[BM_elem_index_get(l_iter->v)]);
968 } while ((l_iter = l_iter->next) != l_first);
969 }
970 else {
971 do {
972 copy_v3_v3(coords[i++], l_iter->v->co);
973 } while ((l_iter = l_iter->next) != l_first);
974 }
975
976 return face_dupli(ctx, inst_ob, child_imat, index, use_scale, scale_fac, coords, coords_len);
977 }
978
make_child_duplis_faces_from_mesh(const DupliContext * ctx,void * userdata,Object * inst_ob)979 static void make_child_duplis_faces_from_mesh(const DupliContext *ctx,
980 void *userdata,
981 Object *inst_ob)
982 {
983 FaceDupliData_Mesh *fdd = userdata;
984 const MPoly *mpoly = fdd->mpoly, *mp;
985 const MLoop *mloop = fdd->mloop;
986 const MVert *mvert = fdd->mvert;
987 const float(*orco)[3] = fdd->orco;
988 const MLoopUV *mloopuv = fdd->mloopuv;
989 const int totface = fdd->totface;
990 const bool use_scale = fdd->params.use_scale;
991 int a;
992
993 float child_imat[4][4];
994
995 invert_m4_m4(inst_ob->imat, inst_ob->obmat);
996 /* Relative transform from parent to child space. */
997 mul_m4_m4m4(child_imat, inst_ob->imat, ctx->object->obmat);
998 const float scale_fac = ctx->object->instance_faces_scale;
999
1000 for (a = 0, mp = mpoly; a < totface; a++, mp++) {
1001 const MLoop *loopstart = mloop + mp->loopstart;
1002 DupliObject *dob = face_dupli_from_mesh(
1003 fdd->params.ctx, inst_ob, child_imat, a, use_scale, scale_fac, mp, loopstart, mvert);
1004
1005 const float w = 1.0f / (float)mp->totloop;
1006 if (orco) {
1007 for (int j = 0; j < mp->totloop; j++) {
1008 madd_v3_v3fl(dob->orco, orco[loopstart[j].v], w);
1009 }
1010 }
1011 if (mloopuv) {
1012 for (int j = 0; j < mp->totloop; j++) {
1013 madd_v2_v2fl(dob->uv, mloopuv[mp->loopstart + j].uv, w);
1014 }
1015 }
1016 }
1017 }
1018
make_child_duplis_faces_from_editmesh(const DupliContext * ctx,void * userdata,Object * inst_ob)1019 static void make_child_duplis_faces_from_editmesh(const DupliContext *ctx,
1020 void *userdata,
1021 Object *inst_ob)
1022 {
1023 FaceDupliData_EditMesh *fdd = userdata;
1024 BMEditMesh *em = fdd->em;
1025 float child_imat[4][4];
1026 int a;
1027 BMFace *f;
1028 BMIter iter;
1029 const bool use_scale = fdd->params.use_scale;
1030
1031 const float(*vert_coords)[3] = fdd->vert_coords;
1032
1033 BLI_assert((vert_coords == NULL) || (em->bm->elem_index_dirty & BM_VERT) == 0);
1034
1035 invert_m4_m4(inst_ob->imat, inst_ob->obmat);
1036 /* Relative transform from parent to child space. */
1037 mul_m4_m4m4(child_imat, inst_ob->imat, ctx->object->obmat);
1038 const float scale_fac = ctx->object->instance_faces_scale;
1039
1040 BM_ITER_MESH_INDEX (f, &iter, em->bm, BM_FACES_OF_MESH, a) {
1041 DupliObject *dob = face_dupli_from_editmesh(
1042 fdd->params.ctx, inst_ob, child_imat, a, use_scale, scale_fac, f, vert_coords);
1043
1044 if (fdd->has_orco) {
1045 const float w = 1.0f / (float)f->len;
1046 BMLoop *l_first, *l_iter;
1047 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
1048 do {
1049 madd_v3_v3fl(dob->orco, l_iter->v->co, w);
1050 } while ((l_iter = l_iter->next) != l_first);
1051 }
1052 if (fdd->has_uvs) {
1053 BM_face_uv_calc_center_median(f, fdd->cd_loop_uv_offset, dob->uv);
1054 }
1055 }
1056 }
1057
make_duplis_faces(const DupliContext * ctx)1058 static void make_duplis_faces(const DupliContext *ctx)
1059 {
1060 Object *parent = ctx->object;
1061
1062 /* Gather mesh info. */
1063 BMEditMesh *em = NULL;
1064 const float(*vert_coords)[3] = NULL;
1065 Mesh *me_eval = mesh_data_from_duplicator_object(parent, &em, &vert_coords, NULL);
1066 if (em == NULL && me_eval == NULL) {
1067 return;
1068 }
1069
1070 FaceDupliData_Params fdd_params = {
1071 .ctx = ctx,
1072 .use_scale = parent->transflag & OB_DUPLIFACES_SCALE,
1073 };
1074
1075 if (em != NULL) {
1076 const int uv_idx = CustomData_get_render_layer(&em->bm->ldata, CD_MLOOPUV);
1077 FaceDupliData_EditMesh fdd = {
1078 .params = fdd_params,
1079 .em = em,
1080 .vert_coords = vert_coords,
1081 .has_orco = (vert_coords != NULL),
1082 .has_uvs = (uv_idx != -1),
1083 .cd_loop_uv_offset = (uv_idx != -1) ?
1084 CustomData_get_n_offset(&em->bm->ldata, CD_MLOOPUV, uv_idx) :
1085 -1,
1086 };
1087 make_child_duplis(ctx, &fdd, make_child_duplis_faces_from_editmesh);
1088 }
1089 else {
1090 const int uv_idx = CustomData_get_render_layer(&me_eval->ldata, CD_MLOOPUV);
1091 FaceDupliData_Mesh fdd = {
1092 .params = fdd_params,
1093 .totface = me_eval->totpoly,
1094 .mpoly = me_eval->mpoly,
1095 .mloop = me_eval->mloop,
1096 .mvert = me_eval->mvert,
1097 .mloopuv = (uv_idx != -1) ? CustomData_get_layer_n(&me_eval->ldata, CD_MLOOPUV, uv_idx) :
1098 NULL,
1099 .orco = CustomData_get_layer(&me_eval->vdata, CD_ORCO),
1100 };
1101 make_child_duplis(ctx, &fdd, make_child_duplis_faces_from_mesh);
1102 }
1103 }
1104
1105 static const DupliGenerator gen_dupli_faces = {
1106 OB_DUPLIFACES, /* type */
1107 make_duplis_faces /* make_duplis */
1108 };
1109
1110 /** \} */
1111
1112 /* -------------------------------------------------------------------- */
1113 /** \name Dupli-Particles Implementation (#OB_DUPLIPARTS)
1114 * \{ */
1115
make_duplis_particle_system(const DupliContext * ctx,ParticleSystem * psys)1116 static void make_duplis_particle_system(const DupliContext *ctx, ParticleSystem *psys)
1117 {
1118 Scene *scene = ctx->scene;
1119 Object *par = ctx->object;
1120 eEvaluationMode mode = DEG_get_mode(ctx->depsgraph);
1121 bool for_render = mode == DAG_EVAL_RENDER;
1122
1123 Object *ob = NULL, **oblist = NULL;
1124 DupliObject *dob;
1125 ParticleDupliWeight *dw;
1126 ParticleSettings *part;
1127 ParticleData *pa;
1128 ChildParticle *cpa = NULL;
1129 ParticleKey state;
1130 ParticleCacheKey *cache;
1131 float ctime, scale = 1.0f;
1132 float tmat[4][4], mat[4][4], pamat[4][4], size = 0.0;
1133 int a, b, hair = 0;
1134 int totpart, totchild;
1135
1136 int no_draw_flag = PARS_UNEXIST;
1137
1138 if (psys == NULL) {
1139 return;
1140 }
1141
1142 part = psys->part;
1143
1144 if (part == NULL) {
1145 return;
1146 }
1147
1148 if (!psys_check_enabled(par, psys, for_render)) {
1149 return;
1150 }
1151
1152 if (!for_render) {
1153 no_draw_flag |= PARS_NO_DISP;
1154 }
1155
1156 /* NOTE: in old animation system, used parent object's time-offset. */
1157 ctime = DEG_get_ctime(ctx->depsgraph);
1158
1159 totpart = psys->totpart;
1160 totchild = psys->totchild;
1161
1162 if ((for_render || part->draw_as == PART_DRAW_REND) &&
1163 ELEM(part->ren_as, PART_DRAW_OB, PART_DRAW_GR)) {
1164 ParticleSimulationData sim = {NULL};
1165 sim.depsgraph = ctx->depsgraph;
1166 sim.scene = scene;
1167 sim.ob = par;
1168 sim.psys = psys;
1169 sim.psmd = psys_get_modifier(par, psys);
1170 /* Make sure emitter `imat` is in global coordinates instead of render view coordinates. */
1171 invert_m4_m4(par->imat, par->obmat);
1172
1173 /* First check for loops (particle system object used as dupli-object). */
1174 if (part->ren_as == PART_DRAW_OB) {
1175 if (ELEM(part->instance_object, NULL, par)) {
1176 return;
1177 }
1178 }
1179 else { /* #PART_DRAW_GR. */
1180 if (part->instance_collection == NULL) {
1181 return;
1182 }
1183
1184 const ListBase dup_collection_objects = BKE_collection_object_cache_get(
1185 part->instance_collection);
1186 if (BLI_listbase_is_empty(&dup_collection_objects)) {
1187 return;
1188 }
1189
1190 if (BLI_findptr(&dup_collection_objects, par, offsetof(Base, object))) {
1191 return;
1192 }
1193 }
1194
1195 /* If we have a hair particle system, use the path cache. */
1196 if (part->type == PART_HAIR) {
1197 if (psys->flag & PSYS_HAIR_DONE) {
1198 hair = (totchild == 0 || psys->childcache) && psys->pathcache;
1199 }
1200 if (!hair) {
1201 return;
1202 }
1203
1204 /* We use cache, update `totchild` according to cached data. */
1205 totchild = psys->totchildcache;
1206 totpart = psys->totcached;
1207 }
1208
1209 RNG *rng = BLI_rng_new_srandom(31415926u + (unsigned int)psys->seed);
1210
1211 psys->lattice_deform_data = psys_create_lattice_deform_data(&sim);
1212
1213 /* Gather list of objects or single object. */
1214 int totcollection = 0;
1215
1216 const bool use_whole_collection = part->draw & PART_DRAW_WHOLE_GR;
1217 const bool use_collection_count = part->draw & PART_DRAW_COUNT_GR && !use_whole_collection;
1218 if (part->ren_as == PART_DRAW_GR) {
1219 if (use_collection_count) {
1220 psys_find_group_weights(part);
1221
1222 for (dw = part->instance_weights.first; dw; dw = dw->next) {
1223 FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_BEGIN (
1224 part->instance_collection, object, mode) {
1225 if (dw->ob == object) {
1226 totcollection += dw->count;
1227 break;
1228 }
1229 }
1230 FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_END;
1231 }
1232 }
1233 else {
1234 FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_BEGIN (
1235 part->instance_collection, object, mode) {
1236 (void)object;
1237 totcollection++;
1238 }
1239 FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_END;
1240 }
1241
1242 oblist = MEM_callocN((size_t)totcollection * sizeof(Object *), "dupcollection object list");
1243
1244 if (use_collection_count) {
1245 a = 0;
1246 for (dw = part->instance_weights.first; dw; dw = dw->next) {
1247 FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_BEGIN (
1248 part->instance_collection, object, mode) {
1249 if (dw->ob == object) {
1250 for (b = 0; b < dw->count; b++, a++) {
1251 oblist[a] = dw->ob;
1252 }
1253 break;
1254 }
1255 }
1256 FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_END;
1257 }
1258 }
1259 else {
1260 a = 0;
1261 FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_BEGIN (
1262 part->instance_collection, object, mode) {
1263 oblist[a] = object;
1264 a++;
1265 }
1266 FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_END;
1267 }
1268 }
1269 else {
1270 ob = part->instance_object;
1271 }
1272
1273 if (totchild == 0 || part->draw & PART_DRAW_PARENT) {
1274 a = 0;
1275 }
1276 else {
1277 a = totpart;
1278 }
1279
1280 for (pa = psys->particles; a < totpart + totchild; a++, pa++) {
1281 if (a < totpart) {
1282 /* Handle parent particle. */
1283 if (pa->flag & no_draw_flag) {
1284 continue;
1285 }
1286
1287 #if 0 /* UNUSED */
1288 pa_num = pa->num;
1289 #endif
1290 size = pa->size;
1291 }
1292 else {
1293 /* Handle child particle. */
1294 cpa = &psys->child[a - totpart];
1295
1296 #if 0 /* UNUSED */
1297 pa_num = a;
1298 #endif
1299 size = psys_get_child_size(psys, cpa, ctime, NULL);
1300 }
1301
1302 /* Some hair paths might be non-existent so they can't be used for duplication. */
1303 if (hair && psys->pathcache &&
1304 ((a < totpart && psys->pathcache[a]->segments < 0) ||
1305 (a >= totpart && psys->childcache[a - totpart]->segments < 0))) {
1306 continue;
1307 }
1308
1309 if (part->ren_as == PART_DRAW_GR) {
1310 /* Prevent divide by zero below T28336. */
1311 if (totcollection == 0) {
1312 continue;
1313 }
1314
1315 /* For collections, pick the object based on settings. */
1316 if (part->draw & PART_DRAW_RAND_GR && !use_whole_collection) {
1317 b = BLI_rng_get_int(rng) % totcollection;
1318 }
1319 else {
1320 b = a % totcollection;
1321 }
1322
1323 ob = oblist[b];
1324 }
1325
1326 if (hair) {
1327 /* Hair we handle separate and compute transform based on hair keys. */
1328 if (a < totpart) {
1329 cache = psys->pathcache[a];
1330 psys_get_dupli_path_transform(&sim, pa, NULL, cache, pamat, &scale);
1331 }
1332 else {
1333 cache = psys->childcache[a - totpart];
1334 psys_get_dupli_path_transform(&sim, NULL, cpa, cache, pamat, &scale);
1335 }
1336
1337 copy_v3_v3(pamat[3], cache->co);
1338 pamat[3][3] = 1.0f;
1339 }
1340 else {
1341 /* First key. */
1342 state.time = ctime;
1343 if (psys_get_particle_state(&sim, a, &state, 0) == 0) {
1344 continue;
1345 }
1346
1347 float tquat[4];
1348 normalize_qt_qt(tquat, state.rot);
1349 quat_to_mat4(pamat, tquat);
1350 copy_v3_v3(pamat[3], state.co);
1351 pamat[3][3] = 1.0f;
1352 }
1353
1354 if (part->ren_as == PART_DRAW_GR && psys->part->draw & PART_DRAW_WHOLE_GR) {
1355 b = 0;
1356 FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_BEGIN (
1357 part->instance_collection, object, mode) {
1358 copy_m4_m4(tmat, oblist[b]->obmat);
1359
1360 /* Apply collection instance offset. */
1361 sub_v3_v3(tmat[3], part->instance_collection->instance_offset);
1362
1363 /* Apply particle scale. */
1364 mul_mat3_m4_fl(tmat, size * scale);
1365 mul_v3_fl(tmat[3], size * scale);
1366
1367 /* Individual particle transform. */
1368 mul_m4_m4m4(mat, pamat, tmat);
1369
1370 dob = make_dupli(ctx, object, mat, a);
1371 dob->particle_system = psys;
1372
1373 psys_get_dupli_texture(psys, part, sim.psmd, pa, cpa, dob->uv, dob->orco);
1374
1375 b++;
1376 }
1377 FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_END;
1378 }
1379 else {
1380 float obmat[4][4];
1381 copy_m4_m4(obmat, ob->obmat);
1382
1383 float vec[3];
1384 copy_v3_v3(vec, obmat[3]);
1385 zero_v3(obmat[3]);
1386
1387 /* Particle rotation uses x-axis as the aligned axis,
1388 * so pre-rotate the object accordingly. */
1389 if ((part->draw & PART_DRAW_ROTATE_OB) == 0) {
1390 float xvec[3], q[4], size_mat[4][4], original_size[3];
1391
1392 mat4_to_size(original_size, obmat);
1393 size_to_mat4(size_mat, original_size);
1394
1395 xvec[0] = -1.f;
1396 xvec[1] = xvec[2] = 0;
1397 vec_to_quat(q, xvec, ob->trackflag, ob->upflag);
1398 quat_to_mat4(obmat, q);
1399 obmat[3][3] = 1.0f;
1400
1401 /* Add scaling if requested. */
1402 if ((part->draw & PART_DRAW_NO_SCALE_OB) == 0) {
1403 mul_m4_m4m4(obmat, obmat, size_mat);
1404 }
1405 }
1406 else if (part->draw & PART_DRAW_NO_SCALE_OB) {
1407 /* Remove scaling. */
1408 float size_mat[4][4], original_size[3];
1409
1410 mat4_to_size(original_size, obmat);
1411 size_to_mat4(size_mat, original_size);
1412 invert_m4(size_mat);
1413
1414 mul_m4_m4m4(obmat, obmat, size_mat);
1415 }
1416
1417 mul_m4_m4m4(tmat, pamat, obmat);
1418 mul_mat3_m4_fl(tmat, size * scale);
1419
1420 copy_m4_m4(mat, tmat);
1421
1422 if (part->draw & PART_DRAW_GLOBAL_OB) {
1423 add_v3_v3v3(mat[3], mat[3], vec);
1424 }
1425
1426 dob = make_dupli(ctx, ob, mat, a);
1427 dob->particle_system = psys;
1428 psys_get_dupli_texture(psys, part, sim.psmd, pa, cpa, dob->uv, dob->orco);
1429 }
1430 }
1431
1432 BLI_rng_free(rng);
1433 }
1434
1435 /* Clean up. */
1436 if (oblist) {
1437 MEM_freeN(oblist);
1438 }
1439
1440 if (psys->lattice_deform_data) {
1441 BKE_lattice_deform_data_destroy(psys->lattice_deform_data);
1442 psys->lattice_deform_data = NULL;
1443 }
1444 }
1445
make_duplis_particles(const DupliContext * ctx)1446 static void make_duplis_particles(const DupliContext *ctx)
1447 {
1448 ParticleSystem *psys;
1449 int psysid;
1450
1451 /* Particle system take up one level in id, the particles another. */
1452 for (psys = ctx->object->particlesystem.first, psysid = 0; psys; psys = psys->next, psysid++) {
1453 /* Particles create one more level for persistent `psys` index. */
1454 DupliContext pctx;
1455 copy_dupli_context(&pctx, ctx, ctx->object, NULL, psysid);
1456 make_duplis_particle_system(&pctx, psys);
1457 }
1458 }
1459
1460 static const DupliGenerator gen_dupli_particles = {
1461 OB_DUPLIPARTS, /* type */
1462 make_duplis_particles /* make_duplis */
1463 };
1464
1465 /** \} */
1466
1467 /* -------------------------------------------------------------------- */
1468 /** \name Dupli-Generator Selector For The Given Context
1469 * \{ */
1470
get_dupli_generator(const DupliContext * ctx)1471 static const DupliGenerator *get_dupli_generator(const DupliContext *ctx)
1472 {
1473 int transflag = ctx->object->transflag;
1474 int restrictflag = ctx->object->restrictflag;
1475
1476 if ((transflag & OB_DUPLI) == 0) {
1477 return NULL;
1478 }
1479
1480 /* Should the dupli's be generated for this object? - Respect restrict flags. */
1481 if (DEG_get_mode(ctx->depsgraph) == DAG_EVAL_RENDER ? (restrictflag & OB_RESTRICT_RENDER) :
1482 (restrictflag & OB_RESTRICT_VIEWPORT)) {
1483 return NULL;
1484 }
1485
1486 if (transflag & OB_DUPLIPARTS) {
1487 return &gen_dupli_particles;
1488 }
1489 if (transflag & OB_DUPLIVERTS) {
1490 if (ctx->object->type == OB_MESH) {
1491 return &gen_dupli_verts;
1492 }
1493 if (ctx->object->type == OB_FONT) {
1494 return &gen_dupli_verts_font;
1495 }
1496 if (ctx->object->type == OB_POINTCLOUD) {
1497 return &gen_dupli_verts_pointcloud;
1498 }
1499 }
1500 else if (transflag & OB_DUPLIFACES) {
1501 if (ctx->object->type == OB_MESH) {
1502 return &gen_dupli_faces;
1503 }
1504 }
1505 else if (transflag & OB_DUPLICOLLECTION) {
1506 return &gen_dupli_collection;
1507 }
1508
1509 return NULL;
1510 }
1511
1512 /** \} */
1513
1514 /* -------------------------------------------------------------------- */
1515 /** \name Dupli-Container Implementation
1516 * \{ */
1517
1518 /**
1519 * \return a #ListBase of #DupliObject.
1520 */
object_duplilist(Depsgraph * depsgraph,Scene * sce,Object * ob)1521 ListBase *object_duplilist(Depsgraph *depsgraph, Scene *sce, Object *ob)
1522 {
1523 ListBase *duplilist = MEM_callocN(sizeof(ListBase), "duplilist");
1524 DupliContext ctx;
1525 init_context(&ctx, depsgraph, sce, ob, NULL);
1526 if (ctx.gen) {
1527 ctx.duplilist = duplilist;
1528 ctx.gen->make_duplis(&ctx);
1529 }
1530
1531 return duplilist;
1532 }
1533
free_object_duplilist(ListBase * lb)1534 void free_object_duplilist(ListBase *lb)
1535 {
1536 BLI_freelistN(lb);
1537 MEM_freeN(lb);
1538 }
1539
1540 /** \} */
1541