|
|
@ -0,0 +1,43 @@ |
|
|
|
uniform vec4 U_AmbientMaterial; |
|
|
|
uniform vec4 U_AmbientLight; |
|
|
|
uniform vec4 U_DiffuseMaterial; |
|
|
|
uniform vec4 U_DiffuseLight; |
|
|
|
uniform vec4 U_SpecularMaterial; |
|
|
|
uniform vec4 U_SpecularLight; |
|
|
|
uniform vec4 U_LightPos; |
|
|
|
uniform vec4 U_CameraPos; |
|
|
|
varying vec4 V_Normal; |
|
|
|
varying vec4 V_WorldPos; |
|
|
|
|
|
|
|
void main(){ |
|
|
|
//ambient |
|
|
|
vec4 ambientColor = U_AmbientMaterial * U_AmbientLight; |
|
|
|
|
|
|
|
//diffuse |
|
|
|
vec3 n = normalize(V_Normal.xyz); |
|
|
|
vec3 l = normalize(U_LightPos.xyz - V_WorldPos.xyz); |
|
|
|
float duffuseIntensity = max(0.0, dot(l,n)); |
|
|
|
|
|
|
|
//·½Ïò¹âË¥¼õ¼ÆËã |
|
|
|
float attenuation = 1.0; |
|
|
|
float distance = 0; |
|
|
|
float constantFactor = 0.5; |
|
|
|
float linearFactor = 0.3; |
|
|
|
float expFactor = 0.1; |
|
|
|
|
|
|
|
distance =length(U_LightPos.xyz - V_WorldPos.xyz); |
|
|
|
attenuation = 1.0 / (constantFactor + linearFactor*distance + expFactor*distance*distance); |
|
|
|
|
|
|
|
vec4 duffuseColor = U_DiffuseMaterial * U_DiffuseLight * duffuseIntensity * attenuation; |
|
|
|
|
|
|
|
//specular |
|
|
|
vec4 specularColor = vec4(0); |
|
|
|
if(duffuseIntensity > 0){ |
|
|
|
vec3 rd = normalize(reflect(-l,n)); |
|
|
|
vec3 vd = normalize((U_CameraPos - V_WorldPos).xyz); |
|
|
|
float specularIntensity = pow(max(0.0,dot(vd,rd)), 128); |
|
|
|
specularColor = U_SpecularMaterial * U_SpecularLight * specularIntensity; |
|
|
|
} |
|
|
|
|
|
|
|
gl_FragColor = ambientColor + duffuseColor + specularColor; |
|
|
|
} |