fs_stencil_texture_lighting.sc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. $input v_normal, v_view, v_texcoord0
  2. /*
  3. * Copyright 2013-2014 Dario Manesku. All rights reserved.
  4. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  5. */
  6. #include "../common/common.sh"
  7. #define MAX_NUM_LIGHTS 5
  8. uniform vec4 u_params;
  9. uniform vec4 u_ambient;
  10. uniform vec4 u_diffuse;
  11. uniform vec4 u_color;
  12. uniform vec4 u_specular_shininess;
  13. uniform vec4 u_lightPosRadius[MAX_NUM_LIGHTS];
  14. uniform vec4 u_lightRgbInnerR[MAX_NUM_LIGHTS];
  15. SAMPLER2D(s_texColor, 0);
  16. #define u_ambientPass u_params.x
  17. #define u_lightingPass u_params.y
  18. #define u_lightCount u_params.z
  19. #define u_lightIndex u_params.w
  20. #define u_specular u_specular_shininess.xyz
  21. #define u_shininess u_specular_shininess.w
  22. vec2 blinn(vec3 _lightDir, vec3 _normal, vec3 _viewDir)
  23. {
  24. float ndotl = dot(_normal, _lightDir);
  25. vec3 reflected = 2.0*ndotl*_normal - _lightDir; // reflect(_lightDir, _normal);
  26. float rdotv = dot(reflected, _viewDir);
  27. return vec2(ndotl, rdotv);
  28. }
  29. vec4 lit(float _ndotl, float _rdotv, float _m)
  30. {
  31. float diff = max(0.0, _ndotl);
  32. float spec = step(0.0, _ndotl) * pow(max(0.0, _rdotv), _m);
  33. return vec4(1.0, diff, spec, 1.0);
  34. }
  35. vec3 calcLight(int _idx, vec3 _view, vec3 _normal, vec3 _viewDir)
  36. {
  37. vec3 lightPos = mul(u_view, vec4(u_lightPosRadius[_idx].xyz, 1.0)).xyz;
  38. vec3 toLight = lightPos - _view;
  39. vec3 lightDir = normalize(toLight);
  40. vec2 bln = blinn(lightDir, _normal, _viewDir);
  41. vec4 lc = lit(bln.x, bln.y, u_shininess);
  42. float dist = max(length(toLight), u_lightPosRadius[_idx].w);
  43. float attn = 250.0 * pow(dist, -2.0);
  44. vec3 rgb = (lc.y * u_diffuse.xyz + lc.z * u_specular) * u_lightRgbInnerR[_idx].rgb * attn;
  45. return rgb;
  46. }
  47. void main()
  48. {
  49. vec3 normal = normalize(v_normal);
  50. vec3 viewDir = -normalize(v_view);
  51. vec3 ambientColor = u_ambient.xyz * u_ambientPass;
  52. vec3 lightColor = vec3_splat(0.0);
  53. for(int ii = 0; ii < MAX_NUM_LIGHTS; ++ii)
  54. {
  55. float condition = 0.0;
  56. if (u_lightCount > 1.0) // Stencil Reflection Scene.
  57. {
  58. condition = 1.0 - step(u_lightCount, float(ii)); // True for every light up to u_lightCount.
  59. }
  60. else // Projection Shadows Scene.
  61. {
  62. condition = float(float(ii) == u_lightIndex); // True only for current light.
  63. }
  64. lightColor += calcLight(ii, v_view, normal, viewDir) * condition;
  65. }
  66. lightColor *= u_lightingPass;
  67. vec3 color = toLinear(texture2D(s_texColor, v_texcoord0)).xyz;
  68. vec3 ambient = toGamma(ambientColor * color);
  69. vec3 diffuse = toGamma(lightColor * color);
  70. gl_FragColor.xyz = clamp(ambient + diffuse, 0.0, 1.0);
  71. gl_FragColor.w = u_color.w;
  72. }