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.

87 lines
2.3 KiB

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