fs_shadowvolume_texture_lighting.sc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. uniform vec4 u_params;
  8. uniform vec4 u_svparams;
  9. uniform vec4 u_ambient;
  10. uniform vec4 u_diffuse;
  11. uniform vec4 u_specular_shininess;
  12. uniform vec4 u_fog;
  13. uniform vec4 u_lightPosRadius;
  14. uniform vec4 u_lightRgbInnerR;
  15. SAMPLER2D(s_texColor, 0);
  16. SAMPLER2D(s_texStencil, 1);
  17. #define u_ambientPass u_params.x
  18. #define u_lightingPass u_params.y
  19. #define u_texelHalf u_params.z
  20. #define u_specular u_specular_shininess.xyz
  21. #define u_shininess u_specular_shininess.w
  22. #define u_fogColor u_fog.xyz
  23. #define u_fogDensity u_fog.w
  24. #define u_useStencilTex u_svparams.x
  25. vec2 blinn(vec3 _lightDir, vec3 _normal, vec3 _viewDir)
  26. {
  27. float ndotl = dot(_normal, _lightDir);
  28. vec3 reflected = 2.0*ndotl*_normal - _lightDir; // reflect(_lightDir, _normal);
  29. float rdotv = dot(reflected, _viewDir);
  30. return vec2(ndotl, rdotv);
  31. }
  32. vec4 lit(float _ndotl, float _rdotv, float _m)
  33. {
  34. float diff = max(0.0, _ndotl);
  35. float spec = step(0.0, _ndotl) * pow(max(0.0, _rdotv), _m);
  36. return vec4(1.0, diff, spec, 1.0);
  37. }
  38. vec3 calcLight(vec3 _view, vec3 _normal, vec3 _viewDir)
  39. {
  40. vec3 lightPos = mul(u_view, vec4(u_lightPosRadius.xyz, 1.0)).xyz;
  41. vec3 toLight = lightPos - _view;
  42. vec3 lightDir = normalize(toLight);
  43. vec2 bln = blinn(lightDir, _normal, _viewDir);
  44. vec4 lc = lit(bln.x, bln.y, u_shininess);
  45. float dist = max(length(toLight), u_lightPosRadius.w);
  46. float attn = 50.0 * pow(dist, -2.0);
  47. vec3 rgb = (lc.y * u_diffuse.xyz + lc.z * u_specular) * u_lightRgbInnerR.rgb * attn;
  48. return rgb;
  49. }
  50. void main()
  51. {
  52. vec3 ambientColor = u_ambient.xyz * u_ambientPass;
  53. vec3 normal = normalize(v_normal);
  54. vec3 viewDir = -normalize(v_view);
  55. vec3 lightColor = calcLight(v_view, normal, viewDir) * u_lightingPass;
  56. vec2 ndc = gl_FragCoord.xy * u_viewTexel.xy + u_viewTexel.xy * u_texelHalf;
  57. vec4 texcolor = texture2D(s_texStencil, ndc);
  58. float s = (texcolor.x - texcolor.y) + 2.0 * (texcolor.z - texcolor.w);
  59. s *= u_useStencilTex;
  60. const float LOG2 = 1.442695;
  61. float z = length(v_view);
  62. float fogFactor = 1.0/exp2(u_fogDensity*u_fogDensity*z*z*LOG2);
  63. fogFactor = clamp(fogFactor, 0.0, 1.0);
  64. vec3 color = toLinear(texture2D(s_texColor, v_texcoord0)).xyz;
  65. vec3 ambient = toGamma(ambientColor * color);
  66. vec3 diffuse = toGamma(lightColor * color);
  67. vec3 final = mix(ambient, ambient + diffuse, float((abs(s) < 0.0001)));
  68. gl_FragColor.xyz = mix(u_fogColor, final, fogFactor);
  69. gl_FragColor.w = 1.0;
  70. }