Search Unity

Camera Fade Shader Stopped Working in LWRP (Solved)

Discussion in 'Shaders' started by spryx, Aug 4, 2019.

  1. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    557
    Edit: Found solution. See next post.

    I recently switched to the lwrp. My Camera fade-in/out shader has suddenly stopped working. Does anyone have any advice, or possibly a replacement that works with LWRP? I have included the shader and camera scripts.

    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2. // Taken from: https://github.com/iBicha/Unity-Simple-Fade-Camera
    3. // http://brahim.hadriche.me/
    4. // Author: Brahim Hadriche
    5.  
    6. Shader "Hidden/FadeCameraShader"
    7. {
    8.     Properties
    9.     {
    10.         _MainTex ("Texture", 2D) = "white" {}
    11.         _opacity ("Opacity", Range (0, 1)) = 0
    12.         _Color ("Fade Color", Color) = (0,0,0,1)
    13.     }
    14.  
    15.     SubShader
    16.     {
    17.         // No culling or depth
    18.         Cull Off ZWrite Off ZTest Always
    19.  
    20.         Pass
    21.         {
    22.             CGPROGRAM
    23.             #pragma vertex vert
    24.             #pragma fragment frag
    25.          
    26.             #include "UnityCG.cginc"
    27.  
    28.             struct appdata
    29.             {
    30.                 float4 vertex : POSITION;
    31.                 float2 uv : TEXCOORD0;
    32.             };
    33.  
    34.             struct v2f
    35.             {
    36.                 float2 uv : TEXCOORD0;
    37.                 float4 vertex : SV_POSITION;
    38.             };
    39.  
    40.             uniform sampler2D _MainTex;
    41.             uniform float _opacity;
    42.             uniform float4 _Color;
    43.  
    44.             v2f vert (appdata v)
    45.             {
    46.                 v2f o;
    47.                 o.vertex = UnityObjectToClipPos(v.vertex);
    48.                 o.uv = v.uv;
    49.                 return o;
    50.             }  
    51.  
    52.             fixed4 frag (v2f i) : SV_Target
    53.             {
    54.                 fixed4 col = tex2D(_MainTex, i.uv);
    55.                 col = col * _opacity + _Color * (1-_opacity);
    56.                 return col;
    57.             }
    58.             ENDCG
    59.         }
    60.     }
    61. }

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteInEditMode]
    4. // ReSharper disable once CheckNamespace
    5. public class FadeCamera : MonoBehaviour
    6. {
    7.     [Range(0f, 1f)]
    8.     public float opacity = 1;
    9.     public Color color = Color.black;
    10.  
    11.     private Material _material;
    12.     private float _startTime;
    13.     private float _startOpacity = 1;
    14.     private int _endOpacity = 1;
    15.     private float _duration;
    16.     private bool _isFading;
    17.     private static readonly int Opacity = Shader.PropertyToID("_opacity");
    18.  
    19.     public void FadeIn(float duration = 1)
    20.     {
    21.         _duration = duration;
    22.         _startTime = Time.time;
    23.         _startOpacity = opacity;
    24.         _endOpacity = 1;
    25.         _isFading = true;
    26.     }
    27.  
    28.     public void FadeOut(float duration = 1)
    29.     {
    30.         _duration = duration;
    31.         _startTime = Time.time;
    32.         _startOpacity = opacity;
    33.         _endOpacity = 0;
    34.         _isFading = true;
    35.     }
    36.  
    37.     void Awake()
    38.     {
    39.         _material = new Material(Shader.Find("Hidden/FadeCameraShader"));
    40.     }
    41.  
    42.     void Start()
    43.     {
    44.         FadeIn();
    45.     }
    46.  
    47.     void OnRenderImage(RenderTexture source, RenderTexture destination)
    48.     {
    49.         if (_isFading && _duration > 0)
    50.         {
    51.             opacity = Mathf.Lerp(_startOpacity, _endOpacity, (Time.time - _startTime) / _duration);
    52.             // ReSharper disable once CompareOfFloatsByEqualityOperator
    53.             _isFading = opacity != _endOpacity;
    54.         }
    55.  
    56.         // ReSharper disable once CompareOfFloatsByEqualityOperator
    57.         if (opacity == 1f)
    58.         {
    59.             Graphics.Blit(source, destination);
    60.             return;
    61.         }
    62.  
    63.         _material.color = color;
    64.         _material.SetFloat(Opacity, opacity);
    65.         Graphics.Blit(source, destination, _material);
    66.     }
    67. }
     
    Last edited: Aug 4, 2019
  2. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    557
    Found a simple solution to this as a fullscreen image effect. The shader and effect scripts are below.

    Code (CSharp):
    1. /*
    2. * Camera Fade
    3. * Image Effect by Dan Flanigan, djflan@gmail.com
    4. * Written for Delve on 8/4/2019
    5. */
    6.  
    7. using System;
    8. using UnityEngine;
    9. using UnityEngine.Rendering.PostProcessing;
    10.  
    11. [Serializable]
    12. [PostProcess(typeof(CameraFadeRenderer), PostProcessEvent.AfterStack, "ImageFx/CameraFade")]
    13. public sealed class CameraFade : PostProcessEffectSettings
    14. {
    15.     [Range(0f, 1f), Tooltip("Opacity Value. 0 = Full Transparency, 1 = Full Opacity")]
    16.     public FloatParameter opacity = new FloatParameter { value = 0f };
    17.     [Tooltip( "Fade Color")]
    18.     public ColorParameter fadeColor = new ColorParameter {value = Color.black};
    19. }
    20.  
    21. public sealed class CameraFadeRenderer : PostProcessEffectRenderer<CameraFade>
    22. {
    23.     public override void Render(PostProcessRenderContext context)
    24.     {
    25.         var sheet = context.propertySheets.Get(Shader.Find("Hidden/CameraFade"));
    26.         sheet.properties.SetColor("_Color", settings.fadeColor);
    27.         sheet.properties.SetFloat("_Opacity", settings.opacity);
    28.         context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
    29.     }
    30. }

    Code (CSharp):
    1. /*
    2. * Camera Fade Shader
    3. * Image Effect by Dan Flanigan, djflan@gmail.com
    4. * Written for Delve on 8/4/2019
    5. */
    6.  
    7. Shader "Hidden/CameraFade"
    8. {
    9.     HLSLINCLUDE
    10.  
    11.         #include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
    12.  
    13.         TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
    14.         float _Opacity;
    15.         float4 _Color;
    16.  
    17.         float4 Frag(VaryingsDefault i) : SV_Target
    18.         {
    19.                 float4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord);
    20.                 col = col * _Opacity + _Color * (1-_Opacity);
    21.                 return col;
    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. }

    Sample script controlling fade effect (on object that contains pp fx layer)
    Code (CSharp):
    1. using DG.Tweening;
    2. using UnityEngine;
    3. using UnityEngine.Rendering.PostProcessing;
    4.  
    5. public class PostFxController : MonoBehaviour
    6. {
    7.     private PostProcessVolume effectsVolume;
    8.     private CameraFade cf;
    9.     public float fadeDuration = 1f;
    10.  
    11.     void Awake()
    12.     {
    13.         effectsVolume = GetComponent<PostProcessVolume>();
    14.     }
    15.  
    16.     void Start()
    17.     {
    18.         if (effectsVolume != null)
    19.         {
    20.             if (effectsVolume.profile.TryGetSettings(out cf))
    21.             {
    22.                 cf.Opacity.value = 0f;
    23.                
    24.                 //Use DOTween to tween opacity value;
    25.                 DOTween.To(() => cf.Opacity.value, x => cf.Opacity.value = x, 1f, fadeDuration).OnComplete(Finished);
    26.             }
    27.         }
    28.     }
    29.  
    30.     void Finished()
    31.     {
    32.         //Disable fade and kill this script
    33.         cf.enabled.value = false;
    34.  
    35.         DestroyImmediate(this);
    36.     }
    37. }
     
    Last edited: Aug 4, 2019