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.

89 lines
2.3 KiB

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