x


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 * http://_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;
}
more ▼

asked Feb 24 '12 at 04:57 PM

numberkruncher gravatar image

numberkruncher
2.2k 37 46 59

*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.

Feb 24 '12 at 05:51 PM numberkruncher
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

_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 http://IP.zw says the last two slots. If the interlace pattern was (10, 6, 0.1, 0.7), then http://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.)

more ▼

answered Feb 24 '12 at 10:00 PM

Owen Reynolds gravatar image

Owen Reynolds
12.2k 1 7 46

About _Time. Your links are broken.

Feb 24 '12 at 10:20 PM Jessy

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.

Feb 24 '12 at 11:20 PM numberkruncher

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.

Krunch: yes. changing the order is called a swizzle. For example, uv.xy = uv.yx; is a short way to flip x/y tex coords.

Feb 25 '12 at 05:37 PM Owen Reynolds
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3935
x1744
x13

asked: Feb 24 '12 at 04:57 PM

Seen: 787 times

Last Updated: Feb 25 '12 at 05:37 PM