DefaultUntextured.frag 3.3 KB

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