webgl_postprocessing_ssaa_unbiased.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing manual ssaa</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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Unbiased Manual Supersamling Anti-Aliasing (SSAA) pass by <a href="https://clara.io" target="_blank" rel="noopener">Ben Houston</a><br/><br/>
  12. This example shows how to unbias the rounding errors accumulated using high number of SSAA samples on a 8-bit per channel buffer.<br/><br/>
  13. Turn off the "unbiased" feature to see the banding that results from accumulated rounding errors.
  14. </div>
  15. <div id="container"></div>
  16. <script src="../build/three.js"></script>
  17. <script src="js/libs/stats.min.js"></script>
  18. <script src="js/libs/dat.gui.min.js"></script>
  19. <script src="js/shaders/CopyShader.js"></script>
  20. <script src="js/postprocessing/EffectComposer.js"></script>
  21. <script src="js/postprocessing/SSAARenderPass.js"></script>
  22. <script src="js/postprocessing/RenderPass.js"></script>
  23. <script src="js/postprocessing/MaskPass.js"></script>
  24. <script src="js/postprocessing/ShaderPass.js"></script>
  25. <script>
  26. var scene, renderer, composer, copyPass;
  27. var cameraP, ssaaRenderPassP;
  28. var cameraO, ssaaRenderPassO;
  29. var gui, stats;
  30. var params = {
  31. sampleLevel: 4,
  32. renderToScreen: false,
  33. unbiased: true,
  34. camera: 'perspective',
  35. clearColor: 'black',
  36. clearAlpha: 1.0,
  37. autoRotate: true
  38. };
  39. init();
  40. animate();
  41. clearGui();
  42. function clearGui() {
  43. if ( gui ) gui.destroy();
  44. gui = new dat.GUI();
  45. gui.add( params, "unbiased" );
  46. gui.add( params, "renderToScreen" );
  47. gui.add( params, 'sampleLevel', {
  48. 'Level 0: 1 Sample': 0,
  49. 'Level 1: 2 Samples': 1,
  50. 'Level 2: 4 Samples': 2,
  51. 'Level 3: 8 Samples': 3,
  52. 'Level 4: 16 Samples': 4,
  53. 'Level 5: 32 Samples': 5
  54. } );
  55. gui.add( params, 'camera', [ 'perspective', 'orthographic' ] );
  56. gui.add( params, "clearColor", [ 'black', 'white', 'blue', 'green', 'red' ] );
  57. gui.add( params, "clearAlpha", 0, 1 );
  58. gui.add( params, "autoRotate" );
  59. gui.open();
  60. }
  61. function init() {
  62. var container = document.getElementById( "container" );
  63. var width = window.innerWidth || 1;
  64. var height = window.innerHeight || 1;
  65. var aspect = width / height;
  66. var devicePixelRatio = window.devicePixelRatio || 1;
  67. renderer = new THREE.WebGLRenderer();
  68. renderer.setPixelRatio( devicePixelRatio );
  69. renderer.setSize( width, height );
  70. document.body.appendChild( renderer.domElement );
  71. stats = new Stats();
  72. container.appendChild( stats.dom );
  73. cameraP = new THREE.PerspectiveCamera( 65, aspect, 3, 10 );
  74. cameraP.position.z = 7;
  75. cameraO = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, 3, 10 );
  76. cameraO.position.z = 7;
  77. var fov = THREE.Math.degToRad( cameraP.fov );
  78. var hyperfocus = ( cameraP.near + cameraP.far ) / 2;
  79. var _height = 2 * Math.tan( fov / 2 ) * hyperfocus;
  80. cameraO.zoom = height / _height;
  81. scene = new THREE.Scene();
  82. var group = new THREE.Group();
  83. scene.add( group );
  84. var light = new THREE.PointLight( 0xddffdd, 1.0 );
  85. light.position.z = 70;
  86. light.position.y = - 70;
  87. light.position.x = - 70;
  88. scene.add( light );
  89. var light2 = new THREE.PointLight( 0xffdddd, 1.0 );
  90. light2.position.z = 70;
  91. light2.position.x = - 70;
  92. light2.position.y = 70;
  93. scene.add( light2 );
  94. var light3 = new THREE.PointLight( 0xddddff, 1.0 );
  95. light3.position.z = 70;
  96. light3.position.x = 70;
  97. light3.position.y = - 70;
  98. scene.add( light3 );
  99. var light3 = new THREE.AmbientLight( 0xffffff, 0.05 );
  100. scene.add( light3 );
  101. var geometry = new THREE.SphereBufferGeometry( 3, 48, 24 );
  102. for ( var i = 0; i < 120; i ++ ) {
  103. var material = new THREE.MeshStandardMaterial();
  104. material.roughness = 0.5 * Math.random() + 0.25;
  105. material.metalness = 0;
  106. material.color.setHSL( Math.random(), 1.0, 0.3 );
  107. var mesh = new THREE.Mesh( geometry, material );
  108. mesh.position.x = Math.random() * 4 - 2;
  109. mesh.position.y = Math.random() * 4 - 2;
  110. mesh.position.z = Math.random() * 4 - 2;
  111. mesh.rotation.x = Math.random();
  112. mesh.rotation.y = Math.random();
  113. mesh.rotation.z = Math.random();
  114. mesh.scale.setScalar( Math.random() * 0.2 + 0.05 );
  115. group.add( mesh );
  116. }
  117. // postprocessing
  118. composer = new THREE.EffectComposer( renderer );
  119. composer.setPixelRatio( 1 ); // ensure pixel ratio is always 1 for performance reasons
  120. ssaaRenderPassP = new THREE.SSAARenderPass( scene, cameraP );
  121. composer.addPass( ssaaRenderPassP );
  122. ssaaRenderPassO = new THREE.SSAARenderPass( scene, cameraO );
  123. composer.addPass( ssaaRenderPassO );
  124. copyPass = new THREE.ShaderPass( THREE.CopyShader );
  125. composer.addPass( copyPass );
  126. window.addEventListener( 'resize', onWindowResize, false );
  127. }
  128. function onWindowResize() {
  129. var width = window.innerWidth;
  130. var height = window.innerHeight;
  131. var aspect = width / height;
  132. cameraP.aspect = aspect;
  133. cameraO.updateProjectionMatrix();
  134. cameraO.left = - height * aspect;
  135. cameraO.right = height * aspect;
  136. cameraO.top = height;
  137. cameraO.bottom = - height;
  138. cameraO.updateProjectionMatrix();
  139. renderer.setSize( width, height );
  140. composer.setSize( width, height );
  141. }
  142. function animate() {
  143. requestAnimationFrame( animate );
  144. stats.begin();
  145. if ( params.autoRotate ) {
  146. for ( var i = 0; i < scene.children.length; i ++ ) {
  147. var child = scene.children[ i ];
  148. child.rotation.x += 0.005;
  149. child.rotation.y += 0.01;
  150. }
  151. }
  152. var newColor = ssaaRenderPassP.clearColor;
  153. switch ( params.clearColor ) {
  154. case 'blue': newColor = 0x0000ff; break;
  155. case 'red': newColor = 0xff0000; break;
  156. case 'green': newColor = 0x00ff00; break;
  157. case 'white': newColor = 0xffffff; break;
  158. case 'black': newColor = 0x000000; break;
  159. }
  160. ssaaRenderPassP.clearColor = ssaaRenderPassO.clearColor = newColor;
  161. ssaaRenderPassP.clearAlpha = ssaaRenderPassO.clearAlpha = params.clearAlpha;
  162. ssaaRenderPassP.sampleLevel = ssaaRenderPassO.sampleLevel = params.sampleLevel;
  163. ssaaRenderPassP.unbiased = ssaaRenderPassO.unbiased = params.unbiased;
  164. ssaaRenderPassP.enabled = ( params.camera === 'perspective' );
  165. ssaaRenderPassO.enabled = ( params.camera === 'orthographic' );
  166. copyPass.enabled = ! params.renderToScreen;
  167. composer.render();
  168. stats.end();
  169. }
  170. </script>
  171. </body>
  172. </html>