webgl_effects_ascii.html 3.3 KB

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