Distortion Shader


A basic distortion shader created for Goya's Nightmare videogame

Distortion Unity HLSL

Source Code

The objective of this shader is to distort the camera screen when an enemy is close to you or facing directly to your forward view direction, getting more distortion when the action time is increasing. With it, we simulate the remaining life points you have, getting a fully dark screen when you are dead.

It is designed in Unity, as an image effect shader. It’s written in HLSL language, modifying the fragment function.

To develop this shader, we use different inputs:

  • Camera output screen
    • We take the final render image from the camera and we apply the filter using the function:
    private void OnRenderImage(RenderTexture source, RenderTexture destination) { 
          Graphics.Blit(source, destination, material);
    }
    
  • Noise texture
    • A noisy texture to modify the UV coordinates from the camera image. We will use a random noise based on two different patterns to gain more randomness, using the RGB channels of the texture.

NoiseTexture NoiseCode

  • Distortion and darkness
    • A float to modify the factor of distortion and darkness of the shader.

DistortionFactor DarknessFactor


You can find the code here: Distortion Shader