webgl_loader_ttf.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. {
  10. font-family: Monospace;
  11. background-color: #000;
  12. color: #fff;
  13. margin: 0px;
  14. overflow: hidden;
  15. }
  16. #info
  17. {
  18. position: absolute;
  19. top: 10px;
  20. width: 100%;
  21. text-align: center;
  22. z-index: 100;
  23. display:block;
  24. }
  25. #info a, .button
  26. {
  27. color: #f00;
  28. font-weight: bold;
  29. text-decoration: underline;
  30. cursor: pointer
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <script src="../build/three.js"></script>
  36. <script src="js/utils/GeometryUtils.js"></script>
  37. <script src="js/Detector.js"></script>
  38. <script src="js/loaders/TTFLoader.js"></script>
  39. <script src="js/libs/stats.min.js"></script>
  40. <script src="js/libs/opentype.min.js"></script>
  41. <div id="info">
  42. <a href="http://threejs.org" target="_blank">three.js</a> - TTFLoader using opentype by gero3
  43. <br/>type to enter new text, drag to spin the text
  44. <br/><span class="button" id="color">change color</span>,
  45. <span class="button" id="weight">change weight</span>,
  46. <span class="button" id="bevel">change bevel</span>
  47. </div>
  48. <script>
  49. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  50. THREE.Cache.enabled = true;
  51. var container, stats, hex, color;
  52. var camera, cameraTarget, scene, renderer;
  53. var group, textMesh1, textMesh2, textGeo, material;
  54. var firstLetter = true;
  55. var text = "three.js",
  56. height = 20,
  57. size = 70,
  58. hover = 30,
  59. curveSegments = 4,
  60. bevelThickness = 2,
  61. bevelSize = 1.5,
  62. bevelSegments = 3,
  63. bevelEnabled = true;
  64. var font = null;
  65. var mirror = true;
  66. var targetRotation = 0;
  67. var targetRotationOnMouseDown = 0;
  68. var mouseX = 0;
  69. var mouseXOnMouseDown = 0;
  70. var windowHalfX = window.innerWidth / 2;
  71. var windowHalfY = window.innerHeight / 2;
  72. init();
  73. animate();
  74. function decimalToHex( d ) {
  75. var 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. // CAMERA
  83. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 1500 );
  84. camera.position.set( 0, 400, 700 );
  85. cameraTarget = new THREE.Vector3( 0, 150, 0 );
  86. // SCENE
  87. scene = new THREE.Scene();
  88. scene.fog = new THREE.Fog( 0x000000, 250, 1400 );
  89. // LIGHTS
  90. var dirLight = new THREE.DirectionalLight( 0xffffff, 0.125 );
  91. dirLight.position.set( 0, 0, 1 ).normalize();
  92. scene.add( dirLight );
  93. var pointLight = new THREE.PointLight( 0xffffff, 1.5 );
  94. pointLight.position.set( 0, 100, 90 );
  95. scene.add( pointLight );
  96. // Get text from hash
  97. var hash = document.location.hash.substr( 1 );
  98. if ( hash.length !== 0 ) {
  99. var colorhash = hash.substring( 0, 6 );
  100. var fonthash = hash.substring( 6, 7 );
  101. var weighthash = hash.substring( 7, 8 );
  102. var bevelhash = hash.substring( 8, 9 );
  103. var texthash = hash.substring( 10 );
  104. hex = colorhash;
  105. pointLight.color.setHex( parseInt( colorhash, 16 ) );
  106. fontName = reverseFontMap[ parseInt( fonthash ) ];
  107. fontWeight = reverseWeightMap[ parseInt( weighthash ) ];
  108. bevelEnabled = parseInt( bevelhash );
  109. text = decodeURI( texthash );
  110. } else {
  111. pointLight.color.setHSL( Math.random(), 1, 0.5 );
  112. hex = decimalToHex( pointLight.color.getHex() );
  113. }
  114. material = new THREE.MultiMaterial( [
  115. new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading } ), // front
  116. new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.SmoothShading } ) // side
  117. ] );
  118. group = new THREE.Group();
  119. group.position.y = 100;
  120. scene.add( group );
  121. var loader = new THREE.TTFLoader();
  122. loader.load( 'fonts/ttf/kenpixel.ttf', function ( json ) {
  123. font = new THREE.Font(json);
  124. //scene.add( font );
  125. createText();
  126. } );
  127. var plane = new THREE.Mesh(
  128. new THREE.PlaneBufferGeometry( 10000, 10000 ),
  129. new THREE.MeshBasicMaterial( { color: 0xffffff, opacity: 0.5, transparent: true } )
  130. );
  131. plane.position.y = 100;
  132. plane.rotation.x = - Math.PI / 2;
  133. scene.add( plane );
  134. // RENDERER
  135. renderer = new THREE.WebGLRenderer( { antialias: true } );
  136. renderer.setClearColor( scene.fog.color );
  137. renderer.setPixelRatio( window.devicePixelRatio );
  138. renderer.setSize( window.innerWidth, window.innerHeight );
  139. container.appendChild( renderer.domElement );
  140. // STATS
  141. stats = new Stats();
  142. //container.appendChild( stats.dom );
  143. // EVENTS
  144. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  145. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  146. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  147. document.addEventListener( 'keypress', onDocumentKeyPress, false );
  148. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  149. document.getElementById( "color" ).addEventListener( 'click', function() {
  150. pointLight.color.setHSL( Math.random(), 1, 0.5 );
  151. hex = decimalToHex( pointLight.color.getHex() );
  152. }, false );
  153. document.getElementById( "bevel" ).addEventListener( 'click', function(){
  154. bevelEnabled = !bevelEnabled;
  155. refreshText();
  156. }, false );
  157. window.addEventListener( 'resize', onWindowResize, false );
  158. }
  159. function onWindowResize() {
  160. windowHalfX = window.innerWidth / 2;
  161. windowHalfY = window.innerHeight / 2;
  162. camera.aspect = window.innerWidth / window.innerHeight;
  163. camera.updateProjectionMatrix();
  164. renderer.setSize( window.innerWidth, window.innerHeight );
  165. }
  166. function boolToNum( b ) {
  167. return b ? 1 : 0;
  168. }
  169. function onDocumentKeyDown( event ) {
  170. if ( firstLetter ) {
  171. firstLetter = false;
  172. text = "";
  173. }
  174. var keyCode = event.keyCode;
  175. // backspace
  176. if ( keyCode == 8 ) {
  177. event.preventDefault();
  178. text = text.substring( 0, text.length - 1 );
  179. refreshText();
  180. return false;
  181. }
  182. }
  183. function onDocumentKeyPress( event ) {
  184. var keyCode = event.which;
  185. // backspace
  186. if ( keyCode == 8 ) {
  187. event.preventDefault();
  188. } else {
  189. var ch = String.fromCharCode( keyCode );
  190. text += ch;
  191. refreshText();
  192. }
  193. }
  194. function createText() {
  195. textGeo = new THREE.TextGeometry( text, {
  196. font: font,
  197. size: size,
  198. height: height,
  199. curveSegments: curveSegments,
  200. bevelThickness: bevelThickness,
  201. bevelSize: bevelSize,
  202. bevelEnabled: bevelEnabled,
  203. material: 0,
  204. extrudeMaterial: 1
  205. });
  206. textGeo.computeBoundingBox();
  207. textGeo.computeVertexNormals();
  208. // "fix" side normals by removing z-component of normals for side faces
  209. // (this doesn't work well for beveled geometry as then we lose nice curvature around z-axis)
  210. if ( ! bevelEnabled ) {
  211. var triangleAreaHeuristics = 0.1 * ( height * size );
  212. for ( var i = 0; i < textGeo.faces.length; i ++ ) {
  213. var face = textGeo.faces[ i ];
  214. if ( face.materialIndex == 1 ) {
  215. for ( var j = 0; j < face.vertexNormals.length; j ++ ) {
  216. face.vertexNormals[ j ].z = 0;
  217. face.vertexNormals[ j ].normalize();
  218. }
  219. var va = textGeo.vertices[ face.a ];
  220. var vb = textGeo.vertices[ face.b ];
  221. var vc = textGeo.vertices[ face.c ];
  222. var s = THREE.GeometryUtils.triangleArea( va, vb, vc );
  223. if ( s > triangleAreaHeuristics ) {
  224. for ( var j = 0; j < face.vertexNormals.length; j ++ ) {
  225. face.vertexNormals[ j ].copy( face.normal );
  226. }
  227. }
  228. }
  229. }
  230. }
  231. var centerOffset = -0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
  232. textMesh1 = new THREE.Mesh( textGeo, material );
  233. textMesh1.position.x = centerOffset;
  234. textMesh1.position.y = hover;
  235. textMesh1.position.z = 0;
  236. textMesh1.rotation.x = 0;
  237. textMesh1.rotation.y = Math.PI * 2;
  238. group.add( textMesh1 );
  239. if ( mirror ) {
  240. textMesh2 = new THREE.Mesh( textGeo, material );
  241. textMesh2.position.x = centerOffset;
  242. textMesh2.position.y = -hover;
  243. textMesh2.position.z = height;
  244. textMesh2.rotation.x = Math.PI;
  245. textMesh2.rotation.y = Math.PI * 2;
  246. group.add( textMesh2 );
  247. }
  248. }
  249. function refreshText() {
  250. group.remove( textMesh1 );
  251. if ( mirror ) group.remove( textMesh2 );
  252. if ( !text ) return;
  253. createText();
  254. }
  255. function onDocumentMouseDown( event ) {
  256. event.preventDefault();
  257. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  258. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  259. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  260. mouseXOnMouseDown = event.clientX - windowHalfX;
  261. targetRotationOnMouseDown = targetRotation;
  262. }
  263. function onDocumentMouseMove( event ) {
  264. mouseX = event.clientX - windowHalfX;
  265. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  266. }
  267. function onDocumentMouseUp( event ) {
  268. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  269. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  270. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  271. }
  272. function onDocumentMouseOut( event ) {
  273. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  274. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  275. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  276. }
  277. function onDocumentTouchStart( event ) {
  278. if ( event.touches.length == 1 ) {
  279. event.preventDefault();
  280. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  281. targetRotationOnMouseDown = targetRotation;
  282. }
  283. }
  284. function onDocumentTouchMove( event ) {
  285. if ( event.touches.length == 1 ) {
  286. event.preventDefault();
  287. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  288. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  289. }
  290. }
  291. function animate() {
  292. requestAnimationFrame( animate );
  293. render();
  294. stats.update();
  295. }
  296. function render() {
  297. group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
  298. camera.lookAt( cameraTarget );
  299. renderer.clear();
  300. renderer.render( scene, camera );
  301. }
  302. </script>
  303. </body>
  304. </html>