webgl_geometry_text.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. let container, stats, permalink, hex;
  24. let camera, cameraTarget, scene, renderer;
  25. let group, textMesh1, textMesh2, textGeo, materials;
  26. let firstLetter = true;
  27. let text = "three.js",
  28. bevelEnabled = true,
  29. font = undefined,
  30. fontName = "optimer", // helvetiker, optimer, gentilis, droid sans, droid serif
  31. fontWeight = "bold"; // normal bold
  32. const height = 20,
  33. size = 70,
  34. hover = 30,
  35. curveSegments = 4,
  36. bevelThickness = 2,
  37. bevelSize = 1.5;
  38. const mirror = true;
  39. const fontMap = {
  40. "helvetiker": 0,
  41. "optimer": 1,
  42. "gentilis": 2,
  43. "droid/droid_sans": 3,
  44. "droid/droid_serif": 4
  45. };
  46. const weightMap = {
  47. "regular": 0,
  48. "bold": 1
  49. };
  50. const reverseFontMap = [];
  51. const reverseWeightMap = [];
  52. for ( const i in fontMap ) reverseFontMap[ fontMap[ i ] ] = i;
  53. for ( const i in weightMap ) reverseWeightMap[ weightMap[ i ] ] = i;
  54. let targetRotation = 0;
  55. let targetRotationOnPointerDown = 0;
  56. let pointerX = 0;
  57. let pointerXOnPointerDown = 0;
  58. let windowHalfX = window.innerWidth / 2;
  59. let fontIndex = 1;
  60. init();
  61. animate();
  62. function decimalToHex( d ) {
  63. let 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. const dirLight = new THREE.DirectionalLight( 0xffffff, 0.125 );
  81. dirLight.position.set( 0, 0, 1 ).normalize();
  82. scene.add( dirLight );
  83. const pointLight = new THREE.PointLight( 0xffffff, 1.5 );
  84. pointLight.position.set( 0, 100, 90 );
  85. scene.add( pointLight );
  86. // Get text from hash
  87. const hash = document.location.hash.substr( 1 );
  88. if ( hash.length !== 0 ) {
  89. const colorhash = hash.substring( 0, 6 );
  90. const fonthash = hash.substring( 6, 7 );
  91. const weighthash = hash.substring( 7, 8 );
  92. const bevelhash = hash.substring( 8, 9 );
  93. const 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. const plane = new THREE.Mesh(
  114. new THREE.PlaneGeometry( 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. container.style.touchAction = 'none';
  130. container.addEventListener( 'pointerdown', onPointerDown );
  131. document.addEventListener( 'keypress', onDocumentKeyPress );
  132. document.addEventListener( 'keydown', onDocumentKeyDown );
  133. document.getElementById( "color" ).addEventListener( 'click', function () {
  134. pointLight.color.setHSL( Math.random(), 1, 0.5 );
  135. hex = decimalToHex( pointLight.color.getHex() );
  136. updatePermalink();
  137. } );
  138. document.getElementById( "font" ).addEventListener( 'click', function () {
  139. fontIndex ++;
  140. fontName = reverseFontMap[ fontIndex % reverseFontMap.length ];
  141. loadFont();
  142. } );
  143. document.getElementById( "weight" ).addEventListener( 'click', function () {
  144. if ( fontWeight === "bold" ) {
  145. fontWeight = "regular";
  146. } else {
  147. fontWeight = "bold";
  148. }
  149. loadFont();
  150. } );
  151. document.getElementById( "bevel" ).addEventListener( 'click', function () {
  152. bevelEnabled = ! bevelEnabled;
  153. refreshText();
  154. } );
  155. //
  156. window.addEventListener( 'resize', onWindowResize );
  157. }
  158. function onWindowResize() {
  159. windowHalfX = window.innerWidth / 2;
  160. camera.aspect = window.innerWidth / window.innerHeight;
  161. camera.updateProjectionMatrix();
  162. renderer.setSize( window.innerWidth, window.innerHeight );
  163. }
  164. //
  165. function boolToNum( b ) {
  166. return b ? 1 : 0;
  167. }
  168. function updatePermalink() {
  169. const link = hex + fontMap[ fontName ] + weightMap[ fontWeight ] + boolToNum( bevelEnabled ) + "#" + encodeURI( text );
  170. permalink.href = "#" + link;
  171. window.location.hash = link;
  172. }
  173. function onDocumentKeyDown( event ) {
  174. if ( firstLetter ) {
  175. firstLetter = false;
  176. text = "";
  177. }
  178. const keyCode = event.keyCode;
  179. // backspace
  180. if ( keyCode == 8 ) {
  181. event.preventDefault();
  182. text = text.substring( 0, text.length - 1 );
  183. refreshText();
  184. return false;
  185. }
  186. }
  187. function onDocumentKeyPress( event ) {
  188. const keyCode = event.which;
  189. // backspace
  190. if ( keyCode == 8 ) {
  191. event.preventDefault();
  192. } else {
  193. const ch = String.fromCharCode( keyCode );
  194. text += ch;
  195. refreshText();
  196. }
  197. }
  198. function loadFont() {
  199. const loader = new THREE.FontLoader();
  200. loader.load( 'fonts/' + fontName + '_' + fontWeight + '.typeface.json', function ( response ) {
  201. font = response;
  202. refreshText();
  203. } );
  204. }
  205. function createText() {
  206. textGeo = new THREE.TextGeometry( text, {
  207. font: font,
  208. size: size,
  209. height: height,
  210. curveSegments: curveSegments,
  211. bevelThickness: bevelThickness,
  212. bevelSize: bevelSize,
  213. bevelEnabled: bevelEnabled
  214. } );
  215. textGeo.computeBoundingBox();
  216. const centerOffset = - 0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
  217. textMesh1 = new THREE.Mesh( textGeo, materials );
  218. textMesh1.position.x = centerOffset;
  219. textMesh1.position.y = hover;
  220. textMesh1.position.z = 0;
  221. textMesh1.rotation.x = 0;
  222. textMesh1.rotation.y = Math.PI * 2;
  223. group.add( textMesh1 );
  224. if ( mirror ) {
  225. textMesh2 = new THREE.Mesh( textGeo, materials );
  226. textMesh2.position.x = centerOffset;
  227. textMesh2.position.y = - hover;
  228. textMesh2.position.z = height;
  229. textMesh2.rotation.x = Math.PI;
  230. textMesh2.rotation.y = Math.PI * 2;
  231. group.add( textMesh2 );
  232. }
  233. }
  234. function refreshText() {
  235. updatePermalink();
  236. group.remove( textMesh1 );
  237. if ( mirror ) group.remove( textMesh2 );
  238. if ( ! text ) return;
  239. createText();
  240. }
  241. function onPointerDown( event ) {
  242. if ( event.isPrimary === false ) return;
  243. pointerXOnPointerDown = event.clientX - windowHalfX;
  244. targetRotationOnPointerDown = targetRotation;
  245. document.addEventListener( 'pointermove', onPointerMove );
  246. document.addEventListener( 'pointerup', onPointerUp );
  247. }
  248. function onPointerMove( event ) {
  249. if ( event.isPrimary === false ) return;
  250. pointerX = event.clientX - windowHalfX;
  251. targetRotation = targetRotationOnPointerDown + ( pointerX - pointerXOnPointerDown ) * 0.02;
  252. }
  253. function onPointerUp() {
  254. if ( event.isPrimary === false ) return;
  255. document.removeEventListener( 'pointermove', onPointerMove );
  256. document.removeEventListener( 'pointerup', onPointerUp );
  257. }
  258. //
  259. function animate() {
  260. requestAnimationFrame( animate );
  261. render();
  262. stats.update();
  263. }
  264. function render() {
  265. group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
  266. camera.lookAt( cameraTarget );
  267. renderer.clear();
  268. renderer.render( scene, camera );
  269. }
  270. </script>
  271. </body>
  272. </html>