webgl_geometry_text.html 15.0 KB
Newer Older
M
Mr.doob 已提交
1
<!DOCTYPE html>
Z
zz85 已提交
2 3
<html lang="en">
	<head>
4
		<title>three.js webgl - geometry - text</title>
Z
zz85 已提交
5
		<meta charset="utf-8">
6 7
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
Z
zz85 已提交
8 9
			body {
				font-family: Monospace;
10 11
				background-color: #000;
				color: #fff;
Z
zz85 已提交
12 13 14 15 16
				margin: 0px;
				overflow: hidden;
			}
			#info {
				position: absolute;
17
				top: 10px;
Z
zz85 已提交
18 19
				width: 100%;
				text-align: center;
20 21
				z-index: 100;
				display:block;
Z
zz85 已提交
22
			}
23
			#info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
Z
zz85 已提交
24 25 26
		</style>
	</head>
	<body>
27 28

		<div id="info">
M
Mr.doob 已提交
29
		<a href="http://threejs.org" target="_blank">three.js</a> - procedural 3D text by <a href="http://www.lab4games.net/zz85/blog" target="_blank">zz85</a> &amp; alteredq
30 31
		(fonts from <a href="http://typeface.neocracy.org/">typeface.js</a> and <a href="http://en.wikipedia.org/wiki/Droid_%28font%29">Droid</a>)
		<br/>type to enter new text, drag to spin the text
32 33
		<br/><span class="button" id="color">change color</span>,
			<span class="button" id="font">change font</span>,
34
			<span class="button" id="weight">change weight</span>,
35 36
			<span class="button" id="bevel">change bevel</span>,
			<span class="button" id="postprocessing">change postprocessing</span>,
37 38 39
			<a id="permalink" href="#">permalink</a>
		</div>

Z
zz85 已提交
40

41
		<script src="../build/three.min.js"></script>
42
		<script src="js/utils/GeometryUtils.js"></script>
43

44
		<script src="js/shaders/ConvolutionShader.js"></script>
45
		<script src="js/shaders/CopyShader.js"></script>
46
		<script src="js/shaders/FilmShader.js"></script>
47
		<script src="js/shaders/FXAAShader.js"></script>
48

M
Mr.doob 已提交
49 50 51 52 53 54
		<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/BloomPass.js"></script>
		<script src="js/postprocessing/FilmPass.js"></script>
55

M
Mr.doob 已提交
56
		<script src="js/Detector.js"></script>
57
		<script src="js/libs/stats.min.js"></script>
58

59
		<!-- load the font files -->
60

M
Mr.doob 已提交
61 62 63 64 65 66 67 68 69 70
		<script src="fonts/gentilis_bold.typeface.js"></script>
		<script src="fonts/gentilis_regular.typeface.js"></script>
		<script src="fonts/optimer_bold.typeface.js"></script>
		<script src="fonts/optimer_regular.typeface.js"></script>
		<script src="fonts/helvetiker_bold.typeface.js"></script>
		<script src="fonts/helvetiker_regular.typeface.js"></script>
		<script src="fonts/droid/droid_sans_regular.typeface.js"></script>
		<script src="fonts/droid/droid_sans_bold.typeface.js"></script>
		<script src="fonts/droid/droid_serif_regular.typeface.js"></script>
		<script src="fonts/droid/droid_serif_bold.typeface.js"></script>
71

72
		<!-- todo async loader for fonts -->
Z
zz85 已提交
73

M
Mr.doob 已提交
74
		<script>
Z
zz85 已提交
75

76 77 78
			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var container, stats, permalink, hex, color;
Z
zz85 已提交
79

80
			var camera, cameraTarget, scene, renderer;
Z
zz85 已提交
81

82
			var composer;
83
			var effectFXAA;
84

M
Mr.doob 已提交
85
			var group, textMesh1, textMesh2, textGeo, material;
86 87

			var firstLetter = true;
88

89
			var text = "three.js",
90

91
				height = 20,
92
				size = 70,
93
				hover = 30,
94

