Night Vision Post FX shader

This shader effect is intended for use in Torque 3D game engine.

This GLSL postFx shader is originally developed by Geeks3D. The problem with the original shader was they do not use a UV reconstruction. Therefore you get a circle only for square resolutions. This version uses a dynamic UV reconstruction and works for all monitors (4:3 and wide ones) and SM2 hardware. If you use this shader for commercial (or non-commercial) purposes, you must put credits to Liman3D and Geeks3D:

nightP.hlsl :

#include "shadergen:/autogenConditioners.h"
#include "./postFx.hlsl"
#include "../torque.hlsl"

uniform sampler2D backBuffer;
uniform sampler2D noiseTex;
uniform sampler2D maskTex;
uniform float accumTime;
uniform float4 rtParams0;


float4 main(PFXVertToPix IN) : COLOR0
{
float luminanceThreshold = 0.1;
float colorAmplification = 4.0;
float2 lensRadius = float2(0.45, 0.40);
float2 screen_center = float2(0.5, 0.5);
float4 finalColor;

float2 uv = float2(0,0);
uv.x = 0.4*sin(accumTime/4);
uv.y = 0.4*cos(accumTime/4);
float m = tex2D(maskTex, IN.uv0).r;
float3 n = tex2D(noiseTex,(IN.uv0*3.5) + uv);
float3 c = tex2D(backBuffer, IN.uv0 + (n.xy*0.005));

float lum = dot(float3(0.30, 0.59, 0.11), c);
if (lum < luminanceThreshold) c *= colorAmplification;

float3 visionColor = float3(0.1, 0.5, 0.1);
finalColor.rgb = (c + (n*0.2)) * visionColor * m;

// reconstruct the original resolution from the half texel correction
float resX = 1/(2*rtParams0.x);
float resY = 1/(2*rtParams0.y);

// screen ratio
float ratio = resX / resY;
float2 TC = IN.uv0;

// Center test (used for dubug purposes)
// if(TC.x >= 0.499 && TC.x <= 0.501) finalColor.rgb = float3(1,1,1);
// if(TC.y >= 0.499 && TC.y <= 0.501) finalColor.rgb = float3(1,1,1);

// re-scale to a circle
// keep in mind that unit UV directions are not equal !!!
TC.x *= ratio;

// shift back to center
// I don't have a widescreen monitor to test,but this should work out of the box
screen_center.x *= ratio;

float dist = distance(TC, screen_center);
finalColor.rgb *= smoothstep(lensRadius.x, lensRadius.y, dist);

return float4(finalColor.rgb,1);
}

add this code to "game\core\scripts\client\postFx.cs" :

singleton ShaderData( PFX_NightVisionShader )
{
DXVertexShaderFile = "shaders/common/postFx/postFxV.hlsl";
DXPixelShaderFile = "shaders/common/postFx/nightP.hlsl";
pixVersion = 2.0;
};

singleton GFXStateBlockData( NightVisionBlock : PFX_DefaultStateBlock )
{
samplersDefined = true;
samplerStates[0] = SamplerWrapLinear;
samplerStates[1] = SamplerWrapLinear;
samplerStates[2] = SamplerWrapLinear;
};
singleton PostEffect( NightVisionFx )
{
isEnabled = true;
allowReflectPass = false;

renderTime = "PFXAfterBin";
renderBin = "AL_FormatToken_Pop";

shader = PFX_NightVisionShader;
stateBlock = NightVisionBlock;
texture[2] = "postFx/NightMask.png";
texture[1] = "postFx/NightMask.png";
texture[0] = "$backBuffer";
renderPriority = 10;
};

As a result you will achieve this effect:

Download the tutorials files from here.

back...