You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

52 lines
1.3 KiB

uniform vec4 U_AmbientLightColor;
uniform vec4 U_AmbientMaterial;
uniform vec4 U_LightPos;
uniform vec4 U_DiffuseLightColor;
uniform vec4 U_DiffuseMaterial;
uniform vec3 U_EyePos;
uniform vec4 U_SpecularLightColor;
uniform vec4 U_SpecularMaterial;
varying vec3 V_Normal;
varying vec3 V_WorldPos;
void main()
{
//ambient
vec4 ambientColor = U_AmbientLightColor * U_AmbientMaterial;
//diffuse
vec3 L = vec3(0.0);
float distance = 0.0;
float attenuation = 1.0;
//light attribute
float constantFactor=0.5;
float linearFactor=0.3;
float expFactor=0.1;
if(U_LightPos.w==0.0){
//ÕâÊÇ·½Ïò¹â
L = normalize(U_LightPos.xyz);
} else {
//ÕâÊǵã¹âÔ´
//model point -> light pos
L = normalize(U_LightPos.xyz - V_WorldPos);
distance = length(U_LightPos.xyz - V_WorldPos);
attenuation = 1.0 / (constantFactor + linearFactor * distance + expFactor * distance * distance);
}
vec3 N = normalize(V_Normal);
float diffuseIntensity = max(0.0, dot(L,N));
vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * diffuseIntensity * attenuation;
//specular blin
vec3 viewDir = normalize(U_EyePos - V_WorldPos);
vec3 M = normalize(L + viewDir);
vec4 specularColor = U_SpecularLightColor * U_SpecularMaterial * pow(max(0.0, dot(M,N)), 64.0) * attenuation;
gl_FragColor = ambientColor + diffuseColor + specularColor;
}