95
				curveSegments = 4,
96

97 98
				bevelThickness = 2,
				bevelSize = 1.5,
99
				bevelSegments = 3,
100
				bevelEnabled = true,
101

102 103 104
				font = "optimer", // helvetiker, optimer, gentilis, droid sans, droid serif
				weight = "bold", // normal bold
				style = "normal"; // normal italic
105 106

			var mirror = true;
107

108
			var fontMap = {
109 110 111 112 113 114

				"helvetiker": 0,
				"optimer": 1,
				"gentilis": 2,
				"droid sans": 3,
				"droid serif": 4
115

116
			};
117

118
			var weightMap = {
119 120 121 122 123

				"normal": 0,
				"bold": 1

			};
Z
zz85 已提交
124

125 126
			var reverseFontMap = {};
			var reverseWeightMap = {};
127

128 129
			for ( var i in fontMap ) reverseFontMap[ fontMap[i] ] = i;
			for ( var i in weightMap ) reverseWeightMap[ weightMap[i] ] = i;
Z
zz85 已提交
130 131 132 133 134 135 136 137 138

			var targetRotation = 0;
			var targetRotationOnMouseDown = 0;

			var mouseX = 0;
			var mouseXOnMouseDown = 0;

			var windowHalfX = window.innerWidth / 2;
			var windowHalfY = window.innerHeight / 2;
139

140
			var postprocessing = { enabled : false };
141 142
			var glow = 0.9;

Z
zz85 已提交
143 144 145
			init();
			animate();

146
			function capitalize( txt ) {
147

148 149 150
				return txt.substring( 0, 1 ).toUpperCase() + txt.substring( 1 );

			}
151

152
			function decimalToHex( d ) {
153

154
				var hex = Number( d ).toString( 16 );
155
				hex = "000000".substr( 0, 6 - hex.length ) + hex;
156 157 158
				return hex.toUpperCase();

			}
159

Z
zz85 已提交
160
			function init() {
161

Z
zz85 已提交
162 163 164
				container = document.createElement( 'div' );
				document.body.appendChild( container );

165
				permalink = document.getElementById( "permalink" );
166

167 168
				// CAMERA

169 170 171 172
				camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 1500 );
				camera.position.set( 0, 400, 700 );

				cameraTarget = new THREE.Vector3( 0, 150, 0 );
Z
zz85 已提交
173

174 175 176 177 178
				// SCENE

				scene = new THREE.Scene();
				scene.fog = new THREE.Fog( 0x000000, 250, 1400 );

179
				// LIGHTS
Z
zz85 已提交
180

181
				var dirLight = new THREE.DirectionalLight( 0xffffff, 0.125 );
182
				dirLight.position.set( 0, 0, 1 ).normalize();
183
				scene.add( dirLight );
Z
zz85 已提交
184

185
				var pointLight = new THREE.PointLight( 0xffffff, 1.5 );
Z
zz85 已提交
186
				pointLight.position.set( 0, 100, 90 );
187
				scene.add( pointLight );
Z
zz85 已提交
188

189 190 191 192 193 194 195
				//text = capitalize( font ) + " " + capitalize( weight );
				//text = "abcdefghijklmnopqrstuvwxyz0123456789";
				//text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

				// Get text from hash

				var hash = document.location.hash.substr( 1 );
196

197
				if ( hash.length !== 0 ) {
198

199 200 201 202
					var colorhash  = hash.substring( 0, 6 );
					var fonthash   = hash.substring( 6, 7 );
					var weighthash = hash.substring( 7, 8 );
					var pphash 	   = hash.substring( 8, 9 );
203
					var bevelhash  = hash.substring( 9, 10 );
204
					var texthash   = hash.substring( 10 );
205 206 207

					hex = colorhash;
					pointLight.color.setHex( parseInt( colorhash, 16 ) );
208

209 210
					font = reverseFontMap[ parseInt( fonthash ) ];
					weight = reverseWeightMap[ parseInt( weighthash ) ];
211

212
					postprocessing.enabled = parseInt( pphash );
213
					bevelEnabled = parseInt( bevelhash );
214

215
					text = decodeURI( texthash );
Z
zz85 已提交
216

217
					updatePermalink();
Z
zz85 已提交
218

219
				} else {
Z
zz85 已提交
220

M
Mr.doob 已提交
221
					pointLight.color.setHSL( Math.random(), 1, 0.5 );
M
Mr.doob 已提交
222
					hex = decimalToHex( pointLight.color.getHex() );
Z
zz85 已提交
223 224

				}
