Custom post processing effect crashing in build but not in editor

I wrote a custom post processing effect and it works just fine in the editor but regardless of the platform it causes the game to freeze whenever the effect is active. Does anyone see something glaring in my code that doesn’t work? I am assuming it is in the shader but I don’t know how to approach fixing this problem.

Unity 2019.2.0f1
Post Processing Stack 2.1.7
Default Rendering Pipeline

This is the Shader:

Shader "Hidden/Custom/ScreenGlitch"
{
    HLSLINCLUDE

    #include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"

    TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);

    float _Intensity;
    float _Distance;
    float _Amplitude;
    float4 _MainTex_TexelSize;

    float4 Frag(VaryingsDefault i) : SV_Target
    {
        float amount = _MainTex_TexelSize * _Distance;
        float direction = sign(sin(i.texcoord.y * _Intensity * _Time.y));
        float offset = (amount * direction) * cos(i.texcoord.y * _Amplitude);
        float2 uvcoord = { i.texcoord.x + offset, i.texcoord.y };
        float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uvcoord);
        return color;
    }

        ENDHLSL

        SubShader
    {
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            HLSLPROGRAM

                #pragma vertex VertDefault
                #pragma fragment Frag

            ENDHLSL
        }
    }
}

This is the Custom Effect:

using System;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

[Serializable]
[PostProcess(typeof(ScreenGlitchRenderer), PostProcessEvent.AfterStack, "Custom/ScreenGlitch")]
public sealed class ScreenGlitch : PostProcessEffectSettings
{
    [Range(0f, 20f), Tooltip("Screen Glitch Intensity")]
    public FloatParameter intensity = new FloatParameter { value = 0.5f };
    [Range(0f, 5000f), Tooltip("Screen Glitch Distance")]
    public FloatParameter distance = new FloatParameter { value = 100.0f };
    [Range(0f, 100f), Tooltip("Screen Glitch Amplitude")]
    public FloatParameter amplitude = new FloatParameter { value = 1.0f };
}

public sealed class ScreenGlitchRenderer :PostProcessEffectRenderer<ScreenGlitch>
{
    public override void Render(PostProcessRenderContext context)
    {
        var sheet = context.propertySheets.Get(Shader.Find("Hidden/Custom/ScreenGlitch"));
        sheet.properties.SetFloat("_Intensity", settings.intensity);
        sheet.properties.SetFloat("_Distance", settings.distance);
        sheet.properties.SetFloat("_Amplitude", settings.amplitude);
        context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
    }
}

This is the script used to control the effect:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

public class ScreenGlitchController : MonoBehaviour
{
    public PostProcessVolume ppv_Global;
    private ScreenGlitch GlitchLayer = null;
    [SerializeField]
    private float f_GlitchDuration;
    [SerializeField]
    private float f_MaxGlitchIntensity;
    [SerializeField]
    private float f_MaxGlitchAmplitude;

    public void Start()
    {
        ppv_Global = gameObject.GetComponent<PostProcessVolume>();
        ppv_Global.profile.TryGetSettings(out GlitchLayer);
        GlitchLayer.amplitude.value = 0f;
        GlitchLayer.intensity.value = 0f;
        GlitchLayer.distance.value = 250f;
    }

    public void StartGlitch()
    {
        StartCoroutine(Glitch());
    }

    public IEnumerator Glitch()
    {
        GlitchLayer.active = true;
        float progress = 0;
        float duration = 0;
        GlitchLayer.amplitude.value = f_MaxGlitchAmplitude;
        while(progress < 1.0f)
        {
            if (progress < .5f)
            {
                GlitchLayer.intensity.value = Mathf.Lerp(0.0f, f_MaxGlitchIntensity, progress * 2.0f);
            }
            else
            {
                GlitchLayer.intensity.value = Mathf.Lerp(f_MaxGlitchIntensity, 0, (progress - .5f) * 2.0f);
            }
            yield return null;
            duration += Time.deltaTime;
            progress = duration / f_GlitchDuration;
        }
        GlitchLayer.intensity.value = 0f;
        GlitchLayer.amplitude.value = 0f;
        GlitchLayer.active = false;
    }
}

I found the issue. The shader wasn’t being included in the build. I added it in the always include list and the problem went away immediately. Should have read the documentation a second time :slight_smile: