transparency_sorting.html 6.2 KB

1234567891011121314151617181920212223
  1. <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]--><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="Asciidoctor 1.5.4"><meta name="keywords" content="transparent, sorting, bucket, z-buffer, alpha"><title>Transparency Sorting</title><link rel="stylesheet" href="./asciidoctor.css">
  2. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
  3. <link rel="stylesheet" href="./coderay-asciidoctor.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.css"><link rel="stylesheet" href="/home/travis/build/jMonkeyEngine/wiki/build/asciidoc/html5/jme3/intermediate/twemoji-awesome.css"></head><body class="article toc2 toc-left"><div id="header"><div id="toolbar"><a href="https://github.com/jMonkeyEngine/wiki/edit/master/src/docs/asciidoc/jme3/intermediate/transparency_sorting.adoc"><i class="fa fa-pencil-square" aria-hidden="true"></i></a><a href="https://github.com/jMonkeyEngine/wiki/new/master/src/docs/asciidoc/jme3/intermediate/"><i class="fa fa-plus-square" aria-hidden="true"></i></a><input dir="auto" style="position: relative; vertical-align: top;" spellcheck="false" autocomplete="off" class="searchbox__input aa-input" id="doc-search" name="search" placeholder="Search in the doc" required="required" type="search"></div><h1>Transparency Sorting</h1><div class="details"><span class="author" id="author"></span><br><span id="revnumber">version ,</span> <span id="revdate">2016/03/17 20:48</span></div><div id="toc" class="toc2"><div id="toctitle">Table of Contents</div></div></div><div id="content"><div class="paragraph"><p>There is often a lot of confusion with how pixels get processed in relation to the z-buffer and why sorting is important. Most importantly it can be mind-warping trying to wrap one&#8217;s head around how to 'fix' the cases where proper sorting isn&#8217;t possible.</p></div>
  4. <div class="paragraph"><p>Hopefully these diagrams help show the futility of it all… I mean the trade offs one has to make to get transparency looking its best in the general case.</p></div>
  5. <div class="paragraph"><p>The first image I&#8217;ll show is the 'best case' where all objects are drawn back to front. I&#8217;ll then discuss briefly the steps JME goes through to try to make that happen.</p></div>
  6. <div style="text-align: center;" class="imageblock"><div class="content"><img src="../../jme3/intermediate/transparency_sorting1.png" alt="transparency_sorting1.png" width="600" height=""></div></div>
  7. <div class="paragraph"><p>In JME, the opaque layer would generally be placed in the opaque bucket. All opaque layers are drawn first and within the ability to sort them, they are sorted front to back to prevent overdraw.</p></div>
  8. <div class="paragraph"><p>After the opaque bucket is drawn, the transparent bucket is drawn and it is sorted back to front. So in the ideal case it would appear exactly as in this picture.</p></div>
  9. <div class="admonitionblock note"><table><tr><td class="icon"><i class="fa icon-note" title="Note"></i></td><td class="content"><div class="paragraph"><p>Sorting is done at the object level and it can never be perfect. It is impossible to properly sort objects by distance in the general case even if they were just triangles. Simply imagine intersecting triangles and it&#8217;s clear there is no proper order. JME tries its best.</p></div></td></tr></table></div>
  10. <div class="paragraph"><p>Next I&#8217;ll show an image of the worst case scenario to show why sorting is important and how your 'best friend' the z-buffer is really transparency&#8217;s worst enemy.</p></div>
  11. <div style="text-align: center;" class="imageblock"><div class="content"><img src="../../jme3/intermediate/transparency_sorting2.png" alt="transparency_sorting2.png" width="600" height=""></div></div>
  12. <div class="paragraph"><p>This is what will happen if you put all of your objects in the opaque buffer. JME will sort them front to back and you will get these strange 'windows' into your background.</p></div>
  13. <div class="admonitionblock note"><table><tr><td class="icon"><i class="fa icon-note" title="Note"></i></td><td class="content"><div class="paragraph"><p>Because sorting is done at the object level, you may have triangle to triangle overlap even within the same mesh if it is non-convex. Think of a glass donut where the near surface triangles are drawn before the hole&#8217;s triangles. There will be this same issue where they occlude the farther triangles. A different angle on the donut might produce the correct results depending on the order of the triangles in the mesh.</p></div></td></tr></table></div>
  14. <div class="paragraph"><p>Finally, I&#8217;ll augment the worst case sorting with something like <code>alphaDiscardThreshold</code> (or the old alphaTest/alphaFalloff values). In this example, let&#8217;s pretend we only discard pixels with alpha = 0.</p></div>
  15. <div style="text-align: center;" class="imageblock"><div class="content"><img src="../../jme3/intermediate/transparency_sorting3.png" alt="transparency_sorting3.png" width="600" height=""></div></div>
  16. <div class="paragraph"><p>It&#8217;s better but not perfect. Any partially transparent pixels will still show the issue. Partial transparency will drive you crazy if you let it.</p></div>
  17. <div class="paragraph"><p>Bottom line:</p></div>
  18. <div class="ulist"><ul><li><p>back to front sorting would fix all issues</p></li><li><p>accurate back to front sorting in the general case is impossible</p></li><li><p>for purely on/off a = 1 or a = 0 transparency then a discard threshold is the best bet to mitigate sorting problems.</p></li><li><p>Where 0 &lt; alpha &lt; 1, improper sorting of triangles/pixels will always cause artifacts.</p></li></ul></div></div><div id="footer"><div id="footer-text">Version <br>Last updated 2020-05-21 00:29:23 +00:00</div></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script><script>docsearch({
  19. apiKey: 'a736b6d93de805e26ec2f49b55013fbd',
  20. indexName: 'jmonkeyengine',
  21. inputSelector: '#doc-search',
  22. debug: false // Set debug to true if you want to inspect the dropdown
  23. });</script></body></html>