Hi all,
Thanks for reading (and sorry if this is a bit wordy), I’ve had a Google and a think on this but can’t seem to work out what’s happening. I’m new to shaders and after a few tutorials have started to try to write my own, and I’ve started by trying to attempt to emulate old VHS/CRT effects for a game I’m planning. So far I’ve done a very basic colour blur/bleed effect, which works, and I’ve set it as the render material for the Main Camera using SetReplacementShader() as I’m intending to use it as an image effect. The shader code is here: Shader source - Pastebin.com
It works to a degree, and all the objects in the scene are rendered with their colours displaced, but it seems to be sampling on a per-texture basis rather than sampling from the whole screen as I had expected. This means that the effect on a 50x50 texture is way more obvious (and less convincing) than on a 500X500 texture, and scaling the images down or up doesn’t change the effect size, just makes the pixels look really blocky. The effect also abruptly cuts off at the edge of the sprite instead of having it bleed over into the background which is what I was expecting.
I wanted the effect to apply to the whole screen equally rather than varying on each texture, and I thought by applying it to the camera it would do so. Is there something I’ve missed/fundamentally not understood (more likely!) I am suspicious that _MainTex needs to be set to a render texture or something but can’t for the life of me work out how to capture a render texture, apply the effect, then display the result.
Any help would be greatly appreciated!
Thanks
You would want to do this as an image effect, otherwise this is just going to be changing the albedo texture per object. The shader would need to be more along the lines of;
Shader "Hidden/ColourBleedShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Pass
{
Cull Off
ZWrite Off
ZTest Always
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 _MainTex_TexelSize;
v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.uv;
return o;
}
float4 colorBleed (v2f i, sampler2D mainTexture, float4 texelSize)
{
fixed4 tex = tex2D(_MainTex, i.uv);
float2 RoffsetUV = float2(i.uv.x - texelSize.x*2, i.uv.y);
float2 BoffsetUV = float2(i.uv.x + texelSize.x*2, i.uv.y);
fixed4 rTex = tex2D(_MainTex, RoffsetUV);
fixed4 bTex = tex2D(_MainTex, BoffsetUV);
rTex = float4 (rTex.r, 0, 0, 0.5);
bTex = float4 (0, 0, bTex.b, 0.5);
tex = float4 (0, tex.g, 0, 0.5);
float2 myuv = float2(i.uv.x+_MainTex_TexelSize.x*5, i.uv.y+_MainTex_TexelSize.x*20);
fixed4 endTex = float4 (rTex + bTex + tex);
return endTex;
}
fixed4 frag (v2f i) : SV_Target
{
float4 finalTex = colorBleed(i, _MainTex, _MainTex_TexelSize);
return finalTex;
//TODO: make picture a bit blurrier, add grain effect, scanlines? CRT screen bend?
}
ENDCG
}
}
}
And a script to use this as an image effect to attach to your camera (the following is JS, I can change if you wish).
#pragma strict
@script ExecuteInEditMode
@script AddComponentMenu ("Image Effects/Custom/Colour Bleed")
private var mat : Material;
private var shader : Shader;
function OnRenderImage (src : RenderTexture, dest : RenderTexture)
{
if (!shader)
{
shader = Shader.Find ("Hidden/ColourBleedShader");
}
if (!mat)
{
mat = new Material (shader);
}
Graphics.Blit (src, dest, mat);
}