4 <title>three.js webgl - geometry - text</title>
6 <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
9 font-family: Monospace;
10 background-color: #000;
23 #info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
29 <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - procedural 3D text by <a href="http://www.lab4games.net/zz85/blog" target="_blank" rel="noopener">zz85</a> & alteredq
30 <br/>built-in shape triangulation has been replaced with <a href="https://github.com/jahting/pnltri.js">PnlTri.js</a> by <a href="https://github.com/jahting" target="_blank" rel="noopener">j ahting</a>
31 <br/>type to enter new text, drag to spin the text
32 <br/><span class="button" id="color">change color</span>,
33 <span class="button" id="font">change font</span>,
34 <span class="button" id="weight">change weight</span>,
35 <span class="button" id="bevel">change bevel</span>
36 <a id="permalink" href="#">permalink</a>
40 <script src="../build/three.js"></script>
41 <script src="js/utils/GeometryUtils.js"></script>
43 <script src="js/Detector.js"></script>
44 <script src="js/libs/stats.min.js"></script>
47 <!-- replace built-in triangulation with PnlTri.js -->
48 <script src="js/libs/pnltri.min.js"></script>
50 THREE.ShapeUtils.triangulateShape = ( function () {
51 var pnlTriangulator = new PNLTRI.Triangulator();
52 function removeDupEndPts(points) {
54 var l = points.length;
56 if ( l > 2 && points[ l - 1 ].equals( points[ 0 ] ) ) {
64 return function triangulateShape( contour, holes ) {
65 // console.log("new Triangulation: PnlTri.js " + PNLTRI.REVISION );
67 removeDupEndPts( contour );
68 holes.forEach( removeDupEndPts );
70 return pnlTriangulator.triangulate_polygon( [ contour ].concat(holes) );
77 if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
79 THREE.Cache.enabled = true;
81 var container, stats, permalink, hex, color;
83 var camera, cameraTarget, scene, renderer;
85 var group, textMesh1, textMesh2, textGeo, materials;
87 var firstLetter = true;
104 fontName = "optimer", // helvetiker, optimer, gentilis, droid sans, droid serif
105 fontWeight = "bold"; // normal bold
114 "droid/droid_sans": 3,
115 "droid/droid_serif": 4
126 var reverseFontMap = [];
127 var reverseWeightMap = [];
129 for ( var i in fontMap ) reverseFontMap[ fontMap[i] ] = i;
130 for ( var i in weightMap ) reverseWeightMap[ weightMap[i] ] = i;
132 var targetRotation = 0;
133 var targetRotationOnMouseDown = 0;
136 var mouseXOnMouseDown = 0;
138 var windowHalfX = window.innerWidth / 2;
139 var windowHalfY = window.innerHeight / 2;
146 function decimalToHex( d ) {
148 var hex = Number( d ).toString( 16 );
149 hex = "000000".substr( 0, 6 - hex.length ) + hex;
150 return hex.toUpperCase();
156 container = document.createElement( 'div' );
157 document.body.appendChild( container );
159 permalink = document.getElementById( "permalink" );
163 camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 1500 );
164 camera.position.set( 0, 400, 700 );
166 cameraTarget = new THREE.Vector3( 0, 150, 0 );
170 scene = new THREE.Scene();
171 scene.background = new THREE.Color( 0x000000 );
172 scene.fog = new THREE.Fog( 0x000000, 250, 1400 );
176 var dirLight = new THREE.DirectionalLight( 0xffffff, 0.125 );
177 dirLight.position.set( 0, 0, 1 ).normalize();
178 scene.add( dirLight );
180 var pointLight = new THREE.PointLight( 0xffffff, 1.5 );
181 pointLight.position.set( 0, 100, 90 );
182 scene.add( pointLight );
184 // Get text from hash
186 var hash = document.location.hash.substr( 1 );
188 if ( hash.length !== 0 ) {
190 var colorhash = hash.substring( 0, 6 );
191 var fonthash = hash.substring( 6, 7 );
192 var weighthash = hash.substring( 7, 8 );
193 var bevelhash = hash.substring( 8, 9 );
194 var texthash = hash.substring( 10 );
197 pointLight.color.setHex( parseInt( colorhash, 16 ) );
199 fontName = reverseFontMap[ parseInt( fonthash ) ];
200 fontWeight = reverseWeightMap[ parseInt( weighthash ) ];
202 bevelEnabled = parseInt( bevelhash );
204 text = decodeURI( texthash );
210 pointLight.color.setHSL( Math.random(), 1, 0.5 );
211 hex = decimalToHex( pointLight.color.getHex() );
216 new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } ), // front
217 new THREE.MeshPhongMaterial( { color: 0xffffff } ) // side
220 group = new THREE.Group();
221 group.position.y = 100;
227 var plane = new THREE.Mesh(
228 new THREE.PlaneBufferGeometry( 10000, 10000 ),
229 new THREE.MeshBasicMaterial( { color: 0xffffff, opacity: 0.5, transparent: true } )
231 plane.position.y = 100;
232 plane.rotation.x = - Math.PI / 2;
237 renderer = new THREE.WebGLRenderer( { antialias: true } );
238 renderer.setPixelRatio( window.devicePixelRatio );
239 renderer.setSize( window.innerWidth, window.innerHeight );
240 container.appendChild( renderer.domElement );
245 //container.appendChild( stats.dom );
249 document.addEventListener( 'mousedown', onDocumentMouseDown, false );
250 document.addEventListener( 'touchstart', onDocumentTouchStart, false );
251 document.addEventListener( 'touchmove', onDocumentTouchMove, false );
252 document.addEventListener( 'keypress', onDocumentKeyPress, false );
253 document.addEventListener( 'keydown', onDocumentKeyDown, false );
255 document.getElementById( "color" ).addEventListener( 'click', function() {
257 pointLight.color.setHSL( Math.random(), 1, 0.5 );
258 hex = decimalToHex( pointLight.color.getHex() );
264 document.getElementById( "font" ).addEventListener( 'click', function() {
268 fontName = reverseFontMap[ fontIndex % reverseFontMap.length ];
275 document.getElementById( "weight" ).addEventListener( 'click', function() {
277 if ( fontWeight === "bold" ) {
279 fontWeight = "regular";
291 document.getElementById( "bevel" ).addEventListener( 'click', function() {
293 bevelEnabled = !bevelEnabled;
301 window.addEventListener( 'resize', onWindowResize, false );
305 function onWindowResize() {
307 windowHalfX = window.innerWidth / 2;
308 windowHalfY = window.innerHeight / 2;
310 camera.aspect = window.innerWidth / window.innerHeight;
311 camera.updateProjectionMatrix();
313 renderer.setSize( window.innerWidth, window.innerHeight );
319 function boolToNum( b ) {
325 function updatePermalink() {
327 var link = hex + fontMap[ fontName ] + weightMap[ fontWeight ] + boolToNum( bevelEnabled ) + "#" + encodeURI( text );
329 permalink.href = "#" + link;
330 window.location.hash = link;
334 function onDocumentKeyDown( event ) {
343 var keyCode = event.keyCode;
347 if ( keyCode == 8 ) {
349 event.preventDefault();
351 text = text.substring( 0, text.length - 1 );
360 function onDocumentKeyPress( event ) {
362 var keyCode = event.which;
366 if ( keyCode == 8 ) {
368 event.preventDefault();
372 var ch = String.fromCharCode( keyCode );
381 function loadFont() {
383 var loader = new THREE.FontLoader();
384 loader.load( 'fonts/' + fontName + '_' + fontWeight + '.typeface.json', function ( response ) {
394 function createText() {
396 textGeo = new THREE.TextBufferGeometry( text, {
402 curveSegments: curveSegments,
404 bevelThickness: bevelThickness,
405 bevelSize: bevelSize,
406 bevelEnabled: bevelEnabled,
413 textGeo.computeBoundingBox();
414 textGeo.computeVertexNormals();
416 // "fix" side normals by removing z-component of normals for side faces
417 // (this doesn't work well for beveled geometry as then we lose nice curvature around z-axis)
419 if ( ! bevelEnabled ) {
421 var triangleAreaHeuristics = 0.1 * ( height * size );
423 for ( var i = 0; i < textGeo.faces.length; i ++ ) {
425 var face = textGeo.faces[ i ];
427 if ( face.materialIndex == 1 ) {
429 for ( var j = 0; j < face.vertexNormals.length; j ++ ) {
431 face.vertexNormals[ j ].z = 0;
432 face.vertexNormals[ j ].normalize();
436 var va = textGeo.vertices[ face.a ];
437 var vb = textGeo.vertices[ face.b ];
438 var vc = textGeo.vertices[ face.c ];
440 var s = THREE.GeometryUtils.triangleArea( va, vb, vc );
442 if ( s > triangleAreaHeuristics ) {
444 for ( var j = 0; j < face.vertexNormals.length; j ++ ) {
446 face.vertexNormals[ j ].copy( face.normal );
458 var centerOffset = -0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
460 textMesh1 = new THREE.Mesh( textGeo, materials );
462 textMesh1.position.x = centerOffset;
463 textMesh1.position.y = hover;
464 textMesh1.position.z = 0;
466 textMesh1.rotation.x = 0;
467 textMesh1.rotation.y = Math.PI * 2;
469 group.add( textMesh1 );
473 textMesh2 = new THREE.Mesh( textGeo, materials );
475 textMesh2.position.x = centerOffset;
476 textMesh2.position.y = -hover;
477 textMesh2.position.z = height;
479 textMesh2.rotation.x = Math.PI;
480 textMesh2.rotation.y = Math.PI * 2;
482 group.add( textMesh2 );
488 function refreshText() {
492 group.remove( textMesh1 );
493 if ( mirror ) group.remove( textMesh2 );
501 function onDocumentMouseDown( event ) {
503 event.preventDefault();
505 document.addEventListener( 'mousemove', onDocumentMouseMove, false );
506 document.addEventListener( 'mouseup', onDocumentMouseUp, false );
507 document.addEventListener( 'mouseout', onDocumentMouseOut, false );
509 mouseXOnMouseDown = event.clientX - windowHalfX;
510 targetRotationOnMouseDown = targetRotation;
514 function onDocumentMouseMove( event ) {
516 mouseX = event.clientX - windowHalfX;
518 targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
522 function onDocumentMouseUp( event ) {
524 document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
525 document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
526 document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
530 function onDocumentMouseOut( event ) {
532 document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
533 document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
534 document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
538 function onDocumentTouchStart( event ) {
540 if ( event.touches.length == 1 ) {
542 event.preventDefault();
544 mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
545 targetRotationOnMouseDown = targetRotation;
551 function onDocumentTouchMove( event ) {
553 if ( event.touches.length == 1 ) {
555 event.preventDefault();
557 mouseX = event.touches[ 0 ].pageX - windowHalfX;
558 targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
568 requestAnimationFrame( animate );
577 group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
579 camera.lookAt( cameraTarget );
582 renderer.render( scene, camera );