webgl_postprocessing_sao.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 { SAOPass } from 'three/addons/postprocessing/SAOPass.js';
  27. let container, stats;
  28. let camera, scene, renderer;
  29. let saoPass;
  30. let group;
  31. init();
  32. function init() {
  33. container = document.createElement( 'div' );
  34. document.body.appendChild( container );
  35. const width = window.innerWidth;
  36. const height = window.innerHeight;
  37. renderer = new THREE.WebGLRenderer( { outputBufferType: THREE.HalfFloatType } );
  38. renderer.setPixelRatio( window.devicePixelRatio );
  39. renderer.setSize( width, height );
  40. renderer.setAnimationLoop( animate );
  41. document.body.appendChild( renderer.domElement );
  42. camera = new THREE.PerspectiveCamera( 65, width / height, 3, 10 );
  43. camera.position.z = 7;
  44. scene = new THREE.Scene();
  45. group = new THREE.Object3D();
  46. scene.add( group );
  47. const light = new THREE.PointLight( 0xefffef, 500 );
  48. light.position.z = 10;
  49. light.position.y = - 10;
  50. light.position.x = - 10;
  51. scene.add( light );
  52. const light2 = new THREE.PointLight( 0xffefef, 500 );
  53. light2.position.z = 10;
  54. light2.position.x = - 10;
  55. light2.position.y = 10;
  56. scene.add( light2 );
  57. const light3 = new THREE.PointLight( 0xefefff, 500 );
  58. light3.position.z = 10;
  59. light3.position.x = 10;
  60. light3.position.y = - 10;
  61. scene.add( light3 );
  62. const light4 = new THREE.AmbientLight( 0xffffff, 0.2 );
  63. scene.add( light4 );
  64. const geometry = new THREE.SphereGeometry( 3, 48, 24 );
  65. const material = new THREE.MeshStandardMaterial();
  66. material.roughness = 0.5;
  67. material.metalness = 0;
  68. const mesh = new THREE.InstancedMesh( geometry, material, 120 );
  69. const dummy = new THREE.Object3D();
  70. const color = new THREE.Color();
  71. for ( let i = 0; i < 120; i ++ ) {
  72. dummy.position.x = Math.random() * 4 - 2;
  73. dummy.position.y = Math.random() * 4 - 2;
  74. dummy.position.z = Math.random() * 4 - 2;
  75. dummy.rotation.x = Math.random();
  76. dummy.rotation.y = Math.random();
  77. dummy.rotation.z = Math.random();
  78. const scale = Math.random() * 0.2 + 0.05;
  79. dummy.scale.set( scale, scale, scale );
  80. dummy.updateMatrix();
  81. mesh.setMatrixAt( i, dummy.matrix );
  82. color.setHSL( Math.random(), 1.0, 0.3 );
  83. mesh.setColorAt( i, color );
  84. }
  85. group.add( mesh );
  86. stats = new Stats();
  87. container.appendChild( stats.dom );
  88. saoPass = new SAOPass( scene, camera );
  89. renderer.setEffects( [ saoPass ] );
  90. // Init gui
  91. const gui = new GUI();
  92. gui.add( saoPass.params, 'output', {
  93. 'Default': SAOPass.OUTPUT.Default,
  94. 'SAO Only': SAOPass.OUTPUT.SAO,
  95. 'Normal': SAOPass.OUTPUT.Normal
  96. } ).onChange( function ( value ) {
  97. saoPass.params.output = value;
  98. } );
  99. gui.add( saoPass.params, 'saoBias', - 1, 1 );
  100. gui.add( saoPass.params, 'saoIntensity', 0, 1 );
  101. gui.add( saoPass.params, 'saoScale', 0, 10 );
  102. gui.add( saoPass.params, 'saoKernelRadius', 1, 100 );
  103. gui.add( saoPass.params, 'saoMinResolution', 0, 1 );
  104. gui.add( saoPass.params, 'saoBlur' );
  105. gui.add( saoPass.params, 'saoBlurRadius', 0, 200 );
  106. gui.add( saoPass.params, 'saoBlurStdDev', 0.5, 150 );
  107. gui.add( saoPass.params, 'saoBlurDepthCutoff', 0.0, 0.1 );
  108. gui.add( saoPass, 'enabled' );
  109. window.addEventListener( 'resize', onWindowResize );
  110. }
  111. function onWindowResize() {
  112. const width = window.innerWidth || 1;
  113. const height = window.innerHeight || 1;
  114. camera.aspect = width / height;
  115. camera.updateProjectionMatrix();
  116. renderer.setSize( width, height );
  117. }
  118. function animate() {
  119. stats.begin();
  120. render();
  121. stats.end();
  122. }
  123. function render() {
  124. const timer = performance.now();
  125. group.rotation.x = timer * 0.0002;
  126. group.rotation.y = timer * 0.0001;
  127. renderer.render( scene, camera );
  128. }
  129. </script>
  130. </body>
  131. </html>
粤ICP备19079148号