1
0

webgl_postprocessing_ssaa.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 Supersampling 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 { SSAARenderPass } from 'three/addons/postprocessing/SSAARenderPass.js';
  29. let scene, renderer;
  30. let cameraP, ssaaRenderPassP;
  31. let cameraO, ssaaRenderPassO;
  32. let activePass;
  33. let gui, stats;
  34. const params = {
  35. sampleLevel: 4,
  36. unbiased: true,
  37. camera: 'perspective',
  38. clearColor: 'black',
  39. clearAlpha: 1.0,
  40. viewOffsetX: 0,
  41. autoRotate: true
  42. };
  43. init();
  44. clearGui();
  45. function clearGui() {
  46. if ( gui ) gui.destroy();
  47. gui = new GUI();
  48. gui.add( params, 'unbiased' );
  49. gui.add( params, 'sampleLevel', {
  50. 'Level 0: 1 Sample': 0,
  51. 'Level 1: 2 Samples': 1,
  52. 'Level 2: 4 Samples': 2,
  53. 'Level 3: 8 Samples': 3,
  54. 'Level 4: 16 Samples': 4,
  55. 'Level 5: 32 Samples': 5
  56. } );
  57. gui.add( params, 'camera', [ 'perspective', 'orthographic' ] );
  58. gui.add( params, 'clearColor', [ 'black', 'white', 'blue', 'green', 'red' ] );
  59. gui.add( params, 'clearAlpha', 0, 1 );
  60. gui.add( params, 'viewOffsetX', - 100, 100 );
  61. gui.add( params, 'autoRotate' );
  62. gui.open();
  63. }
  64. function init() {
  65. const container = document.getElementById( 'container' );
  66. const width = window.innerWidth || 1;
  67. const height = window.innerHeight || 1;
  68. const aspect = width / height;
  69. const devicePixelRatio = window.devicePixelRatio || 1;
  70. renderer = new THREE.WebGLRenderer( { outputBufferType: THREE.HalfFloatType } );
  71. renderer.setPixelRatio( devicePixelRatio );
  72. renderer.setSize( width, height );
  73. renderer.setAnimationLoop( animate );
  74. document.body.appendChild( renderer.domElement );
  75. stats = new Stats();
  76. container.appendChild( stats.dom );
  77. cameraP = new THREE.PerspectiveCamera( 65, aspect, 3, 10 );
  78. cameraP.position.z = 7;
  79. cameraP.setViewOffset( width, height, params.viewOffsetX, 0, width, height );
  80. cameraO = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, 3, 10 );
  81. cameraO.position.z = 7;
  82. const fov = THREE.MathUtils.degToRad( cameraP.fov );
  83. const hyperfocus = ( cameraP.near + cameraP.far ) / 2;
  84. const _height = 2 * Math.tan( fov / 2 ) * hyperfocus;
  85. cameraO.zoom = height / _height;
  86. scene = new THREE.Scene();
  87. const group = new THREE.Group();
  88. scene.add( group );
  89. const light = new THREE.PointLight( 0xefffef, 500 );
  90. light.position.z = 10;
  91. light.position.y = - 10;
  92. light.position.x = - 10;
  93. scene.add( light );
  94. const light2 = new THREE.PointLight( 0xffefef, 500 );
  95. light2.position.z = 10;
  96. light2.position.x = - 10;
  97. light2.position.y = 10;
  98. scene.add( light2 );
  99. const light3 = new THREE.PointLight( 0xefefff, 500 );
  100. light3.position.z = 10;
  101. light3.position.x = 10;
  102. light3.position.y = - 10;
  103. scene.add( light3 );
  104. const light4 = new THREE.AmbientLight( 0xffffff, 0.2 );
  105. scene.add( light4 );
  106. const geometry = new THREE.SphereGeometry( 3, 48, 24 );
  107. const material = new THREE.MeshStandardMaterial();
  108. material.roughness = 0.5;
  109. material.metalness = 0;
  110. const mesh = new THREE.InstancedMesh( geometry, material, 120 );
  111. const dummy = new THREE.Object3D();
  112. const color = new THREE.Color();
  113. for ( let i = 0; i < 120; i ++ ) {
  114. dummy.position.x = Math.random() * 4 - 2;
  115. dummy.position.y = Math.random() * 4 - 2;
  116. dummy.position.z = Math.random() * 4 - 2;
  117. dummy.rotation.x = Math.random();
  118. dummy.rotation.y = Math.random();
  119. dummy.rotation.z = Math.random();
  120. dummy.scale.setScalar( Math.random() * 0.2 + 0.05 );
  121. dummy.updateMatrix();
  122. mesh.setMatrixAt( i, dummy.matrix );
  123. color.setHSL( Math.random(), 1.0, 0.3 );
  124. mesh.setColorAt( i, color );
  125. }
  126. group.add( mesh );
  127. // postprocessing
  128. ssaaRenderPassP = new SSAARenderPass( scene, cameraP );
  129. ssaaRenderPassO = new SSAARenderPass( scene, cameraO );
  130. activePass = ssaaRenderPassP;
  131. renderer.setEffects( [ activePass ] );
  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. }
  148. function animate() {
  149. stats.begin();
  150. if ( params.autoRotate ) {
  151. for ( let i = 0; i < scene.children.length; i ++ ) {
  152. const child = scene.children[ i ];
  153. child.rotation.x += 0.005;
  154. child.rotation.y += 0.01;
  155. }
  156. }
  157. let newColor = ssaaRenderPassP.clearColor;
  158. switch ( params.clearColor ) {
  159. case 'blue': newColor = 0x0000ff; break;
  160. case 'red': newColor = 0xff0000; break;
  161. case 'green': newColor = 0x00ff00; break;
  162. case 'white': newColor = 0xffffff; break;
  163. case 'black': newColor = 0x000000; break;
  164. }
  165. ssaaRenderPassP.clearColor = ssaaRenderPassO.clearColor = newColor;
  166. ssaaRenderPassP.clearAlpha = ssaaRenderPassO.clearAlpha = params.clearAlpha;
  167. ssaaRenderPassP.sampleLevel = ssaaRenderPassO.sampleLevel = params.sampleLevel;
  168. ssaaRenderPassP.unbiased = ssaaRenderPassO.unbiased = params.unbiased;
  169. const nextPass = params.camera === 'perspective' ? ssaaRenderPassP : ssaaRenderPassO;
  170. const activeCamera = params.camera === 'perspective' ? cameraP : cameraO;
  171. if ( nextPass !== activePass ) {
  172. activePass = nextPass;
  173. renderer.setEffects( [ activePass ] );
  174. }
  175. cameraP.view.offsetX = params.viewOffsetX;
  176. renderer.render( scene, activeCamera );
  177. stats.end();
  178. }
  179. </script>
  180. </body>
  181. </html>
粤ICP备19079148号