Force Fields: implement early filtering by the Affect flags.

Most fields have Affect Location and Rotation options that switch
off their effect, but they are only checked as the last step after
the force is already computed. It is more efficient to check it
when building the list of field objects, just like zero weight.
It is also possible to check the strength-related fields for 0.

As an aside, this adds Location to Texture fields (they don't
handle rotation) and both Location & Rotation checkboxes to
Fluid Flow. Boid and Curve Guide remain without options for
now as they are completely different from others.

Differential Revision: https://developer.blender.org/D10087
This commit is contained in:
Alexander Gavrilov 2021-01-09 22:24:48 +03:00
parent 64a963486f
commit 78ff852680
9 changed files with 91 additions and 17 deletions

@ -118,6 +118,9 @@ class PHYSICS_PT_field_settings(PhysicButtonsPanel, Panel):
col.prop(field, "strength")
sub = col.column(heading="Affect")
sub.prop(field, "apply_to_location", text="Location")
col = flow.column()
col.prop(field, "texture_nabla")
col.prop(field, "use_object_coords")
@ -128,6 +131,10 @@ class PHYSICS_PT_field_settings(PhysicButtonsPanel, Panel):
col.prop(field, "strength")
col.prop(field, "flow")
sub = col.column(heading="Affect")
sub.prop(field, "apply_to_location", text="Location")
sub.prop(field, "apply_to_rotation", text="Rotation")
col = flow.column()
col.prop(field, "source_object")
col.prop(field, "use_smoke_density")

