dpoit-write.glsl.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Copyright (c) 2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Gianluca Tomasello <giagitom@gmail.com>
  5. */
  6. export const dpoit_write = `
  7. #if defined(dRenderVariant_colorDpoit)
  8. if (uRenderMask == MaskOpaque) {
  9. if (preFogAlpha < 1.0) {
  10. discard;
  11. }
  12. } else if (uRenderMask == MaskTransparent) {
  13. // the 'fragmentDepth > 0.99' check is to handle precision issues with packed depth
  14. vec2 coords = gl_FragCoord.xy / uDrawingBufferSize;
  15. if (preFogAlpha != 1.0 && (fragmentDepth < getDepth(coords) || fragmentDepth > 0.99)) {
  16. #ifdef dTransparentBackfaces_off
  17. if (interior) discard;
  18. #endif
  19. // adapted from https://github.com/tsherif/webgl2examples
  20. // The MIT License, Copyright 2017 Tarek Sherif, Shuai Shao
  21. vec2 lastDepth = texture2D(tDpoitDepth, coords).rg;
  22. vec4 lastFrontColor = texture2D(tDpoitFrontColor, coords);
  23. vec4 fragColor = gl_FragColor;
  24. // depth value always increases
  25. // so we can use MAX blend equation
  26. gl_FragData[2].rg = vec2(-MAX_DPOIT_DEPTH);
  27. // front color always increases
  28. // so we can use MAX blend equation
  29. gl_FragColor = lastFrontColor;
  30. // back color is separately blend afterwards each pass
  31. gl_FragData[1] = vec4(0.0);
  32. float nearestDepth = - lastDepth.x;
  33. float furthestDepth = lastDepth.y;
  34. float alphaMultiplier = 1.0 - lastFrontColor.a;
  35. if (fragmentDepth < nearestDepth || fragmentDepth > furthestDepth) {
  36. // Skip this depth since it's been peeled.
  37. return;
  38. }
  39. if (fragmentDepth > nearestDepth && fragmentDepth < furthestDepth) {
  40. // This needs to be peeled.
  41. // The ones remaining after MAX blended for
  42. // all need-to-peel will be peeled next pass.
  43. gl_FragData[2].rg = vec2(-fragmentDepth, fragmentDepth);
  44. return;
  45. }
  46. // write to back and front color buffer
  47. if (fragmentDepth == nearestDepth) {
  48. gl_FragColor.rgb += fragColor.rgb * fragColor.a * alphaMultiplier;
  49. gl_FragColor.a = 1.0 - alphaMultiplier * (1.0 - fragColor.a);
  50. } else {
  51. gl_FragData[1] += fragColor;
  52. }
  53. } else {
  54. discard;
  55. }
  56. }
  57. #endif
  58. `;