Shader "Unity Shaders Book/Chapter 11/Image Sequence Animation" {
Properties {
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_MainTex ("Image Sequence", 2D) = "white" {}
_HorizontalAmount ("Horizontal Amount", Float) = 4
_VerticalAmount ("Vertical Amount", Float) = 4
_Speed ("Speed", Range(1, 100)) = 30
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
Pass {
Tags { "LightMode"="ForwardBase" }
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
float _HorizontalAmount;
float _VerticalAmount;
float _Speed;
struct a2v {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
v2f vert (a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target {
float time = floor(_Time.y * _Speed);
float row = floor(time / _HorizontalAmount);
float column = time - row * _HorizontalAmount;
half2 uv = i.uv + half2(column, -row);
uv.x /= _HorizontalAmount;
uv.y /= _VerticalAmount;
fixed4 c = tex2D(_MainTex, uv);
c.rgb *= _Color;
return c;
}
ENDCG
}
}
FallBack "Transparent/VertexLit"
}
代码如上,对于frag里的计算部分
1.这句
half2 uv = i.uv + half2(column, -row);
这里如果去掉i.uv会怎么样,
2.这两句
uv.x /= _HorizontalAmount;
uv.y /= _VerticalAmount;
首先想问下,这里的/=是和c#里的/=一样吧
还有,shader里这个frag是每帧都调用吗,如果是的话,这个uv的x和y不是一直在除。。。那得到的值也就不对了啊
3.没了。。。
1,这里片元函数是要对每一个片元,可以认为对每一个像素要做处理,所以要加上i.uv就是这个像素原来所对应的uv坐标点,在这个基础上再去做偏移处理,去掉的话,应该会得到一个单一的色调
2,是一样的, frag不是每帧调用 ,frag是对每一个要绘制到屏幕上的像素调用一次