Blender V5.3
blender/volume.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "scene/volume.h"
6#include "scene/image.h"
7#include "scene/image_vdb.h"
8#include "scene/object.h"
9
10#include "blender/sync.h"
11#include "blender/util.h"
12
13#include "util/log.h"
14#include "util/vector.h"
15
16#include "BLI_bounds.hh"
17
18#include "BKE_volume.hh"
19#include "BKE_volume_grid.hh"
20
21#include "DNA_cachefile_types.h"
22#include "DNA_volume_types.h"
23
24#include "RNA_prototypes.hh"
25
27
28/* TODO: verify this is not loading unnecessary attributes. */
30 public:
32 : VDBImageLoader(Attribute::standard_name(attribute), clipping),
33 b_domain(object_fluid_gas_domain_find(b_ob)),
35 {
36 domain_rna_ptr = RNA_pointer_create_discrete(
37 &b_ob.id, blender::RNA_FluidDomainSettings, b_domain);
38 mesh_texture_space(
39 *blender::id_cast<const blender::Mesh *>(b_ob.data), texspace_loc, texspace_size);
40 }
41
42 void load_grid() override
43 {
44 if (!b_domain) {
45 return;
46 }
47
48 int channels;
51 {
52 channels = 1;
53 }
54 else if (attribute == ATTR_STD_VOLUME_COLOR) {
55 channels = 4;
56 }
58 channels = 3;
59 }
60 else {
61 return;
62 }
63
64 const int3 resolution = make_int3(b_domain->res[0], b_domain->res[1], b_domain->res[2]);
65 int amplify = (b_domain->flags & blender::FLUID_DOMAIN_USE_NOISE) != 0 ?
67 1;
68
69 /* Velocity and heat data is always low-resolution. */
71 amplify = 1;
72 }
73
74 const size_t width = resolution.x * amplify;
75 const size_t height = resolution.y * amplify;
76 const size_t depth = resolution.z * amplify;
77
78 /* Create a matrix to transform from object space to mesh texture space.
79 * This does not work with deformations but that can probably only be done
80 * well with a volume grid mapping of coordinates.
81 *
82 * Mantaflow stores values at cell centers, while OpenVDB convention is
83 * at corners. Offset by half a voxel to compensate for that. */
84 const float3 half_voxel = make_float3(0.5f / width, 0.5f / height, 0.5f / depth);
85 Transform transform_3d = transform_translate(-texspace_loc - half_voxel) *
87
88 vector<float> voxels;
89 if (!get_voxels(width, height, depth, channels, voxels)) {
90 return;
91 }
92
93 grid_from_dense_voxels(width, height, depth, channels, voxels.data(), transform_3d);
94 }
95
96 bool get_voxels(const size_t width,
97 const size_t height,
98 const size_t depth,
99 const int channels,
100 vector<float> &voxels)
101 {
102 if (!b_domain) {
103 return false;
104 }
105
106#ifdef WITH_FLUID
107 voxels.resize(width * height * depth * channels);
108
110 blender::PropertyRNA *prop = RNA_struct_find_property(&domain_rna_ptr, "density_grid");
111 if (RNA_property_array_length(&domain_rna_ptr, prop) == voxels.size()) {
112 RNA_property_float_get_array(&domain_rna_ptr, prop, voxels.data());
113 return true;
114 }
115 }
116 else if (attribute == ATTR_STD_VOLUME_FLAME) {
117 blender::PropertyRNA *prop = RNA_struct_find_property(&domain_rna_ptr, "flame_grid");
118 /* this is in range 0..1, and interpreted by the OpenGL smoke viewer
119 * as 1500..3000 K with the first part faded to zero density */
120 if (RNA_property_array_length(&domain_rna_ptr, prop) == voxels.size()) {
121 RNA_property_float_get_array(&domain_rna_ptr, prop, voxels.data());
122 return true;
123 }
124 }
125 else if (attribute == ATTR_STD_VOLUME_COLOR) {
126 blender::PropertyRNA *prop = RNA_struct_find_property(&domain_rna_ptr, "color_grid");
127 /* the RGB is "premultiplied" by density for better interpolation results */
128 if (RNA_property_array_length(&domain_rna_ptr, prop) == voxels.size()) {
129 RNA_property_float_get_array(&domain_rna_ptr, prop, voxels.data());
130 return true;
131 }
132 }
134 blender::PropertyRNA *prop = RNA_struct_find_property(&domain_rna_ptr, "velocity_grid");
135 if (RNA_property_array_length(&domain_rna_ptr, prop) == voxels.size()) {
136 RNA_property_float_get_array(&domain_rna_ptr, prop, voxels.data());
137 return true;
138 }
139 }
140 else if (attribute == ATTR_STD_VOLUME_HEAT) {
141 blender::PropertyRNA *prop = RNA_struct_find_property(&domain_rna_ptr, "heat_grid");
142 if (RNA_property_array_length(&domain_rna_ptr, prop) == voxels.size()) {
143 RNA_property_float_get_array(&domain_rna_ptr, prop, voxels.data());
144 return true;
145 }
146 }
148 blender::PropertyRNA *prop = RNA_struct_find_property(&domain_rna_ptr, "temperature_grid");
149 if (RNA_property_array_length(&domain_rna_ptr, prop) == voxels.size()) {
150 RNA_property_float_get_array(&domain_rna_ptr, prop, voxels.data());
151 return true;
152 }
153 }
154 else {
155 LOG_ERROR << "Unknown volume attribute " << Attribute::standard_name(attribute)
156 << "skipping ";
157 voxels[0] = 0.0f;
158 return false;
159 }
160 LOG_ERROR << "Unexpected smoke volume resolution, skipping";
161#else
162 (void)voxels;
163 (void)width;
164 (void)height;
165 (void)depth;
166 (void)channels;
167#endif
168 return false;
169 }
170
171 string name() const override
172 {
174 }
175
176 bool equals(const ImageLoader &other) const override
177 {
178 const BlenderSmokeLoader &other_loader = (const BlenderSmokeLoader &)other;
179 return b_domain == other_loader.b_domain && attribute == other_loader.attribute;
180 }
181
186};
187
189 Scene *scene,
190 BObjectInfo &b_ob_info,
191 Volume *volume,
192 const float frame)
193{
194 if (!b_ob_info.is_real_object_data()) {
195 return;
196 }
197 blender::FluidDomainSettings *b_domain = object_fluid_gas_domain_find(*b_ob_info.real_object);
198 if (!b_domain) {
199 return;
200 }
201
202 float velocity_scale = b_domain->velocity_scale;
203 /* Motion blur attribute is relative to seconds, we need it relative to frames. */
204 const bool need_motion = object_need_motion_attribute(b_ob_info, scene);
205 const float motion_scale = (need_motion) ? scene->motion_shutter_time() /
206 (b_scene.r.frs_sec / b_scene.r.frs_sec_base) :
207 0.0f;
208
209 velocity_scale *= motion_scale;
210
211 volume->set_velocity_scale(velocity_scale);
212
213 const AttributeStandard attributes[] = {ATTR_STD_VOLUME_DENSITY,
220
221 const Interval<int> frame_interval = {b_domain->cache_frame_start, b_domain->cache_frame_end};
222
223 for (int i = 0; attributes[i] != ATTR_STD_NONE; i++) {
224 const AttributeStandard std = attributes[i];
225 if (!volume->need_attribute(scene, std)) {
226 continue;
227 }
228
229 const float clipping = b_domain->clipping;
230
231 Attribute *attr = volume->attributes.add(std);
232
233 if (!frame_interval.contains(frame)) {
234 attr->data_voxel_for_write().clear();
235 continue;
236 }
237
238 unique_ptr<ImageLoader> loader = make_unique<BlenderSmokeLoader>(
239 *b_ob_info.real_object, std, clipping);
240 ImageParams params;
241 params.frame = frame;
242
243 attr->data_voxel_for_write() = scene->image_manager->add_image(std::move(loader), params);
244 }
245
246 /* Create a matrix to transform from object space to normalized texture space [0, 1]. */
247 if (volume->need_attribute(scene, ATTR_STD_GENERATED_TRANSFORM)) {
248 const blender::Mesh &b_mesh = *blender::id_cast<const blender::Mesh *>(b_ob_info.object_data);
249
250 float3 loc;
251 float3 size;
252 mesh_texture_space(b_mesh, loc, size);
253
255 Transform *tfm = attr->data_for_write<Transform>();
257 }
258}
259
261 public:
264 const string &grid_name,
266 const float clipping)
268 {
269 BKE_volume_load(&b_volume, &b_data);
270
271#ifdef WITH_OPENVDB
272
273 for (const int grid_index : blender::IndexRange(BKE_volume_num_grids(&b_volume))) {
274 const blender::bke::VolumeGridData &b_volume_grid = *BKE_volume_grid_get(&b_volume,
275 grid_index);
276 if (b_volume_grid.name() == grid_name) {
277 b_volume_grid.add_user();
278 volume_grid = blender::bke::GVolumeGrid{&b_volume_grid};
279 grid = volume_grid->grid_ptr(tree_access_token);
280 break;
281 }
282 }
283#endif
284#ifdef WITH_NANOVDB
285 switch (precision_) {
287 precision = 32;
288 break;
290 precision = 16;
291 break;
292 default:
294 precision = 0;
295 break;
296 }
297#else
298 (void)precision_;
299#endif
300 }
301
303#ifdef WITH_OPENVDB
304 /* Store tree user so that the OPENVDB grid that is shared with Blender is not unloaded. */
305 blender::bke::GVolumeGrid volume_grid;
306 blender::bke::VolumeTreeAccessToken tree_access_token;
307#endif
308};
309
311 blender::Scene &b_scene,
312 BObjectInfo &b_ob_info,
313 Scene *scene,
314 Volume *volume)
315{
316 blender::Volume &b_volume = *blender::id_cast<blender::Volume *>(b_ob_info.object_data);
317 BKE_volume_load(&b_volume, &b_data);
318
319 blender::VolumeRender &b_render = b_volume.render;
320
321 volume->set_step_size(b_render.step_size);
322 volume->set_object_space((b_render.space == blender::VOLUME_SPACE_OBJECT));
323
324 float velocity_scale = b_volume.velocity_scale;
326 /* Motion blur attribute is relative to seconds, we need it relative to frames. */
327 const bool need_motion = object_need_motion_attribute(b_ob_info, scene);
328 const float motion_scale = (need_motion) ? scene->motion_shutter_time() /
329 (b_scene.r.frs_sec / b_scene.r.frs_sec_base) :
330 0.0f;
331
332 velocity_scale *= motion_scale;
333 }
334
335 volume->set_velocity_scale(velocity_scale);
336
337#ifdef WITH_OPENVDB
338 /* Find grid with matching name. */
339 for (const int grid_index : blender::IndexRange(BKE_volume_num_grids(&b_volume))) {
340 const blender::bke::VolumeGridData &b_grid = *BKE_volume_grid_get(&b_volume, grid_index);
341 const ustring name = ustring(b_grid.name());
343
344 if (std == ATTR_STD_NONE) {
345 /* Velocity grid with custom name. */
346 if (name == b_volume.velocity_grid) {
348 }
349 else if (name == b_volume.runtime->velocity_x_grid) {
351 }
352 else if (name == b_volume.runtime->velocity_y_grid) {
354 }
355 else if (name == b_volume.runtime->velocity_z_grid) {
357 }
358 }
359
360 const bool need_std = (std != ATTR_STD_NONE && volume->need_attribute(scene, std));
361 const bool need_named = volume->need_attribute(scene, name);
362
363 if (need_std || need_named) {
364 Attribute *attr;
365 if (need_std) {
366 /* Make grid available both with std and name. */
367 attr = volume->attributes.add(std, name);
368 }
369 else {
370 /* Make grid available by name with appropriate number of channels. */
371 const int channels = blender::bke::volume_grid::get_channels_num(b_grid.grid_type());
372 const TypeDesc type = (channels == 3) ? TypeVector : TypeFloat;
373 attr = volume->attributes.add(name, type, ATTR_ELEMENT_VOXEL);
374 }
375
376 unique_ptr<ImageLoader> loader = make_unique<BlenderVolumeLoader>(
377 b_data,
378 b_volume,
379 name.string(),
381 b_render.clipping);
382 ImageParams params;
383 params.frame = b_volume.runtime->frame;
384
385 attr->data_voxel_for_write() = scene->image_manager->add_image(
386 std::move(loader), params, false);
387 }
388 }
389#endif
390
391 /* Create a matrix to transform from object space to normalized texture space [0, 1]. */
392 if (volume->need_attribute(scene, ATTR_STD_GENERATED_TRANSFORM)) {
393 std::optional<const blender::Bounds<blender::float3>> bounds = BKE_volume_min_max(&b_volume);
394
395 if (bounds.has_value()) {
396 const blender::float3 size = bounds->size();
397 const float3 loc = make_float3(bounds->min[0], bounds->min[1], bounds->min[2]);
398 const float3 inv_size = safe_divide(one_float3(), make_float3(size[0], size[1], size[2]));
399
401 Transform *tfm = attr->data_for_write<Transform>();
402 *tfm = transform_scale(inv_size) * transform_translate(-loc);
403 }
404 }
405}
406
407void BlenderSync::sync_volume(BObjectInfo &b_ob_info, Volume *volume)
408{
409 volume->clear(true);
410
411 if (view_layer.use_volumes) {
412 if (b_ob_info.object_data->id_type() == blender::ID_VO) {
413 /* Volume object. Create only attributes, bounding mesh will then
414 * be automatically generated later. */
415 sync_volume_object(*b_data, *b_scene, b_ob_info, scene, volume);
416 }
417 else {
418 /* Smoke domain. */
419 sync_smoke_volume(*b_scene, scene, b_ob_info, volume, b_scene->r.cfra);
420 }
421 }
422
423 volume->merge_grids(scene);
424
425 /* Tag update. */
426 volume->tag_update(scene, true);
427}
428
Volume data-block.
static void sync_volume_object(blender::Main &b_data, blender::Scene &b_scene, BObjectInfo &b_ob_info, Scene *scene, Volume *volume)
static void sync_smoke_volume(blender::Scene &b_scene, Scene *scene, BObjectInfo &b_ob_info, Volume *volume, const float frame)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
Definition btDbvt.cpp:299
Attribute * add(ustring name, const TypeDesc type, AttributeElement element)
ImageHandle & data_voxel_for_write()
T * data_for_write(const int step=0)
static AttributeStandard name_volume_standard(ustring name)
static const char * standard_name(AttributeStandard std)
void load_grid() override
string name() const override
bool get_voxels(const size_t width, const size_t height, const size_t depth, const int channels, vector< float > &voxels)
blender::FluidDomainSettings * b_domain
blender::PointerRNA domain_rna_ptr
BlenderSmokeLoader(blender::Object &b_ob, AttributeStandard attribute, const float clipping)
bool equals(const ImageLoader &other) const override
AttributeStandard attribute
blender::Volume & b_volume
BlenderVolumeLoader(blender::Main &b_data, blender::Volume &b_volume, const string &grid_name, blender::VolumeRenderPrecision precision_, const float clipping)
void tag_update(Scene *scene, bool rebuild)
AttributeSet attributes
bool need_attribute(const Scene *scene, AttributeStandard std)
Definition scene.h:133
unique_ptr< ImageManager > image_manager
Definition scene.h:166
float motion_shutter_time()
Definition scene.cpp:447
string grid_name
Definition image_vdb.h:64
void grid_from_dense_voxels(const size_t width, const size_t height, const size_t depth, const int channels, const float *voxels, Transform transform_3d)
VDBImageLoader(const string &grid_name, const float clipping=0.001f)
Definition image_vdb.cpp:28
void merge_grids(const Scene *scene)
void clear(bool preserve_shaders=false) override
static void mesh_texture_space(const blender::Mesh &b_mesh, float3 &loc, float3 &size)
static bool object_need_motion_attribute(BObjectInfo &b_ob_info, Scene *scene)
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
ccl_device_forceinline int3 make_int3(const int x, const int y, const int z)
float safe_divide(float a, float b)
@ ATTR_ELEMENT_VOXEL
AttributeStandard
@ ATTR_STD_GENERATED_TRANSFORM
@ ATTR_STD_VOLUME_VELOCITY_Y
@ ATTR_STD_VOLUME_TEMPERATURE
@ ATTR_STD_NONE
@ ATTR_STD_VOLUME_VELOCITY_Z
@ ATTR_STD_VOLUME_DENSITY
@ ATTR_STD_VOLUME_FLAME
@ ATTR_STD_VOLUME_VELOCITY
@ ATTR_STD_VOLUME_COLOR
@ ATTR_STD_VOLUME_HEAT
@ ATTR_STD_VOLUME_VELOCITY_X
#define LOG_ERROR
Definition log.h:101
ccl_device_inline float3 one_float3()
Definition math_float3.h:26
int get_channels_num(VolumeGridType type)
@ FLUID_DOMAIN_USE_NOISE
@ VOLUME_PRECISION_FULL
@ VOLUME_PRECISION_VARIABLE
@ VOLUME_PRECISION_HALF
@ CACHEFILE_VELOCITY_UNIT_SECOND
VecBase< float, 3 > float3
bool is_real_object_data() const
blender::ID * object_data
blender::Object * real_object
ccl_device_inline_method bool contains(T value) const
Definition math_base.h:926
struct RenderData r
VolumeRenderPrecision precision
VolumeRenderSpace space
bke::VolumeRuntime * runtime
VolumeRender render
ccl_device_inline Transform transform_scale(const float3 s)
Definition transform.h:313
ccl_device_inline Transform transform_translate(const float3 t)
Definition transform.h:296