webgl_geometry_text.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - text</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. position: absolute;
  17. top: 10px;
  18. width: 100%;
  19. text-align: center;
  20. z-index: 100;
  21. display:block;
  22. }
  23. #info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="info">
  28. <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
  29. <br/>type to enter new text, drag to spin the text
  30. <br/><span class="button" id="color">change color</span>,
  31. <span class="button" id="font">change font</span>,
  32. <span class="button" id="weight">change weight</span>,
  33. <span class="button" id="bevel">change bevel</span>
  34. <a id="permalink" href="#">permalink</a>
  35. </div>
  36. <script src="../build/three.js"></script>
  37. <script src="js/utils/GeometryUtils.js"></script>
  38. <script src="js/Detector.js"></script>
  39. <script src="js/libs/stats.min.js"></script>
  40. <script>
  41. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  42. THREE.Cache.enabled = true;
  43. var container, stats, permalink, hex, color;
  44. var camera, cameraTarget, scene, renderer;
  45. var group, textMesh1, textMesh2, textGeo, material;
  46. var firstLetter = true;
  47. var text = "three.js",
  48. height = 20,
  49. size = 70,
  50. hover = 30,
  51. curveSegments = 4,
  52. bevelThickness = 2,
  53. bevelSize = 1.5,
  54. bevelSegments = 3,
  55. bevelEnabled = true,
  56. font = undefined,
  57. fontName = "optimer", // helvetiker, optimer, gentilis, droid sans, droid serif
  58. fontWeight = "bold"; // normal bold
  59. var mirror = true;
  60. var fontMap = {
  61. "helvetiker": 0,
  62. "optimer": 1,
  63. "gentilis": 2,
  64. "droid/droid_sans": 3,
  65. "droid/droid_serif": 4
  66. };
  67. var weightMap = {
  68. "regular": 0,
  69. "bold": 1
  70. };
  71. var reverseFontMap = [];
  72. var reverseWeightMap = [];
  73. for ( var i in fontMap ) reverseFontMap[ fontMap[i] ] = i;
  74. for ( var i in weightMap ) reverseWeightMap[ weightMap[i] ] = i;
  75. var targetRotation = 0;
  76. var targetRotationOnMouseDown = 0;
  77. var mouseX = 0;
  78. var mouseXOnMouseDown = 0;
  79. var windowHalfX = window.innerWidth / 2;
  80. var windowHalfY = window.innerHeight / 2;
  81. var fontIndex = 1;
  82. init();
  83. animate();
  84. function decimalToHex( d ) {
  85. var hex = Number( d ).toString( 16 );
  86. hex = "000000".substr( 0, 6 - hex.length ) + hex;
  87. return hex.toUpperCase();
  88. }
  89. function init() {
  90. container = document.createElement( 'div' );
  91. document.body.appendChild( container );
  92. permalink = document.getElementById( "permalink" );
  93. // CAMERA
  94. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 1500 );
  95. camera.position.set( 0, 400, 700 );
  96. cameraTarget = new THREE.Vector3( 0, 150, 0 );
  97. // SCENE
  98. scene = new THREE.Scene();
  99. scene.fog = new THREE.Fog( 0x000000, 250, 1400 );
  100. // LIGHTS
  101. var dirLight = new THREE.DirectionalLight( 0xffffff, 0.125 );
  102. dirLight.position.set( 0, 0, 1 ).normalize();
  103. scene.add( dirLight );
  104. var pointLight = new THREE.PointLight( 0xffffff, 1.5 );
  105. pointLight.position.set( 0, 100, 90 );
  106. scene.add( pointLight );
  107. // Get text from hash
  108. var hash = document.location.hash.substr( 1 );
  109. if ( hash.length !== 0 ) {
  110. var colorhash = hash.substring( 0, 6 );
  111. var fonthash = hash.substring( 6, 7 );
  112. var weighthash = hash.substring( 7, 8 );
  113. var bevelhash = hash.substring( 8, 9 );
  114. var texthash = hash.substring( 10 );
  115. hex = colorhash;
  116. pointLight.color.setHex( parseInt( colorhash, 16 ) );
  117. fontName = reverseFontMap[ parseInt( fonthash ) ];
  118. fontWeight = reverseWeightMap[ parseInt( weighthash ) ];
  119. bevelEnabled = parseInt( bevelhash );
  120. text = decodeURI( texthash );
  121. updatePermalink();
  122. } else {
  123. pointLight.color.setHSL( Math.random(), 1, 0.5 );
  124. hex = decimalToHex( pointLight.color.getHex() );
  125. }
  126. material = new THREE.MultiMaterial( [
  127. new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading } ), // front
  128. new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.SmoothShading } ) // side
  129. ] );
  130. group = new THREE.Group();
  131. group.position.y = 100;
  132. scene.add( group );
  133. loadFont();
  134. var plane = new THREE.Mesh(
  135. new THREE.PlaneBufferGeometry( 10000, 10000 ),
  136. new THREE.MeshBasicMaterial( { color: 0xffffff, opacity: 0.5, transparent: true } )
  137. );
  138. plane.position.y = 100;
  139. plane.rotation.x = - Math.PI / 2;
  140. scene.add( plane );
  141. // RENDERER
  142. renderer = new THREE.WebGLRenderer( { antialias: true } );
  143. renderer.setClearColor( scene.fog.color );
  144. renderer.setPixelRatio( window.devicePixelRatio );
  145. renderer.setSize( window.innerWidth, window.innerHeight );
  146. container.appendChild( renderer.domElement );
  147. // STATS
  148. stats = new Stats();
  149. //container.appendChild( stats.dom );
  150. // EVENTS
  151. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  152. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  153. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  154. document.addEventListener( 'keypress', onDocumentKeyPress, false );
  155. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  156. document.getElementById( "color" ).addEventListener( 'click', function() {
  157. pointLight.color.setHSL( Math.random(), 1, 0.5 );
  158. hex = decimalToHex( pointLight.color.getHex() );
  159. updatePermalink();
  160. }, false );
  161. document.getElementById( "font" ).addEventListener( 'click', function() {
  162. fontIndex ++;
  163. fontName = reverseFontMap[ fontIndex % reverseFontMap.length ];
  164. loadFont();
  165. }, false );
  166. document.getElementById( "weight" ).addEventListener( 'click', function() {
  167. if ( fontWeight === "bold" ) {
  168. fontWeight = "regular";
  169. } else {
  170. fontWeight = "bold";
  171. }
  172. loadFont();
  173. }, false );
  174. document.getElementById( "bevel" ).addEventListener( 'click', function() {
  175. bevelEnabled = !bevelEnabled;
  176. refreshText();
  177. }, false );
  178. //
  179. window.addEventListener( 'resize', onWindowResize, false );
  180. }
  181. function onWindowResize() {
  182. windowHalfX = window.innerWidth / 2;
  183. windowHalfY = window.innerHeight / 2;
  184. camera.aspect = window.innerWidth / window.innerHeight;
  185. camera.updateProjectionMatrix();
  186. renderer.setSize( window.innerWidth, window.innerHeight );
  187. }
  188. //
  189. function boolToNum( b ) {
  190. return b ? 1 : 0;
  191. }
  192. function updatePermalink() {
  193. var link = hex + fontMap[ fontName ] + weightMap[ fontWeight ] + boolToNum( bevelEnabled ) + "#" + encodeURI( text );
  194. permalink.href = "#" + link;
  195. window.location.hash = link;
  196. }
  197. function onDocumentKeyDown( event ) {
  198. if ( firstLetter ) {
  199. firstLetter = false;
  200. text = "";
  201. }
  202. var keyCode = event.keyCode;
  203. // backspace
  204. if ( keyCode == 8 ) {
  205. event.preventDefault();
  206. text = text.substring( 0, text.length - 1 );
  207. refreshText();
  208. return false;
  209. }
  210. }
  211. function onDocumentKeyPress( event ) {
  212. var keyCode = event.which;
  213. // backspace
  214. if ( keyCode == 8 ) {
  215. event.preventDefault();
  216. } else {
  217. var ch = String.fromCharCode( keyCode );
  218. text += ch;
  219. refreshText();
  220. }
  221. }
  222. function loadFont() {
  223. var loader = new THREE.FontLoader();
  224. loader.load( 'fonts/' + fontName + '_' + fontWeight + '.typeface.json', function ( response ) {
  225. font = response;
  226. refreshText();
  227. } );
  228. }
  229. function createText() {
  230. textGeo = new THREE.TextGeometry( text, {
  231. font: font,
  232. size: size,
  233. height: height,
  234. curveSegments: curveSegments,
  235. bevelThickness: bevelThickness,
  236. bevelSize: bevelSize,
  237. bevelEnabled: bevelEnabled,
  238. material: 0,
  239. extrudeMaterial: 1
  240. });
  241. textGeo.computeBoundingBox();
  242. textGeo.computeVertexNormals();
  243. // "fix" side normals by removing z-component of normals for side faces
  244. // (this doesn't work well for beveled geometry as then we lose nice curvature around z-axis)
  245. if ( ! bevelEnabled ) {
  246. var triangleAreaHeuristics = 0.1 * ( height * size );
  247. for ( var i = 0; i < textGeo.faces.length; i ++ ) {
  248. var face = textGeo.faces[ i ];
  249. if ( face.materialIndex == 1 ) {
  250. for ( var j = 0; j < face.vertexNormals.length; j ++ ) {
  251. face.vertexNormals[ j ].z = 0;
  252. face.vertexNormals[ j ].normalize();
  253. }
  254. var va = textGeo.vertices[ face.a ];
  255. var vb = textGeo.vertices[ face.b ];
  256. var vc = textGeo.vertices[ face.c ];
  257. var s = THREE.GeometryUtils.triangleArea( va, vb, vc );
  258. if ( s > triangleAreaHeuristics ) {
  259. for ( var j = 0; j < face.vertexNormals.length; j ++ ) {
  260. face.vertexNormals[ j ].copy( face.normal );
  261. }
  262. }
  263. }
  264. }
  265. }
  266. var centerOffset = -0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
  267. textMesh1 = new THREE.Mesh( textGeo, material );
  268. textMesh1.position.x = centerOffset;
  269. textMesh1.position.y = hover;
  270. textMesh1.position.z = 0;
  271. textMesh1.rotation.x = 0;
  272. textMesh1.rotation.y = Math.PI * 2;
  273. group.add( textMesh1 );
  274. if ( mirror ) {
  275. textMesh2 = new THREE.Mesh( textGeo, material );
  276. textMesh2.position.x = centerOffset;
  277. textMesh2.position.y = -hover;
  278. textMesh2.position.z = height;
  279. textMesh2.rotation.x = Math.PI;
  280. textMesh2.rotation.y = Math.PI * 2;
  281. group.add( textMesh2 );
  282. }
  283. }
  284. function refreshText() {
  285. updatePermalink();
  286. group.remove( textMesh1 );
  287. if ( mirror ) group.remove( textMesh2 );
  288. if ( !text ) return;
  289. createText();
  290. }
  291. function onDocumentMouseDown( event ) {
  292. event.preventDefault();
  293. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  294. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  295. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  296. mouseXOnMouseDown = event.clientX - windowHalfX;
  297. targetRotationOnMouseDown = targetRotation;
  298. }
  299. function onDocumentMouseMove( event ) {
  300. mouseX = event.clientX - windowHalfX;
  301. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  302. }
  303. function onDocumentMouseUp( event ) {
  304. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  305. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  306. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  307. }
  308. function onDocumentMouseOut( event ) {
  309. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  310. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  311. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  312. }
  313. function onDocumentTouchStart( event ) {
  314. if ( event.touches.length == 1 ) {
  315. event.preventDefault();
  316. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  317. targetRotationOnMouseDown = targetRotation;
  318. }
  319. }
  320. function onDocumentTouchMove( event ) {
  321. if ( event.touches.length == 1 ) {
  322. event.preventDefault();
  323. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  324. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  325. }
  326. }
  327. //
  328. function animate() {
  329. requestAnimationFrame( animate );
  330. render();
  331. stats.update();
  332. }
  333. function render() {
  334. group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
  335. camera.lookAt( cameraTarget );
  336. renderer.clear();
  337. renderer.render( scene, camera );
  338. }
  339. </script>
  340. </body>
  341. </html>