[Bf-blender-cvs] [42318e358a8] blender-v2.82-release: Fluid: Cleanup in flow emission loops

Sebastián Barschkis noreply at git.blender.org
Wed Jan 29 19:22:27 CET 2020


Commit: 42318e358a888d49aeb44198bf5560614d0ca7b3
Author: Sebastián Barschkis
Date:   Mon Jan 27 17:28:07 2020 +0100
Branches: blender-v2.82-release
https://developer.blender.org/rB42318e358a888d49aeb44198bf5560614d0ca7b3

Fluid: Cleanup in flow emission loops

- Initial velocities are no longer influenced by surface distance value.
- Added optimizations for different flow types (e.g. skip part of loop for liquid flow objects).
- Comments style cleanup and removed old todos.

===================================================================

M	source/blender/blenkernel/intern/fluid.c

===================================================================

diff --git a/source/blender/blenkernel/intern/fluid.c b/source/blender/blenkernel/intern/fluid.c
index 3e518cf7ec0..d9827ed9b57 100644
--- a/source/blender/blenkernel/intern/fluid.c
+++ b/source/blender/blenkernel/intern/fluid.c
@@ -630,10 +630,9 @@ static void obstacles_from_mesh_task_cb(void *__restrict userdata,
   ObstaclesFromDMData *data = userdata;
   FluidDomainSettings *mds = data->mds;
 
-  /* slightly rounded-up sqrt(3 * (0.5)^2) == max. distance of cell boundary along the diagonal */
-  const float surface_distance = 2.0f;  // 0.867f;
-  /* Note: Use larger surface distance to cover larger area with obvel. Manta will use these obvels
-   * and extrapolate them (inside and outside obstacle) */
+  /* Distance from unit cube center to one of the vertices.
+   * I.e. half the cube diagonal or sqrt(3) * 0.5. */
+  const float surface_distance = 0.867f;
 
   for (int x = mds->res_min[0]; x < mds->res_max[0]; x++) {
     for (int y = mds->res_min[1]; y < mds->res_max[1]; y++) {
@@ -1123,8 +1122,7 @@ static void em_freeData(EmissionMap *em)
   }
 }
 
-static void em_combineMaps(
-    EmissionMap *output, EmissionMap *em2, int additive, float sample_size)
+static void em_combineMaps(EmissionMap *output, EmissionMap *em2, int additive, float sample_size)
 {
   int i, x, y, z;
 
@@ -1240,8 +1238,7 @@ static void emit_from_particles_task_cb(void *__restrict userdata,
                                    1.0f :
                                    (1.0f - (nearest.dist - data->solid) / data->smooth);
         /* Uses particle velocity as initial velocity for smoke. */
-        if (mfs->flags & FLUID_FLOW_INITVELOCITY &&
-            (mfs->psys->part->phystype != PART_PHYS_NO)) {
+        if (mfs->flags & FLUID_FLOW_INITVELOCITY && (mfs->psys->part->phystype != PART_PHYS_NO)) {
           madd_v3_v3fl(
               &em->velocity[index * 3], &data->particle_vel[nearest.index * 3], mfs->vel_multi);
         }
@@ -1583,16 +1580,22 @@ static void sample_mesh(FluidFlowSettings *mfs,
   BVHTreeNearest nearest = {0};
 
   float volume_factor = 0.0f;
-  float sample_str = 0.0f;
+  float emission_strength = 0.0f;
 
   hit.index = -1;
   hit.dist = PHI_MAX;
   nearest.index = -1;
-  nearest.dist_sq = mfs->surface_distance *
-                    mfs->surface_distance; /* find_nearest uses squared distance */
 
-  /* Check volume collision */
-  if (mfs->volume_density) {
+  /* Distance from unit cube center to one of the vertices.
+   * I.e. half the cube diagonal or sqrt(3) * 0.5. */
+  const float surface_distance = 0.867f;
+  nearest.dist_sq = surface_distance * surface_distance; /* find_nearest uses squared distance. */
+
+  bool is_gas_flow = (mfs->type == FLUID_FLOW_TYPE_SMOKE || mfs->type == FLUID_FLOW_TYPE_FIRE ||
+                      mfs->type == FLUID_FLOW_TYPE_SMOKEFIRE);
+
+  /* Emission inside the flow object. */
+  if (is_gas_flow && mfs->volume_density) {
     if (BLI_bvhtree_ray_cast(tree_data->tree,
                              ray_start,
                              ray_dir,
@@ -1601,11 +1604,10 @@ static void sample_mesh(FluidFlowSettings *mfs,
                              tree_data->raycast_callback,
                              tree_data) != -1) {
       float dot = ray_dir[0] * hit.no[0] + ray_dir[1] * hit.no[1] + ray_dir[2] * hit.no[2];
-      /* If ray and hit face normal are facing same direction
-       * hit point is inside a closed mesh. */
+      /* If ray and hit face normal are facing same direction hit point is inside a closed mesh. */
       if (dot >= 0) {
-        /* Also cast a ray in opposite direction to make sure
-         * point is at least surrounded by two faces */
+        /* Also cast a ray in opposite direction to make sure point is at least surrounded by two
+         * faces. */
         negate_v3(ray_dir);
         hit.index = -1;
         hit.dist = PHI_MAX;
@@ -1624,48 +1626,45 @@ static void sample_mesh(FluidFlowSettings *mfs,
     }
   }
 
-  /* find the nearest point on the mesh */
+  /* Find the nearest point on the mesh. */
   if (BLI_bvhtree_find_nearest(
           tree_data->tree, ray_start, &nearest, tree_data->nearest_callback, tree_data) != -1) {
     float weights[3];
     int v1, v2, v3, f_index = nearest.index;
     float n1[3], n2[3], n3[3], hit_normal[3];
 
-    /* emit from surface based on distance */
+    /* Emission from surface is based on UI configurable distance value. */
     if (mfs->surface_distance) {
-      sample_str = sqrtf(nearest.dist_sq) / mfs->surface_distance;
-      CLAMP(sample_str, 0.0f, 1.0f);
-      sample_str = pow(1.0f - sample_str, 0.5f);
+      emission_strength = sqrtf(nearest.dist_sq) / mfs->surface_distance;
+      CLAMP(emission_strength, 0.0f, 1.0f);
+      emission_strength = pow(1.0f - emission_strength, 0.5f);
     }
     else {
-      sample_str = 0.0f;
+      emission_strength = 0.0f;
     }
 
-    /* calculate barycentric weights for nearest point */
+    /* Calculate barycentric weights for nearest point. */
     v1 = mloop[mlooptri[f_index].tri[0]].v;
     v2 = mloop[mlooptri[f_index].tri[1]].v;
     v3 = mloop[mlooptri[f_index].tri[2]].v;
     interp_weights_tri_v3(weights, mvert[v1].co, mvert[v2].co, mvert[v3].co, nearest.co);
 
     if (mfs->flags & FLUID_FLOW_INITVELOCITY && velocity_map) {
-      /* apply normal directional velocity */
+      /* Apply normal directional velocity. */
       if (mfs->vel_normal) {
-        /* interpolate vertex normal vectors to get nearest point normal */
+        /* Interpolate vertex normal vectors to get nearest point normal. */
         normal_short_to_float_v3(n1, mvert[v1].no);
         normal_short_to_float_v3(n2, mvert[v2].no);
         normal_short_to_float_v3(n3, mvert[v3].no);
         interp_v3_v3v3v3(hit_normal, n1, n2, n3, weights);
         normalize_v3(hit_normal);
-        /* apply normal directional and random velocity
-         * - TODO: random disabled for now since it doesn't really work well
-         *   as pressure calc smoothens it out. */
+
+        /* Apply normal directional velocity. */
         velocity_map[index * 3] += hit_normal[0] * mfs->vel_normal * 0.25f;
         velocity_map[index * 3 + 1] += hit_normal[1] * mfs->vel_normal * 0.25f;
         velocity_map[index * 3 + 2] += hit_normal[2] * mfs->vel_normal * 0.25f;
-        /* TODO: for fire emitted from mesh surface we can use
-         * Vf = Vs + (Ps/Pf - 1)*S to model gaseous expansion from solid to fuel */
       }
-      /* apply object velocity */
+      /* Apply object velocity. */
       if (has_velocity && mfs->vel_multi) {
         float hit_vel[3];
         interp_v3_v3v3v3(
@@ -1683,16 +1682,16 @@ static void sample_mesh(FluidFlowSettings *mfs,
       velocity_map[index * 3 + 2] += mfs->vel_coord[2];
     }
 
-    /* apply vertex group influence if used */
+    /* Apply vertex group influence if it is being used. */
     if (defgrp_index != -1 && dvert) {
       float weight_mask = defvert_find_weight(&dvert[v1], defgrp_index) * weights[0] +
                           defvert_find_weight(&dvert[v2], defgrp_index) * weights[1] +
                           defvert_find_weight(&dvert[v3], defgrp_index) * weights[2];
-      sample_str *= weight_mask;
+      emission_strength *= weight_mask;
     }
 
-    /* apply emission texture */
-    if ((mfs->flags & FLUID_FLOW_TEXTUREEMIT) && mfs->noise_texture) {
+    /* Apply emission texture. */
+    if (is_gas_flow && (mfs->flags & FLUID_FLOW_TEXTUREEMIT) && mfs->noise_texture) {
       float tex_co[3] = {0};
       TexResult texres;
 
@@ -1709,24 +1708,19 @@ static void sample_mesh(FluidFlowSettings *mfs,
 
         interp_v2_v2v2v2(tex_co, UNPACK3(uv), weights);
 
-        /* map between -1.0f and 1.0f */
+        /* Map texure coord between -1.0f and 1.0f. */
         tex_co[0] = tex_co[0] * 2.0f - 1.0f;
         tex_co[1] = tex_co[1] * 2.0f - 1.0f;
         tex_co[2] = mfs->texture_offset;
       }
       texres.nor = NULL;
       BKE_texture_get_value(NULL, mfs->noise_texture, tex_co, &texres, false);
-      sample_str *= texres.tin;
+      emission_strength *= texres.tin;
     }
   }
 
-  /* multiply initial velocity by emitter influence */
-  if (mfs->flags & FLUID_FLOW_INITVELOCITY && velocity_map) {
-    mul_v3_fl(&velocity_map[index * 3], sample_str);
-  }
-
-  /* apply final influence based on volume factor */
-  influence_map[index] = MAX2(volume_factor, sample_str);
+  /* Apply final influence value but also consider volume initialization factor. */
+  influence_map[index] = MAX2(volume_factor, emission_strength);
 }
 
 typedef struct EmitFromDMData {
@@ -1761,25 +1755,30 @@ static void emit_from_mesh_task_cb(void *__restrict userdata,
           x - em->min[0], em->res[0], y - em->min[1], em->res[1], z - em->min[2]);
       const float ray_start[3] = {((float)x) + 0.5f, ((float)y) + 0.5f, ((float)z) + 0.5f};
 
-      sample_mesh(data->mfs,
-                  data->mvert,
-                  data->mloop,
-                  data->mlooptri,
-                  data->mloopuv,
-                  em->influence,
-                  em->velocity,
-                  index,
-                  data->mds->base_res,
-                  data->flow_center,
-                  data->tree,
-                  ray_start,
-                  data->vert_vel,
-                  data->has_velocity,
-                  data->defgrp_index,
-                  data->dvert,
-                  (float)x,
-                  (float)y,
-                  (float)z);
+      /* Compute emission only for flow objects that produce fluid (i.e. skip outflow objects).
+       * Result in em->influence. Also computes initial velocities. Result in em->velocity. */
+      if ((data->mfs->behavior == FLUID_FLOW_BEHAVIOR_GEOMETRY) ||
+          (data->mfs->behavior == FLUID_FLOW_BEHAVIOR_INFLOW)) {
+        sample_mesh(data->mfs,
+                    data->mvert,
+                    data->mloop,
+                    data->mlooptri,
+                    data->mloopuv,
+                    em->influence,
+                    em->velocity,
+                    index,
+                    data->mds->base_res,
+                    data->flow_center,
+                    data->tree,
+                    ray_start,


@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list