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.

69 lines
1.8 KiB

4 years ago
  1. uniform vec4 U_AmbientLightColor;
  2. uniform vec4 U_AmbientMaterial;
  3. uniform vec4 U_LightPos;
  4. uniform vec4 U_LightDirection;
  5. uniform float U_Cutoff;
  6. uniform float U_DiffuseIntensity;
  7. uniform vec4 U_DiffuseLightColor;
  8. uniform vec4 U_DiffuseMaterial;
  9. uniform vec3 U_EyePos;
  10. uniform vec4 U_SpecularLightColor;
  11. uniform vec4 U_SpecularMaterial;
  12. varying vec3 V_Normal;
  13. varying vec3 V_WorldPos;
  14. void main()
  15. {
  16. //common
  17. vec3 N = normalize(V_Normal);
  18. //ambient
  19. vec4 ambientColor = U_AmbientLightColor * U_AmbientMaterial;
  20. //diffuse
  21. vec3 L = vec3(0.0);
  22. float attenuation = 1.0;
  23. float diffuseIntensity = 0.0;
  24. if(U_LightPos.w != 0){// ����
  25. L = normalize(U_LightPos.xyz - V_WorldPos);
  26. float distance = length(U_LightPos.xyz - V_WorldPos);
  27. float constantFactor=0.5;
  28. float linearFactor=0.3;
  29. float expFactor=0.1;
  30. attenuation = 1.0 / (constantFactor + linearFactor * distance + expFactor * distance * distance);
  31. if( U_Cutoff > 0){//�۹�
  32. //�Ƕ�ת����
  33. float radianCutoff = U_Cutoff * 3.14 / 180;
  34. float cosThta = cos(radianCutoff);
  35. vec3 spotLightDirection = normalize(U_LightDirection.xyz);
  36. float currentThta = max(0.0, dot(-L, spotLightDirection));
  37. if(currentThta > cosThta){
  38. if(dot(L,N)>0.0)
  39. {
  40. diffuseIntensity=pow(currentThta,U_LightDirection.w);
  41. }
  42. }
  43. } else {
  44. diffuseIntensity = max(0.0, dot(L,N));
  45. }
  46. } else {
  47. // ƽ�й�
  48. L = normalize(U_LightPos.xyz - vec3(0.0));
  49. diffuseIntensity = max(0.0, dot(L,N));
  50. }
  51. vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * attenuation * diffuseIntensity * U_DiffuseIntensity;
  52. //specualr
  53. vec3 RD = normalize(reflect(-L,N));
  54. vec3 VD = normalize(U_EyePos - V_WorldPos);
  55. vec4 specularColor = U_SpecularLightColor * U_SpecularMaterial * pow(max(0.0, dot(RD,VD)) ,128);
  56. gl_FragColor = ambientColor + diffuseColor;
  57. }