webgl_postprocessing_msaa_unbiased.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 scene, renderer;
  46. var cameraP, composerP, copyPassP, msaaRenderPassP;
  47. var cameraO, composerO, copyPassO, msaaRenderPassO;
  48. var gui, stats, texture;
  49. var param = {
  50. sampleLevel: 4,
  51. unbiased: true,
  52. camera: 'perspective'
  53. };
  54. init();
  55. animate();
  56. clearGui();
  57. function clearGui() {
  58. if ( gui ) gui.destroy();
  59. gui = new dat.GUI();
  60. gui.add( param, "unbiased" );
  61. gui.add( param, 'sampleLevel', {
  62. 'Level 0: 1 Sample': 0,
  63. 'Level 1: 2 Samples': 1,
  64. 'Level 2: 4 Samples': 2,
  65. 'Level 3: 8 Samples': 3,
  66. 'Level 4: 16 Samples': 4,
  67. 'Level 5: 32 Samples': 5
  68. } );
  69. gui.add( param, 'camera', [ 'perspective', 'ortho' ] );
  70. gui.open();
  71. }
  72. function init() {
  73. container = document.getElementById( "container" );
  74. var width = window.innerWidth || 1;
  75. var height = window.innerHeight || 1;
  76. var aspect = width / height;
  77. var devicePixelRatio = window.devicePixelRatio || 1;
  78. renderer = new THREE.WebGLRenderer( { antialias: false } );
  79. renderer.setPixelRatio( devicePixelRatio );
  80. renderer.setSize( width, height );
  81. document.body.appendChild( renderer.domElement );
  82. stats = new Stats();
  83. container.appendChild( stats.dom );
  84. //
  85. cameraP = new THREE.PerspectiveCamera( 65, aspect, 3, 10 );
  86. cameraP.position.z = 7;
  87. cameraO = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, 3, 10 );
  88. cameraO.position.z = 7;
  89. var fov = THREE.Math.degToRad( cameraP.fov );
  90. var hyperfocus = ( cameraP.near + cameraP.far ) / 2;
  91. var _height = 2 * Math.tan( fov / 2 ) * hyperfocus;
  92. cameraO.zoom = height / _height;
  93. scene = new THREE.Scene();
  94. group = new THREE.Object3D();
  95. scene.add( group );
  96. var light = new THREE.PointLight( 0xddffdd, 1.0 );
  97. light.position.z = 70;
  98. light.position.y = -70;
  99. light.position.x = -70;
  100. scene.add( light );
  101. var light2 = new THREE.PointLight( 0xffdddd, 1.0 );
  102. light2.position.z = 70;
  103. light2.position.x = -70;
  104. light2.position.y = 70;
  105. scene.add( light2 );
  106. var light3 = new THREE.PointLight( 0xddddff, 1.0 );
  107. light3.position.z = 70;
  108. light3.position.x = 70;
  109. light3.position.y = -70;
  110. scene.add( light3 );
  111. var light3 = new THREE.AmbientLight( 0xffffff, 0.05 );
  112. scene.add( light3 );
  113. var geometry = new THREE.SphereBufferGeometry( 3, 48, 24 );
  114. for ( var i = 0; i < 120; i ++ ) {
  115. var material = new THREE.MeshStandardMaterial();
  116. material.roughness = 0.5 * Math.random() + 0.25;
  117. material.metalness = 0;
  118. material.color.setHSL( Math.random(), 1.0, 0.3 );
  119. var mesh = new THREE.Mesh( geometry, material );
  120. mesh.position.x = Math.random() * 4 - 2;
  121. mesh.position.y = Math.random() * 4 - 2;
  122. mesh.position.z = Math.random() * 4 - 2;
  123. mesh.rotation.x = Math.random();
  124. mesh.rotation.y = Math.random();
  125. mesh.rotation.z = Math.random();
  126. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 0.2 + 0.05;
  127. group.add( mesh );
  128. }
  129. // postprocessing
  130. composerP = new THREE.EffectComposer( renderer );
  131. msaaRenderPassP = new THREE.ManualMSAARenderPass( scene, cameraP );
  132. composerP.addPass( msaaRenderPassP );
  133. copyPassP = new THREE.ShaderPass( THREE.CopyShader );
  134. copyPassP.renderToScreen = true;
  135. composerP.addPass( copyPassP );
  136. composerO = new THREE.EffectComposer( renderer );
  137. msaaRenderPassO = new THREE.ManualMSAARenderPass( scene, cameraO );
  138. composerO.addPass( msaaRenderPassO );
  139. copyPassO = new THREE.ShaderPass( THREE.CopyShader );
  140. copyPassO.renderToScreen = true;
  141. composerO.addPass( copyPassO );
  142. window.addEventListener( 'resize', onWindowResize, false );
  143. }
  144. function onWindowResize() {
  145. var width = window.innerWidth;
  146. var height = window.innerHeight;
  147. var aspect = width / height;
  148. cameraP.aspect = aspect;
  149. cameraO.updateProjectionMatrix();
  150. cameraO.left = - height * aspect;
  151. cameraO.right = height * aspect;
  152. cameraO.top = height;
  153. cameraO.bottom = - height;
  154. cameraO.updateProjectionMatrix();
  155. renderer.setSize( width, height );
  156. var pixelRatio = renderer.getPixelRatio();
  157. var newWidth = Math.floor( width / pixelRatio ) || 1;
  158. var newHeight = Math.floor( height / pixelRatio ) || 1;
  159. composer.setSize( newWidth, newHeight );
  160. }
  161. function animate() {
  162. requestAnimationFrame( animate );
  163. stats.begin();
  164. for ( var i = 0; i < scene.children.length; i ++ ) {
  165. var child = scene.children[ i ];
  166. child.rotation.x += 0.005;
  167. child.rotation.y += 0.01;
  168. }
  169. msaaRenderPassP.sampleLevel = param.sampleLevel;
  170. msaaRenderPassP.unbiased = param.unbiased;
  171. msaaRenderPassO.sampleLevel = param.sampleLevel;
  172. msaaRenderPassO.unbiased = param.unbiased;
  173. if( param.camera === 'perspective' ){
  174. composerP.render();
  175. }else{
  176. composerO.render();
  177. }
  178. stats.end();
  179. }
  180. </script>
  181. <div>
  182. </body>
  183. </html>