### [置頂] Cg入門16：Fragment shader - 片段級光照

`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}} }`