标签:
Shader "Sbin/vf48" { Properties { _MainColor("MainColor",color) = (1,1,1) _SpecalarColor("SpecularColor",color) = (1,1,1) _Shininess("Shininess",Range(1,94)) = 8 } SubShader { pass{ Tags{"LightMode" = "ForwardBase"} CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" #include "Lighting.cginc" struct v2f{ float4 pos : POSITION; float3 normal:NORMAL; float4 vertex:COLOR; }; float4 _MainColor; float4 _SpecalarColor; float _Shininess; v2f vert(appdata_base v){ v2f o; o.pos = mul(UNITY_MATRIX_MVP,v.vertex); o.normal = v.normal; o.vertex = v.vertex; return o; } fixed4 frag(v2f v):COLOR { //Ambient Color 环境光 float4 col = UNITY_LIGHTMODEL_AMBIENT; float3 N = UnityObjectToWorldNormal(v.normal); float3 L = normalize(WorldSpaceLightDir(v.vertex)); float3 V = normalize(WorldSpaceViewDir(v.vertex)); //deffuse Color 漫反射 float ndot = saturate(dot(N,L));//saturate把点积的结果限定在[0-1] col += _LightColor0*_MainColor * ndot; //Specular Color镜面高光 float3 R = 2*dot(N,L)*N-L; R = normalize(R); //dot:向量点积 float specularScale = pow(saturate(dot(R,V)),_Shininess);//pow:数的n次幂 col += _SpecalarColor * specularScale; //点光源 float3 wpos = mul(_Object2World,v.vertex).xyz; col.rgb += Shade4PointLights(unity_4LightPosX0,unity_4LightPosY0,unity_4LightPosZ0 ,unity_LightColor[0].rgb,unity_LightColor[1].rgb,unity_LightColor[2].rgb,unity_LightColor[3].rgb, unity_4LightAtten0,wpos,N); return col; } ENDCG } } }
Cg入门16:Fragment shader - 片段级光照
标签:
原文地址:http://blog.csdn.net/aa4790139/article/details/50964329