Blender V5.3
fluid.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "MEM_guardedalloc.h"
10
11#include "BLI_listbase.hh"
12
13#include "BLI_fileops.hh"
14#include "BLI_hash_c.hh"
15#include "BLI_math_geom_c.hh"
16#include "BLI_math_matrix_c.hh"
17#include "BLI_math_vector_c.hh"
18#include "BLI_path_utils.hh"
19#include "BLI_string.hh"
20#include "BLI_task_c.hh"
21#include "BLI_utildefines.hh"
22
23#include "DNA_colorband_types.h"
24#include "DNA_fluid_types.h"
25#include "DNA_modifier_types.h"
26#include "DNA_object_types.h"
27#include "DNA_rigidbody_types.h"
28#include "DNA_texture_types.h"
29
30#include "BKE_attribute.hh"
31#include "BKE_effect.h"
32#include "BKE_fluid.h"
33#include "BKE_global.hh"
34#include "BKE_layer.hh"
35#include "BKE_lib_id.hh"
36#include "BKE_modifier.hh"
37#include "BKE_pointcache.h"
38
39#ifdef WITH_FLUID
40
41# include <algorithm>
42# include <cfloat>
43# include <cmath>
44# include <cstdio>
45# include <cstring> /* memset */
46
47# include "DNA_customdata_types.h"
48# include "DNA_light_types.h"
49# include "DNA_mesh_types.h"
50# include "DNA_meshdata_types.h"
51# include "DNA_particle_types.h"
52# include "DNA_scene_types.h"
53
54# include "BLI_kdopbvh.hh"
55# include "BLI_kdtree.hh"
56# include "BLI_math_vector.hh"
57# include "BLI_mutex.hh"
58# include "BLI_threads.hh"
59# include "BLI_voxel.hh"
60
61# include "BKE_bvhutils.hh"
62# include "BKE_collision.h"
63# include "BKE_customdata.hh"
64# include "BKE_deform.hh"
65# include "BKE_mesh.hh"
66# include "BKE_mesh_runtime.hh"
67# include "BKE_object.hh"
68# include "BKE_particle.h"
69# include "BKE_scene.hh"
70# include "BKE_texture.h"
71
72# include "DEG_depsgraph.hh"
73# include "DEG_depsgraph_query.hh"
74
75# include "RE_texture.h"
76
77# include "CLG_log.h"
78
79# include "manta_fluid_API.h"
80
81#endif /* WITH_FLUID */
82
83namespace blender {
84
86#define DT_DEFAULT 0.1f
87
89#define PHI_MAX 9999.0f
90
91static void fluid_modifier_reset_ex(FluidModifierData *fmd, bool need_lock);
92
93#ifdef WITH_FLUID
94// #define DEBUG_PRINT
95
96static CLG_LogRef LOG = {"physics.fluid"};
97
98/* -------------------------------------------------------------------- */
101
102static Mutex object_update_lock;
103
104# define ADD_IF_LOWER_POS(a, b) min_ff((a) + (b), max_ff((a), (b)))
105# define ADD_IF_LOWER_NEG(a, b) max_ff((a) + (b), min_ff((a), (b)))
106# define ADD_IF_LOWER(a, b) (((b) > 0) ? ADD_IF_LOWER_POS((a), (b)) : ADD_IF_LOWER_NEG((a), (b)))
107
108bool BKE_fluid_reallocate_fluid(FluidDomainSettings *fds, int res[3], int free_old)
109{
110 if (free_old && fds->fluid) {
111 manta_free(fds->fluid);
112 }
113 if (!std::min({res[0], res[1], res[2]})) {
114 fds->fluid = nullptr;
115 }
116 else {
117 fds->fluid = manta_init(res, fds->fmd);
118
119 fds->res_noise[0] = res[0] * fds->noise_scale;
120 fds->res_noise[1] = res[1] * fds->noise_scale;
121 fds->res_noise[2] = res[2] * fds->noise_scale;
122 }
123
124 return (fds->fluid != nullptr);
125}
126
128 int o_res[3],
129 int n_res[3],
130 const int o_min[3],
131 const int n_min[3],
132 const int o_max[3],
133 int o_shift[3],
134 int n_shift[3])
135{
136 MANTA *fluid_old = fds->fluid;
137 const int block_size = fds->noise_scale;
138 int new_shift[3] = {0};
139 sub_v3_v3v3_int(new_shift, n_shift, o_shift);
140
141 /* Allocate new fluid data. */
142 BKE_fluid_reallocate_fluid(fds, n_res, 0);
143
144 int o_total_cells = o_res[0] * o_res[1] * o_res[2];
145 int n_total_cells = n_res[0] * n_res[1] * n_res[2];
146
147 /* Copy values from old fluid to new fluid object. */
148 if (o_total_cells > 1 && n_total_cells > 1) {
149 float *o_dens = manta_smoke_get_density(fluid_old);
150 float *o_react = manta_smoke_get_react(fluid_old);
151 float *o_flame = manta_smoke_get_flame(fluid_old);
152 float *o_fuel = manta_smoke_get_fuel(fluid_old);
153 float *o_heat = manta_smoke_get_heat(fluid_old);
154 float *o_vx = manta_get_velocity_x(fluid_old);
155 float *o_vy = manta_get_velocity_y(fluid_old);
156 float *o_vz = manta_get_velocity_z(fluid_old);
157 float *o_r = manta_smoke_get_color_r(fluid_old);
158 float *o_g = manta_smoke_get_color_g(fluid_old);
159 float *o_b = manta_smoke_get_color_b(fluid_old);
160
161 float *n_dens = manta_smoke_get_density(fds->fluid);
162 float *n_react = manta_smoke_get_react(fds->fluid);
163 float *n_flame = manta_smoke_get_flame(fds->fluid);
164 float *n_fuel = manta_smoke_get_fuel(fds->fluid);
165 float *n_heat = manta_smoke_get_heat(fds->fluid);
166 float *n_vx = manta_get_velocity_x(fds->fluid);
167 float *n_vy = manta_get_velocity_y(fds->fluid);
168 float *n_vz = manta_get_velocity_z(fds->fluid);
169 float *n_r = manta_smoke_get_color_r(fds->fluid);
170 float *n_g = manta_smoke_get_color_g(fds->fluid);
171 float *n_b = manta_smoke_get_color_b(fds->fluid);
172
173 /* Noise smoke fields. */
174 float *o_wt_dens = manta_noise_get_density(fluid_old);
175 float *o_wt_react = manta_noise_get_react(fluid_old);
176 float *o_wt_flame = manta_noise_get_flame(fluid_old);
177 float *o_wt_fuel = manta_noise_get_fuel(fluid_old);
178 float *o_wt_r = manta_noise_get_color_r(fluid_old);
179 float *o_wt_g = manta_noise_get_color_g(fluid_old);
180 float *o_wt_b = manta_noise_get_color_b(fluid_old);
181 float *o_wt_tcu = manta_noise_get_texture_u(fluid_old);
182 float *o_wt_tcv = manta_noise_get_texture_v(fluid_old);
183 float *o_wt_tcw = manta_noise_get_texture_w(fluid_old);
184 float *o_wt_tcu2 = manta_noise_get_texture_u2(fluid_old);
185 float *o_wt_tcv2 = manta_noise_get_texture_v2(fluid_old);
186 float *o_wt_tcw2 = manta_noise_get_texture_w2(fluid_old);
187
188 float *n_wt_dens = manta_noise_get_density(fds->fluid);
189 float *n_wt_react = manta_noise_get_react(fds->fluid);
190 float *n_wt_flame = manta_noise_get_flame(fds->fluid);
191 float *n_wt_fuel = manta_noise_get_fuel(fds->fluid);
192 float *n_wt_r = manta_noise_get_color_r(fds->fluid);
193 float *n_wt_g = manta_noise_get_color_g(fds->fluid);
194 float *n_wt_b = manta_noise_get_color_b(fds->fluid);
195 float *n_wt_tcu = manta_noise_get_texture_u(fds->fluid);
196 float *n_wt_tcv = manta_noise_get_texture_v(fds->fluid);
197 float *n_wt_tcw = manta_noise_get_texture_w(fds->fluid);
198 float *n_wt_tcu2 = manta_noise_get_texture_u2(fds->fluid);
199 float *n_wt_tcv2 = manta_noise_get_texture_v2(fds->fluid);
200 float *n_wt_tcw2 = manta_noise_get_texture_w2(fds->fluid);
201
202 int wt_res_old[3];
203 manta_noise_get_res(fluid_old, wt_res_old);
204
205 for (int z = o_min[2]; z < o_max[2]; z++) {
206 for (int y = o_min[1]; y < o_max[1]; y++) {
207 for (int x = o_min[0]; x < o_max[0]; x++) {
208 /* old grid index */
209 int xo = x - o_min[0];
210 int yo = y - o_min[1];
211 int zo = z - o_min[2];
212 int index_old = manta_get_index(xo, o_res[0], yo, o_res[1], zo);
213 /* new grid index */
214 int xn = x - n_min[0] - new_shift[0];
215 int yn = y - n_min[1] - new_shift[1];
216 int zn = z - n_min[2] - new_shift[2];
217 int index_new = manta_get_index(xn, n_res[0], yn, n_res[1], zn);
218
219 /* Skip if outside new domain. */
220 if (xn < 0 || xn >= n_res[0] || yn < 0 || yn >= n_res[1] || zn < 0 || zn >= n_res[2]) {
221 continue;
222 }
223# if 0
224 /* Note (sebbas):
225 * Disabling this "skip section" as not copying borders results in weird cut-off effects.
226 * It is possible that this cutting off is the reason for line effects as seen in #74559.
227 * Since domain borders will be handled on the simulation side anyways,
228 * copying border values should not be an issue. */
229
230 /* boundary cells will be skipped when copying data */
231 int bwidth = fds->boundary_width;
232
233 /* Skip if trying to copy from old boundary cell. */
234 if (xo < bwidth || yo < bwidth || zo < bwidth || xo >= o_res[0] - bwidth ||
235 yo >= o_res[1] - bwidth || zo >= o_res[2] - bwidth)
236 {
237 continue;
238 }
239 /* Skip if trying to copy into new boundary cell. */
240 if (xn < bwidth || yn < bwidth || zn < bwidth || xn >= n_res[0] - bwidth ||
241 yn >= n_res[1] - bwidth || zn >= n_res[2] - bwidth)
242 {
243 continue;
244 }
245# endif
246
247 /* copy data */
248 if (fds->flags & FLUID_DOMAIN_USE_NOISE) {
249 int i, j, k;
250 /* old grid index */
251 int xx_o = xo * block_size;
252 int yy_o = yo * block_size;
253 int zz_o = zo * block_size;
254 /* new grid index */
255 int xx_n = xn * block_size;
256 int yy_n = yn * block_size;
257 int zz_n = zn * block_size;
258
259 /* insert old texture values into new texture grids */
260 n_wt_tcu[index_new] = o_wt_tcu[index_old];
261 n_wt_tcv[index_new] = o_wt_tcv[index_old];
262 n_wt_tcw[index_new] = o_wt_tcw[index_old];
263
264 n_wt_tcu2[index_new] = o_wt_tcu2[index_old];
265 n_wt_tcv2[index_new] = o_wt_tcv2[index_old];
266 n_wt_tcw2[index_new] = o_wt_tcw2[index_old];
267
268 for (i = 0; i < block_size; i++) {
269 for (j = 0; j < block_size; j++) {
270 for (k = 0; k < block_size; k++) {
271 int big_index_old = manta_get_index(
272 xx_o + i, wt_res_old[0], yy_o + j, wt_res_old[1], zz_o + k);
273 int big_index_new = manta_get_index(
274 xx_n + i, fds->res_noise[0], yy_n + j, fds->res_noise[1], zz_n + k);
275 /* copy data */
276 n_wt_dens[big_index_new] = o_wt_dens[big_index_old];
277 if (n_wt_flame && o_wt_flame) {
278 n_wt_flame[big_index_new] = o_wt_flame[big_index_old];
279 n_wt_fuel[big_index_new] = o_wt_fuel[big_index_old];
280 n_wt_react[big_index_new] = o_wt_react[big_index_old];
281 }
282 if (n_wt_r && o_wt_r) {
283 n_wt_r[big_index_new] = o_wt_r[big_index_old];
284 n_wt_g[big_index_new] = o_wt_g[big_index_old];
285 n_wt_b[big_index_new] = o_wt_b[big_index_old];
286 }
287 }
288 }
289 }
290 }
291
292 n_dens[index_new] = o_dens[index_old];
293 /* heat */
294 if (n_heat && o_heat) {
295 n_heat[index_new] = o_heat[index_old];
296 }
297 /* fuel */
298 if (n_fuel && o_fuel) {
299 n_flame[index_new] = o_flame[index_old];
300 n_fuel[index_new] = o_fuel[index_old];
301 n_react[index_new] = o_react[index_old];
302 }
303 /* color */
304 if (o_r && n_r) {
305 n_r[index_new] = o_r[index_old];
306 n_g[index_new] = o_g[index_old];
307 n_b[index_new] = o_b[index_old];
308 }
309 n_vx[index_new] = o_vx[index_old];
310 n_vy[index_new] = o_vy[index_old];
311 n_vz[index_new] = o_vz[index_old];
312 }
313 }
314 }
315 }
316 manta_free(fluid_old);
317}
318
320{
324 BKE_fluid_cache_free(fds, ob, cache_map);
325}
326
327void BKE_fluid_cache_free(FluidDomainSettings *fds, Object *ob, int cache_map)
328{
329 char temp_dir[FILE_MAX];
330 eFluidDomain_CacheFlag flags = fds->cache_flag;
331 const char *relbase = BKE_modifier_path_relbase_from_global(ob);
332
333 if (cache_map & FLUID_DOMAIN_OUTDATED_DATA) {
335 BLI_path_join(temp_dir, sizeof(temp_dir), fds->cache_directory, FLUID_DOMAIN_DIR_CONFIG);
336 BLI_path_abs(temp_dir, relbase);
337 if (BLI_exists(temp_dir)) {
338 BLI_delete(temp_dir, true, true);
339 }
340 BLI_path_join(temp_dir, sizeof(temp_dir), fds->cache_directory, FLUID_DOMAIN_DIR_DATA);
341 BLI_path_abs(temp_dir, relbase);
342 if (BLI_exists(temp_dir)) {
343 BLI_delete(temp_dir, true, true);
344 }
345 BLI_path_join(temp_dir, sizeof(temp_dir), fds->cache_directory, FLUID_DOMAIN_DIR_SCRIPT);
346 BLI_path_abs(temp_dir, relbase);
347 if (BLI_exists(temp_dir)) {
348 BLI_delete(temp_dir, true, true);
349 }
350 fds->cache_frame_pause_data = 0;
351 }
352 if (cache_map & FLUID_DOMAIN_OUTDATED_NOISE) {
354 BLI_path_join(temp_dir, sizeof(temp_dir), fds->cache_directory, FLUID_DOMAIN_DIR_NOISE);
355 BLI_path_abs(temp_dir, relbase);
356 if (BLI_exists(temp_dir)) {
357 BLI_delete(temp_dir, true, true);
358 }
359 fds->cache_frame_pause_noise = 0;
360 }
361 if (cache_map & FLUID_DOMAIN_OUTDATED_MESH) {
363 BLI_path_join(temp_dir, sizeof(temp_dir), fds->cache_directory, FLUID_DOMAIN_DIR_MESH);
364 BLI_path_abs(temp_dir, relbase);
365 if (BLI_exists(temp_dir)) {
366 BLI_delete(temp_dir, true, true);
367 }
368 fds->cache_frame_pause_mesh = 0;
369 }
370 if (cache_map & FLUID_DOMAIN_OUTDATED_PARTICLES) {
373 BLI_path_join(temp_dir, sizeof(temp_dir), fds->cache_directory, FLUID_DOMAIN_DIR_PARTICLES);
374 BLI_path_abs(temp_dir, relbase);
375 if (BLI_exists(temp_dir)) {
376 BLI_delete(temp_dir, true, true);
377 }
378 fds->cache_frame_pause_particles = 0;
379 }
380 if (cache_map & FLUID_DOMAIN_OUTDATED_GUIDE) {
382 BLI_path_join(temp_dir, sizeof(temp_dir), fds->cache_directory, FLUID_DOMAIN_DIR_GUIDE);
383 BLI_path_abs(temp_dir, relbase);
384 if (BLI_exists(temp_dir)) {
385 BLI_delete(temp_dir, true, true);
386 }
387 fds->cache_frame_pause_guide = 0;
388 }
389 fds->cache_flag = flags;
390}
391
392/* convert global position to domain cell space */
393static void manta_pos_to_cell(FluidDomainSettings *fds, float pos[3])
394{
395 mul_m4_v3(fds->imat, pos);
396 sub_v3_v3(pos, fds->p0);
397 pos[0] *= 1.0f / fds->cell_size[0];
398 pos[1] *= 1.0f / fds->cell_size[1];
399 pos[2] *= 1.0f / fds->cell_size[2];
400}
401
402/* Set domain transformations and base resolution from object mesh. */
403static void manta_set_domain_from_mesh(FluidDomainSettings *fds,
404 Object *ob,
405 Mesh *mesh,
406 bool init_resolution)
407{
408 size_t i;
409 float min[3] = {FLT_MAX, FLT_MAX, FLT_MAX}, max[3] = {-FLT_MAX, -FLT_MAX, -FLT_MAX};
410 float size[3];
411
412 MutableSpan<float3> positions = mesh->vert_positions_for_write();
413 float scale = 0.0;
414 int res;
415
416 res = fds->maxres;
417
418 /* Set minimum and maximum coordinates of BB. */
419 for (i = 0; i < mesh->verts_num; i++) {
420 minmax_v3v3_v3(min, max, positions[i]);
421 }
422
423 /* Set domain bounds. */
424 copy_v3_v3(fds->p0, min);
425 copy_v3_v3(fds->p1, max);
426 fds->dx = 1.0f / res;
427
428 /* Calculate domain dimensions. */
429 sub_v3_v3v3(size, max, min);
430 if (init_resolution) {
431 zero_v3_int(fds->base_res);
432 copy_v3_v3(fds->cell_size, size);
433 }
434 /* Apply object scale. */
435 for (i = 0; i < 3; i++) {
436 const float scale = ob->scale[i];
437 size[i] = fabsf(size[i] * (isfinite(scale) ? scale : 1.0f));
438 if (!isfinite(size[i])) {
439 size[i] = 1.0f;
440 }
441 }
442 copy_v3_v3(fds->global_size, size);
443 copy_v3_v3(fds->dp0, min);
444
445 invert_m4_m4(fds->imat, ob->object_to_world().ptr());
446
447 /* Prevent crash when initializing a plane as domain. */
448 if (!init_resolution || (size[0] < FLT_EPSILON) || (size[1] < FLT_EPSILON) ||
449 (size[2] < FLT_EPSILON))
450 {
451 return;
452 }
453
454 /* Define grid resolutions from longest domain side. */
455 if (size[0] >= std::max(size[1], size[2])) {
456 scale = res / size[0];
457 fds->scale = size[0] / fabsf(ob->scale[0]);
458 fds->base_res[0] = res;
459 fds->base_res[1] = max_ii(int(size[1] * scale + 0.5f), 4);
460 fds->base_res[2] = max_ii(int(size[2] * scale + 0.5f), 4);
461 }
462 else if (size[1] >= std::max(size[0], size[2])) {
463 scale = res / size[1];
464 fds->scale = size[1] / fabsf(ob->scale[1]);
465 fds->base_res[0] = max_ii(int(size[0] * scale + 0.5f), 4);
466 fds->base_res[1] = res;
467 fds->base_res[2] = max_ii(int(size[2] * scale + 0.5f), 4);
468 }
469 else {
470 scale = res / size[2];
471 fds->scale = size[2] / fabsf(ob->scale[2]);
472 fds->base_res[0] = max_ii(int(size[0] * scale + 0.5f), 4);
473 fds->base_res[1] = max_ii(int(size[1] * scale + 0.5f), 4);
474 fds->base_res[2] = res;
475 }
476
477 /* Set cell size. */
478 fds->cell_size[0] /= float(fds->base_res[0]);
479 fds->cell_size[1] /= float(fds->base_res[1]);
480 fds->cell_size[2] /= float(fds->base_res[2]);
481}
482
483static void update_final_gravity(FluidDomainSettings *fds, Scene *scene)
484{
485 if (scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY) {
486 copy_v3_v3(fds->gravity_final, scene->physics_settings.gravity);
487 }
488 else {
489 copy_v3_v3(fds->gravity_final, fds->gravity);
490 }
491 mul_v3_fl(fds->gravity_final, fds->effector_weights->global_gravity);
492}
493
494static bool fluid_modifier_init(
495 FluidModifierData *fmd, Depsgraph *depsgraph, Object *ob, Scene *scene, Mesh *mesh)
496{
497 int scene_framenr = int(DEG_get_ctime(depsgraph));
498
499 if ((fmd->type & MOD_FLUID_TYPE_DOMAIN) && fmd->domain && !fmd->domain->fluid) {
500 FluidDomainSettings *fds = fmd->domain;
501 int res[3];
502 /* Set domain dimensions from mesh. */
503 manta_set_domain_from_mesh(fds, ob, mesh, true);
504 /* Set domain gravity, use global gravity if enabled. */
505 update_final_gravity(fds, scene);
506 /* Reset domain values. */
507 zero_v3_int(fds->shift);
508 zero_v3(fds->shift_f);
509 add_v3_fl(fds->shift_f, 0.5f);
510 zero_v3(fds->prev_loc);
511 mul_m4_v3(ob->object_to_world().ptr(), fds->prev_loc);
512 copy_m4_m4(fds->obmat, ob->object_to_world().ptr());
513
514 /* Set resolutions. */
515 if (fmd->domain->type == FLUID_DOMAIN_TYPE_GAS &&
516 fmd->domain->flags & FLUID_DOMAIN_USE_ADAPTIVE_DOMAIN)
517 {
518 res[0] = res[1] = res[2] = 1; /* Use minimum res for adaptive init. */
519 }
520 else {
521 copy_v3_v3_int(res, fds->base_res);
522 }
523 copy_v3_v3_int(fds->res, res);
524 fds->total_cells = fds->res[0] * fds->res[1] * fds->res[2];
525 fds->res_min[0] = fds->res_min[1] = fds->res_min[2] = 0;
526 copy_v3_v3_int(fds->res_max, res);
527
528 /* Set time, frame length = 0.1 is at 25fps. */
529 fds->frame_length = DT_DEFAULT * (25.0f / scene->frames_per_second()) * fds->time_scale;
530 /* Initially dt is equal to frame length (dt can change with adaptive-time stepping though). */
531 fds->dt = fds->frame_length;
532 fds->time_per_frame = 0;
533
534 fmd->time = scene_framenr;
535
536 /* Allocate fluid. */
537 return BKE_fluid_reallocate_fluid(fds, fds->res, 0);
538 }
539 if (fmd->type & MOD_FLUID_TYPE_FLOW) {
540 if (!fmd->flow) {
542 }
543 fmd->time = scene_framenr;
544 return true;
545 }
546 if (fmd->type & MOD_FLUID_TYPE_EFFEC) {
547 if (!fmd->effector) {
549 }
550 fmd->time = scene_framenr;
551 return true;
552 }
553 return false;
554}
555
556/* Forward declarations. */
557static void manta_smoke_calc_transparency(const Main &bmain,
559 Scene *scene,
560 ViewLayer *view_layer);
561static float calc_voxel_transp(
562 float *result, const float *input, int res[3], int *pixel, float *t_ray, float correct);
563static void update_distances(int index,
564 float *distance_map,
565 const bke::bvh::Tree &tree,
566 const float ray_start[3],
567 float surface_thickness,
568 bool use_plane_init);
569
570static int get_light(const Main &bmain, Scene *scene, ViewLayer *view_layer, float *light)
571{
572 int found_light = 0;
573
574 /* Try to find a lamp, preferably local. */
575 BKE_view_layer_synced_ensure(bmain, scene, view_layer);
576 for (Base &base_tmp : *BKE_view_layer_object_bases_get(view_layer)) {
577 if (base_tmp.object->type == OB_LAMP) {
578 Light *la = id_cast<Light *>(base_tmp.object->data);
579
580 if (la->type == LA_LOCAL) {
581 copy_v3_v3(light, base_tmp.object->object_to_world().location());
582 return 1;
583 }
584 if (!found_light) {
585 copy_v3_v3(light, base_tmp.object->object_to_world().location());
586 found_light = 1;
587 }
588 }
589 }
590
591 return found_light;
592}
593
594static void clamp_bounds_in_domain(FluidDomainSettings *fds,
595 int min[3],
596 int max[3],
597 const float *min_vel,
598 const float *max_vel,
599 int margin,
600 float dt)
601{
602 for (int i = 0; i < 3; i++) {
603 int adapt = (fds->flags & FLUID_DOMAIN_USE_ADAPTIVE_DOMAIN) ? fds->adapt_res : 0;
604 /* Add some margin. */
605 min[i] -= margin;
606 max[i] += margin;
607
608 /* Adapt to velocity. */
609 if (min_vel && min_vel[i] < 0.0f) {
610 min[i] += int(floor(min_vel[i] * dt));
611 }
612 if (max_vel && max_vel[i] > 0.0f) {
613 max[i] += int(ceil(max_vel[i] * dt));
614 }
615
616 /* Clamp within domain max size. */
617 CLAMP(min[i], -adapt, fds->base_res[i] + adapt);
618 CLAMP(max[i], -adapt, fds->base_res[i] + adapt);
619 }
620}
621
622static bool is_static_object(Object *ob)
623{
624 /* Check if the object has modifiers that might make the object "dynamic". */
625 VirtualModifierData virtual_modifier_data;
626 ModifierData *md = BKE_modifiers_get_virtual_modifierlist(ob, &virtual_modifier_data);
627 for (; md; md = md->next) {
628 if (ELEM(md->type,
636 {
637 return false;
638 }
639 }
640
641 /* Active rigid body objects considered to be dynamic fluid objects. */
642 if (ob->rigidbody_object && ob->rigidbody_object->type == RBO_TYPE_ACTIVE) {
643 return false;
644 }
645
646 /* Finally, check if the object has animation data. If so, it is considered dynamic. */
647 return !BKE_object_moves_in_time(ob, true);
648}
649
651
652/* -------------------------------------------------------------------- */
655
656struct FluidObjectBB {
657 float *influence;
658 float *velocity;
659 float *distances;
660 float *numobjs;
661 int min[3], max[3], res[3];
662 int hmin[3], hmax[3], hres[3];
663 int total_cells, valid;
664};
665
666static void bb_boundInsert(FluidObjectBB *bb, const float point[3])
667{
668 int i = 0;
669 if (!bb->valid) {
670 for (; i < 3; i++) {
671 bb->min[i] = int(floor(point[i]));
672 bb->max[i] = int(ceil(point[i]));
673 }
674 bb->valid = 1;
675 }
676 else {
677 for (; i < 3; i++) {
678 if (point[i] < bb->min[i]) {
679 bb->min[i] = int(floor(point[i]));
680 }
681 if (point[i] > bb->max[i]) {
682 bb->max[i] = int(ceil(point[i]));
683 }
684 }
685 }
686}
687
688static void bb_allocateData(FluidObjectBB *bb, bool use_velocity, bool use_influence)
689{
690 int i, res[3];
691
692 for (i = 0; i < 3; i++) {
693 res[i] = bb->max[i] - bb->min[i];
694 if (res[i] <= 0) {
695 return;
696 }
697 }
698 bb->total_cells = res[0] * res[1] * res[2];
699 copy_v3_v3_int(bb->res, res);
700
701 bb->numobjs = MEM_new_array_zeroed<float>(bb->total_cells, "fluid_bb_numobjs");
702 if (use_influence) {
703 bb->influence = MEM_new_array_zeroed<float>(bb->total_cells, "fluid_bb_influence");
704 }
705 if (use_velocity) {
706 bb->velocity = MEM_new_array_zeroed<float>(3 * size_t(bb->total_cells), "fluid_bb_velocity");
707 }
708
709 bb->distances = MEM_new_array_uninitialized<float>(size_t(bb->total_cells),
710 "fluid_bb_distances");
711 std::fill_n(bb->distances, bb->total_cells, FLT_MAX);
712
713 bb->valid = true;
714}
715
716static void bb_freeData(FluidObjectBB *bb)
717{
718 if (bb->numobjs) {
719 MEM_delete(bb->numobjs);
720 }
721 if (bb->influence) {
722 MEM_delete(bb->influence);
723 }
724 if (bb->velocity) {
725 MEM_delete(bb->velocity);
726 }
727 if (bb->distances) {
728 MEM_delete(bb->distances);
729 }
730}
731
732static void bb_combineMaps(FluidObjectBB *output,
733 FluidObjectBB *bb2,
734 int additive,
735 float sample_size)
736{
737 int i, x, y, z;
738
739 /* Copy-fill input 1 struct and clear output for new allocation. */
740 FluidObjectBB bb1;
741 memcpy(&bb1, output, sizeof(FluidObjectBB));
742 memset(output, 0, sizeof(FluidObjectBB));
743
744 for (i = 0; i < 3; i++) {
745 if (bb1.valid) {
746 output->min[i] = std::min(bb1.min[i], bb2->min[i]);
747 output->max[i] = std::max(bb1.max[i], bb2->max[i]);
748 }
749 else {
750 output->min[i] = bb2->min[i];
751 output->max[i] = bb2->max[i];
752 }
753 }
754 /* Allocate output map. */
755 bb_allocateData(output, (bb1.velocity || bb2->velocity), (bb1.influence || bb2->influence));
756
757 /* Low through bounding box */
758 for (x = output->min[0]; x < output->max[0]; x++) {
759 for (y = output->min[1]; y < output->max[1]; y++) {
760 for (z = output->min[2]; z < output->max[2]; z++) {
761 int index_out = manta_get_index(x - output->min[0],
762 output->res[0],
763 y - output->min[1],
764 output->res[1],
765 z - output->min[2]);
766
767 /* Initialize with first input if in range. */
768 if (x >= bb1.min[0] && x < bb1.max[0] && y >= bb1.min[1] && y < bb1.max[1] &&
769 z >= bb1.min[2] && z < bb1.max[2])
770 {
771 int index_in = manta_get_index(
772 x - bb1.min[0], bb1.res[0], y - bb1.min[1], bb1.res[1], z - bb1.min[2]);
773
774 /* Values. */
775 output->numobjs[index_out] = bb1.numobjs[index_in];
776 if (output->influence && bb1.influence) {
777 output->influence[index_out] = bb1.influence[index_in];
778 }
779 output->distances[index_out] = bb1.distances[index_in];
780 if (output->velocity && bb1.velocity) {
781 copy_v3_v3(&output->velocity[index_out * 3], &bb1.velocity[index_in * 3]);
782 }
783 }
784
785 /* Apply second input if in range. */
786 if (x >= bb2->min[0] && x < bb2->max[0] && y >= bb2->min[1] && y < bb2->max[1] &&
787 z >= bb2->min[2] && z < bb2->max[2])
788 {
789 int index_in = manta_get_index(
790 x - bb2->min[0], bb2->res[0], y - bb2->min[1], bb2->res[1], z - bb2->min[2]);
791
792 /* Values. */
793 output->numobjs[index_out] = std::max(bb2->numobjs[index_in],
794 output->numobjs[index_out]);
795 if (output->influence && bb2->influence) {
796 if (additive) {
797 output->influence[index_out] += bb2->influence[index_in] * sample_size;
798 }
799 else {
800 output->influence[index_out] = std::max(bb2->influence[index_in],
801 output->influence[index_out]);
802 }
803 }
804 output->distances[index_out] = std::min(bb2->distances[index_in],
805 output->distances[index_out]);
806 if (output->velocity && bb2->velocity) {
807 /* Last sample replaces the velocity. */
808 output->velocity[index_out * 3] = ADD_IF_LOWER(output->velocity[index_out * 3],
809 bb2->velocity[index_in * 3]);
810 output->velocity[index_out * 3 + 1] = ADD_IF_LOWER(output->velocity[index_out * 3 + 1],
811 bb2->velocity[index_in * 3 + 1]);
812 output->velocity[index_out * 3 + 2] = ADD_IF_LOWER(output->velocity[index_out * 3 + 2],
813 bb2->velocity[index_in * 3 + 2]);
814 }
815 }
816 } /* Low res loop. */
817 }
818 }
819
820 /* Free original data. */
821 bb_freeData(&bb1);
822}
823
825
826/* -------------------------------------------------------------------- */
829
830BLI_INLINE void apply_effector_fields(FluidEffectorSettings * /*fes*/,
831 int index,
832 float src_distance_value,
833 float *dest_phi_in,
834 float src_numobjs_value,
835 float *dest_numobjs,
836 float const src_vel_value[3],
837 float *dest_vel_x,
838 float *dest_vel_y,
839 float *dest_vel_z)
840{
841 /* Ensure that distance value is "joined" into the levelset. */
842 if (dest_phi_in) {
843 dest_phi_in[index] = std::min(src_distance_value, dest_phi_in[index]);
844 }
845
846 /* Accumulate effector object count (important once effector object overlap). */
847 if (dest_numobjs && src_numobjs_value > 0) {
848 dest_numobjs[index] += 1;
849 }
850
851 /* Accumulate effector velocities for each cell. */
852 if (dest_vel_x && src_numobjs_value > 0) {
853 dest_vel_x[index] += src_vel_value[0];
854 dest_vel_y[index] += src_vel_value[1];
855 dest_vel_z[index] += src_vel_value[2];
856 }
857}
858
859static void update_velocities(FluidEffectorSettings *fes,
860 const Span<float3> vert_positions,
861 const int *corner_verts,
862 const int3 *corner_tris,
863 float *velocity_map,
864 int index,
865 const bke::bvh::Tree &tree,
866 const float ray_start[3],
867 const float *vert_vel,
868 bool has_velocity)
869{
870 /* Distance between two opposing vertices in a unit cube.
871 * I.e. the unit cube diagonal or `sqrt(3)`.
872 * This value is our nearest neighbor search distance. */
873 const float surface_distance = 1.732;
874
875 /* Find the nearest point on the mesh. */
876 const std::optional<bke::bvh::ClosestPointResult> nearest = tree.closest_point(ray_start,
877 surface_distance);
878 if (has_velocity && nearest) {
879 float weights[3];
880 int v1, v2, v3, tri_i = nearest->index;
881
882 /* Calculate barycentric weights for nearest point. */
883 v1 = corner_verts[corner_tris[tri_i][0]];
884 v2 = corner_verts[corner_tris[tri_i][1]];
885 v3 = corner_verts[corner_tris[tri_i][2]];
887 weights, vert_positions[v1], vert_positions[v2], vert_positions[v3], nearest->position);
888
889 /* Apply object velocity. */
890 float hit_vel[3];
891 interp_v3_v3v3v3(hit_vel, &vert_vel[v1 * 3], &vert_vel[v2 * 3], &vert_vel[v3 * 3], weights);
892
893 /* Guiding has additional velocity multiplier */
894 if (fes->type == FLUID_EFFECTOR_TYPE_GUIDE) {
895 mul_v3_fl(hit_vel, fes->vel_multi);
896
897 /* Absolute representation of new object velocity. */
898 float3 abs_hit_vel = math::abs(float3(hit_vel));
899
900 /* Absolute representation of current object velocity. */
901 float3 abs_vel = math::abs(float3(&velocity_map[index * 3]));
902
903 switch (fes->guide_mode) {
905 velocity_map[index * 3] = (velocity_map[index * 3] + hit_vel[0]) * 0.5f;
906 velocity_map[index * 3 + 1] = (velocity_map[index * 3 + 1] + hit_vel[1]) * 0.5f;
907 velocity_map[index * 3 + 2] = (velocity_map[index * 3 + 2] + hit_vel[2]) * 0.5f;
908 break;
910 velocity_map[index * 3] = hit_vel[0];
911 velocity_map[index * 3 + 1] = hit_vel[1];
912 velocity_map[index * 3 + 2] = hit_vel[2];
913 break;
915 velocity_map[index * 3] = std::min(abs_hit_vel[0], abs_vel[0]);
916 velocity_map[index * 3 + 1] = std::min(abs_hit_vel[1], abs_vel[1]);
917 velocity_map[index * 3 + 2] = std::min(abs_hit_vel[2], abs_vel[2]);
918 break;
920 default:
921 velocity_map[index * 3] = std::max(abs_hit_vel[0], abs_vel[0]);
922 velocity_map[index * 3 + 1] = std::max(abs_hit_vel[1], abs_vel[1]);
923 velocity_map[index * 3 + 2] = std::max(abs_hit_vel[2], abs_vel[2]);
924 break;
925 }
926 }
927 else if (fes->type == FLUID_EFFECTOR_TYPE_COLLISION) {
928 velocity_map[index * 3] = hit_vel[0];
929 velocity_map[index * 3 + 1] = hit_vel[1];
930 velocity_map[index * 3 + 2] = hit_vel[2];
931# ifdef DEBUG_PRINT
932 /* Debugging: Print object velocities. */
933 printf("setting effector object vel: [%f, %f, %f]\n", hit_vel[0], hit_vel[1], hit_vel[2]);
934# endif
935 }
936 else {
937 /* Should never reach this block. */
939 }
940 }
941 else {
942 /* Clear velocities at cells that are not moving. */
943 copy_v3_fl(velocity_map, 0.0);
944 }
945}
946
947struct ObstaclesFromDMData {
948 FluidEffectorSettings *fes;
949
950 Span<float3> vert_positions;
951 Span<int> corner_verts;
952 Span<int3> corner_tris;
953
954 const bke::bvh::Tree *tree;
955 FluidObjectBB *bb;
956
957 bool has_velocity;
958 float *vert_vel;
959 int *min, *max, *res;
960};
961
962static void obstacles_from_mesh_task_cb(void *__restrict userdata,
963 const int z,
964 const TaskParallelTLS *__restrict /*tls*/)
965{
966 ObstaclesFromDMData *data = static_cast<ObstaclesFromDMData *>(userdata);
967 FluidObjectBB *bb = data->bb;
968
969 for (int x = data->min[0]; x < data->max[0]; x++) {
970 for (int y = data->min[1]; y < data->max[1]; y++) {
971 const int index = manta_get_index(
972 x - bb->min[0], bb->res[0], y - bb->min[1], bb->res[1], z - bb->min[2]);
973 const float ray_start[3] = {float(x) + 0.5f, float(y) + 0.5f, float(z) + 0.5f};
974
975 /* Calculate levelset values from meshes. Result in bb->distances. */
976 update_distances(index,
977 bb->distances,
978 *data->tree,
979 ray_start,
980 data->fes->surface_distance,
981 data->fes->flags & FLUID_EFFECTOR_USE_PLANE_INIT);
982
983 /* Calculate object velocities. Result in bb->velocity. */
984 update_velocities(data->fes,
985 data->vert_positions,
986 data->corner_verts.data(),
987 data->corner_tris.data(),
988 bb->velocity,
989 index,
990 *data->tree,
991 ray_start,
992 data->vert_vel,
993 data->has_velocity);
994
995 /* Increase obstacle count inside of moving obstacles. */
996 if (bb->distances[index] < 0) {
997 bb->numobjs[index]++;
998 }
999 }
1000 }
1001}
1002
1003static void obstacles_from_mesh(Object *coll_ob,
1006 FluidObjectBB *bb,
1007 float dt)
1008{
1009 if (fes->mesh) {
1010 int numverts, i;
1011
1012 float *vert_vel = nullptr;
1013 bool has_velocity = false;
1014
1015 Mesh *mesh = BKE_mesh_copy_for_eval(*fes->mesh);
1016 MutableSpan<float3> positions = mesh->vert_positions_for_write();
1017
1018 int min[3], max[3], res[3];
1019
1020 const Span<int> corner_verts = mesh->corner_verts();
1021 const Span<int3> corner_tris = mesh->corner_tris();
1022 numverts = mesh->verts_num;
1023
1024 /* TODO(sebbas): Make initialization of vertex velocities optional? */
1025 {
1026 vert_vel = MEM_new_array_zeroed<float>(3 * size_t(numverts), "manta_obs_velocity");
1027
1028 if (fes->numverts != numverts || !fes->verts_old) {
1029 if (fes->verts_old) {
1030 MEM_delete(fes->verts_old);
1031 }
1032
1033 fes->verts_old = MEM_new_array_zeroed<float>(3 * size_t(numverts), "manta_obs_verts_old");
1034 fes->numverts = numverts;
1035 }
1036 else {
1037 has_velocity = true;
1038 }
1039 }
1040
1041 /* Transform mesh vertices to domain grid space for fast lookups.
1042 * This is valid because the mesh is copied above. */
1043 for (i = 0; i < numverts; i++) {
1044 float co[3];
1045
1046 /* Vertex position. */
1047 mul_m4_v3(coll_ob->object_to_world().ptr(), positions[i]);
1048 manta_pos_to_cell(fds, positions[i]);
1049
1050 /* Vertex velocity. */
1051 add_v3fl_v3fl_v3i(co, positions[i], fds->shift);
1052 if (has_velocity) {
1053 sub_v3_v3v3(&vert_vel[i * 3], co, &fes->verts_old[i * 3]);
1054 mul_v3_fl(&vert_vel[i * 3], 1.0f / dt);
1055 }
1056 copy_v3_v3(&fes->verts_old[i * 3], co);
1057
1058 /* Calculate emission map bounds. */
1059 bb_boundInsert(bb, positions[i]);
1060 }
1061
1062 mesh->tag_positions_changed();
1063
1064 /* Set emission map.
1065 * Use 3 cell diagonals as margin (3 * 1.732 = 5.196). */
1066 int bounds_margin = int(ceil(5.196));
1067 clamp_bounds_in_domain(fds, bb->min, bb->max, nullptr, nullptr, bounds_margin, dt);
1068 bb_allocateData(bb, true, false);
1069
1070 /* Setup loop bounds. */
1071 for (i = 0; i < 3; i++) {
1072 min[i] = bb->min[i];
1073 max[i] = bb->max[i];
1074 res[i] = bb->res[i];
1075 }
1076
1077 /* Skip effector sampling loop if object has disabled effector. */
1078 bool use_effector = fes->flags & FLUID_EFFECTOR_USE_EFFEC;
1079 if (use_effector && mesh->faces_num > 0) {
1080 const bke::bvh::Tree &tree_data = mesh->bvh_tris();
1081 ObstaclesFromDMData data{};
1082 data.fes = fes;
1083 data.vert_positions = positions;
1084 data.corner_verts = corner_verts;
1085 data.corner_tris = corner_tris;
1086 data.tree = &tree_data;
1087 data.bb = bb;
1088 data.has_velocity = has_velocity;
1089 data.vert_vel = vert_vel;
1090 data.min = min;
1091 data.max = max;
1092 data.res = res;
1093
1094 TaskParallelSettings settings;
1096 settings.min_iter_per_thread = 2;
1097 BLI_task_parallel_range(min[2], max[2], &data, obstacles_from_mesh_task_cb, &settings);
1098 }
1099
1100 if (vert_vel) {
1101 MEM_delete(vert_vel);
1102 }
1103 BKE_id_free(nullptr, mesh);
1104 }
1105}
1106
1107static void ensure_obstaclefields(FluidDomainSettings *fds)
1108{
1109 if (fds->active_fields & FLUID_DOMAIN_ACTIVE_OBSTACLE) {
1110 manta_ensure_obstacle(fds->fluid, fds->fmd);
1111 }
1112 if (fds->active_fields & FLUID_DOMAIN_ACTIVE_GUIDE) {
1113 manta_ensure_guiding(fds->fluid, fds->fmd);
1114 }
1115 manta_update_pointers(fds->fluid, fds->fmd, false);
1116}
1117
1118static void update_obstacleflags(FluidDomainSettings *fds,
1119 Object **coll_ob_array,
1120 int coll_ob_array_len)
1121{
1122 eFluidDomain_ActiveFields active_fields = fds->active_fields;
1123 uint coll_index;
1124
1125 /* First, remove all flags that we want to update. */
1128 active_fields &= ~prev_flags;
1129
1130 /* Monitor active fields based on flow settings */
1131 for (coll_index = 0; coll_index < coll_ob_array_len; coll_index++) {
1132 Object *coll_ob = coll_ob_array[coll_index];
1135
1136 /* Sanity check. */
1137 if (!fmd2) {
1138 continue;
1139 }
1140
1141 if ((fmd2->type & MOD_FLUID_TYPE_EFFEC) && fmd2->effector) {
1142 FluidEffectorSettings *fes = fmd2->effector;
1143 if (!fes) {
1144 break;
1145 }
1146 if (fes->flags & FLUID_EFFECTOR_NEEDS_UPDATE) {
1147 fes->flags &= ~FLUID_EFFECTOR_NEEDS_UPDATE;
1148 fds->cache_flag |= FLUID_DOMAIN_OUTDATED_DATA;
1149 }
1150 if (fes->type == FLUID_EFFECTOR_TYPE_COLLISION) {
1151 active_fields |= FLUID_DOMAIN_ACTIVE_OBSTACLE;
1152 }
1153 if (fes->type == FLUID_EFFECTOR_TYPE_GUIDE) {
1154 active_fields |= FLUID_DOMAIN_ACTIVE_GUIDE;
1155 }
1156 }
1157 }
1158 fds->active_fields = active_fields;
1159}
1160
1161static bool escape_effectorobject(Object *flowobj,
1163 FluidEffectorSettings * /*fes*/,
1164 int frame)
1165{
1166 bool is_static = is_static_object(flowobj);
1167
1168 bool is_resume = (fds->cache_frame_pause_data == frame);
1169 bool is_adaptive = (fds->flags & FLUID_DOMAIN_USE_ADAPTIVE_DOMAIN);
1170 bool is_first_frame = (frame == fds->cache_frame_start);
1171
1172 /* Cannot use static mode with adaptive domain.
1173 * The adaptive domain might expand and only later discover the static object. */
1174 if (is_adaptive) {
1175 is_static = false;
1176 }
1177 /* Skip static effector objects after initial frame. */
1178 if (is_static && !is_first_frame && !is_resume) {
1179 return true;
1180 }
1181 return false;
1182}
1183
1184static void compute_obstaclesemission(Scene *scene,
1185 FluidObjectBB *bb_maps,
1186 Depsgraph *depsgraph,
1187 float dt,
1188 Object **effecobjs,
1189 int frame,
1190 float frame_length,
1192 uint numeffecobjs,
1193 float time_per_frame)
1194{
1195 bool is_first_frame = (frame == fds->cache_frame_start);
1196
1197 /* Prepare effector maps. */
1198 for (int effec_index = 0; effec_index < numeffecobjs; effec_index++) {
1199 Object *effecobj = effecobjs[effec_index];
1202
1203 /* Sanity check. */
1204 if (!fmd2) {
1205 continue;
1206 }
1207
1208 /* Check for initialized effector object. */
1209 if ((fmd2->type & MOD_FLUID_TYPE_EFFEC) && fmd2->effector) {
1210 FluidEffectorSettings *fes = fmd2->effector;
1211 int subframes = fes->subframes;
1212 FluidObjectBB *bb = &bb_maps[effec_index];
1213
1214 /* Optimization: Skip this object under certain conditions. */
1215 if (escape_effectorobject(effecobj, fds, fes, frame)) {
1216 continue;
1217 }
1218
1219 /* First frame cannot have any subframes because there is (obviously) no previous frame from
1220 * where subframes could come from. */
1221 if (is_first_frame) {
1222 subframes = 0;
1223 }
1224
1225 /* More splitting because of emission subframe: If no subframes present, sample_size is 1. */
1226 float sample_size = 1.0f / float(subframes + 1);
1227 float subframe_dt = dt * sample_size;
1228
1229 /* Emission loop. When not using subframes this will loop only once. */
1230 for (int subframe = 0; subframe <= subframes; subframe++) {
1231
1232 /* Temporary emission map used when subframes are enabled, i.e. at least one subframe. */
1233 FluidObjectBB bb_temp = {nullptr};
1234
1235 /* Set scene time */
1236 /* Handle emission subframe */
1237 if ((subframe < subframes || time_per_frame + dt + FLT_EPSILON < frame_length) &&
1238 !is_first_frame)
1239 {
1240 scene->r.subframe = (time_per_frame + (subframe + 1.0f) * subframe_dt) / frame_length;
1241 scene->r.cfra = frame - 1;
1242 }
1243 else {
1244 scene->r.subframe = 0.0f;
1245 scene->r.cfra = frame;
1246 }
1247 /* Sanity check: subframe portion must be between 0 and 1. */
1248 CLAMP(scene->r.subframe, 0.0f, 1.0f);
1249# ifdef DEBUG_PRINT
1250 /* Debugging: Print subframe information. */
1251 printf(
1252 "effector: frame (is first: %d): %d // scene current frame: %d // scene current "
1253 "subframe: "
1254 "%f\n",
1255 is_first_frame,
1256 frame,
1257 scene->r.cfra,
1258 scene->r.subframe);
1259# endif
1260 /* Update frame time, this is considering current subframe fraction
1261 * BLI_mutex_lock() called in manta_step(), so safe to update subframe here
1262 * TODO(sebbas): Using BKE_scene_ctime_get(scene) instead of new DEG_get_ctime(depsgraph)
1263 * as subframes don't work with the latter yet. */
1265 scene,
1266 effecobj,
1267 true,
1269 BKE_scene_ctime_get(scene),
1271
1272 if (subframes) {
1273 obstacles_from_mesh(effecobj, fds, fes, &bb_temp, subframe_dt);
1274 }
1275 else {
1276 obstacles_from_mesh(effecobj, fds, fes, bb, subframe_dt);
1277 }
1278
1279 /* If this we emitted with temp emission map in this loop (subframe emission), we combine
1280 * the temp map with the original emission map. */
1281 if (subframes) {
1282 /* Combine emission maps. */
1283 bb_combineMaps(bb, &bb_temp, 0, 0.0f);
1284 bb_freeData(&bb_temp);
1285 }
1286 }
1287 }
1288 }
1289}
1290
1291static void update_obstacles(Depsgraph *depsgraph,
1292 Scene *scene,
1293 Object *ob,
1295 float time_per_frame,
1296 float frame_length,
1297 int frame,
1298 float dt)
1299{
1300 FluidObjectBB *bb_maps = nullptr;
1301 Object **effecobjs = nullptr;
1302 uint numeffecobjs = 0;
1303 bool is_resume = (fds->cache_frame_pause_data == frame);
1304 bool is_first_frame = (frame == fds->cache_frame_start);
1305
1306 effecobjs = BKE_collision_objects_create(
1307 depsgraph, ob, fds->effector_group, &numeffecobjs, eModifierType_Fluid);
1308
1309 /* Update all effector related flags and ensure that corresponding grids get initialized. */
1310 update_obstacleflags(fds, effecobjs, numeffecobjs);
1311 ensure_obstaclefields(fds);
1312
1313 /* Allocate effector map for each effector object. */
1314 bb_maps = MEM_new_array_zeroed<FluidObjectBB>(numeffecobjs, "fluid_effector_bb_maps");
1315
1316 /* Initialize effector map for each effector object. */
1317 compute_obstaclesemission(scene,
1318 bb_maps,
1319 depsgraph,
1320 dt,
1321 effecobjs,
1322 frame,
1323 frame_length,
1324 fds,
1325 numeffecobjs,
1326 time_per_frame);
1327
1328 float *vel_x = manta_get_ob_velocity_x(fds->fluid);
1329 float *vel_y = manta_get_ob_velocity_y(fds->fluid);
1330 float *vel_z = manta_get_ob_velocity_z(fds->fluid);
1331 float *vel_x_guide = manta_get_guide_velocity_x(fds->fluid);
1332 float *vel_y_guide = manta_get_guide_velocity_y(fds->fluid);
1333 float *vel_z_guide = manta_get_guide_velocity_z(fds->fluid);
1334 float *phi_obs_in = manta_get_phiobs_in(fds->fluid);
1335 float *phi_obsstatic_in = manta_get_phiobsstatic_in(fds->fluid);
1336 float *phi_guide_in = manta_get_phiguide_in(fds->fluid);
1337 float *num_obstacles = manta_get_num_obstacle(fds->fluid);
1338 float *num_guides = manta_get_num_guide(fds->fluid);
1339 uint z;
1340
1341 bool use_adaptivedomain = (fds->flags & FLUID_DOMAIN_USE_ADAPTIVE_DOMAIN);
1342
1343 /* Grid reset before writing again. */
1344 for (z = 0; z < fds->res[0] * fds->res[1] * fds->res[2]; z++) {
1345
1346 /* Use big value that's not inf to initialize levelset grids. */
1347 if (phi_obs_in) {
1348 phi_obs_in[z] = PHI_MAX;
1349 }
1350 /* Only reset static effectors on first frame. Only use static effectors without adaptive
1351 * domains. */
1352 if (phi_obsstatic_in && (is_first_frame || use_adaptivedomain)) {
1353 phi_obsstatic_in[z] = PHI_MAX;
1354 }
1355 if (phi_guide_in) {
1356 phi_guide_in[z] = PHI_MAX;
1357 }
1358 if (num_obstacles) {
1359 num_obstacles[z] = 0;
1360 }
1361 if (num_guides) {
1362 num_guides[z] = 0;
1363 }
1364 if (vel_x && vel_y && vel_z) {
1365 vel_x[z] = 0.0f;
1366 vel_y[z] = 0.0f;
1367 vel_z[z] = 0.0f;
1368 }
1369 if (vel_x_guide && vel_y_guide && vel_z_guide) {
1370 vel_x_guide[z] = 0.0f;
1371 vel_y_guide[z] = 0.0f;
1372 vel_z_guide[z] = 0.0f;
1373 }
1374 }
1375
1376 /* Prepare grids from effector objects. */
1377 for (int effec_index = 0; effec_index < numeffecobjs; effec_index++) {
1378 Object *effecobj = effecobjs[effec_index];
1381
1382 /* Sanity check. */
1383 if (!fmd2) {
1384 continue;
1385 }
1386
1387 /* Cannot use static mode with adaptive domain.
1388 * The adaptive domain might expand and only later in the simulations discover the static
1389 * object. */
1390 bool is_static = is_static_object(effecobj) && !use_adaptivedomain;
1391
1392 /* Check for initialized effector object. */
1393 if ((fmd2->type & MOD_FLUID_TYPE_EFFEC) && fmd2->effector) {
1394 FluidEffectorSettings *fes = fmd2->effector;
1395
1396 /* Optimization: Skip effector objects with disabled effec flag. */
1397 if ((fes->flags & FLUID_EFFECTOR_USE_EFFEC) == 0) {
1398 continue;
1399 }
1400
1401 FluidObjectBB *bb = &bb_maps[effec_index];
1402 float *velocity_map = bb->velocity;
1403 float *numobjs_map = bb->numobjs;
1404 float *distance_map = bb->distances;
1405
1406 int gx, gy, gz, ex, ey, ez, dx, dy, dz;
1407 size_t e_index, d_index;
1408
1409 /* Loop through every emission map cell. */
1410 for (gx = bb->min[0]; gx < bb->max[0]; gx++) {
1411 for (gy = bb->min[1]; gy < bb->max[1]; gy++) {
1412 for (gz = bb->min[2]; gz < bb->max[2]; gz++) {
1413 /* Compute emission map index. */
1414 ex = gx - bb->min[0];
1415 ey = gy - bb->min[1];
1416 ez = gz - bb->min[2];
1417 e_index = manta_get_index(ex, bb->res[0], ey, bb->res[1], ez);
1418
1419 /* Get domain index. */
1420 dx = gx - fds->res_min[0];
1421 dy = gy - fds->res_min[1];
1422 dz = gz - fds->res_min[2];
1423 d_index = manta_get_index(dx, fds->res[0], dy, fds->res[1], dz);
1424 /* Make sure emission cell is inside the new domain boundary. */
1425 if (dx < 0 || dy < 0 || dz < 0 || dx >= fds->res[0] || dy >= fds->res[1] ||
1426 dz >= fds->res[2])
1427 {
1428 continue;
1429 }
1430
1431 if (fes->type == FLUID_EFFECTOR_TYPE_COLLISION) {
1432 float *levelset = ((is_first_frame || is_resume) && is_static) ? phi_obsstatic_in :
1433 phi_obs_in;
1434 apply_effector_fields(fes,
1435 d_index,
1436 distance_map[e_index],
1437 levelset,
1438 numobjs_map[e_index],
1439 num_obstacles,
1440 &velocity_map[e_index * 3],
1441 vel_x,
1442 vel_y,
1443 vel_z);
1444 }
1445 if (fes->type == FLUID_EFFECTOR_TYPE_GUIDE) {
1446 apply_effector_fields(fes,
1447 d_index,
1448 distance_map[e_index],
1449 phi_guide_in,
1450 numobjs_map[e_index],
1451 num_guides,
1452 &velocity_map[e_index * 3],
1453 vel_x_guide,
1454 vel_y_guide,
1455 vel_z_guide);
1456 }
1457 }
1458 }
1459 } /* End of effector map loop. */
1460 bb_freeData(bb);
1461 } /* End of effector object loop. */
1462 }
1463
1464 BKE_collision_objects_free(effecobjs);
1465 if (bb_maps) {
1466 MEM_delete(bb_maps);
1467 }
1468}
1469
1471
1472/* -------------------------------------------------------------------- */
1475
1476struct EmitFromParticlesData {
1477 FluidFlowSettings *ffs;
1478 KDTree<float3> *tree;
1479
1480 FluidObjectBB *bb;
1481 float *particle_vel;
1482 int *min, *max, *res;
1483
1484 float solid;
1485 float smooth;
1486};
1487
1488static void emit_from_particles_task_cb(void *__restrict userdata,
1489 const int z,
1490 const TaskParallelTLS *__restrict /*tls*/)
1491{
1492 EmitFromParticlesData *data = static_cast<EmitFromParticlesData *>(userdata);
1493 FluidFlowSettings *ffs = data->ffs;
1494 FluidObjectBB *bb = data->bb;
1495
1496 for (int x = data->min[0]; x < data->max[0]; x++) {
1497 for (int y = data->min[1]; y < data->max[1]; y++) {
1498 const int index = manta_get_index(
1499 x - bb->min[0], bb->res[0], y - bb->min[1], bb->res[1], z - bb->min[2]);
1500 const float ray_start[3] = {float(x) + 0.5f, float(y) + 0.5f, float(z) + 0.5f};
1501
1502 /* Find particle distance from the kdtree. */
1503 KDTreeNearest<float3> nearest;
1504 const float range = data->solid + data->smooth;
1505 kdtree_find_nearest<float3>(data->tree, ray_start, &nearest);
1506
1507 if (nearest.dist < range) {
1508 bb->influence[index] = (nearest.dist < data->solid) ?
1509 1.0f :
1510 (1.0f - (nearest.dist - data->solid) / data->smooth);
1511 /* Uses particle velocity as initial velocity for smoke. */
1512 if (ffs->flags & FLUID_FLOW_INITVELOCITY && (ffs->psys->part->phystype != PART_PHYS_NO)) {
1513 madd_v3_v3fl(
1514 &bb->velocity[index * 3], &data->particle_vel[nearest.index * 3], ffs->vel_multi);
1515 }
1516 }
1517 }
1518 }
1519}
1520
1521static void emit_from_particles(Object *flow_ob,
1523 FluidFlowSettings *ffs,
1524 FluidObjectBB *bb,
1525 Depsgraph *depsgraph,
1526 Scene *scene,
1527 float dt)
1528{
1529 /* Is particle system selected. */
1530 if (ffs && ffs->psys && ffs->psys->part && ELEM(ffs->psys->part->type, PART_EMITTER, PART_FLUID))
1531 {
1533 ParticleSystem *psys = ffs->psys;
1534 float *particle_pos;
1535 float *particle_vel;
1536 int totpart = psys->totpart, totchild;
1537 int p = 0;
1538 int valid_particles = 0;
1539 int bounds_margin = 1;
1540
1541 /* radius based flow */
1542 const float solid = ffs->particle_size * 0.5f;
1543 const float smooth = 0.5f; /* add 0.5 cells of linear falloff to reduce aliasing */
1544 KDTree<float3> *tree = nullptr;
1545
1546 sim.depsgraph = depsgraph;
1547 sim.scene = scene;
1548 sim.ob = flow_ob;
1549 sim.psys = psys;
1550
1551 psys_sim_data_init(&sim);
1552
1553 /* initialize particle cache */
1554 if (psys->part->type == PART_HAIR) {
1555 /* TODO: PART_HAIR not supported whatsoever. */
1556 totchild = 0;
1557 }
1558 else {
1559 totchild = psys->totchild * psys->part->disp / 100;
1560 }
1561
1562 particle_pos = MEM_new_array_zeroed<float>(3 * size_t(totpart + totchild),
1563 "manta_flow_particles_pos");
1564 particle_vel = MEM_new_array_zeroed<float>(3 * size_t(totpart + totchild),
1565 "manta_flow_particles_vel");
1566
1567 /* setup particle radius emission if enabled */
1568 if (ffs->flags & FLUID_FLOW_USE_PART_SIZE) {
1569 tree = kdtree_new<float3>(psys->totpart + psys->totchild);
1570 bounds_margin = int(ceil(solid + smooth));
1571 }
1572
1573 /* calculate local position for each particle */
1574 for (p = 0; p < totpart + totchild; p++) {
1576 float *pos, *vel;
1577 if (p < totpart) {
1578 if (psys->particles[p].flag & (PARS_NO_DISP | PARS_UNEXIST)) {
1579 continue;
1580 }
1581 }
1582 else {
1583 /* handle child particle */
1584 ChildParticle *cpa = &psys->child[p - totpart];
1585 if (psys->particles[cpa->parent].flag & (PARS_NO_DISP | PARS_UNEXIST)) {
1586 continue;
1587 }
1588 }
1589
1590 /* `DEG_get_ctime(depsgraph)` does not give sub-frame time. */
1592
1593 if (psys_get_particle_state(&sim, p, &state, false) == 0) {
1594 continue;
1595 }
1596
1597 /* location */
1598 pos = &particle_pos[valid_particles * 3];
1599 copy_v3_v3(pos, state.co);
1600 manta_pos_to_cell(fds, pos);
1601
1602 /* velocity */
1603 vel = &particle_vel[valid_particles * 3];
1604 copy_v3_v3(vel, state.vel);
1605 mul_mat3_m4_v3(fds->imat, &particle_vel[valid_particles * 3]);
1606
1607 if (ffs->flags & FLUID_FLOW_USE_PART_SIZE) {
1608 kdtree_insert<float3>(tree, valid_particles, pos);
1609 }
1610
1611 /* calculate emission map bounds */
1612 bb_boundInsert(bb, pos);
1613 valid_particles++;
1614 }
1615
1616 /* set emission map */
1617 clamp_bounds_in_domain(fds, bb->min, bb->max, nullptr, nullptr, bounds_margin, dt);
1618 bb_allocateData(bb, ffs->flags & FLUID_FLOW_INITVELOCITY, true);
1619
1620 if (!(ffs->flags & FLUID_FLOW_USE_PART_SIZE)) {
1621 for (p = 0; p < valid_particles; p++) {
1622 int cell[3];
1623 size_t i = 0;
1624 size_t index = 0;
1625 int badcell = 0;
1626
1627 /* 1. get corresponding cell */
1628 cell[0] = floor(particle_pos[p * 3]) - bb->min[0];
1629 cell[1] = floor(particle_pos[p * 3 + 1]) - bb->min[1];
1630 cell[2] = floor(particle_pos[p * 3 + 2]) - bb->min[2];
1631 /* check if cell is valid (in the domain boundary) */
1632 for (i = 0; i < 3; i++) {
1633 if ((cell[i] > bb->res[i] - 1) || (cell[i] < 0)) {
1634 badcell = 1;
1635 break;
1636 }
1637 }
1638 if (badcell) {
1639 continue;
1640 }
1641 /* get cell index */
1642 index = manta_get_index(cell[0], bb->res[0], cell[1], bb->res[1], cell[2]);
1643 /* Add influence to emission map */
1644 bb->influence[index] = 1.0f;
1645 /* Uses particle velocity as initial velocity for smoke */
1646 if (ffs->flags & FLUID_FLOW_INITVELOCITY && (psys->part->phystype != PART_PHYS_NO)) {
1647 madd_v3_v3fl(&bb->velocity[index * 3], &particle_vel[p * 3], ffs->vel_multi);
1648 }
1649 } /* particles loop */
1650 }
1651 else if (valid_particles > 0) { /* #FLUID_FLOW_USE_PART_SIZE */
1652 int min[3], max[3], res[3];
1653
1654 /* setup loop bounds */
1655 for (int i = 0; i < 3; i++) {
1656 min[i] = bb->min[i];
1657 max[i] = bb->max[i];
1658 res[i] = bb->res[i];
1659 }
1660
1662
1663 EmitFromParticlesData data{};
1664 data.ffs = ffs;
1665 data.tree = tree;
1666 data.bb = bb;
1667 data.particle_vel = particle_vel;
1668 data.min = min;
1669 data.max = max;
1670 data.res = res;
1671 data.solid = solid;
1672 data.smooth = smooth;
1673
1674 TaskParallelSettings settings;
1676 settings.min_iter_per_thread = 2;
1677 BLI_task_parallel_range(min[2], max[2], &data, emit_from_particles_task_cb, &settings);
1678 }
1679
1680 if (ffs->flags & FLUID_FLOW_USE_PART_SIZE) {
1681 kdtree_free<float3>(tree);
1682 }
1683
1684 /* free data */
1685 if (particle_pos) {
1686 MEM_delete(particle_pos);
1687 }
1688 if (particle_vel) {
1689 MEM_delete(particle_vel);
1690 }
1691
1692 psys_sim_data_free(&sim);
1693 }
1694}
1695
1696/* Calculate map of (minimum) distances to flow/obstacle surface. Distances outside mesh are
1697 * positive, inside negative. */
1698static void update_distances(int index,
1699 float *distance_map,
1700 const bke::bvh::Tree &tree,
1701 const float ray_start[3],
1702 float surface_thickness,
1703 bool use_plane_init)
1704{
1705 float min_dist = PHI_MAX;
1706
1707 /* Planar initialization: Find nearest cells around mesh. */
1708 if (use_plane_init) {
1709 /* Distance between two opposing vertices in a unit cube.
1710 * I.e. the unit cube diagonal or `sqrt(3)`.
1711 * This value is our nearest neighbor search distance. */
1712 float surface_distance = 1.732;
1713 /* find_nearest uses squared distance. */
1714
1715 /* Subtract optional surface thickness value and virtually increase the object size. */
1716 if (surface_thickness) {
1717 surface_distance = std::sqrt(surface_distance * surface_distance + surface_thickness);
1718 }
1719 if (const std::optional<bke::bvh::ClosestPointResult> nearest = tree.closest_point(
1720 ray_start, surface_distance))
1721 {
1722 float ray[3] = {0};
1723 sub_v3_v3v3(ray, ray_start, nearest->position);
1724 min_dist = len_v3(ray);
1725 min_dist = (-1.0f) * fabsf(min_dist);
1726 }
1727 }
1728 /* Volumetric initialization: Ray-casts around mesh object. */
1729 else {
1730 /* Ray-casts in 26 directions.
1731 * (6 main axis + 12 quadrant diagonals (2D) + 8 octant diagonals (3D)). */
1732 float ray_dirs[26][3] = {
1733 {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {-1.0f, 0.0f, 0.0f},
1734 {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {1.0f, 1.0f, 0.0f}, {1.0f, -1.0f, 0.0f},
1735 {-1.0f, 1.0f, 0.0f}, {-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 1.0f}, {1.0f, 0.0f, -1.0f},
1736 {-1.0f, 0.0f, 1.0f}, {-1.0f, 0.0f, -1.0f}, {0.0f, 1.0f, 1.0f}, {0.0f, 1.0f, -1.0f},
1737 {0.0f, -1.0f, 1.0f}, {0.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f}, {1.0f, -1.0f, 1.0f},
1738 {-1.0f, 1.0f, 1.0f}, {-1.0f, -1.0f, 1.0f}, {1.0f, 1.0f, -1.0f}, {1.0f, -1.0f, -1.0f},
1739 {-1.0f, 1.0f, -1.0f}, {-1.0f, -1.0f, -1.0f}};
1740
1741 /* Count ray mesh misses (i.e. no face hit) and cases where the ray direction matches the face
1742 * normal direction. From this information it can be derived whether a cell is inside or
1743 * outside the mesh. */
1744 int miss_count = 0, dir_count = 0;
1745
1746 for (int i = 0; i < ARRAY_SIZE(ray_dirs); i++) {
1747 normalize_v3(ray_dirs[i]);
1748 const bke::bvh::Ray ray(ray_start, ray_dirs[i], PHI_MAX);
1749 const std::optional<bke::bvh::RayHit> hit_tree = tree.ray_intersect(ray);
1750 /* Ray did not hit mesh.
1751 * Current point definitely not inside mesh. Inside mesh as all rays have to hit. */
1752 if (!hit_tree) {
1753 miss_count++;
1754 /* Skip this ray since nothing was hit. */
1755 continue;
1756 }
1757
1758 /* Ray and normal are pointing in opposite directions. */
1759 if (dot_v3v3(ray_dirs[i], math::normalize(hit_tree->normal)) <= 0) {
1760 dir_count++;
1761 }
1762
1763 min_dist = std::min(hit_tree->distance, min_dist);
1764 }
1765
1766 /* Point lies inside mesh. Use negative sign for distance value.
1767 * This "if statement" has 2 conditions that can be true for points outside mesh. */
1768 if (!(miss_count > 0 || dir_count == ARRAY_SIZE(ray_dirs))) {
1769 min_dist = (-1.0f) * fabsf(min_dist);
1770 }
1771
1772 /* Subtract optional surface thickness value and virtually increase the object size. */
1773 if (surface_thickness) {
1774 min_dist -= surface_thickness;
1775 }
1776 }
1777
1778 /* Update global distance array but ensure that older entries are not overridden. */
1779 distance_map[index] = std::min(distance_map[index], min_dist);
1780
1781 /* Sanity check: Ensure that distances don't explode. */
1782 CLAMP(distance_map[index], -PHI_MAX, PHI_MAX);
1783}
1784
1785static void sample_mesh(FluidFlowSettings *ffs,
1786 Span<float3> vert_positions,
1787 const Span<float3> vert_normals,
1788 const int *corner_verts,
1789 const int3 *corner_tris,
1790 Span<float2> uv_map,
1791 float *influence_map,
1792 float *velocity_map,
1793 int index,
1794 const int base_res[3],
1795 const float global_size[3],
1796 const float flow_center[3],
1797 const bke::bvh::Tree &tree,
1798 const float ray_start[3],
1799 const float *vert_vel,
1800 bool has_velocity,
1801 int defgrp_index,
1802 const MDeformVert *dvert,
1803 float x,
1804 float y,
1805 float z)
1806{
1807 float ray_dir[3] = {1.0f, 0.0f, 0.0f};
1808
1809 float volume_factor = 0.0f;
1810
1811 /* Distance between two opposing vertices in a unit cube.
1812 * I.e. the unit cube diagonal or `sqrt(3)`.
1813 * This value is our nearest neighbor search distance. */
1814 const float surface_distance = 1.732;
1815
1816 bool is_gas_flow = ELEM(
1818
1819 /* Emission strength for gases will be computed below.
1820 * For liquids it's not needed. Just set to non zero value
1821 * to allow initial velocity computation. */
1822 float emission_strength = (is_gas_flow) ? 0.0f : 1.0f;
1823
1824 /* Emission inside the flow object. */
1825 if (is_gas_flow && ffs->volume_density) {
1826 const bke::bvh::Ray ray(ray_start, ray_dir, PHI_MAX);
1827 if (const std::optional<bke::bvh::RayHit> hit = tree.ray_intersect(ray)) {
1828 const float dot = math::dot(float3(ray_dir), math::normalize(hit->normal));
1829 /* If ray and hit face normal are facing same direction hit point is inside a closed mesh. */
1830 if (dot >= 0) {
1831 /* Also cast a ray in opposite direction to make sure point is at least surrounded by two
1832 * faces. */
1833 negate_v3(ray_dir);
1834 const bke::bvh::Ray opposite_ray(ray_start, ray_dir, PHI_MAX);
1835 const std::optional<bke::bvh::RayHit> hit_opposite = tree.ray_intersect(opposite_ray);
1836 if (hit_opposite) {
1837 volume_factor = ffs->volume_density;
1838 }
1839 }
1840 }
1841 }
1842
1843 /* Find the nearest point on the mesh. */
1844 if (const std::optional<bke::bvh::ClosestPointResult> nearest = tree.closest_point(
1845 ray_start, surface_distance))
1846 {
1847 float weights[3];
1848 int v1, v2, v3, tri_i = nearest->index;
1849 float hit_normal[3];
1850
1851 /* Calculate barycentric weights for nearest point. */
1852 v1 = corner_verts[corner_tris[tri_i][0]];
1853 v2 = corner_verts[corner_tris[tri_i][1]];
1854 v3 = corner_verts[corner_tris[tri_i][2]];
1856 weights, vert_positions[v1], vert_positions[v2], vert_positions[v3], nearest->position);
1857
1858 /* Compute emission strength for smoke flow. */
1859 if (is_gas_flow) {
1860 /* Emission from surface is based on UI configurable distance value. */
1861 if (ffs->surface_distance) {
1862 emission_strength = math::distance(nearest->position, float3(ray_start)) /
1863 ffs->surface_distance;
1864 CLAMP(emission_strength, 0.0f, 1.0f);
1865 emission_strength = pow(1.0f - emission_strength, 0.5f);
1866 }
1867 else {
1868 emission_strength = 0.0f;
1869 }
1870
1871 /* Apply vertex group influence if it is being used. */
1872 if (defgrp_index != -1 && dvert) {
1873 float weight_mask = BKE_defvert_find_weight(&dvert[v1], defgrp_index) * weights[0] +
1874 BKE_defvert_find_weight(&dvert[v2], defgrp_index) * weights[1] +
1875 BKE_defvert_find_weight(&dvert[v3], defgrp_index) * weights[2];
1876 emission_strength *= weight_mask;
1877 }
1878
1879 /* Apply emission texture. */
1880 if ((ffs->flags & FLUID_FLOW_TEXTUREEMIT) && ffs->noise_texture) {
1881 float tex_co[3] = {0};
1882 TexResult texres;
1883
1884 if (ffs->texture_type == FLUID_FLOW_TEXTURE_MAP_AUTO) {
1885 tex_co[0] = ((x - flow_center[0]) / base_res[0]) / ffs->texture_size;
1886 tex_co[1] = ((y - flow_center[1]) / base_res[1]) / ffs->texture_size;
1887 tex_co[2] = ((z - flow_center[2]) / base_res[2] - ffs->texture_offset) /
1888 ffs->texture_size;
1889 }
1890 else if (!uv_map.is_empty()) {
1891 const float *uv[3];
1892 uv[0] = uv_map[corner_tris[tri_i][0]];
1893 uv[1] = uv_map[corner_tris[tri_i][1]];
1894 uv[2] = uv_map[corner_tris[tri_i][2]];
1895
1896 interp_v2_v2v2v2(tex_co, UNPACK3(uv), weights);
1897
1898 /* Map texture coord between -1.0f and 1.0f. */
1899 tex_co[0] = tex_co[0] * 2.0f - 1.0f;
1900 tex_co[1] = tex_co[1] * 2.0f - 1.0f;
1901 tex_co[2] = ffs->texture_offset;
1902 }
1903 BKE_texture_get_value(ffs->noise_texture, tex_co, &texres, false);
1904 emission_strength *= texres.tin;
1905 }
1906 }
1907
1908 /* Initial velocity of flow object. Only compute velocity if emission is present. */
1909 if (ffs->flags & FLUID_FLOW_INITVELOCITY && velocity_map && emission_strength != 0.0) {
1910 /* Apply normal directional velocity. */
1911 if (ffs->vel_normal) {
1912 /* Interpolate vertex normal vectors to get nearest point normal. */
1914 hit_normal, vert_normals[v1], vert_normals[v2], vert_normals[v3], weights);
1915 normalize_v3(hit_normal);
1916
1917 /* Apply normal directional velocity. */
1918 velocity_map[index * 3] += hit_normal[0] * ffs->vel_normal;
1919 velocity_map[index * 3 + 1] += hit_normal[1] * ffs->vel_normal;
1920 velocity_map[index * 3 + 2] += hit_normal[2] * ffs->vel_normal;
1921 }
1922 /* Apply object velocity. */
1923 if (has_velocity && ffs->vel_multi) {
1924 float hit_vel[3];
1926 hit_vel, &vert_vel[v1 * 3], &vert_vel[v2 * 3], &vert_vel[v3 * 3], weights);
1927 velocity_map[index * 3] += hit_vel[0] * ffs->vel_multi;
1928 velocity_map[index * 3 + 1] += hit_vel[1] * ffs->vel_multi;
1929 velocity_map[index * 3 + 2] += hit_vel[2] * ffs->vel_multi;
1930# ifdef DEBUG_PRINT
1931 /* Debugging: Print flow object velocities. */
1932 printf("adding flow object vel: [%f, %f, %f]\n", hit_vel[0], hit_vel[1], hit_vel[2]);
1933# endif
1934 }
1935 /* Convert XYZ velocities flow settings from world to grid space. */
1936 float convert_vel[3];
1937 copy_v3_v3(convert_vel, ffs->vel_coord);
1938 float time_mult = 1.0 / (25.0f * DT_DEFAULT);
1939 float size_mult = std::max({base_res[0], base_res[1], base_res[2]}) /
1940 std::max({global_size[0], global_size[1], global_size[2]});
1941 mul_v3_v3fl(convert_vel, ffs->vel_coord, size_mult * time_mult);
1942
1943 velocity_map[index * 3] += convert_vel[0];
1944 velocity_map[index * 3 + 1] += convert_vel[1];
1945 velocity_map[index * 3 + 2] += convert_vel[2];
1946# ifdef DEBUG_PRINT
1947 printf("initial vel: [%f, %f, %f]\n",
1948 velocity_map[index * 3],
1949 velocity_map[index * 3 + 1],
1950 velocity_map[index * 3 + 2]);
1951# endif
1952 }
1953 }
1954
1955 /* Apply final influence value but also consider volume initialization factor. */
1956 influence_map[index] = std::max(volume_factor, emission_strength);
1957}
1958
1959struct EmitFromDMData {
1960 FluidDomainSettings *fds;
1961 FluidFlowSettings *ffs;
1962
1963 Span<float3> vert_positions;
1964 Span<float3> vert_normals;
1965 Span<int> corner_verts;
1966 Span<int3> corner_tris;
1967 Span<float2> uv_map;
1968 const MDeformVert *dvert;
1969 int defgrp_index;
1970
1971 const bke::bvh::Tree *tree;
1972 FluidObjectBB *bb;
1973
1974 bool has_velocity;
1975 float *vert_vel;
1976 float *flow_center;
1977 int *min, *max, *res;
1978};
1979
1980static void emit_from_mesh_task_cb(void *__restrict userdata,
1981 const int z,
1982 const TaskParallelTLS *__restrict /*tls*/)
1983{
1984 EmitFromDMData *data = static_cast<EmitFromDMData *>(userdata);
1985 FluidObjectBB *bb = data->bb;
1986
1987 for (int x = data->min[0]; x < data->max[0]; x++) {
1988 for (int y = data->min[1]; y < data->max[1]; y++) {
1989 const int index = manta_get_index(
1990 x - bb->min[0], bb->res[0], y - bb->min[1], bb->res[1], z - bb->min[2]);
1991 const float ray_start[3] = {float(x) + 0.5f, float(y) + 0.5f, float(z) + 0.5f};
1992
1993 /* Compute emission only for flow objects that produce fluid (i.e. skip outflow objects).
1994 * Result in bb->influence. Also computes initial velocities. Result in bb->velocity. */
1996 sample_mesh(data->ffs,
1997 data->vert_positions,
1998 data->vert_normals,
1999 data->corner_verts.data(),
2000 data->corner_tris.data(),
2001 data->uv_map,
2002 bb->influence,
2003 bb->velocity,
2004 index,
2005 data->fds->base_res,
2006 data->fds->global_size,
2007 data->flow_center,
2008 *data->tree,
2009 ray_start,
2010 data->vert_vel,
2011 data->has_velocity,
2012 data->defgrp_index,
2013 data->dvert,
2014 float(x),
2015 float(y),
2016 float(z));
2017 }
2018
2019 /* Calculate levelset values from meshes. Result in bb->distances. */
2020 update_distances(index,
2021 bb->distances,
2022 *data->tree,
2023 ray_start,
2024 data->ffs->surface_distance,
2025 data->ffs->flags & FLUID_FLOW_USE_PLANE_INIT);
2026 }
2027 }
2028}
2029
2030static void emit_from_mesh(
2031 Object *flow_ob, FluidDomainSettings *fds, FluidFlowSettings *ffs, FluidObjectBB *bb, float dt)
2032{
2033 if (ffs->mesh) {
2034 int i;
2035
2036 float *vert_vel = nullptr;
2037 bool has_velocity = false;
2038
2039 int defgrp_index = ffs->vgroup_density - 1;
2040 float flow_center[3] = {0};
2041 int min[3], max[3], res[3];
2042
2043 /* Copy mesh for thread safety as we modify it.
2044 * Main issue is its VertArray being modified, then replaced and freed. */
2045 Mesh *mesh = BKE_mesh_copy_for_eval(*ffs->mesh);
2046 MutableSpan<float3> positions = mesh->vert_positions_for_write();
2047
2048 const Span<int> corner_verts = mesh->corner_verts();
2049 const Span<int3> corner_tris = mesh->corner_tris();
2050 const int numverts = mesh->verts_num;
2051 const MDeformVert *dvert = mesh->deform_verts().data();
2052 const bke::AttributeAccessor attributes = mesh->attributes();
2053 const VArraySpan uv_map = *attributes.lookup<float2>(ffs->uvlayer_name,
2055
2056 if (ffs->flags & FLUID_FLOW_INITVELOCITY) {
2057 vert_vel = MEM_new_array_zeroed<float>(3 * size_t(numverts), "manta_flow_velocity");
2058
2059 if (ffs->numverts != numverts || !ffs->verts_old) {
2060 if (ffs->verts_old) {
2061 MEM_delete(ffs->verts_old);
2062 }
2063 ffs->verts_old = MEM_new_array_zeroed<float>(3 * size_t(numverts), "manta_flow_verts_old");
2064 ffs->numverts = numverts;
2065 }
2066 else {
2067 has_velocity = true;
2068 }
2069 }
2070
2071 /* Transform mesh vertices to domain grid space for fast lookups.
2072 * This is valid because the mesh is copied above. */
2073 for (i = 0; i < numverts; i++) {
2074 /* Vertex position. */
2075 mul_m4_v3(flow_ob->object_to_world().ptr(), positions[i]);
2076 manta_pos_to_cell(fds, positions[i]);
2077
2078 /* Vertex velocity. */
2079 if (ffs->flags & FLUID_FLOW_INITVELOCITY) {
2080 float co[3];
2081 add_v3fl_v3fl_v3i(co, positions[i], fds->shift);
2082 if (has_velocity) {
2083 sub_v3_v3v3(&vert_vel[i * 3], co, &ffs->verts_old[i * 3]);
2084 mul_v3_fl(&vert_vel[i * 3], 1.0 / dt);
2085 }
2086 copy_v3_v3(&ffs->verts_old[i * 3], co);
2087 }
2088
2089 /* Calculate emission map bounds. */
2090 bb_boundInsert(bb, positions[i]);
2091 }
2092 mesh->tag_positions_changed();
2093 mul_m4_v3(flow_ob->object_to_world().ptr(), flow_center);
2094 manta_pos_to_cell(fds, flow_center);
2095
2096 /* Set emission map.
2097 * Use 3 cell diagonals as margin (3 * 1.732 = 5.196). */
2098 int bounds_margin = int(ceil(5.196));
2099 clamp_bounds_in_domain(fds, bb->min, bb->max, nullptr, nullptr, bounds_margin, dt);
2100 bb_allocateData(bb, ffs->flags & FLUID_FLOW_INITVELOCITY, true);
2101
2102 /* Setup loop bounds. */
2103 for (i = 0; i < 3; i++) {
2104 min[i] = bb->min[i];
2105 max[i] = bb->max[i];
2106 res[i] = bb->res[i];
2107 }
2108
2109 /* Skip flow sampling loop if object has disabled flow. */
2110 bool use_flow = ffs->flags & FLUID_FLOW_USE_INFLOW;
2111 if (use_flow && mesh->faces_num > 0) {
2112 const bke::bvh::Tree &tree_data = mesh->bvh_tris();
2113
2114 EmitFromDMData data{};
2115 data.fds = fds;
2116 data.ffs = ffs;
2117 data.vert_positions = positions;
2118 data.vert_normals = mesh->vert_normals();
2119 data.corner_verts = corner_verts;
2120 data.corner_tris = corner_tris;
2121 data.uv_map = uv_map;
2122 data.dvert = dvert;
2123 data.defgrp_index = defgrp_index;
2124 data.tree = &tree_data;
2125 data.bb = bb;
2126 data.has_velocity = has_velocity;
2127 data.vert_vel = vert_vel;
2128 data.flow_center = flow_center;
2129 data.min = min;
2130 data.max = max;
2131 data.res = res;
2132
2133 TaskParallelSettings settings;
2135 settings.min_iter_per_thread = 2;
2136 BLI_task_parallel_range(min[2], max[2], &data, emit_from_mesh_task_cb, &settings);
2137 }
2138
2139 if (vert_vel) {
2140 MEM_delete(vert_vel);
2141 }
2142 BKE_id_free(nullptr, mesh);
2143 }
2144}
2145
2147
2148/* -------------------------------------------------------------------- */
2151
2152static void adaptive_domain_adjust(
2153 FluidDomainSettings *fds, Object *ob, FluidObjectBB *bb_maps, uint numflowobj, float dt)
2154{
2155 /* calculate domain shift for current frame */
2156 int new_shift[3] = {0};
2157 int total_shift[3];
2158 float frame_shift_f[3];
2159 float ob_loc[3] = {0};
2160
2161 mul_m4_v3(ob->object_to_world().ptr(), ob_loc);
2162
2163 sub_v3_v3v3(frame_shift_f, ob_loc, fds->prev_loc);
2164 copy_v3_v3(fds->prev_loc, ob_loc);
2165 /* convert global space shift to local "cell" space */
2166 mul_mat3_m4_v3(fds->imat, frame_shift_f);
2167 frame_shift_f[0] = frame_shift_f[0] / fds->cell_size[0];
2168 frame_shift_f[1] = frame_shift_f[1] / fds->cell_size[1];
2169 frame_shift_f[2] = frame_shift_f[2] / fds->cell_size[2];
2170 /* add to total shift */
2171 add_v3_v3(fds->shift_f, frame_shift_f);
2172 /* convert to integer */
2173 total_shift[0] = int(floorf(fds->shift_f[0]));
2174 total_shift[1] = int(floorf(fds->shift_f[1]));
2175 total_shift[2] = int(floorf(fds->shift_f[2]));
2176 int temp_shift[3];
2177 copy_v3_v3_int(temp_shift, fds->shift);
2178 sub_v3_v3v3_int(new_shift, total_shift, fds->shift);
2179 copy_v3_v3_int(fds->shift, total_shift);
2180
2181 /* calculate new domain boundary points so that smoke doesn't slide on sub-cell movement */
2182 fds->p0[0] = fds->dp0[0] - fds->cell_size[0] * (fds->shift_f[0] - total_shift[0] - 0.5f);
2183 fds->p0[1] = fds->dp0[1] - fds->cell_size[1] * (fds->shift_f[1] - total_shift[1] - 0.5f);
2184 fds->p0[2] = fds->dp0[2] - fds->cell_size[2] * (fds->shift_f[2] - total_shift[2] - 0.5f);
2185 fds->p1[0] = fds->p0[0] + fds->cell_size[0] * fds->base_res[0];
2186 fds->p1[1] = fds->p0[1] + fds->cell_size[1] * fds->base_res[1];
2187 fds->p1[2] = fds->p0[2] + fds->cell_size[2] * fds->base_res[2];
2188
2189 /* adjust domain resolution */
2190 const int block_size = fds->noise_scale;
2191 int min[3] = {32767, 32767, 32767}, max[3] = {-32767, -32767, -32767}, res[3];
2192 int total_cells = 1, res_changed = 0, shift_changed = 0;
2193 float min_vel[3], max_vel[3];
2194 int x, y, z;
2195 float *density = manta_smoke_get_density(fds->fluid);
2196 float *fuel = manta_smoke_get_fuel(fds->fluid);
2197 float *heat = manta_smoke_get_heat(fds->fluid);
2198 float *bigdensity = manta_noise_get_density(fds->fluid);
2199 float *bigfuel = manta_noise_get_fuel(fds->fluid);
2200 float *vx = manta_get_velocity_x(fds->fluid);
2201 float *vy = manta_get_velocity_y(fds->fluid);
2202 float *vz = manta_get_velocity_z(fds->fluid);
2203 int wt_res[3];
2204
2205 if (fds->flags & FLUID_DOMAIN_USE_NOISE && fds->fluid) {
2206 manta_noise_get_res(fds->fluid, wt_res);
2207 }
2208
2209 INIT_MINMAX(min_vel, max_vel);
2210
2211 /* Calculate bounds for current domain content */
2212 for (x = fds->res_min[0]; x < fds->res_max[0]; x++) {
2213 for (y = fds->res_min[1]; y < fds->res_max[1]; y++) {
2214 for (z = fds->res_min[2]; z < fds->res_max[2]; z++) {
2215 int xn = x - new_shift[0];
2216 int yn = y - new_shift[1];
2217 int zn = z - new_shift[2];
2218 int index;
2219 float max_den;
2220
2221 /* skip if cell already belongs to new area */
2222 if (xn >= min[0] && xn <= max[0] && yn >= min[1] && yn <= max[1] && zn >= min[2] &&
2223 zn <= max[2])
2224 {
2225 continue;
2226 }
2227
2228 index = manta_get_index(x - fds->res_min[0],
2229 fds->res[0],
2230 y - fds->res_min[1],
2231 fds->res[1],
2232 z - fds->res_min[2]);
2233 max_den = (fuel) ? std::max(density[index], fuel[index]) : density[index];
2234 max_den = (heat) ? std::max(max_den, heat[index]) : max_den;
2235
2236 /* Check high resolution bounds if max density isn't already high enough. */
2237 if (max_den < fds->adapt_threshold && fds->flags & FLUID_DOMAIN_USE_NOISE && fds->fluid) {
2238 int i, j, k;
2239 /* high res grid index */
2240 int xx = (x - fds->res_min[0]) * block_size;
2241 int yy = (y - fds->res_min[1]) * block_size;
2242 int zz = (z - fds->res_min[2]) * block_size;
2243
2244 for (i = 0; i < block_size; i++) {
2245 for (j = 0; j < block_size; j++) {
2246 for (k = 0; k < block_size; k++) {
2247 int big_index = manta_get_index(xx + i, wt_res[0], yy + j, wt_res[1], zz + k);
2248 float den = (bigfuel) ? std::max(bigdensity[big_index], bigfuel[big_index]) :
2249 bigdensity[big_index];
2250 max_den = std::max(den, max_den);
2251 }
2252 }
2253 }
2254 }
2255
2256 /* content bounds (use shifted coordinates) */
2257 if (max_den >= fds->adapt_threshold) {
2258 min[0] = std::min(min[0], xn);
2259 min[1] = std::min(min[1], yn);
2260 min[2] = std::min(min[2], zn);
2261 max[0] = std::max(max[0], xn);
2262 max[1] = std::max(max[1], yn);
2263 max[2] = std::max(max[2], zn);
2264 }
2265
2266 /* velocity bounds */
2267 min_vel[0] = std::min(min_vel[0], vx[index]);
2268 min_vel[1] = std::min(min_vel[1], vy[index]);
2269 min_vel[2] = std::min(min_vel[2], vz[index]);
2270 max_vel[0] = std::max(max_vel[0], vx[index]);
2271 max_vel[1] = std::max(max_vel[1], vy[index]);
2272 max_vel[2] = std::max(max_vel[2], vz[index]);
2273 }
2274 }
2275 }
2276
2277 /* also apply emission maps */
2278 for (int i = 0; i < numflowobj; i++) {
2279 FluidObjectBB *bb = &bb_maps[i];
2280
2281 for (x = bb->min[0]; x < bb->max[0]; x++) {
2282 for (y = bb->min[1]; y < bb->max[1]; y++) {
2283 for (z = bb->min[2]; z < bb->max[2]; z++) {
2284 int index = manta_get_index(
2285 x - bb->min[0], bb->res[0], y - bb->min[1], bb->res[1], z - bb->min[2]);
2286 float max_den = bb->influence[index];
2287
2288 /* density bounds */
2289 if (max_den >= fds->adapt_threshold) {
2290 min[0] = std::min(min[0], x);
2291 min[1] = std::min(min[1], y);
2292 min[2] = std::min(min[2], z);
2293 max[0] = std::max(max[0], x);
2294 max[1] = std::max(max[1], y);
2295 max[2] = std::max(max[2], z);
2296 }
2297 }
2298 }
2299 }
2300 }
2301
2302 /* calculate new bounds based on these values */
2303 clamp_bounds_in_domain(fds, min, max, min_vel, max_vel, fds->adapt_margin + 1, dt);
2304
2305 for (int i = 0; i < 3; i++) {
2306 /* calculate new resolution */
2307 res[i] = max[i] - min[i];
2308 total_cells *= res[i];
2309
2310 if (new_shift[i]) {
2311 shift_changed = 1;
2312 }
2313
2314 /* if no content set minimum dimensions */
2315 if (res[i] <= 0) {
2316 int j;
2317 for (j = 0; j < 3; j++) {
2318 min[j] = 0;
2319 max[j] = 1;
2320 res[j] = 1;
2321 }
2322 res_changed = 1;
2323 total_cells = 1;
2324 break;
2325 }
2326 if (min[i] != fds->res_min[i] || max[i] != fds->res_max[i]) {
2327 res_changed = 1;
2328 }
2329 }
2330
2331 if (res_changed || shift_changed) {
2332 int o_res[3];
2333 copy_v3_v3_int(o_res, fds->res);
2334 /* The new fluid object sizes its guiding grid from the domain resolution. */
2335 copy_v3_v3_int(fds->res, res);
2336
2338 fds, o_res, res, fds->res_min, min, fds->res_max, temp_shift, total_shift);
2339
2340 /* set new domain dimensions */
2341 copy_v3_v3_int(fds->res_min, min);
2342 copy_v3_v3_int(fds->res_max, max);
2343 fds->total_cells = total_cells;
2344
2345 /* Redo adapt time step in manta to refresh solver state (ie time variables) */
2346 manta_adapt_timestep(fds->fluid);
2347 }
2348}
2349
2350BLI_INLINE void apply_outflow_fields(int index,
2351 float distance_value,
2352 float *density,
2353 float *heat,
2354 float *fuel,
2355 float *react,
2356 float *color_r,
2357 float *color_g,
2358 float *color_b,
2359 float *phiout)
2360{
2361 /* Set levelset value for liquid inflow.
2362 * Ensure that distance value is "joined" into the levelset. */
2363 if (phiout) {
2364 phiout[index] = std::min(distance_value, phiout[index]);
2365 }
2366
2367 /* Set smoke outflow, i.e. reset cell to zero. */
2368 if (density) {
2369 density[index] = 0.0f;
2370 }
2371 if (heat) {
2372 heat[index] = 0.0f;
2373 }
2374 if (fuel) {
2375 fuel[index] = 0.0f;
2376 react[index] = 0.0f;
2377 }
2378 if (color_r) {
2379 color_r[index] = 0.0f;
2380 color_g[index] = 0.0f;
2381 color_b[index] = 0.0f;
2382 }
2383}
2384
2385BLI_INLINE void apply_inflow_fields(FluidFlowSettings *ffs,
2386 float emission_value,
2387 float distance_value,
2388 int index,
2389 float *density_in,
2390 const float *density,
2391 float *heat_in,
2392 const float *heat,
2393 float *fuel_in,
2394 const float *fuel,
2395 float *react_in,
2396 const float *react,
2397 float *color_r_in,
2398 const float *color_r,
2399 float *color_g_in,
2400 const float *color_g,
2401 float *color_b_in,
2402 const float *color_b,
2403 float *phi_in,
2404 float *emission_in)
2405{
2406 /* Set levelset value for liquid inflow.
2407 * Ensure that distance value is "joined" into the levelset. */
2408 if (phi_in) {
2409 phi_in[index] = std::min(distance_value, phi_in[index]);
2410 }
2411
2412 /* Set emission value for smoke inflow.
2413 * Ensure that emission value is "maximized". */
2414 if (emission_in) {
2415 emission_in[index] = std::max(emission_value, emission_in[index]);
2416 }
2417
2418 /* Set inflow for smoke from here on. */
2419 int absolute_flow = (ffs->flags & FLUID_FLOW_ABSOLUTE);
2420 float dens_old = (density) ? density[index] : 0.0;
2421 // float fuel_old = (fuel) ? fuel[index] : 0.0f; /* UNUSED */
2422 float dens_flow = (ffs->type == FLUID_FLOW_TYPE_FIRE) ? 0.0f : emission_value * ffs->density;
2423 float fuel_flow = (fuel) ? emission_value * ffs->fuel_amount : 0.0f;
2424 /* Set heat inflow. */
2425 if (heat && heat_in) {
2426 if (emission_value > 0.0f) {
2427 heat_in[index] = ADD_IF_LOWER(heat[index], ffs->temperature);
2428 }
2429 }
2430
2431 /* Set density and fuel - absolute mode. */
2432 if (absolute_flow) {
2433 if (density && density_in) {
2434 if (ffs->type != FLUID_FLOW_TYPE_FIRE && dens_flow > density[index]) {
2435 /* Use std::max to preserve values from other emitters at this cell. */
2436 density_in[index] = std::max(dens_flow, density_in[index]);
2437 }
2438 }
2439 if (fuel && fuel_in) {
2440 if (ffs->type != FLUID_FLOW_TYPE_SMOKE && fuel_flow && fuel_flow > fuel[index]) {
2441 /* Use std::max to preserve values from other emitters at this cell. */
2442 fuel_in[index] = std::max(fuel_flow, fuel_in[index]);
2443 }
2444 }
2445 }
2446 /* Set density and fuel - additive mode. */
2447 else {
2448 if (density && density_in) {
2449 if (ffs->type != FLUID_FLOW_TYPE_FIRE) {
2450 density_in[index] += dens_flow;
2451 CLAMP(density_in[index], 0.0f, 1.0f);
2452 }
2453 }
2454 if (fuel && fuel_in) {
2455 if (ffs->type != FLUID_FLOW_TYPE_SMOKE && ffs->fuel_amount) {
2456 fuel_in[index] += fuel_flow;
2457 CLAMP(fuel_in[index], 0.0f, 10.0f);
2458 }
2459 }
2460 }
2461
2462 /* Set color. */
2463 if (color_r && color_r_in) {
2464 if (dens_flow) {
2465 float total_dens = density[index] / (dens_old + dens_flow);
2466 color_r_in[index] = (color_r[index] + ffs->color[0] * dens_flow) * total_dens;
2467 color_g_in[index] = (color_g[index] + ffs->color[1] * dens_flow) * total_dens;
2468 color_b_in[index] = (color_b[index] + ffs->color[2] * dens_flow) * total_dens;
2469 }
2470 }
2471
2472 /* Set fire reaction coordinate. */
2473 if (fuel && fuel_in) {
2474 /* Instead of using 1.0 for all new fuel add slight falloff to reduce flow blocky-ness. */
2475 float value = 1.0f - pow2f(1.0f - emission_value);
2476
2477 if (fuel_in[index] > FLT_EPSILON && value > react[index]) {
2478 float f = fuel_flow / fuel_in[index];
2479 react_in[index] = value * f + (1.0f - f) * react[index];
2480 CLAMP(react_in[index], 0.0f, value);
2481 }
2482 }
2483}
2484
2485static void ensure_flowsfields(FluidDomainSettings *fds)
2486{
2487 if (fds->active_fields & FLUID_DOMAIN_ACTIVE_INVEL) {
2488 manta_ensure_invelocity(fds->fluid, fds->fmd);
2489 }
2490 if (fds->active_fields & FLUID_DOMAIN_ACTIVE_OUTFLOW) {
2491 manta_ensure_outflow(fds->fluid, fds->fmd);
2492 }
2493 if (fds->active_fields & FLUID_DOMAIN_ACTIVE_HEAT) {
2494 manta_smoke_ensure_heat(fds->fluid, fds->fmd);
2495 }
2496 if (fds->active_fields & FLUID_DOMAIN_ACTIVE_FIRE) {
2497 manta_smoke_ensure_fire(fds->fluid, fds->fmd);
2498 }
2499 if (fds->active_fields & FLUID_DOMAIN_ACTIVE_COLORS) {
2500 /* Initialize all smoke with "active_color". */
2501 manta_smoke_ensure_colors(fds->fluid, fds->fmd);
2502 }
2503 if (fds->type == FLUID_DOMAIN_TYPE_LIQUID && (fds->particle_type & FLUID_DOMAIN_PARTICLE_SPRAY ||
2504 fds->particle_type & FLUID_DOMAIN_PARTICLE_FOAM ||
2505 fds->particle_type & FLUID_DOMAIN_PARTICLE_TRACER))
2506 {
2507 manta_liquid_ensure_sndparts(fds->fluid, fds->fmd);
2508 }
2509 manta_update_pointers(fds->fluid, fds->fmd, false);
2510}
2511
2512static void update_flowsflags(FluidDomainSettings *fds, Object **flowobjs, int numflowobj)
2513{
2514 eFluidDomain_ActiveFields active_fields = fds->active_fields;
2515 uint flow_index;
2516
2517 /* First, remove all flags that we want to update. */
2522 active_fields &= ~prev_flags;
2523
2524 /* Monitor active fields based on flow settings. */
2525 for (flow_index = 0; flow_index < numflowobj; flow_index++) {
2526 Object *flow_ob = flowobjs[flow_index];
2529
2530 /* Sanity check. */
2531 if (!fmd2) {
2532 continue;
2533 }
2534
2535 /* Activate specific grids if at least one flow object requires this grid. */
2536 if ((fmd2->type & MOD_FLUID_TYPE_FLOW) && fmd2->flow) {
2537 FluidFlowSettings *ffs = fmd2->flow;
2538 if (!ffs) {
2539 break;
2540 }
2541 if (ffs->flags & FLUID_FLOW_NEEDS_UPDATE) {
2542 ffs->flags &= ~FLUID_FLOW_NEEDS_UPDATE;
2543 fds->cache_flag |= FLUID_DOMAIN_OUTDATED_DATA;
2544 }
2545 if (ffs->flags & FLUID_FLOW_INITVELOCITY) {
2546 active_fields |= FLUID_DOMAIN_ACTIVE_INVEL;
2547 }
2548 if (ffs->behavior == FLUID_FLOW_BEHAVIOR_OUTFLOW) {
2549 active_fields |= FLUID_DOMAIN_ACTIVE_OUTFLOW;
2550 }
2551 /* liquids done from here */
2552 if (fds->type == FLUID_DOMAIN_TYPE_LIQUID) {
2553 continue;
2554 }
2555
2556 /* Activate heat field if a flow object produces any heat. */
2557 if (ffs->temperature != 0.0) {
2558 active_fields |= FLUID_DOMAIN_ACTIVE_HEAT;
2559 }
2560 /* Activate fuel field if a flow object is of fire type. */
2561 if (ffs->fuel_amount != 0.0 || ffs->type == FLUID_FLOW_TYPE_FIRE ||
2562 ffs->type == FLUID_FLOW_TYPE_SMOKEFIRE)
2563 {
2564 active_fields |= FLUID_DOMAIN_ACTIVE_FIRE;
2565 }
2566 /* Activate color field if flows add smoke with varying colors. */
2567 if (ffs->density != 0.0 && ELEM(ffs->type, FLUID_FLOW_TYPE_SMOKE, FLUID_FLOW_TYPE_SMOKEFIRE))
2568 {
2569 if (!(active_fields & FLUID_DOMAIN_ACTIVE_COLOR_SET)) {
2570 copy_v3_v3(fds->active_color, ffs->color);
2571 active_fields |= FLUID_DOMAIN_ACTIVE_COLOR_SET;
2572 }
2573 else if (!equals_v3v3(fds->active_color, ffs->color)) {
2574 copy_v3_v3(fds->active_color, ffs->color);
2575 active_fields |= FLUID_DOMAIN_ACTIVE_COLORS;
2576 }
2577 }
2578 }
2579 }
2580 /* Monitor active fields based on domain settings. */
2581 if (fds->type == FLUID_DOMAIN_TYPE_GAS && active_fields & FLUID_DOMAIN_ACTIVE_FIRE) {
2582 /* Heat is always needed for fire. */
2583 active_fields |= FLUID_DOMAIN_ACTIVE_HEAT;
2584 /* Also activate colors if domain smoke color differs from active color. */
2585 if (!(active_fields & FLUID_DOMAIN_ACTIVE_COLOR_SET)) {
2586 copy_v3_v3(fds->active_color, fds->flame_smoke_color);
2587 active_fields |= FLUID_DOMAIN_ACTIVE_COLOR_SET;
2588 }
2589 else if (!equals_v3v3(fds->active_color, fds->flame_smoke_color)) {
2590 copy_v3_v3(fds->active_color, fds->flame_smoke_color);
2591 active_fields |= FLUID_DOMAIN_ACTIVE_COLORS;
2592 }
2593 }
2594 fds->active_fields = active_fields;
2595}
2596
2597static bool escape_flowsobject(Object *flowobj,
2599 FluidFlowSettings *ffs,
2600 int frame)
2601{
2602 bool use_velocity = (ffs->flags & FLUID_FLOW_INITVELOCITY);
2603 bool is_static = is_static_object(flowobj);
2604
2605 bool liquid_flow = ffs->type == FLUID_FLOW_TYPE_LIQUID;
2606 bool gas_flow = ELEM(
2608 bool is_geometry = (ffs->behavior == FLUID_FLOW_BEHAVIOR_GEOMETRY);
2609
2610 bool liquid_domain = fds->type == FLUID_DOMAIN_TYPE_LIQUID;
2611 bool gas_domain = fds->type == FLUID_DOMAIN_TYPE_GAS;
2612 bool is_adaptive = (fds->flags & FLUID_DOMAIN_USE_ADAPTIVE_DOMAIN);
2613 bool is_resume = (fds->cache_frame_pause_data == frame);
2614 bool is_first_frame = (fds->cache_frame_start == frame);
2615
2616 /* Cannot use static mode with adaptive domain.
2617 * The adaptive domain might expand and only later discover the static object. */
2618 if (is_adaptive) {
2619 is_static = false;
2620 }
2621 /* No need to compute emission value if it won't be applied. */
2622 if (liquid_flow && is_geometry && !is_first_frame) {
2623 return true;
2624 }
2625 /* Skip flow object if it does not "belong" to this domain type. */
2626 if ((liquid_flow && gas_domain) || (gas_flow && liquid_domain)) {
2627 return true;
2628 }
2629 /* Optimization: Static liquid flow objects don't need emission after first frame.
2630 * TODO(sebbas): Also do not use static mode if initial velocities are enabled. */
2631 if (liquid_flow && is_static && !is_first_frame && !is_resume && !use_velocity) {
2632 return true;
2633 }
2634 return false;
2635}
2636
2637static void compute_flowsemission(Scene *scene,
2638 FluidObjectBB *bb_maps,
2639 Depsgraph *depsgraph,
2640 float dt,
2641 Object **flowobjs,
2642 int frame,
2643 float frame_length,
2645 uint numflowobjs,
2646 float time_per_frame)
2647{
2648 bool is_first_frame = (frame == fds->cache_frame_start);
2649
2650 /* Prepare flow emission maps. */
2651 for (int flow_index = 0; flow_index < numflowobjs; flow_index++) {
2652 Object *flowobj = flowobjs[flow_index];
2655
2656 /* Sanity check. */
2657 if (!fmd2) {
2658 continue;
2659 }
2660
2661 /* Check for initialized flow object. */
2662 if ((fmd2->type & MOD_FLUID_TYPE_FLOW) && fmd2->flow) {
2663 FluidFlowSettings *ffs = fmd2->flow;
2664 int subframes = ffs->subframes;
2665 FluidObjectBB *bb = &bb_maps[flow_index];
2666
2667 /* Optimization: Skip this object under certain conditions. */
2668 if (escape_flowsobject(flowobj, fds, ffs, frame)) {
2669 continue;
2670 }
2671
2672 /* First frame cannot have any subframes because there is (obviously) no previous frame from
2673 * where subframes could come from. */
2674 if (is_first_frame) {
2675 subframes = 0;
2676 }
2677
2678 /* More splitting because of emission subframe: If no subframes present, sample_size is 1. */
2679 float sample_size = 1.0f / float(subframes + 1);
2680 float subframe_dt = dt * sample_size;
2681
2682 /* Emission loop. When not using subframes this will loop only once. */
2683 for (int subframe = 0; subframe <= subframes; subframe++) {
2684 /* Temporary emission map used when subframes are enabled, i.e. at least one subframe. */
2685 FluidObjectBB bb_temp = {nullptr};
2686
2687 /* Set scene time */
2688 if ((subframe < subframes || time_per_frame + dt + FLT_EPSILON < frame_length) &&
2689 !is_first_frame)
2690 {
2691 scene->r.subframe = (time_per_frame + (subframe + 1.0f) * subframe_dt) / frame_length;
2692 scene->r.cfra = frame - 1;
2693 }
2694 else {
2695 scene->r.subframe = 0.0f;
2696 scene->r.cfra = frame;
2697 }
2698
2699 /* Sanity check: subframe portion must be between 0 and 1. */
2700 CLAMP(scene->r.subframe, 0.0f, 1.0f);
2701# ifdef DEBUG_PRINT
2702 /* Debugging: Print subframe information. */
2703 printf(
2704 "flow: frame (is first: %d): %d // scene current frame: %d // scene current subframe: "
2705 "%f\n",
2706 is_first_frame,
2707 frame,
2708 scene->r.cfra,
2709 scene->r.subframe);
2710# endif
2711 /* Update frame time, this is considering current subframe fraction
2712 * BLI_mutex_lock() called in manta_step(), so safe to update subframe here
2713 * TODO(sebbas): Using BKE_scene_ctime_get(scene) instead of new DEG_get_ctime(depsgraph)
2714 * as subframes don't work with the latter yet. */
2716 scene,
2717 flowobj,
2718 true,
2720 BKE_scene_ctime_get(scene),
2722
2723 /* Emission from particles. */
2724 if (ffs->source == FLUID_FLOW_SOURCE_PARTICLES) {
2725 if (subframes) {
2726 emit_from_particles(flowobj, fds, ffs, &bb_temp, depsgraph, scene, subframe_dt);
2727 }
2728 else {
2729 emit_from_particles(flowobj, fds, ffs, bb, depsgraph, scene, subframe_dt);
2730 }
2731 }
2732 /* Emission from mesh. */
2733 else if (ffs->source == FLUID_FLOW_SOURCE_MESH) {
2734 if (subframes) {
2735 emit_from_mesh(flowobj, fds, ffs, &bb_temp, subframe_dt);
2736 }
2737 else {
2738 emit_from_mesh(flowobj, fds, ffs, bb, subframe_dt);
2739 }
2740 }
2741 else {
2742 printf("Error: unknown flow emission source\n");
2743 }
2744
2745 /* If this we emitted with temp emission map in this loop (subframe emission), we combine
2746 * the temp map with the original emission map. */
2747 if (subframes) {
2748 /* Combine emission maps. */
2749 bb_combineMaps(bb, &bb_temp, !(ffs->flags & FLUID_FLOW_ABSOLUTE), sample_size);
2750 bb_freeData(&bb_temp);
2751 }
2752 }
2753 }
2754 }
2755# ifdef DEBUG_PRINT
2756 /* Debugging: Print time information. */
2757 printf("flow: frame: %d // time per frame: %f // frame length: %f // dt: %f\n",
2758 frame,
2759 time_per_frame,
2760 frame_length,
2761 dt);
2762# endif
2763}
2764
2765static void update_flowsfluids(Depsgraph *depsgraph,
2766 Scene *scene,
2767 Object *ob,
2769 float time_per_frame,
2770 float frame_length,
2771 int frame,
2772 float dt)
2773{
2774 FluidObjectBB *bb_maps = nullptr;
2775 Object **flowobjs = nullptr;
2776 uint numflowobjs = 0;
2777 bool is_resume = (fds->cache_frame_pause_data == frame);
2778 bool is_first_frame = (fds->cache_frame_start == frame);
2779
2781 depsgraph, ob, fds->fluid_group, &numflowobjs, eModifierType_Fluid);
2782
2783 /* Update all flow related flags and ensure that corresponding grids get initialized. */
2784 update_flowsflags(fds, flowobjs, numflowobjs);
2785 ensure_flowsfields(fds);
2786
2787 /* Allocate emission map for each flow object. */
2788 bb_maps = MEM_new_array_zeroed<FluidObjectBB>(numflowobjs, "fluid_flow_bb_maps");
2789
2790 /* Initialize emission map for each flow object. */
2791 compute_flowsemission(scene,
2792 bb_maps,
2793 depsgraph,
2794 dt,
2795 flowobjs,
2796 frame,
2797 frame_length,
2798 fds,
2799 numflowobjs,
2800 time_per_frame);
2801
2802 /* Adjust domain size if needed. Only do this once for every frame. */
2803 if (fds->type == FLUID_DOMAIN_TYPE_GAS && fds->flags & FLUID_DOMAIN_USE_ADAPTIVE_DOMAIN) {
2804 adaptive_domain_adjust(fds, ob, bb_maps, numflowobjs, dt);
2805 }
2806
2807 float *phi_in = manta_get_phi_in(fds->fluid);
2808 float *phistatic_in = manta_get_phistatic_in(fds->fluid);
2809 float *phiout_in = manta_get_phiout_in(fds->fluid);
2810 float *phioutstatic_in = manta_get_phioutstatic_in(fds->fluid);
2811
2812 float *density = manta_smoke_get_density(fds->fluid);
2813 float *color_r = manta_smoke_get_color_r(fds->fluid);
2814 float *color_g = manta_smoke_get_color_g(fds->fluid);
2815 float *color_b = manta_smoke_get_color_b(fds->fluid);
2816 float *fuel = manta_smoke_get_fuel(fds->fluid);
2817 float *heat = manta_smoke_get_heat(fds->fluid);
2818 float *react = manta_smoke_get_react(fds->fluid);
2819
2820 float *density_in = manta_smoke_get_density_in(fds->fluid);
2821 float *heat_in = manta_smoke_get_heat_in(fds->fluid);
2822 float *color_r_in = manta_smoke_get_color_r_in(fds->fluid);
2823 float *color_g_in = manta_smoke_get_color_g_in(fds->fluid);
2824 float *color_b_in = manta_smoke_get_color_b_in(fds->fluid);
2825 float *fuel_in = manta_smoke_get_fuel_in(fds->fluid);
2826 float *react_in = manta_smoke_get_react_in(fds->fluid);
2827 float *emission_in = manta_smoke_get_emission_in(fds->fluid);
2828
2829 float *velx_initial = manta_get_in_velocity_x(fds->fluid);
2830 float *vely_initial = manta_get_in_velocity_y(fds->fluid);
2831 float *velz_initial = manta_get_in_velocity_z(fds->fluid);
2832
2833 float *forcex = manta_get_force_x(fds->fluid);
2834 float *forcey = manta_get_force_y(fds->fluid);
2835 float *forcez = manta_get_force_z(fds->fluid);
2836
2837 BLI_assert(forcex && forcey && forcez);
2838
2839 /* Either all or no components have to exist. */
2840 BLI_assert((color_r && color_g && color_b) || (!color_r && !color_g && !color_b));
2841 BLI_assert((color_r_in && color_g_in && color_b_in) ||
2842 (!color_r_in && !color_g_in && !color_b_in));
2843 BLI_assert((velx_initial && vely_initial && velz_initial) ||
2844 (!velx_initial && !vely_initial && !velz_initial));
2845
2846 uint z;
2847 /* Grid reset before writing again. */
2848 for (z = 0; z < fds->res[0] * fds->res[1] * fds->res[2]; z++) {
2849 /* Only reset static phi on first frame, dynamic phi gets reset every time. */
2850 if (phistatic_in && is_first_frame) {
2851 phistatic_in[z] = PHI_MAX;
2852 }
2853 if (phi_in) {
2854 phi_in[z] = PHI_MAX;
2855 }
2856 /* Only reset static phi on first frame, dynamic phi gets reset every time. */
2857 if (phioutstatic_in && is_first_frame) {
2858 phioutstatic_in[z] = PHI_MAX;
2859 }
2860 if (phiout_in) {
2861 phiout_in[z] = PHI_MAX;
2862 }
2863 /* Sync smoke inflow grids with their counterparts (simulation grids). */
2864 if (density_in) {
2865 density_in[z] = density[z];
2866 }
2867 if (heat_in) {
2868 heat_in[z] = heat[z];
2869 }
2870 if (color_r_in && color_g_in && color_b_in) {
2871 color_r_in[z] = color_r[z];
2872 color_g_in[z] = color_b[z];
2873 color_b_in[z] = color_g[z];
2874 }
2875 if (fuel_in) {
2876 fuel_in[z] = fuel[z];
2877 react_in[z] = react[z];
2878 }
2879 if (emission_in) {
2880 emission_in[z] = 0.0f;
2881 }
2882 if (velx_initial && vely_initial && velz_initial) {
2883 velx_initial[z] = 0.0f;
2884 vely_initial[z] = 0.0f;
2885 velz_initial[z] = 0.0f;
2886 }
2887 /* Reset forces here as update_effectors() is skipped when no external forces are present. */
2888 forcex[z] = 0.0f;
2889 forcey[z] = 0.0f;
2890 forcez[z] = 0.0f;
2891 }
2892
2893 /* Apply emission data for every flow object. */
2894 for (int flow_index = 0; flow_index < numflowobjs; flow_index++) {
2895 Object *flowobj = flowobjs[flow_index];
2898
2899 /* Sanity check. */
2900 if (!fmd2) {
2901 continue;
2902 }
2903
2904 /* Check for initialized flow object. */
2905 if ((fmd2->type & MOD_FLUID_TYPE_FLOW) && fmd2->flow) {
2906 FluidFlowSettings *ffs = fmd2->flow;
2907
2908 bool is_inflow = (ffs->behavior == FLUID_FLOW_BEHAVIOR_INFLOW);
2909 bool is_geometry = (ffs->behavior == FLUID_FLOW_BEHAVIOR_GEOMETRY);
2910 bool is_outflow = (ffs->behavior == FLUID_FLOW_BEHAVIOR_OUTFLOW);
2911 bool is_static = is_static_object(flowobj) &&
2912 ((fds->flags & FLUID_DOMAIN_USE_ADAPTIVE_DOMAIN) == 0);
2913
2914 FluidObjectBB *bb = &bb_maps[flow_index];
2915 float *velocity_map = bb->velocity;
2916 float *emission_map = bb->influence;
2917 float *distance_map = bb->distances;
2918
2919 int gx, gy, gz, ex, ey, ez, dx, dy, dz;
2920 size_t e_index, d_index;
2921
2922 /* Loop through every emission map cell. */
2923 for (gx = bb->min[0]; gx < bb->max[0]; gx++) {
2924 for (gy = bb->min[1]; gy < bb->max[1]; gy++) {
2925 for (gz = bb->min[2]; gz < bb->max[2]; gz++) {
2926 /* Compute emission map index. */
2927 ex = gx - bb->min[0];
2928 ey = gy - bb->min[1];
2929 ez = gz - bb->min[2];
2930 e_index = manta_get_index(ex, bb->res[0], ey, bb->res[1], ez);
2931
2932 /* Get domain index. */
2933 dx = gx - fds->res_min[0];
2934 dy = gy - fds->res_min[1];
2935 dz = gz - fds->res_min[2];
2936 d_index = manta_get_index(dx, fds->res[0], dy, fds->res[1], dz);
2937 /* Make sure emission cell is inside the new domain boundary. */
2938 if (dx < 0 || dy < 0 || dz < 0 || dx >= fds->res[0] || dy >= fds->res[1] ||
2939 dz >= fds->res[2])
2940 {
2941 continue;
2942 }
2943
2944 /* Delete fluid in outflow regions. */
2945 if (is_outflow) {
2946 float *levelset = ((is_first_frame || is_resume) && is_static) ? phioutstatic_in :
2947 phiout_in;
2948 apply_outflow_fields(d_index,
2949 distance_map[e_index],
2950 density_in,
2951 heat_in,
2952 fuel_in,
2953 react_in,
2954 color_r_in,
2955 color_g_in,
2956 color_b_in,
2957 levelset);
2958 }
2959 /* Do not apply inflow after the first frame when in geometry mode. */
2960 else if (is_geometry && !is_first_frame) {
2961 apply_inflow_fields(ffs,
2962 0.0f,
2963 PHI_MAX,
2964 d_index,
2965 density_in,
2966 density,
2967 heat_in,
2968 heat,
2969 fuel_in,
2970 fuel,
2971 react_in,
2972 react,
2973 color_r_in,
2974 color_r,
2975 color_g_in,
2976 color_g,
2977 color_b_in,
2978 color_b,
2979 phi_in,
2980 emission_in);
2981 }
2982 /* Main inflow application. */
2983 else if (is_geometry || is_inflow) {
2984 float *levelset = ((is_first_frame || is_resume) && is_static && !is_geometry) ?
2985 phistatic_in :
2986 phi_in;
2987 apply_inflow_fields(ffs,
2988 emission_map[e_index],
2989 distance_map[e_index],
2990 d_index,
2991 density_in,
2992 density,
2993 heat_in,
2994 heat,
2995 fuel_in,
2996 fuel,
2997 react_in,
2998 react,
2999 color_r_in,
3000 color_r,
3001 color_g_in,
3002 color_g,
3003 color_b_in,
3004 color_b,
3005 levelset,
3006 emission_in);
3007 if (ffs->flags & FLUID_FLOW_INITVELOCITY) {
3008 /* Use the initial velocity from the inflow object with the highest velocity for
3009 * now. */
3010 float vel_initial[3];
3011 vel_initial[0] = velx_initial[d_index];
3012 vel_initial[1] = vely_initial[d_index];
3013 vel_initial[2] = velz_initial[d_index];
3014 float vel_initial_strength = len_squared_v3(vel_initial);
3015 float vel_map_strength = len_squared_v3(velocity_map + 3 * e_index);
3016 if (vel_map_strength > vel_initial_strength) {
3017 velx_initial[d_index] = velocity_map[e_index * 3];
3018 vely_initial[d_index] = velocity_map[e_index * 3 + 1];
3019 velz_initial[d_index] = velocity_map[e_index * 3 + 2];
3020 }
3021 }
3022 }
3023 }
3024 }
3025 } /* End of flow emission map loop. */
3026 bb_freeData(bb);
3027 } /* End of flow object loop. */
3028 }
3029
3031 if (bb_maps) {
3032 MEM_delete(bb_maps);
3033 }
3034}
3035
3036struct UpdateEffectorsData {
3037 Scene *scene;
3038 FluidDomainSettings *fds;
3039 ListBaseT<EffectorCache> *effectors;
3040
3041 float *density;
3042 float *fuel;
3043 float *force_x;
3044 float *force_y;
3045 float *force_z;
3046 float *velocity_x;
3047 float *velocity_y;
3048 float *velocity_z;
3049 int *flags;
3050 float *phi_obs_in;
3051};
3052
3053static void update_effectors_task_cb(void *__restrict userdata,
3054 const int x,
3055 const TaskParallelTLS *__restrict /*tls*/)
3056{
3057 UpdateEffectorsData *data = static_cast<UpdateEffectorsData *>(userdata);
3058 FluidDomainSettings *fds = data->fds;
3059
3060 for (int y = 0; y < fds->res[1]; y++) {
3061 for (int z = 0; z < fds->res[2]; z++) {
3062 EffectedPoint epoint;
3063 float mag;
3064 float voxel_center[3] = {0, 0, 0}, vel[3] = {0, 0, 0}, retvel[3] = {0, 0, 0};
3065 const uint index = manta_get_index(x, fds->res[0], y, fds->res[1], z);
3066
3067 if ((data->fuel && std::max(data->density[index], data->fuel[index]) < FLT_EPSILON) ||
3068 (!data->fuel && data->density && data->density[index] < FLT_EPSILON) ||
3069 (data->phi_obs_in && data->phi_obs_in[index] < 0.0f) ||
3070 /* Manta-flow convention: `2 == FlagObstacle`. */
3071 data->flags[index] & 2)
3072 {
3073 continue;
3074 }
3075
3076 /* Get velocities from manta grid space and convert to blender units. */
3077 vel[0] = data->velocity_x[index];
3078 vel[1] = data->velocity_y[index];
3079 vel[2] = data->velocity_z[index];
3080 mul_v3_fl(vel, fds->dx);
3081
3082 /* Convert vel to global space. */
3083 mag = len_v3(vel);
3084 mul_mat3_m4_v3(fds->obmat, vel);
3085 normalize_v3(vel);
3086 mul_v3_fl(vel, mag);
3087
3088 voxel_center[0] = fds->p0[0] + fds->cell_size[0] * (float(x + fds->res_min[0]) + 0.5f);
3089 voxel_center[1] = fds->p0[1] + fds->cell_size[1] * (float(y + fds->res_min[1]) + 0.5f);
3090 voxel_center[2] = fds->p0[2] + fds->cell_size[2] * (float(z + fds->res_min[2]) + 0.5f);
3091 mul_m4_v3(fds->obmat, voxel_center);
3092
3093 /* Do effectors. */
3094 pd_point_from_loc(data->scene, voxel_center, vel, index, &epoint);
3096 data->effectors, nullptr, fds->effector_weights, &epoint, retvel, nullptr, nullptr);
3097
3098 /* Convert retvel to local space. */
3099 mag = len_v3(retvel);
3100 mul_mat3_m4_v3(fds->imat, retvel);
3101 normalize_v3(retvel);
3102 mul_v3_fl(retvel, mag);
3103
3104 /* Copy computed force to fluid solver forces. */
3105 mul_v3_fl(retvel, 0.2f); /* Factor from 0e6820cc5d62. */
3106 clamp_v3(retvel, -1.0f, 1.0f); /* Restrict forces to +-1 interval. */
3107 data->force_x[index] = retvel[0];
3108 data->force_y[index] = retvel[1];
3109 data->force_z[index] = retvel[2];
3110
3111# ifdef DEBUG_PRINT
3112 /* Debugging: Print forces. */
3113 printf("setting force: [%f, %f, %f]\n",
3114 data->force_x[index],
3115 data->force_y[index],
3116 data->force_z[index]);
3117# endif
3118 }
3119 }
3120}
3121
3122static void update_effectors(
3123 Depsgraph *depsgraph, Scene *scene, Object *ob, FluidDomainSettings *fds, float /*dt*/)
3124{
3125 ListBaseT<EffectorCache> *effectors;
3126 /* make sure smoke flow influence is 0.0f */
3127 fds->effector_weights->weight[PFIELD_FLUIDFLOW] = 0.0f;
3128 effectors = BKE_effectors_create(depsgraph, ob, nullptr, fds->effector_weights, false);
3129
3130 if (effectors) {
3131 /* Precalculate wind forces. */
3132 UpdateEffectorsData data;
3133 data.scene = scene;
3134 data.fds = fds;
3135 data.effectors = effectors;
3136 data.density = manta_smoke_get_density(fds->fluid);
3137 data.fuel = manta_smoke_get_fuel(fds->fluid);
3138 data.force_x = manta_get_force_x(fds->fluid);
3139 data.force_y = manta_get_force_y(fds->fluid);
3140 data.force_z = manta_get_force_z(fds->fluid);
3141 data.velocity_x = manta_get_velocity_x(fds->fluid);
3142 data.velocity_y = manta_get_velocity_y(fds->fluid);
3143 data.velocity_z = manta_get_velocity_z(fds->fluid);
3144 data.flags = manta_smoke_get_flags(fds->fluid);
3145 data.phi_obs_in = manta_get_phiobs_in(fds->fluid);
3146
3147 TaskParallelSettings settings;
3149 settings.min_iter_per_thread = 2;
3150 BLI_task_parallel_range(0, fds->res[0], &data, update_effectors_task_cb, &settings);
3151 }
3152
3153 BKE_effectors_free(effectors);
3154}
3155
3156static Mesh *create_liquid_geometry(FluidDomainSettings *fds,
3157 Scene *scene,
3158 Mesh *orgmesh,
3159 Object *ob)
3160{
3161 using namespace blender::bke;
3162 Mesh *mesh;
3163 float min[3];
3164 float max[3];
3165 float size[3];
3166 float cell_size_scaled[3];
3167
3168 const AttributeAccessor orig_attributes = orgmesh->attributes();
3169 const VArraySpan orig_material_indices = *orig_attributes.lookup<int>("material_index",
3170 AttrDomain::Face);
3171 const short mp_mat_nr = orig_material_indices.is_empty() ? 0 : orig_material_indices[0];
3172
3173 int i;
3174 int num_verts, num_faces;
3175
3176 if (!fds->fluid) {
3177 return nullptr;
3178 }
3179
3180 num_verts = manta_liquid_get_num_verts(fds->fluid);
3181 num_faces = manta_liquid_get_num_triangles(fds->fluid);
3182
3183# ifdef DEBUG_PRINT
3184 /* Debugging: Print number of vertices, normals, and faces. */
3185 printf("num_verts: %d, num_faces: %d\n", num_verts, num_faces);
3186# endif
3187
3188 if (!num_verts || !num_faces) {
3189 return nullptr;
3190 }
3191
3192 mesh = BKE_mesh_new_nomain(num_verts, 0, num_faces, num_faces * 3);
3193 if (!mesh) {
3194 return nullptr;
3195 }
3196 MutableSpan<float3> positions = mesh->vert_positions_for_write();
3197 MutableSpan<int> face_offsets = mesh->face_offsets_for_write();
3198 MutableSpan<int> corner_verts = mesh->corner_verts_for_write();
3199
3200 if (orgmesh->attributes().domain_size(AttrDomain::Face) > 0) {
3201 const bool is_sharp = orgmesh->attributes()
3202 .lookup_or_default<bool>("sharp_face", AttrDomain::Face, false)
3203 .varray[0];
3204 mesh_smooth_set(*mesh, !is_sharp);
3205 }
3206
3207 /* Get size (dimension) but considering scaling. */
3208 copy_v3_v3(cell_size_scaled, fds->cell_size);
3209 mul_v3_v3(cell_size_scaled, ob->scale);
3210 madd_v3fl_v3fl_v3fl_v3i(min, fds->p0, cell_size_scaled, fds->res_min);
3211 madd_v3fl_v3fl_v3fl_v3i(max, fds->p0, cell_size_scaled, fds->res_max);
3212 sub_v3_v3v3(size, max, min);
3213
3214 /* Biggest dimension will be used for up-scaling. */
3215 float max_size = std::max({size[0], size[1], size[2]});
3216
3217 float co_scale[3];
3218 co_scale[0] = max_size / ob->scale[0];
3219 co_scale[1] = max_size / ob->scale[1];
3220 co_scale[2] = max_size / ob->scale[2];
3221
3222 float co_offset[3];
3223 co_offset[0] = (fds->p0[0] + fds->p1[0]) / 2.0f;
3224 co_offset[1] = (fds->p0[1] + fds->p1[1]) / 2.0f;
3225 co_offset[2] = (fds->p0[2] + fds->p1[2]) / 2.0f;
3226
3227 /* Velocities. */
3228 /* If needed, vertex velocities will be read too. */
3229 bool use_speedvectors = fds->flags & FLUID_DOMAIN_USE_SPEED_VECTORS;
3230 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
3231 SpanAttributeWriter<float3> velocities;
3232 float time_mult = fds->dx / (DT_DEFAULT * (25.0f / scene->frames_per_second()));
3233
3234 if (use_speedvectors) {
3235 velocities = attributes.lookup_or_add_for_write_only_span<float3>("velocity",
3236 AttrDomain::Point);
3237 }
3238
3239 /* Loop for vertices and normals. */
3240 for (i = 0; i < num_verts; i++) {
3241
3242 /* Vertices (data is normalized cube around domain origin). */
3243 positions[i][0] = manta_liquid_get_vertex_x_at(fds->fluid, i);
3244 positions[i][1] = manta_liquid_get_vertex_y_at(fds->fluid, i);
3245 positions[i][2] = manta_liquid_get_vertex_z_at(fds->fluid, i);
3246
3247 /* Adjust coordinates from Mantaflow to match viewport scaling. */
3248 float tmp[3] = {float(fds->res[0]), float(fds->res[1]), float(fds->res[2])};
3249 /* Scale to unit cube around 0. */
3250 mul_v3_fl(tmp, fds->mesh_scale * 0.5f);
3251 sub_v3_v3(positions[i], tmp);
3252 /* Apply scaling of domain object. */
3253 mul_v3_fl(positions[i], fds->dx / fds->mesh_scale);
3254
3255 mul_v3_v3(positions[i], co_scale);
3256 add_v3_v3(positions[i], co_offset);
3257
3258# ifdef DEBUG_PRINT
3259 /* Debugging: Print coordinates of vertices. */
3260 printf("positions[i][0]: %f, positions[i][1]: %f, positions[i][2]: %f\n",
3261 positions[i][0],
3262 positions[i][1],
3263 positions[i][2]);
3264# endif
3265
3266# ifdef DEBUG_PRINT
3267 /* Debugging: Print coordinates of normals. */
3268 printf("no_s[0]: %d, no_s[1]: %d, no_s[2]: %d\n", no_s[0], no_s[1], no_s[2]);
3269# endif
3270
3271 if (use_speedvectors) {
3272 velocities.span[i].x = manta_liquid_get_vertvel_x_at(fds->fluid, i) * time_mult;
3273 velocities.span[i].y = manta_liquid_get_vertvel_y_at(fds->fluid, i) * time_mult;
3274 velocities.span[i].z = manta_liquid_get_vertvel_z_at(fds->fluid, i) * time_mult;
3275# ifdef DEBUG_PRINT
3276 /* Debugging: Print velocities of vertices. */
3277 printf("velocities[%d].x: %f, velocities[%d].y: %f, velocities[%d].z: %f\n",
3278 i,
3279 velocities.span[i].x,
3280 i,
3281 velocities.span[i].y,
3282 i,
3283 velocities.span[i].z);
3284# endif
3285 }
3286 }
3287
3288 bke::SpanAttributeWriter material_indices = attributes.lookup_or_add_for_write_span<int>(
3289 "material_index", AttrDomain::Face);
3290
3291 /* Loop for triangles. */
3292 for (const int i : face_offsets.index_range().drop_back(1)) {
3293 /* Initialize from existing face. */
3294 material_indices.span[i] = mp_mat_nr;
3295
3296 face_offsets[i] = i * 3;
3297
3298 corner_verts[i * 3 + 0] = manta_liquid_get_triangle_x_at(fds->fluid, i);
3299 corner_verts[i * 3 + 1] = manta_liquid_get_triangle_y_at(fds->fluid, i);
3300 corner_verts[i * 3 + 2] = manta_liquid_get_triangle_z_at(fds->fluid, i);
3301# ifdef DEBUG_PRINT
3302 /* Debugging: Print mesh faces. */
3303 printf("mloops[0].v: %d, mloops[1].v: %d, mloops[2].v: %d\n",
3304 mloops[0].v,
3305 mloops[1].v,
3306 mloops[2].v);
3307# endif
3308 }
3309
3310 velocities.finish();
3311 material_indices.finish();
3312
3313 mesh_calc_edges(*mesh, false, false);
3314
3315 return mesh;
3316}
3317
3318static Mesh *create_smoke_geometry(FluidDomainSettings *fds, Mesh *orgmesh, Object *ob)
3319{
3320 using namespace blender::bke;
3321 Mesh *result;
3322 float min[3];
3323 float max[3];
3324 float *co;
3325 int *corner_vert;
3326
3327 int num_verts = 8;
3328 int num_faces = 6;
3329 float ob_loc[3] = {0};
3330 float ob_cache_loc[3] = {0};
3331
3332 /* Just copy existing mesh if there is no content or if the adaptive domain is not being used. */
3333 if (fds->total_cells <= 1 || (fds->flags & FLUID_DOMAIN_USE_ADAPTIVE_DOMAIN) == 0) {
3334 return BKE_mesh_copy_for_eval(*orgmesh);
3335 }
3336
3337 result = BKE_mesh_new_nomain(num_verts, 0, num_faces, num_faces * 4);
3338 MutableSpan<float3> positions = result->vert_positions_for_write();
3339 MutableSpan<int> face_offsets = result->face_offsets_for_write();
3340 MutableSpan<int> corner_verts = result->corner_verts_for_write();
3341
3342 if (num_verts) {
3343 /* Volume bounds. */
3344 madd_v3fl_v3fl_v3fl_v3i(min, fds->p0, fds->cell_size, fds->res_min);
3345 madd_v3fl_v3fl_v3fl_v3i(max, fds->p0, fds->cell_size, fds->res_max);
3346
3347 /* Set vertices of smoke BB. Especially important, when BB changes (adaptive domain). */
3348 /* Top slab */
3349 co = positions[0];
3350 co[0] = min[0];
3351 co[1] = min[1];
3352 co[2] = max[2];
3353 co = positions[1];
3354 co[0] = max[0];
3355 co[1] = min[1];
3356 co[2] = max[2];
3357 co = positions[2];
3358 co[0] = max[0];
3359 co[1] = max[1];
3360 co[2] = max[2];
3361 co = positions[3];
3362 co[0] = min[0];
3363 co[1] = max[1];
3364 co[2] = max[2];
3365 /* Bottom slab. */
3366 co = positions[4];
3367 co[0] = min[0];
3368 co[1] = min[1];
3369 co[2] = min[2];
3370 co = positions[5];
3371 co[0] = max[0];
3372 co[1] = min[1];
3373 co[2] = min[2];
3374 co = positions[6];
3375 co[0] = max[0];
3376 co[1] = max[1];
3377 co[2] = min[2];
3378 co = positions[7];
3379 co[0] = min[0];
3380 co[1] = max[1];
3381 co[2] = min[2];
3382
3383 face_offsets.fill(4);
3385
3386 /* Create faces. */
3387 /* Top side. */
3388 corner_vert = &corner_verts[0 * 4];
3389 corner_vert[0] = 0;
3390 corner_vert[1] = 1;
3391 corner_vert[2] = 2;
3392 corner_vert[3] = 3;
3393 /* Right side. */
3394 corner_vert = &corner_verts[1 * 4];
3395 corner_vert[0] = 2;
3396 corner_vert[1] = 1;
3397 corner_vert[2] = 5;
3398 corner_vert[3] = 6;
3399 /* Bottom side. */
3400 corner_vert = &corner_verts[2 * 4];
3401 corner_vert[0] = 7;
3402 corner_vert[1] = 6;
3403 corner_vert[2] = 5;
3404 corner_vert[3] = 4;
3405 /* Left side. */
3406 corner_vert = &corner_verts[3 * 4];
3407 corner_vert[0] = 0;
3408 corner_vert[1] = 3;
3409 corner_vert[2] = 7;
3410 corner_vert[3] = 4;
3411 /* Front side. */
3412 corner_vert = &corner_verts[4 * 4];
3413 corner_vert[0] = 3;
3414 corner_vert[1] = 2;
3415 corner_vert[2] = 6;
3416 corner_vert[3] = 7;
3417 /* Back side. */
3418 corner_vert = &corner_verts[5 * 4];
3419 corner_vert[0] = 1;
3420 corner_vert[1] = 0;
3421 corner_vert[2] = 4;
3422 corner_vert[3] = 5;
3423
3424 /* Calculate required shift to match domain's global position
3425 * it was originally simulated at (if object moves without manta step). */
3426 invert_m4_m4(ob->runtime->world_to_object.ptr(), ob->object_to_world().ptr());
3427 mul_m4_v3(ob->object_to_world().ptr(), ob_loc);
3428 mul_m4_v3(fds->obmat, ob_cache_loc);
3429 sub_v3_v3v3(fds->obj_shift_f, ob_cache_loc, ob_loc);
3430 /* Convert shift to local space and apply to vertices. */
3431 mul_mat3_m4_v3(ob->world_to_object().ptr(), fds->obj_shift_f);
3432 /* Apply shift to vertices. */
3433 for (int i = 0; i < num_verts; i++) {
3434 add_v3_v3(positions[i], fds->obj_shift_f);
3435 }
3436 }
3437
3438 mesh_calc_edges(*result, false, false);
3439 return result;
3440}
3441
3442static int manta_step(
3443 Depsgraph *depsgraph, Scene *scene, Object *ob, Mesh *mesh, FluidModifierData *fmd, int frame)
3444{
3445 FluidDomainSettings *fds = fmd->domain;
3446 float dt, frame_length, time_total, time_total_old;
3447 float time_per_frame;
3448 bool init_resolution = true;
3449
3450 /* Store baking success - bake might be aborted anytime by user. */
3451 int result = 1;
3452 int mode = fds->cache_type;
3453 bool mode_replay = (mode == FLUID_DOMAIN_CACHE_REPLAY);
3454
3455 /* Update object state. */
3456 invert_m4_m4(fds->imat, ob->object_to_world().ptr());
3457 copy_m4_m4(fds->obmat, ob->object_to_world().ptr());
3458
3459 /* Gas domain might use adaptive domain. */
3460 if (fds->type == FLUID_DOMAIN_TYPE_GAS) {
3461 init_resolution = (fds->flags & FLUID_DOMAIN_USE_ADAPTIVE_DOMAIN) != 0;
3462 }
3463 manta_set_domain_from_mesh(fds, ob, mesh, init_resolution);
3464
3465 /* Use local variables for adaptive loop, dt can change. */
3466 frame_length = fds->frame_length;
3467 dt = fds->dt;
3468 time_per_frame = 0;
3469 time_total = fds->time_total;
3470 /* Keep track of original total time to correct small errors at end of step. */
3471 time_total_old = fds->time_total;
3472
3473 std::scoped_lock lock(object_update_lock);
3474
3475 /* Loop as long as time_per_frame (sum of sub dt's) does not exceed actual frame-length. */
3476 while (time_per_frame + FLT_EPSILON < frame_length) {
3478 dt = manta_get_timestep(fds->fluid);
3479
3480 /* Save adapted dt so that MANTA object can access it (important when adaptive domain creates
3481 * new MANTA object). */
3482 fds->dt = dt;
3483
3484 /* Calculate inflow geometry. */
3485 update_flowsfluids(depsgraph, scene, ob, fds, time_per_frame, frame_length, frame, dt);
3486
3487 /* If user requested stop, quit baking */
3488 if (G.is_break && !mode_replay) {
3489 result = 0;
3490 break;
3491 }
3492
3493 manta_update_variables(fds->fluid, fmd);
3494
3495 /* Calculate obstacle geometry. */
3496 update_obstacles(depsgraph, scene, ob, fds, time_per_frame, frame_length, frame, dt);
3497
3498 /* If user requested stop, quit baking */
3499 if (G.is_break && !mode_replay) {
3500 result = 0;
3501 break;
3502 }
3503
3504 /* Only bake if the domain is bigger than one cell (important for adaptive domain). */
3505 if (fds->total_cells > 1) {
3506 update_effectors(depsgraph, scene, ob, fds, dt);
3507 manta_bake_data(fds->fluid, fmd, frame);
3508 }
3509
3510 /* Count for how long this while loop is running. */
3511 time_per_frame += dt;
3512 time_total += dt;
3513
3514 fds->time_per_frame = time_per_frame;
3515 fds->time_total = time_total;
3516 }
3517
3518 /* Total time must not exceed frame-count times frame-length. Correct tiny errors here. */
3519 CLAMP_MAX(fds->time_total, time_total_old + fds->frame_length);
3520
3521 /* Compute shadow grid for gas simulations. Make sure to skip if bake job was canceled early. */
3522 if (fds->type == FLUID_DOMAIN_TYPE_GAS && result) {
3523 manta_smoke_calc_transparency(*DEG_get_bmain(depsgraph),
3524 fds,
3525 DEG_get_evaluated_scene(depsgraph),
3526 DEG_get_evaluated_view_layer(depsgraph));
3527 }
3528
3529 return result;
3530}
3531
3532static void manta_guiding(
3533 Depsgraph *depsgraph, Scene *scene, Object *ob, FluidModifierData *fmd, int frame)
3534{
3535 FluidDomainSettings *fds = fmd->domain;
3536 float dt = DT_DEFAULT * (25.0f / scene->frames_per_second()) * fds->time_scale;
3537
3538 std::scoped_lock lock(object_update_lock);
3539
3540 update_obstacles(depsgraph, scene, ob, fds, dt, dt, frame, dt);
3541 manta_bake_guiding(fds->fluid, fmd, frame);
3542}
3543
3544static void fluid_modifier_processFlow(FluidModifierData *fmd,
3545 Depsgraph *depsgraph,
3546 Scene *scene,
3547 Object *ob,
3548 Mesh *mesh,
3549 const int scene_framenr)
3550{
3551 if (scene_framenr >= fmd->time) {
3552 fluid_modifier_init(fmd, depsgraph, ob, scene, mesh);
3553 }
3554
3555 if (fmd->flow) {
3556 if (fmd->flow->mesh) {
3557 BKE_id_free(nullptr, fmd->flow->mesh);
3558 }
3560 }
3561
3562 if (scene_framenr > fmd->time) {
3563 fmd->time = scene_framenr;
3564 }
3565 else if (scene_framenr < fmd->time) {
3566 fmd->time = scene_framenr;
3567 fluid_modifier_reset_ex(fmd, false);
3568 }
3569}
3570
3571static void fluid_modifier_processEffector(FluidModifierData *fmd,
3572 Depsgraph *depsgraph,
3573 Scene *scene,
3574 Object *ob,
3575 Mesh *mesh,
3576 const int scene_framenr)
3577{
3578 if (scene_framenr >= fmd->time) {
3579 fluid_modifier_init(fmd, depsgraph, ob, scene, mesh);
3580 }
3581
3582 if (fmd->effector) {
3583 if (fmd->effector->mesh) {
3584 BKE_id_free(nullptr, fmd->effector->mesh);
3585 }
3587 }
3588
3589 if (scene_framenr > fmd->time) {
3590 fmd->time = scene_framenr;
3591 }
3592 else if (scene_framenr < fmd->time) {
3593 fmd->time = scene_framenr;
3594 fluid_modifier_reset_ex(fmd, false);
3595 }
3596}
3597
3598static void fluid_modifier_processDomain(FluidModifierData *fmd,
3599 Depsgraph *depsgraph,
3600 Scene *scene,
3601 Object *ob,
3602 Mesh *mesh,
3603 const int scene_framenr)
3604{
3605 FluidDomainSettings *fds = fmd->domain;
3606 Object *guide_parent = nullptr;
3607 Object **objs = nullptr;
3608 uint numobj = 0;
3609 FluidModifierData *fmd_parent = nullptr;
3610
3611 bool is_startframe, has_advanced;
3612 is_startframe = (scene_framenr == fds->cache_frame_start);
3613 has_advanced = (scene_framenr == fmd->time + 1);
3614 int mode = fds->cache_type;
3615
3616 /* Do not process modifier if current frame is out of cache range. */
3617 bool escape = false;
3618 switch (mode) {
3621 if (fds->cache_frame_offset > 0) {
3622 if (scene_framenr < fds->cache_frame_start ||
3623 scene_framenr > fds->cache_frame_end + fds->cache_frame_offset)
3624 {
3625 escape = true;
3626 }
3627 }
3628 else {
3629 if (scene_framenr < fds->cache_frame_start + fds->cache_frame_offset ||
3630 scene_framenr > fds->cache_frame_end)
3631 {
3632 escape = true;
3633 }
3634 }
3635 break;
3637 default:
3638 if (scene_framenr < fds->cache_frame_start || scene_framenr > fds->cache_frame_end) {
3639 escape = true;
3640 }
3641 break;
3642 }
3643 /* If modifier will not be processed, update/flush pointers from (old) fluid object once more. */
3644 if (escape && fds->fluid) {
3645 manta_update_pointers(fds->fluid, fmd, true);
3646 return;
3647 }
3648
3649 /* Reset fluid if no fluid present. Also resets active fields. */
3650 if (!fds->fluid) {
3651 fluid_modifier_reset_ex(fmd, false);
3652 }
3653
3654 /* Ensure cache directory is not relative. */
3655 const char *relbase = BKE_modifier_path_relbase_from_global(ob);
3656 BLI_path_abs(fds->cache_directory, relbase);
3657
3658 /* If 'outdated', reset the cache here. */
3659 if (is_startframe && mode == FLUID_DOMAIN_CACHE_REPLAY) {
3660 PTCacheID pid;
3661 BKE_ptcache_id_from_smoke(&pid, ob, fmd);
3662 if (pid.cache->flag & PTCACHE_OUTDATED) {
3664 BKE_fluid_cache_free_all(fds, ob);
3665 fluid_modifier_reset_ex(fmd, false);
3666 }
3667 }
3668
3669 /* Ensure that all flags are up to date before doing any baking and/or cache reading. */
3671 depsgraph, ob, fds->fluid_group, &numobj, eModifierType_Fluid);
3672 update_flowsflags(fds, objs, numobj);
3673 if (objs) {
3674 MEM_delete(objs);
3675 }
3677 depsgraph, ob, fds->effector_group, &numobj, eModifierType_Fluid);
3678 update_obstacleflags(fds, objs, numobj);
3679 if (objs) {
3680 MEM_delete(objs);
3681 }
3682
3683 /* Fluid domain init must not fail in order to continue modifier evaluation. */
3684 if (!fds->fluid && !fluid_modifier_init(fmd, depsgraph, ob, scene, mesh)) {
3685 CLOG_ERROR(&LOG, "Fluid initialization failed. Should not happen!");
3686 return;
3687 }
3688 BLI_assert(fds->fluid);
3689
3690 /* Guiding parent res pointer needs initialization. */
3691 guide_parent = fds->guide_parent;
3692 if (guide_parent) {
3694 if (fmd_parent && fmd_parent->domain) {
3695 copy_v3_v3_int(fds->guide_res, fmd_parent->domain->res);
3696 }
3697 }
3698
3699 /* Adaptive domain needs to know about current state, so save it here. */
3700 int o_res[3], o_min[3], o_max[3], o_shift[3];
3701 copy_v3_v3_int(o_res, fds->res);
3702 copy_v3_v3_int(o_min, fds->res_min);
3703 copy_v3_v3_int(o_max, fds->res_max);
3704 copy_v3_v3_int(o_shift, fds->shift);
3705
3706 /* Ensure that time parameters are initialized correctly before every step. */
3707 fds->frame_length = DT_DEFAULT * (25.0f / scene->frames_per_second()) * fds->time_scale;
3708 fds->dt = fds->frame_length;
3709 fds->time_per_frame = 0;
3710
3711 /* Ensure that gravity is copied over every frame (could be keyframed). */
3712 update_final_gravity(fds, scene);
3713
3714 int next_frame = scene_framenr + 1;
3715 int prev_frame = scene_framenr - 1;
3716 /* Ensure positive of previous frame. */
3717 CLAMP_MIN(prev_frame, fds->cache_frame_start);
3718
3719 int data_frame = scene_framenr, noise_frame = scene_framenr;
3720 int mesh_frame = scene_framenr, particles_frame = scene_framenr, guide_frame = scene_framenr;
3721
3722 bool with_smoke, with_liquid;
3723 with_smoke = fds->type == FLUID_DOMAIN_TYPE_GAS;
3724 with_liquid = fds->type == FLUID_DOMAIN_TYPE_LIQUID;
3725
3726 bool drops, bubble, floater;
3730
3731 bool with_resumable_cache = fds->flags & FLUID_DOMAIN_USE_RESUMABLE_CACHE;
3732 bool with_script, with_noise, with_mesh, with_particles, with_guide;
3733 with_script = fds->flags & FLUID_DOMAIN_EXPORT_MANTA_SCRIPT;
3734 with_noise = fds->flags & FLUID_DOMAIN_USE_NOISE;
3735 with_mesh = fds->flags & FLUID_DOMAIN_USE_MESH;
3736 with_guide = fds->flags & FLUID_DOMAIN_USE_GUIDE;
3737 with_particles = drops || bubble || floater;
3738
3739 bool has_data, has_noise, has_mesh, has_particles, has_guide, has_config;
3740 has_data = manta_has_data(fds->fluid, fmd, scene_framenr);
3741 has_noise = manta_has_noise(fds->fluid, fmd, scene_framenr);
3742 has_mesh = manta_has_mesh(fds->fluid, fmd, scene_framenr);
3743 has_particles = manta_has_particles(fds->fluid, fmd, scene_framenr);
3744 has_guide = manta_has_guiding(fds->fluid, fmd, scene_framenr, guide_parent);
3745 has_config = manta_read_config(fds->fluid, fmd, scene_framenr);
3746
3747 /* When reading data from cache (has_config == true) ensure that active fields are allocated.
3748 * update_flowsflags() and update_obstacleflags() will not find flow sources hidden from renders.
3749 * See also: #72192. */
3750 if (has_config) {
3751 ensure_flowsfields(fds);
3752 ensure_obstaclefields(fds);
3753 }
3754
3755 bool baking_data, baking_noise, baking_mesh, baking_particles, baking_guide;
3756 baking_data = fds->cache_flag & FLUID_DOMAIN_BAKING_DATA;
3757 baking_noise = fds->cache_flag & FLUID_DOMAIN_BAKING_NOISE;
3758 baking_mesh = fds->cache_flag & FLUID_DOMAIN_BAKING_MESH;
3759 baking_particles = fds->cache_flag & FLUID_DOMAIN_BAKING_PARTICLES;
3760 baking_guide = fds->cache_flag & FLUID_DOMAIN_BAKING_GUIDE;
3761
3762 bool resume_data, resume_noise, resume_mesh, resume_particles, resume_guide;
3763 resume_data = (!is_startframe) && (fds->cache_frame_pause_data == scene_framenr);
3764 resume_noise = (!is_startframe) && (fds->cache_frame_pause_noise == scene_framenr);
3765 resume_mesh = (!is_startframe) && (fds->cache_frame_pause_mesh == scene_framenr);
3766 resume_particles = (!is_startframe) && (fds->cache_frame_pause_particles == scene_framenr);
3767 resume_guide = (!is_startframe) && (fds->cache_frame_pause_guide == scene_framenr);
3768
3769 bool read_cache, bake_cache;
3770 read_cache = false;
3771 bake_cache = baking_data || baking_noise || baking_mesh || baking_particles || baking_guide;
3772
3773 bool next_data, next_noise, next_mesh, next_particles, next_guide;
3774 next_data = manta_has_data(fds->fluid, fmd, next_frame);
3775 next_noise = manta_has_noise(fds->fluid, fmd, next_frame);
3776 next_mesh = manta_has_mesh(fds->fluid, fmd, next_frame);
3777 next_particles = manta_has_particles(fds->fluid, fmd, next_frame);
3778 next_guide = manta_has_guiding(fds->fluid, fmd, next_frame, guide_parent);
3779
3780 bool prev_data, prev_noise, prev_mesh, prev_particles, prev_guide;
3781 prev_data = manta_has_data(fds->fluid, fmd, prev_frame);
3782 prev_noise = manta_has_noise(fds->fluid, fmd, prev_frame);
3783 prev_mesh = manta_has_mesh(fds->fluid, fmd, prev_frame);
3784 prev_particles = manta_has_particles(fds->fluid, fmd, prev_frame);
3785 prev_guide = manta_has_guiding(fds->fluid, fmd, prev_frame, guide_parent);
3786
3787 /* Unused for now. */
3788 UNUSED_VARS(next_mesh, next_guide);
3789
3790 bool with_gdomain;
3791 with_gdomain = (fds->guide_source == FLUID_DOMAIN_GUIDE_SRC_DOMAIN);
3792
3793 /* Cache mode specific settings. */
3794 switch (mode) {
3797 /* Just load the data that has already been baked */
3798 if (!baking_data && !baking_noise && !baking_mesh && !baking_particles && !baking_guide) {
3799 read_cache = true;
3800 bake_cache = false;
3801
3802 /* Apply frame offset. */
3803 data_frame -= fmd->domain->cache_frame_offset;
3804 noise_frame -= fmd->domain->cache_frame_offset;
3805 mesh_frame -= fmd->domain->cache_frame_offset;
3806 particles_frame -= fmd->domain->cache_frame_offset;
3807 break;
3808 }
3809
3810 /* Set to previous frame if the bake was resumed
3811 * ie don't read all of the already baked frames, just the one before bake resumes */
3812 if (baking_data && resume_data) {
3813 data_frame = prev_frame;
3814 }
3815 if (baking_noise && resume_noise) {
3816 noise_frame = prev_frame;
3817 }
3818 if (baking_mesh && resume_mesh) {
3819 mesh_frame = prev_frame;
3820 }
3821 if (baking_particles && resume_particles) {
3822 particles_frame = prev_frame;
3823 }
3824 if (baking_guide && resume_guide) {
3825 guide_frame = prev_frame;
3826 }
3827
3828 /* Noise, mesh and particles can never be baked more than data. */
3829 CLAMP_MAX(noise_frame, data_frame);
3830 CLAMP_MAX(mesh_frame, data_frame);
3831 CLAMP_MAX(particles_frame, data_frame);
3832 CLAMP_MAX(guide_frame, fds->cache_frame_end);
3833
3834 /* Force to read cache as we're resuming the bake */
3835 read_cache = true;
3836 break;
3838 default:
3839 baking_data = !has_data && (is_startframe || prev_data);
3840 if (with_smoke && with_noise) {
3841 baking_noise = !has_noise && (is_startframe || prev_noise);
3842 }
3843 if (with_liquid && with_mesh) {
3844 baking_mesh = !has_mesh && (is_startframe || prev_mesh);
3845 }
3846 if (with_liquid && with_particles) {
3847 baking_particles = !has_particles && (is_startframe || prev_particles);
3848 }
3849
3850 /* Always trying to read the cache in replay mode. */
3851 read_cache = true;
3852 bake_cache = false;
3853 break;
3854 }
3855
3856 bool read_partial = false, read_all = false;
3857 bool grid_display = fds->use_coba;
3858
3859 /* Try to read from cache and keep track of read success. */
3860 if (read_cache) {
3861 /* Read mesh cache. */
3862 if (with_liquid && with_mesh) {
3863 if (mesh_frame != scene_framenr) {
3864 has_config = manta_read_config(fds->fluid, fmd, mesh_frame);
3865 }
3866
3867 /* The liquid mesh cache is read before the regular data-cache allocation. Ensure the Manta
3868 * instance matches the cached configuration here instead of reallocating all cache types. */
3869 if (has_config && manta_needs_realloc(fds->fluid, fmd)) {
3870 BKE_fluid_reallocate_fluid(fds, fds->res, 1);
3871 }
3872
3873 /* Only load the mesh at the resolution it was originally simulated at.
3874 * The mesh files don't have a header, i.e. they don't store the grid resolution. */
3875 if (!manta_needs_realloc(fds->fluid, fmd)) {
3876 has_mesh = manta_read_mesh(fds->fluid, fmd, mesh_frame);
3877 }
3878 }
3879
3880 /* Read particles cache. */
3881 if (with_liquid && with_particles) {
3882 if (particles_frame != scene_framenr) {
3883 has_config = manta_read_config(fds->fluid, fmd, particles_frame);
3884 }
3885
3886 /* The liquid particle cache is also read before the regular data-cache allocation. */
3887 if (has_config && manta_needs_realloc(fds->fluid, fmd)) {
3888 BKE_fluid_reallocate_fluid(fds, fds->res, 1);
3889 }
3890
3891 read_partial = !baking_data && !baking_particles && next_particles;
3892 read_all = !read_partial && with_resumable_cache;
3893 has_particles = manta_read_particles(fds->fluid, fmd, particles_frame, read_all);
3894 }
3895
3896 /* Read guide cache. */
3897 if (with_guide) {
3898 FluidModifierData *fmd2 = (with_gdomain) ? fmd_parent : fmd;
3899 has_guide = manta_read_guiding(fds->fluid, fmd2, scene_framenr, with_gdomain);
3900 }
3901
3902 /* Read noise and data cache */
3903 if (with_smoke && with_noise) {
3904 if (noise_frame != scene_framenr) {
3905 has_config = manta_read_config(fds->fluid, fmd, noise_frame);
3906 }
3907
3908 /* Only reallocate when just reading cache or when resuming during bake. */
3909 if (has_data && has_config && manta_needs_realloc(fds->fluid, fmd)) {
3911 fds, o_res, fds->res, o_min, fds->res_min, o_max, o_shift, fds->shift);
3912 }
3913
3914 read_partial = !baking_data && !baking_noise && next_noise;
3915 read_all = !read_partial && with_resumable_cache;
3916 has_noise = manta_read_noise(fds->fluid, fmd, noise_frame, read_all);
3917
3918 read_partial = !baking_data && !baking_noise && next_data && next_noise;
3919 read_all = !read_partial && with_resumable_cache;
3920 has_data = manta_read_data(fds->fluid, fmd, data_frame, read_all);
3921 }
3922 /* Read data cache only */
3923 else {
3924 if (data_frame != scene_framenr) {
3925 has_config = manta_read_config(fds->fluid, fmd, data_frame);
3926 }
3927
3928 if (with_smoke || with_liquid) {
3929 /* Read config and realloc fluid object if needed. */
3930 if (has_config && manta_needs_realloc(fds->fluid, fmd)) {
3931 BKE_fluid_reallocate_fluid(fds, fds->res, 1);
3932 }
3933 }
3934
3935 read_partial = !baking_data && !baking_particles && !baking_mesh && next_data &&
3936 !grid_display;
3937 read_all = !read_partial && with_resumable_cache;
3938 has_data = manta_read_data(fds->fluid, fmd, data_frame, read_all);
3939 }
3940 }
3941
3942 /* Cache mode specific settings */
3943 switch (mode) {
3946 if (!baking_data && !baking_noise && !baking_mesh && !baking_particles && !baking_guide) {
3947 bake_cache = false;
3948 }
3949 break;
3951 default:
3952 if (with_guide) {
3953 baking_guide = !has_guide && (is_startframe || prev_guide);
3954 }
3955 baking_data = !has_data && (is_startframe || prev_data);
3956 if (with_smoke && with_noise) {
3957 baking_noise = !has_noise && (is_startframe || prev_noise);
3958 }
3959 if (with_liquid && with_mesh) {
3960 baking_mesh = !has_mesh && (is_startframe || prev_mesh);
3961 }
3962 if (with_liquid && with_particles) {
3963 baking_particles = !has_particles && (is_startframe || prev_particles);
3964 }
3965
3966 /* Only bake if time advanced by one frame. */
3967 if (is_startframe || has_advanced) {
3968 bake_cache = baking_data || baking_noise || baking_mesh || baking_particles;
3969 }
3970 break;
3971 }
3972
3973 /* Trigger bake calls individually */
3974 if (bake_cache) {
3975 /* Ensure fresh variables at every animation step */
3976 manta_update_variables(fds->fluid, fmd);
3977
3978 /* Export mantaflow python script on first frame (once only) and for any bake type */
3979 if (with_script && is_startframe) {
3980 if (with_smoke) {
3982 }
3983 if (with_liquid) {
3985 }
3986 }
3987
3988 if (baking_guide && with_guide) {
3989 manta_guiding(depsgraph, scene, ob, fmd, scene_framenr);
3990 }
3991 if (baking_data) {
3992 /* Only save baked data if all of it completed successfully. */
3993 if (manta_step(depsgraph, scene, ob, mesh, fmd, scene_framenr)) {
3994 manta_write_config(fds->fluid, fmd, scene_framenr);
3995 manta_write_data(fds->fluid, fmd, scene_framenr);
3996 }
3997 }
3998 if (has_data || baking_data) {
3999 if (baking_noise && with_smoke && with_noise) {
4000 /* Ensure that no bake occurs if domain was minimized by adaptive domain. */
4001 if (fds->total_cells > 1) {
4002 manta_bake_noise(fds->fluid, fmd, scene_framenr);
4003 }
4004 manta_write_noise(fds->fluid, fmd, scene_framenr);
4005 }
4006 if (baking_mesh && with_liquid && with_mesh) {
4007 manta_bake_mesh(fds->fluid, fmd, scene_framenr);
4008 }
4009 if (baking_particles && with_liquid && with_particles) {
4010 manta_bake_particles(fds->fluid, fmd, scene_framenr);
4011 }
4012 }
4013 }
4014
4015 /* Ensure that fluid pointers are always up to date at the end of modifier processing. */
4016 manta_update_pointers(fds->fluid, fmd, false);
4017
4019 fmd->time = scene_framenr;
4020}
4021
4022static void fluid_modifier_process(
4023 FluidModifierData *fmd, Depsgraph *depsgraph, Scene *scene, Object *ob, Mesh *mesh)
4024{
4025 const int scene_framenr = int(DEG_get_ctime(depsgraph));
4026
4027 if (fmd->type & MOD_FLUID_TYPE_FLOW) {
4028 fluid_modifier_processFlow(fmd, depsgraph, scene, ob, mesh, scene_framenr);
4029 }
4030 else if (fmd->type & MOD_FLUID_TYPE_EFFEC) {
4031 fluid_modifier_processEffector(fmd, depsgraph, scene, ob, mesh, scene_framenr);
4032 }
4033 else if (fmd->type & MOD_FLUID_TYPE_DOMAIN) {
4034 fluid_modifier_processDomain(fmd, depsgraph, scene, ob, mesh, scene_framenr);
4035 }
4036}
4037
4039 FluidModifierData *fmd, Depsgraph *depsgraph, Scene *scene, Object *ob, Mesh *mesh)
4040{
4041 /* Optimization: Do not update viewport during bakes (except in replay mode)
4042 * Reason: UI is locked and updated liquid / smoke geometry is not visible anyways. */
4043 bool needs_viewport_update = false;
4044
4045 /* Optimization: Only process modifier if object is not being altered. */
4046 if (!G.moving) {
4047 /* Lock so preview render does not read smoke data while it gets modified. */
4048 if ((fmd->type & MOD_FLUID_TYPE_DOMAIN) && fmd->domain) {
4050 }
4051
4052 fluid_modifier_process(fmd, depsgraph, scene, ob, mesh);
4053
4054 if ((fmd->type & MOD_FLUID_TYPE_DOMAIN) && fmd->domain) {
4055 BLI_rw_mutex_unlock(static_cast<ThreadRWMutex *>(fmd->domain->fluid_mutex));
4056 }
4057
4058 if (fmd->domain) {
4059 FluidDomainSettings *fds = fmd->domain;
4060
4061 /* Always update viewport in cache replay mode. */
4064 {
4065 needs_viewport_update = true;
4066 }
4067 /* In other cache modes, only update the viewport when no bake is going on. */
4068 else {
4069 bool with_mesh;
4070 with_mesh = fds->flags & FLUID_DOMAIN_USE_MESH;
4071 bool baking_data, baking_noise, baking_mesh, baking_particles, baking_guide;
4072 baking_data = fds->cache_flag & FLUID_DOMAIN_BAKING_DATA;
4073 baking_noise = fds->cache_flag & FLUID_DOMAIN_BAKING_NOISE;
4074 baking_mesh = fds->cache_flag & FLUID_DOMAIN_BAKING_MESH;
4075 baking_particles = fds->cache_flag & FLUID_DOMAIN_BAKING_PARTICLES;
4076 baking_guide = fds->cache_flag & FLUID_DOMAIN_BAKING_GUIDE;
4077
4078 if (with_mesh && !baking_data && !baking_noise && !baking_mesh && !baking_particles &&
4079 !baking_guide)
4080 {
4081 needs_viewport_update = true;
4082 }
4083 }
4084 }
4085 }
4086
4087 Mesh *result = nullptr;
4088 if (fmd->type & MOD_FLUID_TYPE_DOMAIN && fmd->domain) {
4089 if (needs_viewport_update) {
4090 /* Return generated geometry depending on domain type. */
4091 if (fmd->domain->type == FLUID_DOMAIN_TYPE_LIQUID) {
4092 result = create_liquid_geometry(fmd->domain, scene, mesh, ob);
4093 }
4094 if (fmd->domain->type == FLUID_DOMAIN_TYPE_GAS) {
4095 result = create_smoke_geometry(fmd->domain, mesh, ob);
4096 }
4097 }
4098
4099 /* Clear flag outside of locked block (above). */
4105 }
4106
4107 if (!result) {
4109 }
4110 else {
4112 }
4113
4114 /* Liquid simulation has a texture space that based on the bounds of the fluid mesh.
4115 * This does not seem particularly useful, but it's backwards compatible.
4116 *
4117 * Smoke simulation needs a texture space relative to the adaptive domain bounds, not the
4118 * original mesh. So recompute it at this point in the modifier stack. See #58492. */
4120
4121 return result;
4122}
4123
4124static float calc_voxel_transp(
4125 float *result, const float *input, int res[3], int *pixel, float *t_ray, float correct)
4126{
4127 const size_t index = manta_get_index(pixel[0], res[0], pixel[1], res[1], pixel[2]);
4128
4129 /* `T_ray *= T_vox`. */
4130 *t_ray *= expf(input[index] * correct);
4131
4132 if (result[index] < 0.0f) {
4133 result[index] = *t_ray;
4134 }
4135
4136 return *t_ray;
4137}
4138
4139static void bresenham_linie_3D(int x1,
4140 int y1,
4141 int z1,
4142 int x2,
4143 int y2,
4144 int z2,
4145 float *t_ray,
4147 float *result,
4148 float *input,
4149 int res[3],
4150 float correct)
4151{
4152 int dx, dy, dz, i, l, m, n, x_inc, y_inc, z_inc, err_1, err_2, dx2, dy2, dz2;
4153 int pixel[3];
4154
4155 pixel[0] = x1;
4156 pixel[1] = y1;
4157 pixel[2] = z1;
4158
4159 dx = x2 - x1;
4160 dy = y2 - y1;
4161 dz = z2 - z1;
4162
4163 x_inc = (dx < 0) ? -1 : 1;
4164 l = abs(dx);
4165 y_inc = (dy < 0) ? -1 : 1;
4166 m = abs(dy);
4167 z_inc = (dz < 0) ? -1 : 1;
4168 n = abs(dz);
4169 dx2 = l << 1;
4170 dy2 = m << 1;
4171 dz2 = n << 1;
4172
4173 if ((l >= m) && (l >= n)) {
4174 err_1 = dy2 - l;
4175 err_2 = dz2 - l;
4176 for (i = 0; i < l; i++) {
4177 if (cb(result, input, res, pixel, t_ray, correct) <= FLT_EPSILON) {
4178 break;
4179 }
4180 if (err_1 > 0) {
4181 pixel[1] += y_inc;
4182 err_1 -= dx2;
4183 }
4184 if (err_2 > 0) {
4185 pixel[2] += z_inc;
4186 err_2 -= dx2;
4187 }
4188 err_1 += dy2;
4189 err_2 += dz2;
4190 pixel[0] += x_inc;
4191 }
4192 }
4193 else if ((m >= l) && (m >= n)) {
4194 err_1 = dx2 - m;
4195 err_2 = dz2 - m;
4196 for (i = 0; i < m; i++) {
4197 if (cb(result, input, res, pixel, t_ray, correct) <= FLT_EPSILON) {
4198 break;
4199 }
4200 if (err_1 > 0) {
4201 pixel[0] += x_inc;
4202 err_1 -= dy2;
4203 }
4204 if (err_2 > 0) {
4205 pixel[2] += z_inc;
4206 err_2 -= dy2;
4207 }
4208 err_1 += dx2;
4209 err_2 += dz2;
4210 pixel[1] += y_inc;
4211 }
4212 }
4213 else {
4214 err_1 = dy2 - n;
4215 err_2 = dx2 - n;
4216 for (i = 0; i < n; i++) {
4217 if (cb(result, input, res, pixel, t_ray, correct) <= FLT_EPSILON) {
4218 break;
4219 }
4220 if (err_1 > 0) {
4221 pixel[1] += y_inc;
4222 err_1 -= dz2;
4223 }
4224 if (err_2 > 0) {
4225 pixel[0] += x_inc;
4226 err_2 -= dz2;
4227 }
4228 err_1 += dy2;
4229 err_2 += dx2;
4230 pixel[2] += z_inc;
4231 }
4232 }
4233 cb(result, input, res, pixel, t_ray, correct);
4234}
4235
4236static void manta_smoke_calc_transparency(const Main &bmain,
4238 Scene *scene,
4239 ViewLayer *view_layer)
4240{
4241 float bv[6] = {0};
4242 float light[3];
4243 int slabsize = fds->res[0] * fds->res[1];
4244 float *density = manta_smoke_get_density(fds->fluid);
4245 float *shadow = manta_smoke_get_shadow(fds->fluid);
4246 float correct = -7.0f * fds->dx;
4247
4248 if (!get_light(bmain, scene, view_layer, light)) {
4249 return;
4250 }
4251
4252 /* Convert light pos to sim cell space. */
4253 mul_m4_v3(fds->imat, light);
4254 light[0] = (light[0] - fds->p0[0]) / fds->cell_size[0] - 0.5f - float(fds->res_min[0]);
4255 light[1] = (light[1] - fds->p0[1]) / fds->cell_size[1] - 0.5f - float(fds->res_min[1]);
4256 light[2] = (light[2] - fds->p0[2]) / fds->cell_size[2] - 0.5f - float(fds->res_min[2]);
4257
4258 /* Calculate domain bounds in sim cell space. */
4259 /* 0,2,4 = 0.0f */
4260 bv[1] = float(fds->res[0]); /* X */
4261 bv[3] = float(fds->res[1]); /* Y */
4262 bv[5] = float(fds->res[2]); /* Z */
4263
4264 for (int z = 0; z < fds->res[2]; z++) {
4265 size_t index = z * slabsize;
4266
4267 for (int y = 0; y < fds->res[1]; y++) {
4268 for (int x = 0; x < fds->res[0]; x++, index++) {
4269 float voxel_center[3];
4270 float pos[3];
4271 int cell[3];
4272 float t_ray = 1.0;
4273
4274 /* Reset shadow value. */
4275 shadow[index] = -1.0f;
4276
4277 voxel_center[0] = float(x);
4278 voxel_center[1] = float(y);
4279 voxel_center[2] = float(z);
4280
4281 /* Get starting cell (light pos). */
4282 if (BLI_bvhtree_bb_raycast(bv, light, voxel_center, pos) > FLT_EPSILON) {
4283 /* We're outside -> use point on side of domain. */
4284 cell[0] = int(floor(pos[0]));
4285 cell[1] = int(floor(pos[1]));
4286 cell[2] = int(floor(pos[2]));
4287 }
4288 else {
4289 /* We're inside -> use light itself. */
4290 cell[0] = int(floor(light[0]));
4291 cell[1] = int(floor(light[1]));
4292 cell[2] = int(floor(light[2]));
4293 }
4294 /* Clamp within grid bounds */
4295 CLAMP(cell[0], 0, fds->res[0] - 1);
4296 CLAMP(cell[1], 0, fds->res[1] - 1);
4297 CLAMP(cell[2], 0, fds->res[2] - 1);
4298
4299 bresenham_linie_3D(cell[0],
4300 cell[1],
4301 cell[2],
4302 x,
4303 y,
4304 z,
4305 &t_ray,
4306 calc_voxel_transp,
4307 shadow,
4308 density,
4309 fds->res,
4310 correct);
4311
4312 /* Convention -> from a RGBA float array, use G value for t_ray. */
4313 shadow[index] = t_ray;
4314 }
4315 }
4316 }
4317}
4318
4319float BKE_fluid_get_velocity_at(Object *ob, float position[3], float velocity[3])
4320{
4322 zero_v3(velocity);
4323
4324 if (fmd && (fmd->type & MOD_FLUID_TYPE_DOMAIN) && fmd->domain && fmd->domain->fluid) {
4325 FluidDomainSettings *fds = fmd->domain;
4326 float time_mult = 25.0f * DT_DEFAULT;
4327 float size_mult = std::max({fds->global_size[0], fds->global_size[1], fds->global_size[2]}) /
4328 std::max({fds->base_res[0], fds->base_res[1], fds->base_res[2]});
4329 float vel_mag;
4330 float density = 0.0f, fuel = 0.0f;
4331 float pos[3];
4332 copy_v3_v3(pos, position);
4333 manta_pos_to_cell(fds, pos);
4334
4335 /* Check if position is outside domain max bounds. */
4336 if (pos[0] < fds->res_min[0] || pos[1] < fds->res_min[1] || pos[2] < fds->res_min[2]) {
4337 return -1.0f;
4338 }
4339 if (pos[0] > fds->res_max[0] || pos[1] > fds->res_max[1] || pos[2] > fds->res_max[2]) {
4340 return -1.0f;
4341 }
4342
4343 /* map pos between 0.0 - 1.0 */
4344 pos[0] = (pos[0] - fds->res_min[0]) / float(fds->res[0]);
4345 pos[1] = (pos[1] - fds->res_min[1]) / float(fds->res[1]);
4346 pos[2] = (pos[2] - fds->res_min[2]) / float(fds->res[2]);
4347
4348 /* Check if position is outside active area. */
4350 if (pos[0] < 0.0f || pos[1] < 0.0f || pos[2] < 0.0f) {
4351 return 0.0f;
4352 }
4353 if (pos[0] > 1.0f || pos[1] > 1.0f || pos[2] > 1.0f) {
4354 return 0.0f;
4355 }
4356 }
4357
4358 /* Get interpolated velocity at given position. */
4362
4363 /* Convert simulation units to Blender units. */
4364 mul_v3_fl(velocity, size_mult);
4365 mul_v3_fl(velocity, time_mult);
4366
4367 /* Convert velocity direction to global space. */
4368 vel_mag = len_v3(velocity);
4369 mul_mat3_m4_v3(fds->obmat, velocity);
4370 normalize_v3(velocity);
4371 mul_v3_fl(velocity, vel_mag);
4372
4373 /* Use max value of fuel or smoke density. */
4374 density = BLI_voxel_sample_trilinear(manta_smoke_get_density(fds->fluid), fds->res, pos);
4375 if (manta_smoke_has_fuel(fds->fluid)) {
4377 }
4378 return std::max(density, fuel);
4379 }
4380 return -1.0f;
4381}
4382
4384{
4385 int flags = 0;
4386
4387 if (fds->fluid) {
4388 if (manta_smoke_has_heat(fds->fluid)) {
4389 flags |= FLUID_DOMAIN_ACTIVE_HEAT;
4390 }
4391 if (manta_smoke_has_fuel(fds->fluid)) {
4392 flags |= FLUID_DOMAIN_ACTIVE_FIRE;
4393 }
4394 if (manta_smoke_has_colors(fds->fluid)) {
4396 }
4397 }
4398
4399 return flags;
4400}
4401
4403 Object *ob,
4404 const char *pset_name,
4405 const char *parts_name,
4406 const char *psys_name,
4407 eParticleType psys_type)
4408{
4409 ParticleSystem *psys;
4410 ParticleSettings *part;
4412
4413 /* add particle system */
4414 part = BKE_particlesettings_add(bmain, pset_name);
4415 psys = MEM_new<ParticleSystem>(__func__);
4416
4417 part->type = psys_type;
4418 part->totpart = 0;
4419 part->draw_size = 0.01f; /* Make fluid particles more subtle in viewport. */
4421
4422 /* Use different shape and color for fluid particles to be able to find issues in Viewport */
4423 if (psys_type == PART_FLUID_BUBBLE) {
4424 part->draw_as = PART_DRAW_CIRC;
4425 }
4426 if (psys_type == PART_FLUID_FOAM) {
4427 part->draw_as = PART_DRAW_CROSS;
4428 }
4429
4430 part->phystype = PART_PHYS_NO; /* No physics needed, part system only used to display data. */
4431 psys->part = part;
4432 psys->pointcache = BKE_ptcache_add(&psys->ptcaches);
4433 STRNCPY(psys->name, parts_name);
4434 BLI_addtail(&ob->particlesystem, psys);
4435
4436 /* add modifier */
4438 STRNCPY(pfmd->modifier.name, psys_name);
4439 pfmd->psys = psys;
4440 BLI_addtail(&ob->modifiers, pfmd);
4441 BKE_modifier_unique_name(&ob->modifiers, (ModifierData *)pfmd);
4443}
4444
4445void BKE_fluid_particle_system_destroy(Object *ob, const int particle_type)
4446{
4448 ParticleSystem *psys, *next_psys;
4449
4450 for (psys = ob->particlesystem.first(); psys; psys = next_psys) {
4451 next_psys = psys->next;
4452 if (psys->part->type == particle_type) {
4453 /* clear modifier */
4454 pfmd = psys_get_modifier(ob, psys);
4457
4458 /* clear particle system */
4459 BLI_remlink(&ob->particlesystem, psys);
4460 psys_free(ob, psys);
4461 }
4462 }
4463}
4464
4466
4467#endif /* WITH_FLUID */
4468
4469/* -------------------------------------------------------------------- */
4474
4476{
4477 settings->cache_frame_start = (value > settings->cache_frame_end) ? settings->cache_frame_end :
4478 value;
4479}
4480
4482{
4483 settings->cache_frame_end = (value < settings->cache_frame_start) ? settings->cache_frame_start :
4484 value;
4485}
4486
4488 eFluidDomain_FileFormat cache_mesh_format)
4489{
4490 if (cache_mesh_format == settings->cache_mesh_format) {
4491 return;
4492 }
4493 /* TODO(sebbas): Clear old caches. */
4494 settings->cache_mesh_format = cache_mesh_format;
4495}
4496
4498 eFluidDomain_FileFormat cache_data_format)
4499{
4500 if (cache_data_format == settings->cache_data_format) {
4501 return;
4502 }
4503 /* TODO(sebbas): Clear old caches. */
4504 settings->cache_data_format = cache_data_format;
4505}
4506
4508 eFluidDomain_FileFormat cache_particle_format)
4509{
4510 if (cache_particle_format == settings->cache_particle_format) {
4511 return;
4512 }
4513 /* TODO(sebbas): Clear old caches. */
4514 settings->cache_particle_format = cache_particle_format;
4515}
4516
4518 eFluidDomain_FileFormat cache_noise_format)
4519{
4520 if (cache_noise_format == settings->cache_noise_format) {
4521 return;
4522 }
4523 /* TODO(sebbas): Clear old caches. */
4524 settings->cache_noise_format = cache_noise_format;
4525}
4526
4529 bool clear)
4530{
4531 if (clear) {
4532 settings->border_collisions &= ~value;
4533 }
4534 else {
4535 settings->border_collisions |= value;
4536 }
4537}
4538
4541 bool clear)
4542{
4543 if (clear) {
4544 settings->particle_type &= ~value;
4545 }
4546 else {
4547 settings->particle_type |= value;
4548 }
4549}
4550
4552 FluidDomainSettings *settings,
4553 eFluidDomain_Type type)
4554{
4555 /* Set values for border collision:
4556 * Liquids should have a closed domain, smoke domains should be open. */
4557 if (type == FLUID_DOMAIN_TYPE_GAS) {
4564 object->dt = OB_WIRE;
4565 }
4566 else if (type == FLUID_DOMAIN_TYPE_LIQUID) {
4573 object->dt = OB_SOLID;
4574 }
4575
4576 /* Set actual domain type. */
4577 settings->type = type;
4578}
4579
4581 FluidFlowSettings *settings,
4582 eFluidFlow_Behavior behavior)
4583{
4584 settings->behavior = behavior;
4585}
4586
4588{
4589 /* By default, liquid flow objects should behave like their geometry (geometry behavior),
4590 * gas flow objects should continuously produce smoke (inflow behavior). */
4591 if (type == FLUID_FLOW_TYPE_LIQUID) {
4593 }
4594 else {
4596 }
4597
4598 /* Set actual flow type. */
4599 settings->type = type;
4600}
4601
4603 FluidEffectorSettings *settings,
4605{
4606 settings->type = type;
4607}
4608
4610{
4611 /* Based on the domain type, certain fields are defaulted accordingly if the selected field
4612 * is unsupported. */
4613 const char coba_field = settings->coba_field;
4614 const char data_depth = settings->openvdb_data_depth;
4615
4616 if (settings->type == FLUID_DOMAIN_TYPE_GAS) {
4617 if (ELEM(coba_field,
4622 {
4623 /* Defaulted to density for gas domain. */
4625 }
4626
4627 /* Gas domains do not support VDB mini precision. */
4628 if (data_depth == VDB_PRECISION_MINI_FLOAT) {
4630 }
4631 }
4632 else if (settings->type == FLUID_DOMAIN_TYPE_LIQUID) {
4633 if (ELEM(coba_field,
4641 {
4642 /* Defaulted to phi for liquid domain. */
4644 }
4645 }
4646}
4647
4649
4650/* -------------------------------------------------------------------- */
4655
4657{
4658 if (fmd->domain) {
4659 if (fmd->domain->fluid) {
4660#ifdef WITH_FLUID
4661 manta_free(fmd->domain->fluid);
4662#endif
4663 }
4664
4665 if (fmd->domain->fluid_mutex) {
4666 BLI_rw_mutex_free(static_cast<ThreadRWMutex *>(fmd->domain->fluid_mutex));
4667 }
4668
4669 MEM_SAFE_DELETE(fmd->domain->effector_weights);
4670
4673 fmd->domain->point_cache[0] = nullptr;
4674 }
4675
4676 if (fmd->domain->coba) {
4677 MEM_delete(fmd->domain->coba);
4678 }
4679
4680 MEM_delete(fmd->domain);
4681 fmd->domain = nullptr;
4682 }
4683}
4684
4686{
4687 if (fmd->flow) {
4688 if (fmd->flow->mesh) {
4689 BKE_id_free(nullptr, fmd->flow->mesh);
4690 }
4691 fmd->flow->mesh = nullptr;
4692
4693 MEM_SAFE_DELETE(fmd->flow->verts_old);
4694 fmd->flow->numverts = 0;
4696
4697 MEM_delete(fmd->flow);
4698 fmd->flow = nullptr;
4699 }
4700}
4701
4703{
4704 if (fmd->effector) {
4705 if (fmd->effector->mesh) {
4706 BKE_id_free(nullptr, fmd->effector->mesh);
4707 }
4708 fmd->effector->mesh = nullptr;
4709
4710 MEM_SAFE_DELETE(fmd->effector->verts_old);
4711 fmd->effector->numverts = 0;
4713
4714 MEM_delete(fmd->effector);
4715 fmd->effector = nullptr;
4716 }
4717}
4718
4719static void fluid_modifier_reset_ex(FluidModifierData *fmd, bool need_lock)
4720{
4721 if (!fmd) {
4722 return;
4723 }
4724
4725 if (fmd->domain) {
4726 if (fmd->domain->fluid) {
4727 if (need_lock) {
4728 BLI_rw_mutex_lock(static_cast<ThreadRWMutex *>(fmd->domain->fluid_mutex),
4730 }
4731
4732#ifdef WITH_FLUID
4733 manta_free(fmd->domain->fluid);
4734#endif
4735 fmd->domain->fluid = nullptr;
4736
4737 if (need_lock) {
4738 BLI_rw_mutex_unlock(static_cast<ThreadRWMutex *>(fmd->domain->fluid_mutex));
4739 }
4740 }
4741
4742 fmd->time = -1;
4743 fmd->domain->total_cells = 0;
4745 }
4746 else if (fmd->flow) {
4747 MEM_SAFE_DELETE(fmd->flow->verts_old);
4748 fmd->flow->numverts = 0;
4750 }
4751 else if (fmd->effector) {
4752 MEM_SAFE_DELETE(fmd->effector->verts_old);
4753 fmd->effector->numverts = 0;
4755 }
4756}
4757
4762
4764{
4765 if (!fmd) {
4766 return;
4767 }
4768
4772}
4773
4775{
4776 if (!fmd) {
4777 return;
4778 }
4779
4780 if (fmd->type & MOD_FLUID_TYPE_DOMAIN) {
4781 if (fmd->domain) {
4783 }
4784
4785 fmd->domain = MEM_new<FluidDomainSettings>(__func__);
4786 fmd->domain->fmd = fmd;
4787
4788 /* Turn off incompatible options. */
4789#ifndef WITH_OPENVDB
4793#endif
4794#ifndef WITH_OPENVDB_BLOSC
4796#endif
4797
4800
4801 char cache_name[64];
4802 BKE_fluid_cache_new_name_for_current_session(sizeof(cache_name), cache_name);
4804 fmd->domain->cache_directory, sizeof(fmd->domain->cache_directory), cache_name);
4805
4806 /* pointcache options */
4807 fmd->domain->point_cache[0] = BKE_ptcache_add(&(fmd->domain->ptcaches[0]));
4809 fmd->domain->point_cache[0]->step = 1;
4810 fmd->domain->point_cache[1] = nullptr; /* Deprecated */
4811 }
4812 else if (fmd->type & MOD_FLUID_TYPE_FLOW) {
4813 if (fmd->flow) {
4815 }
4816
4817 fmd->flow = MEM_new<FluidFlowSettings>(__func__);
4818 fmd->flow->fmd = fmd;
4819 }
4820 else if (fmd->type & MOD_FLUID_TYPE_EFFEC) {
4821 if (fmd->effector) {
4823 }
4824
4825 fmd->effector = MEM_new<FluidEffectorSettings>(__func__);
4826 fmd->effector->fmd = fmd;
4827 }
4828}
4829
4830void BKE_fluid_modifier_copy(const FluidModifierData *fmd, FluidModifierData *tfmd, const int flag)
4831{
4832 tfmd->type = fmd->type;
4833 tfmd->time = fmd->time;
4834
4836
4837 if (tfmd->domain) {
4838 FluidDomainSettings *tfds = tfmd->domain;
4839 FluidDomainSettings *fds = fmd->domain;
4840
4841 /* domain object data */
4842 tfds->fluid_group = fds->fluid_group;
4843 tfds->force_group = fds->force_group;
4844 tfds->effector_group = fds->effector_group;
4845 if (tfds->effector_weights) {
4846 MEM_delete(tfds->effector_weights);
4847 }
4848 tfds->effector_weights = MEM_dupalloc(fds->effector_weights);
4849
4850 /* adaptive domain options */
4851 tfds->adapt_margin = fds->adapt_margin;
4852 tfds->adapt_res = fds->adapt_res;
4853 tfds->adapt_threshold = fds->adapt_threshold;
4854
4855 /* fluid domain options */
4856 tfds->maxres = fds->maxres;
4857 tfds->solver_res = fds->solver_res;
4859 tfds->flags = fds->flags;
4860 tfds->gravity[0] = fds->gravity[0];
4861 tfds->gravity[1] = fds->gravity[1];
4862 tfds->gravity[2] = fds->gravity[2];
4863 tfds->active_fields = fds->active_fields;
4864 tfds->type = fds->type;
4865 tfds->boundary_width = fds->boundary_width;
4866
4867 /* smoke domain options */
4868 tfds->alpha = fds->alpha;
4869 tfds->beta = fds->beta;
4870 tfds->diss_speed = fds->diss_speed;
4871 tfds->vorticity = fds->vorticity;
4873
4874 /* flame options */
4875 tfds->burning_rate = fds->burning_rate;
4876 tfds->flame_smoke = fds->flame_smoke;
4877 tfds->flame_vorticity = fds->flame_vorticity;
4878 tfds->flame_ignition = fds->flame_ignition;
4879 tfds->flame_max_temp = fds->flame_max_temp;
4881
4882 /* noise options */
4883 tfds->noise_strength = fds->noise_strength;
4884 tfds->noise_pos_scale = fds->noise_pos_scale;
4885 tfds->noise_time_anim = fds->noise_time_anim;
4886 tfds->noise_scale = fds->noise_scale;
4887
4888 /* liquid domain options */
4889 tfds->flip_ratio = fds->flip_ratio;
4891 tfds->particle_number = fds->particle_number;
4894 tfds->particle_radius = fds->particle_radius;
4900
4901 /* viscosity options */
4902 tfds->viscosity_value = fds->viscosity_value;
4903
4904 /* Diffusion options. */
4905 tfds->surface_tension = fds->surface_tension;
4906 tfds->viscosity_base = fds->viscosity_base;
4908
4909 /* mesh options */
4915 tfds->mesh_scale = fds->mesh_scale;
4916 tfds->mesh_generator = fds->mesh_generator;
4917
4918 /* secondary particle options */
4919 tfds->sndparticle_k_b = fds->sndparticle_k_b;
4920 tfds->sndparticle_k_d = fds->sndparticle_k_d;
4935 tfds->particle_type = fds->particle_type;
4936 tfds->particle_scale = fds->particle_scale;
4937
4938 /* fluid guide options */
4939 tfds->guide_parent = fds->guide_parent;
4940 tfds->guide_alpha = fds->guide_alpha;
4941 tfds->guide_beta = fds->guide_beta;
4943 copy_v3_v3_int(tfds->guide_res, fds->guide_res);
4944 tfds->guide_source = fds->guide_source;
4945
4946 /* cache options */
4948 tfds->cache_frame_end = fds->cache_frame_end;
4955 tfds->cache_flag = fds->cache_flag;
4956 tfds->cache_type = fds->cache_type;
4962
4963 /* time options */
4964 tfds->time_scale = fds->time_scale;
4965 tfds->cfl_condition = fds->cfl_condition;
4968
4969 /* display options */
4971 tfds->slice_axis = fds->slice_axis;
4972 tfds->interp_method = fds->interp_method;
4973 tfds->draw_velocity = fds->draw_velocity;
4974 tfds->slice_per_voxel = fds->slice_per_voxel;
4975 tfds->slice_depth = fds->slice_depth;
4977 tfds->show_gridlines = fds->show_gridlines;
4978 if (fds->coba) {
4979 tfds->coba = MEM_dupalloc(fds->coba);
4980 }
4981 tfds->vector_scale = fds->vector_scale;
4983 tfds->vector_field = fds->vector_field;
4986 tfds->use_coba = fds->use_coba;
4987 tfds->coba_field = fds->coba_field;
4988 tfds->grid_scale = fds->grid_scale;
4994
4995 /* -- Deprecated / unused options (below)-- */
4996
4997 /* pointcache options */
4998 BKE_ptcache_free_list(&(tfds->ptcaches[0]));
5000 /* Share the cache with the original object's modifier. */
5002 tfds->point_cache[0] = fds->point_cache[0];
5003 tfds->ptcaches[0] = fds->ptcaches[0];
5004 }
5005 else {
5007 &(tfds->ptcaches[0]), &(fds->ptcaches[0]), flag);
5008 }
5009
5010 /* OpenVDB cache options */
5012 tfds->clipping = fds->clipping;
5014
5015 /* Render options. */
5016 tfds->velocity_scale = fds->velocity_scale;
5017 }
5018 else if (tfmd->flow) {
5019 FluidFlowSettings *tffs = tfmd->flow;
5020 FluidFlowSettings *ffs = fmd->flow;
5021
5022 /* NOTE: This is dangerous, as it will generate invalid data in case we are copying between
5023 * different objects. Extra external code has to be called to ensure proper remapping of
5024 * that pointer. See e.g. `BKE_object_copy_particlesystems` or `BKE_object_copy_modifier`. */
5025 tffs->psys = ffs->psys;
5026 tffs->noise_texture = ffs->noise_texture;
5027
5028 /* initial velocity */
5029 tffs->vel_multi = ffs->vel_multi;
5030 tffs->vel_normal = ffs->vel_normal;
5031 tffs->vel_random = ffs->vel_random;
5032 tffs->vel_coord[0] = ffs->vel_coord[0];
5033 tffs->vel_coord[1] = ffs->vel_coord[1];
5034 tffs->vel_coord[2] = ffs->vel_coord[2];
5035
5036 /* emission */
5037 tffs->density = ffs->density;
5038 copy_v3_v3(tffs->color, ffs->color);
5039 tffs->fuel_amount = ffs->fuel_amount;
5040 tffs->temperature = ffs->temperature;
5041 tffs->volume_density = ffs->volume_density;
5043 tffs->particle_size = ffs->particle_size;
5044 tffs->subframes = ffs->subframes;
5045
5046 /* texture control */
5047 tffs->texture_size = ffs->texture_size;
5048 tffs->texture_offset = ffs->texture_offset;
5049 STRNCPY(tffs->uvlayer_name, ffs->uvlayer_name);
5050 tffs->vgroup_density = ffs->vgroup_density;
5051
5052 tffs->type = ffs->type;
5053 tffs->behavior = ffs->behavior;
5054 tffs->source = ffs->source;
5055 tffs->texture_type = ffs->texture_type;
5056 tffs->flags = ffs->flags;
5057 }
5058 else if (tfmd->effector) {
5059 FluidEffectorSettings *tfes = tfmd->effector;
5060 FluidEffectorSettings *fes = fmd->effector;
5061
5063 tfes->type = fes->type;
5064 tfes->flags = fes->flags;
5065 tfes->subframes = fes->subframes;
5066
5067 /* guide options */
5068 tfes->guide_mode = fes->guide_mode;
5069 tfes->vel_multi = fes->vel_multi;
5070 }
5071}
5072
5074{
5075 static int counter = 1;
5076 BLI_snprintf(r_name, maxlen, FLUID_DOMAIN_DIR_DEFAULT "_%x", BLI_hash_int(counter));
5077 counter++;
5078}
5079
5081
5082} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
support for deformation groups and hooks.
General operations, lookup, etc. for blender objects.
#define OBJECT_MODIFIER_UPDATE_SUBFRAME_RECURSION_DEFAULT
#define PTCACHE_RESET_OUTDATED
File and directory operations.
A KD-tree for nearest neighbor search.
#define FILE_MAX
#define BLI_path_join(...)
#define THREAD_LOCK_WRITE
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define FLUID_DOMAIN_DIR_DATA
#define FLUID_DOMAIN_DIR_PARTICLES
#define FLUID_DOMAIN_DIR_DEFAULT
#define FLUID_DOMAIN_DIR_MESH
#define FLUID_DOMAIN_DIR_GUIDE
#define FLUID_DOMAIN_DIR_SCRIPT
#define FLUID_DOMAIN_DIR_CONFIG
#define FLUID_DOMAIN_DIR_NOISE
Object is a sort of wrapper for general info.
Types and defines for representing Rigid Body entities.
Read Blender memory allocation and freeing.
volatile int lock
#define CLAMP(a, b, c)
#define CLAMP_MAX(a, c)
#define INIT_MINMAX(min, max)
#define BLI_assert_unreachable()
#define UNUSED_VARS(...)
#define BLI_INLINE
#define UNPACK3(a)
#define ELEM(...)
#define CLAMP_MIN(a, b)
btScalar max
#define output
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
AttributeSet attributes
array< Particle > particles
Definition particles.h:41
constexpr bool is_empty() const
Definition BLI_span.hh:264
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
GAttributeReader lookup(const StringRef name) const
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef name, AttrDomain domain, AttrType data_type)
constexpr IndexRange drop_back(int64_t n) const
constexpr bool is_empty() const
#define expf(x)
#define PHI_MAX
Definition fluid.cc:89
#define DT_DEFAULT
Definition fluid.cc:86
#define FLT_MAX
#define FLT_EPSILON
#define position
#define smooth
#define pow
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
#define floor
#define ceil
float3x3 scale(float3x3 mat, float2 scale)
const int state
#define LOG(level)
Definition log.h:97
void mesh_smooth_set(Mesh &mesh, bool use_smooth, bool keep_sharp_edges=false)
void mesh_calc_edges(Mesh &mesh, bool keep_existing_edges, bool select_new_edges)
int size(const AssetLibraryReference *library_reference)
T distance(const T &a, const T &b)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
T abs(const T &a)
OffsetIndices< int > accumulate_counts_to_offsets(MutableSpan< int > counts_to_offsets, int start_offset=0)
bool is_adaptive(const CpuPatchTable *patch_table)
BMesh Iterators.
@ FLUID_DOMAIN_FIELD_COLOR_G
@ FLUID_DOMAIN_FIELD_FUEL
@ FLUID_DOMAIN_FIELD_PHI_OBSTACLE
@ FLUID_DOMAIN_FIELD_PHI_IN
@ FLUID_DOMAIN_FIELD_FLAME
@ FLUID_DOMAIN_FIELD_COLOR_B
@ FLUID_DOMAIN_FIELD_DENSITY
@ FLUID_DOMAIN_FIELD_COLOR_R
@ FLUID_DOMAIN_FIELD_PHI_OUT
@ FLUID_DOMAIN_FIELD_PHI
@ FLUID_DOMAIN_FIELD_HEAT
void BKE_fluid_cachetype_data_set(struct FluidDomainSettings *settings, eFluidDomain_FileFormat cache_data_format)
Definition fluid.cc:4497
static void fluid_modifier_freeEffector(FluidModifierData *fmd)
Definition fluid.cc:4702
MINLINE void add_v3fl_v3fl_v3i(float r[3], const float a[3], const int b[3])
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_fluid_cache_new_name_for_current_session(int maxlen, char *r_name)
Definition fluid.cc:5073
void BKE_fluid_modifier_reset(struct FluidModifierData *fmd)
Definition fluid.cc:4758
void kdtree_balance(KDTree< CoordT > *tree)
bool manta_read_noise(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr, bool resumable)
bool manta_bake_mesh(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr)
@ VDB_PRECISION_HALF_FLOAT
@ VDB_PRECISION_MINI_FLOAT
size_t BLI_snprintf(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
float * manta_get_ob_velocity_z(struct MANTA *fluid)
float * manta_get_guide_velocity_x(struct MANTA *fluid)
char * STRNCPY(char(&dst)[N], const char *src)
int manta_liquid_get_triangle_y_at(struct MANTA *liquid, int i)
float manta_liquid_get_vertex_x_at(struct MANTA *liquid, int i)
bool manta_has_mesh(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr)
static void fluid_modifier_reset_ex(FluidModifierData *fmd, bool need_lock)
Definition fluid.cc:4719
float * manta_noise_get_texture_u2(struct MANTA *smoke)
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BKE_fluid_cachetype_noise_set(struct FluidDomainSettings *settings, eFluidDomain_FileFormat cache_noise_format)
Definition fluid.cc:4517
float * manta_noise_get_color_b(struct MANTA *smoke)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
float(*) BKE_Fluid_BresenhamFn(float *result, const float *input, int res[3], int *pixel, float *tRay, float correct)
Definition BKE_fluid.h:30
@ FLUID_FLOW_BEHAVIOR_GEOMETRY
@ FLUID_FLOW_BEHAVIOR_OUTFLOW
@ FLUID_FLOW_BEHAVIOR_INFLOW
float * manta_smoke_get_react(struct MANTA *smoke)
void BKE_fluid_cache_endframe_set(struct FluidDomainSettings *settings, int value)
Definition fluid.cc:4481
void minmax_v3v3_v3(float min[3], float max[3], const float vec[3])
float * manta_get_in_velocity_x(struct MANTA *fluid)
@ FLUID_DOMAIN_BORDER_BACK
@ FLUID_DOMAIN_BORDER_LEFT
@ FLUID_DOMAIN_BORDER_TOP
@ FLUID_DOMAIN_BORDER_RIGHT
@ FLUID_DOMAIN_BORDER_FRONT
@ FLUID_DOMAIN_BORDER_BOTTOM
float * manta_smoke_get_shadow(struct MANTA *smoke)
void psys_sim_data_init(struct ParticleSimulationData *sim)
Definition particle.cc:621
ATTR_WARN_UNUSED_RESULT const BMLoop * l
void manta_free(struct MANTA *fluid)
ThreadRWMutex * BLI_rw_mutex_alloc()
Definition threads.cc:490
MINLINE void sub_v3_v3v3_int(int r[3], const int a[3], const int b[3])
float * manta_noise_get_texture_w2(struct MANTA *smoke)
bool BKE_object_moves_in_time(const Object *object, bool recurse_parent)
Definition object.cc:4968
@ FLUID_EFFECTOR_GUIDE_MIN
@ FLUID_EFFECTOR_GUIDE_AVERAGED
@ FLUID_EFFECTOR_GUIDE_MAX
@ FLUID_EFFECTOR_GUIDE_OVERRIDE
Mesh * BKE_mesh_copy_for_eval(const Mesh &source)
void interp_v2_v2v2v2(float r[2], const float a[2], const float b[2], const float c[2], const float t[3])
float * manta_get_force_x(struct MANTA *fluid)
void BKE_fluid_effector_type_set(struct Object *object, struct FluidEffectorSettings *settings, eFluidEffector_Type type)
Definition fluid.cc:4602
int BKE_ptcache_id_reset(struct Scene *scene, PTCacheID *pid, int mode)
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
MINLINE void copy_v3_v3_int(int r[3], const int a[3])
float * manta_smoke_get_color_r_in(struct MANTA *smoke)
void BLI_rw_mutex_free(ThreadRWMutex *mutex)
Definition threads.cc:497
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
float * manta_get_phiout_in(struct MANTA *fluid)
@ eModifierType_ParticleSystem
@ eModifierType_DynamicPaint
float * manta_get_phi_in(struct MANTA *fluid)
bool manta_has_particles(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr)
float * manta_get_ob_velocity_x(struct MANTA *fluid)
float * manta_smoke_get_heat(struct MANTA *smoke)
MINLINE void add_v3_fl(float r[3], float f)
void BKE_effectors_apply(ListBaseT< EffectorCache > *effectors, ListBaseT< ColliderCache > *colliders, struct EffectorWeights *weights, struct EffectedPoint *point, float *force, float *wind_force, float *impulse)
Definition effect.cc:1134
bool manta_has_noise(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr)
void BKE_ptcache_id_from_smoke(PTCacheID *pid, struct Object *ob, struct FluidModifierData *fmd)
MINLINE int max_ii(int a, int b)
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
MINLINE void madd_v3fl_v3fl_v3fl_v3i(float r[3], const float a[3], const float b[3], const int c[3])
ATTR_WARN_UNUSED_RESULT const BMVert * v
void BKE_fluid_fields_sanitize(struct FluidDomainSettings *settings)
Definition fluid.cc:4609
struct ParticleSystemModifierData * psys_get_modifier(struct Object *ob, struct ParticleSystem *psys)
Definition particle.cc:2190
bool manta_smoke_ensure_heat(struct MANTA *smoke, struct FluidModifierData *fmd)
void BKE_fluid_domain_type_set(struct Object *object, struct FluidDomainSettings *settings, eFluidDomain_Type type)
Definition fluid.cc:4551
struct ParticleSettings * BKE_particlesettings_add(struct Main *bmain, const char *name)
Definition particle.cc:4135
void BLI_rw_mutex_lock(ThreadRWMutex *mutex, int mode)
Definition threads.cc:470
void BKE_fluid_collisionextents_set(struct FluidDomainSettings *settings, eFluidDomain_BorderFlags value, bool clear)
Definition fluid.cc:4527
void BKE_modifiers_persistent_uid_init(const Object &object, ModifierData &md)
int manta_liquid_get_num_triangles(struct MANTA *liquid)
@ FLUID_DOMAIN_FILE_LOAD
@ FLUID_DOMAIN_USE_RESUMABLE_CACHE
@ FLUID_DOMAIN_USE_GUIDE
@ FLUID_DOMAIN_USE_SPEED_VECTORS
@ FLUID_DOMAIN_EXPORT_MANTA_SCRIPT
@ FLUID_DOMAIN_USE_NOISE
@ FLUID_DOMAIN_USE_MESH
@ FLUID_DOMAIN_USE_ADAPTIVE_DOMAIN
float * manta_noise_get_density(struct MANTA *smoke)
@ LIB_ID_COPY_SET_COPIED_ON_WRITE
float * manta_get_guide_velocity_y(struct MANTA *fluid)
bool manta_smoke_has_colors(struct MANTA *smoke)
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE float pow2f(float x)
bool manta_bake_data(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr)
float * manta_smoke_get_color_g_in(struct MANTA *smoke)
bool manta_ensure_obstacle(struct MANTA *fluid, struct FluidModifierData *fmd)
float * manta_get_phiobsstatic_in(struct MANTA *fluid)
@ FLUID_DOMAIN_CACHE_MODULAR
@ FLUID_DOMAIN_CACHE_REPLAY
@ FLUID_DOMAIN_CACHE_ALL
float * manta_smoke_get_react_in(struct MANTA *smoke)
bool manta_read_config(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr)
@ FLUID_DOMAIN_ACTIVE_OUTFLOW
@ FLUID_DOMAIN_ACTIVE_COLORS
@ FLUID_DOMAIN_ACTIVE_FIRE
@ FLUID_DOMAIN_ACTIVE_COLOR_SET
@ FLUID_DOMAIN_ACTIVE_HEAT
@ FLUID_DOMAIN_ACTIVE_INVEL
@ FLUID_DOMAIN_ACTIVE_GUIDE
@ FLUID_DOMAIN_ACTIVE_OBSTACLE
float * manta_noise_get_color_r(struct MANTA *smoke)
Global G
Definition blender.cc:57
MINLINE void sub_v3_v3(float r[3], const float a[3])
void BKE_modifier_unique_name(ListBaseT< ModifierData > *modifiers, ModifierData *md)
float * manta_get_phioutstatic_in(struct MANTA *fluid)
float manta_liquid_get_vertvel_z_at(struct MANTA *liquid, int i)
float BKE_fluid_get_velocity_at(struct Object *ob, float position[3], float velocity[3])
struct MANTA * manta_init(int *res, struct FluidModifierData *fmd)
KDTree< CoordT > * kdtree_new(uint nodes_len_capacity)
Definition BLI_kdtree.hh:65
bool manta_ensure_invelocity(struct MANTA *fluid, struct FluidModifierData *fmd)
void mul_m4_v3(const float M[4][4], float r[3])
void BKE_fluid_modifier_free(struct FluidModifierData *fmd)
Definition fluid.cc:4763
int manta_liquid_get_num_verts(struct MANTA *liquid)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE bool equals_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
bool manta_ensure_outflow(struct MANTA *fluid, struct FluidModifierData *fmd)
float BKE_defvert_find_weight(const MDeformVert *dvert, int defgroup)
Definition deform.cc:779
void BKE_mesh_copy_parameters_for_eval(Mesh *me_dst, const Mesh *me_src)
float * manta_get_num_guide(struct MANTA *fluid)
float * manta_get_velocity_z(struct MANTA *fluid)
int manta_liquid_get_triangle_z_at(struct MANTA *liquid, int i)
VecBase< float, 2 > float2
void manta_update_variables(struct MANTA *fluid, struct FluidModifierData *fmd)
bool psys_get_particle_state(struct ParticleSimulationData *sim, int p, struct ParticleKey *state, bool always)
Definition particle.cc:4930
VecBase< int32_t, 3 > int3
struct Mesh * BKE_fluid_modifier_do(struct FluidModifierData *fmd, struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, struct Mesh *mesh)
void BKE_fluid_cache_free_all(struct FluidDomainSettings *fds, struct Object *ob)
MINLINE void mul_v3_v3(float r[3], const float a[3])
BLI_INLINE unsigned int BLI_hash_int(unsigned int k)
Definition BLI_hash_c.hh:89
@ FLUID_EFFECTOR_USE_PLANE_INIT
@ FLUID_EFFECTOR_USE_EFFEC
@ FLUID_EFFECTOR_NEEDS_UPDATE
float * manta_get_force_z(struct MANTA *fluid)
float * manta_get_in_velocity_y(struct MANTA *fluid)
void BLI_rw_mutex_unlock(ThreadRWMutex *mutex)
Definition threads.cc:480
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
Main * DEG_get_bmain(const Depsgraph *graph)
float BLI_voxel_sample_trilinear(const float *data, const int res[3], const float co[3])
Definition voxel.cc:46
BMesh const char void * data
void BKE_fluid_particles_set(struct FluidDomainSettings *settings, eFluidDomain_ParticleTypes value, bool clear)
Definition fluid.cc:4539
float manta_liquid_get_vertvel_x_at(struct MANTA *liquid, int i)
ModifierData * BKE_modifier_new(int type)
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
void manta_adapt_timestep(struct MANTA *fluid)
float * manta_noise_get_color_g(struct MANTA *smoke)
void psys_free(struct Object *ob, struct ParticleSystem *psys)
Definition particle.cc:1009
bool manta_bake_guiding(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr)
float * manta_smoke_get_color_r(struct MANTA *smoke)
void interp_weights_tri_v3(float w[3], const float v1[3], const float v2[3], const float v3[3], const float co[3])
void kdtree_insert(KDTree< CoordT > *tree, int index, const CoordT &co)
Definition BLI_kdtree.hh:96
bool manta_read_particles(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr, bool resumable)
void BKE_fluid_cachetype_particle_set(struct FluidDomainSettings *settings, eFluidDomain_FileFormat cache_particle_format)
Definition fluid.cc:4507
@ FLUID_DOMAIN_FILE_UNI
float * manta_get_phistatic_in(struct MANTA *fluid)
Mesh * BKE_mesh_new_nomain(int verts_num, int edges_num, int faces_num, int corners_num)
ModifierData * BKE_modifiers_get_virtual_modifierlist(const Object *ob, VirtualModifierData *data)
bool manta_liquid_ensure_sndparts(struct MANTA *liquid, struct FluidModifierData *fmd)
void BKE_modifier_free(ModifierData *md)
void manta_noise_get_res(struct MANTA *smoke, int *res)
int kdtree_find_nearest(const KDTree< CoordT > *tree, const CoordT &co, KDTreeNearest< CoordT > *r_nearest)
void BKE_fluid_cache_startframe_set(struct FluidDomainSettings *settings, int value)
Definition fluid.cc:4475
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
MINLINE void negate_v3(float r[3])
float manta_liquid_get_vertex_y_at(struct MANTA *liquid, int i)
bool manta_has_data(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr)
ViewLayer * DEG_get_evaluated_view_layer(const Depsgraph *graph)
float BLI_bvhtree_bb_raycast(const float bv[6], const float light_start[3], const float light_end[3], float pos[3])
float * manta_get_force_y(struct MANTA *fluid)
float manta_get_timestep(struct MANTA *fluid)
void BKE_modifier_remove_from_list(Object *ob, ModifierData *md)
float * manta_noise_get_fuel(struct MANTA *smoke)
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
void BKE_fluid_cachetype_mesh_set(struct FluidDomainSettings *settings, eFluidDomain_FileFormat cache_mesh_format)
Definition fluid.cc:4487
int manta_liquid_get_triangle_x_at(struct MANTA *liquid, int i)
void BKE_object_modifier_update_subframe(Depsgraph *depsgraph, Scene *scene, Object *ob, bool update_mesh, int parent_recursion_limit, float frame, int modifier_type)
Definition object.cc:5624
MINLINE void zero_v3(float r[3])
float manta_liquid_get_vertvel_y_at(struct MANTA *liquid, int i)
void BKE_id_free(Main *bmain, void *idv)
bool manta_ensure_guiding(struct MANTA *fluid, struct FluidModifierData *fmd)
bool manta_smoke_has_fuel(struct MANTA *smoke)
float * manta_smoke_get_fuel(struct MANTA *smoke)
void BKE_fluid_flow_behavior_set(struct Object *object, struct FluidFlowSettings *settings, eFluidFlow_Behavior behavior)
Definition fluid.cc:4580
void mul_mat3_m4_v3(const float mat[4][4], float r[3])
void BKE_texture_get_value(struct Tex *texture, const float *tex_co, struct TexResult *texres, bool use_color_management)
Definition texture.cc:640
float * manta_smoke_get_density(struct MANTA *smoke)
bool BKE_fluid_reallocate_fluid(struct FluidDomainSettings *fds, int res[3], int free_old)
size_t manta_get_index(int x, int max_x, int y, int max_y, int z)
float * manta_smoke_get_heat_in(struct MANTA *smoke)
void BKE_fluid_cache_free(struct FluidDomainSettings *fds, struct Object *ob, int cache_map)
int * manta_smoke_get_flags(struct MANTA *smoke)
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
void BKE_fluid_modifier_copy(const struct FluidModifierData *fmd, struct FluidModifierData *tfmd, int flag)
MINLINE float normalize_v3(float n[3])
@ FLUID_DOMAIN_BAKED_PARTICLES
@ FLUID_DOMAIN_BAKED_GUIDE
@ FLUID_DOMAIN_BAKED_NOISE
@ FLUID_DOMAIN_OUTDATED_MESH
@ FLUID_DOMAIN_BAKED_DATA
@ FLUID_DOMAIN_OUTDATED_NOISE
@ FLUID_DOMAIN_BAKING_NOISE
@ FLUID_DOMAIN_BAKING_MESH
@ FLUID_DOMAIN_OUTDATED_GUIDE
@ FLUID_DOMAIN_BAKED_MESH
@ FLUID_DOMAIN_OUTDATED_PARTICLES
@ FLUID_DOMAIN_BAKING_PARTICLES
@ FLUID_DOMAIN_BAKING_DATA
@ FLUID_DOMAIN_OUTDATED_DATA
@ FLUID_DOMAIN_BAKING_GUIDE
float manta_liquid_get_vertex_z_at(struct MANTA *liquid, int i)
ModifierData * BKE_modifiers_findby_type(const Object *ob, ModifierType type)
MINLINE void add_v3_v3(float r[3], const float a[3])
void kdtree_free(KDTree< CoordT > *tree)
Definition BLI_kdtree.hh:84
float * manta_get_guide_velocity_z(struct MANTA *fluid)
float * manta_noise_get_flame(struct MANTA *smoke)
float * manta_get_ob_velocity_y(struct MANTA *fluid)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void pd_point_from_loc(struct Scene *scene, float *loc, float *vel, int index, struct EffectedPoint *point)
Definition effect.cc:424
void manta_update_pointers(struct MANTA *fluid, struct FluidModifierData *fmd, bool flush)
void BKE_ptcache_free_list(ListBaseT< PointCache > *ptcaches)
void BKE_fluid_particle_system_destroy(struct Object *ob, int particle_type)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
bool manta_smoke_ensure_colors(struct MANTA *smoke, struct FluidModifierData *fmd)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
float BKE_scene_ctime_get(const Scene *scene)
Definition scene.cc:2581
MINLINE void copy_v3_fl(float r[3], float f)
bool manta_write_config(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr)
struct Object ** BKE_collision_objects_create(struct Depsgraph *depsgraph, struct Object *self, struct Collection *collection, unsigned int *numcollobj, unsigned int modifier_type)
float * manta_get_num_obstacle(struct MANTA *fluid)
float * manta_smoke_get_flame(struct MANTA *smoke)
bool manta_bake_particles(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr)
float * manta_smoke_get_color_b_in(struct MANTA *smoke)
@ FLUID_FLOW_TYPE_SMOKEFIRE
@ FLUID_FLOW_TYPE_LIQUID
@ FLUID_FLOW_TYPE_SMOKE
std::mutex Mutex
Definition BLI_mutex.hh:51
void BKE_fluid_flow_type_set(struct Object *object, struct FluidFlowSettings *settings, eFluidFlow_Type type)
Definition fluid.cc:4587
static void fluid_modifier_freeDomain(FluidModifierData *fmd)
Definition fluid.cc:4656
ListBaseT< EffectorCache > * BKE_effectors_create(struct Depsgraph *depsgraph, struct Object *ob_src, struct ParticleSystem *psys_src, struct EffectorWeights *weights, bool use_rotation)
Definition effect.cc:316
bool manta_needs_realloc(struct MANTA *fluid, struct FluidModifierData *fmd)
@ FLUID_DOMAIN_GUIDE_SRC_DOMAIN
MINLINE float len_squared_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
float * manta_smoke_get_density_in(struct MANTA *smoke)
float * manta_get_phiguide_in(struct MANTA *fluid)
float * manta_get_phiobs_in(struct MANTA *fluid)
MINLINE void clamp_v3(float vec[3], float min, float max)
float * manta_smoke_get_emission_in(struct MANTA *smoke)
struct PointCache * BKE_ptcache_add(ListBaseT< PointCache > *ptcaches)
bool manta_smoke_ensure_fire(struct MANTA *smoke, struct FluidModifierData *fmd)
void BKE_fluid_reallocate_copy_fluid(struct FluidDomainSettings *fds, int o_res[3], int n_res[3], const int o_min[3], const int n_min[3], const int o_max[3], int o_shift[3], int n_shift[3])
struct PointCache * BKE_ptcache_copy_list(ListBaseT< PointCache > *ptcaches_new, const ListBaseT< PointCache > *ptcaches_old, int flag)
float * manta_get_velocity_x(struct MANTA *fluid)
float * manta_noise_get_react(struct MANTA *smoke)
bool manta_has_guiding(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr, bool domain)
MINLINE void copy_v4_v4(float r[4], const float a[4])
bool manta_read_mesh(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr)
@ FLUID_FLOW_SOURCE_PARTICLES
@ FLUID_FLOW_SOURCE_MESH
float * manta_noise_get_texture_v(struct MANTA *smoke)
void BKE_mesh_texspace_calc(Mesh *mesh)
float * manta_noise_get_texture_w(struct MANTA *smoke)
void copy_m4_m4(float m1[4][4], const float m2[4][4])
float * manta_smoke_get_fuel_in(struct MANTA *smoke)
VecBase< float, 3 > float3
void interp_v3_v3v3v3(float p[3], const float v1[3], const float v2[3], const float v3[3], const float w[3])
float * manta_get_velocity_y(struct MANTA *fluid)
@ FLUID_FLOW_USE_PLANE_INIT
@ FLUID_FLOW_INITVELOCITY
@ FLUID_FLOW_TEXTUREEMIT
@ FLUID_FLOW_NEEDS_UPDATE
@ FLUID_FLOW_USE_PART_SIZE
@ FLUID_FLOW_USE_INFLOW
bool manta_liquid_export_script(struct MANTA *liquid, struct FluidModifierData *fmd)
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
float * manta_get_in_velocity_z(struct MANTA *fluid)
float * manta_smoke_get_color_b(struct MANTA *smoke)
static void fluid_modifier_freeFlow(FluidModifierData *fmd)
Definition fluid.cc:4685
const char * BKE_modifier_path_relbase_from_global(Object *ob)
pthread_rwlock_t ThreadRWMutex
struct EffectorWeights * BKE_effector_add_weights(struct Collection *collection)
Definition effect.cc:63
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void BKE_fluid_particle_system_create(struct Main *bmain, struct Object *ob, const char *pset_name, const char *parts_name, const char *psys_name, eParticleType psys_type)
bool manta_read_guiding(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr, bool sourceDomain)
float DEG_get_ctime(const Depsgraph *graph)
void BKE_modifier_path_init(char *path, int path_maxncpy, const char *name)
MINLINE void zero_v3_int(int r[3])
bool manta_bake_noise(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr)
bool manta_write_noise(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr)
void BKE_fluid_modifier_create_type_data(struct FluidModifierData *fmd)
Definition fluid.cc:4774
ListBaseT< Base > * BKE_view_layer_object_bases_get(ViewLayer *view_layer)
void BKE_collision_objects_free(struct Object **objects)
bool manta_smoke_has_heat(struct MANTA *smoke)
int BKE_fluid_get_data_flags(struct FluidDomainSettings *fds)
@ FLUID_EFFECTOR_TYPE_COLLISION
@ FLUID_EFFECTOR_TYPE_GUIDE
bool manta_smoke_export_script(struct MANTA *smoke, struct FluidModifierData *fmd)
void psys_sim_data_free(struct ParticleSimulationData *sim)
Definition particle.cc:662
float * manta_smoke_get_color_g(struct MANTA *smoke)
void BKE_effectors_free(ListBaseT< EffectorCache > *lb)
Definition effect.cc:371
float * manta_noise_get_texture_u(struct MANTA *smoke)
@ FLUID_DOMAIN_TYPE_LIQUID
@ FLUID_DOMAIN_TYPE_GAS
bool manta_read_data(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr, bool resumable)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
@ FLUID_FLOW_TEXTURE_MAP_AUTO
float * manta_noise_get_texture_v2(struct MANTA *smoke)
bool manta_write_data(struct MANTA *fluid, struct FluidModifierData *fmd, int framenr)
@ FLUID_DOMAIN_PARTICLE_TRACER
@ FLUID_DOMAIN_PARTICLE_FOAM
@ FLUID_DOMAIN_PARTICLE_SPRAY
@ FLUID_DOMAIN_PARTICLE_BUBBLE
#define floorf
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
const NodeType * type
Definition graph/node.h:179
ustring name
Definition graph/node.h:178
eFluidDomain_FileFormat cache_noise_format
eFluidDomain_FileFormat cache_mesh_format
eFluidDomain_CacheFlag cache_flag
eFluidDomain_BorderFlags border_collisions
struct ListBaseT< PointCache > ptcaches[2]
struct EffectorWeights * effector_weights
FLUID_DisplayInterpolationMethod interp_method
eFluidDomain_AxisSliceMethod axis_slice_method
eFluidDomain_VectorDrawType vector_draw_type
struct Collection * effector_group
eFluidDomain_OpenVDBCompression openvdb_compression
eFluidDomain_CobaField coba_field
eFluidDomain_SimMethod simulation_method
struct PointCache * point_cache[2]
eFluidDomain_OpenVDBDepth openvdb_data_depth
struct Collection * fluid_group
eFluidDomain_FileFormat cache_data_format
eFluidDomain_FileFormat cache_particle_format
eFluidDomain_HighresSampling highres_sampling
FLUID_DisplayVectorField vector_field
eFluidDomain_ParticleTypes particle_type
eFluidDomain_GridlineColorField gridlines_color_field
eFluidDomain_ActiveFields active_fields
eFluidDomain_SliceAxis slice_axis
eFluidDomain_CacheType cache_type
eFluidDomain_VectorDrawMAC vector_draw_mac_components
eFluidDomain_SndParticleBoundary sndparticle_boundary
eFluidDomain_GridlineCellFilter gridlines_cell_filter
eFluidDomain_GuideSource guide_source
struct Collection * force_group
struct FluidModifierData * fmd
eFluidDomain_SndParticleCombinedExport sndparticle_combined_export
eFluidDomain_MeshGenerator mesh_generator
struct FluidModifierData * fmd
eFluidEffector_GuideMode guide_mode
struct ParticleSystem * psys
eFluidFlow_TextureType texture_type
eFluidFlow_Behavior behavior
struct FluidModifierData * fmd
struct FluidEffectorSettings * effector
struct FluidFlowSettings * flow
struct FluidDomainSettings * domain
struct PointCache * cache
eParticlePhysicsType phystype