<p>I was over thinking it.</p>
<p>Instead of discarding it, I needed to just let the ray keep marching.</p>
<p>adding</p>
<pre><code>if (p.x &lt; xSlicer.x){
       p += rayDir * delta;
       continue;
   }
</code></pre>
<p>To the top of the for loop fixed it.</p>
<p><a href="https://i.sstatic.net/ZOUmh.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ZOUmh.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2024-03-01T08:53:07.047Z
2024-03-01T08:53:07.047Z
TheJeran
<p>I'm currently visualizing scientific data as volumes using Three and GLSL. I have a raymarching script that is pretty standard and colors based on a colormap I feed into the shader.</p>
<p>It looks like this</p>
<p><a href="https://i.sstatic.net/HpkXp.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/HpkXp.png" alt="enter image description here" /></a></p>
<p>I want to be able to slice through the data using the slice sliders. However, I'm not certain how to implement this in my raymarching script. I originally had it discard fragments with positions less than the <em>slice</em> sliders. However, that method discards all rays if the camera is also positioned to the negative of that slice value. Not to mention it won't show me the side of the volume.</p>
<p>Here is the main of my shader.</p>
<pre><code>void main(){
    vec3 rayDir = normalize( vDirection );
    vec2 bounds = hitBox( vOrigin, rayDir );

    if ( bounds.x &gt; bounds.y ) discard;

    bounds.x = max( bounds.x, 0.0 );

    vec3 p = vOrigin + bounds.x * rayDir;
    vec3 inc = 1.0 / abs( rayDir );
    float delta = min( inc.x, min( inc.y, inc.z ) );
    delta /= steps;

    for ( float t = bounds.x; t &lt; bounds.y; t += delta ) {

        float d = sample1( p/scaleVec/scale + 0.5 );
        bool cond = d &gt; threshold;
        cond = flip ? !cond: cond;

        if ( cond ) {
            color.rgb = texture2D(cmap, vec2(d, 0.5)).rgb;
            color.a = 1.;

            break;
        }

        p += rayDir * delta;

    }

    if ( color.a == 0.0 ) discard;

}
</code></pre>
<p>What is the correct way to be able to slice this while also seeing the side of the volume?</p>

<glsl><three.js>
Slice a volume in GLSL using raymarching
TheJeran
# Slice a volume in GLSL using raymarching

(Asked by: username_0 on 2024-02-29)

I'm currently visualizing scientific data as volumes using Three and GLSL. I have a raymarching script that is pretty standard and colors based on a colormap I feed into the shader.

It looks like this

[![enter image description here](https://i.sstatic.net/HpkXp.png)](https://i.sstatic.net/HpkXp.png)

I want to be able to slice through the data using the slice sliders. However, I'm not certain how to implement this in my raymarching script. I originally had it discard fragments with positions less than the *slice* sliders. However, that method discards all rays if the camera is also positioned to the negative of that slice value. Not to mention it won't show me the side of the volume.

Here is the main of my shader.

```
void main(){
    vec3 rayDir = normalize( vDirection );
    vec2 bounds = hitBox( vOrigin, rayDir );

    if ( bounds.x > bounds.y ) discard;

    bounds.x = max( bounds.x, 0.0 );

    vec3 p = vOrigin + bounds.x * rayDir;
    vec3 inc = 1.0 / abs( rayDir );
    float delta = min( inc.x, min( inc.y, inc.z ) );
    delta /= steps;

    for ( float t = bounds.x; t < bounds.y; t += delta ) {

        float d = sample1( p/scaleVec/scale + 0.5 );
        bool cond = d > threshold;
        cond = flip ? !cond: cond;

        if ( cond ) {
            color.rgb = texture2D(cmap, vec2(d, 0.5)).rgb;
            color.a = 1.;

            break;
        }

        p += rayDir * delta;

    }

    if ( color.a == 0.0 ) discard;

}

```

What is the correct way to be able to slice this while also seeing the side of the volume?

**Tags:** glsl, three.js


---

## 1 answers

### Answer by username_0 on 2024-03-01. Score: 2 | Accepted answer

I was over thinking it.

Instead of discarding it, I needed to just let the ray keep marching.

adding

```
if (p.x < xSlicer.x){
       p += rayDir * delta;
       continue;
   }

```

To the top of the for loop fixed it.

[![enter image description here](https://i.sstatic.net/ZOUmh.png)](https://i.sstatic.net/ZOUmh.png)


---

