webgl_geometry_text.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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<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. <!-- Import maps polyfill -->
  20. <!-- Remove this when import maps will be widely supported -->
  21. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  22. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.module.js"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three';
  31. import { FontLoader } from './jsm/loaders/FontLoader.js';
  32. import { TextGeometry } from './jsm/geometries/TextGeometry.js';
  33. import Stats from './jsm/libs/stats.module.js';
  34. THREE.Cache.enabled = true;
  35. let container, stats, permalink, hex;
  36. let camera, cameraTarget, scene, renderer;
  37. let group, textMesh1, textMesh2, textGeo, materials;
  38. let firstLetter = true;
  39. let text = 'three.js',
  40. bevelEnabled = true,
  41. font = undefined,
  42. fontName = 'optimer', // helvetiker, optimer, gentilis, droid sans, droid serif
  43. fontWeight = 'bold'; // normal bold
  44. const height = 20,
  45. size = 70,
  46. hover = 30,
  47. curveSegments = 4,
  48. bevelThickness = 2,
  49. bevelSize = 1.5;
  50. const mirror = true;
  51. const fontMap = {
  52. 'helvetiker': 0,
  53. 'optimer': 1,
  54. 'gentilis': 2,
  55. 'droid/droid_sans': 3,
  56. 'droid/droid_serif': 4
  57. };
  58. const weightMap = {
  59. 'regular': 0,
  60. 'bold': 1
  61. };
  62. const reverseFontMap = [];
  63. const reverseWeightMap = [];
  64. for ( const i in fontMap ) reverseFontMap[ fontMap[ i ] ] = i;
  65. for ( const i in weightMap ) reverseWeightMap[ weightMap[ i ] ] = i;
  66. let targetRotation = 0;
  67. let targetRotationOnPointerDown = 0;
  68. let pointerX = 0;
  69. let pointerXOnPointerDown = 0;
  70. let windowHalfX = window.innerWidth / 2;
  71. let fontIndex = 1;
  72. init();
  73. animate();
  74. function decimalToHex( d ) {
  75. let hex = Number( d ).toString( 16 );
  76. hex = '000000'.substr( 0, 6 - hex.length ) + hex;
  77. return hex.toUpperCase();
  78. }
  79. function init() {
  80. container = document.createElement( 'div' );
  81. document.body.appendChild( container );
  82. permalink = document.getElementById( 'permalink' );
  83. // CAMERA
  84. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 1500 );
  85. camera.position.set( 0, 400, 700 );
  86. cameraTarget = new THREE.Vector3( 0, 150, 0 );
  87. // SCENE
  88. scene = new THREE.Scene();
  89. scene.background = new THREE.Color( 0x000000 );
  90. scene.fog = new THREE.Fog( 0x000000, 250, 1400 );
  91. // LIGHTS
  92. const dirLight = new THREE.DirectionalLight( 0xffffff, 0.125 );
  93. dirLight.position.set( 0, 0, 1 ).normalize();
  94. scene.add( dirLight );
  95. const pointLight = new THREE.PointLight( 0xffffff, 1.5 );
  96. pointLight.position.set( 0, 100, 90 );
  97. scene.add( pointLight );
  98. // Get text from hash
  99. const hash = document.location.hash.substr( 1 );
  100. if ( hash.length !== 0 ) {
  101. const colorhash = hash.substring( 0, 6 );
  102. const fonthash = hash.substring( 6, 7 );
  103. const weighthash = hash.substring( 7, 8 );
  104. const bevelhash = hash.substring( 8, 9 );
  105. const texthash = hash.substring( 10 );
  106. hex = colorhash;
  107. pointLight.color.setHex( parseInt( colorhash, 16 ) );
  108. fontName = reverseFontMap[ parseInt( fonthash ) ];
  109. fontWeight = reverseWeightMap[ parseInt( weighthash ) ];
  110. bevelEnabled = parseInt( bevelhash );
  111. text = decodeURI( texthash );
  112. updatePermalink();
  113. } else {
  114. pointLight.color.setHSL( Math.random(), 1, 0.5 );
  115. hex = decimalToHex( pointLight.color.getHex() );
  116. }
  117. materials = [
  118. new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } ), // front
  119. new THREE.MeshPhongMaterial( { color: 0xffffff } ) // side
  120. ];
  121. group = new THREE.Group();
  122. group.position.y = 100;
  123. scene.add( group );
  124. loadFont();
  125. const plane = new THREE.Mesh(
  126. new THREE.PlaneGeometry( 10000, 10000 ),
  127. new THREE.MeshBasicMaterial( { color: 0xffffff, opacity: 0.5, transparent: true } )
  128. );
  129. plane.position.y = 100;
  130. plane.rotation.x = - Math.PI / 2;
  131. scene.add( plane );
  132. // RENDERER
  133. renderer = new THREE.WebGLRenderer( { antialias: true } );
  134. renderer.setPixelRatio( window.devicePixelRatio );
  135. renderer.setSize( window.innerWidth, window.innerHeight );
  136. container.appendChild( renderer.domElement );
  137. // STATS
  138. stats = new Stats();
  139. //container.appendChild( stats.dom );
  140. // EVENTS
  141. container.style.touchAction = 'none';
  142. container.addEventListener( 'pointerdown', onPointerDown );
  143. document.addEventListener( 'keypress', onDocumentKeyPress );
  144. document.addEventListener( 'keydown', onDocumentKeyDown );
  145. document.getElementById( 'color' ).addEventListener( 'click', function () {
  146. pointLight.color.setHSL( Math.random(), 1, 0.5 );
  147. hex = decimalToHex( pointLight.color.getHex() );
  148. updatePermalink();
  149. } );
  150. document.getElementById( 'font' ).addEventListener( 'click', function () {
  151. fontIndex ++;
  152. fontName = reverseFontMap[ fontIndex % reverseFontMap.length ];
  153. loadFont();
  154. } );
  155. document.getElementById( 'weight' ).addEventListener( 'click', function () {
  156. if ( fontWeight === 'bold' ) {
  157. fontWeight = 'regular';
  158. } else {
  159. fontWeight = 'bold';
  160. }
  161. loadFont();
  162. } );
  163. document.getElementById( 'bevel' ).addEventListener( 'click', function () {
  164. bevelEnabled = ! bevelEnabled;
  165. refreshText();
  166. } );
  167. //
  168. window.addEventListener( 'resize', onWindowResize );
  169. }
  170. function onWindowResize() {
  171. windowHalfX = window.innerWidth / 2;
  172. camera.aspect = window.innerWidth / window.innerHeight;
  173. camera.updateProjectionMatrix();
  174. renderer.setSize( window.innerWidth, window.innerHeight );
  175. }
  176. //
  177. function boolToNum( b ) {
  178. return b ? 1 : 0;
  179. }
  180. function updatePermalink() {
  181. const link = hex + fontMap[ fontName ] + weightMap[ fontWeight ] + boolToNum( bevelEnabled ) + '#' + encodeURI( text );
  182. permalink.href = '#' + link;
  183. window.location.hash = link;
  184. }
  185. function onDocumentKeyDown( event ) {
  186. if ( firstLetter ) {
  187. firstLetter = false;
  188. text = '';
  189. }
  190. const keyCode = event.keyCode;
  191. // backspace
  192. if ( keyCode == 8 ) {
  193. event.preventDefault();
  194. text = text.substring( 0, text.length - 1 );
  195. refreshText();
  196. return false;
  197. }
  198. }
  199. function onDocumentKeyPress( event ) {
  200. const keyCode = event.which;
  201. // backspace
  202. if ( keyCode == 8 ) {
  203. event.preventDefault();
  204. } else {
  205. const ch = String.fromCharCode( keyCode );
  206. text += ch;
  207. refreshText();
  208. }
  209. }
  210. function loadFont() {
  211. const loader = new FontLoader();
  212. loader.load( 'fonts/' + fontName + '_' + fontWeight + '.typeface.json', function ( response ) {
  213. font = response;
  214. refreshText();
  215. } );
  216. }
  217. function createText() {
  218. textGeo = new TextGeometry( text, {
  219. font: font,
  220. size: size,
  221. height: height,
  222. curveSegments: curveSegments,
  223. bevelThickness: bevelThickness,
  224. bevelSize: bevelSize,
  225. bevelEnabled: bevelEnabled
  226. } );
  227. textGeo.computeBoundingBox();
  228. const centerOffset = - 0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
  229. textMesh1 = new THREE.Mesh( textGeo, materials );
  230. textMesh1.position.x = centerOffset;
  231. textMesh1.position.y = hover;
  232. textMesh1.position.z = 0;
  233. textMesh1.rotation.x = 0;
  234. textMesh1.rotation.y = Math.PI * 2;
  235. group.add( textMesh1 );
  236. if ( mirror ) {
  237. textMesh2 = new THREE.Mesh( textGeo, materials );
  238. textMesh2.position.x = centerOffset;
  239. textMesh2.position.y = - hover;
  240. textMesh2.position.z = height;
  241. textMesh2.rotation.x = Math.PI;
  242. textMesh2.rotation.y = Math.PI * 2;
  243. group.add( textMesh2 );
  244. }
  245. }
  246. function refreshText() {
  247. updatePermalink();
  248. group.remove( textMesh1 );
  249. if ( mirror ) group.remove( textMesh2 );
  250. if ( ! text ) return;
  251. createText();
  252. }
  253. function onPointerDown( event ) {
  254. if ( event.isPrimary === false ) return;
  255. pointerXOnPointerDown = event.clientX - windowHalfX;
  256. targetRotationOnPointerDown = targetRotation;
  257. document.addEventListener( 'pointermove', onPointerMove );
  258. document.addEventListener( 'pointerup', onPointerUp );
  259. }
  260. function onPointerMove( event ) {
  261. if ( event.isPrimary === false ) return;
  262. pointerX = event.clientX - windowHalfX;
  263. targetRotation = targetRotationOnPointerDown + ( pointerX - pointerXOnPointerDown ) * 0.02;
  264. }
  265. function onPointerUp() {
  266. if ( event.isPrimary === false ) return;
  267. document.removeEventListener( 'pointermove', onPointerMove );
  268. document.removeEventListener( 'pointerup', onPointerUp );
  269. }
  270. //
  271. function animate() {
  272. requestAnimationFrame( animate );
  273. render();
  274. stats.update();
  275. }
  276. function render() {
  277. group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
  278. camera.lookAt( cameraTarget );
  279. renderer.clear();
  280. renderer.render( scene, camera );
  281. }
  282. </script>
  283. </body>
  284. </html>