123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - postprocessing - depth-of-field</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="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl realistic depth-of-field bokeh example<br/>
- shader ported from <a href="http://blenderartists.org/forum/showthread.php?237488-GLSL-depth-of-field-with-bokeh-v2-4-(update)">Martins Upitis</a>
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { GUI } from './jsm/libs/dat.gui.module.js';
- import { BokehShader, BokehDepthShader } from './jsm/shaders/BokehShader2.js';
- var container, stats;
- var camera, scene, renderer, materialDepth;
- var windowHalfX = window.innerWidth / 2;
- var windowHalfY = window.innerHeight / 2;
- var postprocessing = { enabled: true };
- var shaderSettings = {
- rings: 3,
- samples: 4
- };
- var mouse = new THREE.Vector2();
- var raycaster = new THREE.Raycaster();
- var distance = 100;
- var target = new THREE.Vector3( 0, 20, - 50 );
- var effectController;
- var planes = [];
- var leaves = 100;
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 3000 );
- camera.position.y = 150;
- camera.position.z = 450;
- scene = new THREE.Scene();
- scene.add( camera );
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.autoClear = false;
- container.appendChild( renderer.domElement );
- var depthShader = BokehDepthShader;
- materialDepth = new THREE.ShaderMaterial( {
- uniforms: depthShader.uniforms,
- vertexShader: depthShader.vertexShader,
- fragmentShader: depthShader.fragmentShader
- } );
- materialDepth.uniforms[ 'mNear' ].value = camera.near;
- materialDepth.uniforms[ 'mFar' ].value = camera.far;
- // skybox
- var r = 'textures/cube/Bridge2/';
- var urls = [ r + 'posx.jpg', r + 'negx.jpg',
- r + 'posy.jpg', r + 'negy.jpg',
- r + 'posz.jpg', r + 'negz.jpg' ];
- var textureCube = new THREE.CubeTextureLoader().load( urls );
- textureCube.format = THREE.RGBFormat;
- scene.background = textureCube;
- // plane particles
- var planePiece = new THREE.PlaneBufferGeometry( 10, 10, 1, 1 );
- var planeMat = new THREE.MeshPhongMaterial( {
- color: 0xffffff * 0.4,
- shininess: 0.5,
- specular: 0xffffff,
- envMap: textureCube,
- side: THREE.DoubleSide
- } );
- var rand = Math.random;
- for ( var i = 0; i < leaves; i ++ ) {
- var plane = new THREE.Mesh( planePiece, planeMat );
- plane.rotation.set( rand(), rand(), rand() );
- plane.rotation.dx = rand() * 0.1;
- plane.rotation.dy = rand() * 0.1;
- plane.rotation.dz = rand() * 0.1;
- plane.position.set( rand() * 150, 0 + rand() * 300, rand() * 150 );
- plane.position.dx = ( rand() - 0.5 );
- plane.position.dz = ( rand() - 0.5 );
- scene.add( plane );
- planes.push( plane );
- }
- // adding Monkeys
- var loader2 = new THREE.BufferGeometryLoader();
- loader2.load( 'models/json/suzanne_buffergeometry.json', function ( geometry ) {
- geometry.computeVertexNormals();
- var material = new THREE.MeshPhongMaterial( {
- specular: 0xffffff,
- envMap: textureCube,
- shininess: 50,
- reflectivity: 1.0,
- flatShading: true
- } );
- var monkeys = 20;
- for ( var i = 0; i < monkeys; i ++ ) {
- var mesh = new THREE.Mesh( geometry, material );
- mesh.position.z = Math.cos( i / monkeys * Math.PI * 2 ) * 200;
- mesh.position.y = Math.sin( i / monkeys * Math.PI * 3 ) * 20;
- mesh.position.x = Math.sin( i / monkeys * Math.PI * 2 ) * 200;
- mesh.rotation.y = i / monkeys * Math.PI * 2;
- mesh.scale.setScalar( 30 );
- scene.add( mesh );
- }
- } );
- // add balls
- var geometry = new THREE.SphereBufferGeometry( 1, 20, 20 );
- for ( var i = 0; i < 20; i ++ ) {
- var ballmaterial = new THREE.MeshPhongMaterial( {
- color: 0xffffff * Math.random(),
- shininess: 0.5,
- specular: 0xffffff,
- envMap: textureCube } );
- var mesh = new THREE.Mesh( geometry, ballmaterial );
- mesh.position.x = ( Math.random() - 0.5 ) * 200;
- mesh.position.y = Math.random() * 50;
- mesh.position.z = ( Math.random() - 0.5 ) * 200;
- mesh.scale.multiplyScalar( 10 );
- scene.add( mesh );
- }
- // lights
- scene.add( new THREE.AmbientLight( 0x222222 ) );
- var directionalLight = new THREE.DirectionalLight( 0xffffff, 2 );
- directionalLight.position.set( 2, 1.2, 10 ).normalize();
- scene.add( directionalLight );
- var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
- directionalLight.position.set( - 2, 1.2, - 10 ).normalize();
- scene.add( directionalLight );
- initPostprocessing();
- stats = new Stats();
- container.appendChild( stats.dom );
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- document.addEventListener( 'touchstart', onDocumentTouchStart, false );
- document.addEventListener( 'touchmove', onDocumentTouchMove, false );
- effectController = {
- enabled: true,
- jsDepthCalculation: true,
- shaderFocus: false,
- fstop: 2.2,
- maxblur: 1.0,
- showFocus: false,
- focalDepth: 2.8,
- manualdof: false,
- vignetting: false,
- depthblur: false,
- threshold: 0.5,
- gain: 2.0,
- bias: 0.5,
- fringe: 0.7,
- focalLength: 35,
- noise: true,
- pentagon: false,
- dithering: 0.0001
- };
- var matChanger = function () {
- for ( var e in effectController ) {
- if ( e in postprocessing.bokeh_uniforms ) {
- postprocessing.bokeh_uniforms[ e ].value = effectController[ e ];
- }
- }
- postprocessing.enabled = effectController.enabled;
- postprocessing.bokeh_uniforms[ 'znear' ].value = camera.near;
- postprocessing.bokeh_uniforms[ 'zfar' ].value = camera.far;
- camera.setFocalLength( effectController.focalLength );
- };
- var gui = new GUI();
- gui.add( effectController, 'enabled' ).onChange( matChanger );
- gui.add( effectController, 'jsDepthCalculation' ).onChange( matChanger );
- gui.add( effectController, 'shaderFocus' ).onChange( matChanger );
- gui.add( effectController, 'focalDepth', 0.0, 200.0 ).listen().onChange( matChanger );
- gui.add( effectController, 'fstop', 0.1, 22, 0.001 ).onChange( matChanger );
- gui.add( effectController, 'maxblur', 0.0, 5.0, 0.025 ).onChange( matChanger );
- gui.add( effectController, 'showFocus' ).onChange( matChanger );
- gui.add( effectController, 'manualdof' ).onChange( matChanger );
- gui.add( effectController, 'vignetting' ).onChange( matChanger );
- gui.add( effectController, 'depthblur' ).onChange( matChanger );
- gui.add( effectController, 'threshold', 0, 1, 0.001 ).onChange( matChanger );
- gui.add( effectController, 'gain', 0, 100, 0.001 ).onChange( matChanger );
- gui.add( effectController, 'bias', 0, 3, 0.001 ).onChange( matChanger );
- gui.add( effectController, 'fringe', 0, 5, 0.001 ).onChange( matChanger );
- gui.add( effectController, 'focalLength', 16, 80, 0.001 ).onChange( matChanger );
- gui.add( effectController, 'noise' ).onChange( matChanger );
- gui.add( effectController, 'dithering', 0, 0.001, 0.0001 ).onChange( matChanger );
- gui.add( effectController, 'pentagon' ).onChange( matChanger );
- gui.add( shaderSettings, 'rings', 1, 8 ).step( 1 ).onChange( shaderUpdate );
- gui.add( shaderSettings, 'samples', 1, 13 ).step( 1 ).onChange( shaderUpdate );
- matChanger();
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- windowHalfX = window.innerWidth / 2;
- windowHalfY = window.innerHeight / 2;
- postprocessing.rtTextureDepth.setSize( window.innerWidth, window.innerHeight );
- postprocessing.rtTextureColor.setSize( window.innerWidth, window.innerHeight );
- postprocessing.bokeh_uniforms[ 'textureWidth' ].value = window.innerWidth;
- postprocessing.bokeh_uniforms[ 'textureHeight' ].value = window.innerHeight;
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function onDocumentMouseMove( event ) {
- mouse.x = ( event.clientX - windowHalfX ) / windowHalfX;
- mouse.y = - ( event.clientY - windowHalfY ) / windowHalfY;
- postprocessing.bokeh_uniforms[ 'focusCoords' ].value.set( event.clientX / window.innerWidth, 1 - ( event.clientY / window.innerHeight ) );
- }
- function onDocumentTouchStart( event ) {
- if ( event.touches.length == 1 ) {
- event.preventDefault();
- mouse.x = ( event.touches[ 0 ].pageX - windowHalfX ) / windowHalfX;
- mouse.y = - ( event.touches[ 0 ].pageY - windowHalfY ) / windowHalfY;
- }
- }
- function onDocumentTouchMove( event ) {
- if ( event.touches.length == 1 ) {
- event.preventDefault();
- mouse.x = ( event.touches[ 0 ].pageX - windowHalfX ) / windowHalfX;
- mouse.y = - ( event.touches[ 0 ].pageY - windowHalfY ) / windowHalfY;
- }
- }
- function initPostprocessing() {
- postprocessing.scene = new THREE.Scene();
- postprocessing.camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 10000, 10000 );
- postprocessing.camera.position.z = 100;
- postprocessing.scene.add( postprocessing.camera );
- var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat };
- postprocessing.rtTextureDepth = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
- postprocessing.rtTextureColor = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
- var bokeh_shader = BokehShader;
- postprocessing.bokeh_uniforms = THREE.UniformsUtils.clone( bokeh_shader.uniforms );
- postprocessing.bokeh_uniforms[ 'tColor' ].value = postprocessing.rtTextureColor.texture;
- postprocessing.bokeh_uniforms[ 'tDepth' ].value = postprocessing.rtTextureDepth.texture;
- postprocessing.bokeh_uniforms[ 'textureWidth' ].value = window.innerWidth;
- postprocessing.bokeh_uniforms[ 'textureHeight' ].value = window.innerHeight;
- postprocessing.materialBokeh = new THREE.ShaderMaterial( {
- uniforms: postprocessing.bokeh_uniforms,
- vertexShader: bokeh_shader.vertexShader,
- fragmentShader: bokeh_shader.fragmentShader,
- defines: {
- RINGS: shaderSettings.rings,
- SAMPLES: shaderSettings.samples
- }
- } );
- postprocessing.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( window.innerWidth, window.innerHeight ), postprocessing.materialBokeh );
- postprocessing.quad.position.z = - 500;
- postprocessing.scene.add( postprocessing.quad );
- }
- function shaderUpdate() {
- postprocessing.materialBokeh.defines.RINGS = shaderSettings.rings;
- postprocessing.materialBokeh.defines.SAMPLES = shaderSettings.samples;
- postprocessing.materialBokeh.needsUpdate = true;
- }
- function animate() {
- requestAnimationFrame( animate, renderer.domElement );
- render();
- stats.update();
- }
- function linearize( depth ) {
- var zfar = camera.far;
- var znear = camera.near;
- return - zfar * znear / ( depth * ( zfar - znear ) - zfar );
- }
- function smoothstep( near, far, depth ) {
- var x = saturate( ( depth - near ) / ( far - near ) );
- return x * x * ( 3 - 2 * x );
- }
- function saturate( x ) {
- return Math.max( 0, Math.min( 1, x ) );
- }
- function render() {
- var time = Date.now() * 0.00015;
- camera.position.x = Math.cos( time ) * 400;
- camera.position.z = Math.sin( time ) * 500;
- camera.position.y = Math.sin( time / 1.4 ) * 100;
- camera.lookAt( target );
- camera.updateMatrixWorld();
- if ( effectController.jsDepthCalculation ) {
- raycaster.setFromCamera( mouse, camera );
- var intersects = raycaster.intersectObjects( scene.children, true );
- var targetDistance = ( intersects.length > 0 ) ? intersects[ 0 ].distance : 1000;
- distance += ( targetDistance - distance ) * 0.03;
- var sdistance = smoothstep( camera.near, camera.far, distance );
- var ldistance = linearize( 1 - sdistance );
- postprocessing.bokeh_uniforms[ 'focalDepth' ].value = ldistance;
- effectController[ 'focalDepth' ] = ldistance;
- }
- for ( var i = 0; i < leaves; i ++ ) {
- var plane = planes[ i ];
- plane.rotation.x += plane.rotation.dx;
- plane.rotation.y += plane.rotation.dy;
- plane.rotation.z += plane.rotation.dz;
- plane.position.y -= 2;
- plane.position.x += plane.position.dx;
- plane.position.z += plane.position.dz;
- if ( plane.position.y < 0 ) plane.position.y += 300;
- }
- if ( postprocessing.enabled ) {
- renderer.clear();
- // render scene into texture
- renderer.setRenderTarget( postprocessing.rtTextureColor );
- renderer.clear();
- renderer.render( scene, camera );
- // render depth into texture
- scene.overrideMaterial = materialDepth;
- renderer.setRenderTarget( postprocessing.rtTextureDepth );
- renderer.clear();
- renderer.render( scene, camera );
- scene.overrideMaterial = null;
- // render bokeh composite
- renderer.setRenderTarget( null );
- renderer.render( postprocessing.scene, postprocessing.camera );
- } else {
- scene.overrideMaterial = null;
- renderer.setRenderTarget( null );
- renderer.clear();
- renderer.render( scene, camera );
- }
- }
- </script>
- </body>
- </html>
|