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.
FadeCameraShader
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
// Taken from: https://github.com/iBicha/Unity-Simple-Fade-Camera
// http://brahim.hadriche.me/
// Author: Brahim Hadriche
Shader "Hidden/FadeCameraShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_opacity ("Opacity", Range (0, 1)) = 0
_Color ("Fade Color", Color) = (0,0,0,1)
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
uniform sampler2D _MainTex;
uniform float _opacity;
uniform float4 _Color;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
col = col * _opacity + _Color * (1-_opacity);
return col;
}
ENDCG
}
}
}
FadeCamera.cs
using UnityEngine;
[ExecuteInEditMode]
// ReSharper disable once CheckNamespace
public class FadeCamera : MonoBehaviour
{
[Range(0f, 1f)]
public float opacity = 1;
public Color color = Color.black;
private Material _material;
private float _startTime;
private float _startOpacity = 1;
private int _endOpacity = 1;
private float _duration;
private bool _isFading;
private static readonly int Opacity = Shader.PropertyToID("_opacity");
public void FadeIn(float duration = 1)
{
_duration = duration;
_startTime = Time.time;
_startOpacity = opacity;
_endOpacity = 1;
_isFading = true;
}
public void FadeOut(float duration = 1)
{
_duration = duration;
_startTime = Time.time;
_startOpacity = opacity;
_endOpacity = 0;
_isFading = true;
}
void Awake()
{
_material = new Material(Shader.Find("Hidden/FadeCameraShader"));
}
void Start()
{
FadeIn();
}
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (_isFading && _duration > 0)
{
opacity = Mathf.Lerp(_startOpacity, _endOpacity, (Time.time - _startTime) / _duration);
// ReSharper disable once CompareOfFloatsByEqualityOperator
_isFading = opacity != _endOpacity;
}
// ReSharper disable once CompareOfFloatsByEqualityOperator
if (opacity == 1f)
{
Graphics.Blit(source, destination);
return;
}
_material.color = color;
_material.SetFloat(Opacity, opacity);
Graphics.Blit(source, destination, _material);
}
}