DefaultShader.frag 3.6 KB

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