DefaultShaderNoTexture.frag 3.4 KB

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