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.

96 lines
2.5 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 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. vec2 texelSize = 1.0/textureSize(U_ShadowMap, 0);
  22. float shadow = 0;
  23. for(int y = -1; y < 1; ++y){
  24. for(int x = -1; x < 1; ++x){
  25. float pcfDepth = texture2D(U_ShadowMap, fragPos.xy + texelSize * vec2(x,y)).r;
  26. shadow += (currentDepth-0.05) > pcfDepth ? 1.0 : 0.0;
  27. }
  28. }
  29. shadow/=9.0;
  30. return shadow;
  31. }
  32. void main()
  33. {
  34. //common
  35. vec3 N = normalize(V_Normal);
  36. //ambient
  37. vec4 ambientColor = U_AmbientLightColor * U_AmbientMaterial;
  38. //diffuse
  39. vec3 L = vec3(0.0);
  40. float attenuation = 1.0;
  41. float diffuseIntensity = 0.0;
  42. if(U_LightPos.w != 0){// ����
  43. L = normalize(U_LightPos.xyz - V_WorldPos);
  44. float distance = length(U_LightPos.xyz - V_WorldPos);
  45. float constantFactor=0.5;
  46. float linearFactor=0.3;
  47. float expFactor=0.1;
  48. attenuation = 1.0 / (constantFactor + linearFactor * distance + expFactor * distance * distance);
  49. if( U_Cutoff > 0){//�۹�
  50. //�Ƕ�ת����
  51. float radianCutoff = U_Cutoff * 3.14 / 180;
  52. float cosThta = cos(radianCutoff);
  53. vec3 spotLightDirection = normalize(U_LightDirection.xyz);
  54. float currentThta = max(0.0, dot(-L, spotLightDirection));
  55. if(currentThta > cosThta){
  56. if(dot(L,N)>0.0)
  57. {
  58. diffuseIntensity=pow(currentThta,U_LightDirection.w);
  59. }
  60. }
  61. } else {
  62. diffuseIntensity = max(0.0, dot(L,N));
  63. }
  64. } else {
  65. // ƽ�й�
  66. L = normalize(U_LightPos.xyz - vec3(0.0));
  67. diffuseIntensity = max(0.0, dot(L,N));
  68. }
  69. vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * attenuation * diffuseIntensity * U_DiffuseIntensity;
  70. //specualr
  71. vec3 RD = normalize(reflect(-L,N));
  72. vec3 VD = normalize(U_EyePos - V_WorldPos);
  73. vec4 specularColor = U_SpecularLightColor * U_SpecularMaterial * pow(max(0.0, dot(RD,VD)) ,128);
  74. //gl_FragColor = ambientColor + diffuseColor;
  75. vec4 color = ambientColor + diffuseColor * vec4(vec3(1.0 - CalculateShadow()),1.0);
  76. gl_FragData[0] = color;
  77. gl_FragData[1] = vec4(0.0);
  78. }