标签:
高光或者说镜面反射,是光线经过物体表面,反射到视野中,当反射光线与人的眼睛看得方向平行时,强度最大,夹角为90度时,强度最小。
人眼睛的位置: float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz- i.posWorld.xyz); 都在世界坐标,_WorldSpaceCameraPos.xyz是摄像机位置,i.posWorld.xyz是顶点在世界坐标的位置;
float3 specularReflection = atten *_LightColor0.xyz * _SpecColor.rgb*max(0.0, dot(normalDirection,lightDirection))*pow(max(0,dot(reflect(-lightDirection,normalDirection),viewDirection)),_Shininess);
atten:光强系数
_LightColor0:光源颜色
_SpecColor:高光颜色
max(0.0, dot(normalDirection,lightDirection)):光源强度
pow(max(0,dot(reflect(-lightDirection,normalDirection),viewDirection)),_Shininess):高光强度
高光是建立在Lambert光照模型上的,加入Lambert光照结果为diffuseRefliction:
float3 lightFinal = diffuseReflection +specularReflection+ UNITY_LIGHTMODEL_AMBIENT.xyz;
return float4(lightFinal*_Color.rgb,1.0);
源代码:
Shader "JQM/Specular" { Properties { _Color("Color", color) = (1.0,1.0,1.0,1.0) _SpecColor("Specular Color", color) = (1.0,1.0,1.0,1.0) _Shininess("Shininess",float) = 10 } SubShader{ Pass{ Tags { "LightMode" = "ForwardBase"} CGPROGRAM #pragma vertex vert #pragma fragment frag //使用自定义变量 uniform float4 _Color; uniform float4 _SpecColor; uniform float _Shininess; //使用Unity定义的变量 uniform float4 _LightColor0; struct vertexInput{ float4 vertex:POSITION; float3 normal:NORMAL; }; struct vertexOutput{ float4 pos:SV_POSITION; float4 posWorld:TEXCOORD0; float3 normalDir:TEXCOORD1; }; //顶点程序 vertexOutput vert(vertexInput v) { vertexOutput o; o.posWorld = mul(_Object2World, v.vertex); o.normalDir = normalize( mul(float4(v.normal,0.0),_World2Object).xyz); o.pos = mul(UNITY_MATRIX_MVP, v.vertex); return o; } //片段程序 float4 frag(vertexOutput i):COLOR { float3 normalDirection = i.normalDir; float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz- i.posWorld.xyz); float3 lightDirection; float atten = 1.0; lightDirection = normalize(_WorldSpaceLightPos0.xyz); float3 diffuseReflection = atten * _LightColor0.xyz * max(0.0, dot(normalDirection,lightDirection)); float3 specularReflection = atten * _LightColor0.xyz * _SpecColor.rgb*max(0.0, dot(normalDirection,lightDirection))*pow(max(0,dot(reflect(-lightDirection,normalDirection),viewDirection)),_Shininess); float3 lightFinal = diffuseReflection +specularReflection+ UNITY_LIGHTMODEL_AMBIENT.xyz; return float4(lightFinal*_Color.rgb,1.0); } ENDCG } } }
标签:
原文地址:http://www.cnblogs.com/jqm304775992/p/4890634.html