I came across this beautiful shader from shadertoy which I am trying to implement in Unity. I managed to get the blur box background and also the random dots get generated. But I am not able to get the curl effect as shown in the original shader. Here is my code, where _MainText
takes the default colour black and _MainTexBg
has this texture attached,
Shader "Unlit/MyFirstShader"
{
Properties
{
_MainTex ("Texture", 2D) = "black" {}
_MainTexBg ("Curl Pattern Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
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_ST;
sampler2D _MainTexBg;
float4 _MainTexBg_ST;
float2 hash2( float n )
{
return frac(sin(float2(n,n+1.0))*float2(43758.5453123,22578.1459123));
}
// smoothstep interpolation of texture
float4 ssamp( float2 uv, float oct )
{
uv = uv.xy / oct;
//return texture( iChannel0, uv, -10.0 );
float texSize = 8.;
float2 x = uv * texSize - .5;
float2 f = frac(x);
// remove fractional part
x = x - f;
// apply smoothstep to fractional part
f = f*f*(3.0-2.0*f);
// reapply fractional part
x = x + f;
uv = (x+.5) / texSize;
return tex2D( _MainTexBg, uv );
}
float2 e = float2(1./256., 0.);
float4 dx( float2 uv, float oct )
{
return (ssamp(uv+e.xy,oct) - ssamp(uv-e.xy,oct)) / (2.*e.x);
}
float4 dy( float2 uv, float oct )
{
return (ssamp(uv+e.yx,oct) - ssamp(uv-e.yx,oct)) / (2.*e.x);
}
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
i.uv=1-i.uv;
fixed4 col = tex2D(_MainTex, i.uv);
col=mul(col,2.0);
col = smoothstep(0.,1.,col);
col.xyz = 1.-col.xyz;
col.xyz = mul(col.xyz,pow(1. - 1.9*dot(i.uv-.5,i.uv-.5),.07));
float4 res = float4(0.,0.,0.,0.);
float scl = _ScreenParams.x/640.;
// random paint drops
//float fr = float(_Time.y);
float period = _Time.y < 2.9 ? 30. : _Time.y < 47. ? 8. : 3.;
float2 sparkPos = hash2(_Time.y+1.11) * _ScreenParams.xy;
if( length(sparkPos-i.vertex)<5.*scl && i.vertex.x > 1. && i.vertex.y > 1. )
{
// everyones favourite colour gradient
res = res + 2.5*float4(i.uv,0.5+0.5*sin(_Time.y),1.0);
}
float2 off = 0.* (float2(128.,128.)/_ScreenParams.xy) * unity_DeltaTime;
float oct = .25;
float2 curl1 = .001*float2( dy(i.uv,oct).x, -dx(i.uv,oct).x )*oct;
oct = 5.; float sp = 0.1;
curl1 = curl1 + .0002*float2( dy(i.uv+sp*_Time.y,oct).x, -dx(i.uv+sp*_Time.y,oct).x )*oct;
off = off + curl1;
off = mul(off,.4);
res = res + .999*tex2D( _MainTexBg, i.uv - off);
return col*res;
}
ENDCG
}
}
}
I am very new to shader programming. Thanks for your help.