fragment/vertex shader - distorting UVs and the Grabpass
Two things shown here, with a bit of fiddling needed to make it actually work, which shouldn't be a problem if you've looked at all the other bits.
In the appdata struct we're including the UVs now, with TEXCOORD0 attribute.
In the v2f function, we use TRANSFORM_TEX(v.uv, _MainTex) to get our uv coordinates into screenspace. Once we have these values, we use the sin function to distort both the X&Y, or U & V!, multiplying the input by some properties we defined earlier. This is passed to the Frag shader, which displays "a" texture..
What I've not mentioned is the GrabPass -it essentially takes a snapshot of the framebuffer and stores it as a texture. The Transparent Queue tag stops it from going crazy & rendering into infinity (think of when you record a tv and feed the recording into the tv..
The GrabPass is defined as a sampler2D, which must be labelled as _GrabTexture- after that it can be used in a tex2D function as usual.
Shader "Dave/Unlit/vf_Mat"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_ScaleUVX("Scale UVX",Range(1,10))=1
_ScaleUVY("Scale UVY",Range(1,10)) = 1
}
SubShader
{
Tags{"Queue"="Transparent"}//we push the object to render last (or later), so the grab pass doesn't recursively go to black
GrabPass{}
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 _GrabTexture;//must be spelt this way
sampler2D _MainTex;
float4 _MainTex_ST;
float _ScaleUVX;
float _ScaleUVY;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.uv.x = sin(o.uv.x*_ScaleUVX);
o.uv.y = sin(o.uv.y*_ScaleUVY);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_GrabTexture, i.uv);
return col;
}
ENDCG
}
}
}
Comments
Post a Comment