webgl_trails.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - trails</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. <!-- Import maps polyfill -->
  11. <!-- Remove this when import maps will be widely supported -->
  12. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. <script type="module">
  21. import * as THREE from 'three';
  22. import Stats from './jsm/libs/stats.module.js';
  23. let stats;
  24. let camera, scene, renderer, clock;
  25. init();
  26. animate();
  27. function init() {
  28. const container = document.createElement( 'div' );
  29. document.body.appendChild( container );
  30. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 10 );
  31. camera.position.set( 0, 0, 1 );
  32. clock = new THREE.Clock();
  33. scene = new THREE.Scene();
  34. const colorArray = [ new THREE.Color( 0xff0080 ), new THREE.Color( 0xffffff ), new THREE.Color( 0x8000ff ) ];
  35. const positions = [];
  36. const colors = [];
  37. for ( let i = 0; i < 100; i ++ ) {
  38. positions.push( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 );
  39. const clr = colorArray[ Math.floor( Math.random() * colorArray.length ) ];
  40. colors.push( clr.r, clr.g, clr.b );
  41. }
  42. const geometry = new THREE.BufferGeometry();
  43. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  44. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  45. const material = new THREE.PointsMaterial( { size: 4, vertexColors: true, depthTest: false, sizeAttenuation: false } );
  46. const mesh = new THREE.Points( geometry, material );
  47. scene.add( mesh );
  48. renderer = new THREE.WebGLRenderer( { preserveDrawingBuffer: true } );
  49. renderer.setPixelRatio( window.devicePixelRatio );
  50. renderer.setSize( window.innerWidth, window.innerHeight );
  51. renderer.autoClearColor = false;
  52. container.appendChild( renderer.domElement );
  53. stats = new Stats();
  54. container.appendChild( stats.dom );
  55. //
  56. window.addEventListener( 'resize', onWindowResize );
  57. }
  58. function onWindowResize() {
  59. camera.aspect = window.innerWidth / window.innerHeight;
  60. camera.updateProjectionMatrix();
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. }
  63. //
  64. function animate() {
  65. requestAnimationFrame( animate );
  66. render();
  67. stats.update();
  68. }
  69. function render() {
  70. const elapsedTime = clock.getElapsedTime();
  71. scene.rotation.y = elapsedTime * 0.5;
  72. renderer.render( scene, camera );
  73. }
  74. </script>
  75. </body>
  76. </html>