webgl_trails.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. <script type="module">
  11. import * as THREE from '../build/three.module.js';
  12. import Stats from './jsm/libs/stats.module.js';
  13. var container, stats;
  14. var camera, scene, renderer;
  15. init();
  16. animate();
  17. function init() {
  18. container = document.createElement( 'div' );
  19. document.body.appendChild( container );
  20. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 10 );
  21. camera.position.set( 0, 0, 1 );
  22. scene = new THREE.Scene();
  23. var colorArray = [ new THREE.Color( 0xff0080 ), new THREE.Color( 0xffffff ), new THREE.Color( 0x8000ff ) ];
  24. var positions = [];
  25. var colors = [];
  26. for ( var i = 0; i < 100; i ++ ) {
  27. positions.push( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 );
  28. var clr = colorArray[ Math.floor( Math.random() * colorArray.length ) ];
  29. colors.push( clr.r, clr.g, clr.b );
  30. }
  31. var geometry = new THREE.BufferGeometry();
  32. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  33. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  34. var material = new THREE.PointsMaterial( { size: 4, vertexColors: THREE.VertexColors, depthTest: false, sizeAttenuation: false } );
  35. var mesh = new THREE.Points( geometry, material );
  36. scene.add( mesh );
  37. renderer = new THREE.WebGLRenderer( { preserveDrawingBuffer: true } );
  38. renderer.setPixelRatio( window.devicePixelRatio );
  39. renderer.setSize( window.innerWidth, window.innerHeight );
  40. renderer.autoClearColor = false;
  41. container.appendChild( renderer.domElement );
  42. stats = new Stats();
  43. container.appendChild( stats.dom );
  44. //
  45. window.addEventListener( 'resize', onWindowResize, false );
  46. }
  47. function onWindowResize() {
  48. camera.aspect = window.innerWidth / window.innerHeight;
  49. camera.updateProjectionMatrix();
  50. renderer.setSize( window.innerWidth, window.innerHeight );
  51. }
  52. //
  53. function animate() {
  54. requestAnimationFrame( animate );
  55. render();
  56. stats.update();
  57. }
  58. function render() {
  59. scene.rotation.y = Date.now() / 2000;
  60. renderer.render( scene, camera );
  61. }
  62. </script>
  63. </body>
  64. </html>