DefaultUntextured.frag 3.3 KB

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