@ -121,7 +121,8 @@ void BKE_effector_relations_free(struct ListBase *lb);
struct ListBase *BKE_effectors_create(struct Depsgraph *depsgraph,
struct Object *ob_src,
struct ParticleSystem *psys_src,
struct EffectorWeights *weights);
struct EffectorWeights *weights,
bool use_rotation);
void BKE_effectors_apply(struct ListBase *effectors,
struct ListBase *colliders,
struct EffectorWeights *weights,

@ -285,7 +285,7 @@ static int do_step_cloth(
mul_m4_v3(ob->obmat, verts->xconst);
}
effectors = BKE_effectors_create(depsgraph, ob, NULL, clmd->sim_parms->effector_weights);
effectors = BKE_effectors_create(depsgraph, ob, NULL, clmd->sim_parms->effector_weights, false);
if (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_DYNAMIC_BASEMESH) {
cloth_update_verts(ob, clmd, result);

@ -5156,7 +5156,8 @@ static int dynamicPaint_prepareEffectStep(struct Depsgraph *depsgraph,
/* Init force data if required */
if (surface->effect & MOD_DPAINT_EFFECT_DO_DRIP) {
ListBase *effectors = BKE_effectors_create(depsgraph, ob, NULL, surface->effector_weights);
ListBase *effectors = BKE_effectors_create(
depsgraph, ob, NULL, surface->effector_weights, false);
/* allocate memory for force data (dir vector + strength) */
*force = MEM_mallocN(sizeof(float[4]) * sData->total_points, "PaintEffectForces");

@ -270,11 +270,71 @@ void BKE_effector_relations_free(ListBase *lb)
}
}
/* Check that the force field isn't disabled via its flags. */
static bool is_effector_enabled(PartDeflect *pd, bool use_rotation)
{
switch (pd->forcefield) {
case PFIELD_BOID:
case PFIELD_GUIDE:
return true;
case PFIELD_TEXTURE:
return (pd->flag & PFIELD_DO_LOCATION) != 0 && pd->tex != NULL;
default:
if (use_rotation) {
return (pd->flag & (PFIELD_DO_LOCATION | PFIELD_DO_ROTATION)) != 0;
}
else {
return (pd->flag & PFIELD_DO_LOCATION) != 0;
}
}
}
/* Check that the force field won't have zero effect due to strength settings. */
static bool is_effector_nonzero_strength(PartDeflect *pd)
{
if (pd->f_strength != 0.0f) {
return true;
}
if (pd->forcefield == PFIELD_TEXTURE) {
return false;
}
if (pd->f_noise > 0.0f || pd->f_flow != 0.0f) {
return true;
}
switch (pd->forcefield) {
case PFIELD_BOID:
case PFIELD_GUIDE:
return true;
case PFIELD_VORTEX:
return pd->shape != PFIELD_SHAPE_POINT;
case PFIELD_DRAG:
return pd->f_damp != 0.0f;
default:
return false;
}
}
/* Check if the force field will affect its user. */
static bool is_effector_relevant(PartDeflect *pd, EffectorWeights *weights, bool use_rotation)
{
return (weights->weight[pd->forcefield] != 0.0f) && is_effector_enabled(pd, use_rotation) &&
is_effector_nonzero_strength(pd);
}
/* Create effective list of effectors from relations built beforehand. */
ListBase *BKE_effectors_create(Depsgraph *depsgraph,
Object *ob_src,
ParticleSystem *psys_src,
EffectorWeights *weights)
EffectorWeights *weights,
bool use_rotation)
{
Scene *scene = DEG_get_evaluated_scene(depsgraph);
ListBase *relations = DEG_get_effector_relations(depsgraph, weights->group);
@ -299,7 +359,8 @@ ListBase *BKE_effectors_create(Depsgraph *depsgraph,
}
PartDeflect *pd = (relation->pd == relation->psys->part->pd) ? part->pd : part->pd2;
if (weights->weight[pd->forcefield] == 0.0f) {
if (!is_effector_relevant(pd, weights, use_rotation)) {
continue;
}
@ -310,7 +371,7 @@ ListBase *BKE_effectors_create(Depsgraph *depsgraph,
if (ob == ob_src) {
continue;
}
if (weights->weight[ob->pd->forcefield] == 0.0f) {
if (!is_effector_relevant(ob->pd, weights, use_rotation)) {
continue;
}
if (ob->pd->shape == PFIELD_SHAPE_POINTS && BKE_object_get_evaluated_mesh(ob) == NULL) {
@ -903,7 +964,9 @@ static void do_texture_effector(EffectorCache *eff,
madd_v3_v3fl(force, efd->nor, fac);
}
add_v3_v3(total_force, force);
if (eff->pd->flag & PFIELD_DO_LOCATION) {
add_v3_v3(total_force, force);
}
}
static void do_physical_effector(EffectorCache *eff,
EffectorData *efd,
@ -918,6 +981,7 @@ static void do_physical_effector(EffectorCache *eff,
float strength = pd->f_strength;
float damp = pd->f_damp;
float noise_factor = pd->f_noise;
float flow_falloff = efd->falloff;
if (noise_factor > 0.0f) {
strength += wind_func(rng, noise_factor);
@ -1027,6 +1091,7 @@ static void do_physical_effector(EffectorCache *eff,
break;
case PFIELD_FLUIDFLOW:
zero_v3(force);
flow_falloff = 0;
#ifdef WITH_FLUID
if (pd->f_source) {
float density;
@ -1036,8 +1101,7 @@ static void do_physical_effector(EffectorCache *eff,
influence *= density;
}
mul_v3_fl(force, influence);
/* apply flow */
madd_v3_v3fl(total_force, point->vel, -pd->f_flow * influence);
flow_falloff = influence;
}
}
#endif
@ -1047,9 +1111,8 @@ static void do_physical_effector(EffectorCache *eff,
if (pd->flag & PFIELD_DO_LOCATION) {
madd_v3_v3fl(total_force, force, 1.0f / point->vel_to_sec);
if (ELEM(pd->forcefield, PFIELD_HARMONIC, PFIELD_DRAG, PFIELD_FLUIDFLOW) == 0 &&
pd->f_flow != 0.0f) {
madd_v3_v3fl(total_force, point->vel, -pd->f_flow * efd->falloff);
if (!ELEM(pd->forcefield, PFIELD_HARMONIC, PFIELD_DRAG) && pd->f_flow != 0.0f) {
madd_v3_v3fl(total_force, point->vel, -pd->f_flow * flow_falloff);
}
}

@ -3225,7 +3225,7 @@ static void update_effectors(
ListBase *effectors;
/* make sure smoke flow influence is 0.0f */
fds->effector_weights->weight[PFIELD_FLUIDFLOW] = 0.0f;
effectors = BKE_effectors_create(depsgraph, ob, NULL, fds->effector_weights);
effectors = BKE_effectors_create(depsgraph, ob, NULL, fds->effector_weights, false);
if (effectors) {
/* Precalculate wind forces. */

@ -1400,8 +1400,9 @@ void psys_update_particle_tree(ParticleSystem *psys, float cfra)
static void psys_update_effectors(ParticleSimulationData *sim)
{
BKE_effectors_free(sim->psys->effectors);
bool use_rotation = (sim->psys->part->flag & PART_ROT_DYN) != 0;
sim->psys->effectors = BKE_effectors_create(
sim->depsgraph, sim->ob, sim->psys, sim->psys->part->effector_weights);
sim->depsgraph, sim->ob, sim->psys, sim->psys->part->effector_weights, use_rotation);
precalc_guides(sim, sim->psys->effectors);
}

@ -1720,7 +1720,7 @@ static void rigidbody_update_sim_ob(
ListBase *effectors;
/* get effectors present in the group specified by effector_weights */
effectors = BKE_effectors_create(depsgraph, ob, NULL, effector_weights);
effectors = BKE_effectors_create(depsgraph, ob, NULL, effector_weights, false);
if (effectors) {
float eff_force[3] = {0.0f, 0.0f, 0.0f};
float eff_loc[3], eff_vel[3];

@ -1539,7 +1539,8 @@ static void sb_sfesf_threads_run(struct Depsgraph *depsgraph,
* or even be UI option sb->spawn_cf_threads_nopts */
int lowsprings = 100;
ListBase *effectors = BKE_effectors_create(depsgraph, ob, NULL, ob->soft->effector_weights);
ListBase *effectors = BKE_effectors_create(
depsgraph, ob, NULL, ob->soft->effector_weights, false);
/* figure the number of threads while preventing pretty pointless threading overhead */
totthread = BKE_scene_num_threads(scene);
@ -2300,7 +2301,7 @@ static void softbody_calc_forces(
}
/* after spring scan because it uses Effoctors too */
ListBase *effectors = BKE_effectors_create(depsgraph, ob, NULL, sb->effector_weights);
ListBase *effectors = BKE_effectors_create(depsgraph, ob, NULL, sb->effector_weights, false);
if (do_deflector) {
float defforce[3];