unity, TRANSFORM_TEX
TRANSFORM_TEX在UnityCG.cginc中定义。
----补充:
为啥buildin shader Unlit-Normal.shader中有一个float4 _MainTex_ST变量,现在就明白了,因为
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex)
就等价于
o.texcoord = v.texcoord.xy *_MainTex_ST.xy+_MainTex_ST.zw
即这句中用到了_MainTex_ST。
下面是一个两张贴图进行正片叠底(Multiply)的shader:
Shader "Custom/twoTexMultiply" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Tex2 ("Tex2", 2D) ="white" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 100
Pass {
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma multi_compile_fwdbase
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 vertex : SV_POSITION;
float2 texcoord : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
};
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _Tex2;
float4 _Tex2_ST;
v2f vert (appdata_full v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
o.texcoord1 = TRANSFORM_TEX(v.texcoord1,_Tex2);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.texcoord);
fixed4 col2=tex2D(_Tex2, i.texcoord1);
fixed4 finalCol=col*col2;
UNITY_OPAQUE_ALPHA(finalCol.a);
return finalCol;
}
ENDCG
}
}
}