webgl_postprocessing_msaa_unbiased.html 7.2 KB

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