2
0

webgl_geometry_text_earcut.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. 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.ShapeUtils.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. //container.appendChild( stats.dom );
  178. // EVENTS
  179. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  180. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  181. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  182. document.addEventListener( 'keypress', onDocumentKeyPress, false );
  183. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  184. document.getElementById( "color" ).addEventListener( 'click', function() {
  185. pointLight.color.setHSL( Math.random(), 1, 0.5 );
  186. hex = decimalToHex( pointLight.color.getHex() );
  187. updatePermalink();
  188. }, false );
  189. document.getElementById( "font" ).addEventListener( 'click', function() {
  190. fontIndex ++;
  191. fontName = reverseFontMap[ fontIndex % reverseFontMap.length ];
  192. loadFont();
  193. }, false );
  194. document.getElementById( "weight" ).addEventListener( 'click', function() {
  195. if ( fontWeight === "bold" ) {
  196. fontWeight = "regular";
  197. } else {
  198. fontWeight = "bold";
  199. }
  200. loadFont();
  201. }, false );
  202. document.getElementById( "bevel" ).addEventListener( 'click', function() {
  203. bevelEnabled = !bevelEnabled;
  204. refreshText();
  205. }, false );
  206. //
  207. window.addEventListener( 'resize', onWindowResize, false );
  208. }
  209. function onWindowResize() {
  210. windowHalfX = window.innerWidth / 2;
  211. windowHalfY = window.innerHeight / 2;
  212. camera.aspect = window.innerWidth / window.innerHeight;
  213. camera.updateProjectionMatrix();
  214. renderer.setSize( window.innerWidth, window.innerHeight );
  215. }
  216. //
  217. function boolToNum( b ) {
  218. return b ? 1 : 0;
  219. }
  220. function updatePermalink() {
  221. var link = hex + fontMap[ fontName ] + weightMap[ fontWeight ] + boolToNum( bevelEnabled ) + "#" + encodeURI( text );
  222. permalink.href = "#" + link;
  223. window.location.hash = link;
  224. }
  225. function onDocumentKeyDown( event ) {
  226. if ( firstLetter ) {
  227. firstLetter = false;
  228. text = "";
  229. }
  230. var keyCode = event.keyCode;
  231. // backspace
  232. if ( keyCode == 8 ) {
  233. event.preventDefault();
  234. text = text.substring( 0, text.length - 1 );
  235. refreshText();
  236. return false;
  237. }
  238. }
  239. function onDocumentKeyPress( event ) {
  240. var keyCode = event.which;
  241. // backspace
  242. if ( keyCode == 8 ) {
  243. event.preventDefault();
  244. } else {
  245. var ch = String.fromCharCode( keyCode );
  246. text += ch;
  247. refreshText();
  248. }
  249. }
  250. function loadFont() {
  251. var loader = new THREE.FontLoader();
  252. loader.load( 'fonts/' + fontName + '_' + fontWeight + '.typeface.json', function ( response ) {
  253. font = response;
  254. refreshText();
  255. } );
  256. }
  257. function createText() {
  258. textGeo = new THREE.TextGeometry( text, {
  259. font: font,
  260. size: size,
  261. height: height,
  262. curveSegments: curveSegments,
  263. bevelThickness: bevelThickness,
  264. bevelSize: bevelSize,
  265. bevelEnabled: bevelEnabled,
  266. material: 0,
  267. extrudeMaterial: 1
  268. });
  269. textGeo.computeBoundingBox();
  270. textGeo.computeVertexNormals();
  271. // "fix" side normals by removing z-component of normals for side faces
  272. // (this doesn't work well for beveled geometry as then we lose nice curvature around z-axis)
  273. if ( ! bevelEnabled ) {
  274. var triangleAreaHeuristics = 0.1 * ( height * size );
  275. for ( var i = 0; i < textGeo.faces.length; i ++ ) {
  276. var face = textGeo.faces[ i ];
  277. if ( face.materialIndex == 1 ) {
  278. for ( var j = 0; j < face.vertexNormals.length; j ++ ) {
  279. face.vertexNormals[ j ].z = 0;
  280. face.vertexNormals[ j ].normalize();
  281. }
  282. var va = textGeo.vertices[ face.a ];
  283. var vb = textGeo.vertices[ face.b ];
  284. var vc = textGeo.vertices[ face.c ];
  285. var s = THREE.GeometryUtils.triangleArea( va, vb, vc );
  286. if ( s > triangleAreaHeuristics ) {
  287. for ( var j = 0; j < face.vertexNormals.length; j ++ ) {
  288. face.vertexNormals[ j ].copy( face.normal );
  289. }
  290. }
  291. }
  292. }
  293. }
  294. var centerOffset = -0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
  295. textMesh1 = new THREE.Mesh( textGeo, material );
  296. textMesh1.position.x = centerOffset;
  297. textMesh1.position.y = hover;
  298. textMesh1.position.z = 0;
  299. textMesh1.rotation.x = 0;
  300. textMesh1.rotation.y = Math.PI * 2;
  301. group.add( textMesh1 );
  302. if ( mirror ) {
  303. textMesh2 = new THREE.Mesh( textGeo, material );
  304. textMesh2.position.x = centerOffset;
  305. textMesh2.position.y = -hover;
  306. textMesh2.position.z = height;
  307. textMesh2.rotation.x = Math.PI;
  308. textMesh2.rotation.y = Math.PI * 2;
  309. group.add( textMesh2 );
  310. }
  311. }
  312. function refreshText() {
  313. updatePermalink();
  314. group.remove( textMesh1 );
  315. if ( mirror ) group.remove( textMesh2 );
  316. if ( !text ) return;
  317. createText();
  318. }
  319. function onDocumentMouseDown( event ) {
  320. event.preventDefault();
  321. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  322. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  323. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  324. mouseXOnMouseDown = event.clientX - windowHalfX;
  325. targetRotationOnMouseDown = targetRotation;
  326. }
  327. function onDocumentMouseMove( event ) {
  328. mouseX = event.clientX - windowHalfX;
  329. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  330. }
  331. function onDocumentMouseUp( event ) {
  332. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  333. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  334. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  335. }
  336. function onDocumentMouseOut( event ) {
  337. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  338. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  339. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  340. }
  341. function onDocumentTouchStart( event ) {
  342. if ( event.touches.length == 1 ) {
  343. event.preventDefault();
  344. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  345. targetRotationOnMouseDown = targetRotation;
  346. }
  347. }
  348. function onDocumentTouchMove( event ) {
  349. if ( event.touches.length == 1 ) {
  350. event.preventDefault();
  351. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  352. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  353. }
  354. }
  355. //
  356. function animate() {
  357. requestAnimationFrame( animate );
  358. render();
  359. stats.update();
  360. }
  361. function render() {
  362. group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
  363. camera.lookAt( cameraTarget );
  364. renderer.clear();
  365. renderer.render( scene, camera );
  366. }
  367. </script>
  368. </body>
  369. </html>