Doodle3D-Slicer/three.js-master/examples/webgl_postprocessing_dof.html

315 lines
7.2 KiB
HTML
Raw Normal View History

2015-06-12 15:58:26 +02:00
<!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;
}
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.min.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">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,
material_depth, 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 );
renderer.sortObjects = false;
material_depth = new THREE.MeshDepthMaterial();
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 = THREE.ImageUtils.loadTextureCube( urls );
parameters = { color: 0xff1100, envMap: textureCube, shading: THREE.FlatShading };
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;
c = 0;
//var s = 0.25;
var s = 60;
for ( i = 0; i < xgrid; i ++ )
for ( j = 0; j < ygrid; j ++ )
for ( k = 0; k < zgrid; k ++ ) {
if ( singleMaterial ) {
mesh = new THREE.Mesh( geo, zmaterial );
} else {
materials[ c ] = new THREE.MeshBasicMaterial( parameters );
mesh = new THREE.Mesh( geo, materials[ c ] );
}
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 );
c ++;
}
//console.log("init time: ", Date.now() - start );
scene.matrixAutoUpdate = false;
initPostprocessing();
renderer.autoClear = false;
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'touchstart', onDocumentTouchStart, false );
document.addEventListener( 'touchmove', onDocumentTouchMove, false );
window.addEventListener( 'resize', onWindowResize, false );
var effectController = {
focus: 1.0,
aperture: 0.025,
maxblur: 1.0
};
var matChanger = function( ) {
postprocessing.bokeh.uniforms[ "focus" ].value = effectController.focus;
postprocessing.bokeh.uniforms[ "aperture" ].value = effectController.aperture;
postprocessing.bokeh.uniforms[ "maxblur" ].value = effectController.maxblur;
};
var gui = new dat.GUI();
gui.add( effectController, "focus", 0.0, 3.0, 0.025 ).onChange( matChanger );
gui.add( effectController, "aperture", 0.001, 0.2, 0.001 ).onChange( matChanger );
gui.add( effectController, "maxblur", 0.0, 3.0, 0.025 ).onChange( matChanger );
gui.close();
}
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 );
render();
stats.update();
}
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>