Skip to content

Occlusion

Jonas Mortensen edited this page Feb 13, 2020 · 5 revisions

This demo showcases how Rendering Features can be used to apply a dithering effect to occluded objects. Making occluded objects visible is widely used in games where the player needs positional information. The game might show the location of the player's team mates at all times, or show occluded units in Real Time Strategy games in order to make them selectable.

  • Can be found at _CompletedDemos/UnityOcclusionDemoCompleted/UnityOcclusionDemo.unity
  • Uses the Render Objects (Experimental) feature that is provided with the URP Package
  • Debth settings overridden to force drawing "character" occluded objects with custom material.
  • Render "character" objects after dither effect to avoid drawing self occlusion.

The Setup

The solution needs a material which will override when drawing the occluded objects. This material will apply a screen space dither effect.

Most of the setup is in the renderer asset. The renderer Default Layer Mask will be set to ignore the Character layer as it will be handled by the renderer features. Two renderer features will be added:

Dither Effect

This renderer feature is responsible for drawing the dither effect in the occluded areas. The renderer feature Render Objects is perfect for this as it contains options for overriding depth and material. The material is set to the previously mentioned dither material. This material uses a simple unlit shader graph shader. the depth is overridden and tests whether the debth is greater. This will make the renderer feature render in top whehever the object is behind. The layer mask is set to the Character layer. The event is set to After Rendering Opaques to force that the dither effect is drawn on top.

Character

After adding the dither effect and removing the Character layer from the default layer mask, the characters will no longer render when they are in front. This is fixed with another Render Objects renderer feature. The settings are default except for the layer mask being set to Character and the event set to After Rendering Opaques. This will draw the opaque characters on top of the dither effect when not occluded.

One might think that adding Character to the deafult layer mask of the renderer and removing the second renderer feature would suffice, but this would creat artifacts as the characters would occlude themselves and therefore draw dither effects when not occluded:

Clone this wiki locally