webgl_postprocessing_msaa_unbiased.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing manual msaa</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. margin: 0px;
  10. background-color: #000;
  11. overflow: hidden;
  12. font-family:Monospace;
  13. font-size:13px;
  14. margin: 0px;
  15. text-align:center;
  16. overflow: hidden;
  17. }
  18. #info {
  19. color: #fff;
  20. position: absolute;
  21. top: 10px;
  22. width: 100%;
  23. text-align: center;
  24. display:block;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="info">
  30. <a href="http://threejs.org" target="_blank">three.js</a> - Unbiased Manual Multi-Sample Anti-Aliasing (MSAA) pass by <a href="https://clara.io" target="_blank">Ben Houston</a><br/><br/>
  31. This example shows how to unbias the rounding errors accumulated using high number of MSAA samples on a 8-bit per channel buffer.<br/><br/>
  32. Turn off the "unbiased" feature to see the banding that results from accumulated rounding errors.
  33. </div>
  34. <div id="container"></div>
  35. <script src="../build/three.js"></script>
  36. <script src="js/libs/stats.min.js"></script>
  37. <script src="js/libs/dat.gui.min.js"></script>
  38. <script src="js/shaders/CopyShader.js"></script>
  39. <script src="js/postprocessing/EffectComposer.js"></script>
  40. <script src="js/postprocessing/ManualMSAARenderPass.js"></script>
  41. <script src="js/postprocessing/RenderPass.js"></script>
  42. <script src="js/postprocessing/MaskPass.js"></script>
  43. <script src="js/postprocessing/ShaderPass.js"></script>
  44. <script>
  45. var camera, scene, renderer, composer, copyPass, msaaRenderPass;
  46. var gui, stats, texture;
  47. var param = {
  48. sampleLevel: 4,
  49. unbiased: true
  50. };
  51. init();
  52. animate();
  53. clearGui();
  54. function clearGui() {
  55. if ( gui ) gui.destroy();
  56. gui = new dat.GUI();
  57. gui.add( param, "unbiased" );
  58. gui.add( param, 'sampleLevel', {
  59. 'Level 0: 1 Sample': 0,
  60. 'Level 1: 2 Samples': 1,
  61. 'Level 2: 4 Samples': 2,
  62. 'Level 3: 8 Samples': 3,
  63. 'Level 4: 16 Samples': 4,
  64. 'Level 5: 32 Samples': 5
  65. } );
  66. gui.open();
  67. }
  68. function init() {
  69. container = document.getElementById( "container" );
  70. var width = window.innerWidth || 1;
  71. var height = window.innerHeight || 1;
  72. var devicePixelRatio = window.devicePixelRatio || 1;
  73. renderer = new THREE.WebGLRenderer( { antialias: false } );
  74. renderer.setPixelRatio( devicePixelRatio );
  75. renderer.setSize( width, height );
  76. document.body.appendChild( renderer.domElement );
  77. stats = new Stats();
  78. container.appendChild( stats.dom );
  79. //
  80. camera = new THREE.PerspectiveCamera( 65, width / height, 3, 10 );
  81. camera.position.z = 7;
  82. scene = new THREE.Scene();
  83. group = new THREE.Object3D();
  84. scene.add( group );
  85. var light = new THREE.PointLight( 0xddffdd, 1.0 );
  86. light.position.z = 70;
  87. light.position.y = -70;
  88. light.position.x = -70;
  89. scene.add( light );
  90. var light2 = new THREE.PointLight( 0xffdddd, 1.0 );
  91. light2.position.z = 70;
  92. light2.position.x = -70;
  93. light2.position.y = 70;
  94. scene.add( light2 );
  95. var light3 = new THREE.PointLight( 0xddddff, 1.0 );
  96. light3.position.z = 70;
  97. light3.position.x = 70;
  98. light3.position.y = -70;
  99. scene.add( light3 );
  100. var light3 = new THREE.AmbientLight( 0xffffff, 0.05 );
  101. scene.add( light3 );
  102. var geometry = new THREE.SphereBufferGeometry( 3, 48, 24 );
  103. for ( var i = 0; i < 120; i ++ ) {
  104. var material = new THREE.MeshStandardMaterial();
  105. material.roughness = 0.5 * Math.random() + 0.25;
  106. material.metalness = 0;
  107. material.color.setHSL( Math.random(), 1.0, 0.3 );
  108. var mesh = new THREE.Mesh( geometry, material );
  109. mesh.position.x = Math.random() * 4 - 2;
  110. mesh.position.y = Math.random() * 4 - 2;
  111. mesh.position.z = Math.random() * 4 - 2;
  112. mesh.rotation.x = Math.random();
  113. mesh.rotation.y = Math.random();
  114. mesh.rotation.z = Math.random();
  115. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 0.2 + 0.05;
  116. group.add( mesh );
  117. }
  118. // postprocessing
  119. composer = new THREE.EffectComposer( renderer );
  120. msaaRenderPass = new THREE.ManualMSAARenderPass( scene, camera );
  121. composer.addPass( msaaRenderPass );
  122. copyPass = new THREE.ShaderPass( THREE.CopyShader );
  123. copyPass.renderToScreen = true;
  124. composer.addPass( copyPass );
  125. window.addEventListener( 'resize', onWindowResize, false );
  126. }
  127. function onWindowResize() {
  128. var width = window.innerWidth;
  129. var height = window.innerHeight;
  130. camera.aspect = width / height;
  131. camera.updateProjectionMatrix();
  132. renderer.setSize( width, height );
  133. var pixelRatio = renderer.getPixelRatio();
  134. var newWidth = Math.floor( width / pixelRatio ) || 1;
  135. var newHeight = Math.floor( height / pixelRatio ) || 1;
  136. composer.setSize( newWidth, newHeight );
  137. }
  138. function animate() {
  139. requestAnimationFrame( animate );
  140. stats.begin();
  141. for ( var i = 0; i < scene.children.length; i ++ ) {
  142. var child = scene.children[ i ];
  143. child.rotation.x += 0.005;
  144. child.rotation.y += 0.01;
  145. }
  146. msaaRenderPass.sampleLevel = param.sampleLevel;
  147. msaaRenderPass.unbiased = param.unbiased;
  148. composer.render();
  149. stats.end();
  150. }
  151. </script>
  152. <div>
  153. </body>
  154. </html>