webgl_effects_ascii.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - effects - ascii</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. padding:0;
  10. margin:0;
  11. font-weight: bold;
  12. overflow:hidden;
  13. }
  14. #info {
  15. position: absolute;
  16. top: 0px; width: 100%;
  17. color: #ffffff;
  18. padding: 5px;
  19. font-family:Monospace;
  20. font-size:13px;
  21. text-align:center;
  22. z-index:1000;
  23. }
  24. a {
  25. color: #ff0000;
  26. }
  27. #webglmessage a { color:#da0 }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - effects - ascii</div>
  32. <script src="../build/three.js"></script>
  33. <script src="js/controls/TrackballControls.js"></script>
  34. <script src="js/effects/AsciiEffect.js"></script>
  35. <script src="js/WebGL.js"></script>
  36. <script>
  37. if ( WEBGL.isWebGLAvailable() === false ) {
  38. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  39. }
  40. var camera, controls, scene, renderer, effect;
  41. var sphere, plane;
  42. var start = Date.now();
  43. init();
  44. animate();
  45. function init() {
  46. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  47. camera.position.y = 150;
  48. camera.position.z = 500;
  49. controls = new THREE.TrackballControls( camera );
  50. scene = new THREE.Scene();
  51. var light = new THREE.PointLight( 0xffffff );
  52. light.position.set( 500, 500, 500 );
  53. scene.add( light );
  54. var light = new THREE.PointLight( 0xffffff, 0.25 );
  55. light.position.set( - 500, - 500, - 500 );
  56. scene.add( light );
  57. sphere = new THREE.Mesh( new THREE.SphereBufferGeometry( 200, 20, 10 ), new THREE.MeshPhongMaterial( { flatShading: true }) );
  58. scene.add( sphere );
  59. // Plane
  60. plane = new THREE.Mesh( new THREE.PlaneBufferGeometry( 400, 400 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
  61. plane.position.y = - 200;
  62. plane.rotation.x = - Math.PI / 2;
  63. scene.add( plane );
  64. renderer = new THREE.WebGLRenderer();
  65. renderer.setSize( window.innerWidth, window.innerHeight );
  66. effect = new THREE.AsciiEffect( renderer, ' .:-+*=%@#', { invert: true } );
  67. effect.setSize( window.innerWidth, window.innerHeight );
  68. effect.domElement.style.color = 'white';
  69. effect.domElement.style.backgroundColor = 'black';
  70. // Special case: append effect.domElement, instead of renderer.domElement.
  71. // AsciiEffect creates a custom domElement (a div container) where the ASCII elements are placed.
  72. document.body.appendChild( effect.domElement );
  73. //
  74. window.addEventListener( 'resize', onWindowResize, false );
  75. }
  76. function onWindowResize() {
  77. camera.aspect = window.innerWidth / window.innerHeight;
  78. camera.updateProjectionMatrix();
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. effect.setSize( window.innerWidth, window.innerHeight );
  81. }
  82. //
  83. function animate() {
  84. requestAnimationFrame( animate );
  85. render();
  86. }
  87. function render() {
  88. var timer = Date.now() - start;
  89. sphere.position.y = Math.abs( Math.sin( timer * 0.002 ) ) * 150;
  90. sphere.rotation.x = timer * 0.0003;
  91. sphere.rotation.z = timer * 0.0002;
  92. controls.update();
  93. effect.render( scene, camera );
  94. }
  95. </script>
  96. </body>
  97. </html>