ShaderToy内的原码:
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// distance from center of image, used to adjust blur
vec2 uv = fragCoord.xy / iResolution.xy;
float d = length(uv - vec2(0.5,0.5));
// blur amount
float blur = 0.0;
blur = (1.0 + sin(iTime*6.0)) * 0.5;
blur *= 1.0 + sin(iTime*16.0) * 0.5;
blur = pow(blur, 3.0);
blur *= 0.05;
// reduce blur towards center
blur *= d;
// final color
vec3 col;
col.r = texture( iChannel0, vec2(uv.x+blur,uv.y) ).r;
col.g = texture( iChannel0, uv ).g;
col.b = texture( iChannel0, vec2(uv.x-blur,uv.y) ).b;
// scanline
float scanline = sin(uv.y*800.0)*0.04;
col -= scanline;
// vignette
col *= 1.0 - d * 0.5;
fragColor = vec4(col,1.0);
}
类似技术分享这里有
【着色器实现Tricolor三原色型变效果_Shader效果第十八篇】
在Unity内的效果和代码:
Shader"ShaderToy/ChromaticAberration"
{
Properties
{
_MainTex("MainTex", 2D) = "white"{}
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
sampler2D _MainTex;
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float4 screenCoord : TEXCOORD1;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.screenCoord.xy = ComputeScreenPos(o.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed2 uv = i.uv.xy;
fixed amount = 0.0;
amount = (1.0 + sin(_Time.y*6.0)) * 0.5;
amount *= 1.0 + sin(_Time.y*16.0) * 0.5;
// amount *= 1.0 + sin(_Time.y*19.0) * 0.5;
// amount *= 1.0 + sin(_Time.y*27.0) * 0.5;
amount = pow(amount, 3.0);
amount *= 0.05;
fixed4 col;
col.r = tex2D( _MainTex, fixed2(uv.x+amount,uv.y) ).r;
col.g = tex2D( _MainTex, uv ).g;
col.b = tex2D( _MainTex, fixed2(uv.x-amount,uv.y) ).b;
col.a = tex2D( _MainTex, uv ).a;
fixed scanline = sin(uv.y*800.0)*0.04;
col -= scanline;
col *= (1.0 - amount * 0.5);
return fixed4(col);
}
ENDCG
}
}
}