Search Unity

Custom post processing effect crashing in build but not in editor

Discussion in 'Image Effects' started by DejaMooGames, Sep 15, 2019.

  1. DejaMooGames

    DejaMooGames

    Joined:
    Apr 1, 2019
    Posts:
    108
    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:

    Code (HLSL):
    1. Shader "Hidden/Custom/ScreenGlitch"
    2. {
    3.     HLSLINCLUDE
    4.  
    5.     #include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
    6.  
    7.     TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
    8.  
    9.     float _Intensity;
    10.     float _Distance;
    11.     float _Amplitude;
    12.     float4 _MainTex_TexelSize;
    13.  
    14.     float4 Frag(VaryingsDefault i) : SV_Target
    15.     {
    16.         float amount = _MainTex_TexelSize * _Distance;
    17.         float direction = sign(sin(i.texcoord.y * _Intensity * _Time.y));
    18.         float offset = (amount * direction) * cos(i.texcoord.y * _Amplitude);
    19.         float2 uvcoord = { i.texcoord.x + offset, i.texcoord.y };
    20.         float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uvcoord);
    21.         return color;
    22.     }
    23.  
    24.         ENDHLSL
    25.  
    26.         SubShader
    27.     {
    28.         Cull Off ZWrite Off ZTest Always
    29.  
    30.         Pass
    31.         {
    32.             HLSLPROGRAM
    33.  
    34.                 #pragma vertex VertDefault
    35.                 #pragma fragment Frag
    36.  
    37.             ENDHLSL
    38.         }
    39.     }
    40. }
    This is the Custom Effect:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.Rendering.PostProcessing;
    4.  
    5. [Serializable]
    6. [PostProcess(typeof(ScreenGlitchRenderer), PostProcessEvent.AfterStack, "Custom/ScreenGlitch")]
    7. public sealed class ScreenGlitch : PostProcessEffectSettings
    8. {
    9.     [Range(0f, 20f), Tooltip("Screen Glitch Intensity")]
    10.     public FloatParameter intensity = new FloatParameter { value = 0.5f };
    11.     [Range(0f, 5000f), Tooltip("Screen Glitch Distance")]
    12.     public FloatParameter distance = new FloatParameter { value = 100.0f };
    13.     [Range(0f, 100f), Tooltip("Screen Glitch Amplitude")]
    14.     public FloatParameter amplitude = new FloatParameter { value = 1.0f };
    15. }
    16.  
    17. public sealed class ScreenGlitchRenderer :PostProcessEffectRenderer<ScreenGlitch>
    18. {
    19.     public override void Render(PostProcessRenderContext context)
    20.     {
    21.         var sheet = context.propertySheets.Get(Shader.Find("Hidden/Custom/ScreenGlitch"));
    22.         sheet.properties.SetFloat("_Intensity", settings.intensity);
    23.         sheet.properties.SetFloat("_Distance", settings.distance);
    24.         sheet.properties.SetFloat("_Amplitude", settings.amplitude);
    25.         context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
    26.     }
    27. }
    This is the script used to control the effect:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering.PostProcessing;
    5.  
    6. public class ScreenGlitchController : MonoBehaviour
    7. {
    8.     public PostProcessVolume ppv_Global;
    9.     private ScreenGlitch GlitchLayer = null;
    10.     [SerializeField]
    11.     private float f_GlitchDuration;
    12.     [SerializeField]
    13.     private float f_MaxGlitchIntensity;
    14.     [SerializeField]
    15.     private float f_MaxGlitchAmplitude;
    16.  
    17.     public void Start()
    18.     {
    19.         ppv_Global = gameObject.GetComponent<PostProcessVolume>();
    20.         ppv_Global.profile.TryGetSettings(out GlitchLayer);
    21.         GlitchLayer.amplitude.value = 0f;
    22.         GlitchLayer.intensity.value = 0f;
    23.         GlitchLayer.distance.value = 250f;
    24.     }
    25.  
    26.     public void StartGlitch()
    27.     {
    28.         StartCoroutine(Glitch());
    29.     }
    30.  
    31.     public IEnumerator Glitch()
    32.     {
    33.         GlitchLayer.active = true;
    34.         float progress = 0;
    35.         float duration = 0;
    36.         GlitchLayer.amplitude.value = f_MaxGlitchAmplitude;
    37.         while(progress < 1.0f)
    38.         {
    39.             if (progress < .5f)
    40.             {
    41.                 GlitchLayer.intensity.value = Mathf.Lerp(0.0f, f_MaxGlitchIntensity, progress * 2.0f);
    42.             }
    43.             else
    44.             {
    45.                 GlitchLayer.intensity.value = Mathf.Lerp(f_MaxGlitchIntensity, 0, (progress - .5f) * 2.0f);
    46.             }
    47.             yield return null;
    48.             duration += Time.deltaTime;
    49.             progress = duration / f_GlitchDuration;
    50.         }
    51.         GlitchLayer.intensity.value = 0f;
    52.         GlitchLayer.amplitude.value = 0f;
    53.         GlitchLayer.active = false;
    54.     }
    55. }
     
  2. DejaMooGames

    DejaMooGames

    Joined:
    Apr 1, 2019
    Posts:
    108
    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 :)