123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <head>
- <title>three.js webgl - postprocessing - Screen Space Refraction</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="container"></div>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> -
- SSRrPass demo by <a href="https://github.com/gonnavis" target="_blank">Vis</a>.<br />
- click object to toggle transparent<br/>
- </div>
- <!-- Import maps polyfill -->
- <!-- Remove this when import maps will be widely supported -->
- <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.module.js"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import Stats from './jsm/libs/stats.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { GUI } from './jsm/libs/lil-gui.module.min.js';
- import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
- import { SSRrPass } from './jsm/postprocessing/SSRrPass.js';
- import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
- import { GammaCorrectionShader } from './jsm/shaders/GammaCorrectionShader.js';
- import { DRACOLoader } from './jsm/loaders/DRACOLoader.js';
- const params = {
- enableSSRr: true,
- autoRotate: true,
- };
- let composer;
- let ssrrPass;
- let gui;
- let stats;
- let controls;
- let camera, scene, renderer;
- const objects = [];
- const selects = [];
- const raycaster = new THREE.Raycaster();
- const mouseDown = new THREE.Vector2();
- const mouse = new THREE.Vector2();
- const container = document.querySelector( '#container' );
- // Configure and create Draco decoder.
- const dracoLoader = new DRACOLoader();
- dracoLoader.setDecoderPath( 'js/libs/draco/' );
- dracoLoader.setDecoderConfig( { type: 'js' } );
- init();
- animate();
- function init() {
- camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 15 );
- camera.position.set( 0.13271600513224902, 0.3489546826045913, 0.43921296427927076 );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x443333 );
- scene.fog = new THREE.Fog( 0x443333, 1, 4 );
- // Ground
- const map = new THREE.TextureLoader().load( './textures/uv_grid_opengl.jpg' );
- map.wrapS = THREE.RepeatWrapping;
- map.wrapT = THREE.RepeatWrapping;
- map.repeat.set( 20, 20 );
- const plane = new THREE.Mesh(
- new THREE.PlaneGeometry( 8, 8 ),
- new THREE.MeshPhongMaterial( {
- color: 0x999999,
- specular: 0x101010,
- map,
- } )
- );
- plane.rotation.x = - Math.PI / 2;
- plane.position.y = - 0.0001;
- // plane.receiveShadow = true;
- scene.add( plane );
- plane.name = 'plane';
- // Lights
- const hemiLight = new THREE.HemisphereLight( 0x443333, 0x111122 );
- hemiLight.name = 'hemiLight';
- scene.add( hemiLight );
- const spotLight = new THREE.SpotLight();
- spotLight.name = 'spotLight';
- spotLight.angle = Math.PI / 16;
- spotLight.penumbra = 0.5;
- // spotLight.castShadow = true;
- spotLight.position.set( - 1, 1, 1 );
- scene.add( spotLight );
- dracoLoader.load( 'models/draco/bunny.drc', function ( geometry ) {
- geometry.computeVertexNormals();
- const material = new THREE.MeshStandardMaterial( { color: 0x606060 } );
- const mesh = new THREE.Mesh( geometry, material );
- mesh.position.y = - 0.0365;
- mesh.name = 'bunny';
- scene.add( mesh );
- objects.push( mesh );
- selects.push( mesh );
- // Release decoder resources.
- dracoLoader.dispose();
- } );
- let geometry, material, mesh;
- geometry = new THREE.BoxGeometry( .05, .05, .05 );
- material = new THREE.MeshStandardMaterial( { color: 'green' } );
- mesh = new THREE.Mesh( geometry, material );
- mesh.position.set( - .12, .025, .015 );
- mesh.name = 'box';
- scene.add( mesh );
- objects.push( mesh );
- selects.push( mesh );
- geometry = new THREE.IcosahedronGeometry( .025, 4 );
- material = new THREE.MeshStandardMaterial( { color: 'cyan' } );
- mesh = new THREE.Mesh( geometry, material );
- mesh.position.set( - .05, .025, .08 );
- mesh.name = 'sphere';
- scene.add( mesh );
- objects.push( mesh );
- // selects.push( mesh );
- geometry = new THREE.ConeGeometry( .025, .05, 64 );
- material = new THREE.MeshStandardMaterial( { color: 'yellow' } );
- mesh = new THREE.Mesh( geometry, material );
- mesh.position.set( - .05, .025, - .055 );
- mesh.name = 'cone';
- scene.add( mesh );
- objects.push( mesh );
- // selects.push( mesh );
- // renderer
- renderer = new THREE.WebGLRenderer( { antialias: false } );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.autoClear = false;
- container.appendChild( renderer.domElement );
- //
- controls = new OrbitControls( camera, renderer.domElement );
- controls.enableDamping = true;
- controls.target.set( 0, 0.0635, 0 );
- controls.update();
- controls.enabled = ! params.autoRotate;
- // STATS
- stats = new Stats();
- container.appendChild( stats.dom );
- window.addEventListener( 'resize', onWindowResize );
- window.addEventListener( 'pointerdown', onPointerDown );
- window.addEventListener( 'pointerup', onPointerUp );
- // composer
- composer = new EffectComposer( renderer );
- ssrrPass = new SSRrPass( {
- renderer,
- scene,
- camera,
- width: innerWidth,
- height: innerHeight,
- selects: selects
- } );
- composer.addPass( ssrrPass );
- composer.addPass( new ShaderPass( GammaCorrectionShader ) );
- // GUI
- gui = new GUI();
- gui.add( params, 'enableSSRr' ).name( 'Enable SSRr' );
- ssrrPass.ior = 1.1;
- gui.add( ssrrPass, 'ior' ).name( 'IOR' ).min( 1 ).max( 1.5 ).step( .0001 );
- gui.add( ssrrPass, 'fillHole' );
- gui.add( params, 'autoRotate' ).onChange( () => {
- controls.enabled = ! params.autoRotate;
- } );
- const folder = gui.addFolder( 'more settings' );
- folder.add( ssrrPass, 'specular' );
- folder.add( ssrrPass.specularMaterial, 'metalness' ).min( 0 ).max( 1 ).step( .01 );
- folder.add( ssrrPass.specularMaterial, 'roughness' ).min( 0 ).max( 1 ).step( .01 );
- folder.add( ssrrPass, 'output', {
- 'Default': SSRrPass.OUTPUT.Default,
- 'SSRr Only': SSRrPass.OUTPUT.SSRr,
- 'Beauty': SSRrPass.OUTPUT.Beauty,
- 'Depth': SSRrPass.OUTPUT.Depth,
- 'DepthSelects': SSRrPass.OUTPUT.DepthSelects,
- 'NormalSelects': SSRrPass.OUTPUT.NormalSelects,
- 'Refractive': SSRrPass.OUTPUT.Refractive,
- 'Specular': SSRrPass.OUTPUT.Specular,
- } ).onChange( function ( value ) {
- ssrrPass.output = parseInt( value );
- } );
- ssrrPass.surfDist = 0.0015;
- folder.add( ssrrPass, 'surfDist' ).min( 0 ).max( .005 ).step( .0001 );
- ssrrPass.maxDistance = 50;
- folder.add( ssrrPass, 'maxDistance' ).min( 0 ).max( 100 ).step( .001 );
- folder.add( ssrrPass, 'infiniteThick' );
- // folder.open()
- // gui.close()
- }
- function onPointerDown( event ) {
- mouseDown.x = ( event.clientX / window.innerWidth ) * 2 - 1;
- mouseDown.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
- }
- function onPointerUp( event ) {
- // calculate mouse position in normalized device coordinates
- // (-1 to +1) for both components
- mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
- mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
- if ( mouseDown.sub( mouse ).length() > 0 ) return;
- raycaster.setFromCamera( mouse, camera );
- const intersect = raycaster.intersectObjects( objects, false )[ 0 ];
- if ( intersect ) {
- const index = selects.indexOf( intersect.object );
- if ( index >= 0 ) {
- selects.splice( index, 1 );
- } else {
- selects.push( intersect.object );
- }
- }
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- composer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- requestAnimationFrame( animate );
- stats.begin();
- render();
- stats.end();
- }
- function render() {
- if ( params.autoRotate ) {
- const timer = Date.now() * 0.0003;
- camera.position.x = Math.sin( timer ) * 0.5;
- camera.position.y = 0.2135;
- camera.position.z = Math.cos( timer ) * 0.5;
- camera.lookAt( 0, 0.0635, 0 );
- } else {
- controls.update();
- }
- if ( params.enableSSRr ) {
- composer.render();
- } else {
- renderer.render( scene, camera );
- }
- }
- </script>
- </body>
- </html>
|