webgl_postprocessing_ssaa.html 7.3 KB

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