webgl_postprocessing_ssaa_unbiased.html 6.6 KB

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