webgl_text.html 17 KB

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