- Home /
How does AngryBots InterlacePatternBlend shader work?
The _Time variable is used to produce the animation, but what is _Time.xx and _InterlacePattern_ST.zw and how does this actually work?
I am also a little confused, how can both xy coordinates be calculated at same time?
Source from AngryBots for reference: #include "UnityCG.cginc"
sampler2D _MainTex;
sampler2D _InterlacePattern;
half4 _InterlacePattern_ST;
half4 _MainTex_ST;
fixed4 _TintColor;
struct v2f {
half4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
half2 uv2 : TEXCOORD1;
};
v2f vert(appdata_full v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv.xy = TRANSFORM_TEX(v.texcoord.xy, _MainTex);
o.uv2.xy = TRANSFORM_TEX(v.texcoord.xy, _InterlacePattern) + _Time.xx * _InterlacePattern_ST.zw;
return o;
}
fixed4 frag( v2f i ) : COLOR
{
fixed4 colorTex = tex2D (_MainTex, i.uv);
fixed4 interlace = tex2D (_InterlacePattern, i.uv2);
colorTex *= interlace;
return colorTex;
}
*NOTE: For some reason this website is adding http:// into the source below ... It is not in the question text! Additionally it seems to be ignoring any edits that I make. I couldn't even add this comment into the question body.
Answer by Owen-Reynolds · Feb 24, 2012 at 10:00 PM
_Time.xx means two copies of the time, bundled up as a Vector2. So if time is 7.4, then time.xx is (7.4,7.4). The x doesn't mean x-coordinate -- it just goes there. Imagine if you HAD to store your age in a Vector4 named A. The best you could do would be "A.x=age, and uh, ignore y, z and w." (NOTE: I didn't know _Time was built in -- does that work?)
The same way you can add a pair of Vector3's in Unity, graphics cards add side-by-side. The simplified line: uv2.xy += Time.xx; says to add time to uv.x and also to uv.y. This slides the uv coords right and up, which makes the texture move diagonal as you move.
If you took out Time, but added a line below: o.uv.x += _Time.x; then the texture slides sideways. Change to o.uv.y and you get up/down (adding time to the y-tex coord.)
I don't know what the interlace pattern is, but IP.zw says the last two slots. If the interlace pattern was (10, 6, 0.1, 0.7), then IP.zw is (0.1, 0.7) and it would slide the texture 1/10th of a tile L/R and 70% of a tile U/D (based on the unwrap.)
Cheers, that also makes several other things a lot clearer. So presumably _AnyFloat4.xyz will return a Float3 where the order can be customised _AnyFloat4.yzx if appropriate. I wondered why many shaders were doing this.
Jessy -- aarrg! I typed the letters IP, a dot, then zw and the parser decided to make it look like a link. Testing, do single quotes fix it: IP.zw.
Thanks on time. I use a unity manual page just like yours, but showing only the top half.
$$anonymous$$runch: yes. changing the order is called a swizzle. For example, uv.xy = uv.yx; is a short way to flip x/y tex coords.
Your answer