webgl_effects_ascii.html 3.2 KB

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