DefaultParticleShader.frag 2.7 KB

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