DefaultShaderShadows.frag 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. varying vec3 normal;
  2. varying vec4 pos;
  3. varying vec4 vertexColor;
  4. varying vec4 ShadowCoord0;
  5. varying vec4 ShadowCoord1;
  6. uniform sampler2D diffuse;
  7. uniform sampler2D shadowMap0;
  8. uniform sampler2D shadowMap1;
  9. uniform vec4 diffuse_color;
  10. uniform vec4 specular_color;
  11. uniform vec4 ambient_color;
  12. uniform float shininess;
  13. uniform float shadowAmount;
  14. float calculateAttenuation(in int i, in float dist)
  15. {
  16. return(1.0 / (gl_LightSource[i].constantAttenuation +
  17. gl_LightSource[i].linearAttenuation * dist +
  18. gl_LightSource[i].quadraticAttenuation * dist * dist));
  19. }
  20. void pointLight(in int i, in vec3 normal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) {
  21. vec4 color = diffuse_color;
  22. vec4 matspec = specular_color;
  23. float shininess = shininess;
  24. vec4 lightspec = gl_LightSource[i].specular;
  25. vec4 lpos = gl_LightSource[i].position;
  26. vec4 s = pos-lpos;
  27. vec4 sn = -normalize(s);
  28. vec3 light = sn.xyz;
  29. vec3 n = normalize(normal);
  30. vec3 r = -reflect(light, n);
  31. r = normalize(r);
  32. vec3 v = -pos.xyz;
  33. v = normalize(v);
  34. float nDotL = dot(n, sn.xyz);
  35. if(nDotL > 0.0) {
  36. float dist = length(s);
  37. float attenuation = calculateAttenuation(i, dist);
  38. diffuse += color * max(0.0, nDotL) * gl_LightSource[i].diffuse * attenuation;
  39. if (shininess != 0.0) {
  40. specular += lightspec * matspec * pow(max(0.0,dot(r, v)), shininess) * attenuation;
  41. }
  42. }
  43. }
  44. void spotLight(in int i, in vec3 normal, in vec4 pos, inout vec4 diffuse, inout vec4 specular, sampler2D shadowMap, vec4 ShadowCoord) {
  45. vec4 shadowCoordinateWdivide = ShadowCoord / ShadowCoord.w;
  46. //shadowCoordinateWdivide.z -= 0.00005;
  47. float distanceFromLight = texture2D(shadowMap,shadowCoordinateWdivide.st).z;
  48. float shadow = 1.0;
  49. if (shadowCoordinateWdivide.x > 0.001 && shadowCoordinateWdivide.y > 0.001 && shadowCoordinateWdivide.x < 0.999 && shadowCoordinateWdivide.y < 0.999)
  50. shadow = distanceFromLight < shadowCoordinateWdivide.z ? 0.0 : 1.0 ;
  51. shadow = clamp(shadow+(1.0-shadowAmount), 0.0, 1.0);
  52. vec4 color = diffuse_color;
  53. vec4 matspec = specular_color;
  54. float shininess = shininess;
  55. vec4 lightspec = gl_LightSource[i].specular;
  56. vec4 lpos = gl_LightSource[i].position;
  57. vec4 s = pos-lpos;
  58. vec4 sn = -normalize(s);
  59. vec3 light = sn.xyz;
  60. vec3 n = normalize(normal);
  61. vec3 r = -reflect(light, n);
  62. r = normalize(r);
  63. vec3 v = -pos.xyz;
  64. v = normalize(v);
  65. float cos_outer_cone_angle = (1.0-gl_LightSource[i].spotExponent) * gl_LightSource[i].spotCosCutoff;
  66. float cos_cur_angle = dot(-normalize(gl_LightSource[i].spotDirection), sn.xyz);
  67. float cos_inner_cone_angle = gl_LightSource[i].spotCosCutoff;
  68. float cos_inner_minus_outer_angle = cos_inner_cone_angle - cos_outer_cone_angle;
  69. float spot = 0.0;
  70. spot = clamp((cos_cur_angle - cos_outer_cone_angle) / cos_inner_minus_outer_angle, 0.0, 1.0);
  71. float nDotL = dot(n, sn.xyz);
  72. if(nDotL > 0.0) {
  73. float dist = length(s);
  74. float attenuation = calculateAttenuation(i, dist);
  75. diffuse += color * max(0.0, nDotL) * gl_LightSource[i].diffuse * attenuation * spot * shadow;
  76. if (shininess != 0.0) {
  77. specular += lightspec * matspec * pow(max(0.0,dot(r, v)), shininess) * attenuation * spot * shadow;
  78. }
  79. }
  80. }
  81. void doLights(in int numLights, in vec3 normal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) {
  82. int spot = 0;
  83. for (int i = 0; i < numLights; i++)
  84. {
  85. if (gl_LightSource[i].spotCutoff == 180.0) {
  86. pointLight(i, normal, pos, diffuse, specular);
  87. } else {
  88. if(spot == 0) {
  89. spotLight(i, normal, pos, diffuse, specular,shadowMap0, ShadowCoord0);
  90. spot = 1;
  91. } else {
  92. spotLight(i, normal, pos, diffuse, specular,shadowMap1, ShadowCoord1);
  93. }
  94. }
  95. }
  96. }
  97. void main()
  98. {
  99. vec4 diffuse_val = vec4(0.0);
  100. vec4 specular_val = vec4(0.0);
  101. doLights(6, normal, pos, diffuse_val, specular_val);
  102. vec4 texColor = texture2D(diffuse, gl_TexCoord[0].st);
  103. vec4 color = diffuse_val + ambient_color;
  104. color = clamp((color*vertexColor*texColor) + specular_val, 0.0, 1.0);
  105. // fog
  106. const float LOG2 = 1.442695;
  107. float z = gl_FragCoord.z / gl_FragCoord.w;
  108. float fogFactor = exp2( -gl_Fog.density *
  109. gl_Fog.density *
  110. z *
  111. z *
  112. LOG2 );
  113. fogFactor = clamp(fogFactor, 0.0, 1.0);
  114. color = mix(gl_Fog.color, color, fogFactor );
  115. color.a = diffuse_color.a * texColor.a;
  116. gl_FragColor = color;
  117. }