webgl_postprocessing_ssaa.html 6.9 KB

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