Skip to content

FPS Demo

Felipe Lira edited this page Feb 11, 2020 · 3 revisions

First Person Demo

This demo showcases a setup to render first person perspective objects with a FOV(Field Of View) that differs from the game scene rendering FOV, this is common in first person games where the FOV needed for the experience is too wide for the objects held in hand ends up distorted.

  • Can be found at _CompletedDemos/FPS/FPSRenderPass/FPSCameraDemo.unity
  • Uses the Render Objects (Experimental) feature that is provided with the URP Package
  • Different FOV for GameObjects with the Layer set to First Person Objects
  • Depth settings to avoid rendering inside close objects like walls
  • Stencil settings to properly handle transparencies

The setup

We need three Render Objects (Experimental) features and to also override both the Default Layer Mask and Stencil settings as shown below:

  • Default Layer Mask is set to ignore the First Person Objects layer as these will be rendered by the three Renderer Features.
  • Stencil settings have been overidden, this changes the default passes(Forward Opaque and Forward Transparent) handling of stencil values:
    • Value and Compare Function are important here, because we only want to write pixels if the existing value is equal to 0, this is because we will render the weapon into Stencil Value 1

GunOpaques

this Renderer Feature is rendering the opaque parts of the weapon

  • Uses Before Rendering Opaques event to execute before the URP Forward Opaque Pass
  • Filters Opaque renderers that have been tagged First Person Objects
  • Overrides Stencil
    • Value that will be used set to 1
    • Compare Function set to Always return true
    • Replace the existing value when passing(the initial value of the Stencil Buffer is 0) with the Value specified
  • Overrides Camera
    • Use a Field Of View of 40
    • Restore the camera after finishing this render pass

GunTransparents

this Renderer Feature renders the transparent parts of the weapon that overlap with the Opaque parts

  • Uses After Rendering Tranparents event to execute after the URP Forward Transparent Pass
  • Filters Transparent renderers that have been tagged First Person Objects
  • Overrides Stencil
    • Value that will be used set to 1
    • Compare Function set to Equal so we only render if the existing value is the same as 1, this is rendering to any pixels inside of the opaque weapon
  • Overrides Camera
    • Use a Field Of View of 40
    • Restore the camera after finishing this render pass

GunTransparentsOverlay

this Renderer Feature renders the transparent parts of the weapon that do not overlap with the Opaque parts but always render over the scene regardless of depth.

  • Uses After Rendering Tranparents event to execute after the URP Forward Transparent Pass and after the previous pass due to the ordering in the list
  • Filters Transparent renderers that have been tagged First Person Objects
  • Overrides Depth
    • Depth Test is set to Always to make sure regardless of the existing depth value, we always pass.
  • Overrides Stencil
    • Value that will be used set to 0
    • Compare Function set to Equal so we only render if the existing value is the same as 0, this is rendering to any pixels outside of the opaque weapon
  • Overrides Camera
    • Use a Field Of View of 40
    • Restore the camera after finishing this render pass
Clone this wiki locally