2014-10-28 16:29:33 +01:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2007 by Janne Karhu.
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup bke
|
2014-10-28 16:29:33 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
#include "BLI_utildefines.h"
|
2018-02-18 21:27:33 +11:00
|
|
|
#include "BLI_jitter_2d.h"
|
2014-10-28 16:29:33 +01:00
|
|
|
#include "BLI_kdtree.h"
|
|
|
|
#include "BLI_math.h"
|
2017-09-14 19:43:00 +05:00
|
|
|
#include "BLI_math_geom.h"
|
2014-10-28 16:29:33 +01:00
|
|
|
#include "BLI_rand.h"
|
|
|
|
#include "BLI_sort.h"
|
|
|
|
#include "BLI_task.h"
|
|
|
|
|
|
|
|
#include "DNA_mesh_types.h"
|
|
|
|
#include "DNA_meshdata_types.h"
|
|
|
|
#include "DNA_modifier_types.h"
|
|
|
|
#include "DNA_particle_types.h"
|
|
|
|
#include "DNA_scene_types.h"
|
|
|
|
|
|
|
|
#include "BKE_global.h"
|
2018-05-15 13:26:40 +02:00
|
|
|
#include "BKE_library.h"
|
2014-10-28 16:29:33 +01:00
|
|
|
#include "BKE_mesh.h"
|
|
|
|
#include "BKE_object.h"
|
|
|
|
#include "BKE_particle.h"
|
|
|
|
|
2018-04-08 09:28:52 +02:00
|
|
|
#include "DEG_depsgraph_query.h"
|
2014-10-28 17:16:59 +01:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
static void alloc_child_particles(ParticleSystem *psys, int tot)
|
|
|
|
{
|
|
|
|
if (psys->child) {
|
|
|
|
/* only re-allocate if we have to */
|
|
|
|
if (psys->part->childtype && psys->totchild == tot) {
|
|
|
|
memset(psys->child, 0, tot*sizeof(ChildParticle));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MEM_freeN(psys->child);
|
|
|
|
psys->child=NULL;
|
|
|
|
psys->totchild=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (psys->part->childtype) {
|
|
|
|
psys->totchild= tot;
|
|
|
|
if (psys->totchild)
|
|
|
|
psys->child= MEM_callocN(psys->totchild*sizeof(ChildParticle), "child_particles");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
static void distribute_simple_children(Scene *scene, Object *ob, Mesh *final_mesh, Mesh *deform_mesh, ParticleSystem *psys, const bool use_render_params)
|
2014-10-28 16:29:33 +01:00
|
|
|
{
|
|
|
|
ChildParticle *cpa = NULL;
|
|
|
|
int i, p;
|
2018-04-08 09:28:52 +02:00
|
|
|
int child_nbr= psys_get_child_number(scene, psys, use_render_params);
|
|
|
|
int totpart= psys_get_tot_child(scene, psys, use_render_params);
|
2018-06-12 14:20:46 +02:00
|
|
|
RNG *rng = BLI_rng_new_srandom(31415926 + psys->seed + psys->child_seed);
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
alloc_child_particles(psys, totpart);
|
|
|
|
|
|
|
|
cpa = psys->child;
|
|
|
|
for (i=0; i<child_nbr; i++) {
|
|
|
|
for (p=0; p<psys->totpart; p++,cpa++) {
|
|
|
|
float length=2.0;
|
|
|
|
cpa->parent=p;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
/* create even spherical distribution inside unit sphere */
|
|
|
|
while (length>=1.0f) {
|
2018-06-12 14:20:46 +02:00
|
|
|
cpa->fuv[0]=2.0f*BLI_rng_get_float(rng)-1.0f;
|
|
|
|
cpa->fuv[1]=2.0f*BLI_rng_get_float(rng)-1.0f;
|
|
|
|
cpa->fuv[2]=2.0f*BLI_rng_get_float(rng)-1.0f;
|
2014-10-28 16:29:33 +01:00
|
|
|
length=len_v3(cpa->fuv);
|
|
|
|
}
|
|
|
|
|
|
|
|
cpa->num=-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* dmcache must be updated for parent particles if children from faces is used */
|
2018-05-15 13:26:40 +02:00
|
|
|
psys_calc_dmcache(ob, final_mesh, deform_mesh, psys);
|
2018-06-12 14:20:46 +02:00
|
|
|
|
|
|
|
BLI_rng_free(rng);
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
2018-05-15 13:26:40 +02:00
|
|
|
static void distribute_grid(Mesh *mesh, ParticleSystem *psys)
|
2014-10-28 16:29:33 +01:00
|
|
|
{
|
|
|
|
ParticleData *pa=NULL;
|
|
|
|
float min[3], max[3], delta[3], d;
|
2018-05-15 13:26:40 +02:00
|
|
|
MVert *mv, *mvert = mesh->mvert;
|
|
|
|
int totvert=mesh->totvert, from=psys->part->from;
|
2014-10-28 16:29:33 +01:00
|
|
|
int i, j, k, p, res=psys->part->grid_res, size[3], axis;
|
|
|
|
|
|
|
|
/* find bounding box of dm */
|
|
|
|
if (totvert > 0) {
|
|
|
|
mv=mvert;
|
|
|
|
copy_v3_v3(min, mv->co);
|
|
|
|
copy_v3_v3(max, mv->co);
|
|
|
|
mv++;
|
|
|
|
for (i = 1; i < totvert; i++, mv++) {
|
|
|
|
minmax_v3v3_v3(min, max, mv->co);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
zero_v3(min);
|
|
|
|
zero_v3(max);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub_v3_v3v3(delta, max, min);
|
|
|
|
|
|
|
|
/* determine major axis */
|
|
|
|
axis = axis_dominant_v3_single(delta);
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
d = delta[axis]/(float)res;
|
|
|
|
|
|
|
|
size[axis] = res;
|
|
|
|
size[(axis+1)%3] = (int)ceil(delta[(axis+1)%3]/d);
|
|
|
|
size[(axis+2)%3] = (int)ceil(delta[(axis+2)%3]/d);
|
|
|
|
|
|
|
|
/* float errors grrr.. */
|
|
|
|
size[(axis+1)%3] = MIN2(size[(axis+1)%3],res);
|
|
|
|
size[(axis+2)%3] = MIN2(size[(axis+2)%3],res);
|
|
|
|
|
|
|
|
size[0] = MAX2(size[0], 1);
|
|
|
|
size[1] = MAX2(size[1], 1);
|
|
|
|
size[2] = MAX2(size[2], 1);
|
|
|
|
|
|
|
|
/* no full offset for flat/thin objects */
|
|
|
|
min[0]+= d < delta[0] ? d/2.f : delta[0]/2.f;
|
|
|
|
min[1]+= d < delta[1] ? d/2.f : delta[1]/2.f;
|
|
|
|
min[2]+= d < delta[2] ? d/2.f : delta[2]/2.f;
|
|
|
|
|
|
|
|
for (i=0,p=0,pa=psys->particles; i<res; i++) {
|
|
|
|
for (j=0; j<res; j++) {
|
|
|
|
for (k=0; k<res; k++,p++,pa++) {
|
|
|
|
pa->fuv[0] = min[0] + (float)i*d;
|
|
|
|
pa->fuv[1] = min[1] + (float)j*d;
|
|
|
|
pa->fuv[2] = min[2] + (float)k*d;
|
|
|
|
pa->flag |= PARS_UNEXIST;
|
|
|
|
pa->hair_index = 0; /* abused in volume calculation */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enable particles near verts/edges/faces/inside surface */
|
|
|
|
if (from==PART_FROM_VERT) {
|
|
|
|
float vec[3];
|
|
|
|
|
|
|
|
pa=psys->particles;
|
|
|
|
|
|
|
|
min[0] -= d/2.0f;
|
|
|
|
min[1] -= d/2.0f;
|
|
|
|
min[2] -= d/2.0f;
|
|
|
|
|
|
|
|
for (i=0,mv=mvert; i<totvert; i++,mv++) {
|
|
|
|
sub_v3_v3v3(vec,mv->co,min);
|
|
|
|
vec[0]/=delta[0];
|
|
|
|
vec[1]/=delta[1];
|
|
|
|
vec[2]/=delta[2];
|
|
|
|
pa[((int)(vec[0] * (size[0] - 1)) * res +
|
|
|
|
(int)(vec[1] * (size[1] - 1))) * res +
|
|
|
|
(int)(vec[2] * (size[2] - 1))].flag &= ~PARS_UNEXIST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ELEM(from,PART_FROM_FACE,PART_FROM_VOLUME)) {
|
|
|
|
float co1[3], co2[3];
|
|
|
|
|
|
|
|
MFace *mface= NULL, *mface_array;
|
|
|
|
float v1[3], v2[3], v3[3], v4[4], lambda;
|
|
|
|
int a, a1, a2, a0mul, a1mul, a2mul, totface;
|
|
|
|
int amax= from==PART_FROM_FACE ? 3 : 1;
|
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
totface = mesh->totface;
|
|
|
|
mface = mface_array = mesh->mface;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
for (a=0; a<amax; a++) {
|
|
|
|
if (a==0) { a0mul=res*res; a1mul=res; a2mul=1; }
|
|
|
|
else if (a==1) { a0mul=res; a1mul=1; a2mul=res*res; }
|
|
|
|
else { a0mul=1; a1mul=res*res; a2mul=res; }
|
|
|
|
|
|
|
|
for (a1=0; a1<size[(a+1)%3]; a1++) {
|
|
|
|
for (a2=0; a2<size[(a+2)%3]; a2++) {
|
|
|
|
mface= mface_array;
|
|
|
|
|
|
|
|
pa = psys->particles + a1*a1mul + a2*a2mul;
|
|
|
|
copy_v3_v3(co1, pa->fuv);
|
|
|
|
co1[a] -= d < delta[a] ? d/2.f : delta[a]/2.f;
|
|
|
|
copy_v3_v3(co2, co1);
|
|
|
|
co2[a] += delta[a] + 0.001f*d;
|
|
|
|
co1[a] -= 0.001f*d;
|
2017-09-14 19:43:00 +05:00
|
|
|
|
|
|
|
struct IsectRayPrecalc isect_precalc;
|
|
|
|
float ray_direction[3];
|
|
|
|
sub_v3_v3v3(ray_direction, co2, co1);
|
|
|
|
isect_ray_tri_watertight_v3_precalc(&isect_precalc, ray_direction);
|
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
/* lets intersect the faces */
|
|
|
|
for (i=0; i<totface; i++,mface++) {
|
|
|
|
copy_v3_v3(v1, mvert[mface->v1].co);
|
|
|
|
copy_v3_v3(v2, mvert[mface->v2].co);
|
|
|
|
copy_v3_v3(v3, mvert[mface->v3].co);
|
|
|
|
|
2017-09-14 19:43:00 +05:00
|
|
|
bool intersects_tri = isect_ray_tri_watertight_v3(co1,
|
|
|
|
&isect_precalc,
|
|
|
|
v1, v2, v3,
|
|
|
|
&lambda, NULL);
|
2016-02-02 13:54:53 +01:00
|
|
|
if (intersects_tri) {
|
2014-10-28 16:29:33 +01:00
|
|
|
if (from==PART_FROM_FACE)
|
|
|
|
(pa+(int)(lambda*size[a])*a0mul)->flag &= ~PARS_UNEXIST;
|
|
|
|
else /* store number of intersections */
|
|
|
|
(pa+(int)(lambda*size[a])*a0mul)->hair_index++;
|
|
|
|
}
|
2016-02-02 13:54:53 +01:00
|
|
|
|
|
|
|
if (mface->v4 && (!intersects_tri || from==PART_FROM_VOLUME)) {
|
2014-10-28 16:29:33 +01:00
|
|
|
copy_v3_v3(v4, mvert[mface->v4].co);
|
|
|
|
|
2018-04-16 18:13:48 +02:00
|
|
|
if (isect_ray_tri_watertight_v3(
|
|
|
|
co1,
|
|
|
|
&isect_precalc,
|
|
|
|
v1, v3, v4,
|
|
|
|
&lambda, NULL))
|
|
|
|
{
|
2014-10-28 16:29:33 +01:00
|
|
|
if (from==PART_FROM_FACE)
|
|
|
|
(pa+(int)(lambda*size[a])*a0mul)->flag &= ~PARS_UNEXIST;
|
|
|
|
else
|
|
|
|
(pa+(int)(lambda*size[a])*a0mul)->hair_index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (from==PART_FROM_VOLUME) {
|
|
|
|
int in=pa->hair_index%2;
|
|
|
|
if (in) pa->hair_index++;
|
|
|
|
for (i=0; i<size[0]; i++) {
|
|
|
|
if (in || (pa+i*a0mul)->hair_index%2)
|
|
|
|
(pa+i*a0mul)->flag &= ~PARS_UNEXIST;
|
|
|
|
/* odd intersections == in->out / out->in */
|
|
|
|
/* even intersections -> in stays same */
|
|
|
|
in=(in + (pa+i*a0mul)->hair_index) % 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (psys->part->flag & PART_GRID_HEXAGONAL) {
|
|
|
|
for (i=0,p=0,pa=psys->particles; i<res; i++) {
|
|
|
|
for (j=0; j<res; j++) {
|
|
|
|
for (k=0; k<res; k++,p++,pa++) {
|
|
|
|
if (j%2)
|
|
|
|
pa->fuv[0] += d/2.f;
|
|
|
|
|
|
|
|
if (k%2) {
|
|
|
|
pa->fuv[0] += d/2.f;
|
|
|
|
pa->fuv[1] += d/2.f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (psys->part->flag & PART_GRID_INVERT) {
|
|
|
|
for (i=0; i<size[0]; i++) {
|
|
|
|
for (j=0; j<size[1]; j++) {
|
|
|
|
pa=psys->particles + res*(i*res + j);
|
|
|
|
for (k=0; k<size[2]; k++, pa++) {
|
|
|
|
pa->flag ^= PARS_UNEXIST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (psys->part->grid_rand > 0.f) {
|
|
|
|
float rfac = d * psys->part->grid_rand;
|
|
|
|
for (p=0,pa=psys->particles; p<psys->totpart; p++,pa++) {
|
|
|
|
if (pa->flag & PARS_UNEXIST)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pa->fuv[0] += rfac * (psys_frand(psys, p + 31) - 0.5f);
|
|
|
|
pa->fuv[1] += rfac * (psys_frand(psys, p + 32) - 0.5f);
|
|
|
|
pa->fuv[2] += rfac * (psys_frand(psys, p + 33) - 0.5f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* modified copy from rayshade.c */
|
|
|
|
static void hammersley_create(float *out, int n, int seed, float amount)
|
|
|
|
{
|
|
|
|
RNG *rng;
|
2017-09-26 21:38:23 +02:00
|
|
|
|
|
|
|
double offs[2], t;
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
rng = BLI_rng_new(31415926 + n + seed);
|
|
|
|
offs[0] = BLI_rng_get_double(rng) + (double)amount;
|
|
|
|
offs[1] = BLI_rng_get_double(rng) + (double)amount;
|
|
|
|
BLI_rng_free(rng);
|
|
|
|
|
2017-09-26 21:38:23 +02:00
|
|
|
for (int k = 0; k < n; k++) {
|
|
|
|
BLI_hammersley_1D(k, &t);
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
out[2*k + 0] = fmod((double)k/(double)n + offs[0], 1.0);
|
|
|
|
out[2*k + 1] = fmod(t + offs[1], 1.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* almost exact copy of BLI_jitter_init */
|
|
|
|
static void init_mv_jit(float *jit, int num, int seed2, float amount)
|
|
|
|
{
|
|
|
|
RNG *rng;
|
|
|
|
float *jit2, x, rad1, rad2, rad3;
|
|
|
|
int i, num2;
|
|
|
|
|
|
|
|
if (num==0) return;
|
|
|
|
|
|
|
|
rad1= (float)(1.0f/sqrtf((float)num));
|
|
|
|
rad2= (float)(1.0f/((float)num));
|
|
|
|
rad3= (float)sqrtf((float)num)/((float)num);
|
|
|
|
|
|
|
|
rng = BLI_rng_new(31415926 + num + seed2);
|
|
|
|
x= 0;
|
2015-05-17 16:47:44 +10:00
|
|
|
num2 = 2 * num;
|
2014-10-28 16:29:33 +01:00
|
|
|
for (i=0; i<num2; i+=2) {
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
jit[i] = x + amount*rad1*(0.5f - BLI_rng_get_float(rng));
|
|
|
|
jit[i+1] = i/(2.0f*num) + amount*rad1*(0.5f - BLI_rng_get_float(rng));
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
jit[i]-= (float)floor(jit[i]);
|
|
|
|
jit[i+1]-= (float)floor(jit[i+1]);
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
x+= rad3;
|
|
|
|
x -= (float)floor(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
jit2= MEM_mallocN(12 + 2*sizeof(float)*num, "initjit");
|
|
|
|
|
|
|
|
for (i=0 ; i<4 ; i++) {
|
|
|
|
BLI_jitterate1((float (*)[2])jit, (float (*)[2])jit2, num, rad1);
|
|
|
|
BLI_jitterate1((float (*)[2])jit, (float (*)[2])jit2, num, rad1);
|
|
|
|
BLI_jitterate2((float (*)[2])jit, (float (*)[2])jit2, num, rad2);
|
|
|
|
}
|
|
|
|
MEM_freeN(jit2);
|
|
|
|
BLI_rng_free(rng);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void psys_uv_to_w(float u, float v, int quad, float *w)
|
|
|
|
{
|
|
|
|
float vert[4][3], co[3];
|
|
|
|
|
|
|
|
if (!quad) {
|
|
|
|
if (u+v > 1.0f)
|
|
|
|
v= 1.0f-v;
|
|
|
|
else
|
|
|
|
u= 1.0f-u;
|
|
|
|
}
|
|
|
|
|
|
|
|
vert[0][0] = 0.0f; vert[0][1] = 0.0f; vert[0][2] = 0.0f;
|
|
|
|
vert[1][0] = 1.0f; vert[1][1] = 0.0f; vert[1][2] = 0.0f;
|
|
|
|
vert[2][0] = 1.0f; vert[2][1] = 1.0f; vert[2][2] = 0.0f;
|
|
|
|
|
|
|
|
co[0] = u;
|
|
|
|
co[1] = v;
|
|
|
|
co[2] = 0.0f;
|
|
|
|
|
|
|
|
if (quad) {
|
|
|
|
vert[3][0] = 0.0f; vert[3][1] = 1.0f; vert[3][2] = 0.0f;
|
|
|
|
interp_weights_poly_v3( w,vert, 4, co);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
interp_weights_poly_v3( w,vert, 3, co);
|
|
|
|
w[3] = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the index in "sum" array before "value" is crossed. */
|
|
|
|
static int distribute_binary_search(float *sum, int n, float value)
|
|
|
|
{
|
2016-04-30 16:51:06 +02:00
|
|
|
int mid, low = 0, high = n - 1;
|
2014-10-28 16:29:33 +01:00
|
|
|
|
2016-06-11 14:37:47 +02:00
|
|
|
if (high == low)
|
|
|
|
return low;
|
|
|
|
|
2016-04-30 16:51:06 +02:00
|
|
|
if (sum[low] >= value)
|
|
|
|
return low;
|
|
|
|
|
2016-06-11 14:37:47 +02:00
|
|
|
if (sum[high - 1] < value)
|
2016-04-30 16:51:06 +02:00
|
|
|
return high;
|
2014-10-28 16:29:33 +01:00
|
|
|
|
2016-04-30 16:51:06 +02:00
|
|
|
while (low < high) {
|
|
|
|
mid = (low + high) / 2;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2016-06-11 14:37:47 +02:00
|
|
|
if ((sum[mid] >= value) && (sum[mid - 1] < value))
|
2014-10-28 16:29:33 +01:00
|
|
|
return mid;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2016-06-11 14:37:47 +02:00
|
|
|
if (sum[mid] > value) {
|
2016-06-20 11:07:36 +02:00
|
|
|
high = mid - 1;
|
2016-04-30 16:51:06 +02:00
|
|
|
}
|
2016-06-11 14:37:47 +02:00
|
|
|
else {
|
2016-06-20 11:07:36 +02:00
|
|
|
low = mid + 1;
|
2016-04-30 16:51:06 +02:00
|
|
|
}
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return low;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the max number if calls to rng_* funcs within psys_thread_distribute_particle
|
|
|
|
* be sure to keep up to date if this changes */
|
2018-06-12 15:26:37 +02:00
|
|
|
#define PSYS_RND_DIST_SKIP 3
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
/* note: this function must be thread safe, for from == PART_FROM_CHILD */
|
|
|
|
#define ONLY_WORKING_WITH_PA_VERTS 0
|
2014-10-28 16:45:23 +01:00
|
|
|
static void distribute_from_verts_exec(ParticleTask *thread, ParticleData *pa, int p)
|
2014-10-28 16:29:33 +01:00
|
|
|
{
|
|
|
|
ParticleThreadContext *ctx= thread->ctx;
|
2017-07-05 12:23:42 +02:00
|
|
|
MFace *mface;
|
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
mface = ctx->mesh->mface;
|
2017-07-05 12:23:42 +02:00
|
|
|
|
|
|
|
int rng_skip_tot = PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */
|
2014-10-28 16:29:33 +01:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
/* TODO_PARTICLE - use original index */
|
2017-07-05 12:23:42 +02:00
|
|
|
pa->num = ctx->index[p];
|
|
|
|
|
|
|
|
zero_v4(pa->fuv);
|
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
if (pa->num != DMCACHE_NOTFOUND && pa->num < ctx->mesh->totvert) {
|
2017-07-05 16:16:57 +02:00
|
|
|
|
|
|
|
/* This finds the first face to contain the emitting vertex,
|
|
|
|
* this is not ideal, but is mostly fine as UV seams generally
|
|
|
|
* map to equal-colored parts of a texture */
|
2018-05-15 13:26:40 +02:00
|
|
|
for (int i = 0; i < ctx->mesh->totface; i++, mface++) {
|
2017-07-05 12:23:42 +02:00
|
|
|
if (ELEM(pa->num, mface->v1, mface->v2, mface->v3, mface->v4)) {
|
|
|
|
unsigned int *vert = &mface->v1;
|
|
|
|
|
|
|
|
for (int j = 0; j < 4; j++, vert++) {
|
|
|
|
if (*vert == pa->num) {
|
|
|
|
pa->fuv[j] = 1.0f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
#if ONLY_WORKING_WITH_PA_VERTS
|
2014-10-28 16:45:23 +01:00
|
|
|
if (ctx->tree) {
|
2019-03-20 00:46:33 +11:00
|
|
|
KDTreeNearest_3d ptn[3];
|
2014-10-28 16:45:23 +01:00
|
|
|
int w, maxw;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
psys_particle_on_dm(ctx->mesh,from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co1,0,0,0,orco1,0);
|
2018-10-23 10:53:40 +11:00
|
|
|
BKE_mesh_orco_verts_transform(ob->data, &orco1, 1, 1);
|
2019-03-20 00:46:33 +11:00
|
|
|
maxw = BLI_kdtree_3d_find_nearest_n(ctx->tree,orco1,ptn,3);
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
for (w=0; w<maxw; w++) {
|
|
|
|
pa->verts[w]=ptn->num;
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
|
|
|
}
|
2014-10-28 16:45:23 +01:00
|
|
|
#endif
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2018-11-25 14:21:02 +01:00
|
|
|
BLI_assert(rng_skip_tot >= 0); /* should never be below zero */
|
2018-06-12 15:26:37 +02:00
|
|
|
if (rng_skip_tot > 0) {
|
2014-10-28 16:45:23 +01:00
|
|
|
BLI_rng_skip(thread->rng, rng_skip_tot);
|
2018-06-12 15:26:37 +02:00
|
|
|
}
|
2014-10-28 16:45:23 +01:00
|
|
|
}
|
2014-10-28 16:29:33 +01:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
static void distribute_from_faces_exec(ParticleTask *thread, ParticleData *pa, int p) {
|
|
|
|
ParticleThreadContext *ctx= thread->ctx;
|
2018-05-15 13:26:40 +02:00
|
|
|
Mesh *mesh = ctx->mesh;
|
2014-10-28 16:45:23 +01:00
|
|
|
float randu, randv;
|
|
|
|
int distr= ctx->distr;
|
|
|
|
int i;
|
|
|
|
int rng_skip_tot= PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */
|
|
|
|
|
|
|
|
MFace *mface;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
pa->num = i = ctx->index[p];
|
2018-05-15 13:26:40 +02:00
|
|
|
mface = &mesh->mface[i];
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
switch (distr) {
|
2014-10-28 16:29:33 +01:00
|
|
|
case PART_DISTR_JIT:
|
|
|
|
if (ctx->jitlevel == 1) {
|
|
|
|
if (mface->v4)
|
|
|
|
psys_uv_to_w(0.5f, 0.5f, mface->v4, pa->fuv);
|
|
|
|
else
|
|
|
|
psys_uv_to_w(1.0f / 3.0f, 1.0f / 3.0f, mface->v4, pa->fuv);
|
|
|
|
}
|
|
|
|
else {
|
2015-03-13 13:06:40 +01:00
|
|
|
float offset = fmod(ctx->jitoff[i] + (float)p, (float)ctx->jitlevel);
|
|
|
|
if (!isnan(offset)) {
|
|
|
|
psys_uv_to_w(ctx->jit[2*(int)offset], ctx->jit[2*(int)offset+1], mface->v4, pa->fuv);
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PART_DISTR_RAND:
|
|
|
|
randu= BLI_rng_get_float(thread->rng);
|
|
|
|
randv= BLI_rng_get_float(thread->rng);
|
|
|
|
rng_skip_tot -= 2;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
psys_uv_to_w(randu, randv, mface->v4, pa->fuv);
|
|
|
|
break;
|
2014-10-28 16:45:23 +01:00
|
|
|
}
|
|
|
|
pa->foffset= 0.0f;
|
2018-06-12 15:26:37 +02:00
|
|
|
|
2018-11-25 14:21:02 +01:00
|
|
|
BLI_assert(rng_skip_tot >= 0); /* should never be below zero */
|
2018-06-12 15:26:37 +02:00
|
|
|
if (rng_skip_tot > 0) {
|
2014-10-28 16:45:23 +01:00
|
|
|
BLI_rng_skip(thread->rng, rng_skip_tot);
|
2018-06-12 15:26:37 +02:00
|
|
|
}
|
2014-10-28 16:45:23 +01:00
|
|
|
}
|
2014-10-28 16:29:33 +01:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
static void distribute_from_volume_exec(ParticleTask *thread, ParticleData *pa, int p) {
|
|
|
|
ParticleThreadContext *ctx= thread->ctx;
|
2018-05-15 13:26:40 +02:00
|
|
|
Mesh *mesh = ctx->mesh;
|
2015-05-05 21:39:34 +02:00
|
|
|
float *v1, *v2, *v3, *v4, nor[3], co[3];
|
2014-10-28 16:45:23 +01:00
|
|
|
float cur_d, min_d, randu, randv;
|
|
|
|
int distr= ctx->distr;
|
|
|
|
int i, intersect, tot;
|
|
|
|
int rng_skip_tot= PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
MFace *mface;
|
2018-05-15 13:26:40 +02:00
|
|
|
MVert *mvert = mesh->mvert;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
pa->num = i = ctx->index[p];
|
2018-05-15 13:26:40 +02:00
|
|
|
mface = &mesh->mface[i];
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
switch (distr) {
|
|
|
|
case PART_DISTR_JIT:
|
|
|
|
if (ctx->jitlevel == 1) {
|
|
|
|
if (mface->v4)
|
|
|
|
psys_uv_to_w(0.5f, 0.5f, mface->v4, pa->fuv);
|
|
|
|
else
|
|
|
|
psys_uv_to_w(1.0f / 3.0f, 1.0f / 3.0f, mface->v4, pa->fuv);
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-03-13 13:06:40 +01:00
|
|
|
float offset = fmod(ctx->jitoff[i] + (float)p, (float)ctx->jitlevel);
|
|
|
|
if (!isnan(offset)) {
|
|
|
|
psys_uv_to_w(ctx->jit[2*(int)offset], ctx->jit[2*(int)offset+1], mface->v4, pa->fuv);
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
|
|
|
}
|
2014-10-28 16:45:23 +01:00
|
|
|
break;
|
|
|
|
case PART_DISTR_RAND:
|
|
|
|
randu= BLI_rng_get_float(thread->rng);
|
|
|
|
randv= BLI_rng_get_float(thread->rng);
|
|
|
|
rng_skip_tot -= 2;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
psys_uv_to_w(randu, randv, mface->v4, pa->fuv);
|
|
|
|
break;
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
2014-10-28 16:45:23 +01:00
|
|
|
pa->foffset= 0.0f;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
/* experimental */
|
2018-05-15 13:26:40 +02:00
|
|
|
tot = mesh->totface;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
Remove Blender Internal and legacy viewport from Blender 2.8.
Brecht authored this commit, but he gave me the honours to actually
do it. Here it goes; Blender Internal. Bye bye, you did great!
* Point density, voxel data, ocean, environment map textures were removed,
as these only worked within BI rendering. Note that the ocean modifier
and the Cycles point density shader node continue to work.
* Dynamic paint using material shading was removed, as this only worked
with BI. If we ever wanted to support this again probably it should go
through the baking API.
* GPU shader export through the Python API was removed. This only worked
for the old BI GLSL shaders, which no longer exists. Doing something
similar for Eevee would be significantly more complicated because it
uses a lot of multiplass rendering and logic outside the shader, it's
probably impractical.
* Collada material import / export code is mostly gone, as it only worked
for BI materials. We need to add Cycles / Eevee material support at some
point.
* The mesh noise operator was removed since it only worked with BI
material texture slots. A displacement modifier can be used instead.
* The delete texture paint slot operator was removed since it only worked
for BI material texture slots. Could be added back with node support.
* Not all legacy viewport features are supported in the new viewport, but
their code was removed. If we need to bring anything back we can look at
older git revisions.
* There is some legacy viewport code that I could not remove yet, and some
that I probably missed.
* Shader node execution code was left mostly intact, even though it is not
used anywhere now. We may eventually use this to replace the texture
nodes with Cycles / Eevee shader nodes.
* The Cycles Bake panel now includes settings for baking multires normal
and displacement maps. The underlying code needs to be merged properly,
and we plan to add back support for multires AO baking and add support
to Cycles baking for features like vertex color, displacement, and other
missing baking features.
* This commit removes DNA and the Python API for BI material, lamp, world
and scene settings. This breaks a lot of addons.
* There is more DNA that can be removed or renamed, where Cycles or Eevee
are reusing some old BI properties but the names are not really correct
anymore.
* Texture slots for materials, lamps and world were removed. They remain
for brushes, particles and freestyle linestyles.
* 'BLENDER_RENDER' remains in the COMPAT_ENGINES of UI panels. Cycles and
other renderers use this to find all panels to show, minus a few panels
that they have their own replacement for.
2018-04-19 17:34:44 +02:00
|
|
|
psys_interpolate_face(mvert,mface,0,0,pa->fuv,co,nor,0,0,0);
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
normalize_v3(nor);
|
2015-05-05 21:39:34 +02:00
|
|
|
negate_v3(nor);
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2015-05-05 21:39:34 +02:00
|
|
|
min_d=FLT_MAX;
|
2014-10-28 16:45:23 +01:00
|
|
|
intersect=0;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
for (i=0, mface=mesh->mface; i<tot; i++,mface++) {
|
2014-10-28 16:45:23 +01:00
|
|
|
if (i==pa->num) continue;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
v1=mvert[mface->v1].co;
|
|
|
|
v2=mvert[mface->v2].co;
|
|
|
|
v3=mvert[mface->v3].co;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2015-08-20 15:09:25 +10:00
|
|
|
if (isect_ray_tri_v3(co, nor, v2, v3, v1, &cur_d, NULL)) {
|
2014-10-28 16:45:23 +01:00
|
|
|
if (cur_d<min_d) {
|
|
|
|
min_d=cur_d;
|
2015-05-05 21:39:34 +02:00
|
|
|
pa->foffset=cur_d*0.5f; /* to the middle of volume */
|
2014-10-28 16:45:23 +01:00
|
|
|
intersect=1;
|
|
|
|
}
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
2014-10-28 16:45:23 +01:00
|
|
|
if (mface->v4) {
|
|
|
|
v4=mvert[mface->v4].co;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2015-08-20 15:09:25 +10:00
|
|
|
if (isect_ray_tri_v3(co, nor, v4, v1, v3, &cur_d, NULL)) {
|
2014-10-28 16:45:23 +01:00
|
|
|
if (cur_d<min_d) {
|
|
|
|
min_d=cur_d;
|
2015-05-05 21:39:34 +02:00
|
|
|
pa->foffset=cur_d*0.5f; /* to the middle of volume */
|
2014-10-28 16:45:23 +01:00
|
|
|
intersect=1;
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
|
|
|
}
|
2014-10-28 16:45:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (intersect==0)
|
|
|
|
pa->foffset=0.0;
|
|
|
|
else {
|
|
|
|
switch (distr) {
|
|
|
|
case PART_DISTR_JIT:
|
|
|
|
pa->foffset *= ctx->jit[p % (2 * ctx->jitlevel)];
|
|
|
|
break;
|
|
|
|
case PART_DISTR_RAND:
|
2018-06-12 15:26:37 +02:00
|
|
|
pa->foffset *= BLI_rng_get_float(thread->rng);
|
|
|
|
rng_skip_tot--;
|
2014-10-28 16:45:23 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2018-11-25 14:21:02 +01:00
|
|
|
BLI_assert(rng_skip_tot >= 0); /* should never be below zero */
|
2018-06-12 15:26:37 +02:00
|
|
|
if (rng_skip_tot > 0) {
|
2014-10-28 16:45:23 +01:00
|
|
|
BLI_rng_skip(thread->rng, rng_skip_tot);
|
2018-06-12 15:26:37 +02:00
|
|
|
}
|
2014-10-28 16:45:23 +01:00
|
|
|
}
|
2014-10-28 16:29:33 +01:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
static void distribute_children_exec(ParticleTask *thread, ChildParticle *cpa, int p) {
|
|
|
|
ParticleThreadContext *ctx= thread->ctx;
|
|
|
|
Object *ob= ctx->sim.ob;
|
2018-05-15 13:26:40 +02:00
|
|
|
Mesh *mesh = ctx->mesh;
|
2014-10-28 16:45:23 +01:00
|
|
|
float orco1[3], co1[3], nor1[3];
|
|
|
|
float randu, randv;
|
|
|
|
int cfrom= ctx->cfrom;
|
|
|
|
int i;
|
|
|
|
int rng_skip_tot= PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
MFace *mf;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
if (ctx->index[p] < 0) {
|
|
|
|
cpa->num=0;
|
|
|
|
cpa->fuv[0]=cpa->fuv[1]=cpa->fuv[2]=cpa->fuv[3]=0.0f;
|
|
|
|
cpa->pa[0]=cpa->pa[1]=cpa->pa[2]=cpa->pa[3]=0;
|
|
|
|
return;
|
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
mf = &mesh->mface[ctx->index[p]];
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
randu= BLI_rng_get_float(thread->rng);
|
|
|
|
randv= BLI_rng_get_float(thread->rng);
|
|
|
|
rng_skip_tot -= 2;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
psys_uv_to_w(randu, randv, mf->v4, cpa->fuv);
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
cpa->num = ctx->index[p];
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
if (ctx->tree) {
|
2019-03-20 00:46:33 +11:00
|
|
|
KDTreeNearest_3d ptn[10];
|
2014-10-28 16:45:23 +01:00
|
|
|
int w,maxw;//, do_seams;
|
|
|
|
float maxd /*, mind,dd */, totw= 0.0f;
|
|
|
|
int parent[10];
|
|
|
|
float pweight[10];
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
psys_particle_on_dm(mesh,cfrom,cpa->num,DMCACHE_ISCHILD,cpa->fuv,cpa->foffset,co1,nor1,NULL,NULL,orco1);
|
2018-10-23 10:53:40 +11:00
|
|
|
BKE_mesh_orco_verts_transform(ob->data, &orco1, 1, 1);
|
2019-03-20 00:46:33 +11:00
|
|
|
maxw = BLI_kdtree_3d_find_nearest_n(ctx->tree,orco1,ptn,3);
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
maxd=ptn[maxw-1].dist;
|
|
|
|
/* mind=ptn[0].dist; */ /* UNUSED */
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
/* the weights here could be done better */
|
|
|
|
for (w=0; w<maxw; w++) {
|
|
|
|
parent[w]=ptn[w].index;
|
|
|
|
pweight[w]=(float)pow(2.0,(double)(-6.0f*ptn[w].dist/maxd));
|
|
|
|
}
|
|
|
|
for (;w<10; w++) {
|
|
|
|
parent[w]=-1;
|
|
|
|
pweight[w]=0.0f;
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
for (w=0,i=0; w<maxw && i<4; w++) {
|
|
|
|
if (parent[w]>=0) {
|
|
|
|
cpa->pa[i]=parent[w];
|
|
|
|
cpa->w[i]=pweight[w];
|
|
|
|
totw+=pweight[w];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (;i<4; i++) {
|
|
|
|
cpa->pa[i]=-1;
|
|
|
|
cpa->w[i]=0.0f;
|
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2015-01-24 03:10:23 +11:00
|
|
|
if (totw > 0.0f) {
|
|
|
|
for (w = 0; w < 4; w++) {
|
|
|
|
cpa->w[w] /= totw;
|
|
|
|
}
|
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:45:23 +01:00
|
|
|
cpa->parent=cpa->pa[0];
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rng_skip_tot > 0) /* should never be below zero */
|
|
|
|
BLI_rng_skip(thread->rng, rng_skip_tot);
|
|
|
|
}
|
|
|
|
|
2015-12-21 13:00:06 +11:00
|
|
|
static void exec_distribute_parent(TaskPool * __restrict UNUSED(pool), void *taskdata, int UNUSED(threadid))
|
2014-10-28 16:29:33 +01:00
|
|
|
{
|
|
|
|
ParticleTask *task = taskdata;
|
|
|
|
ParticleSystem *psys= task->ctx->sim.psys;
|
|
|
|
ParticleData *pa;
|
|
|
|
int p;
|
2015-05-01 19:14:28 +02:00
|
|
|
|
|
|
|
BLI_rng_skip(task->rng, PSYS_RND_DIST_SKIP * task->begin);
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
pa= psys->particles + task->begin;
|
2014-10-28 16:45:23 +01:00
|
|
|
switch (psys->part->from) {
|
|
|
|
case PART_FROM_FACE:
|
|
|
|
for (p = task->begin; p < task->end; ++p, ++pa)
|
|
|
|
distribute_from_faces_exec(task, pa, p);
|
|
|
|
break;
|
|
|
|
case PART_FROM_VOLUME:
|
|
|
|
for (p = task->begin; p < task->end; ++p, ++pa)
|
|
|
|
distribute_from_volume_exec(task, pa, p);
|
|
|
|
break;
|
|
|
|
case PART_FROM_VERT:
|
|
|
|
for (p = task->begin; p < task->end; ++p, ++pa)
|
|
|
|
distribute_from_verts_exec(task, pa, p);
|
|
|
|
break;
|
|
|
|
}
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
|
|
|
|
2015-12-21 13:00:06 +11:00
|
|
|
static void exec_distribute_child(TaskPool * __restrict UNUSED(pool), void *taskdata, int UNUSED(threadid))
|
2014-10-28 16:29:33 +01:00
|
|
|
{
|
|
|
|
ParticleTask *task = taskdata;
|
|
|
|
ParticleSystem *psys = task->ctx->sim.psys;
|
|
|
|
ChildParticle *cpa;
|
|
|
|
int p;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
/* RNG skipping at the beginning */
|
|
|
|
cpa = psys->child;
|
|
|
|
for (p = 0; p < task->begin; ++p, ++cpa) {
|
|
|
|
BLI_rng_skip(task->rng, PSYS_RND_DIST_SKIP);
|
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
for (; p < task->end; ++p, ++cpa) {
|
2014-10-28 16:45:23 +01:00
|
|
|
distribute_children_exec(task, cpa, p);
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int distribute_compare_orig_index(const void *p1, const void *p2, void *user_data)
|
|
|
|
{
|
|
|
|
int *orig_index = (int *) user_data;
|
|
|
|
int index1 = orig_index[*(const int *)p1];
|
|
|
|
int index2 = orig_index[*(const int *)p2];
|
|
|
|
|
|
|
|
if (index1 < index2)
|
|
|
|
return -1;
|
|
|
|
else if (index1 == index2) {
|
|
|
|
/* this pointer comparison appears to make qsort stable for glibc,
|
|
|
|
* and apparently on solaris too, makes the renders reproducible */
|
|
|
|
if (p1 < p2)
|
|
|
|
return -1;
|
|
|
|
else if (p1 == p2)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-04-08 09:28:52 +02:00
|
|
|
static void distribute_invalid(ParticleSimulationData *sim, int from)
|
2014-10-28 16:29:33 +01:00
|
|
|
{
|
2018-04-08 09:28:52 +02:00
|
|
|
Scene *scene = sim->scene;
|
|
|
|
ParticleSystem *psys = sim->psys;
|
2018-04-06 12:07:27 +02:00
|
|
|
const bool use_render_params = (DEG_get_mode(sim->depsgraph) == DAG_EVAL_RENDER);
|
2018-04-08 09:28:52 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
if (from == PART_FROM_CHILD) {
|
|
|
|
ChildParticle *cpa;
|
2018-04-08 09:28:52 +02:00
|
|
|
int p, totchild = psys_get_tot_child(scene, psys, use_render_params);
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
if (psys->child && totchild) {
|
|
|
|
for (p=0,cpa=psys->child; p<totchild; p++,cpa++) {
|
|
|
|
cpa->fuv[0]=cpa->fuv[1]=cpa->fuv[2]=cpa->fuv[3] = 0.0;
|
|
|
|
cpa->foffset= 0.0f;
|
|
|
|
cpa->parent=0;
|
|
|
|
cpa->pa[0]=cpa->pa[1]=cpa->pa[2]=cpa->pa[3]=0;
|
|
|
|
cpa->num= -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PARTICLE_P;
|
|
|
|
LOOP_PARTICLES {
|
|
|
|
pa->fuv[0] = pa->fuv[1] = pa->fuv[2] = pa->fuv[3] = 0.0;
|
|
|
|
pa->foffset= 0.0f;
|
|
|
|
pa->num= -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
/* Creates a distribution of coordinates on a Mesh */
|
2014-10-28 16:29:33 +01:00
|
|
|
static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, ParticleSimulationData *sim, int from)
|
|
|
|
{
|
|
|
|
Scene *scene = sim->scene;
|
2018-05-15 13:26:40 +02:00
|
|
|
Mesh *final_mesh = sim->psmd->mesh_final;
|
2014-10-28 16:29:33 +01:00
|
|
|
Object *ob = sim->ob;
|
|
|
|
ParticleSystem *psys= sim->psys;
|
|
|
|
ParticleData *pa=0, *tpars= 0;
|
|
|
|
ParticleSettings *part;
|
|
|
|
ParticleSeam *seams= 0;
|
2019-03-20 00:46:33 +11:00
|
|
|
KDTree_3d *tree=0;
|
2018-05-15 13:26:40 +02:00
|
|
|
Mesh *mesh = NULL;
|
2014-10-28 16:29:33 +01:00
|
|
|
float *jit= NULL;
|
|
|
|
int i, p=0;
|
|
|
|
int cfrom=0;
|
|
|
|
int totelem=0, totpart, *particle_element=0, children=0, totseam=0;
|
|
|
|
int jitlevel= 1, distr;
|
2016-04-30 16:51:06 +02:00
|
|
|
float *element_weight=NULL,*jitter_offset=NULL, *vweight=NULL;
|
2014-10-28 16:29:33 +01:00
|
|
|
float cur, maxweight=0.0, tweight, totweight, inv_totweight, co[3], nor[3], orco[3];
|
2018-06-12 14:20:46 +02:00
|
|
|
RNG *rng = NULL;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
if (ELEM(NULL, ob, psys, psys->part))
|
|
|
|
return 0;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
part=psys->part;
|
|
|
|
totpart=psys->totpart;
|
|
|
|
if (totpart==0)
|
|
|
|
return 0;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
if (!final_mesh->runtime.deformed_only && !CustomData_get_layer(&final_mesh->fdata, CD_ORIGINDEX)) {
|
2014-10-28 16:29:33 +01:00
|
|
|
printf("Can't create particles with the current modifier stack, disable destructive modifiers\n");
|
|
|
|
// XXX error("Can't paint with the current modifier stack, disable destructive modifiers");
|
|
|
|
return 0;
|
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2016-03-29 17:45:56 +02:00
|
|
|
/* XXX This distribution code is totally broken in case from == PART_FROM_CHILD, it's always using finaldm
|
|
|
|
* even if use_modifier_stack is unset... But making things consistent here break all existing edited
|
|
|
|
* hair systems, so better wait for complete rewrite.
|
|
|
|
*/
|
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
psys_thread_context_init(ctx, sim);
|
2018-04-08 09:28:52 +02:00
|
|
|
|
2018-04-06 12:07:27 +02:00
|
|
|
const bool use_render_params = (DEG_get_mode(sim->depsgraph) == DAG_EVAL_RENDER);
|
2018-06-17 17:10:19 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
/* First handle special cases */
|
|
|
|
if (from == PART_FROM_CHILD) {
|
|
|
|
/* Simple children */
|
|
|
|
if (part->childtype != PART_CHILD_FACES) {
|
2018-05-25 21:44:33 +02:00
|
|
|
distribute_simple_children(scene, ob, final_mesh, sim->psmd->mesh_original, psys, use_render_params);
|
2014-10-28 16:29:33 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Grid distribution */
|
|
|
|
if (part->distr==PART_DISTR_GRID && from != PART_FROM_VERT) {
|
2016-03-29 17:45:56 +02:00
|
|
|
if (psys->part->use_modifier_stack) {
|
2018-05-15 13:26:40 +02:00
|
|
|
mesh = final_mesh;
|
2016-03-29 17:45:56 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-02-04 17:51:15 +01:00
|
|
|
BKE_id_copy_ex(NULL, ob->data, (ID **)&mesh, LIB_ID_COPY_LOCALIZE);
|
2016-03-29 17:45:56 +02:00
|
|
|
}
|
2018-05-15 13:26:40 +02:00
|
|
|
BKE_mesh_tessface_ensure(mesh);
|
2016-03-29 17:45:56 +02:00
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
distribute_grid(mesh,psys);
|
2016-03-29 17:45:56 +02:00
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
if (mesh != final_mesh) {
|
|
|
|
BKE_id_free(NULL, mesh);
|
2016-03-29 17:45:56 +02:00
|
|
|
}
|
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
/* Create trees and original coordinates if needed */
|
|
|
|
if (from == PART_FROM_CHILD) {
|
2018-06-12 14:20:46 +02:00
|
|
|
distr = PART_DISTR_RAND;
|
|
|
|
rng = BLI_rng_new_srandom(31415926 + psys->seed + psys->child_seed);
|
2018-05-15 13:26:40 +02:00
|
|
|
mesh= final_mesh;
|
2016-03-29 17:33:08 +02:00
|
|
|
|
|
|
|
/* BMESH ONLY */
|
2018-05-15 13:26:40 +02:00
|
|
|
BKE_mesh_tessface_ensure(mesh);
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
children=1;
|
|
|
|
|
2019-03-20 00:46:33 +11:00
|
|
|
tree=BLI_kdtree_3d_new(totpart);
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
for (p=0,pa=psys->particles; p<totpart; p++,pa++) {
|
2018-05-15 13:26:40 +02:00
|
|
|
psys_particle_on_dm(mesh,part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co,nor,0,0,orco);
|
2018-10-23 10:53:40 +11:00
|
|
|
BKE_mesh_orco_verts_transform(ob->data, &orco, 1, 1);
|
2019-03-20 00:46:33 +11:00
|
|
|
BLI_kdtree_3d_insert(tree, p, orco);
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
|
|
|
|
2019-03-20 00:46:33 +11:00
|
|
|
BLI_kdtree_3d_balance(tree);
|
2014-10-28 16:29:33 +01:00
|
|
|
|
2018-04-08 09:28:52 +02:00
|
|
|
totpart = psys_get_tot_child(scene, psys, use_render_params);
|
2014-10-28 16:29:33 +01:00
|
|
|
cfrom = from = PART_FROM_FACE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
distr = part->distr;
|
2018-06-12 14:20:46 +02:00
|
|
|
|
|
|
|
rng = BLI_rng_new_srandom(31415926 + psys->seed);
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2016-03-29 17:33:08 +02:00
|
|
|
if (psys->part->use_modifier_stack)
|
2018-05-15 13:26:40 +02:00
|
|
|
mesh = final_mesh;
|
2016-03-29 17:33:08 +02:00
|
|
|
else
|
2019-02-04 17:51:15 +01:00
|
|
|
BKE_id_copy_ex(NULL, ob->data, (ID **)&mesh, LIB_ID_COPY_LOCALIZE);
|
2016-03-29 17:33:08 +02:00
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
BKE_mesh_tessface_ensure(mesh);
|
2016-03-29 17:33:08 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
/* we need orco for consistent distributions */
|
2018-05-15 13:26:40 +02:00
|
|
|
if (!CustomData_has_layer(&mesh->vdata, CD_ORCO))
|
|
|
|
CustomData_add_layer(&mesh->vdata, CD_ORCO, CD_ASSIGN, BKE_mesh_orco_verts_get(ob), mesh->totvert);
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
if (from == PART_FROM_VERT) {
|
2018-05-15 13:26:40 +02:00
|
|
|
MVert *mv = mesh->mvert;
|
|
|
|
float (*orcodata)[3] = CustomData_get_layer(&mesh->vdata, CD_ORCO);
|
|
|
|
int totvert = mesh->totvert;
|
2014-10-28 16:29:33 +01:00
|
|
|
|
2019-03-20 00:46:33 +11:00
|
|
|
tree=BLI_kdtree_3d_new(totvert);
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
for (p=0; p<totvert; p++) {
|
|
|
|
if (orcodata) {
|
|
|
|
copy_v3_v3(co,orcodata[p]);
|
2018-10-23 10:53:40 +11:00
|
|
|
BKE_mesh_orco_verts_transform(ob->data, &co, 1, 1);
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
copy_v3_v3(co,mv[p].co);
|
2019-03-20 00:46:33 +11:00
|
|
|
BLI_kdtree_3d_insert(tree, p, co);
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
|
|
|
|
2019-03-20 00:46:33 +11:00
|
|
|
BLI_kdtree_3d_balance(tree);
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get total number of emission elements and allocate needed arrays */
|
2018-05-15 13:26:40 +02:00
|
|
|
totelem = (from == PART_FROM_VERT) ? mesh->totvert : mesh->totface;
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
if (totelem == 0) {
|
2018-04-08 09:28:52 +02:00
|
|
|
distribute_invalid(sim, children ? PART_FROM_CHILD : 0);
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
if (G.debug & G_DEBUG)
|
|
|
|
fprintf(stderr,"Particle distribution error: Nothing to emit from!\n");
|
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
if (mesh != final_mesh) BKE_id_free(NULL, mesh);
|
2014-10-28 16:29:33 +01:00
|
|
|
|
2019-03-20 00:46:33 +11:00
|
|
|
BLI_kdtree_3d_free(tree);
|
2018-06-12 14:20:46 +02:00
|
|
|
BLI_rng_free(rng);
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-23 11:51:19 +01:00
|
|
|
element_weight = MEM_callocN(sizeof(float) * totelem, "particle_distribution_weights");
|
|
|
|
particle_element = MEM_callocN(sizeof(int) * totpart, "particle_distribution_indexes");
|
|
|
|
jitter_offset = MEM_callocN(sizeof(float) * totelem, "particle_distribution_jitoff");
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
/* Calculate weights from face areas */
|
2018-10-23 10:53:40 +11:00
|
|
|
if ((part->flag & PART_EDISTR || children) && from != PART_FROM_VERT) {
|
2014-10-28 16:29:33 +01:00
|
|
|
MVert *v1, *v2, *v3, *v4;
|
|
|
|
float totarea=0.f, co1[3], co2[3], co3[3], co4[3];
|
|
|
|
float (*orcodata)[3];
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
orcodata = CustomData_get_layer(&mesh->vdata, CD_ORCO);
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
for (i=0; i<totelem; i++) {
|
2018-05-15 13:26:40 +02:00
|
|
|
MFace *mf = &mesh->mface[i];
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
if (orcodata) {
|
|
|
|
copy_v3_v3(co1, orcodata[mf->v1]);
|
|
|
|
copy_v3_v3(co2, orcodata[mf->v2]);
|
|
|
|
copy_v3_v3(co3, orcodata[mf->v3]);
|
2018-10-23 10:53:40 +11:00
|
|
|
BKE_mesh_orco_verts_transform(ob->data, &co1, 1, 1);
|
|
|
|
BKE_mesh_orco_verts_transform(ob->data, &co2, 1, 1);
|
|
|
|
BKE_mesh_orco_verts_transform(ob->data, &co3, 1, 1);
|
2014-10-28 16:29:33 +01:00
|
|
|
if (mf->v4) {
|
|
|
|
copy_v3_v3(co4, orcodata[mf->v4]);
|
2018-10-23 10:53:40 +11:00
|
|
|
BKE_mesh_orco_verts_transform(ob->data, &co4, 1, 1);
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2018-05-15 13:26:40 +02:00
|
|
|
v1 = &mesh->mvert[mf->v1];
|
|
|
|
v2 = &mesh->mvert[mf->v2];
|
|
|
|
v3 = &mesh->mvert[mf->v3];
|
2014-10-28 16:29:33 +01:00
|
|
|
copy_v3_v3(co1, v1->co);
|
|
|
|
copy_v3_v3(co2, v2->co);
|
|
|
|
copy_v3_v3(co3, v3->co);
|
|
|
|
if (mf->v4) {
|
2018-05-15 13:26:40 +02:00
|
|
|
v4 = &mesh->mvert[mf->v4];
|
2014-10-28 16:29:33 +01:00
|
|
|
copy_v3_v3(co4, v4->co);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = mf->v4 ? area_quad_v3(co1, co2, co3, co4) : area_tri_v3(co1, co2, co3);
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
if (cur > maxweight)
|
|
|
|
maxweight = cur;
|
|
|
|
|
|
|
|
element_weight[i] = cur;
|
|
|
|
totarea += cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<totelem; i++)
|
|
|
|
element_weight[i] /= totarea;
|
|
|
|
|
|
|
|
maxweight /= totarea;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
float min=1.0f/(float)(MIN2(totelem,totpart));
|
|
|
|
for (i=0; i<totelem; i++)
|
|
|
|
element_weight[i]=min;
|
|
|
|
maxweight=min;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate weights from vgroup */
|
2018-05-15 13:26:40 +02:00
|
|
|
vweight = psys_cache_vgroup(mesh,psys,PSYS_VG_DENSITY);
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
if (vweight) {
|
|
|
|
if (from==PART_FROM_VERT) {
|
|
|
|
for (i=0;i<totelem; i++)
|
|
|
|
element_weight[i]*=vweight[i];
|
|
|
|
}
|
|
|
|
else { /* PART_FROM_FACE / PART_FROM_VOLUME */
|
|
|
|
for (i=0;i<totelem; i++) {
|
2018-05-15 13:26:40 +02:00
|
|
|
MFace *mf = &mesh->mface[i];
|
2014-10-28 16:29:33 +01:00
|
|
|
tweight = vweight[mf->v1] + vweight[mf->v2] + vweight[mf->v3];
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
if (mf->v4) {
|
|
|
|
tweight += vweight[mf->v4];
|
|
|
|
tweight /= 4.0f;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tweight /= 3.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
element_weight[i]*=tweight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MEM_freeN(vweight);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate total weight of all elements */
|
2016-04-30 16:51:06 +02:00
|
|
|
int totmapped = 0;
|
|
|
|
totweight = 0.0f;
|
|
|
|
for (i = 0; i < totelem; i++) {
|
2016-07-04 16:10:40 +02:00
|
|
|
if (element_weight[i] > 0.0f) {
|
2016-04-30 16:51:06 +02:00
|
|
|
totmapped++;
|
2016-06-20 11:07:36 +02:00
|
|
|
totweight += element_weight[i];
|
2016-04-30 16:51:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 11:07:36 +02:00
|
|
|
if (totmapped == 0) {
|
2016-04-30 16:51:06 +02:00
|
|
|
/* We are not allowed to distribute particles anywhere... */
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-28 16:29:33 +01:00
|
|
|
|
2016-06-20 11:07:36 +02:00
|
|
|
inv_totweight = 1.0f / totweight;
|
2014-10-28 16:29:33 +01:00
|
|
|
|
2016-04-30 16:51:06 +02:00
|
|
|
/* Calculate cumulative weights.
|
|
|
|
* We remove all null-weighted elements from element_sum, and create a new mapping
|
|
|
|
* 'activ'_elem_index -> orig_elem_index.
|
2016-06-11 14:37:47 +02:00
|
|
|
* This simplifies greatly the filtering of zero-weighted items - and can be much more efficient
|
2016-04-30 16:51:06 +02:00
|
|
|
* especially in random case (reducing a lot the size of binary-searched array)...
|
|
|
|
*/
|
|
|
|
float *element_sum = MEM_mallocN(sizeof(*element_sum) * totmapped, __func__);
|
|
|
|
int *element_map = MEM_mallocN(sizeof(*element_map) * totmapped, __func__);
|
|
|
|
int i_mapped = 0;
|
|
|
|
|
2016-07-04 16:10:40 +02:00
|
|
|
for (i = 0; i < totelem && element_weight[i] == 0.0f; i++);
|
2016-04-30 16:51:06 +02:00
|
|
|
element_sum[i_mapped] = element_weight[i] * inv_totweight;
|
|
|
|
element_map[i_mapped] = i;
|
|
|
|
i_mapped++;
|
|
|
|
for (i++; i < totelem; i++) {
|
2016-07-04 16:10:40 +02:00
|
|
|
if (element_weight[i] > 0.0f) {
|
2016-04-30 16:51:06 +02:00
|
|
|
element_sum[i_mapped] = element_sum[i_mapped - 1] + element_weight[i] * inv_totweight;
|
2016-07-04 16:10:40 +02:00
|
|
|
/* Skip elements which weight is so small that it does not affect the sum. */
|
|
|
|
if (element_sum[i_mapped] > element_sum[i_mapped - 1]) {
|
|
|
|
element_map[i_mapped] = i;
|
|
|
|
i_mapped++;
|
|
|
|
}
|
2016-04-30 16:51:06 +02:00
|
|
|
}
|
2016-04-09 22:42:52 +02:00
|
|
|
}
|
2016-07-04 16:10:40 +02:00
|
|
|
totmapped = i_mapped;
|
2016-06-20 11:07:36 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
/* Finally assign elements to particles */
|
2018-04-08 09:28:52 +02:00
|
|
|
if (part->flag & PART_TRAND) {
|
2016-04-30 16:51:06 +02:00
|
|
|
for (p = 0; p < totpart; p++) {
|
2016-06-11 14:37:47 +02:00
|
|
|
/* In theory element_sum[totmapped - 1] should be 1.0,
|
2016-04-30 16:51:06 +02:00
|
|
|
* but due to float errors this is not necessarily always true, so scale pos accordingly. */
|
2018-06-12 14:20:46 +02:00
|
|
|
const float pos = BLI_rng_get_float(rng) * element_sum[totmapped - 1];
|
2016-06-11 14:37:47 +02:00
|
|
|
const int eidx = distribute_binary_search(element_sum, totmapped, pos);
|
|
|
|
particle_element[p] = element_map[eidx];
|
2016-06-16 14:05:53 +02:00
|
|
|
BLI_assert(pos <= element_sum[eidx]);
|
|
|
|
BLI_assert(eidx ? (pos > element_sum[eidx - 1]) : (pos >= 0.0f));
|
2014-10-28 16:29:33 +01:00
|
|
|
jitter_offset[particle_element[p]] = pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
double step, pos;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2016-04-09 22:42:52 +02:00
|
|
|
step = (totpart < 2) ? 0.5 : 1.0 / (double)totpart;
|
2016-04-09 18:59:05 +02:00
|
|
|
/* This is to address tricky issues with vertex-emitting when user tries (and expects) exact 1-1 vert/part
|
|
|
|
* distribution (see T47983 and its two example files). It allows us to consider pos as
|
|
|
|
* 'midpoint between v and v+1' (or 'p and p+1', depending whether we have more vertices than particles or not),
|
2018-02-26 19:58:31 +11:00
|
|
|
* and avoid stumbling over float impression in element_sum.
|
2017-09-11 12:31:33 +02:00
|
|
|
* Note: moved face and volume distribution to this as well (instead of starting at zero),
|
|
|
|
* for the same reasons, see T52682. */
|
|
|
|
pos = (totpart < totmapped) ? 0.5 / (double)totmapped : step * 0.5; /* We choose the smaller step. */
|
2014-10-28 16:29:33 +01:00
|
|
|
|
2016-04-30 16:51:06 +02:00
|
|
|
for (i = 0, p = 0; p < totpart; p++, pos += step) {
|
|
|
|
for ( ; (i < totmapped - 1) && (pos > (double)element_sum[i]); i++);
|
2014-10-28 16:29:33 +01:00
|
|
|
|
2016-04-30 16:51:06 +02:00
|
|
|
particle_element[p] = element_map[i];
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
jitter_offset[particle_element[p]] = pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MEM_freeN(element_sum);
|
2016-04-30 16:51:06 +02:00
|
|
|
MEM_freeN(element_map);
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
/* For hair, sort by origindex (allows optimization's in rendering), */
|
|
|
|
/* however with virtual parents the children need to be in random order. */
|
2017-06-02 15:38:04 +10:00
|
|
|
if (part->type == PART_HAIR && !(part->childtype==PART_CHILD_FACES && part->parents != 0.0f)) {
|
2014-10-28 16:29:33 +01:00
|
|
|
int *orig_index = NULL;
|
|
|
|
|
|
|
|
if (from == PART_FROM_VERT) {
|
2018-05-15 13:26:40 +02:00
|
|
|
if (mesh->totvert)
|
|
|
|
orig_index = CustomData_get_layer(&mesh->vdata, CD_ORIGINDEX);
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
|
|
|
else {
|
2018-05-15 13:26:40 +02:00
|
|
|
if (mesh->totface)
|
|
|
|
orig_index = CustomData_get_layer(&mesh->fdata, CD_ORIGINDEX);
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (orig_index) {
|
|
|
|
BLI_qsort_r(particle_element, totpart, sizeof(int), distribute_compare_orig_index, orig_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create jittering if needed */
|
|
|
|
if (distr==PART_DISTR_JIT && ELEM(from,PART_FROM_FACE,PART_FROM_VOLUME)) {
|
|
|
|
jitlevel= part->userjit;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
if (jitlevel == 0) {
|
|
|
|
jitlevel= totpart/totelem;
|
2017-09-11 12:31:33 +02:00
|
|
|
if (part->flag & PART_EDISTR) jitlevel*= 2; /* looks better in general, not very scientific */
|
2014-10-28 16:29:33 +01:00
|
|
|
if (jitlevel<3) jitlevel= 3;
|
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
jit= MEM_callocN((2+ jitlevel*2)*sizeof(float), "jit");
|
|
|
|
|
|
|
|
/* for small amounts of particles we use regular jitter since it looks
|
2018-06-17 17:05:51 +02:00
|
|
|
* a bit better, for larger amounts we switch to hammersley sequence
|
2014-10-28 16:29:33 +01:00
|
|
|
* because it is much faster */
|
|
|
|
if (jitlevel < 25)
|
|
|
|
init_mv_jit(jit, jitlevel, psys->seed, part->jitfac);
|
|
|
|
else
|
|
|
|
hammersley_create(jit, jitlevel+1, psys->seed, part->jitfac);
|
|
|
|
BLI_array_randomize(jit, 2*sizeof(float), jitlevel, psys->seed); /* for custom jit or even distribution */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup things for threaded distribution */
|
|
|
|
ctx->tree= tree;
|
|
|
|
ctx->seams= seams;
|
|
|
|
ctx->totseam= totseam;
|
|
|
|
ctx->sim.psys= psys;
|
|
|
|
ctx->index= particle_element;
|
|
|
|
ctx->jit= jit;
|
|
|
|
ctx->jitlevel= jitlevel;
|
|
|
|
ctx->jitoff= jitter_offset;
|
|
|
|
ctx->weight= element_weight;
|
|
|
|
ctx->maxweight= maxweight;
|
|
|
|
ctx->cfrom= cfrom;
|
|
|
|
ctx->distr= distr;
|
2018-05-15 13:26:40 +02:00
|
|
|
ctx->mesh= mesh;
|
2014-10-28 16:29:33 +01:00
|
|
|
ctx->tpars= tpars;
|
|
|
|
|
|
|
|
if (children) {
|
|
|
|
alloc_child_particles(psys, totpart);
|
|
|
|
}
|
|
|
|
|
2018-06-12 14:20:46 +02:00
|
|
|
BLI_rng_free(rng);
|
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void psys_task_init_distribute(ParticleTask *task, ParticleSimulationData *sim)
|
|
|
|
{
|
|
|
|
/* init random number generator */
|
|
|
|
int seed = 31415926 + sim->psys->seed;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
task->rng = BLI_rng_new(seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void distribute_particles_on_dm(ParticleSimulationData *sim, int from)
|
|
|
|
{
|
|
|
|
TaskScheduler *task_scheduler;
|
|
|
|
TaskPool *task_pool;
|
|
|
|
ParticleThreadContext ctx;
|
|
|
|
ParticleTask *tasks;
|
2018-05-15 13:26:40 +02:00
|
|
|
Mesh *final_mesh = sim->psmd->mesh_final;
|
2014-10-28 16:29:33 +01:00
|
|
|
int i, totpart, numtasks;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
/* create a task pool for distribution tasks */
|
|
|
|
if (!psys_thread_context_init_distribute(&ctx, sim, from))
|
|
|
|
return;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
task_scheduler = BLI_task_scheduler_get();
|
|
|
|
task_pool = BLI_task_pool_create(task_scheduler, &ctx);
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
totpart = (from == PART_FROM_CHILD ? sim->psys->totchild : sim->psys->totpart);
|
2015-03-25 18:34:52 +01:00
|
|
|
psys_tasks_create(&ctx, 0, totpart, &tasks, &numtasks);
|
2014-10-28 16:29:33 +01:00
|
|
|
for (i = 0; i < numtasks; ++i) {
|
|
|
|
ParticleTask *task = &tasks[i];
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
psys_task_init_distribute(task, sim);
|
|
|
|
if (from == PART_FROM_CHILD)
|
|
|
|
BLI_task_pool_push(task_pool, exec_distribute_child, task, false, TASK_PRIORITY_LOW);
|
|
|
|
else
|
|
|
|
BLI_task_pool_push(task_pool, exec_distribute_parent, task, false, TASK_PRIORITY_LOW);
|
|
|
|
}
|
|
|
|
BLI_task_pool_work_and_wait(task_pool);
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
BLI_task_pool_free(task_pool);
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2018-05-25 21:44:33 +02:00
|
|
|
psys_calc_dmcache(sim->ob, final_mesh, sim->psmd->mesh_original, sim->psys);
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
if (ctx.mesh != final_mesh)
|
|
|
|
BKE_id_free(NULL, ctx.mesh);
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-10-28 16:29:33 +01:00
|
|
|
psys_tasks_free(tasks, numtasks);
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2015-01-08 12:40:27 +01:00
|
|
|
psys_thread_context_free(&ctx);
|
2014-10-28 16:29:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ready for future use, to emit particles without geometry */
|
|
|
|
static void distribute_particles_on_shape(ParticleSimulationData *sim, int UNUSED(from))
|
|
|
|
{
|
2018-04-08 09:28:52 +02:00
|
|
|
distribute_invalid(sim, 0);
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
fprintf(stderr,"Shape emission not yet possible!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void distribute_particles(ParticleSimulationData *sim, int from)
|
|
|
|
{
|
|
|
|
PARTICLE_PSMD;
|
|
|
|
int distr_error=0;
|
|
|
|
|
|
|
|
if (psmd) {
|
2018-05-15 13:26:40 +02:00
|
|
|
if (psmd->mesh_final)
|
2014-10-28 16:29:33 +01:00
|
|
|
distribute_particles_on_dm(sim, from);
|
|
|
|
else
|
|
|
|
distr_error=1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
distribute_particles_on_shape(sim, from);
|
|
|
|
|
|
|
|
if (distr_error) {
|
2018-04-08 09:28:52 +02:00
|
|
|
distribute_invalid(sim, from);
|
2014-10-28 16:29:33 +01:00
|
|
|
|
|
|
|
fprintf(stderr,"Particle distribution error!\n");
|
|
|
|
}
|
|
|
|
}
|