webgl_postprocessing_ssaa.html 6.9 KB

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