webgl_geometry_text.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. (fonts from <a href="http://typeface.neocracy.org/">typeface.js</a> and <a href="http://en.wikipedia.org/wiki/Droid_%28font%29">Droid</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. <span class="button" id="postprocessing">change postprocessing</span>,
  36. <a id="permalink" href="#">permalink</a>
  37. </div>
  38. <script src="../build/three.min.js"></script>
  39. <script src="js/utils/GeometryUtils.js"></script>
  40. <script src="js/shaders/ConvolutionShader.js"></script>
  41. <script src="js/shaders/CopyShader.js"></script>
  42. <script src="js/shaders/FilmShader.js"></script>
  43. <script src="js/shaders/FXAAShader.js"></script>
  44. <script src="js/postprocessing/EffectComposer.js"></script>
  45. <script src="js/postprocessing/RenderPass.js"></script>
  46. <script src="js/postprocessing/ShaderPass.js"></script>
  47. <script src="js/postprocessing/MaskPass.js"></script>
  48. <script src="js/postprocessing/BloomPass.js"></script>
  49. <script src="js/postprocessing/FilmPass.js"></script>
  50. <script src="js/Detector.js"></script>
  51. <script src="js/libs/stats.min.js"></script>
  52. <!-- load the font files -->
  53. <script src="fonts/gentilis_bold.typeface.js"></script>
  54. <script src="fonts/gentilis_regular.typeface.js"></script>
  55. <script src="fonts/optimer_bold.typeface.js"></script>
  56. <script src="fonts/optimer_regular.typeface.js"></script>
  57. <script src="fonts/helvetiker_bold.typeface.js"></script>
  58. <script src="fonts/helvetiker_regular.typeface.js"></script>
  59. <script src="fonts/droid/droid_sans_regular.typeface.js"></script>
  60. <script src="fonts/droid/droid_sans_bold.typeface.js"></script>
  61. <script src="fonts/droid/droid_serif_regular.typeface.js"></script>
  62. <script src="fonts/droid/droid_serif_bold.typeface.js"></script>
  63. <!-- todo async loader for fonts -->
  64. <script>
  65. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  66. var container, stats, permalink, hex, color;
  67. var camera, cameraTarget, scene, renderer;
  68. var composer;
  69. var effectFXAA;
  70. var group, textMesh1, textMesh2, textGeo, material;
  71. var firstLetter = true;
  72. var text = "three.js",
  73. height = 20,
  74. size = 70,
  75. hover = 30,
  76. curveSegments = 4,
  77. bevelThickness = 2,
  78. bevelSize = 1.5,
  79. bevelSegments = 3,
  80. bevelEnabled = true,
  81. font = "optimer", // helvetiker, optimer, gentilis, droid sans, droid serif
  82. weight = "bold", // normal bold
  83. style = "normal"; // normal italic
  84. var mirror = true;
  85. var fontMap = {
  86. "helvetiker": 0,
  87. "optimer": 1,
  88. "gentilis": 2,
  89. "droid sans": 3,
  90. "droid serif": 4
  91. };
  92. var weightMap = {
  93. "normal": 0,
  94. "bold": 1
  95. };
  96. var reverseFontMap = [];
  97. var reverseWeightMap = [];
  98. for ( var i in fontMap ) reverseFontMap[ fontMap[i] ] = i;
  99. for ( var i in weightMap ) reverseWeightMap[ weightMap[i] ] = i;
  100. var targetRotation = 0;
  101. var targetRotationOnMouseDown = 0;
  102. var mouseX = 0;
  103. var mouseXOnMouseDown = 0;
  104. var windowHalfX = window.innerWidth / 2;
  105. var windowHalfY = window.innerHeight / 2;
  106. var postprocessing = { enabled : false };
  107. var glow = 0.9;
  108. var fontIndex = 0;
  109. init();
  110. animate();
  111. function decimalToHex( d ) {
  112. var hex = Number( d ).toString( 16 );
  113. hex = "000000".substr( 0, 6 - hex.length ) + hex;
  114. return hex.toUpperCase();
  115. }
  116. function init() {
  117. container = document.createElement( 'div' );
  118. document.body.appendChild( container );
  119. permalink = document.getElementById( "permalink" );
  120. // CAMERA
  121. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 1500 );
  122. camera.position.set( 0, 400, 700 );
  123. cameraTarget = new THREE.Vector3( 0, 150, 0 );
  124. // SCENE
  125. scene = new THREE.Scene();
  126. scene.fog = new THREE.Fog( 0x000000, 250, 1400 );
  127. // LIGHTS
  128. var dirLight = new THREE.DirectionalLight( 0xffffff, 0.125 );
  129. dirLight.position.set( 0, 0, 1 ).normalize();
  130. scene.add( dirLight );
  131. var pointLight = new THREE.PointLight( 0xffffff, 1.5 );
  132. pointLight.position.set( 0, 100, 90 );
  133. scene.add( pointLight );
  134. //text = capitalize( font ) + " " + capitalize( weight );
  135. //text = "abcdefghijklmnopqrstuvwxyz0123456789";
  136. //text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  137. // Get text from hash
  138. var hash = document.location.hash.substr( 1 );
  139. if ( hash.length !== 0 ) {
  140. var colorhash = hash.substring( 0, 6 );
  141. var fonthash = hash.substring( 6, 7 );
  142. var weighthash = hash.substring( 7, 8 );
  143. var pphash = hash.substring( 8, 9 );
  144. var bevelhash = hash.substring( 9, 10 );
  145. var texthash = hash.substring( 10 );
  146. hex = colorhash;
  147. pointLight.color.setHex( parseInt( colorhash, 16 ) );
  148. font = reverseFontMap[ parseInt( fonthash ) ];
  149. weight = reverseWeightMap[ parseInt( weighthash ) ];
  150. postprocessing.enabled = parseInt( pphash );
  151. bevelEnabled = parseInt( bevelhash );
  152. text = decodeURI( texthash );
  153. updatePermalink();
  154. } else {
  155. pointLight.color.setHSL( Math.random(), 1, 0.5 );
  156. hex = decimalToHex( pointLight.color.getHex() );
  157. }
  158. material = new THREE.MeshFaceMaterial( [
  159. new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading } ), // front
  160. new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.SmoothShading } ) // side
  161. ] );
  162. group = new THREE.Group();
  163. group.position.y = 100;
  164. scene.add( group );
  165. createText();
  166. var plane = new THREE.Mesh(
  167. new THREE.PlaneBufferGeometry( 10000, 10000 ),
  168. new THREE.MeshBasicMaterial( { color: 0xffffff, opacity: 0.5, transparent: true } )
  169. );
  170. plane.position.y = 100;
  171. plane.rotation.x = - Math.PI / 2;
  172. scene.add( plane );
  173. // RENDERER
  174. renderer = new THREE.WebGLRenderer( { antialias: true } );
  175. renderer.setClearColor( scene.fog.color );
  176. renderer.setPixelRatio( window.devicePixelRatio );
  177. renderer.setSize( window.innerWidth, window.innerHeight );
  178. container.appendChild( renderer.domElement );
  179. // STATS
  180. stats = new Stats();
  181. stats.domElement.style.position = 'absolute';
  182. stats.domElement.style.top = '0px';
  183. //container.appendChild( stats.domElement );
  184. // EVENTS
  185. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  186. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  187. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  188. document.addEventListener( 'keypress', onDocumentKeyPress, false );
  189. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  190. renderer.domElement.addEventListener( 'drop', onDrop, false );
  191. renderer.domElement.addEventListener("dragover", function (evt) {
  192. evt.preventDefault();
  193. }, false);
  194. document.getElementById( "color" ).addEventListener( 'click', function() {
  195. pointLight.color.setHSL( Math.random(), 1, 0.5 );
  196. hex = decimalToHex( pointLight.color.getHex() );
  197. updatePermalink();
  198. }, false );
  199. document.getElementById( "font" ).addEventListener( 'click', function() {
  200. fontIndex ++;
  201. if ( fontIndex >= reverseFontMap.length ) {
  202. fontIndex = 0;
  203. }
  204. font = reverseFontMap[fontIndex];
  205. refreshText();
  206. }, false );
  207. document.getElementById( "weight" ).addEventListener( 'click', function() {
  208. if ( weight == "bold" ) {
  209. weight = "normal";
  210. } else {
  211. weight = "bold";
  212. }
  213. refreshText();
  214. }, false );
  215. document.getElementById( "bevel" ).addEventListener( 'click', function() {
  216. bevelEnabled = !bevelEnabled;
  217. refreshText();
  218. }, false );
  219. document.getElementById( "postprocessing" ).addEventListener( 'click', function() {
  220. postprocessing.enabled = !postprocessing.enabled;
  221. updatePermalink();
  222. }, false );
  223. // POSTPROCESSING
  224. renderer.autoClear = false;
  225. var renderModel = new THREE.RenderPass( scene, camera );
  226. var effectBloom = new THREE.BloomPass( 0.25 );
  227. var effectFilm = new THREE.FilmPass( 0.5, 0.125, 2048, false );
  228. effectFXAA = new THREE.ShaderPass( THREE.FXAAShader );
  229. var width = window.innerWidth || 2;
  230. var height = window.innerHeight || 2;
  231. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / width, 1 / height );
  232. effectFilm.renderToScreen = true;
  233. composer = new THREE.EffectComposer( renderer );
  234. composer.addPass( renderModel );
  235. composer.addPass( effectFXAA );
  236. composer.addPass( effectBloom );
  237. composer.addPass( effectFilm );
  238. //
  239. window.addEventListener( 'resize', onWindowResize, false );
  240. }
  241. function onWindowResize() {
  242. windowHalfX = window.innerWidth / 2;
  243. windowHalfY = window.innerHeight / 2;
  244. camera.aspect = window.innerWidth / window.innerHeight;
  245. camera.updateProjectionMatrix();
  246. renderer.setSize( window.innerWidth, window.innerHeight );
  247. composer.reset();
  248. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / window.innerWidth, 1 / window.innerHeight );
  249. }
  250. //
  251. function boolToNum( b ) {
  252. return b ? 1 : 0;
  253. }
  254. function updatePermalink() {
  255. var link = hex + fontMap[ font ] + weightMap[ weight ] + boolToNum( postprocessing.enabled ) + boolToNum( bevelEnabled ) + "#" + encodeURI( text );
  256. permalink.href = "#" + link;
  257. window.location.hash = link;
  258. }
  259. function onDrop( e ) {
  260. e.preventDefault();
  261. var file = e.dataTransfer.files[0],
  262. reader = new FileReader();
  263. reader.onload = function ( event ) {
  264. console.log( event.target );
  265. eval( event.target.result );
  266. var index = 0;
  267. for ( f in THREE.FontUtils.faces ) {
  268. if ( ! fontMap[ f ] ) {
  269. fontMap[ f ] = reverseFontMap.length;
  270. reverseFontMap[ reverseFontMap.length ] = f;
  271. font = f;
  272. }
  273. };
  274. refreshText();
  275. };
  276. console.log(file);
  277. reader.readAsText(file);
  278. return false;
  279. };
  280. function onDocumentKeyDown( event ) {
  281. if ( firstLetter ) {
  282. firstLetter = false;
  283. text = "";
  284. }
  285. var keyCode = event.keyCode;
  286. // backspace
  287. if ( keyCode == 8 ) {
  288. event.preventDefault();
  289. text = text.substring( 0, text.length - 1 );
  290. refreshText();
  291. return false;
  292. }
  293. }
  294. function onDocumentKeyPress( event ) {
  295. var keyCode = event.which;
  296. // backspace
  297. if ( keyCode == 8 ) {
  298. event.preventDefault();
  299. } else {
  300. var ch = String.fromCharCode( keyCode );
  301. text += ch;
  302. refreshText();
  303. }
  304. }
  305. function createText() {
  306. textGeo = new THREE.TextGeometry( text, {
  307. size: size,
  308. height: height,
  309. curveSegments: curveSegments,
  310. font: font,
  311. weight: weight,
  312. style: style,
  313. bevelThickness: bevelThickness,
  314. bevelSize: bevelSize,
  315. bevelEnabled: bevelEnabled,
  316. material: 0,
  317. extrudeMaterial: 1
  318. });
  319. textGeo.computeBoundingBox();
  320. textGeo.computeVertexNormals();
  321. // "fix" side normals by removing z-component of normals for side faces
  322. // (this doesn't work well for beveled geometry as then we lose nice curvature around z-axis)
  323. if ( ! bevelEnabled ) {
  324. var triangleAreaHeuristics = 0.1 * ( height * size );
  325. for ( var i = 0; i < textGeo.faces.length; i ++ ) {
  326. var face = textGeo.faces[ i ];
  327. if ( face.materialIndex == 1 ) {
  328. for ( var j = 0; j < face.vertexNormals.length; j ++ ) {
  329. face.vertexNormals[ j ].z = 0;
  330. face.vertexNormals[ j ].normalize();
  331. }
  332. var va = textGeo.vertices[ face.a ];
  333. var vb = textGeo.vertices[ face.b ];
  334. var vc = textGeo.vertices[ face.c ];
  335. var s = THREE.GeometryUtils.triangleArea( va, vb, vc );
  336. if ( s > triangleAreaHeuristics ) {
  337. for ( var j = 0; j < face.vertexNormals.length; j ++ ) {
  338. face.vertexNormals[ j ].copy( face.normal );
  339. }
  340. }
  341. }
  342. }
  343. }
  344. var centerOffset = -0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
  345. textMesh1 = new THREE.Mesh( textGeo, material );
  346. textMesh1.position.x = centerOffset;
  347. textMesh1.position.y = hover;
  348. textMesh1.position.z = 0;
  349. textMesh1.rotation.x = 0;
  350. textMesh1.rotation.y = Math.PI * 2;
  351. group.add( textMesh1 );
  352. if ( mirror ) {
  353. textMesh2 = new THREE.Mesh( textGeo, material );
  354. textMesh2.position.x = centerOffset;
  355. textMesh2.position.y = -hover;
  356. textMesh2.position.z = height;
  357. textMesh2.rotation.x = Math.PI;
  358. textMesh2.rotation.y = Math.PI * 2;
  359. group.add( textMesh2 );
  360. }
  361. }
  362. function refreshText() {
  363. updatePermalink();
  364. group.remove( textMesh1 );
  365. if ( mirror ) group.remove( textMesh2 );
  366. if ( !text ) return;
  367. createText();
  368. }
  369. function onDocumentMouseDown( event ) {
  370. event.preventDefault();
  371. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  372. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  373. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  374. mouseXOnMouseDown = event.clientX - windowHalfX;
  375. targetRotationOnMouseDown = targetRotation;
  376. }
  377. function onDocumentMouseMove( event ) {
  378. mouseX = event.clientX - windowHalfX;
  379. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  380. }
  381. function onDocumentMouseUp( event ) {
  382. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  383. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  384. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  385. }
  386. function onDocumentMouseOut( event ) {
  387. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  388. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  389. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  390. }
  391. function onDocumentTouchStart( event ) {
  392. if ( event.touches.length == 1 ) {
  393. event.preventDefault();
  394. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  395. targetRotationOnMouseDown = targetRotation;
  396. }
  397. }
  398. function onDocumentTouchMove( event ) {
  399. if ( event.touches.length == 1 ) {
  400. event.preventDefault();
  401. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  402. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  403. }
  404. }
  405. //
  406. function animate() {
  407. requestAnimationFrame( animate );
  408. render();
  409. stats.update();
  410. }
  411. function render() {
  412. group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
  413. camera.lookAt( cameraTarget );
  414. renderer.clear();
  415. if ( postprocessing.enabled ) {
  416. composer.render( 0.05 );
  417. } else {
  418. renderer.render( scene, camera );
  419. }
  420. }
  421. </script>
  422. </body>
  423. </html>