47 MEM_delete(psys->
child);
48 psys->
child =
nullptr;
65 const bool use_render_params)
76 for (i = 0; i < child_num; i++) {
77 for (p = 0; p < psys->
totpart; p++, cpa++) {
100 float min[3],
max[3], delta[3], d;
103 int i, j, k, p, res = psys->
part->
grid_res, size[3], axis;
108 for (i = 1; i < totvert; i++) {
122 d = delta[axis] /
float(res);
125 size[(axis + 1) % 3] =
int(
ceil(delta[(axis + 1) % 3] / d));
126 size[(axis + 2) % 3] =
int(
ceil(delta[(axis + 2) % 3] / d));
129 size[(axis + 1) % 3] = std::min(size[(axis + 1) % 3], res);
130 size[(axis + 2) % 3] = std::min(size[(axis + 2) % 3], res);
132 size[0] = std::max(size[0], 1);
133 size[1] = std::max(size[1], 1);
134 size[2] = std::max(size[2], 1);
137 min[0] += d < delta[0] ? d / 2.0f : delta[0] / 2.0f;
138 min[1] += d < delta[1] ? d / 2.0f : delta[1] / 2.0f;
139 min[2] += d < delta[2] ? d / 2.0f : delta[2] / 2.0f;
141 for (i = 0, p = 0, pa = psys->
particles; i < res; i++) {
142 for (j = 0; j < res; j++) {
143 for (k = 0; k < res; k++, p++, pa++) {
163 for (i = 0; i < totvert; i++) {
168 pa[(int(vec[0] * (size[0] - 1)) * res + int(vec[1] * (size[1] - 1))) * res +
169 int(vec[2] * (size[2] - 1))]
174 float co1[3], co2[3];
176 MFace *mface =
nullptr, *mface_array;
177 float v1[3],
v2[3], v3[3], v4[4], lambda;
178 int a, a1, a2, a0mul, a1mul, a2mul, totface;
181 totface =
mesh->totface_legacy;
182 mface = mface_array =
static_cast<MFace *
>(
185 for (a = 0; a < amax; a++) {
202 for (a1 = 0; a1 < size[(a + 1) % 3]; a1++) {
203 for (a2 = 0; a2 < size[(a + 2) % 3]; a2++) {
206 pa = psys->
particles + a1 * a1mul + a2 * a2mul;
208 co1[a] -= d < delta[a] ? d / 2.0f : delta[a] / 2.0f;
210 co2[a] += delta[a] + 0.001f * d;
211 co1[a] -= 0.001f * d;
214 float ray_direction[3];
219 for (i = 0; i < totface; i++, mface++) {
227 co1, &isect_precalc, v1,
v2, v3, &lambda,
nullptr);
228 if (intersects_tri) {
229 pa1 = (pa + int(lambda * size[a]) * a0mul);
236 pa2 = (pa + int(lambda * size[a]) * a0mul);
249 if (pa2 && pa2 != pa1) {
264 for (i = 0; i < size[0]; i++) {
265 if (
in || (pa + i * a0mul)->hair_index % 2) {
270 in = (
in + (pa + i * a0mul)->hair_index) % 2;
279 for (i = 0, p = 0, pa = psys->
particles; i < res; i++) {
280 for (j = 0; j < res; j++) {
281 for (k = 0; k < res; k++, p++, pa++) {
283 pa->
fuv[0] += d / 2.0f;
287 pa->
fuv[0] += d / 2.0f;
288 pa->
fuv[1] += d / 2.0f;
296 for (i = 0; i < size[0]; i++) {
297 for (j = 0; j < size[1]; j++) {
298 pa = psys->
particles + res * (i * res + j);
299 for (k = 0; k < size[2]; k++, pa++) {
308 for (p = 0, pa = psys->
particles; p < psys->totpart; p++, pa++) {
333 for (
int k = 0; k < n; k++) {
336 out[2 * k + 0] =
fmod(
double(k) /
double(n) + ofs[0], 1.0);
337 out[2 * k + 1] =
fmod(t + ofs[1], 1.0);
345 float *jit2,
x, rad1, rad2, rad3;
359 for (i = 0; i < num2; i += 2) {
374 for (i = 0; i < 4; i++) {
376 reinterpret_cast<float (*)[2]
>(
jit),
reinterpret_cast<float (*)[2]
>(jit2),
num, rad1);
378 reinterpret_cast<float (*)[2]
>(
jit),
reinterpret_cast<float (*)[2]
>(jit2),
num, rad1);
380 reinterpret_cast<float (*)[2]
>(
jit),
reinterpret_cast<float (*)[2]
>(jit2),
num, rad2);
388 float vert[4][3], co[3];
428 int mid, low = 0, high = n - 1;
434 if (sum[low] >= value) {
438 if (sum[high - 1] < value) {
443 mid = (low + high) / 2;
445 if ((sum[mid] >= value) && (sum[mid - 1] < value)) {
449 if (sum[mid] > value) {
462#define PSYS_RND_DIST_SKIP 3
465#define ONLY_WORKING_WITH_PA_VERTS 0
490 for (
int j = 0; j < 4; j++, vert++) {
491 if (*vert == pa->
num) {
502#if ONLY_WORKING_WITH_PA_VERTS
508 ctx->
mesh, from, pa->
num, pa->
num_dmcache, pa->
fuv, pa->
foffset, co1, 0, 0, 0, orco1, 0);
512 for (
w = 0;
w < maxw;
w++) {
513 pa->verts[
w] = ptn->num;
519 if (rng_skip_tot > 0) {
529 int distr = ctx->
distr;
552 if (!
isnan(offset)) {
554 ctx->
jit[2 *
int(offset)], ctx->
jit[2 *
int(offset) + 1], mface->
v4, pa->
fuv);
569 if (rng_skip_tot > 0) {
578 const float *v1, *
v2, *v3, *v4;
580 float cur_d, min_d, randu, randv;
581 int distr = ctx->
distr;
605 if (!
isnan(offset)) {
607 ctx->
jit[2 *
int(offset)], ctx->
jit[2 *
int(offset) + 1], mface->
v4, pa->
fuv);
622 tot =
mesh->totface_legacy;
625 reinterpret_cast<const float (*)[3]
>(positions.
data()),
626 reinterpret_cast<const float (*)[3]
>(
mesh->vert_normals().data()),
642 mface =
static_cast<MFace *
>(
644 for (i = 0; i < tot; i++, mface++) {
649 v1 = positions[mface->
v1];
650 v2 = positions[mface->
v2];
651 v3 = positions[mface->
v3];
661 v4 = positions[mface->
v4];
688 if (rng_skip_tot > 0) {
698 float orco1[3], co1[3], nor1[3];
700 int cfrom = ctx->
cfrom;
706 if (ctx->
index[p] < 0) {
708 cpa->
fuv[0] = cpa->
fuv[1] = cpa->
fuv[2] = cpa->
fuv[3] = 0.0f;
709 cpa->
pa[0] = cpa->
pa[1] = cpa->
pa[2] = cpa->
pa[3] = 0;
715 mf = &mfaces[ctx->
index[p]];
728 float maxd , totw = 0.0f;
746 maxd = ptn[maxw - 1].
dist;
750 for (
w = 0;
w < maxw;
w++) {
752 pweight[
w] =
float(
pow(2.0,
double(-6.0f * ptn[
w].dist / maxd)));
754 for (;
w < 10;
w++) {
759 for (
w = 0, i = 0;
w < maxw && i < 4;
w++) {
760 if (parent[
w] >= 0) {
761 cpa->
pa[i] = parent[
w];
762 cpa->
w[i] = pweight[
w];
773 for (
w = 0;
w < 4;
w++) {
781 if (rng_skip_tot > 0) {
798 for (p = task->
begin; p < task->end; p++, pa++) {
803 for (p = task->
begin; p < task->end; p++, pa++) {
808 for (p = task->
begin; p < task->end; p++, pa++) {
826 for (p = 0; p < task->
begin; p++, cpa++) {
830 for (; p < task->
end; p++, cpa++) {
837 const int *orig_index =
static_cast<const int *
>(user_data);
838 int index1 = orig_index[*
static_cast<const int *
>(p1)];
839 int index2 = orig_index[*
static_cast<const int *
>(p2)];
841 if (index1 < index2) {
844 if (index1 == index2) {
870 if (psys->
child && totchild) {
871 for (p = 0, cpa = psys->
child; p < totchild; p++, cpa++) {
872 cpa->
fuv[0] = cpa->
fuv[1] = cpa->
fuv[2] = cpa->
fuv[3] = 0.0;
875 cpa->
pa[0] = cpa->
pa[1] = cpa->
pa[2] = cpa->
pa[3] = 0;
884 pa->fuv[0] = pa->fuv[1] = pa->fuv[2] = pa->fuv[3] = 0.0;
905 float *
jit =
nullptr;
908 int totelem = 0, totpart, *particle_element =
nullptr, children = 0, totseam = 0;
909 int jitlevel = 1, distr;
910 float *element_weight =
nullptr, *jitter_offset =
nullptr, *vweight =
nullptr;
911 float cur, maxweight = 0.0, tweight, totweight, inv_totweight, co[3], nor[3], orco[3];
914 if (
ELEM(
nullptr, ob, psys, psys->
part)) {
928 "Can't create particles with the current modifier stack, disable destructive modifiers\n");
965 if (
mesh != final_mesh) {
989 for (p = 0, pa = psys->
particles; p < totpart; p++, pa++) {
1011 distr = part->
distr;
1030 const float (*orcodata)[3] =
static_cast<const float (*)[3]
>(
1032 int totvert =
mesh->verts_num;
1036 for (p = 0; p < totvert; p++) {
1058 fprintf(stderr,
"Particle distribution error: Nothing to emit from!\n");
1061 if (
mesh != final_mesh) {
1077 float totarea = 0.0f, co1[3], co2[3], co3[3], co4[3];
1078 const float (*orcodata)[3];
1084 for (i = 0; i < totelem; i++) {
1085 MFace *mf = &mfaces[i];
1112 maxweight = std::max(cur, maxweight);
1114 element_weight[i] = cur;
1118 for (i = 0; i < totelem; i++) {
1119 element_weight[i] /= totarea;
1122 maxweight /= totarea;
1125 float min = 1.0f /
float(std::min(totelem, totpart));
1126 for (i = 0; i < totelem; i++) {
1127 element_weight[i] =
min;
1137 for (i = 0; i < totelem; i++) {
1138 element_weight[i] *= vweight[i];
1144 for (i = 0; i < totelem; i++) {
1145 MFace *mf = &mfaces[i];
1146 tweight = vweight[mf->
v1] + vweight[mf->
v2] + vweight[mf->
v3];
1149 tweight += vweight[mf->
v4];
1156 element_weight[i] *= tweight;
1159 MEM_delete(vweight);
1165 for (i = 0; i < totelem; i++) {
1166 if (element_weight[i] > 0.0f) {
1168 totweight += element_weight[i];
1172 if (totmapped == 0) {
1174 if (
mesh != final_mesh) {
1179 MEM_delete(element_weight);
1180 MEM_delete(particle_element);
1181 MEM_delete(jitter_offset);
1185 inv_totweight = 1.0f / totweight;
1197 for (i = 0; i < totelem && element_weight[i] == 0.0f; i++) {
1200 element_sum[i_mapped] = element_weight[i] * inv_totweight;
1201 element_map[i_mapped] = i;
1203 for (i++; i < totelem; i++) {
1204 if (element_weight[i] > 0.0f) {
1205 element_sum[i_mapped] = element_sum[i_mapped - 1] + element_weight[i] * inv_totweight;
1207 if (element_sum[i_mapped] > element_sum[i_mapped - 1]) {
1208 element_map[i_mapped] = i;
1213 totmapped = i_mapped;
1217 for (p = 0; p < totpart; p++) {
1222 particle_element[p] = element_map[eidx];
1224 BLI_assert(eidx ? (pos > element_sum[eidx - 1]) : (pos >= 0.0f));
1225 jitter_offset[particle_element[p]] = pos;
1231 step = (totpart < 2) ? 0.5 : 1.0 / double(totpart);
1239 pos = (totpart < totmapped) ? 0.5 /
double(totmapped) :
1242 for (i = 0, p = 0; p < totpart; p++, pos +=
step) {
1243 for (; (i < totmapped - 1) && (pos > double(element_sum[i])); i++) {
1247 particle_element[p] = element_map[i];
1249 jitter_offset[particle_element[p]] = pos;
1253 MEM_delete(element_sum);
1254 MEM_delete(element_map);
1259 const int *orig_index =
nullptr;
1262 if (
mesh->verts_num) {
1263 orig_index =
static_cast<const int *
>(
1268 if (
mesh->totface_legacy) {
1269 orig_index =
static_cast<const int *
>(
1279 (
void *)orig_index);
1287 if (jitlevel == 0) {
1288 jitlevel = totpart / totelem;
1292 jitlevel = std::max(jitlevel, 3);
1300 if (jitlevel < 25) {
1307 jit,
sizeof(
float[2]), jitlevel, psys->
seed);
1315 ctx->
index = particle_element;
1318 ctx->
jitoff = jitter_offset;
1319 ctx->
weight = element_weight;
1338 int seed = 31415926 + sim->
psys->
seed;
1372 if (ctx.
mesh != final_mesh) {
1386 fprintf(stderr,
"Shape emission not yet possible!\n");
1392 int distr_error = 0;
1395 if (psmd->mesh_final) {
1409 fprintf(stderr,
"Particle distribution error!\n");
CustomData interface, see also DNA_customdata_types.h.
A KD-tree for nearest neighbor search.
Read Blender memory allocation and freeing.
#define INIT_MINMAX(min, max)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
constexpr const T * data() const
bool intersect(IsectPyramid i_pyramid, Box box)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
float length(VecOp< float, D >) RET
ccl_device_inline float2 fmod(const float2 a, const float b)
float area_quad_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
static void init_mv_jit(float *jit, int num, int seed2, float amount)
static void exec_distribute_parent(TaskPool *__restrict, void *taskdata)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void kdtree_balance(KDTree< CoordT > *tree)
MINLINE void copy_v3_v3(float r[3], const float a[3])
static void distribute_from_verts_exec(ParticleTask *thread, ParticleData *pa, int p)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
Vector< ParticleTask > psys_tasks_create(struct ParticleThreadContext *ctx, int startpart, int endpart)
void minmax_v3v3_v3(float min[3], float max[3], const float vec[3])
double BLI_rng_get_double(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
static void distribute_simple_children(Scene *scene, Object *ob, Mesh *final_mesh, Mesh *deform_mesh, ParticleSystem *psys, const bool use_render_params)
bool isect_ray_tri_watertight_v3(const float ray_origin[3], const struct IsectRayPrecalc *isect_precalc, const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2])
void BLI_jitterate2(float(*jit1)[2], float(*jit2)[2], int num, float radius2)
struct RNG * BLI_rng_new_srandom(unsigned int seed)
static void psys_task_init_distribute(ParticleTask *task, ParticleSimulationData *sim)
float * psys_cache_vgroup(struct Mesh *mesh, struct ParticleSystem *psys, int vgroup)
void psys_thread_context_init(struct ParticleThreadContext *ctx, struct ParticleSimulationData *sim)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void psys_interpolate_face(struct Mesh *mesh, const float(*vert_positions)[3], const float(*vert_normals)[3], const struct MFace *mface, struct MTFace *tface, const float(*orcodata)[3], float w[4], float vec[3], float nor[3], float utan[3], float vtan[3], float orco[3])
static void distribute_children_exec(ParticleTask *thread, ChildParticle *cpa, int p)
static void alloc_child_particles(ParticleSystem *psys, int tot)
struct RNG * BLI_rng_new(unsigned int seed)
static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, ParticleSimulationData *sim, int from)
static void distribute_particles_on_dm(ParticleSimulationData *sim, int from)
void BLI_array_randomize(void *data, unsigned int elem_size, unsigned int elem_num, unsigned int seed)
void BLI_task_pool_work_and_wait(TaskPool *pool)
MINLINE void zero_v4(float r[4])
KDTree< CoordT > * kdtree_new(uint nodes_len_capacity)
static void distribute_particles_on_shape(ParticleSimulationData *sim, int)
int kdtree_find_nearest_n(const KDTree< CoordT > *tree, const CoordT &co, KDTreeNearest< CoordT > r_nearest[], uint nearest_len_capacity)
void psys_particle_on_dm(struct Mesh *mesh_final, int from, int index, int index_dmcache, const float fw[4], float foffset, float vec[3], float nor[3], float utan[3], float vtan[3], float orco[3])
MINLINE int axis_dominant_v3_single(const float vec[3])
void kdtree_insert(KDTree< CoordT > *tree, int index, const CoordT &co)
void BKE_mesh_texspace_ensure(Mesh *mesh)
void BLI_jitterate1(float(*jit1)[2], float(*jit2)[2], int num, float radius1)
MINLINE void negate_v3(float r[3])
static int distribute_binary_search(const float *sum, int n, float value)
void isect_ray_tri_watertight_v3_precalc(struct IsectRayPrecalc *isect_precalc, const float ray_direction[3])
void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1)
static int distribute_compare_orig_index(const void *p1, const void *p2, void *user_data)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
ATTR_WARN_UNUSED_RESULT const size_t num
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
void interp_weights_poly_v3(float w[], float v[][3], int n, const float co[3])
static void distribute_from_faces_exec(ParticleTask *thread, ParticleData *pa, int p)
bool isect_ray_tri_v3(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2])
MINLINE void zero_v3(float r[3])
void BKE_id_free(Main *bmain, void *idv)
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
void BKE_mesh_orco_verts_transform(Mesh *mesh, MutableSpan< float3 > orco, bool invert)
void BLI_task_pool_free(TaskPool *pool)
void distribute_particles(struct ParticleSimulationData *sim, int from)
void BKE_mesh_orco_ensure(Object *ob, Mesh *mesh)
float BLI_rng_get_float(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void psys_calc_dmcache(struct Object *ob, struct Mesh *mesh_final, struct Mesh *mesh_original, struct ParticleSystem *psys)
static void distribute_from_volume_exec(ParticleTask *thread, ParticleData *pa, int p)
TaskPool * BLI_task_pool_create(void *userdata, eTaskPriority priority)
MINLINE float normalize_v3(float n[3])
void kdtree_free(KDTree< CoordT > *tree)
static void distribute_grid(Mesh *mesh, ParticleSystem *psys)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
int psys_get_tot_child(struct Scene *scene, struct ParticleSystem *psys, bool use_render_params)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
void BLI_qsort_r(void *a, size_t n, size_t es, BLI_sort_cmp_t cmp, void *thunk)
float area_tri_v3(const float v1[3], const float v2[3], const float v3[3])
void psys_thread_context_free(struct ParticleThreadContext *ctx)
static void psys_uv_to_w(float u, float v, int quad, float *w)
void BKE_mesh_tessface_ensure(Mesh *mesh)
void void void BLI_rng_skip(struct RNG *rng, int n) ATTR_NONNULL(1)
static void exec_distribute_child(TaskPool *__restrict, void *taskdata)
void psys_tasks_free(Vector< ParticleTask > &tasks)
void * CustomData_get_layer_for_write(CustomData *data, eCustomDataType type, int totelem)
ID * BKE_id_copy_ex(Main *bmain, const ID *id, ID **new_id_p, int flag)
void BLI_hammersley_1d(unsigned int n, double *r)
static void distribute_invalid(ParticleSimulationData *sim, int from)
BLI_INLINE float psys_frand(ParticleSystem *psys, unsigned int seed)
static void hammersley_create(float *out, int n, int seed, float amount)
void BLI_task_pool_push(TaskPool *pool, TaskRunFunction run, void *taskdata, bool free_taskdata, TaskFreeFunction freedata)
int psys_get_child_number(struct Scene *scene, struct ParticleSystem *psys, bool use_render_params)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
#define PSYS_RND_DIST_SKIP
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
bke::MeshRuntime * runtime
eParticleDistribution distr
eParticleChildType childtype
struct Depsgraph * depsgraph
struct ParticleSystem * psys
struct ParticleSystemModifierData * psmd
struct Mesh * mesh_original
ParticleThreadContext * ctx
struct ParticleData * tpars
struct ParticleSeam * seams
struct ParticleSimulationData sim