webgl_geometry_text.html 15 KB

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