Volumetric Mesh
TGorillaVolumetricMesh renders a 3D scalar field (a volume) with a GPU ray-marching algorithm. Instead of a surface made of triangles, every pixel casts a ray through a 3D texture and accumulates the voxel values along the way. This is the technique used for medical imaging (CT / MRI / MRT scans), scientific data, and — with the Volumetric Noise subclass — for fog, clouds and smoke.
Table of Contents
- Overview
- Required Setup: Front / Back Face Passes
- Loading Volume Data
- Volume Size & Data Type
- Rendering Modes: Solid Surface vs. Translucent Medium
- Lighting & Iso-Surfaces
- Transfer Functions (Gamut)
- Quality & Performance
- Clipping & Ray Control
- Flying Inside the Volume
- Best Practices
Overview
The volume is stored as a 3D texture (width × height × depth voxels). At render time the mesh rasterizes a surrounding box (or sphere / cylinder) and, for every fragment, marches a ray from the entry point to the exit point, sampling the 3D texture at fixed steps and compositing the samples.
| Feature | Description |
|---|---|
| Unit | Gorilla.Volumetric.Mesh |
| Class | TGorillaVolumetricMesh (inherits TGorillaSingletonMesh) |
| Data | 3D texture, 8/16-bit or float, single- or multi-channel |
| Technique | GPU ray marching (front-to-back compositing) |
| Typical use | CT/MRI/MRT scans, scientific fields, fog / clouds / smoke |
uses
Gorilla.Volumetric.Mesh,
Gorilla.Controller.Passes.Face;
var Vol := TGorillaVolumetricMesh.Create(GorillaViewport1);
Vol.Parent := GorillaViewport1;
Vol.SetSize(1, 1, 1);
Required Setup: Front / Back Face Passes
Ray marching needs the ray entry and exit positions per pixel. The volumetric mesh obtains them from two pre-render passes that render the surrounding box's front and back faces into position textures. A volumetric mesh will not render without them.
uses Gorilla.Controller.Passes.Face;
// Front face pass = ray entry points
FFrontFace := TGorillaRenderPassFace.Create(GorillaViewport1, 'FrontFace');
FFrontFace.FaceKind := TFaceKind.FrontFace;
FFrontFace.Viewport := GorillaViewport1;
FFrontFace.Enabled := True;
// Back face pass = ray exit points
FBackFace := TGorillaRenderPassFace.Create(GorillaViewport1, 'BackFace');
FBackFace.FaceKind := TFaceKind.BackFace;
FBackFace.Viewport := GorillaViewport1;
FBackFace.Enabled := True;
// Assign them to the volume and allow the passes to render it
Vol.FrontFaceRenderPass := FFrontFace;
Vol.BackFaceRenderPass := FBackFace;
FFrontFace.AllowControl(Vol);
FBackFace.AllowControl(Vol);
You can share the same two passes across several volumes in the scene.
Loading Volume Data
The volume texture is filled from raw binary data. Wrap size/type changes in BeginUpdate / EndUpdate so the texture is only rebuilt once.
From a RAW file
Vol.BeginUpdate;
try
Vol.Sizes := TGorillaVolumetricMeshSize.vms256x256x256;
Vol.DataType := TGorillaVolumetricMeshDataType.vmdtUInt8;
finally
Vol.EndUpdate;
end;
Vol.LoadFromRawFile('teapot.raw', 256, 256, 178,
TGorillaVolumetricMeshDataType.vmdtUInt8);
From an NRRD file
NRRD headers carry size, spacing and data type, so no extra parameters are needed:
Vol.LoadFromNRRDFile('skull.nrrd');
From image slices
Assemble a volume from a numbered sequence of 2D images (e.g. slice_000.jpg … slice_255.jpg):
Vol.LoadFromImageSlices('scans\', 'slice_%s.jpg',
0, 255, // from / to index
3, '0', // pad the index to 3 digits with '0'
256, 256, 256 // target texture width / height / depth
);
From memory
Push your own buffer directly. The data layout must match Sizes and DataType:
Vol.SetRawData(MyBufferPtr, MyBufferSizeInBytes);
Volume Size & Data Type
Sizes selects a fixed cubic resolution; use vmsCustom together with CustomWidth/CustomHeight/CustomDepth (or CustomSize) for non-cubic volumes.
TGorillaVolumetricMeshSize |
Resolution |
|---|---|
vms4x4x4 … vms1024x1024x1024 |
4³ … 1024³ |
vmsCustom |
Use CustomWidth / CustomHeight / CustomDepth |
DataType describes one voxel:
TGorillaVolumetricMeshDataType |
Bytes / voxel | Notes |
|---|---|---|
vmdtUInt8 |
1 | 8-bit unsigned (most CT/MRI raw data) |
vmdtUInt16 |
2 | 16-bit unsigned |
vmdtHalfFloat |
2 | 16-bit float, filterable |
vmdtFloat |
4 | 32-bit float |
vmdtRGBUInt8 / vmdtRGBAUInt8 |
3 / 4 | color volumes |
vmdtRGBFloat / vmdtRGBAFloat |
12 / 16 | float color volumes |
Tip: enable
LinearFilteringfor smooth trilinear interpolation between voxels. With it off, integer textures are point-sampled and every voxel shows as a hard cube.
Rendering Modes: Solid Surface vs. Translucent Medium
The mesh composites samples along each ray. Two blend models are available, and choosing the right one is the single most important decision:
Linear blend — solid / opaque objects (default)
The default front-to-back blend saturates: dense data becomes opaque and forms a surface. Combined with lighting this gives a solid, shaded object (e.g. a CT scan of a skull or teapot).
Vol.SetLinearBlend; // this is the default; call it only to switch back
Vol.UseLighting := True;
Emission-absorption blend — fog / clouds / smoke / translucent media
For a medium you look through, use the physically based Beer-Lambert blend. Its per-step opacity is exponential in the optical density, so it accumulates smoothly and never hard-saturates into a shell (which would show up as concentric "onion-ring" step banding from inside a dense body).
Vol.SetEmissionAbsorptionBlend(1.0); // 1.0 = neutral, resolution-independent
AAbsorptionScale controls opacity and is internally normalized by the volume resolution, so 1.0 looks comparable on a 64³ and a 256³ volume. Raise it for a denser medium, lower it for a thinner one.
Rule of thumb:
- Solid, lit object → linear blend (default).
- Fog / cloud / smoke / any see-through medium →
SetEmissionAbsorptionBlend.Using the emission-absorption blend on a solid object makes it translucent (you lose the surface); using the linear blend on a dense medium produces hard saturation terraces.
Lighting & Iso-Surfaces
With UseLighting := True the ray marcher detects the first voxel that crosses IsoSurfaceLimit, computes a surface normal from the volume gradient, and shades it with the selected ShadingModel. The surface hit is refined to sub-sample precision automatically, so lit iso-surfaces stay smooth instead of terracing at the discrete ray steps.
Vol.UseLighting := True;
Vol.IsoSurfaceLimit := 0.15; // threshold that defines "the surface"
Vol.ShadingModel := TGorillaShadingModel.smBlinnPhong;
Vol.ShadingIntensity := 1.0;
Vol.Brightness := 1.0;
Iso-surface lighting is a surface model — it is intended for solid objects with the linear blend, not for fog/clouds (a homogeneous medium has no iso-surface, so the normal falls back to the cube face). See Volumetric Noise for the correct fog/cloud model.
Transfer Functions (Gamut)
A gamut texture maps scalar voxel values to colors — the classic transfer function for medical visualization. Assign a 1D gradient bitmap and choose how it is applied:
Vol.Gamut.LoadFromFile('gamut.png');
Vol.GamutMode := TGorillaVolumetricGamutMode.GamutBySum;
Vol.GamutFactor := 0.75;
Vol.GamutIntensity := 1.0; // RGB contribution
Vol.GamutAlphaIntensity := 1.0; // alpha contribution
TGorillaVolumetricGamutMode |
Meaning |
|---|---|
GamutNone |
disabled |
GamutByValue |
color by each sampled value |
GamutByValueMultiply |
as above, multiplied with the original value |
GamutBySum |
color the final ray-traced value |
GamutBySumMultiply |
as above, multiplied with the original value |
Quality & Performance
| Property | Effect |
|---|---|
Details |
Ray-march step density (steps ≈ Slices × Details). Higher = finer, smoother, slower. |
Dithering |
Per-pixel ray-start jitter that breaks step banding into fine noise (default True). |
RayStop |
Alpha value at which a ray terminates early (default 0.95). Raise toward 0.99 for fewer hard terminations. |
LinearFiltering |
Trilinear filtering of the 3D texture (smooth vs. blocky). |
MipMaps |
Mip-map minification (can introduce "lines" with linear filtering). |
TextureInterpolation |
Sampling of the front/back-face position textures. |
Detailsis the main quality/performance dial.1–2is a good range for medical data; higher only when you zoom in close.
Clipping & Ray Control
// Slice the volume with a clipping plane
Vol.ClippingPlane := TPlaneF.Create(0, 0, -1, -0.2);
// Skip a front offset (X) and shorten the back (Y) of every ray
Vol.RayLimits := Point(2, 4);
IntensityMode / AlphaIntensityMode (TRayCastDistanceMethod) modulate the sampled value or alpha along the ray — for example to fade far samples (rcdInvLinear) or focus on a band (rcdFocused with IntensityFocus / IntensityExp). RayIntensityTexture / RayAlphaIntensityTexture drive these from a 1D texture when the mode is rcdTexture.
Flying Inside the Volume
To move the camera into the volume (essential for fog / clouds), enable two-sided rendering:
Vol.RenderInside := True;
The ray marcher then restarts the ray at the camera position while it is inside the box, and the medium correctly composites over the scene. This costs performance (both faces are rasterized), so leave it off for solid objects you only look at from outside.
Best Practices
- Always create and assign the front/back face passes — nothing renders without them.
- Wrap
Sizes/DataTypechanges inBeginUpdate/EndUpdate. - Solid, lit object → linear blend (default) +
UseLighting. Translucent medium →SetEmissionAbsorptionBlend. - Enable
LinearFilteringfor smooth interpolation; keepDitheringon to hide step banding. - Use
Detailsto trade quality vs. performance; start low and raise only when needed. - For procedural fog, clouds and smoke, prefer the ready-made Volumetric Noise component.
See Also
- Volumetric Noise — procedural clouds / smoke / fog built on this mesh
- Gorilla.Volumetric.Mesh Reference — full class reference
- Render Pass Controller — the render pass system used for the face passes