webgl_postprocessing_ssaa_unbiased.html 6.7 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="http://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 {
  18. AmbientLight,
  19. Group,
  20. Math as _Math,
  21. Mesh,
  22. MeshStandardMaterial,
  23. OrthographicCamera,
  24. PerspectiveCamera,
  25. PointLight,
  26. Scene,
  27. SphereBufferGeometry,
  28. WebGLRenderer
  29. } from "../build/three.module.js";
  30. import Stats from './jsm/libs/stats.module.js';
  31. import { GUI } from './jsm/libs/dat.gui.module.js';
  32. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  33. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  34. import { SSAARenderPass } from './jsm/postprocessing/SSAARenderPass.js';
  35. import { CopyShader } from './jsm/shaders/CopyShader.js';
  36. var scene, renderer, composer, copyPass;
  37. var cameraP, ssaaRenderPassP;
  38. var cameraO, ssaaRenderPassO;
  39. var gui, stats;
  40. var params = {
  41. sampleLevel: 4,
  42. renderToScreen: false,
  43. unbiased: true,
  44. camera: 'perspective',
  45. clearColor: 'black',
  46. clearAlpha: 1.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, "autoRotate" );
  69. gui.open();
  70. }
  71. function init() {
  72. var container = document.getElementById( "container" );
  73. var width = window.innerWidth || 1;
  74. var height = window.innerHeight || 1;
  75. var aspect = width / height;
  76. var devicePixelRatio = window.devicePixelRatio || 1;
  77. renderer = new WebGLRenderer();
  78. renderer.setPixelRatio( devicePixelRatio );
  79. renderer.setSize( width, height );
  80. document.body.appendChild( renderer.domElement );
  81. stats = new Stats();
  82. container.appendChild( stats.dom );
  83. cameraP = new PerspectiveCamera( 65, aspect, 3, 10 );
  84. cameraP.position.z = 7;
  85. cameraO = new OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, 3, 10 );
  86. cameraO.position.z = 7;
  87. var fov = _Math.degToRad( cameraP.fov );
  88. var hyperfocus = ( cameraP.near + cameraP.far ) / 2;
  89. var _height = 2 * Math.tan( fov / 2 ) * hyperfocus;
  90. cameraO.zoom = height / _height;
  91. scene = new Scene();
  92. var group = new Group();
  93. scene.add( group );
  94. var light = new PointLight( 0xddffdd, 1.0 );
  95. light.position.z = 70;
  96. light.position.y = - 70;
  97. light.position.x = - 70;
  98. scene.add( light );
  99. var light2 = new PointLight( 0xffdddd, 1.0 );
  100. light2.position.z = 70;
  101. light2.position.x = - 70;
  102. light2.position.y = 70;
  103. scene.add( light2 );
  104. var light3 = new PointLight( 0xddddff, 1.0 );
  105. light3.position.z = 70;
  106. light3.position.x = 70;
  107. light3.position.y = - 70;
  108. scene.add( light3 );
  109. var light3 = new AmbientLight( 0xffffff, 0.05 );
  110. scene.add( light3 );
  111. var geometry = new SphereBufferGeometry( 3, 48, 24 );
  112. for ( var i = 0; i < 120; i ++ ) {
  113. var material = new MeshStandardMaterial();
  114. material.roughness = 0.5 * Math.random() + 0.25;
  115. material.metalness = 0;
  116. material.color.setHSL( Math.random(), 1.0, 0.3 );
  117. var mesh = new Mesh( geometry, material );
  118. mesh.position.x = Math.random() * 4 - 2;
  119. mesh.position.y = Math.random() * 4 - 2;
  120. mesh.position.z = Math.random() * 4 - 2;
  121. mesh.rotation.x = Math.random();
  122. mesh.rotation.y = Math.random();
  123. mesh.rotation.z = Math.random();
  124. mesh.scale.setScalar( Math.random() * 0.2 + 0.05 );
  125. group.add( mesh );
  126. }
  127. // postprocessing
  128. composer = new EffectComposer( renderer );
  129. composer.setPixelRatio( 1 ); // ensure pixel ratio is always 1 for performance reasons
  130. ssaaRenderPassP = new SSAARenderPass( scene, cameraP );
  131. composer.addPass( ssaaRenderPassP );
  132. ssaaRenderPassO = new SSAARenderPass( scene, cameraO );
  133. composer.addPass( ssaaRenderPassO );
  134. copyPass = new ShaderPass( CopyShader );
  135. composer.addPass( copyPass );
  136. window.addEventListener( 'resize', onWindowResize, false );
  137. }
  138. function onWindowResize() {
  139. var width = window.innerWidth;
  140. var height = window.innerHeight;
  141. var aspect = width / height;
  142. cameraP.aspect = aspect;
  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 ( var i = 0; i < scene.children.length; i ++ ) {
  157. var child = scene.children[ i ];
  158. child.rotation.x += 0.005;
  159. child.rotation.y += 0.01;
  160. }
  161. }
  162. var 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. copyPass.enabled = ! params.renderToScreen;
  177. composer.render();
  178. stats.end();
  179. }
  180. </script>
  181. </body>
  182. </html>