225

M
Mr.doob 已提交
226
				material = new THREE.MeshFaceMaterial( [
227 228 229
					new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading } ), // front
					new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.SmoothShading } ) // side
				] );
230

M
Mr.doob 已提交
231
				group = new THREE.Group();
M
Mr.doob 已提交
232
				group.position.y = 100;
233

M
Mr.doob 已提交
234
				scene.add( group );
235

236 237
				createText();

238 239 240 241
				var plane = new THREE.Mesh(
					new THREE.PlaneBufferGeometry( 10000, 10000 ),
					new THREE.MeshBasicMaterial( { color: 0xffffff, opacity: 0.5, transparent: true } )
				);
242
				plane.position.y = 100;
243
				plane.rotation.x = - Math.PI / 2;
244
				scene.add( plane );
245

246 247
				// RENDERER

248
				renderer = new THREE.WebGLRenderer( { antialias: true } );
M
Mr.doob 已提交
249
				renderer.setClearColor( scene.fog.color );
250 251
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
Z
zz85 已提交
252 253
				container.appendChild( renderer.domElement );

254 255
				// STATS

Z
zz85 已提交
256 257 258
				stats = new Stats();
				stats.domElement.style.position = 'absolute';
				stats.domElement.style.top = '0px';
259
				//container.appendChild( stats.domElement );
Z
zz85 已提交
260

261 262
				// EVENTS

263
				document.addEventListener( 'mousedown', onDocumentMouseDown, false );
Z
zz85 已提交
264 265
				document.addEventListener( 'touchstart', onDocumentTouchStart, false );
				document.addEventListener( 'touchmove', onDocumentTouchMove, false );
266 267 268 269
				document.addEventListener( 'keypress', onDocumentKeyPress, false );
				document.addEventListener( 'keydown', onDocumentKeyDown, false );

				document.getElementById( "color" ).addEventListener( 'click', function() {
270

M
Mr.doob 已提交
271
					pointLight.color.setHSL( Math.random(), 1, 0.5 );
M
Mr.doob 已提交
272
					hex = decimalToHex( pointLight.color.getHex() );
273

274
					updatePermalink();
275

276 277 278
				}, false );

				document.getElementById( "font" ).addEventListener( 'click', function() {
279

280 281 282 283 284
					if ( font == "helvetiker" ) {

						font = "optimer";

					} else if ( font == "optimer" ) {
285

286
						font = "gentilis";
287

288
					} else if ( font == "gentilis" ) {
289

290
						font = "droid sans";
291

292
					} else if ( font == "droid sans" ) {
293

294 295 296
						font = "droid serif";

					} else {
297

298
						font = "helvetiker";
299

300
					}
301

302
					refreshText();
303

304 305 306
				}, false );

				document.getElementById( "weight" ).addEventListener( 'click', function() {
307

308 309 310 311 312
					if ( weight == "bold" ) {

						weight = "normal";

					} else {
313

314
						weight = "bold";
315

316
					}
317

318
					refreshText();
319

320 321
				}, false );

322 323 324 325
				document.getElementById( "bevel" ).addEventListener( 'click', function() {

					bevelEnabled = !bevelEnabled;

326
					refreshText();
327

328 329
				}, false );

330
				document.getElementById( "postprocessing" ).addEventListener( 'click', function() {
331

332 333
					postprocessing.enabled = !postprocessing.enabled;
					updatePermalink();
334

335 336
				}, false );

337 338 339

				// POSTPROCESSING

