webgl_geometry_text.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas/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: #000000;
  11. /*f0f0f0*/
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. position: absolute;
  17. top: 10px;
  18. width: 100%;
  19. padding: 5px;
  20. text-align: center;
  21. color: white;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="info">
  27. Drag to spin the text
  28. <br/>Simple Dynamic 3D Text WebGL Example by <a href="http://www.lab4games.net/zz85/blog">zz85</a>
  29. </div>
  30. <!--
  31. <script type="text/javascript" src="http://mrdoob.github.com/three.js/build/Three.js"></script>
  32. -->
  33. <script type="text/javascript" src="../build/Three.js"></script>
  34. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  35. <script type="text/javascript" src="js/Stats.js"></script>
  36. <link href="js/gui/gui.css" media="screen" rel="stylesheet" type="text/css" />
  37. <script type="text/javascript" src="js/gui/gui.min.js"></script>
  38. <script type="text/javascript" src="../src/extras/geometries/Text.js"></script>
  39. <!--
  40. http://canvas-text.googlecode.com/svn-history/r41/trunk/faces/dejavu_serif-normal-normal.js
  41. optimer-normal-normal.js
  42. gentilis-normal-normal
  43. http://canvas-text.googlecode.com/svn-history/r41/trunk/faces/helvetiker-normal-normal.js
  44. http://canvas-text.googlecode.com/svn-history/r41/trunk/faces/dejavu_sans-normal-normal.js
  45. http://canvas-text.googlecode.com/svn-history/r41/trunk/faces/optimer-normal-normal.js
  46. http://canvas-text.googlecode.com/svn-history/r41/trunk/faces/gentilis-normal-normal.js
  47. optimer-bold-normal
  48. -->
  49. <script type="text/javascript" src="http://canvas-text.googlecode.com/svn-history/r41/trunk/faces/gentilis-normal-normal.js"></script>
  50. <script type="text/javascript" src="js/helvetiker-normal-normal.js"></script>
  51. <script type="text/javascript">
  52. var container, stats;
  53. var camera, scene, renderer;
  54. var text, plane;
  55. var targetRotation = 0;
  56. var targetRotationOnMouseDown = 0;
  57. var mouseX = 0;
  58. var mouseXOnMouseDown = 0;
  59. var windowHalfX = window.innerWidth / 2;
  60. var windowHalfY = window.innerHeight / 2;
  61. var mouseY = 0;
  62. var materials;
  63. init();
  64. animate();
  65. function init() {
  66. //ThreeFont.face = 'optimer';
  67. container = document.createElement( 'div' );
  68. document.body.appendChild( container );
  69. camera = new THREE.Camera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  70. var originalCamPos = new THREE.Vector3(0,150,500);
  71. camera.position = originalCamPos;
  72. //camera.position.z = 500;
  73. //camera.target.position.y = 150;
  74. scene = new THREE.Scene();
  75. //THREE.FontUtils.weight = "bold";
  76. // Materials
  77. var materials = [];
  78. for ( var i = 0; i < 6; i ++ ) {
  79. materials.push( [ new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff , wireframe:false} ) ] );
  80. }
  81. var generatedTexture = new THREE.Texture( generateTexture() );
  82. generatedTexture.needsUpdate = true;
  83. //var
  84. materials = [];
  85. materials.push( new THREE.MeshLambertMaterial( { map: generatedTexture } ) );
  86. materials.push( new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.FlatShading } ) );
  87. materials.push( new THREE.MeshPhongMaterial( { ambient: 0x030303, color: 0xdddddd, specular: 0x009900, shininess: 30, shading: THREE.FlatShading } ) );
  88. materials.push( new THREE.MeshNormalMaterial( ) );
  89. materials.push( new THREE.MeshBasicMaterial( { color: 0x665500, blending: THREE.AdditiveBlending } ) );
  90. materials.push( new THREE.MeshBasicMaterial( { color: 0xff0000, blending: THREE.SubtractiveBlending } ) );
  91. materials.push( new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.SmoothShading } ) );
  92. materials.push( new THREE.MeshPhongMaterial( { ambient: 0x030303, color: 0xdddddd, specular: 0x009900, shininess: 30, shading: THREE.SmoothShading } ) );
  93. materials.push( new THREE.MeshNormalMaterial( { shading: THREE.SmoothShading } ) );
  94. materials.push( new THREE.MeshBasicMaterial( { color: 0xffaa00, wireframe: true } ) );
  95. materials.push( new THREE.MeshDepthMaterial() );
  96. materials.push( new THREE.MeshBasicMaterial( { map: generatedTexture } ) );
  97. function generateTexture() {
  98. var canvas = document.createElement( 'canvas' );
  99. canvas.width = 256;
  100. canvas.height = 256;
  101. var context = canvas.getContext( '2d' );
  102. var image = context.getImageData( 0, 0, 256, 256 );
  103. var x = 0, y = 0;
  104. for ( var i = 0, j = 0, l = image.data.length; i < l; i += 4, j ++ ) {
  105. x = j % 256;
  106. y = x == 0 ? y + 1 : y;
  107. image.data[ i + 2 ] = Math.floor( x ^ y );
  108. image.data[ i + 3 ] = 255;
  109. }
  110. context.putImageData( image, 0, 0 );
  111. return canvas;
  112. }
  113. var theText = "B"; //Hello three.js :)
  114. var hash = document.location.hash.substr(1);
  115. if (hash.length === 0) {
  116. } else {
  117. theText = hash;
  118. }
  119. textParams = {
  120. size: 80,
  121. height:40,
  122. curveSegments:2,
  123. font: "gentilis" //gentilis helvetiker
  124. };
  125. //var
  126. text3d = new THREE.Text(theText, textParams);//dejavu sans
  127. //MEsh Normal MeshBasic
  128. //MeshPhongMaterial MeshLambertMaterial MeshPhongMaterial
  129. // new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff, wireframe:false} )
  130. //text = new THREE.Mesh( text3d, materials[6] ); materials[3]
  131. //new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff, wireframe:false } )
  132. // Math.random() *
  133. //0xf0f0f0
  134. /*
  135. * color: <hex>,
  136. * ambient: <hex>,
  137. * specular: <hex>,
  138. * shininess: <float>,2
  139. * opacity: <float>,
  140. */
  141. var textMaterial = new THREE.MeshPhongMaterial( { color: 0xa0a0a0, wireframe:false, opacity:1,shininess:100,ambient:0xff0000 , specular:0x00ff00,reflectivity:100});
  142. textMaterial = materials[2];
  143. text = new THREE.Mesh( text3d, textMaterial);
  144. text.overdraw = true;
  145. text.doubleSided = false;
  146. text.position.y = 15;
  147. text.position.z = 0;
  148. text.rotation.x = 0*Math.PI;
  149. text.rotation.y = Math.PI*2;
  150. text.overdraw = false;
  151. scene.addObject( text );
  152. //scene.fog = new THREE.Fog( 0xffffff, 1, 10000 );
  153. ambientLight = new THREE.AmbientLight( 0xffffff ) ;
  154. scene.addLight(ambientLight );
  155. pointLight = new THREE.PointLight( 0xffffff, 1 );
  156. pointLight.position.x = 0;
  157. pointLight.position.y = 150;
  158. pointLight.position.z = 450;
  159. scene.addLight( pointLight );
  160. //Math.random() *
  161. directionalLight = new THREE.DirectionalLight( 0xffffff );
  162. /*
  163. directionalLight.position.x = Math.random() - 0.5;
  164. directionalLight.position.y = Math.random() - 0.5;
  165. directionalLight.position.z = Math.random() - 0.5;
  166. */
  167. directionalLight.position.x = 50;
  168. directionalLight.position.y = 50;
  169. directionalLight.position.z = 50;
  170. directionalLight.position.normalize();
  171. directionalLight.lookAt(text);
  172. directionalLight.castShadow = true;
  173. //intensity, distance, castShadow
  174. scene.addLight( directionalLight );
  175. //0x202020
  176. // Plane
  177. // ,reflectivity:1,refractionRatio:0
  178. //, shading:THREE.FlatShading
  179. //new THREE.MeshBasicMaterial( { color: 0xe0e0e0, wireframe:false} )
  180. //new THREE.MeshPhongMaterial( { color: 0xf0f0f0, wireframe:false,shininess:100})
  181. plane = new THREE.Mesh( new THREE.Plane( 800, 800 ), textMaterial );
  182. plane.rotation.x = - 90 * ( Math.PI / 180 );
  183. plane.position.x = 0;
  184. plane.overdraw = true;
  185. scene.addObject( plane );
  186. renderer = new THREE.WebGLRenderer({antialias:true});
  187. renderer.setSize( window.innerWidth, window.innerHeight );
  188. container.appendChild( renderer.domElement );
  189. stats = new Stats();
  190. stats.domElement.style.position = 'absolute';
  191. stats.domElement.style.top = '0px';
  192. container.appendChild( stats.domElement );
  193. //mousedown
  194. document.addEventListener( 'mousemove', onDocumentMouseDown, false );
  195. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  196. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  197. var effectController = {
  198. focus: 1.0,
  199. aperture: 0.025,
  200. maxblur: 1.0,
  201. ambientLightColor: 0.5,
  202. directionLightX: 1,
  203. directionLightY: 1,
  204. directionLightZ: 1,
  205. pointLightX: 0,
  206. pointLightY: 1,
  207. pointLightZ: 1,
  208. pointLightDistance: 0,
  209. pointLightIntensity:-1,
  210. text: theText,
  211. wireframe: false
  212. };
  213. var textChanger = function() {
  214. console.log(effectController.text);
  215. /*
  216. text3d.set(effectController.text);
  217. text3d.__dirtyVertices = true;
  218. text3d.__dirtyNormals = true;
  219. text3d.__dirtyColors = true;
  220. text3d.__dirtyElements = true;
  221. text3d.__dirtyMorphTargets = true;
  222. text3d.__dirtyNormals = true;
  223. text3d.__dirtyTangents = true;
  224. text3d.__dirtyUvs = true;
  225. text3d.__dirtyVertices = true;
  226. renderer.clear();
  227. text.matrixAutoUpdate = true;
  228. text.updateMatrix();
  229. */
  230. scene.removeChild(text);
  231. text3d = new THREE.Text(effectController.text, textParams);
  232. text = new THREE.Mesh( text3d, textMaterial)
  233. scene.addChild(text);
  234. }
  235. var matChanger = function( ) {
  236. //postprocessing.bokeh_uniforms["focus"].value = effectController.focus;
  237. //postprocessing.bokeh_uniforms["aperture"].value = effectController.aperture;
  238. //postprocessing.bokeh_uniforms["maxblur"].value = effectController.maxblur;\
  239. ambientLight.color.setHex(effectController.ambientLightColor*0xffffff);
  240. directionalLight.position.x = 50 * effectController.directionLightX;
  241. directionalLight.position.y = 50 * effectController.directionLightY;
  242. directionalLight.position.z = 50 * effectController.directionLightZ;
  243. pointLight.position.x = 500 * effectController.pointLightX;
  244. pointLight.position.y = 500 * effectController.pointLightY;
  245. pointLight.position.z = 500 * effectController.pointLightZ;
  246. pointLight.distance = effectController.pointLightDistance ;
  247. pointLight.intensity = effectController.pointLightIntensity ;
  248. };
  249. var gui = new GUI();
  250. //gui.add( effectController, "focus", 0.0, 3.0, 0.025 ).onChange( matChanger );
  251. //gui.add( effectController, "aperture", 0.001, 0.2, 0.001 ).onChange( matChanger );
  252. //gui.add( effectController, "maxblur", 0.0, 3.0, 0.025 ).onChange( matChanger );
  253. gui.add( effectController, "text").onChange( textChanger );
  254. gui.add( effectController, "wireframe");
  255. gui.add( effectController, "ambientLightColor", 0.0, 1.0, 0.025 ).onChange( matChanger );
  256. gui.add( effectController, "directionLightX", -1, 1.0, 0.025 ).onChange( matChanger );
  257. gui.add( effectController, "directionLightY", -1, 1.0, 0.025 ).onChange( matChanger );
  258. gui.add( effectController, "directionLightZ", -1, 1.0, 0.025 ).onChange( matChanger );
  259. gui.add( effectController, "pointLightX", -1, 1.0, 0.025 ).onChange( matChanger );
  260. gui.add( effectController, "pointLightY", -1, 1.0, 0.025 ).onChange( matChanger );
  261. gui.add( effectController, "pointLightZ", -1, 1.0, 0.025 ).onChange( matChanger );
  262. gui.add( effectController, "pointLightDistance", -1, 1000, 0.5).onChange( matChanger );
  263. gui.add( effectController, "pointLightIntensity", -1, 1.0, 0.025 ).onChange( matChanger );
  264. gui.show();
  265. matChanger();
  266. }
  267. //
  268. function onDocumentMouseDown( event ) {
  269. event.preventDefault();
  270. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  271. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  272. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  273. mouseXOnMouseDown = event.clientX - windowHalfX;
  274. targetRotationOnMouseDown = targetRotation;
  275. }
  276. function onDocumentMouseMove( event ) {
  277. mouseX = event.clientX - windowHalfX;
  278. mouseY = event.clientY - windowHalfY;
  279. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  280. }
  281. function onDocumentMouseUp( event ) {
  282. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  283. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  284. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  285. }
  286. function onDocumentMouseOut( event ) {
  287. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  288. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  289. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  290. }
  291. function onDocumentTouchStart( event ) {
  292. if ( event.touches.length == 1 ) {
  293. event.preventDefault();
  294. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  295. targetRotationOnMouseDown = targetRotation;
  296. }
  297. }
  298. function onDocumentTouchMove( event ) {
  299. if ( event.touches.length == 1 ) {
  300. event.preventDefault();
  301. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  302. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  303. }
  304. }
  305. //
  306. function animate() {
  307. requestAnimationFrame( animate );
  308. render();
  309. stats.update();
  310. }
  311. function render() {
  312. //console.log("targetRotation",targetRotation,text.rotation.y );
  313. //plane.rotation.z =
  314. //text.rotation.y += ( targetRotation - text.rotation.y ) * 0.05;
  315. //camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  316. //camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  317. //camera.position.y += ( - mouseY + 200 - camera.position.y ) * .05;
  318. //camera.position.z;
  319. //camera.useTarget = false;
  320. //camera.rotation.z += ( targetRotation - camera.rotation.z ) * 0.05;
  321. //camera.rotation.y += ( targetRotation - camera.rotation.y ) * 0.05;
  322. //renderer.clear();
  323. camera.position.x = Math.cos(
  324. (mouseX - windowHalfX)/windowHalfX * Math.PI + Math.PI/2
  325. ) * 500 + camera.target.position.x;
  326. camera.position.z = -Math.sin(
  327. (mouseX - windowHalfX)/windowHalfX * Math.PI + Math.PI/2
  328. ) * 500;
  329. // + camera.target.position.z + Math.sin(
  330. //(mouseY - windowHalfY)/windowHalfY * Math.PI + Math.PI/2
  331. //) * 500
  332. /*
  333. camera.position.y = Math.cos(
  334. (mouseY - windowHalfY)/windowHalfY * Math.PI + Math.PI/2
  335. ) * 500 + camera.target.position.y;
  336. */
  337. camera.position.y = (windowHalfY - mouseY)/windowHalfY *200 + camera.target.position.y;
  338. //
  339. renderer.render( scene, camera );
  340. // Camera movement
  341. // Camera rotation
  342. // Text angle
  343. // Text rotation
  344. // Keydowns
  345. // Settings
  346. // Materials
  347. }
  348. </script>
  349. <script>
  350. var _gaq=[["_setAccount","UA-7549263-1"],["_trackPageview"]];
  351. (function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.async=1;
  352. g.src=("https:"==location.protocol?"//ssl":"//www")+".google-analytics.com/ga.js";
  353. s.parentNode.insertBefore(g,s)}(document,"script"));
  354. </script>
  355. </body>
  356. </html>