webgl_effects_ascii.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. function init() {
  28. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  29. camera.position.y = 150;
  30. camera.position.z = 500;
  31. scene = new THREE.Scene();
  32. scene.background = new THREE.Color( 0, 0, 0 );
  33. const pointLight1 = new THREE.PointLight( 0xffffff, 3, 0, 0 );
  34. pointLight1.position.set( 500, 500, 500 );
  35. scene.add( pointLight1 );
  36. const pointLight2 = new THREE.PointLight( 0xffffff, 1, 0, 0 );
  37. pointLight2.position.set( - 500, - 500, - 500 );
  38. scene.add( pointLight2 );
  39. sphere = new THREE.Mesh( new THREE.SphereGeometry( 200, 20, 10 ), new THREE.MeshPhongMaterial( { flatShading: true } ) );
  40. scene.add( sphere );
  41. // Plane
  42. plane = new THREE.Mesh( new THREE.PlaneGeometry( 400, 400 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
  43. plane.position.y = - 200;
  44. plane.rotation.x = - Math.PI / 2;
  45. scene.add( plane );
  46. renderer = new THREE.WebGLRenderer();
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  48. renderer.setAnimationLoop( animate );
  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. const timer = Date.now() - start;
  69. sphere.position.y = Math.abs( Math.sin( timer * 0.002 ) ) * 150;
  70. sphere.rotation.x = timer * 0.0003;
  71. sphere.rotation.z = timer * 0.0002;
  72. controls.update();
  73. effect.render( scene, camera );
  74. }
  75. </script>
  76. </body>
  77. </html>