首先先看下代码内容:
Shader "WSP/FrameWork7"{Properties{_Tex("角色贴图",2D) = "White" {}_Color("被击中颜色",Color) = (0,0,0,0)}SubShader{pass{CGPROGRAM#pragma vertex vert#pragma fragment frag#include "UnityCG.cginc"sampler2D _Tex;fixed4 _Color;struct appdata{float4 vertex :POSITION;float4 uv :TEXCOORD;};struct v2f{float4 pos :POSITION;float4 uv :TEXCOORD;};v2f vert(appdata v){v2f o;o.pos = UnityObjectToClipPos(v.vertex);o.uv = v.uv;return o;}fixed4 frag(v2f i):SV_TARGET{fixed4 c;fixed4 tex = tex2D(_Tex,i.uv);c = tex;c += _Color;return c;}ENDCG}}}
{
fixed4 c;
fixed4 tex = tex2D(_Tex,i.uv);
c = tex;
c += _Color;
return c;
}
结论:
1.“=”就是基本运算符,比如还有“+=”。”c += _Color;” = “c = c + _Color”
2.“=”就是赋值属性。 fixed4 tex = tex2D(_Tex,i.uv); 就是把 等于号后面的值“tex2D(_Tex,i.uv);”赋予给等于号前面的变量fixed4 tex。
3.“c = tex;”就是一个引用的转换关系。把变量tex的值转换给c。那么就会有问题要问了:岂不是 “c = tex2D(_Tex,i.uv) = tex” ?
fixed4 tex = tex2D(_Tex,i.uv);
c = tex2D(_Tex,i.uv);
但是这样会使片断着色器进行两次贴图采样,前面笔记有提到过多次片断着色器的贴图采样会造成极大的运算性能浪费。
回顾纹理采样三步骤:
1. _Name(“Display”,2D) = “White” {} “属性定义”
2. Sampler _Name; “CG中声明”
3. tex2D(_Name,uv); “着色器中采样”
因此通过引用的方式获得需要存储的变量”c”
4.”+=” “-=” “*=”等都属于简写的赋值/基本运算符。
