Permalink
6857d8d Aug 3, 2017
@mrdoob @alteredq @WestLangley @bhouston @Mugen87
311 lines (206 sloc) 7.07 KB
<!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">
<style>
body {
background-color: #000000;
margin: 0px;
overflow: hidden;
font-family: Monospace;
font-size: 13px;
text-align: center;
font-weight: bold;
text-align: center;
}
a {
color: #0078ff;
}
#info {
color: #fff;
position: relative;
top: 0px;
width: 50em;
margin: 0 auto -2.1em;
padding: 5px;
z-index: 100;
}
</style>
</head>
<body>
<script src="../build/three.js"></script>
<script src="js/shaders/CopyShader.js"></script>
<script src="js/shaders/BokehShader.js"></script>
<script src="js/postprocessing/EffectComposer.js"></script>
<script src="js/postprocessing/RenderPass.js"></script>
<script src="js/postprocessing/ShaderPass.js"></script>
<script src="js/postprocessing/MaskPass.js"></script>
<script src="js/postprocessing/BokehPass.js"></script>
<script src="js/Detector.js"></script>
<script src="js/libs/stats.min.js"></script>
<script src='js/libs/dat.gui.min.js'></script>
<div id="info">
<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl depth-of-field with bokeh example -
shader by <a href="http://artmartinsh.blogspot.com/2010/02/glsl-lens-blur-filter-with-bokeh.html">Martins Upitis</a>
</div>
<script>
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
var container, stats;
var camera, scene, renderer,
materials = [], objects = [],
singleMaterial, zmaterial = [],
parameters, i, j, k, h, color, x, y, z, s, n, 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( { antialias: false } );
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.SphereGeometry( 1, 20, 10 );
var start = Date.now();
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 ++;
}
//console.log("init time: ", Date.now() - start );
scene.matrixAutoUpdate = false;
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 dat.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 THREE.RenderPass( scene, camera );
var bokehPass = new THREE.BokehPass( scene, camera, {
focus: 1.0,
aperture: 0.025,
maxblur: 1.0,
width: width,
height: height
} );
bokehPass.renderToScreen = true;
var composer = new THREE.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>