webgl_loader_ttf.html 7.6 KB

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