webgl_geometry_text.html 9.7 KB

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