Selaa lähdekoodia

Adding a more realistic + configurable Bokeh Shader to examples (#3182)

zz85 12 vuotta sitten
vanhempi
commit
8dadb3a6c6
2 muutettua tiedostoa jossa 988 lisäystä ja 0 poistoa
  1. 405 0
      examples/js/shaders/BokehShader2.js
  2. 583 0
      examples/webgl_postprocessing_dof2.html

+ 405 - 0
examples/js/shaders/BokehShader2.js

@@ -0,0 +1,405 @@
+/**
+ * @author zz85 / https://github.com/zz85 | twitter.com/blurspline
+ *
+ * Depth-of-field shader with bokeh
+ * ported from GLSL shader by Martins Upitis
+ * http://blenderartists.org/forum/showthread.php?237488-GLSL-depth-of-field-with-bokeh-v2-4-(update)
+ */
+
+
+
+THREE.BokehShader = {
+
+	uniforms: {
+
+		"textureWidth":  { type: "f", value: 1.0 },
+		"textureHeight":  { type: "f", value: 1.0 },
+
+		"focalDepth":   { type: "f", value: 1.0 },
+		"focalLength":   { type: "f", value: 24.0 },
+		"fstop": { type: "f", value: 0.9 },
+
+		"tColor":   { type: "t", value: null },
+		"tDepth":   { type: "t", value: null },
+
+		"maxblur":  { type: "f", value: 1.0 },
+
+		"showFocus":   { type: "i", value: 0 },
+		"manualdof":   { type: "i", value: 0 },
+		"vignetting":   { type: "i", value: 0 },
+		"depthblur":   { type: "i", value: 0 },
+
+		"threshold":  { type: "f", value: 0.5 },
+		"gain":  { type: "f", value: 2.0 },
+		"bias":  { type: "f", value: 0.5 },
+		"fringe":  { type: "f", value: 0.7 },
+
+		"znear":  { type: "f", value: 0.1 },
+		"zfar":  { type: "f", value: 100 },
+
+		"noise":  { type: "i", value: 1 },
+		"dithering":  { type: "f", value: 0.0001 },
+		"pentagon": { type: "i", value: 0 },
+
+		"shaderFocus":  { type: "i", value: 1 },
+		"focusCoords":  { type: "v2", value: new THREE.Vector2()},
+
+
+	},
+
+	vertexShader: [
+
+		"varying vec2 vUv;",
+
+		"void main() {",
+
+			"vUv = uv;",
+			"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
+
+		"}"
+
+	].join("\n"),
+
+	fragmentShader: [
+
+		"varying vec2 vUv;",
+
+		"uniform sampler2D tColor;",
+		"uniform sampler2D tDepth;",
+		"uniform float textureWidth;",
+		"uniform float textureHeight;",
+
+		"const float PI = 3.14159265;",
+
+		"float width = textureWidth; //texture width",
+		"float height = textureHeight; //texture height",
+
+		"vec2 texel = vec2(1.0/width,1.0/height);",
+
+		"uniform float focalDepth;  //focal distance value in meters, but you may use autofocus option below",
+		"uniform float focalLength; //focal length in mm",
+		"uniform float fstop; //f-stop value",
+		"uniform bool showFocus; //show debug focus point and focal range (red = focal point, green = focal range)",
+
+		"/*",
+		"make sure that these two values are the same for your camera, otherwise distances will be wrong.",
+		"*/",
+
+		"uniform float znear; // camera clipping start",
+		"uniform float zfar; // camera clipping end",
+
+		"//------------------------------------------",
+		"//user variables",
+
+		"const int samples = #SAMPLES#; //samples on the first ring",
+		"const int rings = #RINGS#; //ring count",
+
+		"const int maxringsamples = rings * samples;",
+
+		"uniform bool manualdof; // manual dof calculation",
+		"float ndofstart = 1.0; // near dof blur start",
+		"float ndofdist = 2.0; // near dof blur falloff distance",
+		"float fdofstart = 1.0; // far dof blur start",
+		"float fdofdist = 3.0; // far dof blur falloff distance",
+
+		"float CoC = 0.03; //circle of confusion size in mm (35mm film = 0.03mm)",
+
+		"uniform bool vignetting; // use optical lens vignetting",
+
+		"float vignout = 1.3; // vignetting outer border",
+		"float vignin = 0.0; // vignetting inner border",
+		"float vignfade = 22.0; // f-stops till vignete fades",
+
+		"uniform bool shaderFocus;",
+
+		"bool autofocus = shaderFocus;",
+		"//use autofocus in shader - use with focusCoords",
+		"// disable if you use external focalDepth value",
+
+		"uniform vec2 focusCoords;",
+		"// autofocus point on screen (0.0,0.0 - left lower corner, 1.0,1.0 - upper right)",
+		"// if center of screen use vec2(0.5, 0.5);",
+
+		"uniform float maxblur;",
+		"//clamp value of max blur (0.0 = no blur, 1.0 default)",
+
+		"uniform float threshold; // highlight threshold;",
+		"uniform float gain; // highlight gain;",
+
+		"uniform float bias; // bokeh edge bias",
+		"uniform float fringe; // bokeh chromatic aberration / fringing",
+
+		"uniform bool noise; //use noise instead of pattern for sample dithering",
+
+		"uniform float dithering;",
+		"float namount = dithering; //dither amount",
+
+		"uniform bool depthblur; // blur the depth buffer",
+		"float dbsize = 1.25; // depth blur size",
+
+		"/*",
+		"next part is experimental",
+		"not looking good with small sample and ring count",
+		"looks okay starting from samples = 4, rings = 4",
+		"*/",
+
+		"uniform bool pentagon; //use pentagon as bokeh shape?",
+		"float feather = 0.4; //pentagon shape feather",
+
+		"//------------------------------------------",
+
+		"float penta(vec2 coords) {",
+			"//pentagonal shape",
+			"float scale = float(rings) - 1.3;",
+			"vec4  HS0 = vec4( 1.0,         0.0,         0.0,  1.0);",
+			"vec4  HS1 = vec4( 0.309016994, 0.951056516, 0.0,  1.0);",
+			"vec4  HS2 = vec4(-0.809016994, 0.587785252, 0.0,  1.0);",
+			"vec4  HS3 = vec4(-0.809016994,-0.587785252, 0.0,  1.0);",
+			"vec4  HS4 = vec4( 0.309016994,-0.951056516, 0.0,  1.0);",
+			"vec4  HS5 = vec4( 0.0        ,0.0         , 1.0,  1.0);",
+
+			"vec4  one = vec4( 1.0 );",
+
+			"vec4 P = vec4((coords),vec2(scale, scale));",
+
+			"vec4 dist = vec4(0.0);",
+			"float inorout = -4.0;",
+
+			"dist.x = dot( P, HS0 );",
+			"dist.y = dot( P, HS1 );",
+			"dist.z = dot( P, HS2 );",
+			"dist.w = dot( P, HS3 );",
+
+			"dist = smoothstep( -feather, feather, dist );",
+
+			"inorout += dot( dist, one );",
+
+			"dist.x = dot( P, HS4 );",
+			"dist.y = HS5.w - abs( P.z );",
+
+			"dist = smoothstep( -feather, feather, dist );",
+			"inorout += dist.x;",
+
+			"return clamp( inorout, 0.0, 1.0 );",
+		"}",
+
+		"float bdepth(vec2 coords) {",
+			"// Depth buffer blur",
+			"float d = 0.0;",
+			"float kernel[9];",
+			"vec2 offset[9];",
+
+			"vec2 wh = vec2(texel.x, texel.y) * dbsize;",
+
+			"offset[0] = vec2(-wh.x,-wh.y);",
+			"offset[1] = vec2( 0.0, -wh.y);",
+			"offset[2] = vec2( wh.x -wh.y);",
+
+			"offset[3] = vec2(-wh.x,  0.0);",
+			"offset[4] = vec2( 0.0,   0.0);",
+			"offset[5] = vec2( wh.x,  0.0);",
+
+			"offset[6] = vec2(-wh.x, wh.y);",
+			"offset[7] = vec2( 0.0,  wh.y);",
+			"offset[8] = vec2( wh.x, wh.y);",
+
+			"kernel[0] = 1.0/16.0;   kernel[1] = 2.0/16.0;   kernel[2] = 1.0/16.0;",
+			"kernel[3] = 2.0/16.0;   kernel[4] = 4.0/16.0;   kernel[5] = 2.0/16.0;",
+			"kernel[6] = 1.0/16.0;   kernel[7] = 2.0/16.0;   kernel[8] = 1.0/16.0;",
+
+
+			"for( int i=0; i<9; i++ ) {",
+				"float tmp = texture2D(tDepth, coords + offset[i]).r;",
+				"d += tmp * kernel[i];",
+			"}",
+
+			"return d;",
+		"}",
+
+
+		"vec3 color(vec2 coords,float blur) {",
+			"//processing the sample",
+
+			"vec3 col = vec3(0.0);",
+
+			"col.r = texture2D(tColor,coords + vec2(0.0,1.0)*texel*fringe*blur).r;",
+			"col.g = texture2D(tColor,coords + vec2(-0.866,-0.5)*texel*fringe*blur).g;",
+			"col.b = texture2D(tColor,coords + vec2(0.866,-0.5)*texel*fringe*blur).b;",
+
+			"vec3 lumcoeff = vec3(0.299,0.587,0.114);",
+			"float lum = dot(col.rgb, lumcoeff);",
+			"float thresh = max((lum-threshold)*gain, 0.0);",
+			"return col+mix(vec3(0.0),col,thresh*blur);",
+		"}",
+
+		"vec2 rand(vec2 coord) {",
+			"// generating noise / pattern texture for dithering",
+
+			"float noiseX = ((fract(1.0-coord.s*(width/2.0))*0.25)+(fract(coord.t*(height/2.0))*0.75))*2.0-1.0;",
+			"float noiseY = ((fract(1.0-coord.s*(width/2.0))*0.75)+(fract(coord.t*(height/2.0))*0.25))*2.0-1.0;",
+
+			"if (noise) {",
+				"noiseX = clamp(fract(sin(dot(coord ,vec2(12.9898,78.233))) * 43758.5453),0.0,1.0)*2.0-1.0;",
+				"noiseY = clamp(fract(sin(dot(coord ,vec2(12.9898,78.233)*2.0)) * 43758.5453),0.0,1.0)*2.0-1.0;",
+			"}",
+
+			"return vec2(noiseX,noiseY);",
+		"}",
+
+		"vec3 debugFocus(vec3 col, float blur, float depth) {",
+			"float edge = 0.002*depth; //distance based edge smoothing",
+			"float m = clamp(smoothstep(0.0,edge,blur),0.0,1.0);",
+			"float e = clamp(smoothstep(1.0-edge,1.0,blur),0.0,1.0);",
+
+			"col = mix(col,vec3(1.0,0.5,0.0),(1.0-m)*0.6);",
+			"col = mix(col,vec3(0.0,0.5,1.0),((1.0-e)-(1.0-m))*0.2);",
+
+			"return col;",
+		"}",
+
+		"float linearize(float depth) {",
+			"return -zfar * znear / (depth * (zfar - znear) - zfar);",
+		"}",
+
+
+		"float vignette() {",
+			"float dist = distance(vUv.xy, vec2(0.5,0.5));",
+			"dist = smoothstep(vignout+(fstop/vignfade), vignin+(fstop/vignfade), dist);",
+			"return clamp(dist,0.0,1.0);",
+		"}",
+
+		"float gather(float i, float j, int ringsamples, inout vec3 col, float w, float h, float blur) {",
+			"float rings2 = float(rings);",
+			"float step = PI*2.0 / float(ringsamples);",
+			"float pw = cos(j*step)*i;",
+			"float ph = sin(j*step)*i;",
+			"float p = 1.0;",
+			"if (pentagon) {",
+				"p = penta(vec2(pw,ph));",
+			"}",
+			"col += color(vUv.xy + vec2(pw*w,ph*h), blur) * mix(1.0, i/rings2, bias) * p;",
+			"return 1.0 * mix(1.0, i /rings2, bias) * p;",
+		"}",
+
+		"void main() {",
+			"//scene depth calculation",
+
+			"float depth = linearize(texture2D(tDepth,vUv.xy).x);",
+
+			"// Blur depth?",
+			"if (depthblur) {",
+				"depth = linearize(bdepth(vUv.xy));",
+			"}",
+
+			"//focal plane calculation",
+
+			"float fDepth = focalDepth;",
+
+			"if (autofocus) {",
+
+				"fDepth = linearize(texture2D(tDepth,focusCoords).x);",
+
+			"}",
+
+			"// dof blur factor calculation",
+
+			"float blur = 0.0;",
+
+			"if (manualdof) {",
+				"float a = depth-fDepth; // Focal plane",
+				"float b = (a-fdofstart)/fdofdist; // Far DoF",
+				"float c = (-a-ndofstart)/ndofdist; // Near Dof",
+				"blur = (a>0.0) ? b : c;",
+			"} else {",
+				"float f = focalLength; // focal length in mm",
+				"float d = fDepth*1000.0; // focal plane in mm",
+				"float o = depth*1000.0; // depth in mm",
+
+				"float a = (o*f)/(o-f);",
+				"float b = (d*f)/(d-f);",
+				"float c = (d-f)/(d*fstop*CoC);",
+
+				"blur = abs(a-b)*c;",
+			"}",
+
+			"blur = clamp(blur,0.0,1.0);",
+
+			"// calculation of pattern for dithering",
+
+			"vec2 noise = rand(vUv.xy)*namount*blur;",
+
+			"// getting blur x and y step factor",
+
+			"float w = (1.0/width)*blur*maxblur+noise.x;",
+			"float h = (1.0/height)*blur*maxblur+noise.y;",
+
+			"// calculation of final color",
+
+			"vec3 col = vec3(0.0);",
+
+			"if(blur < 0.05) {",
+				"//some optimization thingy",
+				"col = texture2D(tColor, vUv.xy).rgb;",
+			"} else {",
+				"col = texture2D(tColor, vUv.xy).rgb;",
+				"float s = 1.0;",
+				"int ringsamples;",
+
+				"for (int i = 1; i <= rings; i++) {",
+					"/*unboxstart*/",
+					"ringsamples = i * samples;",
+
+					"for (int j = 0 ; j < maxringsamples ; j++) {",
+						"if (j >= ringsamples) break;",
+						"s += gather(float(i), float(j), ringsamples, col, w, h, blur);",
+					"}",
+					"/*unboxend*/",
+				"}",
+
+				"col /= s; //divide by sample count",
+			"}",
+
+			"if (showFocus) {",
+				"col = debugFocus(col, blur, depth);",
+			"}",
+
+			"if (vignetting) {",
+				"col *= vignette();",
+			"}",
+
+			"gl_FragColor.rgb = col;",
+			"gl_FragColor.a = 1.0;",
+		"} "
+
+	].join("\n"),
+
+	generate: function(rings, samples) {
+
+		var frag = THREE.BokehShader.fragmentShader;
+		frag = frag
+			.replace(/#RINGS#/, rings)
+			.replace(/#SAMPLES#/, samples);
+
+		/*
+		var unboxing = false;
+		if (unboxing) {
+			var codegen = [];
+			for (var i=1;i<=rings;i++) {
+				var ringsamples = i * samples;
+				var a = 'if (i==?) {'.replace('?', i);
+				codegen.push( i == 1 ? a : '  ' + a); //else
+				codegen.push('for (int j = 0 ; j < ?; j++) '.replace('?', ringsamples));
+				codegen.push('\ts += gather(float(i), float(j), ?, col, w, h, blur);'.replace('?', ringsamples))
+				codegen.push('}');
+			}
+
+			codegen = codegen.join('\n');
+
+			frag = frag.replace(/\/[*]unboxstart[*]\/([\s\S]*)\/[*]unboxend[*]\//m, codegen);
+		}
+		*/
+
+		return frag;
+	}
+
+};

+ 583 - 0
examples/webgl_postprocessing_dof2.html

@@ -0,0 +1,583 @@
+<!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;
+
+				top: 0px;
+
+				position: absolute;
+				padding: 5px;
+				width: 500px;
+				z-index: 5;
+
+				left: 50px;
+
+			}
+		</style>
+	</head>
+<!-- TODO
+Setup Number Focus Test Plates
+Use WEBGL Depth buffer support?
+-->
+	<body>
+		<script src="../build/three.min.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>
+		<script src="js/modifiers/SubdivisionModifier.js"></script>
+
+		<div id="info">
+
+
+	<a href="http://threejs.org" target="_blank">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 src="js/shaders/BokehShader2.js"></script>
+		<script>
+
+			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
+
+			var container, stats;
+			var camera, scene, renderer,
+				material_depth;
+
+			var mouseX = 0, mouseY = 0;
+
+			var windowHalfX = window.innerWidth / 2;
+			var windowHalfY = window.innerHeight / 2;
+
+			var height = window.innerHeight;
+
+			var postprocessing = { enabled  : true };
+
+			var shaderSettings = {
+				rings: 3,
+				samples: 4
+			};
+
+			var projector;
+			var singleMaterial = false;
+			var vector = new THREE.Vector3();
+			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 / height, 1, 3000 );
+
+				camera.position.y = 150;
+				camera.position.z = 450;
+
+
+				scene = new THREE.Scene();
+
+				renderer = new THREE.WebGLRenderer( { antialias: false, clearColor: 0xffffff } );
+				renderer.setSize( window.innerWidth, height );
+				container.appendChild( renderer.domElement );
+
+				projector = new THREE.Projector();
+
+				renderer.sortObjects = false;
+
+				material_depth = new THREE.MeshDepthMaterial();
+
+				//
+
+				object = new THREE.Object3D();
+				scene.add( object );
+
+				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 = THREE.ImageUtils.loadTextureCube( urls  );
+				textureCube.format = THREE.RGBFormat;
+
+				// Skybox
+
+				var shader = THREE.ShaderLib[ "cube" ];
+				shader.uniforms[ "tCube" ].value = textureCube;
+
+				var material = new THREE.ShaderMaterial( {
+
+					fragmentShader: shader.fragmentShader,
+					vertexShader: shader.vertexShader,
+					uniforms: shader.uniforms,
+					depthWrite: false,
+					side: THREE.BackSide
+
+				} );
+
+				mesh = new THREE.Mesh( new THREE.CubeGeometry( 1000, 1000, 1000 ), material );
+				scene.add( mesh );
+
+
+				// Focusing Floor
+
+				// var planeGeometry = new THREE.PlaneGeometry( 500, 500, 1, 1 );
+
+				// var planeMat = new THREE.MeshPhongMaterial(
+				// 	{  map: texture }
+				// );
+				// var plane = new THREE.Mesh(planeGeometry, planeMat );
+				// plane.rotation.x = - Math.PI / 2;
+				// plane.position.y = - 5;
+
+				// scene.add(plane);
+
+				// Plane particles
+				var planePiece = new THREE.PlaneGeometry( 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 (i=0;i<leaves;i++) {
+					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.JSONLoader();
+
+				var modifier = new THREE.SubdivisionModifier( 1 );
+
+				loader2.load( 'obj/Suzanne.js', function ( geometry ) {
+
+					var smooth = geometry;
+					smooth.mergeVertices();
+					modifier.modify( smooth );
+
+					smooth.computeCentroids();
+					smooth.computeFaceNormals();
+					smooth.computeVertexNormals();
+
+					var material = new THREE.MeshPhongMaterial( {
+						color: 0xffffff,
+						specular:0xffffff,
+						envMap: textureCube,
+						combine: THREE.MultiplyOperation,
+						shininess: 50,
+						reflectivity: 1.0
+					});
+
+					var mesh;
+
+					var monkeys = 20;
+					for ( var i = 0; i < monkeys; i ++ ) {
+						var mesh = new THREE.Mesh( geometry, material );
+						mesh.scale.multiplyScalar(30);
+
+
+						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.x = Math.PI / 2;
+						mesh.rotation.z = -i / monkeys * Math.PI * 2;
+
+						object.add( mesh );
+
+					}
+
+				} );
+
+
+
+				// Add Balls
+				var geometry = new THREE.SphereGeometry( 1, 20, 20 );
+
+				for ( var i = 0; i < 20; i ++ ) {
+					// MeshPhongMaterial
+					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.set(
+						(Math.random() - 0.5) * 200,
+						Math.random() * 50, 
+						(Math.random() - 0.5)  * 200
+					);
+
+					mesh.scale.multiplyScalar(10);
+					object.add( mesh );
+
+				}
+
+
+				// Lights
+
+				// scene.add( new THREE.AmbientLight( 0xffffff ) );
+
+				// light = new THREE.DirectionalLight( 0xffffff );
+				// light.position.set( 1, 1, 1 );
+				// scene.add( light );
+
+				scene.add( new THREE.AmbientLight( 0x222222 ) );
+
+				directionalLight = new THREE.DirectionalLight( 0xffffff, 2 );
+				directionalLight.position.set( 2, 1.2, 10 ).normalize();
+				scene.add( directionalLight );
+
+				directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
+				directionalLight.position.set( -2, 1.2, -10 ).normalize();
+				scene.add( directionalLight );
+
+
+				initPostprocessing();
+
+				renderer.autoClear = false;
+
+				renderer.domElement.style.position = 'absolute';
+				renderer.domElement.style.left = "0px";
+
+				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 );
+
+				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.setLens(effectController.focalLength);
+
+				};
+
+				var gui = new dat.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();
+
+				renderer.setSize( window.innerWidth, window.innerHeight );
+
+			}
+
+			function onDocumentMouseMove( event ) {
+
+				mouseX = event.clientX - windowHalfX;
+				mouseY = event.clientY - 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();
+
+					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 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, height, pars );
+				postprocessing.rtTextureColor = new THREE.WebGLRenderTarget( window.innerWidth, height, pars );
+
+
+				var fragmentShader = THREE.BokehShader.generate(shaderSettings.rings, shaderSettings.samples);
+
+				var bokeh_shader = THREE.BokehShader;
+
+				postprocessing.bokeh_uniforms = THREE.UniformsUtils.clone( bokeh_shader.uniforms );
+
+				postprocessing.bokeh_uniforms[ "tColor" ].value = postprocessing.rtTextureColor;
+				postprocessing.bokeh_uniforms[ "tDepth" ].value = postprocessing.rtTextureDepth;
+
+				postprocessing.bokeh_uniforms[ "textureWidth" ].value = window.innerWidth;
+
+				postprocessing.bokeh_uniforms[ "textureHeight" ].value = height;
+
+				postprocessing.materialBokeh = new THREE.ShaderMaterial( {
+
+					uniforms: postprocessing.bokeh_uniforms,
+					vertexShader: bokeh_shader.vertexShader,
+					fragmentShader: fragmentShader
+
+				} );
+
+				postprocessing.quad = new THREE.Mesh( new THREE.PlaneGeometry( window.innerWidth, window.innerHeight ), postprocessing.materialBokeh );
+				postprocessing.quad.position.z = - 500;
+				postprocessing.scene.add( postprocessing.quad );
+
+			}
+
+			function shaderUpdate() {
+				var fragmentShader = THREE.BokehShader.generate(shaderSettings.rings, shaderSettings.samples);
+
+				postprocessing.materialBokeh.fragmentShader = fragmentShader;
+				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 );
+
+				vector.set( mouseX / windowHalfX, -mouseY / windowHalfY, 1 );
+
+
+				if (effectController.jsDepthCalculation) {
+
+					projector.unprojectVector( vector, camera );
+
+					var raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );
+
+					var intersects = raycaster.intersectObjects( scene.children, true );
+
+
+					if ( intersects.length > 0 ) {
+
+						var targetDistance = intersects[ 0 ].distance;
+
+						distance += (targetDistance - distance) * 0.03;
+
+						var sdistance = smoothstep(camera.near, camera.far, distance);
+
+						var ldistance = linearize(1 -  sdistance);
+
+						// (Math.random() < 0.1) && console.log('moo', targetDistance, distance, ldistance);
+
+						postprocessing.bokeh_uniforms[ 'focalDepth' ].value = ldistance;
+
+						effectController['focalDepth'] = ldistance;
+
+					}
+
+				}
+
+				for (i=0;i<leaves;i++) {
+					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
+
+					scene.overrideMaterial = null;
+					renderer.render( scene, camera, postprocessing.rtTextureColor, true );
+
+					// Render depth into texture
+
+					scene.overrideMaterial = material_depth;
+					renderer.render( scene, camera, postprocessing.rtTextureDepth, true );
+
+					// Render bokeh composite
+
+					renderer.render( postprocessing.scene, postprocessing.camera );
+
+
+				} else {
+
+					scene.overrideMaterial = null;
+
+					renderer.clear();
+					renderer.render( scene, camera );
+
+				}
+
+			}
+
+
+		</script>
+	</body>
+</html>