webgl_postprocessing_sao.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - post processing - Scalable Ambient Occlusion (SAO)</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 - post processing - Scalable Ambient Occlusion (SAO)">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_postprocessing_sao.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_postprocessing_sao.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 noreferrer">three.js</a> - Scalable Ambient Occlusion (SAO)<br/>
  16. shader by <a href="http://clara.io">Ben Houston</a> / Post-processing pass by <a href="http://ludobaka.github.io">Ludobaka</a>
  17. </div>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  30. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  31. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  32. import { SAOPass } from 'three/addons/postprocessing/SAOPass.js';
  33. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  34. let container, stats;
  35. let camera, scene, renderer;
  36. let composer, renderPass, saoPass;
  37. let group;
  38. init();
  39. function init() {
  40. container = document.createElement( 'div' );
  41. document.body.appendChild( container );
  42. const width = window.innerWidth;
  43. const height = window.innerHeight;
  44. renderer = new THREE.WebGLRenderer();
  45. renderer.setPixelRatio( window.devicePixelRatio );
  46. renderer.setSize( width, height );
  47. renderer.setAnimationLoop( animate );
  48. document.body.appendChild( renderer.domElement );
  49. camera = new THREE.PerspectiveCamera( 65, width / height, 3, 10 );
  50. camera.position.z = 7;
  51. scene = new THREE.Scene();
  52. group = new THREE.Object3D();
  53. scene.add( group );
  54. const light = new THREE.PointLight( 0xefffef, 500 );
  55. light.position.z = 10;
  56. light.position.y = - 10;
  57. light.position.x = - 10;
  58. scene.add( light );
  59. const light2 = new THREE.PointLight( 0xffefef, 500 );
  60. light2.position.z = 10;
  61. light2.position.x = - 10;
  62. light2.position.y = 10;
  63. scene.add( light2 );
  64. const light3 = new THREE.PointLight( 0xefefff, 500 );
  65. light3.position.z = 10;
  66. light3.position.x = 10;
  67. light3.position.y = - 10;
  68. scene.add( light3 );
  69. const light4 = new THREE.AmbientLight( 0xffffff, 0.2 );
  70. scene.add( light4 );
  71. const geometry = new THREE.SphereGeometry( 3, 48, 24 );
  72. const material = new THREE.MeshStandardMaterial();
  73. material.roughness = 0.5;
  74. material.metalness = 0;
  75. const mesh = new THREE.InstancedMesh( geometry, material, 120 );
  76. const dummy = new THREE.Object3D();
  77. const color = new THREE.Color();
  78. for ( let i = 0; i < 120; i ++ ) {
  79. dummy.position.x = Math.random() * 4 - 2;
  80. dummy.position.y = Math.random() * 4 - 2;
  81. dummy.position.z = Math.random() * 4 - 2;
  82. dummy.rotation.x = Math.random();
  83. dummy.rotation.y = Math.random();
  84. dummy.rotation.z = Math.random();
  85. const scale = Math.random() * 0.2 + 0.05;
  86. dummy.scale.set( scale, scale, scale );
  87. dummy.updateMatrix();
  88. mesh.setMatrixAt( i, dummy.matrix );
  89. color.setHSL( Math.random(), 1.0, 0.3 );
  90. mesh.setColorAt( i, color );
  91. }
  92. group.add( mesh );
  93. stats = new Stats();
  94. container.appendChild( stats.dom );
  95. composer = new EffectComposer( renderer );
  96. renderPass = new RenderPass( scene, camera );
  97. composer.addPass( renderPass );
  98. saoPass = new SAOPass( scene, camera );
  99. composer.addPass( saoPass );
  100. const outputPass = new OutputPass();
  101. composer.addPass( outputPass );
  102. // Init gui
  103. const gui = new GUI();
  104. gui.add( saoPass.params, 'output', {
  105. 'Default': SAOPass.OUTPUT.Default,
  106. 'SAO Only': SAOPass.OUTPUT.SAO,
  107. 'Normal': SAOPass.OUTPUT.Normal
  108. } ).onChange( function ( value ) {
  109. saoPass.params.output = value;
  110. } );
  111. gui.add( saoPass.params, 'saoBias', - 1, 1 );
  112. gui.add( saoPass.params, 'saoIntensity', 0, 1 );
  113. gui.add( saoPass.params, 'saoScale', 0, 10 );
  114. gui.add( saoPass.params, 'saoKernelRadius', 1, 100 );
  115. gui.add( saoPass.params, 'saoMinResolution', 0, 1 );
  116. gui.add( saoPass.params, 'saoBlur' );
  117. gui.add( saoPass.params, 'saoBlurRadius', 0, 200 );
  118. gui.add( saoPass.params, 'saoBlurStdDev', 0.5, 150 );
  119. gui.add( saoPass.params, 'saoBlurDepthCutoff', 0.0, 0.1 );
  120. gui.add( saoPass, 'enabled' );
  121. window.addEventListener( 'resize', onWindowResize );
  122. }
  123. function onWindowResize() {
  124. const width = window.innerWidth || 1;
  125. const height = window.innerHeight || 1;
  126. camera.aspect = width / height;
  127. camera.updateProjectionMatrix();
  128. renderer.setSize( width, height );
  129. composer.setSize( width, height );
  130. }
  131. function animate() {
  132. stats.begin();
  133. render();
  134. stats.end();
  135. }
  136. function render() {
  137. const timer = performance.now();
  138. group.rotation.x = timer * 0.0002;
  139. group.rotation.y = timer * 0.0001;
  140. composer.render();
  141. }
  142. </script>
  143. </body>
  144. </html>
粤ICP备19079148号