fs_tree.sc 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. $input v_pos, v_view, v_normal, v_texcoord0
  2. /*
  3. * Copyright 2013 Milos Tosic. All rights reserved.
  4. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  5. */
  6. #include "../common/common.sh"
  7. SAMPLER2D(s_texColor, 0);
  8. SAMPLER2D(s_texStipple, 1);
  9. uniform vec4 u_stipple;
  10. vec2 blinn(vec3 _lightDir, vec3 _normal, vec3 _viewDir)
  11. {
  12. float ndotl = dot(_normal, _lightDir);
  13. vec3 reflected = _lightDir - 2.0*ndotl*_normal; // reflect(_lightDir, _normal);
  14. float rdotv = dot(reflected, _viewDir);
  15. return vec2(ndotl, rdotv);
  16. }
  17. void main()
  18. {
  19. vec2 viewport = (u_viewRect.zw - u_viewRect.xy) * vec2(1.0/8.0, 1.0/4.0);
  20. vec2 stippleUV = viewport*(v_pos.xy*0.5 + 0.5);
  21. vec4 color = texture2D(s_texColor, v_texcoord0);
  22. if ( (u_stipple.x - texture2D(s_texStipple, stippleUV).x)*u_stipple.y > u_stipple.z
  23. || color.w < 0.5)
  24. {
  25. discard;
  26. }
  27. vec3 lightDir = vec3(0.0, 0.0, -1.0);
  28. vec3 normal = normalize(v_normal);
  29. vec3 view = normalize(v_view);
  30. vec2 bln = blinn(lightDir, normal, view);
  31. float l = saturate(bln.y) + 0.12;
  32. color.xyz = toLinear(color.xyz)*l;
  33. gl_FragColor = toGamma(color);
  34. }