webgl_text.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
  7. <style type="text/css">
  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://github.com/mrdoob/three.js" 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="postprocessing">change postprocessing</span>,
  35. <a id="permalink" href="#">permalink</a>
  36. </div>
  37. <script type="text/javascript" src="../build/Three.js"></script>
  38. <script type="text/javascript" src="js/Detector.js"></script>
  39. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  40. <script type="text/javascript" src="js/Stats.js"></script>
  41. <!-- load the font file from canvas-text -->
  42. <!--
  43. -->
  44. <script type="text/javascript" src="fonts/gentilis_bold.typeface.js"></script>
  45. <script type="text/javascript" src="fonts/gentilis_regular.typeface.js"></script>
  46. <script type="text/javascript" src="fonts/optimer_bold.typeface.js"></script>
  47. <script type="text/javascript" src="fonts/optimer_regular.typeface.js"></script>
  48. <script type="text/javascript" src="fonts/helvetiker_bold.typeface.js"></script>
  49. <script type="text/javascript" src="fonts/helvetiker_regular.typeface.js"></script>
  50. <script type="text/javascript" src="fonts/droid/droid_sans_regular.typeface.js"></script>
  51. <script type="text/javascript" src="fonts/droid/droid_sans_bold.typeface.js"></script>
  52. <script type="text/javascript" src="fonts/droid/droid_serif_regular.typeface.js"></script>
  53. <script type="text/javascript" src="fonts/droid/droid_serif_bold.typeface.js"></script>
  54. <script type="text/javascript">
  55. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  56. var container, stats, permalink, hex, color;
  57. var camera, scene, renderer;
  58. var textMesh1, textMesh2, textGeo, textMaterial, parent;
  59. var firstLetter = true;
  60. var text = "three.js",
  61. height = 20,
  62. size = 80,
  63. hover = 30,
  64. curveSegments = 6,
  65. font = "optimer", // helvetiker, optimer, gentilis, droid sans, droid serif
  66. weight = "bold", // normal bold
  67. style = "normal"; // normal italic
  68. var mirror = true;
  69. var fontMap = {
  70. "helvetiker" : 0,
  71. "optimer" : 1,
  72. "gentilis" : 2,
  73. "droid sans" : 3,
  74. "droid serif" : 4
  75. };
  76. var weightMap = {
  77. "normal" : 0,
  78. "bold" : 1
  79. }
  80. var reverseFontMap = {};
  81. var reverseWeightMap = {};
  82. for ( var i in fontMap ) reverseFontMap[ fontMap[i] ] = i;
  83. for ( var i in weightMap ) reverseWeightMap[ weightMap[i] ] = i;
  84. var targetRotation = 0;
  85. var targetRotationOnMouseDown = 0;
  86. var mouseX = 0;
  87. var mouseXOnMouseDown = 0;
  88. var windowHalfX = window.innerWidth / 2;
  89. var windowHalfY = window.innerHeight / 2;
  90. var postprocessing = { enabled : true };
  91. var glow = 0.9;
  92. init();
  93. animate();
  94. function capitalize( txt ) {
  95. return txt.substring( 0, 1 ).toUpperCase() + txt.substring( 1 );
  96. }
  97. function decimalToHex( d ) {
  98. var hex = Number( d ).toString( 16 );
  99. hex = "000000".substr( 0, 6 - hex.length ) + hex;
  100. return hex.toUpperCase();
  101. }
  102. function init() {
  103. container = document.createElement( 'div' );
  104. document.body.appendChild( container );
  105. permalink = document.getElementById( "permalink" );
  106. camera = new THREE.Camera( 30, window.innerWidth / window.innerHeight, 1, 1500 );
  107. camera.position.y = 400;
  108. camera.position.z = 700;
  109. camera.target.position.y = 100;
  110. scene = new THREE.Scene();
  111. scene.fog = new THREE.Fog( 0x000000, 250, 1400 );
  112. var dirLight = new THREE.DirectionalLight( 0xffffff, 0.125 );
  113. dirLight.position.set( 0, 0, 1 );
  114. dirLight.position.normalize();
  115. scene.addLight( dirLight );
  116. var pointLight = new THREE.PointLight( 0xffffff, 1.5 );
  117. pointLight.position.set( 0, 100, 50 );
  118. scene.addLight( pointLight );
  119. //text = capitalize( font ) + " " + capitalize( weight );
  120. //text = "abcdefghijklmnopqrstuvwxyz0123456789";
  121. //text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  122. // Get text from hash
  123. var hash = document.location.hash.substr( 1 );
  124. if ( hash.length !== 0 ) {
  125. var colorhash = hash.substring( 0, 6 );
  126. var fonthash = hash.substring( 6, 7 );
  127. var weighthash = hash.substring( 7, 8 );
  128. var pphash = hash.substring( 8, 9 );
  129. var texthash = hash.substring( 10 );
  130. hex = colorhash;
  131. pointLight.color.setHex( parseInt( colorhash, 16 ) );
  132. font = reverseFontMap[ parseInt( fonthash ) ];
  133. weight = reverseWeightMap[ parseInt( weighthash ) ];
  134. postprocessing.enabled = parseInt( pphash );
  135. text = decodeURI( texthash );
  136. updatePermalink();
  137. } else {
  138. pointLight.color.setHSV( Math.random(), 0.95, 0.85 );
  139. pointLight.color.updateHex();
  140. hex = decimalToHex( pointLight.color.hex );
  141. }
  142. textGeo = new THREE.Text( text, {
  143. size: size,
  144. height: height,
  145. curveSegments: curveSegments,
  146. font: font,
  147. weight: weight,
  148. style: style
  149. });
  150. textMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, wireframe: false } );
  151. parent = new THREE.Object3D();
  152. textMesh1 = new THREE.Mesh( textGeo, textMaterial );
  153. textMesh1.position.x = 0;
  154. textMesh1.position.y = hover;
  155. textMesh1.position.z = 0;
  156. textMesh1.rotation.x = 0;
  157. textMesh1.rotation.y = Math.PI * 2;
  158. parent.addChild( textMesh1 );
  159. if ( mirror ) {
  160. textMesh2 = new THREE.Mesh( textGeo, textMaterial );
  161. textMesh2.position.x = 0;
  162. textMesh2.position.y = -hover;
  163. textMesh2.position.z = height;
  164. textMesh2.rotation.x = Math.PI;
  165. textMesh2.rotation.y = Math.PI * 2;
  166. parent.addChild( textMesh2 );
  167. }
  168. parent.position.y = 100;
  169. scene.addChild( parent );
  170. var plane = new THREE.Mesh( new THREE.Plane( 10000, 10000 ), new THREE.MeshBasicMaterial( { color: 0xffffff, opacity: 0.5, transparent: true } ) );
  171. plane.rotation.x = -1.57;
  172. plane.position.y = 100;
  173. scene.addChild( plane );
  174. renderer = new THREE.WebGLRenderer( { antialias: false } );
  175. renderer.setSize( window.innerWidth, window.innerHeight );
  176. renderer.setClearColor( scene.fog.color, 1 );
  177. container.appendChild( renderer.domElement );
  178. stats = new Stats();
  179. stats.domElement.style.position = 'absolute';
  180. stats.domElement.style.top = '0px';
  181. //container.appendChild( stats.domElement );
  182. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  183. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  184. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  185. document.addEventListener( 'keypress', onDocumentKeyPress, false );
  186. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  187. document.getElementById( "color" ).addEventListener( 'click', function() {
  188. pointLight.color.setHSV( Math.random(), 0.95, 0.85 );
  189. pointLight.color.updateHex();
  190. hex = decimalToHex( pointLight.color.hex );
  191. updatePermalink();
  192. }, false );
  193. document.getElementById( "font" ).addEventListener( 'click', function() {
  194. if ( font == "helvetiker" ) {
  195. font = "optimer";
  196. } else if ( font == "optimer" ) {
  197. font = "gentilis";
  198. } else if ( font == "gentilis" ) {
  199. font = "droid sans";
  200. } else if ( font == "droid sans" ) {
  201. font = "droid serif";
  202. } else {
  203. font = "helvetiker";
  204. }
  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( "postprocessing" ).addEventListener( 'click', function() {
  216. postprocessing.enabled = !postprocessing.enabled;
  217. updatePermalink();
  218. }, false );
  219. initPostprocessing();
  220. renderer.autoClear = false;
  221. }
  222. //
  223. function boolToNum( b ) {
  224. return b ? 1 : 0;
  225. }
  226. function updatePermalink() {
  227. var link = hex + fontMap[ font ] + weightMap[ weight ] + boolToNum( postprocessing.enabled ) + "#" + encodeURI( text );
  228. permalink.href = "#" + link;
  229. window.location.hash = link;
  230. }
  231. function onDocumentKeyDown( event ) {
  232. if ( firstLetter ) {
  233. firstLetter = false;
  234. text = "";
  235. }
  236. var keyCode = event.keyCode;
  237. // backspace
  238. if ( keyCode == 8 ) {
  239. event.preventDefault();
  240. text = text.substring( 0, text.length - 1 );
  241. refreshText();
  242. return false;
  243. }
  244. }
  245. function onDocumentKeyPress( event ) {
  246. var keyCode = event.which;
  247. // backspace
  248. if ( keyCode == 8 ) {
  249. event.preventDefault();
  250. } else {
  251. var ch = String.fromCharCode( keyCode );
  252. text += ch;
  253. refreshText();
  254. }
  255. }
  256. function refreshText() {
  257. updatePermalink();
  258. scene.removeChild( textMesh1 );
  259. textGeo = new THREE.Text( text, {
  260. size: size,
  261. height: height,
  262. curveSegments: curveSegments,
  263. font: font,
  264. weight: weight,
  265. style: style
  266. });
  267. textMesh1 = new THREE.Mesh( textGeo, textMaterial );
  268. textMesh1.position.x = 0;
  269. textMesh1.position.y = hover;
  270. textMesh1.position.z = 0;
  271. textMesh1.rotation.x = 0;
  272. textMesh1.rotation.y = Math.PI * 2;
  273. parent.addChild( textMesh1 );
  274. if ( mirror ) {
  275. scene.removeChild( textMesh2 );
  276. textMesh2 = new THREE.Mesh( textGeo, textMaterial );
  277. textMesh2.position.x = 0;
  278. textMesh2.position.y = -hover;
  279. textMesh2.position.z = height;
  280. textMesh2.rotation.x = Math.PI;
  281. textMesh2.rotation.y = Math.PI * 2;
  282. parent.addChild( textMesh2 );
  283. }
  284. }
  285. function onDocumentMouseDown( event ) {
  286. event.preventDefault();
  287. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  288. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  289. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  290. mouseXOnMouseDown = event.clientX - windowHalfX;
  291. targetRotationOnMouseDown = targetRotation;
  292. }
  293. function onDocumentMouseMove( event ) {
  294. mouseX = event.clientX - windowHalfX;
  295. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  296. }
  297. function onDocumentMouseUp( event ) {
  298. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  299. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  300. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  301. }
  302. function onDocumentMouseOut( event ) {
  303. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  304. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  305. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  306. }
  307. function onDocumentTouchStart( event ) {
  308. if ( event.touches.length == 1 ) {
  309. event.preventDefault();
  310. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  311. targetRotationOnMouseDown = targetRotation;
  312. }
  313. }
  314. function onDocumentTouchMove( event ) {
  315. if ( event.touches.length == 1 ) {
  316. event.preventDefault();
  317. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  318. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  319. }
  320. }
  321. //
  322. function initPostprocessing() {
  323. postprocessing.scene = new THREE.Scene();
  324. postprocessing.camera = new THREE.Camera();
  325. postprocessing.camera.projectionMatrix = THREE.Matrix4.makeOrtho( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -10000, 10000 );
  326. postprocessing.camera.position.z = 100;
  327. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat };
  328. postprocessing.rtTexture1 = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
  329. postprocessing.rtTexture2 = new THREE.WebGLRenderTarget( 512, 512, pars );
  330. postprocessing.rtTexture3 = new THREE.WebGLRenderTarget( 512, 512, pars );
  331. var screen_shader = THREE.ShaderUtils.lib["screen"];
  332. var screen_uniforms = THREE.UniformsUtils.clone( screen_shader.uniforms );
  333. screen_uniforms["tDiffuse"].texture = postprocessing.rtTexture1;
  334. screen_uniforms["opacity"].value = 1.0;
  335. postprocessing.materialScreen = new THREE.MeshShaderMaterial( {
  336. uniforms: screen_uniforms,
  337. vertexShader: screen_shader.vertexShader,
  338. fragmentShader: screen_shader.fragmentShader,
  339. blending: THREE.AdditiveBlending,
  340. transparent: true
  341. } );
  342. var convolution_shader = THREE.ShaderUtils.lib["convolution"];
  343. var convolution_uniforms = THREE.UniformsUtils.clone( convolution_shader.uniforms );
  344. postprocessing.blurx = new THREE.Vector2( 0.001953125, 0.0 ),
  345. postprocessing.blury = new THREE.Vector2( 0.0, 0.001953125 );
  346. convolution_uniforms["tDiffuse"].texture = postprocessing.rtTexture1;
  347. convolution_uniforms["uImageIncrement"].value = postprocessing.blurx;
  348. convolution_uniforms["cKernel"].value = THREE.ShaderUtils.buildKernel( 4.0 );
  349. postprocessing.materialConvolution = new THREE.MeshShaderMaterial( {
  350. uniforms: convolution_uniforms,
  351. vertexShader: "#define KERNEL_SIZE 25.0\n" + convolution_shader.vertexShader,
  352. fragmentShader: "#define KERNEL_SIZE 25\n" + convolution_shader.fragmentShader
  353. } );
  354. var film_shader = THREE.ShaderUtils.lib["film"];
  355. var film_uniforms = THREE.UniformsUtils.clone( film_shader.uniforms );
  356. film_uniforms["tDiffuse"].texture = postprocessing.rtTexture1;
  357. postprocessing.materialFilm = new THREE.MeshShaderMaterial( { uniforms: film_uniforms, vertexShader: film_shader.vertexShader, fragmentShader: film_shader.fragmentShader } );
  358. postprocessing.materialFilm.uniforms.grayscale.value = 0;
  359. postprocessing.materialFilm.uniforms.nIntensity.value = 0.15;
  360. postprocessing.materialFilm.uniforms.sIntensity.value = 0.25;
  361. postprocessing.materialFilm.uniforms.sCount.value = 2048;
  362. //postprocessing.materialFilm.uniforms.nIntensity.value = 0;
  363. //postprocessing.materialFilm.uniforms.sIntensity.value = 0;
  364. postprocessing.materialScreen.uniforms.opacity.value = glow;
  365. postprocessing.quad = new THREE.Mesh( new THREE.Plane( window.innerWidth, window.innerHeight ), postprocessing.materialConvolution );
  366. postprocessing.quad.position.z = - 500;
  367. postprocessing.scene.addObject( postprocessing.quad );
  368. }
  369. //
  370. function animate() {
  371. requestAnimationFrame( animate );
  372. render();
  373. stats.update();
  374. }
  375. var delta, time, oldTime;
  376. function render() {
  377. if ( ! oldTime ) oldTime = new Date().getTime();
  378. time = new Date().getTime();
  379. delta = 0.1 * ( time - oldTime );
  380. oldTime = time;
  381. parent.rotation.y += ( targetRotation - parent.rotation.y ) * 0.05;
  382. if ( postprocessing.enabled ) {
  383. renderer.clear();
  384. // Render scene into texture
  385. renderer.render( scene, camera, postprocessing.rtTexture1, true );
  386. // Render quad with blured scene into texture (convolution pass 1)
  387. postprocessing.quad.materials[ 0 ] = postprocessing.materialConvolution;
  388. postprocessing.materialConvolution.uniforms.tDiffuse.texture = postprocessing.rtTexture1;
  389. postprocessing.materialConvolution.uniforms.uImageIncrement.value = postprocessing.blurx;
  390. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture2, true );
  391. // Render quad with blured scene into texture (convolution pass 2)
  392. postprocessing.materialConvolution.uniforms.tDiffuse.texture = postprocessing.rtTexture2;
  393. postprocessing.materialConvolution.uniforms.uImageIncrement.value = postprocessing.blury;
  394. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture3, true );
  395. // Render original scene with superimposed blur to texture
  396. postprocessing.quad.materials[ 0 ] = postprocessing.materialScreen;
  397. postprocessing.materialScreen.uniforms.tDiffuse.texture = postprocessing.rtTexture3;
  398. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture1, false );
  399. // Render to screen
  400. postprocessing.materialFilm.uniforms.time.value += 0.01;
  401. postprocessing.quad.materials[ 0 ] = postprocessing.materialFilm;
  402. postprocessing.materialScreen.uniforms.tDiffuse.texture = postprocessing.rtTexture1;
  403. renderer.render( postprocessing.scene, postprocessing.camera );
  404. } else {
  405. renderer.clear();
  406. renderer.render( scene, camera );
  407. }
  408. }
  409. </script>
  410. </body>
  411. </html>