webgl_loader_ttf.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - loader - ttf</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. <script src="js/libs/opentype.min.js"></script>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - TTFLoader using opentype by gero3
  13. <br/>type to enter new text, drag to spin the text
  14. </div>
  15. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import { TTFLoader } from './jsm/loaders/TTFLoader.js';
  18. var container;
  19. var camera, cameraTarget, scene, renderer;
  20. var group, textMesh1, textMesh2, textGeo, material;
  21. var firstLetter = true;
  22. var text = 'three.js',
  23. height = 20,
  24. size = 70,
  25. hover = 30,
  26. curveSegments = 4,
  27. bevelThickness = 2,
  28. bevelSize = 1.5;
  29. var font = null;
  30. var mirror = true;
  31. var targetRotation = 0;
  32. var targetRotationOnMouseDown = 0;
  33. var mouseX = 0;
  34. var mouseXOnMouseDown = 0;
  35. var windowHalfX = window.innerWidth / 2;
  36. init();
  37. animate();
  38. function init() {
  39. container = document.createElement( 'div' );
  40. document.body.appendChild( container );
  41. // CAMERA
  42. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 1500 );
  43. camera.position.set( 0, 400, 700 );
  44. cameraTarget = new THREE.Vector3( 0, 150, 0 );
  45. // SCENE
  46. scene = new THREE.Scene();
  47. scene.background = new THREE.Color( 0x000000 );
  48. scene.fog = new THREE.Fog( 0x000000, 250, 1400 );
  49. // LIGHTS
  50. var dirLight = new THREE.DirectionalLight( 0xffffff, 0.125 );
  51. dirLight.position.set( 0, 0, 1 ).normalize();
  52. scene.add( dirLight );
  53. var pointLight = new THREE.PointLight( 0xffffff, 1.5 );
  54. pointLight.position.set( 0, 100, 90 );
  55. pointLight.color.setHSL( Math.random(), 1, 0.5 );
  56. scene.add( pointLight );
  57. material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
  58. group = new THREE.Group();
  59. group.position.y = 100;
  60. scene.add( group );
  61. var loader = new TTFLoader();
  62. loader.load( 'fonts/ttf/kenpixel.ttf', function ( json ) {
  63. font = new THREE.Font( json );
  64. createText();
  65. } );
  66. var plane = new THREE.Mesh(
  67. new THREE.PlaneBufferGeometry( 10000, 10000 ),
  68. new THREE.MeshBasicMaterial( { color: 0xffffff, opacity: 0.5, transparent: true } )
  69. );
  70. plane.position.y = 100;
  71. plane.rotation.x = - Math.PI / 2;
  72. scene.add( plane );
  73. // RENDERER
  74. renderer = new THREE.WebGLRenderer( { antialias: true } );
  75. renderer.setPixelRatio( window.devicePixelRatio );
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. container.appendChild( renderer.domElement );
  78. // EVENTS
  79. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  80. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  81. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  82. document.addEventListener( 'keypress', onDocumentKeyPress, false );
  83. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  84. window.addEventListener( 'resize', onWindowResize, false );
  85. }
  86. function onWindowResize() {
  87. windowHalfX = window.innerWidth / 2;
  88. camera.aspect = window.innerWidth / window.innerHeight;
  89. camera.updateProjectionMatrix();
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. }
  92. function onDocumentKeyDown( event ) {
  93. if ( firstLetter ) {
  94. firstLetter = false;
  95. text = '';
  96. }
  97. var keyCode = event.keyCode;
  98. // backspace
  99. if ( keyCode === 8 ) {
  100. event.preventDefault();
  101. text = text.substring( 0, text.length - 1 );
  102. refreshText();
  103. return false;
  104. }
  105. }
  106. function onDocumentKeyPress( event ) {
  107. var keyCode = event.which;
  108. // backspace
  109. if ( keyCode === 8 ) {
  110. event.preventDefault();
  111. } else {
  112. var ch = String.fromCharCode( keyCode );
  113. text += ch;
  114. refreshText();
  115. }
  116. }
  117. function createText() {
  118. textGeo = new THREE.TextBufferGeometry( text, {
  119. font: font,
  120. size: size,
  121. height: height,
  122. curveSegments: curveSegments,
  123. bevelThickness: bevelThickness,
  124. bevelSize: bevelSize,
  125. bevelEnabled: true
  126. } );
  127. textGeo.computeBoundingBox();
  128. textGeo.computeVertexNormals();
  129. var centerOffset = - 0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
  130. textMesh1 = new THREE.Mesh( textGeo, material );
  131. textMesh1.position.x = centerOffset;
  132. textMesh1.position.y = hover;
  133. textMesh1.position.z = 0;
  134. textMesh1.rotation.x = 0;
  135. textMesh1.rotation.y = Math.PI * 2;
  136. group.add( textMesh1 );
  137. if ( mirror ) {
  138. textMesh2 = new THREE.Mesh( textGeo, material );
  139. textMesh2.position.x = centerOffset;
  140. textMesh2.position.y = - hover;
  141. textMesh2.position.z = height;
  142. textMesh2.rotation.x = Math.PI;
  143. textMesh2.rotation.y = Math.PI * 2;
  144. group.add( textMesh2 );
  145. }
  146. }
  147. function refreshText() {
  148. group.remove( textMesh1 );
  149. if ( mirror ) group.remove( textMesh2 );
  150. if ( ! text ) return;
  151. createText();
  152. }
  153. function onDocumentMouseDown( event ) {
  154. event.preventDefault();
  155. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  156. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  157. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  158. mouseXOnMouseDown = event.clientX - windowHalfX;
  159. targetRotationOnMouseDown = targetRotation;
  160. }
  161. function onDocumentMouseMove( event ) {
  162. mouseX = event.clientX - windowHalfX;
  163. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  164. }
  165. function onDocumentMouseUp() {
  166. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  167. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  168. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  169. }
  170. function onDocumentMouseOut() {
  171. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  172. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  173. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  174. }
  175. function onDocumentTouchStart( event ) {
  176. if ( event.touches.length === 1 ) {
  177. event.preventDefault();
  178. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  179. targetRotationOnMouseDown = targetRotation;
  180. }
  181. }
  182. function onDocumentTouchMove( event ) {
  183. if ( event.touches.length === 1 ) {
  184. event.preventDefault();
  185. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  186. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  187. }
  188. }
  189. function animate() {
  190. requestAnimationFrame( animate );
  191. group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
  192. camera.lookAt( cameraTarget );
  193. renderer.render( scene, camera );
  194. }
  195. </script>
  196. </body>
  197. </html>