Skip to main content

Camera Based Masking

Use the camera's world position as a mask in your materials.

Published Updated |
Materials
graphic

Use the camera’s world position as a mask in your materials. This masking method is helpful for pushing foliage away with World Position Offset as the camera gets close, fading out walls when the character is behind them, and so on.

Examples

Here are a few examples that show this mask in use.

How do I build the mask?

The mask compares the world position of each pixel or vertex against the camera’s world position. Here are the material nodes involved:

NodeDescription
Camera PositionOutputs the camera’s position in world space.
Absolute World PositionOutputs the world position of the pixel or vertex being shaded.
DistanceOutputs the distance between two positions. Divide it by a radius to get a gradient.
SphereMaskOutputs 1 inside a radius around a point and falls off to 0. Radius defaults to 256 and Hardness to 100%.

For a ready-made falloff, connect Camera Position and Absolute World Position to the A and B inputs of a SphereMask. The default Hardness of 100% gives a hard edge, so lower it for a softer one, or add a OneMinus node (1-x) after it to invert the mask.

Plot of SphereMask output against distance with Radius 256. Hardness 100% drops from 1 to 0 at the radius. Hardness 0%, 50% and 80% stay at 1 out to that fraction of the radius, then fall linearly to 0 at the radius.

Frequently asked questions

Why isn’t my surface fading out?

Opaque materials don’t use the Opacity or Opacity Mask inputs, so switch the material to the Masked or Translucent blend mode to fade or cut out a surface with this mask.

Is the Hardness pin a percent too?

No. The Hardness property in the Details panel is a percent, but a value connected to the Hardness pin is read as 0 to 1. So, 0.8 on the pin matches 80% in the property.

Check out the following resources to learn more about the subject.

Unreal Directive is free and ad-free.

If it saved you time, you can help keep it that way.

Donate