Raymarcher Technical & APi Documentation
RM Volume Depth Sampler
Namespace: Raymarcher.Toolkit
The Volume Depth Sampler is a regular class that allows you to raytrace a 3D render texture and read voxel data from 3D render texture.
It needs proper initialization with a target 3D render texture and raytrace data (iterations and threshold).
Remember to call 'Dispose' to release sensitive resources.



The Depth Sampler enables you to sample and read pixel data from a target RT3D. Two methods can be called: SampleVolumeDepth and SampleVolumePixel.

- SampleVolumeDepth returns a boolean parameter indicating whether the ray hit the raytraced voxel and outputs the world hit position of the voxel.
- SampleVolumePixel outputs a vec3 with the actual hit voxel value (red channel), material index (green channel) and an extra channel value (blue channel).

These methods can be utilized to precisely sample the target volume box with tex3D data and place objects in the scene.
Properties & Fields
public bool IsInitialized { get; }
public RayTraceData CurrentRaytraceData { get; }

public RenderTexture TargetWorkingRT3D { get; }

public int TargetVolumeResolution { get; }
public CommonVolumeResolution TargetCommonVolumeResolution { get; }
Methods
// Initialize the depth sampler with an existing 3D render texture and raytrace data (count of iterations and pixel threshold)
public RMVolumeDepthSampler(RenderTexture targetRT3D, RayTraceData raytraceData)
// Or re-initialize an existing instance
public void Initialize(RenderTexture targetRT3D, RayTraceData raytraceData)

// Update the depth sampler instance with a new 3D render texture
public void Update(RenderTexture targetRT3D, RayTraceData raytraceData)

// Dispose the current depth sampler instance
public void Dispose()

// Sample the current 3D render texture, returns true if hit; outputs the hit world position
public bool SampleVolumeDepth(out Vector3 hitWorldSpacePosition, Vector3 rayOriginWorldSpace, Vector3 rayDirectionNormalized, RMSdf_VolumeBoxBase targetVolume, float rayLengthWorldSpace = 1, bool moduloWrap = false)
// Sample the current 3D render texture in a volume-space coordinates (0-volume resolution)
public bool SampleVolumeDepthNative(out Vector3 hitVolumeSpacePosition, Vector3 rayOriginVolumeSpace, Vector3 rayEndVolumeSpace)

// Sample a specific voxel value in the current 3D render texture on a world-space coordinate
public void SampleVolumePixel(out Vector3 sampledPixelResult, Vector3 pointWorldSpace, RMSdf_VolumeBoxBase targetVolume, bool moduloWrap = false)
// Sample a specific voxel value in the current 3D render texture on a volume-space coordinate (0-volume resolution)
public void SampleVolumePixelNative(out Vector3 sampledPixelResult, Vector3 pointVolumeSpace)

Practical Use
You can utilize the Volume Depth Sampler in your own C# script, dispatching all the parameters manually, or you can leverage the example content provided by Raymarcher.
In the example content, there's a script named 'RMSample_VolumeBrushLocator' that already encompasses all the functionality for placing an object on the target volume.

This script works closely with the 'RMSample_VolumeVoxelPainterFPS' script, which automatically creates a Volume Voxel Painter.
You can either use this script or create a custom script that initializes the Volume Voxel Painter and sets up the Volume Brush Locator.

The Volume Brush Locator essentially positions a 'brush' object on the target volume box and is primarily used as a 'brush' in voxel painting.