r/Unity3D•
Nyoox

Help with shader

Question

Hello, can someone help me colorize this camera effect? It is a circular fade in/out shader that closes/opens the scene when triggered.

I Don't know anything about shader so I would like to assign a specific color to this effect dynamically via code. Now it is only and always black.

Thank you!

P.S: I found it in a Github repository, thank you "Ellioman"

Shader "Ellioman/BlackCircle"
{
Properties 
{
	_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader 
{
	Pass
	{
		ZTest Always
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#pragma fragmentoption ARB_precision_hint_fastest
		#pragma target 3.0
		#pragma glsl
		#include "UnityCG.cginc"
		
		
		uniform sampler2D _MainTex;
		uniform fixed4 _Color;
		uniform float _Size;
		uniform float4 _ScreenResolution;
		uniform float _PositionX;
		uniform float _PositionY;
		
	       struct appdata_t
        {
            float4 vertex   : POSITION;
            float4 color    : COLOR;
            float2 texcoord : TEXCOORD0;
        };

        struct v2f
        {
              half2 texcoord  : TEXCOORD0;
              float4 vertex   : SV_POSITION;
              fixed4 color    : COLOR;
       };   


		v2f vert(appdata_t IN)
        {
            v2f OUT;
            OUT.vertex = UnityObjectToClipPos(IN.vertex);
            OUT.texcoord = IN.texcoord;
            OUT.color = IN.color;
            
            return OUT;
        }
		
		float4 frag (v2f i) : COLOR
		{
			float2 uv = i.texcoord.xy;
			float2 center = float2(_PositionX * _ScreenResolution.x, _PositionY * _ScreenResolution.y);
			float light = (1.0 - clamp(distance(center.xy, (i.texcoord.xy * _ScreenResolution.xy)) - _Size + 1, 0.0, 1.0));

			return tex2D(_MainTex, uv) * light;
		}
		
		ENDCG
	}
	
}

}

Posted May 11, 2017, 7:20 AM UTC

03
Matt3088•

You need to do a few things:

  1. In the shader, change return tex2D(_MainTex, uv) * light; to return tex2D(_MainTex, uv) * light + (1.0 - light) * _Color;

  2. In the CameraFader.cs script file, add

     CircleMaterial.SetVector("_Color", Color.white);
    

To line 156. You can of course then create your own color variable in the class and use it instead of the white color.

2
NyooxOPHobbyist•

thank you Matt3088, that was extremely helpful!

1
Matt3088•

No problem :)

1