Cutaway views

I always hang out in the #engine_unreal channel of the Tech-Artists.Org slack. One day someone asked a question about creating a cutaway material for viewing CAD models. I couldn’t help right away but the problem piqued my interest so I investigated it. Bellow are the 3 ways I could come up with.

cutaway views Cutaway views Cutaway17

These are the CAD models used in the article:

 

Air compressor by Alireza Akbarzadeh

Reduction Gearbox for BAJA vehicles by Pranay Vaka

 

A big thanks to the talented creators! 🙂

Shared mechanics
cutaway views Cutaway views Cutaway16 300x274

What every method has in common is that the materials get the clipping plane’s data from a Material Parameter Collection. The values there are modified by a ClippingManager blueprint class which ticks in the editor: PlaneLocation is set to actor location, PlaneNormal is set to actor up vector. A small quad mesh component is used to visualize the location and alignment of the clipping plane.

cutaway views Cutaway views Cutaway1 300x128

In every masked material each pixel’s distance is calculated from our clipping plane and the ones behind it are hidden. (See the logic on the left.) Of course this could be replaced with any parametric shape (e.g.: box, sphere).

cutaway views Cutaway views Cutaway2 300x111

The blue stripe pattern is produced by the same logic except its incoming coordinates vary in the different solutions.

The simplest way
cutaway views Cutaway views Cutaway4 300x135

The idea here is to take the backfaces (which we can see now that part of the mesh surface is hidden) and set their normals to the normal of the clipping plane, thus creating the illusion of a flat surface (although we are peeking into the clipped mesh).

cutaway views Cutaway views Cutaway3 300x90

To place the stripes on the (non existent) cutaway surface we’ll have to use screen position as a base. To make the effect less jarring/hypnotizing I used Jan Klauza’s Object-stabilized screen-space UVs.

cutaway views Cutaway views Cutaway5 300x123

To prevent z-fighting where two meshes with the cutaway material touch this simple logic is linked to the Pixel Depth Offset material property: all front faces are pushed back a tiny bit giving priority to any backface at the same world location.

It’s a fairly simple setup and the material is reasonably cheap but this technique has several drawbacks: since the z-buffer is not affected, only the normals of backfaces, post process effects relying on scene depth betray the illusion.

cutaway views Cutaway views Cutaway6 300x284

SSAO

cutaway views Cutaway views Cutaway7 300x284

SSR

cutaway views Cutaway views Cutaway8 300x284

Both SSAO and SSR disabled.

There is also no way to fix situations when one mesh is penetrating another. That issue might pop up not just due to the way the mesh was modeled but also can happen due to the way the CAD data is polygonized. (For example a rod and a tube around it might get different number of sides so their cross-section don’t match perfectly anymore.)

cutaway views Cutaway views Cutaway9 300x254
cutaway views Cutaway views Cutaway10 300x292

Shadows can also pose a problem: If they can’t be turned off then make sure that the backfaces are fully metallic and have roughness in the 0-0.1 range. The specular highlights will look weird from certain angles but otherwise the shadows won’t be that visible inside.

And of course distance field shadows can’t be used because the opacity change will not affect them.

 

So with all that in mind this method is probably better suited for VR applications.

As a post-process
cutaway views Cutaway views Cutaway11 300x139

The setup of the mesh material is similar to the previous one except here we only change the normal of the backfaces: we set it to Camera Direction Vector. (The multiplication by -1 is there to compensate for the back facing tangent space.)

This way we can tell apart frontfaces and backfaces later in the post-process material. (We should never be able to see a frontface facing the same way as the camera.)

On every clipped mesh the Render Custom Depth Pass has to be enabled with the stencil value of 1. This will help us in the next step.

cutaway views Cutaway views Cutaway12 300x194

The post-process material blends between the rendered frame buffer and the blue stripes. The alpha the blending is based on is the result of two calculations:

  1. With a dot product we check how much the world normals match to the camera direction. Since we marked the backfaces they will end up as 1.
  2. This logic makes sure than only the meshes which show up in the custom depth buffer are considered, culling a false positives from skyboxes or unlit surfaces for example.

Compared to the previous method the advantage here is that we can have both screen space effects and shadows enabled. The disadvantage is that the cross section is unlit, the stripes are less stable and the custom depth pass requires an additional buffer.

The deluxe method
cutaway views Cutaway views Cutaway18 300x168

This time around the setup is a bit more elaborate.

First every clipped mesh gets a Custom Depth Stencil Value of 0. Then we have to create duplicates of them with the value of 1. These copies will show up as the only non-zero value in the Custom Stencil buffer. To prevent z-fighting with their originals, a simple, unlit material, seen on the left, is applied to them: frontfaces are hidden, backfaces are shown. It is recommended to turn off shadow casting, Render In Main Pass, Use As Occluder, Receives Decals and collision on them too.

cutaway views Cutaway views Cutaway14 300x121

If all went well then we have a Custom Stencil buffer which only shows the backfaces of the marked meshes without other, interpenetrating objects.

cutaway views Cutaway views Cutaway15 300x81

Now we are going to use this neat mask on an actual plane mesh to control it’s opacity. To be able to use that buffer the material has to be forward shaded so the deferred rendering is all done by the time it’s evaluated. The simplest, cheapest is an unlit transparent setup but for full PBR results go for a lit transparent one with Surface Forward Shading lighting mode (expensive).