Bläddra i källkod

Examples: Clean up

Mugen87 7 år sedan
förälder
incheckning
444b4cd7cb
1 ändrade filer med 97 tillägg och 126 borttagningar
  1. 97 126
      examples/webgl_postprocessing_dof2.html

+ 97 - 126
examples/webgl_postprocessing_dof2.html

@@ -53,14 +53,11 @@ Use WEBGL Depth buffer support?
 			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
 
 			var container, stats;
-			var camera, scene, renderer,
-				material_depth;
+			var camera, scene, renderer, materialDepth;
 
 			var windowHalfX = window.innerWidth / 2;
 			var windowHalfY = window.innerHeight / 2;
 
-			var height = window.innerHeight;
-
 			var postprocessing = { enabled  : true };
 
 			var shaderSettings = {
@@ -72,7 +69,7 @@ Use WEBGL Depth buffer support?
 			var mouse = new THREE.Vector2();
 			var raycaster = new THREE.Raycaster();
 			var distance = 100;
-			var target = new THREE.Vector3( 0, 20, -50 );
+			var target = new THREE.Vector3( 0, 20, - 50 );
 			var effectController;
 			var planes = [];
 			var leaves = 100;
@@ -85,7 +82,7 @@ Use WEBGL Depth buffer support?
 				container = document.createElement( 'div' );
 				document.body.appendChild( container );
 
-				camera = new THREE.PerspectiveCamera( 70, window.innerWidth / height, 1, 3000 );
+				camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 3000 );
 
 				camera.position.y = 150;
 				camera.position.z = 450;
@@ -95,56 +92,28 @@ Use WEBGL Depth buffer support?
 
 				renderer = new THREE.WebGLRenderer();
 				renderer.setPixelRatio( window.devicePixelRatio );
-				renderer.setSize( window.innerWidth, height );
+				renderer.setSize( window.innerWidth, window.innerHeight );
+				renderer.autoClear = false;
 				container.appendChild( renderer.domElement );
 
-				material_depth = new THREE.MeshDepthMaterial();
-				material_depth.depthPacking = THREE.RGBADepthPacking;
-				material_depth.blending = THREE.NoBlending;
+				materialDepth = new THREE.MeshDepthMaterial();
+				materialDepth.depthPacking = THREE.RGBADepthPacking;
+				materialDepth.blending = THREE.NoBlending;
 
-				//
+				// 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 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;
 
-				// 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
-
-				} );
-
-				var mesh = new THREE.Mesh( new THREE.BoxGeometry( 1000, 1000, 1000 ), material );
-				camera.add( mesh );
-
-
-				// Focusing Floor
+				scene.background = textureCube;
 
-				// var planeGeometry = new THREE.PlaneBufferGeometry( 500, 500, 1, 1 );
+				// plane particles
 
