webgl_geometry_text.html 12 KB

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