webgl_geometry_text_pnltri.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. <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, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="info">
  28. <a href="http://threejs.org" target="_blank">three.js</a> - procedural 3D text by <a href="http://www.lab4games.net/zz85/blog" target="_blank">zz85</a> &amp; alteredq
  29. <br/>built-in shape triangulation has been replaced with <a href="https://github.com/jahting/pnltri.js">PnlTri.js</a> by <a href="https://github.com/jahting" target="_blank">j ahting</a>
  30. <br/>type to enter new text, drag to spin the text
  31. <br/><span class="button" id="color">change color</span>,
  32. <span class="button" id="font">change font</span>,
  33. <span class="button" id="weight">change weight</span>,
  34. <span class="button" id="bevel">change bevel</span>
  35. <a id="permalink" href="#">permalink</a>
  36. </div>
  37. <script src="../build/three.js"></script>
  38. <script src="js/utils/GeometryUtils.js"></script>
  39. <script src="js/Detector.js"></script>
  40. <script src="js/libs/stats.min.js"></script>
  41. <!-- replace built-in triangulation with PnlTri.js -->
  42. <script src="js/libs/pnltri.min.js"></script>
  43. <script>
  44. THREE.ShapeUtils.triangulateShape = ( function () {
  45. var pnlTriangulator = new PNLTRI.Triangulator();
  46. return function triangulateShape( contour, holes ) {
  47. // console.log("new Triangulation: PnlTri.js " + PNLTRI.REVISION );
  48. return pnlTriangulator.triangulate_polygon( [ contour ].concat(holes) );
  49. };
  50. } )();
  51. </script>
  52. <script>
  53. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  54. THREE.Cache.enabled = true;
  55. var container, stats, permalink, hex, color;
  56. var camera, cameraTarget, scene, renderer;
  57. var group, textMesh1, textMesh2, textGeo, material;
  58. var firstLetter = true;
  59. var text = "PnlTri",
  60. height = 20,
  61. size = 70,
  62. hover = 30,
  63. curveSegments = 4,
  64. bevelThickness = 2,
  65. bevelSize = 1.5,
  66. bevelSegments = 3,
  67. bevelEnabled = true,
  68. font = undefined,
  69. fontName = "optimer", // helvetiker, optimer, gentilis, droid sans, droid serif
  70. fontWeight = "bold"; // normal bold
  71. var mirror = true;
  72. var fontMap = {
  73. "helvetiker": 0,
  74. "optimer": 1,
  75. "gentilis": 2,
  76. "droid/droid_sans": 3,
  77. "droid/droid_serif": 4
  78. };
  79. var weightMap = {
  80. "regular": 0,
  81. "bold": 1
  82. };
  83. var reverseFontMap = [];
  84. var reverseWeightMap = [];
  85. for ( var i in fontMap ) reverseFontMap[ fontMap[i] ] = i;
  86. for ( var i in weightMap ) reverseWeightMap[ weightMap[i] ] = i;
  87. var targetRotation = 0;
  88. var targetRotationOnMouseDown = 0;
  89. var mouseX = 0;
  90. var mouseXOnMouseDown = 0;
  91. var windowHalfX = window.innerWidth / 2;
  92. var windowHalfY = window.innerHeight / 2;
  93. var fontIndex = 1;
  94. init();
  95. animate();
  96. function decimalToHex( d ) {
  97. var hex = Number( d ).toString( 16 );
  98. hex = "000000".substr( 0, 6 - hex.length ) + hex;
  99. return hex.toUpperCase();
  100. }
  101. function init() {
  102. container = document.createElement( 'div' );
  103. document.body.appendChild( container );
  104. permalink = document.getElementById( "permalink" );
  105. // CAMERA
  106. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 1500 );
  107. camera.position.set( 0, 400, 700 );
  108. cameraTarget = new THREE.Vector3( 0, 150, 0 );
  109. // SCENE
  110. scene = new THREE.Scene();
  111. scene.fog = new THREE.Fog( 0x000000, 250, 1400 );
  112. // LIGHTS
  113. var dirLight = new THREE.DirectionalLight( 0xffffff, 0.125 );
  114. dirLight.position.set( 0, 0, 1 ).normalize();
  115. scene.add( dirLight );
  116. var pointLight = new THREE.PointLight( 0xffffff, 1.5 );
  117. pointLight.position.set( 0, 100, 90 );
  118. scene.add( pointLight );
  119. // Get text from hash
  120. var hash = document.location.hash.substr( 1 );
  121. if ( hash.length !== 0 ) {
  122. var colorhash = hash.substring( 0, 6 );
  123. var fonthash = hash.substring( 6, 7 );
  124. var weighthash = hash.substring( 7, 8 );
  125. var bevelhash = hash.substring( 8, 9 );
  126. var texthash = hash.substring( 10 );
  127. hex = colorhash;
  128. pointLight.color.setHex( parseInt( colorhash, 16 ) );
  129. fontName = reverseFontMap[ parseInt( fonthash ) ];
  130. fontWeight = reverseWeightMap[ parseInt( weighthash ) ];
  131. bevelEnabled = parseInt( bevelhash );
  132. text = decodeURI( texthash );
  133. updatePermalink();
  134. } else {
  135. pointLight.color.setHSL( Math.random(), 1, 0.5 );
  136. hex = decimalToHex( pointLight.color.getHex() );
  137. }
  138. material = new THREE.MultiMaterial( [
  139. new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading } ), // front
  140. new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.SmoothShading } ) // side
  141. ] );
  142. group = new THREE.Group();
  143. group.position.y = 100;
  144. scene.add( group );
  145. loadFont();
  146. var plane = new THREE.Mesh(
  147. new THREE.PlaneBufferGeometry( 10000, 10000 ),
  148. new THREE.MeshBasicMaterial( { color: 0xffffff, opacity: 0.5, transparent: true } )
  149. );
  150. plane.position.y = 100;
  151. plane.rotation.x = - Math.PI / 2;
  152. scene.add( plane );
  153. // RENDERER
  154. renderer = new THREE.WebGLRenderer( { antialias: true } );
  155. renderer.setClearColor( scene.fog.color );
  156. renderer.setPixelRatio( window.devicePixelRatio );
  157. renderer.setSize( window.innerWidth, window.innerHeight );
  158. container.appendChild( renderer.domElement );
  159. // STATS
  160. stats = new Stats();
  161. stats.domElement.style.position = 'absolute';
  162. stats.domElement.style.top = '0px';
  163. //container.appendChild( stats.domElement );
  164. // EVENTS
  165. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  166. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  167. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  168. document.addEventListener( 'keypress', onDocumentKeyPress, false );
  169. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  170. document.getElementById( "color" ).addEventListener( 'click', function() {
  171. pointLight.color.setHSL( Math.random(), 1, 0.5 );
  172. hex = decimalToHex( pointLight.color.getHex() );
  173. updatePermalink();
  174. }, false );
  175. document.getElementById( "font" ).addEventListener( 'click', function() {
  176. fontIndex ++;
  177. fontName = reverseFontMap[ fontIndex % reverseFontMap.length ];
  178. loadFont();
  179. }, false );
  180. document.getElementById( "weight" ).addEventListener( 'click', function() {
  181. if ( fontWeight === "bold" ) {
  182. fontWeight = "regular";
  183. } else {
  184. fontWeight = "bold";
  185. }
  186. loadFont();
  187. }, false );
  188. document.getElementById( "bevel" ).addEventListener( 'click', function() {
  189. bevelEnabled = !bevelEnabled;
  190. refreshText();
  191. }, false );
  192. //
  193. window.addEventListener( 'resize', onWindowResize, false );
  194. }
  195. function onWindowResize() {
  196. windowHalfX = window.innerWidth / 2;
  197. windowHalfY = window.innerHeight / 2;
  198. camera.aspect = window.innerWidth / window.innerHeight;
  199. camera.updateProjectionMatrix();
  200. renderer.setSize( window.innerWidth, window.innerHeight );
  201. }
  202. //
  203. function boolToNum( b ) {
  204. return b ? 1 : 0;
  205. }
  206. function updatePermalink() {
  207. var link = hex + fontMap[ fontName ] + weightMap[ fontWeight ] + boolToNum( bevelEnabled ) + "#" + encodeURI( text );
  208. permalink.href = "#" + link;
  209. window.location.hash = link;
  210. }
  211. function onDocumentKeyDown( event ) {
  212. if ( firstLetter ) {
  213. firstLetter = false;
  214. text = "";
  215. }
  216. var keyCode = event.keyCode;
  217. // backspace
  218. if ( keyCode == 8 ) {
  219. event.preventDefault();
  220. text = text.substring( 0, text.length - 1 );
  221. refreshText();
  222. return false;
  223. }
  224. }
  225. function onDocumentKeyPress( event ) {
  226. var keyCode = event.which;
  227. // backspace
  228. if ( keyCode == 8 ) {
  229. event.preventDefault();
  230. } else {
  231. var ch = String.fromCharCode( keyCode );
  232. text += ch;
  233. refreshText();
  234. }
  235. }
  236. function loadFont() {
  237. var loader = new THREE.FontLoader();
  238. loader.load( 'fonts/' + fontName + '_' + fontWeight + '.typeface.js', function ( response ) {
  239. font = response;
  240. refreshText();
  241. } );
  242. }
  243. function createText() {
  244. textGeo = new THREE.TextGeometry( text, {
  245. font: font,
  246. size: size,
  247. height: height,
  248. curveSegments: curveSegments,
  249. bevelThickness: bevelThickness,
  250. bevelSize: bevelSize,
  251. bevelEnabled: bevelEnabled,
  252. material: 0,
  253. extrudeMaterial: 1
  254. });
  255. textGeo.computeBoundingBox();
  256. textGeo.computeVertexNormals();
  257. // "fix" side normals by removing z-component of normals for side faces
  258. // (this doesn't work well for beveled geometry as then we lose nice curvature around z-axis)
  259. if ( ! bevelEnabled ) {
  260. var triangleAreaHeuristics = 0.1 * ( height * size );
  261. for ( var i = 0; i < textGeo.faces.length; i ++ ) {
  262. var face = textGeo.faces[ i ];
  263. if ( face.materialIndex == 1 ) {
  264. for ( var j = 0; j < face.vertexNormals.length; j ++ ) {
  265. face.vertexNormals[ j ].z = 0;
  266. face.vertexNormals[ j ].normalize();
  267. }
  268. var va = textGeo.vertices[ face.a ];
  269. var vb = textGeo.vertices[ face.b ];
  270. var vc = textGeo.vertices[ face.c ];
  271. var s = THREE.GeometryUtils.triangleArea( va, vb, vc );
  272. if ( s > triangleAreaHeuristics ) {
  273. for ( var j = 0; j < face.vertexNormals.length; j ++ ) {
  274. face.vertexNormals[ j ].copy( face.normal );
  275. }
  276. }
  277. }
  278. }
  279. }
  280. var centerOffset = -0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
  281. textMesh1 = new THREE.Mesh( textGeo, material );
  282. textMesh1.position.x = centerOffset;
  283. textMesh1.position.y = hover;
  284. textMesh1.position.z = 0;
  285. textMesh1.rotation.x = 0;
  286. textMesh1.rotation.y = Math.PI * 2;
  287. group.add( textMesh1 );
  288. if ( mirror ) {
  289. textMesh2 = new THREE.Mesh( textGeo, material );
  290. textMesh2.position.x = centerOffset;
  291. textMesh2.position.y = -hover;
  292. textMesh2.position.z = height;
  293. textMesh2.rotation.x = Math.PI;
  294. textMesh2.rotation.y = Math.PI * 2;
  295. group.add( textMesh2 );
  296. }
  297. }
  298. function refreshText() {
  299. updatePermalink();
  300. group.remove( textMesh1 );
  301. if ( mirror ) group.remove( textMesh2 );
  302. if ( !text ) return;
  303. createText();
  304. }
  305. function onDocumentMouseDown( event ) {
  306. event.preventDefault();
  307. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  308. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  309. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  310. mouseXOnMouseDown = event.clientX - windowHalfX;
  311. targetRotationOnMouseDown = targetRotation;
  312. }
  313. function onDocumentMouseMove( event ) {
  314. mouseX = event.clientX - windowHalfX;
  315. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  316. }
  317. function onDocumentMouseUp( event ) {
  318. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  319. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  320. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  321. }
  322. function onDocumentMouseOut( event ) {
  323. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  324. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  325. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  326. }
  327. function onDocumentTouchStart( event ) {
  328. if ( event.touches.length == 1 ) {
  329. event.preventDefault();
  330. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  331. targetRotationOnMouseDown = targetRotation;
  332. }
  333. }
  334. function onDocumentTouchMove( event ) {
  335. if ( event.touches.length == 1 ) {
  336. event.preventDefault();
  337. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  338. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  339. }
  340. }
  341. //
  342. function animate() {
  343. requestAnimationFrame( animate );
  344. render();
  345. stats.update();
  346. }
  347. function render() {
  348. group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
  349. camera.lookAt( cameraTarget );
  350. renderer.clear();
  351. renderer.render( scene, camera );
  352. }
  353. </script>
  354. </body>
  355. </html>