webgl_geometry_text_earcut.html 13 KB

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