添加uv贴图

Shader "Custom/s7" {
Properties{
_Diffuse("diffuse color",Color) = (1,1,1,1)
_MainTex("Main Tex",2D) = "white"{}
_Specular("specular color",Color) = (1,1,1,1)
_Gloss("gloss",Range(1,40)) = 10
}
SubShader{
Pass{
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#include "Lighting.cginc"
#pragma vertex vert
#pragma fragment frag


fixed4 _Diffuse;
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Specular;
half _Gloss;


struct a2v {
float3 normal:NORMAL;
float4 vertex:POSITION;
float4 texcoord:TEXCOORD;
};
struct v2f {
float4 svPos : SV_POSITION;
float4 worldVertex:TEXCOORD1;
float3 worldNormal:TEXCOORD0;
float2 uv:TEXCOORD2;
};


v2f vert(a2v v) {
v2f f;
f.svPos = mul(UNITY_MATRIX_MVP, v.vertex);
f.worldNormal = UnityObjectToWorldNormal(v.normal);
f.worldVertex = mul(v.vertex, unity_WorldToObject);
f.uv = v.texcoord.xy*_MainTex_ST.xy+_MainTex_ST.zw;
return f;
}


fixed4 frag(v2f f) :SV_Target{

fixed3 normalDir = normalize(f.worldNormal);


fixed3 lightDir = normalize(WorldSpaceLightDir(f.worldVertex));


fixed3 texColor = tex2D(_MainTex, f.uv.xy);


fixed3 diffuse = _Diffuse*_LightColor0.rgb * texColor * max(0, dot(normalDir, lightDir));




fixed3 viewDir = normalize(UnityWorldSpaceViewDir(f.worldVertex));




fixed3 halfDir = normalize(viewDir + lightDir) ;


fixed3 specular = _LightColor0.rgb*_Specular.rgb*pow(max(dot(normalDir, halfDir), 0),_Gloss);


fixed3 tempcolor = diffuse + specular + UNITY_LIGHTMODEL_AMBIENT.rgb;


return fixed4(tempcolor,1);
}


ENDCG
}
}
FallBack "Diffuse"
}

你可能感兴趣的:(shader学习)