340 341
				renderer.autoClear = false;

342 343 344
				var renderModel = new THREE.RenderPass( scene, camera );
				var effectBloom = new THREE.BloomPass( 0.25 );
				var effectFilm = new THREE.FilmPass( 0.5, 0.125, 2048, false );
345

346
				effectFXAA = new THREE.ShaderPass( THREE.FXAAShader );
347 348 349 350 351

				var width = window.innerWidth || 2;
				var height = window.innerHeight || 2;

				effectFXAA.uniforms[ 'resolution' ].value.set( 1 / width, 1 / height );
352 353 354 355 356 357

				effectFilm.renderToScreen = true;

				composer = new THREE.EffectComposer( renderer );

				composer.addPass( renderModel );
358
				composer.addPass( effectFXAA );
359 360 361
				composer.addPass( effectBloom );
				composer.addPass( effectFilm );

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
				//

				window.addEventListener( 'resize', onWindowResize, false );

			}

			function onWindowResize() {

				windowHalfX = window.innerWidth / 2;
				windowHalfY = window.innerHeight / 2;

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

				composer.reset();

				effectFXAA.uniforms[ 'resolution' ].value.set( 1 / window.innerWidth, 1 / window.innerHeight );

382
			}
383

384
			//
385

386
			function boolToNum( b ) {
387

388 389 390
				return b ? 1 : 0;

			}
391

392
			function updatePermalink() {
393

394
				var link = hex + fontMap[ font ] + weightMap[ weight ] + boolToNum( postprocessing.enabled ) + boolToNum( bevelEnabled ) + "#" + encodeURI( text );
395

396 397 398 399 400 401 402 403
				permalink.href = "#" + link;
				window.location.hash = link;

			}

			function onDocumentKeyDown( event ) {

				if ( firstLetter ) {
404

405 406 407
					firstLetter = false;
					text = "";

Z
zz85 已提交
408
				}
409

410
				var keyCode = event.keyCode;
411

412 413 414
				// backspace

				if ( keyCode == 8 ) {
Z
zz85 已提交
415

416
					event.preventDefault();
Z
zz85 已提交
417

418 419 420 421
					text = text.substring( 0, text.length - 1 );
					refreshText();

					return false;
422

423 424 425
				}

			}
426

427
			function onDocumentKeyPress( event ) {
428

429
				var keyCode = event.which;
430

431 432 433
				// backspace

				if ( keyCode == 8 ) {
434

435
					event.preventDefault();
436

437
				} else {
438

439 440 441 442 443 444 445
					var ch = String.fromCharCode( keyCode );
					text += ch;

					refreshText();

				}

Z
zz85 已提交
446
			}
447

