【ShaderToy中图形效果转译到UnityShaderlab案例分享,实现三原色型变效果_Chromatic Aberration】

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
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暴走约伯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值