webgl_postprocessing_sao.html 5.1 KB

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