Rasagar/Library/PackageCache/com.unity.render-pipelines.high-definition/Documentation~/create-a-fullscreen-material.md
2024-08-26 23:07:20 +03:00

6.2 KiB
Raw Blame History

Create a Fullscreen Material

Create a Fullscreen shader graph

To create a full-screen shader in Shader Graph, use one of the following methods:

  • Create a new Fullscreen shader graph:

    • Go to Assets > Create > Shader Graph > HDRP > Fullscreen Shader Graph.
  • Modify an existing shader graph:

  1. In the Project window, select a shader graph to open it in the Shader Editor.
  2. In Graph Settings, select the HDRP Target. If there isn't one, go to Active Targets, click the Plus button, and select HDRP.
  3. In the Material drop-down, select Fullscreen.

This creates a shader graph that contains the Fullscreen Master Stack.

Fullscreen shader samples

HDRP includes sample Fullscreen shaders. To use these samples in your project:

  1. Select Window > Package Manager.
  2. Select High Definition RP.
  3. Open the Samples tab.
  4. Find Fullscreen Samples and select Import.

To see the Fullscreen shader graph samples in a scene, open the Fullscreen sample scene:

  1. In the Project window, open Assets > Samples > High Definition RP > Version Number > Fullscreen Samples > Scenes.
  2. Select the FullscreenSamplesTestingScene to open it.
  3. Select the Play button and use the arrow keys to switch between Fullscreen shader examples.

Apply a full-screen shader graph

You can apply a full-screen shader graph in a post processing effect or a custom pass:

Make a full-screen shader graph Blit compatible

To make a full-screen shader graph material compatible with assets or C# scripts that use the Blit() command, do the following:

Set a Blit source material

To set a source material for the Blit() command to use in shader graph:

  1. Double-click a Fullscreen shader graph to open it in Shader Graph.
  2. Create a MainTex node:
    1. In the Blackboard, select the Add (+) button.
    2. Select Texture2DArray.
    3. Select the Texture2D Array node you just created to open its properties in the Graph Inspector.
    4. In the Name property, enter _MainTex.
  3. In Node Settings, enable the Exposed toggle so Blit()can bind the texture.
  4. Drag the MainTex node into your shader graph.
  5. Press the Spacebar to open the Create Node window
  6. In the Create Node window, search for the Texture 2D Array node and select it to create it in your scene.
  7. Connect the MainTex node to the Texture Array port of the Sample Texture 2D Array node.
  8. Connect the RBGA output port of the Sample Texture 2D Array to the Base Color block in the Fragment context.
  9. Blit() automatically binds the source texture inside the _MainTex property.

Note: The type of source material node depends on the platform you use. Windows and some platforms use Texture2DArray and other platforms use Texture2D. To see which texture type to use, refer to the documentation for the platform you are using.

Set the Blit pass in a C# script

Shader Graph outputs multiple passes for compatibility reasons. This means you need to manually tell the Blit() command to use the correct pass. You can do this in any script that uses a full-screen pass, for example a custom pass or a custom render texture script.

The Blit pass is pass number 0. Use the following line to reference this pass:

Graphics.Blit(sourceTexture, destinationTexture, material, pass: 0);

Troubleshooting

This section helps you resolve the most common issues you can have in a full-screen shader.

Gray screen

Your scene appears gray if you read from and write to the color buffer in the same custom pass. To fix this, see Write to and read from the custom color buffer.

Missing part of full-screen effect

This error can happen when you use a full-screen shader with a custom pass C# script that uses the Blit() function, and you havent selected the Blit shader pass in the Inspector window or in a script.

To fix this error:

  • In a full-screen custom pass, set the Pass Name property to Blit.
  • In a post processing script, set the Blit pass to 0. For instructions, see Set the Blit pass in a C# script.

Inverted full-screen effect

In some cases, HDRP flips the full-screen effect upside down. This can happen when you write to the Camera color buffer with BuiltinRenderTextureType.CameraTarget or write directly to the display.

To fix this error, flip the material on the Y axis in the script that uses a full-screen shader. To do this, add the following line before the call that applies the full-screen effect:

material.SetFloat("_FlipY", 1);

Note: You can also do this with a MaterialPropertyBlock if you use a CommandBuffer to build your effect.