webgl_geometry_text.html 11 KB

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