voxelizer is a tool that converts 3D models in Wavefront OBJ format to a 3D texture including transparency. The output texture is in raw format (4 8-bit channels, 0xAABBGGRR) and corresponds to the AABB of the scene. Each 3D texel of the output contains the average color of objects intersecting it and has the alpha channel proportional to the volume of intersection. Separate objects are treated individually and are simply added together to the output (overflow is clamped at 1.0). The default object color is opaque white. You can change it by setting an appropriate material in the MTL file. See below for examples.
For each 3D texel of the output texture, it samples quasi-random locations withing that texel, checks if they are contained in the object by shooting a ray in the positive X direction and counting the number of intersections, and averages over the samples. Sampling is done using the R3 sequence. Then, it performs a flood-fill on untouched transparent black pixels to prevent black color leaking when filtering the texture close to object boundaries.
It uses a simple ad-hoc implementation of an R-tree to speed up intersection tests, and uses all available cores when sampling. It takes 152 seconds to convert a Stanford bunny model with 5002 triangles into a 256x256x256 texture using 256 samples per texel on a 16-core i9-9900K 3.60GHz. Not blazing fast, but gets the job done.
The only requirements are C++20, GLM (included here a Git submodule) and CMake.
Call it like this:
voxelizer <input-obj-file> <x-resolution> <y-resolution> <z-resolution> <samples-per-texel> <output-file>
It depends on the scene and texture resolution, but I've found 64 to be more-or-less enough, with 128 being almost perfect and 256 being indistinguishable from 128.
Model | Blender view | Volumetric view |
---|---|---|
Stanford bunny | ||
Example scene |