webgl_geometry_text_earcut.html 13 KB

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