webgl_postprocessing_ssrr.html 8.6 KB

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