Bläddra i källkod

Improved AsciiEffect example.

Mr.doob 13 år sedan
förälder
incheckning
b959ceae62
2 ändrade filer med 84 tillägg och 155 borttagningar
  1. 61 134
      examples/canvas_ascii_effect.html
  2. 23 21
      examples/js/effects/AsciiEffect.js

+ 61 - 134
examples/canvas_ascii_effect.html

@@ -2,7 +2,7 @@
 <html lang="en">
 
 	<head>
-		<title>three.js ASCII - geometry - cube</title>
+		<title>three.js - ASCII Effect</title>
 		<meta charset="utf-8">
 		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
 		<style>
@@ -17,172 +17,99 @@
 	<body>
 
 		<script src="../build/Three.js"></script>
+		<script src="js/effects/AsciiEffect.js"></script>
 		<script src="js/Stats.js"></script>
-		<script src="js/renderers/AsciiRenderer.js"></script>
 		<script>
-		// Just swap out the default CanvasRenderer -> AsciiEffect and things should work
-		// Let's start with the class spinning cube example from three.js
 
-		var container, stats;
+			var container, stats;
 
-		var camera, scene, renderer;
+			var camera, controls, scene, renderer;
 
+			var sphere, plane;
 
-		var cube, plane;
+			var start = Date.now();
 
-		var targetYRotation = targetXRotation = 0;
-		var targetYRotationOnMouseDown = targetXRotationOnMouseDown = 0;
+			init();
+			animate();
 
