webgl_effects_ascii.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - effects - ascii</div>
  11. <script type="module">
  12. import * as THREE from '../build/three.module.js';
  13. import { AsciiEffect } from './jsm/effects/AsciiEffect.js';
  14. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  15. var camera, controls, scene, renderer, effect;
  16. var sphere, plane;
  17. var start = Date.now();
  18. init();
  19. animate();
  20. function init() {
  21. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  22. camera.position.y = 150;
  23. camera.position.z = 500;
  24. scene = new THREE.Scene();
  25. var light = new THREE.PointLight( 0xffffff );
  26. light.position.set( 500, 500, 500 );
  27. scene.add( light );
  28. var light = new THREE.PointLight( 0xffffff, 0.25 );
  29. light.position.set( - 500, - 500, - 500 );
  30. scene.add( light );
  31. sphere = new THREE.Mesh( new THREE.SphereBufferGeometry( 200, 20, 10 ), new THREE.MeshPhongMaterial( { flatShading: true } ) );
  32. scene.add( sphere );
  33. // Plane
  34. plane = new THREE.Mesh( new THREE.PlaneBufferGeometry( 400, 400 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
  35. plane.position.y = - 200;
  36. plane.rotation.x = - Math.PI / 2;
  37. scene.add( plane );
  38. renderer = new THREE.WebGLRenderer();
  39. renderer.setSize( window.innerWidth, window.innerHeight );
  40. effect = new AsciiEffect( renderer, ' .:-+*=%@#', { invert: true } );
  41. effect.setSize( window.innerWidth, window.innerHeight );
  42. effect.domElement.style.color = 'white';
  43. effect.domElement.style.backgroundColor = 'black';
  44. // Special case: append effect.domElement, instead of renderer.domElement.
  45. // AsciiEffect creates a custom domElement (a div container) where the ASCII elements are placed.
  46. document.body.appendChild( effect.domElement );
  47. controls = new TrackballControls( camera, effect.domElement );
  48. //
  49. window.addEventListener( 'resize', onWindowResize, false );
  50. }
  51. function onWindowResize() {
  52. camera.aspect = window.innerWidth / window.innerHeight;
  53. camera.updateProjectionMatrix();
  54. renderer.setSize( window.innerWidth, window.innerHeight );
  55. effect.setSize( window.innerWidth, window.innerHeight );
  56. }
  57. //
  58. function animate() {
  59. requestAnimationFrame( animate );
  60. render();
  61. }
  62. function render() {
  63. var timer = Date.now() - start;
  64. sphere.position.y = Math.abs( Math.sin( timer * 0.002 ) ) * 150;
  65. sphere.rotation.x = timer * 0.0003;
  66. sphere.rotation.z = timer * 0.0002;
  67. controls.update();
  68. effect.render( scene, camera );
  69. }
  70. </script>
  71. </body>
  72. </html>