- Blinn-Phong光照模型
- 直射光 Specular = 直射光 * pow( max(cosθ,0),10) θ:是法线和x的夹角 x 是平行光和视野方向的平分线
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject' // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject' Shader "LXC/09 Specular Fragment BlinnPhong" { Properties{ _Diffuse("Diffuse Color",Color) = (1,1,1,1) _Gloss("Gloss",Range(8,200)) = 10 _Specular("Specular Color",Color)=(1,1,1,1) } SubShader { Pass{ Tags{ "LightMode" = "ForwardBase" } CGPROGRAM #include "Lighting.cginc" #pragma vertex vert #pragma fragment frag fixed4 _Diffuse; half _Gloss; fixed4 _Specular; struct a2v { float4 vertex:POSITION; float3 normal:NORMAL; }; struct v2f { float4 position:SV_POSITION; float3 worldNormal : TEXCOLOR0; float3 worldVertex : TEXCOLOR1; }; v2f vert(a2v v) { v2f f; f.position = UnityObjectToClipPos(v.vertex); f.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject); f.worldVertex = mul(v.vertex, unity_WorldToObject).xyz; return f; } fixed4 frag(v2f f) :SV_Target{ fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb; fixed3 normalDir = normalize(f.worldNormal); fixed3 lightDir = normalize(_WorldSpaceLightPos0.xyz); fixed3 diffuse = _LightColor0.rgb * max(dot(normalDir, lightDir), 0)*_Diffuse.rgb; //fixed3 reflectDir = normalize(reflect(-lightDir, normalDir)); fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - f.worldVertex); //获取平行光和视野方向的平分线 fixed3 halfDir = normalize(viewDir + lightDir); //两个向量相加单位化就是平分线 fixed3 specular = _LightColor0.rgb * pow(max(dot(normalDir, halfDir), 0), _Gloss) * _Specular.rgb; fixed3 tempColor = diffuse + ambient + specular; return fixed4(tempColor,1); } ENDCG } } Fallback "Diffuse" }
Shader-Blinn-Phong高光反射
最新推荐文章于 2022-10-19 15:36:23 发布