webgl_postprocessing_ssrr.html 7.9 KB

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