Export volumes from object modifier (quick effect) #62

Merged
Bogdan Nagirniak merged 28 commits from BLEN-448 into hydra-render 2023-07-20 23:49:25 +02:00
3 changed files with 33 additions and 1 deletions
Showing only changes of commit 1a55b48367 - Show all commits

View File

@ -344,6 +344,7 @@ float Object::compute_volume_step_size() const
/* Step size is transformed from voxel to world space. */
Transform voxel_tfm = tfm;
if (metadata.use_transform_3d) {
voxel_tfm = tfm * transform_inverse(metadata.transform_3d);
}
voxel_step_size = reduce_min(fabs(transform_direction(&voxel_tfm, size)));

View File

@ -7,12 +7,16 @@
#include <pxr/usd/usdHydra/tokens.h>
#include <pxr/usd/usdVol/tokens.h>
#include <pxr/usdImaging/usdVolImaging/tokens.h>
#include <pxr/base/gf/matrix3d.h>
#include <pxr/base/gf/rotation.h>
#include "BKE_material.h"
#include "BKE_object.h"
#include "BKE_modifier.h"
#include "BKE_volume.h"
#include "BLI_index_range.hh"
#include "BLI_path_util.h"
#include "BLI_math_rotation.h"
#include "DNA_fluid_types.h"
#include "DNA_scene_types.h"
#include "DNA_volume_types.h"
@ -50,13 +54,14 @@ bool VolumeData::is_volume_modifier(Object *object)
void VolumeData::init()
{
ID_LOG(1, "");
write_transform();
if (is_volume_modifier((Object *)this->id)) {
init_from_modifier();
}
else {
init_from_volume();
}
write_transform();
write_materials();
}
@ -98,6 +103,7 @@ void VolumeData::update()
}
if (id->recalc & ID_RECALC_TRANSFORM) {
write_transform();
assign_volume_transform();
bits |= pxr::HdChangeTracker::DirtyTransform;
}
@ -200,6 +206,9 @@ void VolumeData::init_from_modifier()
object, eModifierType_Fluid);
filepath_ = get_cached_file_path(volume_modifier->domain->cache_directory,
scene_delegate_->scene->r.cfra);
assign_volume_transform();
for (auto &grid_name : supported_grids) {
field_descriptors_.emplace_back(grid_name,
pxr::UsdVolImagingTokens->openvdbAsset,
@ -232,4 +241,25 @@ void VolumeData::init_from_volume()
BKE_volume_unload(volume);
}
void VolumeData::assign_volume_transform() {
Object *object = (Object *)this->id;
auto mod = pxr::GfMatrix4d(1.0f).SetScale(0.5) *
pxr::GfMatrix4d(1.0f).SetTranslate(pxr::GfVec3d(0.5));
auto det = mod.GetDeterminant();
auto inversed = mod.GetInverse(&det);
float scale[3] = {0.5f, 0.5f, 0.5f};
if (object->scale[0] != 0.0f) {
scale[0] = 0.5f / object->scale[0];
}
if (object->scale[1] != 0.0f) {
scale[1] = 0.5f / object->scale[1];
}
if (object->scale[2] != 0.0f) {
scale[2] = 0.5f / object->scale[2];
}
auto scale_matrix = pxr::GfMatrix4d(1.0f).SetScale(pxr::GfVec3d(scale[0], scale[1], scale[2]));
transform = scale_matrix * inversed * transform;
}
} // namespace blender::render::hydra

View File

@ -32,6 +32,7 @@ class VolumeData : public ObjectData {
std::string get_cached_file_path(std::string directory, int frame);
void init_from_modifier();
void init_from_volume();
void assign_volume_transform();
std::string filepath_;
pxr::HdVolumeFieldDescriptorVector field_descriptors_;