top of page
Writer's picturesantosh nalla

Post Processing Effect: Night Vision Goggles

Make sure you read my previous blog on Introduction to Post Processing before you move on to this.


Now here in this blog, using the same post processing effect for an image, i am gonna generate night vision goggles effect. So basically when you hold "A", you get those cool goggles, which are visible even in the dark.


As i have already explained, you need this script attached to main camera. The only thing added on to this script is the condition to use the post processing materials on holding "A" key.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MainCameraScriptPP : MonoBehaviour
{
    public Material PostProcessingMaterial;
    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (Input.GetKey(KeyCode.A))  //Activating night vision effect
        {
            Graphics.Blit(source, destination, PostProcessingMaterial);
        }
        else
        {
            Graphics.Blit(source, destination);
        }
    }
}

Now the main part is to write the code in fragment shader. This is pretty simple.


fixed4 frag(vertexOutput i) : SV_TARGET
				{
	float3 rendTex = tex2D(_MainTex,i.uv).rgb;
	float3 tintColor = fixed3(0.6, 1, 0.6);
	float3 greyScale = (rendTex.r + rendTex.g + rendTex.b) / 3;
	loat3 tinted = greyScale * tintColor;

	return fixed4(tinted * 10, 1);
				
				}

So the rendered image color for each pixel is obviously given by tex2D(_MainTex,i.uv).rgb.

Now for this rendered image, when you multiplay greyScale( greyScale = (rendTex.r + rendTex.g + rendTex.b) / 3) and TintColor(float3 tintColor = fixed3(0.6, 1, 0.6)), you get that night vision effect which we want.

There are many algorithms which are available for each image filter effect you apply. This is related to image processing domain not shaders. So once you understand the basic steps, you can modify image as you wish.



Code :

Shader "ShaderLearning/PostProcessing_NightVision"
{
	Properties
	{
		//MainTex is the keywork lol :D
		_MainTex("Basic Texture", 2D) = "white" {}
     	_Color("Main Color", Color) = (1,1,1,1)
	}


		Subshader
	{

		  Pass
			{

				CGPROGRAM

				 #pragma vertex vert
				 #pragma fragment frag

	
			   sampler2D _MainTex;
		       half4 _Color;

				struct vertexInput
				{
					float4 vertex: POSITION;
					float2 uv: TEXCOORD0;
				};

				struct vertexOutput
				{
					float4 pos : SV_POSITION;
					float2 uv: TEXCOORD0;
				};

				vertexOutput vert(vertexInput v)
				{
					vertexOutput o;
					o.pos = UnityObjectToClipPos(v.vertex);
					o.uv = v.uv;
					return o;
				}


				fixed4 frag(vertexOutput i) : SV_TARGET
				{
					float3 renderedTex = tex2D(_MainTex,i.uv).rgb;
					float3 tintColor = fixed3(0.6, 1, 0.6);
					float3 greyScale = (renderedTex.r + renderedTex.g + renderedTex.b) / 3;
					float3 tinted = greyScale * tintColor;

					return fixed4(tinted * 10, 1);
				
				}

			   ENDCG
		   }

	}
}



20 views0 comments

Recent Posts

See All

Comments


bottom of page