I’ve attached a shader to a screen renderer, however, the parts that should be alpha, are completely black and I can’t see what should be on the camera (Culling Mask: Everything).
The shader worked perfectly before updating Unity to 5.6 (I was on 5.4 before and now I’m on 2017). I have no idea why it’s not working and a friend of mine used it and said it was working on his end. Did something update behind the scenes I’m not seeing that changed the way alphas work in Unity?
Here’s the code for my screen renderer:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class ScreenRenderer : MonoBehaviour
{
public Material mat;
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Graphics.Blit(source, destination, mat);
}
}
And here’s the shader I’m using:
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Custom/ScreenRenderer_Circle"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color("Color", Color) = (0,0,0,0)
_OffX("Offset X", Float) = 0
_OffY("Offset Y", Float) = 0
_Radius ("Radius", Range(0, 2.5)) = 0
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
Tags {
"Queue" = "Transparent"
}
Blend SrcAlpha OneMinusSrcAlpha
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;
};
sampler2D _MainTex;
float4 _Color;
float _Radius, _OffX, _OffY;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
float4 frag (v2f i) : SV_Target
{
float2 screen = normalize(_ScreenParams.xy);
float X = (_OffX - i.uv.x) * screen.x, Y = (_OffY - i.uv.y) * screen.y;
float uvDist = (X * X) + (Y * Y);
float cenDist = (_Radius * _Radius) - uvDist;
float alphaVal = (cenDist > uvDist) ? 1 : 0;
return _Color - alphaVal;
}
ENDCG
}
}
}