webgl_geometry_text.html 11 KB

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