Files
test2/source/blender/blenkernel/intern/volume_grid_fields.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

79 lines
2.6 KiB
C++
Raw Normal View History

Geometry Nodes: initial support for volume grids in function nodes This patch implements basic support for evaluating function nodes on volume grids. Conceptually, a function node always creates a new grid for the output, though the output is often a modified version of the input. The topology of the output grid is a union of all the input grids. All input grids have to have the same transform. Otherwise one has to use resampling to make grids compatible. Non-grid inputs are allowed to be single values or fields. The fields are evaluated in a voxel/tile context, so they compute a value per voxel or per tile. One optimization is missing that will probably be key in the future: the ability to merge multiple function nodes and execute them at the same time. Currently the entire function evaluation is started and finished for every function node that outputs a grid. This will add significant overhead in some situations. Implementing this optimization requires some more changes outside of the scope of this patch though. It's good to have something that works first. Note: Not all function nodes are supported yet, because we don't have grid types for all of them yet. Most notably, there are no color/float4 grids yet. Implementing those properly is not super straight forward and may require some more changes, because there isn't a 1-to-1 mapping between grid types and socket types (a float4 grid may correspond to a color or vector socket later on). Using grids with function nodes and fields can result in false positive warnings in the UI currently. That's a limitation of our current socket type inferencing and can be improved once we have better socket shape inferencing. Pull Request: https://projects.blender.org/blender/blender/pulls/125110
2025-05-19 18:30:58 +02:00
/* SPDX-FileCopyrightText: 2025 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#include "BKE_geometry_fields.hh"
#include "BKE_volume_grid_fields.hh"
#ifdef WITH_OPENVDB
# include <openvdb/math/Transform.h>
Geometry Nodes: initial support for volume grids in function nodes This patch implements basic support for evaluating function nodes on volume grids. Conceptually, a function node always creates a new grid for the output, though the output is often a modified version of the input. The topology of the output grid is a union of all the input grids. All input grids have to have the same transform. Otherwise one has to use resampling to make grids compatible. Non-grid inputs are allowed to be single values or fields. The fields are evaluated in a voxel/tile context, so they compute a value per voxel or per tile. One optimization is missing that will probably be key in the future: the ability to merge multiple function nodes and execute them at the same time. Currently the entire function evaluation is started and finished for every function node that outputs a grid. This will add significant overhead in some situations. Implementing this optimization requires some more changes outside of the scope of this patch though. It's good to have something that works first. Note: Not all function nodes are supported yet, because we don't have grid types for all of them yet. Most notably, there are no color/float4 grids yet. Implementing those properly is not super straight forward and may require some more changes, because there isn't a 1-to-1 mapping between grid types and socket types (a float4 grid may correspond to a color or vector socket later on). Using grids with function nodes and fields can result in false positive warnings in the UI currently. That's a limitation of our current socket type inferencing and can be improved once we have better socket shape inferencing. Pull Request: https://projects.blender.org/blender/blender/pulls/125110
2025-05-19 18:30:58 +02:00
namespace blender::bke {
VoxelFieldContext::VoxelFieldContext(const openvdb::math::Transform &transform,
const Span<openvdb::Coord> voxels)
: transform_(transform), voxels_(voxels)
{
}
GVArray VoxelFieldContext::get_varray_for_input(const fn::FieldInput &field_input,
const IndexMask & /*mask*/,
ResourceScope & /*scope*/) const
{
const bke::AttributeFieldInput *attribute_field_input =
dynamic_cast<const bke::AttributeFieldInput *>(&field_input);
if (!attribute_field_input) {
return {};
}
if (attribute_field_input->attribute_name() != "position") {
return {};
}
/* Support retrieving voxel positions. */
Array<float3> positions(voxels_.size());
threading::parallel_for(positions.index_range(), 1024, [&](const IndexRange range) {
for (const int64_t i : range) {
const openvdb::Coord &voxel = voxels_[i];
const openvdb::Vec3d position = transform_.indexToWorld(voxel);
positions[i] = float3(position.x(), position.y(), position.z());
}
});
return VArray<float3>::ForContainer(std::move(positions));
}
TilesFieldContext::TilesFieldContext(const openvdb::math::Transform &transform,
const Span<openvdb::CoordBBox> tiles)
: transform_(transform), tiles_(tiles)
{
}
GVArray TilesFieldContext::get_varray_for_input(const fn::FieldInput &field_input,
const IndexMask & /*mask*/,
ResourceScope & /*scope*/) const
{
const bke::AttributeFieldInput *attribute_field_input =
dynamic_cast<const bke::AttributeFieldInput *>(&field_input);
if (attribute_field_input == nullptr) {
return {};
}
if (attribute_field_input->attribute_name() != "position") {
return {};
}
/* Support retrieving tile positions. */
Array<float3> positions(tiles_.size());
threading::parallel_for(positions.index_range(), 1024, [&](const IndexRange range) {
for (const int64_t i : range) {
const openvdb::CoordBBox &tile = tiles_[i];
const openvdb::Vec3d position = transform_.indexToWorld(tile.getCenter());
positions[i] = float3(position.x(), position.y(), position.z());
}
});
return VArray<float3>::ForContainer(std::move(positions));
}
} // namespace blender::bke
#endif