webgl_postprocessing_ssrr.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <head>
  5. <title>three.js webgl - postprocessing - Screen Space Refraction</title>
  6. <meta charset="utf-8">
  7. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  8. <link type="text/css" rel="stylesheet" href="main.css">
  9. </head>
  10. <body>
  11. <div id="container"></div>
  12. <div id="info">
  13. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> -
  14. SSRrPass demo by <a href="https://github.com/gonnavis" target="_blank">Vis</a>.<br />
  15. click object to toggle transparent<br/>
  16. </div>
  17. <script type="module">
  18. import * as THREE from '../build/three.module.js';
  19. import Stats from './jsm/libs/stats.module.js';
  20. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  21. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  22. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  23. import { SSRrPass } from './jsm/postprocessing/SSRrPass.js';
  24. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  25. import { GammaCorrectionShader } from './jsm/shaders/GammaCorrectionShader.js';
  26. import { DRACOLoader } from './jsm/loaders/DRACOLoader.js';
  27. const params = {
  28. enableSSRr: true,
  29. autoRotate: true,
  30. };
  31. let composer;
  32. let ssrrPass;
  33. let gui;
  34. let stats;
  35. let controls;
  36. let camera, scene, renderer;
  37. const objects = [];
  38. const selects = [];
  39. const raycaster = new THREE.Raycaster();
  40. const mouseDown = new THREE.Vector2();
  41. const mouse = new THREE.Vector2();
  42. const container = document.querySelector( '#container' );
  43. // Configure and create Draco decoder.
  44. const dracoLoader = new DRACOLoader();
  45. dracoLoader.setDecoderPath( 'js/libs/draco/' );
  46. dracoLoader.setDecoderConfig( { type: 'js' } );
  47. init();
  48. animate();
  49. function init() {
  50. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 15 );
  51. camera.position.set( 0.13271600513224902, 0.3489546826045913, 0.43921296427927076 );
  52. scene = new THREE.Scene();
  53. scene.background = new THREE.Color( 0x443333 );
  54. scene.fog = new THREE.Fog( 0x443333, 1, 4 );
  55. // Ground
  56. const map = new THREE.TextureLoader().load( './textures/uv_grid_opengl.jpg' );
  57. map.wrapS = THREE.RepeatWrapping;
  58. map.wrapT = THREE.RepeatWrapping;
  59. map.repeat.set( 20, 20 );
  60. const plane = new THREE.Mesh(
  61. new THREE.PlaneGeometry( 8, 8 ),
  62. new THREE.MeshPhongMaterial( {
  63. color: 0x999999,
  64. specular: 0x101010,
  65. map,
  66. } )
  67. );
  68. plane.rotation.x = - Math.PI / 2;
  69. plane.position.y = - 0.0001;
  70. // plane.receiveShadow = true;
  71. scene.add( plane );
  72. plane.name = 'plane';
  73. // Lights
  74. const hemiLight = new THREE.HemisphereLight( 0x443333, 0x111122 );
  75. hemiLight.name = 'hemiLight';
  76. scene.add( hemiLight );
  77. const spotLight = new THREE.SpotLight();
  78. spotLight.name = 'spotLight';
  79. spotLight.angle = Math.PI / 16;
  80. spotLight.penumbra = 0.5;
  81. // spotLight.castShadow = true;
  82. spotLight.position.set( - 1, 1, 1 );
  83. scene.add( spotLight );
  84. dracoLoader.load( 'models/draco/bunny.drc', function ( geometry ) {
  85. geometry.computeVertexNormals();
  86. const material = new THREE.MeshStandardMaterial( { color: 0x606060 } );
  87. const mesh = new THREE.Mesh( geometry, material );
  88. mesh.position.y = - 0.0365;
  89. mesh.name = 'bunny';
  90. scene.add( mesh );
  91. objects.push( mesh );
  92. selects.push( mesh );
  93. // Release decoder resources.
  94. dracoLoader.dispose();
  95. } );
  96. let geometry, material, mesh;
  97. geometry = new THREE.BoxBufferGeometry( .05, .05, .05 );
  98. material = new THREE.MeshStandardMaterial( { color: 'green' } );
  99. mesh = new THREE.Mesh( geometry, material );
  100. mesh.position.set( - .12, .025, .015 );
  101. mesh.name = 'box';
  102. scene.add( mesh );
  103. objects.push( mesh );
  104. selects.push( mesh );
  105. geometry = new THREE.IcosahedronBufferGeometry( .025, 4 );
  106. material = new THREE.MeshStandardMaterial( { color: 'cyan' } );
  107. mesh = new THREE.Mesh( geometry, material );
  108. mesh.position.set( - .05, .025, .08 );
  109. mesh.name = 'sphere';
  110. scene.add( mesh );
  111. objects.push( mesh );
  112. // selects.push( mesh );
  113. geometry = new THREE.ConeBufferGeometry( .025, .05, 64 );
  114. material = new THREE.MeshStandardMaterial( { color: 'yellow' } );
  115. mesh = new THREE.Mesh( geometry, material );
  116. mesh.position.set( - .05, .025, - .055 );
  117. mesh.name = 'cone';
  118. scene.add( mesh );
  119. objects.push( mesh );
  120. // selects.push( mesh );
  121. // renderer
  122. renderer = new THREE.WebGLRenderer( { antialias: false } );
  123. renderer.setSize( window.innerWidth, window.innerHeight );
  124. renderer.autoClear = false;
  125. container.appendChild( renderer.domElement );
  126. //
  127. controls = new OrbitControls( camera, renderer.domElement );
  128. controls.enableDamping = true;
  129. controls.target.set( 0, 0.0635, 0 );
  130. controls.update();
  131. controls.enabled = ! params.autoRotate;
  132. // STATS
  133. stats = new Stats();
  134. container.appendChild( stats.dom );
  135. window.addEventListener( 'resize', onWindowResize );
  136. window.addEventListener( 'pointerdown', onPointerDown );
  137. window.addEventListener( 'pointerup', onPointerUp );
  138. // composer
  139. composer = new EffectComposer( renderer );
  140. ssrrPass = new SSRrPass( {
  141. renderer,
  142. scene,
  143. camera,
  144. width: innerWidth,
  145. height: innerHeight,
  146. selects: selects
  147. } );
  148. composer.addPass( ssrrPass );
  149. composer.addPass( new ShaderPass( GammaCorrectionShader ) );
  150. // GUI
  151. gui = new GUI();
  152. gui.add( params, 'enableSSRr' ).name( 'Enable SSRr' );
  153. ssrrPass.ior = 1.1;
  154. gui.add( ssrrPass, 'ior' ).name( 'IOR' ).min( 1 ).max( 1.5 ).step( .0001 );
  155. gui.add( ssrrPass, 'fillHole' );
  156. gui.add( params, 'autoRotate' ).onChange( () => {
  157. controls.enabled = ! params.autoRotate;
  158. } );
  159. const folder = gui.addFolder( 'more settings' );
  160. folder.add( ssrrPass, 'specular' );
  161. folder.add( ssrrPass.specularMaterial, 'metalness' ).min( 0 ).max( 1 ).step( .01 );
  162. folder.add( ssrrPass.specularMaterial, 'roughness' ).min( 0 ).max( 1 ).step( .01 );
  163. folder.add( ssrrPass, 'output', {
  164. 'Default': SSRrPass.OUTPUT.Default,
  165. 'SSRr Only': SSRrPass.OUTPUT.SSRr,
  166. 'Beauty': SSRrPass.OUTPUT.Beauty,
  167. 'Depth': SSRrPass.OUTPUT.Depth,
  168. 'DepthSelects': SSRrPass.OUTPUT.DepthSelects,
  169. 'NormalSelects': SSRrPass.OUTPUT.NormalSelects,
  170. 'Refractive': SSRrPass.OUTPUT.Refractive,
  171. 'Specular': SSRrPass.OUTPUT.Specular,
  172. } ).onChange( function ( value ) {
  173. ssrrPass.output = parseInt( value );
  174. } );
  175. ssrrPass.surfDist = 0.0015;
  176. folder.add( ssrrPass, 'surfDist' ).min( 0 ).max( .005 ).step( .0001 );
  177. ssrrPass.maxDistance = 50;
  178. folder.add( ssrrPass, 'maxDistance' ).min( 0 ).max( 100 ).step( .001 );
  179. folder.add( ssrrPass, 'infiniteThick' );
  180. // folder.open()
  181. // gui.close()
  182. }
  183. function onPointerDown( event ) {
  184. mouseDown.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  185. mouseDown.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  186. }
  187. function onPointerUp( event ) {
  188. // calculate mouse position in normalized device coordinates
  189. // (-1 to +1) for both components
  190. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  191. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  192. if ( mouseDown.sub( mouse ).length() > 0 ) return;
  193. raycaster.setFromCamera( mouse, camera );
  194. const intersect = raycaster.intersectObjects( objects, false )[ 0 ];
  195. if ( intersect ) {
  196. const index = selects.indexOf( intersect.object );
  197. if ( index >= 0 ) {
  198. selects.splice( index, 1 );
  199. } else {
  200. selects.push( intersect.object );
  201. }
  202. }
  203. }
  204. function onWindowResize() {
  205. camera.aspect = window.innerWidth / window.innerHeight;
  206. camera.updateProjectionMatrix();
  207. renderer.setSize( window.innerWidth, window.innerHeight );
  208. composer.setSize( window.innerWidth, window.innerHeight );
  209. }
  210. function animate() {
  211. requestAnimationFrame( animate );
  212. stats.begin();
  213. render();
  214. stats.end();
  215. }
  216. function render() {
  217. if ( params.autoRotate ) {
  218. const timer = Date.now() * 0.0003;
  219. camera.position.x = Math.sin( timer ) * 0.5;
  220. camera.position.y = 0.2135;
  221. camera.position.z = Math.cos( timer ) * 0.5;
  222. camera.lookAt( 0, 0.0635, 0 );
  223. } else {
  224. controls.update();
  225. }
  226. if ( params.enableSSRr ) {
  227. composer.render();
  228. } else {
  229. renderer.render( scene, camera );
  230. }
  231. }
  232. </script>
  233. </body>
  234. </html>