|
|
@ -15,50 +15,46 @@ varying vec3 V_WorldPos; |
|
|
|
|
|
|
|
void main() |
|
|
|
{ |
|
|
|
//角度转弧度 |
|
|
|
float radianCutoff = U_Cutoff * 3.14 / 180; |
|
|
|
float cosThta = cos(radianCutoff); |
|
|
|
vec3 spotLightDirection = normalize(U_LightDirection.xyz); |
|
|
|
|
|
|
|
//common |
|
|
|
vec3 N = normalize(V_Normal); |
|
|
|
|
|
|
|
//ambient |
|
|
|
vec4 ambientColor = U_AmbientLightColor * U_AmbientMaterial; |
|
|
|
|
|
|
|
//diffuse |
|
|
|
vec3 L = vec3(0.0); |
|
|
|
float distance = 0.0; |
|
|
|
float attenuation = 1.0; |
|
|
|
|
|
|
|
//light attribute |
|
|
|
float diffuseIntensity = 0.0; |
|
|
|
if(U_LightPos.w == 0 && U_Cutoff == 0){ |
|
|
|
// 平行光 |
|
|
|
L = normalize(U_LightPos.xyz - vec3(0.0)); |
|
|
|
} else { |
|
|
|
// 点光 |
|
|
|
L = normalize(U_LightPos.xyz - V_WorldPos); |
|
|
|
float distance = length(U_LightPos.xyz - V_WorldPos); |
|
|
|
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); |
|
|
|
} |
|
|
|
|
|
|
|
//角度转弧度 |
|
|
|
float radianCutoff = U_Cutoff * 3.14 / 180; |
|
|
|
float cosThta = cos(radianCutoff); |
|
|
|
vec3 spotLightDirection = normalize(U_LightDirection.xyz); |
|
|
|
float currentThta = max(0.0, dot(-L, spotLightDirection)); |
|
|
|
|
|
|
|
L = normalize(-L); |
|
|
|
|
|
|
|
vec3 N = normalize(V_Normal); |
|
|
|
|
|
|
|
|
|
|
|
float currentCosThta = max(0.0, dot(L,spotLightDirection)); |
|
|
|
|
|
|
|
float diffuseIntensity = 0.0; |
|
|
|
|
|
|
|
if(currentCosThta > cosThta){ |
|
|
|
diffuseIntensity = pow(currentCosThta, U_LightDirection.w); |
|
|
|
if(currentThta > cosThta){ |
|
|
|
diffuseIntensity = pow(currentThta, U_LightDirection.w); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * diffuseIntensity * attenuation *4.0; |
|
|
|
vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * max(0.0, dot(L,N)) * attenuation * diffuseIntensity; |
|
|
|
|
|
|
|
//specualr |
|
|
|
vec3 RD = normalize(reflect(-L,N)); |
|
|
|
vec3 VD = normalize(U_EyePos - V_WorldPos); |
|
|
|
vec4 specularColor = U_SpecularLightColor * U_SpecularMaterial * pow(max(0.0, dot(RD,VD)) ,128); |
|
|
|
|
|
|
|
gl_FragColor = ambientColor + diffuseColor; |
|
|
|
gl_FragColor = ambientColor + diffuseColor + specularColor; |
|
|
|
} |