# Can we intersect fragment/raymarched "object" with regular mesh?

**URL:** <https://discourse.threejs.org/t/can-we-intersect-fragment-raymarched-object-with-regular-mesh/59922>\
**Category:** Questions\
**Tags:** fragment-shader, geometry\
**Created:** [January 4, 2024, 8:00am UTC](https://discourse.threejs.org/t/can-we-intersect-fragment-raymarched-object-with-regular-mesh/59922 "2024-01-04T08:00:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![trusktr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/trusktr/32/13726_2.png) [@trusktr](https://discourse.threejs.org/u/trusktr)\
**Post date:** [January 4, 2024, 8:00am UTC](https://discourse.threejs.org/t/can-we-intersect-fragment-raymarched-object-with-regular-mesh/59922/1 "2024-01-04T08:00:00Z")

</div>

I’d like to take something like the blobs/metaballs in this demo:

> **[Shadertoy](https://www.shadertoy.com/view/4ll3R7)**
>
> Build shaders, share them, and learn from the best community.

and intersect them with regular meshes. Never done that before.

I’m guessing that if I use `gl_FragDepth`, I can specify the depth of the fragment drawing, and this can make the pixels result in being in front of or behind of regular meshes.

If so, is there a demo of this?

EDIT: similar question: [How I can combine a shader based rendering and three.js scene graph](https://discourse.threejs.org/t/how-i-can-combine-a-shader-based-rendering-and-three-js-scene-graph/25075)

---

<div class="post-metadata">

**Author:** ![manthrax](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/manthrax/32/2596_2.png) [@manthrax](https://discourse.threejs.org/u/manthrax)\
**Post date:** [January 5, 2024, 1:46am UTC](https://discourse.threejs.org/t/can-we-intersect-fragment-raymarched-object-with-regular-mesh/59922/2 "2024-01-05T01:46:54Z")

</div>

You can do this. I’ve done it before… and iirc yes it involves using gl\_FragDepth if you want correct depth values. I don’t have any samples I can point you to though. I think when I did it, I used a cube, and did the raymarching in the cubes shader.

Here’s a project that uses SDFs for modelling and is quite nice:

> **[MagicaVoxel](https://ephtracy.github.io/index.html?page=magicacsg#ss-carousel_ss)**
>
> MagicaVoxel Official Website

Another approach is to extract the SDF into a buffergeometry via marchingcubes. I haven’t messed with this much but it looks promising?

[https://threejs.org/examples/?q=SDF#webgl\_geometry\_sdf](https://threejs.org/examples/?q=SDF#webgl_geometry_sdf)
