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.

57 lines
1.7 KiB

4 years ago
  1. uniform vec4 U_AmbientMaterial;
  2. uniform vec4 U_AmbientLight;
  3. uniform vec4 U_DiffuseMaterial;
  4. uniform vec4 U_DiffuseLight;
  5. uniform vec4 U_SpecularMaterial;
  6. uniform vec4 U_SpecularLight;
  7. uniform vec4 U_LightPos;
  8. uniform vec4 U_CameraPos;
  9. uniform vec4 U_LightDirection;
  10. uniform vec4 U_LightOption;
  11. varying vec4 V_Normal;
  12. varying vec4 V_WorldPos;
  13. void main(){
  14. //ambient
  15. vec4 ambientColor = U_AmbientMaterial * U_AmbientLight;
  16. //diffuse
  17. vec3 n = normalize(V_Normal.xyz);
  18. vec3 l = normalize(U_LightPos.xyz - V_WorldPos.xyz);
  19. float duffuseIntensity = max(0.0, dot(l,n));
  20. //�۹�����
  21. float attenuation = 1.0;
  22. float distance = 0;
  23. float constantFactor = 0.5;
  24. float linearFactor = 0.3;
  25. float expFactor = 0.1;
  26. distance =length(U_LightPos.xyz - V_WorldPos.xyz);
  27. attenuation = 1.0 / (constantFactor + linearFactor*distance + expFactor*distance*distance);
  28. if(duffuseIntensity > 0){
  29. vec3 spot_direction = normalize(U_LightDirection.xyz);
  30. float currentCosThta = max(0.0, dot(-l, spot_direction));
  31. float radianCutoff = U_LightDirection.w * 3.14 / 180;
  32. float cosThta = cos(radianCutoff);
  33. if(currentCosThta > cosThta) {
  34. duffuseIntensity = pow(currentCosThta, U_LightOption.x)*U_LightOption.y;
  35. } else {
  36. duffuseIntensity = 0.0;
  37. }
  38. }
  39. vec4 duffuseColor = U_DiffuseMaterial * U_DiffuseLight * duffuseIntensity * attenuation;
  40. //specular
  41. vec4 specularColor = vec4(0);
  42. if(duffuseIntensity > 0){
  43. vec3 rd = normalize(reflect(-l,n));
  44. vec3 vd = normalize((U_CameraPos - V_WorldPos).xyz);
  45. float specularIntensity = pow(max(0.0,dot(vd,rd)), 128);
  46. specularColor = U_SpecularMaterial * U_SpecularLight * specularIntensity;
  47. }
  48. gl_FragColor = ambientColor + duffuseColor + specularColor;
  49. }