webgl_postprocessing_ssaa.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. <meta property="og:title" content="three.js webgl - postprocessing manual ssaa">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_postprocessing_ssaa.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_postprocessing_ssaa.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Unbiased Manual Supersampling Anti-Aliasing (SSAA) pass by <a href="https://clara.io" target="_blank" rel="noopener">Ben Houston</a><br/><br/>
  16. 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/>
  17. Turn off the "unbiased" feature to see the banding that results from accumulated rounding errors.
  18. </div>
  19. <div id="container"></div>
  20. <script type="importmap">
  21. {
  22. "imports": {
  23. "three": "../build/three.module.js",
  24. "three/addons/": "./jsm/"
  25. }
  26. }
  27. </script>
  28. <script type="module">
  29. import * as THREE from 'three';
  30. import Stats from 'three/addons/libs/stats.module.js';
  31. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  32. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  33. import { SSAARenderPass } from 'three/addons/postprocessing/SSAARenderPass.js';
  34. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  35. let scene, renderer, composer;
  36. let cameraP, ssaaRenderPassP;
  37. let cameraO, ssaaRenderPassO;
  38. let gui, stats;
  39. const params = {
  40. sampleLevel: 4,
  41. unbiased: true,
  42. camera: 'perspective',
  43. clearColor: 'black',
  44. clearAlpha: 1.0,
  45. viewOffsetX: 0,
  46. autoRotate: true
  47. };
  48. init();
  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. renderer.setAnimationLoop( animate );
  79. document.body.appendChild( renderer.domElement );
  80. stats = new Stats();
  81. container.appendChild( stats.dom );
  82. cameraP = new THREE.PerspectiveCamera( 65, aspect, 3, 10 );
  83. cameraP.position.z = 7;
  84. cameraP.setViewOffset( width, height, params.viewOffsetX, 0, width, height );
  85. cameraO = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, 3, 10 );
  86. cameraO.position.z = 7;
  87. const fov = THREE.MathUtils.degToRad( cameraP.fov );
  88. const hyperfocus = ( cameraP.near + cameraP.far ) / 2;
  89. const _height = 2 * Math.tan( fov / 2 ) * hyperfocus;
  90. cameraO.zoom = height / _height;
  91. scene = new THREE.Scene();
  92. const group = new THREE.Group();
  93. scene.add( group );
  94. const light = new THREE.PointLight( 0xefffef, 500 );
  95. light.position.z = 10;
  96. light.position.y = - 10;
  97. light.position.x = - 10;
  98. scene.add( light );
  99. const light2 = new THREE.PointLight( 0xffefef, 500 );
  100. light2.position.z = 10;
  101. light2.position.x = - 10;
  102. light2.position.y = 10;
  103. scene.add( light2 );
  104. const light3 = new THREE.PointLight( 0xefefff, 500 );
  105. light3.position.z = 10;
  106. light3.position.x = 10;
  107. light3.position.y = - 10;
  108. scene.add( light3 );
  109. const light4 = new THREE.AmbientLight( 0xffffff, 0.2 );
  110. scene.add( light4 );
  111. const geometry = new THREE.SphereGeometry( 3, 48, 24 );
  112. const material = new THREE.MeshStandardMaterial();
  113. material.roughness = 0.5;
  114. material.metalness = 0;
  115. const mesh = new THREE.InstancedMesh( geometry, material, 120 );
  116. const dummy = new THREE.Object3D();
  117. const color = new THREE.Color();
  118. for ( let i = 0; i < 120; i ++ ) {
  119. dummy.position.x = Math.random() * 4 - 2;
  120. dummy.position.y = Math.random() * 4 - 2;
  121. dummy.position.z = Math.random() * 4 - 2;
  122. dummy.rotation.x = Math.random();
  123. dummy.rotation.y = Math.random();
  124. dummy.rotation.z = Math.random();
  125. dummy.scale.setScalar( Math.random() * 0.2 + 0.05 );
  126. dummy.updateMatrix();
  127. mesh.setMatrixAt( i, dummy.matrix );
  128. color.setHSL( Math.random(), 1.0, 0.3 );
  129. mesh.setColorAt( i, color );
  130. }
  131. group.add( mesh );
  132. // postprocessing
  133. composer = new EffectComposer( renderer );
  134. composer.setPixelRatio( 1 ); // ensure pixel ratio is always 1 for performance reasons
  135. ssaaRenderPassP = new SSAARenderPass( scene, cameraP );
  136. composer.addPass( ssaaRenderPassP );
  137. ssaaRenderPassO = new SSAARenderPass( scene, cameraO );
  138. composer.addPass( ssaaRenderPassO );
  139. const outputPass = new OutputPass();
  140. composer.addPass( outputPass );
  141. window.addEventListener( 'resize', onWindowResize );
  142. }
  143. function onWindowResize() {
  144. const width = window.innerWidth;
  145. const height = window.innerHeight;
  146. const aspect = width / height;
  147. cameraP.aspect = aspect;
  148. cameraP.setViewOffset( width, height, params.viewOffsetX, 0, width, height );
  149. cameraO.updateProjectionMatrix();
  150. cameraO.left = - height * aspect;
  151. cameraO.right = height * aspect;
  152. cameraO.top = height;
  153. cameraO.bottom = - height;
  154. cameraO.updateProjectionMatrix();
  155. renderer.setSize( width, height );
  156. composer.setSize( width, height );
  157. }
  158. function animate() {
  159. stats.begin();
  160. if ( params.autoRotate ) {
  161. for ( let i = 0; i < scene.children.length; i ++ ) {
  162. const child = scene.children[ i ];
  163. child.rotation.x += 0.005;
  164. child.rotation.y += 0.01;
  165. }
  166. }
  167. let newColor = ssaaRenderPassP.clearColor;
  168. switch ( params.clearColor ) {
  169. case 'blue': newColor = 0x0000ff; break;
  170. case 'red': newColor = 0xff0000; break;
  171. case 'green': newColor = 0x00ff00; break;
  172. case 'white': newColor = 0xffffff; break;
  173. case 'black': newColor = 0x000000; break;
  174. }
  175. ssaaRenderPassP.clearColor = ssaaRenderPassO.clearColor = newColor;
  176. ssaaRenderPassP.clearAlpha = ssaaRenderPassO.clearAlpha = params.clearAlpha;
  177. ssaaRenderPassP.sampleLevel = ssaaRenderPassO.sampleLevel = params.sampleLevel;
  178. ssaaRenderPassP.unbiased = ssaaRenderPassO.unbiased = params.unbiased;
  179. ssaaRenderPassP.enabled = ( params.camera === 'perspective' );
  180. ssaaRenderPassO.enabled = ( params.camera === 'orthographic' );
  181. cameraP.view.offsetX = params.viewOffsetX;
  182. composer.render();
  183. stats.end();
  184. }
  185. </script>
  186. </body>
  187. </html>
粤ICP备19079148号