448
			function createText() {
449

450
				textGeo = new THREE.TextGeometry( text, {
451

452
					size: size,
453 454
					height: height,
					curveSegments: curveSegments,
Z
zz85 已提交
455

456 457
					font: font,
					weight: weight,
458 459
					style: style,

460 461
					bevelThickness: bevelThickness,
					bevelSize: bevelSize,
Z
zz85 已提交
462
					bevelEnabled: bevelEnabled,
463

464 465
					material: 0,
					extrudeMaterial: 1
466 467 468

				});

469
				textGeo.computeBoundingBox();
470 471
				textGeo.computeVertexNormals();

472 473 474 475 476
				// "fix" side normals by removing z-component of normals for side faces
				// (this doesn't work well for beveled geometry as then we lose nice curvature around z-axis)

				if ( ! bevelEnabled ) {

477 478
					var triangleAreaHeuristics = 0.1 * ( height * size );

479 480 481 482
					for ( var i = 0; i < textGeo.faces.length; i ++ ) {

						var face = textGeo.faces[ i ];

483
						if ( face.materialIndex == 1 ) {
484 485 486 487 488 489 490 491

							for ( var j = 0; j < face.vertexNormals.length; j ++ ) {

								face.vertexNormals[ j ].z = 0;
								face.vertexNormals[ j ].normalize();

							}

492 493 494
							var va = textGeo.vertices[ face.a ];
							var vb = textGeo.vertices[ face.b ];
							var vc = textGeo.vertices[ face.c ];
495 496 497 498 499 500

							var s = THREE.GeometryUtils.triangleArea( va, vb, vc );

							if ( s > triangleAreaHeuristics ) {

								for ( var j = 0; j < face.vertexNormals.length; j ++ ) {
501

502 503 504 505 506 507 508
									face.vertexNormals[ j ].copy( face.normal );

								}

							}

						}
509 510 511 512 513

					}

				}

514
				var centerOffset = -0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
515

516
				textMesh1 = new THREE.Mesh( textGeo, material );
517

518
				textMesh1.position.x = centerOffset;
519 520
				textMesh1.position.y = hover;
				textMesh1.position.z = 0;
521

522 523
				textMesh1.rotation.x = 0;
				textMesh1.rotation.y = Math.PI * 2;
524

M
Mr.doob 已提交
525
				group.add( textMesh1 );
526 527

				if ( mirror ) {
Z
zz85 已提交
528

529
					textMesh2 = new THREE.Mesh( textGeo, material );
530

531
					textMesh2.position.x = centerOffset;
532 533 534 535 536
					textMesh2.position.y = -hover;
					textMesh2.position.z = height;

					textMesh2.rotation.x = Math.PI;
					textMesh2.rotation.y = Math.PI * 2;
537

M
Mr.doob 已提交
538
					group.add( textMesh2 );
539

540 541 542
				}

			}
543

544 545 546 547
			function refreshText() {

				updatePermalink();

M
Mr.doob 已提交
548 549
				group.remove( textMesh1 );
				if ( mirror ) group.remove( textMesh2 );
550 551 552 553 554 555 556

				if ( !text ) return;

				createText();

			}

Z
zz85 已提交
557 558 559 560 561 562 563 564 565 566
			function onDocumentMouseDown( event ) {

				event.preventDefault();

				document.addEventListener( 'mousemove', onDocumentMouseMove, false );
				document.addEventListener( 'mouseup', onDocumentMouseUp, false );
				document.addEventListener( 'mouseout', onDocumentMouseOut, false );

				mouseXOnMouseDown = event.clientX - windowHalfX;
				targetRotationOnMouseDown = targetRotation;
567

Z
zz85 已提交
568 569 570 571 572 573 574
			}

			function onDocumentMouseMove( event ) {

				mouseX = event.clientX - windowHalfX;

				targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
575

Z
zz85 已提交
576 577 578 579 580 581 582
			}

			function onDocumentMouseUp( event ) {

				document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
				document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
				document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
583

Z
zz85 已提交
584 585 586 587 588 589 590
			}

			function onDocumentMouseOut( event ) {

				document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
				document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
				document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
591

Z
zz85 已提交
592 593 594 595 596 597 598 599 600 601 602 603
			}

			function onDocumentTouchStart( event ) {

				if ( event.touches.length == 1 ) {

					event.preventDefault();

					mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
					targetRotationOnMouseDown = targetRotation;

				}
604

Z
zz85 已提交
605 606 607 608 609 610 611 612 613 614 615 616
			}

			function onDocumentTouchMove( event ) {

				if ( event.touches.length == 1 ) {

					event.preventDefault();

					mouseX = event.touches[ 0 ].pageX - windowHalfX;
					targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;

				}
617 618 619 620

			}

			//
621

Z
zz85 已提交
622 623 624 625 626 627 628 629 630 631 632
			function animate() {

				requestAnimationFrame( animate );

				render();
				stats.update();

			}

			function render() {

M
Mr.doob 已提交
633
				group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
634

635 636
				camera.lookAt( cameraTarget );

637
				renderer.clear();
638

639
				if ( postprocessing.enabled ) {
640

641
					composer.render( 0.05 );
642 643 644 645 646

				} else {

					renderer.render( scene, camera );

647
				}
648

Z
zz85 已提交
649 650 651 652 653
			}

		</script>

	</body>
654
</html>