webgl_text.html 17 KB

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