123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <!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 depth-of-field with bokeh example<br/>
- shader by <a href="http://artmartinsh.blogspot.com/2010/02/glsl-lens-blur-filter-with-bokeh.html">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 { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
- import { RenderPass } from './jsm/postprocessing/RenderPass.js';
- import { BokehPass } from './jsm/postprocessing/BokehPass.js';
- var container, stats;
- var camera, scene, renderer,
- materials = [], objects = [],
- singleMaterial, zmaterial = [],
- parameters, i, j, k, h, x, y, z, nobjects, cubeMaterial;
- var mouseX = 0, mouseY = 0;
- var windowHalfX = window.innerWidth / 2;
- var windowHalfY = window.innerHeight / 2;
- var width = window.innerWidth;
- var height = window.innerHeight;
- var postprocessing = {};
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 70, width / height, 1, 3000 );
- camera.position.z = 200;
- scene = new THREE.Scene();
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( width, height );
- container.appendChild( renderer.domElement );
- var path = "textures/cube/SwedishRoyalCastle/";
- var format = '.jpg';
- var urls = [
- path + 'px' + format, path + 'nx' + format,
- path + 'py' + format, path + 'ny' + format,
- path + 'pz' + format, path + 'nz' + format
- ];
- var textureCube = new THREE.CubeTextureLoader().load( urls );
- parameters = { color: 0xff1100, envMap: textureCube };
- cubeMaterial = new THREE.MeshBasicMaterial( parameters );
- singleMaterial = false;
- if ( singleMaterial ) zmaterial = [ cubeMaterial ];
- var geo = new THREE.SphereBufferGeometry( 1, 20, 10 );
- var xgrid = 14,
- ygrid = 9,
- zgrid = 14;
- nobjects = xgrid * ygrid * zgrid;
- var s = 60;
- var count = 0;
- for ( i = 0; i < xgrid; i ++ )
- for ( j = 0; j < ygrid; j ++ )
- for ( k = 0; k < zgrid; k ++ ) {
- var mesh;
- if ( singleMaterial ) {
- mesh = new THREE.Mesh( geo, zmaterial );
- } else {
- mesh = new THREE.Mesh( geo, new THREE.MeshBasicMaterial( parameters ) );
- materials[ count ] = mesh.material;
- }
- x = 200 * ( i - xgrid / 2 );
- y = 200 * ( j - ygrid / 2 );
- z = 200 * ( k - zgrid / 2 );
- mesh.position.set( x, y, z );
- mesh.scale.set( s, s, s );
- mesh.matrixAutoUpdate = false;
- mesh.updateMatrix();
- scene.add( mesh );
- objects.push( mesh );
- count ++;
- }
- initPostprocessing();
- renderer.autoClear = false;
- stats = new Stats();
- container.appendChild( stats.dom );
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- document.addEventListener( 'touchstart', onDocumentTouchStart, false );
- document.addEventListener( 'touchmove', onDocumentTouchMove, false );
- window.addEventListener( 'resize', onWindowResize, false );
- var effectController = {
- focus: 500.0,
- aperture: 5,
- maxblur: 1.0
- };
- var matChanger = function ( ) {
- postprocessing.bokeh.uniforms[ "focus" ].value = effectController.focus;
- postprocessing.bokeh.uniforms[ "aperture" ].value = effectController.aperture * 0.00001;
- postprocessing.bokeh.uniforms[ "maxblur" ].value = effectController.maxblur;
- };
- var gui = new GUI();
- gui.add( effectController, "focus", 10.0, 3000.0, 10 ).onChange( matChanger );
- gui.add( effectController, "aperture", 0, 10, 0.1 ).onChange( matChanger );
- gui.add( effectController, "maxblur", 0.0, 3.0, 0.025 ).onChange( matChanger );
- gui.close();
- matChanger();
- }
- function onDocumentMouseMove( event ) {
- mouseX = event.clientX - windowHalfX;
- mouseY = event.clientY - windowHalfY;
- }
- function onDocumentTouchStart( event ) {
- if ( event.touches.length == 1 ) {
- event.preventDefault();
- mouseX = event.touches[ 0 ].pageX - windowHalfX;
- mouseY = event.touches[ 0 ].pageY - windowHalfY;
- }
- }
- function onDocumentTouchMove( event ) {
- if ( event.touches.length == 1 ) {
- event.preventDefault();
- mouseX = event.touches[ 0 ].pageX - windowHalfX;
- mouseY = event.touches[ 0 ].pageY - windowHalfY;
- }
- }
- function onWindowResize() {
- windowHalfX = window.innerWidth / 2;
- windowHalfY = window.innerHeight / 2;
- width = window.innerWidth;
- height = window.innerHeight;
- camera.aspect = width / height;
- camera.updateProjectionMatrix();
- renderer.setSize( width, height );
- postprocessing.composer.setSize( width, height );
- }
- function initPostprocessing() {
- var renderPass = new RenderPass( scene, camera );
- var bokehPass = new BokehPass( scene, camera, {
- focus: 1.0,
- aperture: 0.025,
- maxblur: 1.0,
- width: width,
- height: height
- } );
- var composer = new EffectComposer( renderer );
- composer.addPass( renderPass );
- composer.addPass( bokehPass );
- postprocessing.composer = composer;
- postprocessing.bokeh = bokehPass;
- }
- function animate() {
- requestAnimationFrame( animate, renderer.domElement );
- stats.begin();
- render();
- stats.end();
- }
- function render() {
- var time = Date.now() * 0.00005;
- camera.position.x += ( mouseX - camera.position.x ) * 0.036;
- camera.position.y += ( - ( mouseY ) - camera.position.y ) * 0.036;
- camera.lookAt( scene.position );
- if ( ! singleMaterial ) {
- for ( i = 0; i < nobjects; i ++ ) {
- h = ( 360 * ( i / nobjects + time ) % 360 ) / 360;
- materials[ i ].color.setHSL( h, 1, 0.5 );
- }
- }
- postprocessing.composer.render( 0.1 );
- }
- </script>
- </body>
- </html>
|