webgl_geometry_text.html 14.9 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

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

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

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

236 237
				createText();

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

243 244
				// RENDERER

245
				renderer = new THREE.WebGLRenderer( { antialias: true } );
246
				renderer.setSize( window.innerWidth, window.innerHeight );
247

248
				renderer.setClearColor( scene.fog.color, 1 );
Z
zz85 已提交
249 250 251

				container.appendChild( renderer.domElement );

252 253
				// STATS

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

259 260
				// EVENTS

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

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

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

272
					updatePermalink();
273

274 275 276
				}, false );

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

278 279 280 281 282
					if ( font == "helvetiker" ) {

						font = "optimer";

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

284
						font = "gentilis";
285

286
					} else if ( font == "gentilis" ) {
287

288
						font = "droid sans";
289

290
					} else if ( font == "droid sans" ) {
291

292 293 294
						font = "droid serif";

					} else {
295

296
						font = "helvetiker";
297

298
					}
299

300
					refreshText();
301

302 303 304
				}, false );

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

306 307 308 309 310
					if ( weight == "bold" ) {

						weight = "normal";

					} else {
311

312
						weight = "bold";
313

314
					}
315

316
					refreshText();
317

318 319
				}, false );

320 321 322 323
				document.getElementById( "bevel" ).addEventListener( 'click', function() {

					bevelEnabled = !bevelEnabled;

324
					refreshText();
325

326 327
				}, false );

328
				document.getElementById( "postprocessing" ).addEventListener( 'click', function() {
329

330 331
					postprocessing.enabled = !postprocessing.enabled;
					updatePermalink();
332

333 334
				}, false );

335 336 337

				// POSTPROCESSING

338 339
				renderer.autoClear = false;

340 341 342
				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 );
343

344
				effectFXAA = new THREE.ShaderPass( THREE.FXAAShader );
345 346 347 348 349

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

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

				effectFilm.renderToScreen = true;

				composer = new THREE.EffectComposer( renderer );

				composer.addPass( renderModel );
356
				composer.addPass( effectFXAA );
357 358 359
				composer.addPass( effectBloom );
				composer.addPass( effectFilm );

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

				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 );

380
			}
381

382
			//
383

384
			function boolToNum( b ) {
385

386 387 388
				return b ? 1 : 0;

			}
389

390
			function updatePermalink() {
391

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

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

			}

			function onDocumentKeyDown( event ) {

				if ( firstLetter ) {
402

403 404 405
					firstLetter = false;
					text = "";

Z
zz85 已提交
406
				}
407

408
				var keyCode = event.keyCode;
409

410 411 412
				// backspace

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

414
					event.preventDefault();
Z
zz85 已提交
415

416 417 418 419
					text = text.substring( 0, text.length - 1 );
					refreshText();

					return false;
420

421 422 423
				}

			}
424

425
			function onDocumentKeyPress( event ) {
426

427
				var keyCode = event.which;
428

429 430 431
				// backspace

				if ( keyCode == 8 ) {
432

433
					event.preventDefault();
434

435
				} else {
436

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

					refreshText();

				}

Z
zz85 已提交
444
			}
445

446
			function createText() {
447

448
				textGeo = new THREE.TextGeometry( text, {
449

450
					size: size,
451 452
					height: height,
					curveSegments: curveSegments,
Z
zz85 已提交
453

454 455
					font: font,
					weight: weight,
456 457
					style: style,

458 459
					bevelThickness: bevelThickness,
					bevelSize: bevelSize,
Z
zz85 已提交
460
					bevelEnabled: bevelEnabled,
461

462 463
					material: 0,
					extrudeMaterial: 1
464 465 466

				});

467
				textGeo.computeBoundingBox();
468 469
				textGeo.computeVertexNormals();

470 471 472 473 474
				// "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 ) {

475 476
					var triangleAreaHeuristics = 0.1 * ( height * size );

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

						var face = textGeo.faces[ i ];

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

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

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

							}

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

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

							if ( s > triangleAreaHeuristics ) {

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

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

								}

							}

						}
507 508 509 510 511

					}

				}

512
				var centerOffset = -0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
513

514
				textMesh1 = new THREE.Mesh( textGeo, material );
515

516
				textMesh1.position.x = centerOffset;
517 518
				textMesh1.position.y = hover;
				textMesh1.position.z = 0;
519

520 521
				textMesh1.rotation.x = 0;
				textMesh1.rotation.y = Math.PI * 2;
522

M
Mr.doob 已提交
523
				group.add( textMesh1 );
524 525

				if ( mirror ) {
Z
zz85 已提交
526

527
					textMesh2 = new THREE.Mesh( textGeo, material );
528

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

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

M
Mr.doob 已提交
536
					group.add( textMesh2 );
537

538 539 540
				}

			}
541

542 543 544 545
			function refreshText() {

				updatePermalink();

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

				if ( !text ) return;

				createText();

			}

Z
zz85 已提交
555 556 557 558 559 560 561 562 563 564
			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;
565

Z
zz85 已提交
566 567 568 569 570 571 572
			}

			function onDocumentMouseMove( event ) {

				mouseX = event.clientX - windowHalfX;

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

Z
zz85 已提交
574 575 576 577 578 579 580
			}

			function onDocumentMouseUp( event ) {

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

Z
zz85 已提交
582 583 584 585 586 587 588
			}

			function onDocumentMouseOut( event ) {

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

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

			function onDocumentTouchStart( event ) {

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

					event.preventDefault();

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

				}
602

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

			function onDocumentTouchMove( event ) {

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

					event.preventDefault();

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

				}
615 616 617 618

			}

			//
619

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

				requestAnimationFrame( animate );

				render();
				stats.update();

			}

			function render() {

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

633 634
				camera.lookAt( cameraTarget );

635
				renderer.clear();
636

637
				if ( postprocessing.enabled ) {
638

639
					composer.render( 0.05 );
640 641 642 643 644

				} else {

					renderer.render( scene, camera );

645
				}
646

Z
zz85 已提交
647 648 649 650 651
			}

		</script>

	</body>
652
</html>