-		var mouseX = 0, mouseY = 0;
+			function init() {
 
-		var mouseYOnMouseDown = mouseXOnMouseDown = 0;
-		var windowHalfX = window.innerWidth / 2;
-		var windowHalfY = window.innerHeight / 2;
+				container = document.createElement( 'div' );
+				document.body.appendChild( container );
 
+				var info = document.createElement( 'div' );
+				info.style.position = 'absolute';
+				info.style.top = '10px';
+				info.style.width = '100%';
+				info.style.textAlign = 'center';
+				info.innerHTML = 'Drag to change the view';
+				container.appendChild( info );
 
-		init();
-		animate();
+				scene = new THREE.Scene();
 
-		function init() {
+				camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
+				camera.position.y = 150;
+				camera.position.z = 500;
+				scene.add( camera );
 
-			container = document.createElement( 'div' );
-			document.body.appendChild( container );
+				controls = new THREE.TrackballControls( camera );
 
-			var info = document.createElement( 'div' );
-			info.style.position = 'absolute';
-			info.style.top = '10px';
-			info.style.width = '100%';
-			info.style.textAlign = 'center';
-			info.innerHTML = 'Drag to spin the cube';
-			container.appendChild( info );
+				var light = new THREE.PointLight( 0xffffff );
+				light.position.set( 500, 500, 500 );
+				scene.add( light );
 
-			scene = new THREE.Scene();
+				var light = new THREE.PointLight( 0xffffff, 0.25 );
+				light.position.set( - 500, - 500, - 500 );
+				scene.add( light );
 
-			camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
-			camera.position.y = 150;
-			camera.position.z = 500;
-			scene.add( camera );
+				sphere = new THREE.Mesh( new THREE.SphereGeometry( 200, 20, 10 ), new THREE.MeshLambertMaterial( { shading: THREE.FlatShading } ) );
+				scene.add( sphere );
 
-			// Cube
+				// Plane
 
-			var materials = [];
+				plane = new THREE.Mesh( new THREE.PlaneGeometry( 400, 400 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
+				plane.position.y = - 200;
+				scene.add( plane );
 
-			for ( var i = 0; i < 6; i ++ ) {
+				renderer = new THREE.CanvasRenderer();
+				// container.appendChild( renderer.domElement );
 
-				materials.push( new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } ) );
+				effect = new THREE.AsciiEffect( renderer );
+				effect.setSize( window.innerWidth, window.innerHeight );
+				container.appendChild( effect.domElement );
 
-			}
-
-			cube = new THREE.Mesh( new THREE.CubeGeometry( 200, 200, 200, 1, 1, 1, materials ), new THREE.MeshFaceMaterial() );
-			cube.position.y = 150;
-			scene.add( cube );
-
-			// Plane
-
-			plane = new THREE.Mesh( new THREE.PlaneGeometry( 400, 400 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
-			scene.add( plane );
-
-			renderer = new THREE.AsciiEffect(); //THREE.CanvasRenderer
-			renderer.setSize( window.innerWidth, window.innerHeight );
-		  
-
-			container.appendChild( renderer.domElement );
-
-			stats = new Stats();
-			stats.domElement.style.position = 'absolute';
-			stats.domElement.style.top = '0px';
-			container.appendChild( stats.domElement );
-
-			document.addEventListener( 'mousedown', onDocumentMouseDown, false );
-			document.addEventListener( 'touchstart', onDocumentTouchStart, false );
-			document.addEventListener( 'touchmove', onDocumentTouchMove, false );
-		}
-
-		//
-
-		function onDocumentMouseDown( event ) {
-
-			event.preventDefault();
-		   
-		  
-			document.addEventListener( 'mousemove', onDocumentMouseMove, false );
-			document.addEventListener( 'mouseup', onDocumentMouseUp, false );
-			document.addEventListener( 'mouseout', onDocumentMouseOut, false );
-
-			mouseXOnMouseDown = event.clientX - windowHalfX;
-			mouseYOnMouseDown = event.clientY - windowHalfY;
-			targetYRotationOnMouseDown = targetYRotation;
-			targetXRotationOnMouseDown = targetXRotation;
-		}
-
-		function onDocumentMouseMove( event ) {
-
-			mouseX = event.clientX - windowHalfX;
+				stats = new Stats();
+				stats.domElement.style.position = 'absolute';
+				stats.domElement.style.top = '0px';
+				container.appendChild( stats.domElement );
 
-			targetYRotation = targetYRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
-			targetXRotation = targetXRotationOnMouseDown + ( mouseY - mouseYOnMouseDown ) * 0.02;
-		}
-
-		function onDocumentMouseUp( event ) {
-
-			document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
-			document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
-			document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
-		}
-
-		function onDocumentMouseOut( event ) {
-
-			document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
-			document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
-			document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
-		}
-
-		function onDocumentTouchStart( event ) {
-
-			if ( event.touches.length == 1 ) {
-
-				event.preventDefault();
-
-				mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
-				targetRotationOnMouseDown = targetRotation;
 			}
-		}
-
-		function onDocumentTouchMove( event ) {
 
-			if ( event.touches.length == 1 ) {
+			//
 
-				event.preventDefault();
-
-				mouseX = event.touches[ 0 ].pageX - windowHalfX;
-				targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;;
-			}
-		}
+			function animate() {
 
-		//
+				requestAnimationFrame( animate );
 
-		function animate() {
+				render();
+				stats.update();
 
-			requestAnimationFrame( animate );
-
-			render();
-			stats.update();
+			}
 
-		}
+			function render() {
 
-		function render() {
+				var timer = Date.now() - start;
 
-			cube.rotation.x += ( targetXRotation - cube.rotation.x ) * 0.05;
-			plane.rotation.y  = cube.rotation.y += ( targetYRotation - cube.rotation.y ) * 0.05;
-			
-			
-			renderer.render( scene, camera );
-		 
+				sphere.position.y = Math.abs( Math.sin( timer * 0.002 ) ) * 150;
+				sphere.rotation.x = timer * 0.0003;
+				sphere.rotation.z = timer * 0.0002;
 
+				controls.update();
 
-		}
+				effect.render( scene, camera );
 
+			}
 
 		</script>
-</body>
-</html>
+	</body>
+</html>

+ 23 - 21
examples/js/effects/AsciiEffects.js → examples/js/effects/AsciiEffect.js

@@ -9,8 +9,7 @@
  * 16 April 2012 - @blurspline
  */
   
-THREE.AsciiEffect = function( renderer, charSet, options ) {
-
+THREE.AsciiEffect = function ( renderer, charSet, options ) {
 
 	// its fun to create one your own!
 	charSet = (charSet === undefined) ? ' .:-=+*#%@' : charSet; 
@@ -30,30 +29,34 @@ THREE.AsciiEffect = function( renderer, charSet, options ) {
 	
 	var strResolution = 'low';
 
-	renderer = (renderer === undefined) ? THREE.CanvasRenderer : renderer; //THREE.WebGLRenderer 
-	var canvasRenderer = new renderer();
 	var width, height;
 
 	var domElement = document.createElement('div');
+	domElement.style.cursor = 'default';
+
 	var oAscii = document.createElement("table");
 	domElement.appendChild(oAscii);
 
 	var iWidth, iHeight;
 	var oImg;
 
-	this.setSize = function(w, h) {
+	this.setSize = function ( w, h ) {
+
 		width = w;
 		height = h;
-		canvasRenderer.setSize( w, h );
+
+		renderer.setSize( w, h );
 
 		initAsciiSize();
 
 	};
 
 
-	this.render = function() {
-		canvasRenderer.render.apply(canvasRenderer, arguments);
-		asciifyImage(canvasRenderer, oAscii);
+	this.render = function ( scene, camera ) {
+
+		renderer.render( scene, camera );
+		asciifyImage( renderer, oAscii );
+
 	};
 
 	this.domElement = domElement;
@@ -76,7 +79,7 @@ THREE.AsciiEffect = function( renderer, charSet, options ) {
 		// oCanvas.style.width = iWidth;
 		// oCanvas.style.height = iHeight;
 
-		oImg = canvasRenderer.domElement;
+		oImg = renderer.domElement;
 		if (oImg.style.backgroundColor) {
 			oAscii.rows[0].cells[0].style.backgroundColor = oImg.style.backgroundColor;
 			oAscii.rows[0].cells[0].style.color = oImg.style.color;
@@ -105,7 +108,7 @@ THREE.AsciiEffect = function( renderer, charSet, options ) {
 	var aDefaultColorCharList = (" CGO08@").split("");
 	var strFont = "courier new, monospace";
 
-	var oCanvasImg = canvasRenderer.domElement;
+	var oCanvasImg = renderer.domElement;
 
 	var oCanvas = document.createElement("canvas");
 	if (!oCanvas.getContext) {
@@ -168,8 +171,7 @@ THREE.AsciiEffect = function( renderer, charSet, options ) {
 
 
 	// convert img element to ascii
-	function asciifyImage(canvasRenderer, oAscii) 
-	{
+	function asciifyImage(canvasRenderer, oAscii) {
 
 		oCtx.clearRect(0, 0, iWidth, iHeight);
 		oCtx.drawImage(oCanvasImg, 0, 0, iWidth, iHeight);
@@ -191,12 +193,12 @@ THREE.AsciiEffect = function( renderer, charSet, options ) {
 				var iCharIdx;
 
 				var fBrightness;
-			  
+
 				fBrightness = (0.3*iRed + 0.59*iGreen + 0.11*iBlue) / 255;
 				// fBrightness = (0.3*iRed + 0.5*iGreen + 0.3*iBlue) / 255;
 				
 				if (iAlpha == 0) {
-  					// should calculate alpha instead, but quick hack :)
+					// should calculate alpha instead, but quick hack :)
 					//fBrightness *= (iAlpha / 255); 
 					fBrightness = 1;
 					
@@ -207,16 +209,16 @@ THREE.AsciiEffect = function( renderer, charSet, options ) {
 				if (bInvert) {
 					iCharIdx = aCharList.length - iCharIdx - 1;
 				}
-			  
+
 				// good for debugging
 				//fBrightness = Math.floor(fBrightness * 10);
 				//strThisChar = fBrightness;
-			  
+
 				var strThisChar = aCharList[iCharIdx];		
-			  
-				if (strThisChar===undefined || strThisChar == " ") 
+
+				if (strThisChar===undefined || strThisChar == " ")
 					strThisChar = "&nbsp;";
-			  
+
 				if (bColor) {
 					strChars += "<span style='"
 						+ "color:rgb("+iRed+","+iGreen+","+iBlue+");"
@@ -241,4 +243,4 @@ THREE.AsciiEffect = function( renderer, charSet, options ) {
 	// end modified asciifyImage block
 
 	
-};
+};