webgl_effects_ascii.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. let camera, controls, scene, renderer, effect;
  16. let sphere, plane;
  17. const 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. scene.background = new THREE.Color( 0, 0, 0 );
  26. const pointLight1 = new THREE.PointLight( 0xffffff );
  27. pointLight1.position.set( 500, 500, 500 );
  28. scene.add( pointLight1 );
  29. const pointLight2 = new THREE.PointLight( 0xffffff, 0.25 );
  30. pointLight2.position.set( - 500, - 500, - 500 );
  31. scene.add( pointLight2 );
  32. sphere = new THREE.Mesh( new THREE.SphereGeometry( 200, 20, 10 ), new THREE.MeshPhongMaterial( { flatShading: true } ) );
  33. scene.add( sphere );
  34. // Plane
  35. plane = new THREE.Mesh( new THREE.PlaneGeometry( 400, 400 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
  36. plane.position.y = - 200;
  37. plane.rotation.x = - Math.PI / 2;
  38. scene.add( plane );
  39. renderer = new THREE.WebGLRenderer();
  40. renderer.setSize( window.innerWidth, window.innerHeight );
  41. effect = new AsciiEffect( renderer, ' .:-+*=%@#', { invert: true } );
  42. effect.setSize( window.innerWidth, window.innerHeight );
  43. effect.domElement.style.color = 'white';
  44. effect.domElement.style.backgroundColor = 'black';
  45. // Special case: append effect.domElement, instead of renderer.domElement.
  46. // AsciiEffect creates a custom domElement (a div container) where the ASCII elements are placed.
  47. document.body.appendChild( effect.domElement );
  48. controls = new TrackballControls( camera, effect.domElement );
  49. //
  50. window.addEventListener( 'resize', onWindowResize );
  51. }
  52. function onWindowResize() {
  53. camera.aspect = window.innerWidth / window.innerHeight;
  54. camera.updateProjectionMatrix();
  55. renderer.setSize( window.innerWidth, window.innerHeight );
  56. effect.setSize( window.innerWidth, window.innerHeight );
  57. }
  58. //
  59. function animate() {
  60. requestAnimationFrame( animate );
  61. render();
  62. }
  63. function render() {
  64. const timer = Date.now() - start;
  65. sphere.position.y = Math.abs( Math.sin( timer * 0.002 ) ) * 150;
  66. sphere.rotation.x = timer * 0.0003;
  67. sphere.rotation.z = timer * 0.0002;
  68. controls.update();
  69. effect.render( scene, camera );
  70. }
  71. </script>
  72. </body>
  73. </html>