webgl_geometry_text.html 16 KB

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