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.

81 lines
2.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
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. uniform float U_FogStart;
  13. uniform float U_FogEnd;
  14. uniform vec4 U_FogColor;
  15. varying vec3 V_Normal;
  16. varying vec3 V_WorldPos;
  17. varying vec4 V_EyeSpacePos;
  18. float calculateLinearFog(float distance){
  19. float fogAlpha = (distance - U_FogStart) / (U_FogEnd - U_FogStart);
  20. fogAlpha = 1.0 - clamp(fogAlpha, 0.0, 1.0);
  21. return fogAlpha;
  22. }
  23. void main()
  24. {
  25. //common
  26. vec3 N = normalize(V_Normal);
  27. //ambient
  28. vec4 ambientColor = U_AmbientLightColor * U_AmbientMaterial;
  29. //diffuse
  30. vec3 L = vec3(0.0);
  31. float attenuation = 1.0;
  32. float diffuseIntensity = 0.0;
  33. if(U_LightPos.w != 0){// ����
  34. L = normalize(U_LightPos.xyz - V_WorldPos);
  35. float distance = length(U_LightPos.xyz - V_WorldPos);
  36. float constantFactor=0.5;
  37. float linearFactor=0.3;
  38. float expFactor=0.1;
  39. attenuation = 1.0 / (constantFactor + linearFactor * distance + expFactor * distance * distance);
  40. if( U_Cutoff > 0){//�۹�
  41. //�Ƕ�ת����
  42. float radianCutoff = U_Cutoff * 3.14 / 180;
  43. float cosThta = cos(radianCutoff);
  44. vec3 spotLightDirection = normalize(U_LightDirection.xyz);
  45. float currentThta = max(0.0, dot(-L, spotLightDirection));
  46. if(currentThta > cosThta){
  47. if(dot(L,N)>0.0)
  48. {
  49. diffuseIntensity=pow(currentThta,U_LightDirection.w);
  50. }
  51. }
  52. } else {
  53. diffuseIntensity = max(0.0, dot(L,N));
  54. }
  55. } else {
  56. // ƽ�й�
  57. L = normalize(U_LightPos.xyz - vec3(0.0));
  58. diffuseIntensity = max(0.0, dot(L,N));
  59. }
  60. vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * attenuation * diffuseIntensity * U_DiffuseIntensity;
  61. //specualr
  62. vec3 RD = normalize(reflect(-L,N));
  63. vec3 VD = normalize(U_EyePos - V_WorldPos);
  64. vec4 specularColor = U_SpecularLightColor * U_SpecularMaterial * pow(max(0.0, dot(RD,VD)) ,128);
  65. float fogAlpha = calculateLinearFog(abs(V_EyeSpacePos.z/V_EyeSpacePos.w));
  66. gl_FragColor = mix(U_FogColor,ambientColor + diffuseColor + specularColor, fogAlpha);
  67. }