DefaultShader.frag 3.5 KB

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