webgl_geometry_text_pnltri.html 13 KB

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