webgl_postprocessing_msaa_unbiased.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. a { color: #88f; }
  19. #info {
  20. color: #fff;
  21. position: absolute;
  22. top: 10px;
  23. width: 100%;
  24. text-align: center;
  25. display:block;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div id="info">
  31. <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/>
  32. 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/>
  33. Turn off the "unbiased" feature to see the banding that results from accumulated rounding errors.
  34. </div>
  35. <div id="container"></div>
  36. <script src="../build/three.js"></script>
  37. <script src="js/libs/stats.min.js"></script>
  38. <script src="js/libs/dat.gui.min.js"></script>
  39. <script src="js/shaders/CopyShader.js"></script>
  40. <script src="js/postprocessing/EffectComposer.js"></script>
  41. <script src="js/postprocessing/ManualMSAARenderPass.js"></script>
  42. <script src="js/postprocessing/RenderPass.js"></script>
  43. <script src="js/postprocessing/MaskPass.js"></script>
  44. <script src="js/postprocessing/ShaderPass.js"></script>
  45. <script>
  46. var scene, renderer, composer, copyPass;
  47. var cameraP, msaaRenderPassP;
  48. var cameraO, msaaRenderPassO;
  49. var gui, stats, texture;
  50. var params = {
  51. sampleLevel: 4,
  52. unbiased: true,
  53. camera: 'perspective',
  54. clearColor: 'black',
  55. clearAlpha: 1.0,
  56. autoRotate: true
  57. };
  58. init();
  59. animate();
  60. clearGui();
  61. function clearGui() {
  62. if ( gui ) gui.destroy();
  63. gui = new dat.GUI();
  64. gui.add( params, "unbiased" );
  65. gui.add( params, 'sampleLevel', {
  66. 'Level 0: 1 Sample': 0,
  67. 'Level 1: 2 Samples': 1,
  68. 'Level 2: 4 Samples': 2,
  69. 'Level 3: 8 Samples': 3,
  70. 'Level 4: 16 Samples': 4,
  71. 'Level 5: 32 Samples': 5
  72. } );
  73. gui.add( params, 'camera', [ 'perspective', 'orthographic' ] );
  74. gui.add( params, "clearColor", [ 'black', 'white', 'blue', 'green', 'red' ] );
  75. gui.add( params, "clearAlpha", 0, 1 );
  76. gui.add( params, "autoRotate" );
  77. gui.open();
  78. }
  79. function init() {
  80. container = document.getElementById( "container" );
  81. var width = window.innerWidth || 1;
  82. var height = window.innerHeight || 1;
  83. var aspect = width / height;
  84. var devicePixelRatio = window.devicePixelRatio || 1;
  85. renderer = new THREE.WebGLRenderer( { antialias: false } );
  86. renderer.setPixelRatio( devicePixelRatio );
  87. renderer.setSize( width, height );
  88. document.body.appendChild( renderer.domElement );
  89. stats = new Stats();
  90. container.appendChild( stats.dom );
  91. //
  92. cameraP = new THREE.PerspectiveCamera( 65, aspect, 3, 10 );
  93. cameraP.position.z = 7;
  94. cameraO = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, 3, 10 );
  95. cameraO.position.z = 7;
  96. var fov = THREE.Math.degToRad( cameraP.fov );
  97. var hyperfocus = ( cameraP.near + cameraP.far ) / 2;
  98. var _height = 2 * Math.tan( fov / 2 ) * hyperfocus;
  99. cameraO.zoom = height / _height;
  100. scene = new THREE.Scene();
  101. group = new THREE.Object3D();
  102. scene.add( group );
  103. var light = new THREE.PointLight( 0xddffdd, 1.0 );
  104. light.position.z = 70;
  105. light.position.y = -70;
  106. light.position.x = -70;
  107. scene.add( light );
  108. var light2 = new THREE.PointLight( 0xffdddd, 1.0 );
  109. light2.position.z = 70;
  110. light2.position.x = -70;
  111. light2.position.y = 70;
  112. scene.add( light2 );
  113. var light3 = new THREE.PointLight( 0xddddff, 1.0 );
  114. light3.position.z = 70;
  115. light3.position.x = 70;
  116. light3.position.y = -70;
  117. scene.add( light3 );
  118. var light3 = new THREE.AmbientLight( 0xffffff, 0.05 );
  119. scene.add( light3 );
  120. var geometry = new THREE.SphereBufferGeometry( 3, 48, 24 );
  121. for ( var i = 0; i < 120; i ++ ) {
  122. var material = new THREE.MeshStandardMaterial();
  123. material.roughness = 0.5 * Math.random() + 0.25;
  124. material.metalness = 0;
  125. material.color.setHSL( Math.random(), 1.0, 0.3 );
  126. var mesh = new THREE.Mesh( geometry, material );
  127. mesh.position.x = Math.random() * 4 - 2;
  128. mesh.position.y = Math.random() * 4 - 2;
  129. mesh.position.z = Math.random() * 4 - 2;
  130. mesh.rotation.x = Math.random();
  131. mesh.rotation.y = Math.random();
  132. mesh.rotation.z = Math.random();
  133. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 0.2 + 0.05;
  134. group.add( mesh );
  135. }
  136. // postprocessing
  137. composer = new THREE.EffectComposer( renderer );
  138. msaaRenderPassP = new THREE.ManualMSAARenderPass( scene, cameraP );
  139. composer.addPass( msaaRenderPassP );
  140. msaaRenderPassO = new THREE.ManualMSAARenderPass( scene, cameraO );
  141. composer.addPass( msaaRenderPassO );
  142. copyPass = new THREE.ShaderPass( THREE.CopyShader );
  143. copyPass.renderToScreen = true;
  144. composer.addPass( copyPass );
  145. window.addEventListener( 'resize', onWindowResize, false );
  146. }
  147. function onWindowResize() {
  148. var width = window.innerWidth;
  149. var height = window.innerHeight;
  150. var aspect = width / height;
  151. cameraP.aspect = aspect;
  152. cameraO.updateProjectionMatrix();
  153. cameraO.left = - height * aspect;
  154. cameraO.right = height * aspect;
  155. cameraO.top = height;
  156. cameraO.bottom = - height;
  157. cameraO.updateProjectionMatrix();
  158. renderer.setSize( width, height );
  159. var pixelRatio = renderer.getPixelRatio();
  160. var newWidth = Math.floor( width / pixelRatio ) || 1;
  161. var newHeight = Math.floor( height / pixelRatio ) || 1;
  162. composer.setSize( newWidth, newHeight );
  163. }
  164. function animate() {
  165. requestAnimationFrame( animate );
  166. stats.begin();
  167. if( params.autoRotate ) {
  168. for ( var i = 0; i < scene.children.length; i ++ ) {
  169. var child = scene.children[ i ];
  170. child.rotation.x += 0.005;
  171. child.rotation.y += 0.01;
  172. }
  173. }
  174. var newColor = msaaRenderPassP.clearColor;
  175. switch( params.clearColor ) {
  176. case 'blue': newColor = 0x0000ff; break;
  177. case 'red': newColor = 0xff0000; break;
  178. case 'green': newColor = 0x00ff00; break;
  179. case 'white': newColor = 0xffffff; break;
  180. case 'black': newColor = 0x000000; break;
  181. }
  182. msaaRenderPassP.clearColor = msaaRenderPassO.clearColor = newColor;
  183. msaaRenderPassP.clearAlpha = msaaRenderPassO.clearAlpha = params.clearAlpha;
  184. msaaRenderPassP.sampleLevel = msaaRenderPassO.sampleLevel = params.sampleLevel;
  185. msaaRenderPassP.unbiased = msaaRenderPassO.unbiased = params.unbiased;
  186. msaaRenderPassP.enabled = ( params.camera === 'perspective' );
  187. msaaRenderPassO.enabled = ( params.camera === 'orthographic' );
  188. composer.render();
  189. stats.end();
  190. }
  191. </script>
  192. <div>
  193. </body>
  194. </html>