webgl_trails.html 2.5 KB

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