webgl_effects_ascii.html 3.3 KB

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