# Sample Volume Texture (Custom HLSL)

**URL:** <https://forums.unrealengine.com/t/sample-volume-texture-custom-hlsl/2651872>\
**Category:** C++\
**Tags:** Volumetrics, unreal-engine, Shaders, hlsl, question\
**Created:** [August 28, 2025, 6:41pm UTC](https://forums.unrealengine.com/t/sample-volume-texture-custom-hlsl/2651872 "2025-08-28T18:41:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scottimus06](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scottimus06/32/469167_2.png) [@Scottimus06](https://forums.unrealengine.com/u/Scottimus06)\
**Post date:** [August 28, 2025, 6:41pm UTC](https://forums.unrealengine.com/t/sample-volume-texture-custom-hlsl/2651872/1 "2025-08-28T18:41:51Z")

</div>

Howdy,

There are some great resources for HLSL and materials out there BUT for volume textures things seem pretty sparse (except for sparse volume textures 😛 which seem to have some more popularity due to VDB ).

**Question:**

How can I sample a volume texture to do ray marching in the Materials editor?

**Notes:**

- Using UE 5.5
- Open to Volumetrics plugin if there is something in there.
- I currently generate my volume textures from c++ similar to the great [TBRaymarch Plugin](https://github.com/tommybazar/TBRaymarcherPlugin)
- Is that plugin now part of the Volumetrics plugin…?

**Tried:**

I followed this awesome [tutorial](https://www.youtube.com/watch?v=eDYyBc3cRmw) and got it working just find for a 2D texture (PseudoVolumeTexture). However, tried to switch a volume texture causes the Sampler to never be found.

Example custom hlsl code. Neither sampling mode below worked.

```auto
float accumdens = 0;LocalCamVec = normalize(mul(Parameters.CameraVector, (float3x3)LWCToFloat(GetPrimitiveData(Parameters).WorldToLocal)));

for (int i = 0; i < MaxSteps; i++) {// Use Texture3DSample or VolumeTexture.Sample instead of PseudoVolumeTexture

//float cursample = Texture3DSample (VolumeTexture, VolumeTextureSampler, saturate(CurPos)).r;float cursample = VolumeTexture.Sample(VolumeTextureSampler, saturate(CurPos)).r;accumdens += cursample * StepSize;CurPos += -LocalCamVec * StepSize;}

CurPos -= -LocalCamVec * StepSize;CurPos += -LocalCamVec * StepSize * FinalStepSize;

// Final sample with adjusted step size

//float cursample = Texture3DSample (VolumeTexture, VolumeTextureSampler, saturate(CurPos)).r;float cursample = VolumeTexture.Sample(VolumeTextureSampler, saturate(CurPos)).r;accumdens += cursample * StepSize * FinalStepSize;

return accumdens;

```

![voltex_sortof](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/c/6/cc642368d65ac577566d6de3478fefa1d239bd8e.gif)  
Here is an image using a VolumeTexture but manually sweeping through the Z axis (no HLSL just a time + sine node on the Texture UV)… ☹

---

<div class="post-metadata">

**Author:** ![Scottimus06](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scottimus06/32/469167_2.png) [@Scottimus06](https://forums.unrealengine.com/u/Scottimus06)\
**Post date:** [August 29, 2025, 12:50pm UTC](https://forums.unrealengine.com/t/sample-volume-texture-custom-hlsl/2651872/2 "2025-08-29T12:50:57Z")

</div>

Trying to find examples in the Engine code, found:

`Engine\Shaders\Private\BasePassPixelShader.usf`

which uses it like this:

`float4 Vector0 = Texture3DSample(IndirectLightingCache.IndirectLightingCacheTexture0, IndirectLightingCache.IndirectLightingCacheTextureSampler0, VolumeUVs);`

So the function exists… hmm…

If a VolumeTexture render method is already implemented I would love to just use that and avoid this whole custom HLSL black voodoo magic.

---

<div class="post-metadata">

**Author:** ![Scottimus06](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scottimus06/32/469167_2.png) [@Scottimus06](https://forums.unrealengine.com/u/Scottimus06)\
**Post date:** [August 29, 2025, 2:54pm UTC](https://forums.unrealengine.com/t/sample-volume-texture-custom-hlsl/2651872/3 "2025-08-29T14:54:54Z")

</div>

Some progress made…

- The input texture had to switch to a TextureObject and not a TextureSampler
- Use `SampleLevel` and add the mimap value.

```auto
float accumdens = 0;
LocalCamVec = normalize(mul(Parameters.CameraVector, (float3x3)LWCToFloat(GetPrimitiveData(Parameters).WorldToLocal)));

for (int i = 0; i < MaxSteps; i++) {
    // Use Texture3DSample instead of PseudoVolumeTexture
    float cursample = VolumeTexture.SampleLevel(VolumeTextureSampler, saturate(CurPos),0).r;
 
    accumdens += cursample * StepSize;
    CurPos += -LocalCamVec * StepSize;
}

CurPos -= -LocalCamVec * StepSize;
CurPos += -LocalCamVec * StepSize * FinalStepSize;

// Final sample with adjusted step size
float cursample = VolumeTexture.SampleLevel(VolumeTextureSampler, saturate(CurPos),0).r;
accumdens += cursample * StepSize * FinalStepSize;

return accumdens;

```

---

<div class="post-metadata">

**Author:** ![BOBtheROSS](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BOBtheROSS](https://forums.unrealengine.com/u/BOBtheROSS)\
**Post date:** [August 29, 2025, 3:26pm UTC](https://forums.unrealengine.com/t/sample-volume-texture-custom-hlsl/2651872/4 "2025-08-29T15:26:51Z")

</div>

Maybe you already know this, just in case:

The syntax for the Texture2DSampler in HLSL for Unreal is:

`Texture2DSample([TextureObject], [TextureObject]Sampler, [UV]);`

If you name your TextureObject “InTex”, the automatically created sampler would be named “InTexSampler”.  
So for example:

`Texture2DSample(`InTex`, `InTex`Sampler, float2(0.f, 0.f));`

I guess it is the same for the 3D/volume sampler. But maybe you already have a better way to sample.  
————————————————————————————————-  
Unreal materials always generate HLSL code:

- MaterialEditor → Window → Shader Code → HLSL Code
- contents of Custom nodes get parsed into a function, search for the comment “// uniform\_material\_expressions” the function should be called something like MaterialFloat3 CustomExpression0(FMaterialPixelParameters Parameters)

I got to know it from from this article:

> **[How the Unreal Engine Translates a Material Graph to HLSL | Knowledge base](https://dev.epicgames.com/community/learning/knowledge-base/0qGY/how-the-unreal-engine-translates-a-material-graph-to-hlsl)**
>
> How the Unreal Engine Translates a Material Graph to HLSL
> Article written by Matt O. 
> This article will shed light on the process by which Unreal transl...

---

<div class="post-metadata">

**Author:** ![Scottimus06](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scottimus06/32/469167_2.png) [@Scottimus06](https://forums.unrealengine.com/u/Scottimus06)\
**Post date:** [August 29, 2025, 7:51pm UTC](https://forums.unrealengine.com/t/sample-volume-texture-custom-hlsl/2651872/5 "2025-08-29T19:51:54Z")

</div>

I didn’t know you could view the HLSL code. Thanks for that tip and the link. With all these things combined, I have achieved what I desired.

Voila!

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/7/c/c7c3cb636ed169c67b7d0c653cc1d1c28edf5863.jpeg)