-				// 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.PlaneBufferGeometry( 10, 10, 1, 1 );
 
 				var planeMat = new THREE.MeshPhongMaterial( {
@@ -157,21 +126,23 @@ Use WEBGL Depth buffer support?
 
 				var rand = Math.random;
 
-				for (var i=0;i<leaves;i++) {
-					var plane = new THREE.Mesh(planePiece, planeMat);
-					plane.rotation.set(rand(), rand(), rand());
+				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.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);
+					scene.add( plane );
+					planes.push( plane );
+
 				}
 
-				// Adding Monkeys
+				// adding Monkeys
 
 				var loader2 = new THREE.BufferGeometryLoader();
 				loader2.load( 'models/json/suzanne_buffergeometry.json', function ( geometry ) {
@@ -194,12 +165,12 @@ Use WEBGL Depth buffer support?
 						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.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 = -Math.PI / 2;
-						mesh.rotation.z = -i / monkeys * Math.PI * 2;
+						mesh.rotation.y = - Math.PI / 2;
+						mesh.rotation.z = - i / monkeys * Math.PI * 2;
 
 						scene.add( mesh );
 
@@ -207,13 +178,12 @@ Use WEBGL Depth buffer support?
 
 				} );
 
+				// add balls
 
-
-				// Add Balls
-				var geometry = new THREE.SphereGeometry( 1, 20, 20 );
+				var geometry = new THREE.SphereBufferGeometry( 1, 20, 20 );
 
 				for ( var i = 0; i < 20; i ++ ) {
-					// MeshPhongMaterial
+
 					var ballmaterial = new THREE.MeshPhongMaterial( {
 						color: 0xffffff * Math.random(),
 						shininess: 0.5,
@@ -230,8 +200,7 @@ Use WEBGL Depth buffer support?
 
 				}
 
-
-				// Lights
+				// lights
 
 				scene.add( new THREE.AmbientLight( 0x222222 ) );
 
@@ -240,17 +209,11 @@ Use WEBGL Depth buffer support?
 				scene.add( directionalLight );
 
 				var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
-				directionalLight.position.set( -2, 1.2, -10 ).normalize();
+				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();
 				container.appendChild( stats.dom );
 
@@ -286,11 +249,16 @@ Use WEBGL Depth buffer support?
 
 				};
 
-				var matChanger = function( ) {
+				var matChanger = function() {
+
+					for ( var e in effectController ) {
+
+						if ( e in postprocessing.bokeh_uniforms ) {
+
+							postprocessing.bokeh_uniforms[ e ].value = effectController[ e ];
+
+						}
 
-					for (var e in effectController) {
-						if (e in postprocessing.bokeh_uniforms)
-						postprocessing.bokeh_uniforms[ e ].value = effectController[ e ];
 					}
 
 					postprocessing.enabled = effectController.enabled;
@@ -302,35 +270,35 @@ Use WEBGL Depth buffer support?
 
 				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, '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, '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, 'showFocus' ).onChange( matChanger );
+				gui.add( effectController, 'manualdof' ).onChange( matChanger );
+				gui.add( effectController, 'vignetting' ).onChange( matChanger );
 
-				gui.add( effectController, "depthblur" ).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, '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, 'focalLength', 16, 80, 0.001 ).onChange( matChanger );
 
-				gui.add( effectController, "noise" ).onChange( matChanger );
+				gui.add( effectController, 'noise' ).onChange( matChanger );
 
-				gui.add( effectController, "dithering", 0, 0.001, 0.0001 ).onChange( matChanger );
+				gui.add( effectController, 'dithering', 0, 0.001, 0.0001 ).onChange( matChanger );
 
-				gui.add( effectController, "pentagon" ).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 );
+				gui.add( shaderSettings, 'rings', 1, 8).step(1).onChange( shaderUpdate );
+				gui.add( shaderSettings, 'samples', 1, 13).step(1).onChange( shaderUpdate );
 
 				matChanger();
 
@@ -352,7 +320,7 @@ Use WEBGL Depth buffer support?
 				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));
+				postprocessing.bokeh_uniforms[ 'focusCoords' ].value.set( event.clientX / window.innerWidth, 1 - ( event.clientY / window.innerHeight ) );
 
 			}
 
@@ -391,21 +359,17 @@ Use WEBGL Depth buffer support?
 				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 );
-
-
+				postprocessing.rtTextureDepth = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
+				postprocessing.rtTextureColor = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
 
 				var bokeh_shader = THREE.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 = height;
+				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( {
 
@@ -427,9 +391,9 @@ Use WEBGL Depth buffer support?
 			}
 
 			function shaderUpdate() {
+
 				postprocessing.materialBokeh.defines.RINGS = shaderSettings.rings;
 				postprocessing.materialBokeh.defines.SAMPLES = shaderSettings.samples;
-
 				postprocessing.materialBokeh.needsUpdate = true;
 
 			}
@@ -443,29 +407,35 @@ Use WEBGL Depth buffer support?
 
 			}
 
-			function linearize(depth) {
+			function linearize( depth ) {
+
 				var zfar = camera.far;
 				var znear = camera.near;
-				return -zfar * znear / (depth * (zfar - znear) - zfar);
+				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 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 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.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 );
 
@@ -488,8 +458,6 @@ Use WEBGL Depth buffer support?
 
 						var ldistance = linearize(1 -  sdistance);
 
-						// (Math.random() < 0.1) && console.log('moo', targetDistance, distance, ldistance);
-
 						postprocessing.bokeh_uniforms[ 'focalDepth' ].value = ldistance;
 
 						effectController['focalDepth'] = ldistance;
@@ -498,15 +466,18 @@ Use WEBGL Depth buffer support?
 
 				}
 
-				for (var i=0;i<leaves;i++) {
-					var plane = planes[i];
+				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 ( plane.position.y < 0 ) plane.position.y += 300;
+
 				}
 
 
@@ -514,17 +485,17 @@ Use WEBGL Depth buffer support?
 
 					renderer.clear();
 
-					// Render scene into texture
+					// render scene into texture
 
 					renderer.render( scene, camera, postprocessing.rtTextureColor, true );
 
-					// Render depth into texture
+					// render depth into texture
 
-					scene.overrideMaterial = material_depth;
+					scene.overrideMaterial = materialDepth;
 					renderer.render( scene, camera, postprocessing.rtTextureDepth, true );
 					scene.overrideMaterial = null;
 
-					// Render bokeh composite
+					// render bokeh composite
 
 					renderer.render( postprocessing.scene, postprocessing.camera );