fs_bokeh_forward_grid.sc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. $input v_normal, v_texcoord0, v_texcoord1, v_texcoord2
  2. /*
  3. * Copyright 2021 elven cache. All rights reserved.
  4. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  5. */
  6. #include "../common/common.sh"
  7. // struct ModelUniforms
  8. uniform vec4 u_modelParams[2];
  9. #define u_color (u_modelParams[0].xyz)
  10. #define u_lightPosition (u_modelParams[1].xyz)
  11. int ModHelper (float a, float b)
  12. {
  13. return int( a - (b*floor(a/b)));
  14. }
  15. vec3 GetGridColor (vec2 position, float width, vec3 color)
  16. {
  17. position = abs(floor( position + vec2(-width, -width) ));
  18. int posXMod = ModHelper(position.x, 2.0);
  19. int posYMod = ModHelper(position.y, 2.0);
  20. float gridColorScale = (posXMod == posYMod) ? 0.75 : 1.25;
  21. return toLinear(color) * gridColorScale;
  22. }
  23. void main()
  24. {
  25. vec3 worldSpacePosition = v_texcoord1.xyz; // contains ws pos
  26. vec2 gridCoord = worldSpacePosition.xz; // assuming y is up
  27. vec3 gridColor = GetGridColor(gridCoord.xy, 0.002, u_color);
  28. // get vertex normal
  29. vec3 normal = normalize(v_normal);
  30. vec3 light = (u_lightPosition - worldSpacePosition);
  31. light = normalize(light);
  32. float NdotL = saturate(dot(normal, light));
  33. float diffuse = NdotL * 1.0;
  34. vec3 V = v_texcoord2.xyz; // contains view vector
  35. vec3 H = normalize(V+light);
  36. float NdotH = saturate(dot(normal, H));
  37. float specular = 5.0 * pow(NdotH, 256);
  38. float ambient = 0.1;
  39. float lightAmount = ambient + diffuse;
  40. vec3 color = gridColor * lightAmount + specular;
  41. // leave color in linear space for better dof filter result
  42. gl_FragColor = vec4(color, 1.0);
  43. }