webgl_postprocessing_ssaa.html 7.1 KB

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