webgl_buffergeometry_points.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - buffergeometry - particles</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="container"></div>
  11. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - buffergeometry - particles</div>
  12. <script type="importmap">
  13. {
  14. "imports": {
  15. "three": "../build/three.module.js",
  16. "three/addons/": "./jsm/"
  17. }
  18. }
  19. </script>
  20. <script type="module">
  21. import * as THREE from 'three';
  22. import Stats from 'three/addons/libs/stats.module.js';
  23. let container, stats;
  24. let camera, scene, renderer;
  25. let points;
  26. init();
  27. animate();
  28. function init() {
  29. container = document.getElementById( 'container' );
  30. //
  31. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 5, 3500 );
  32. camera.position.z = 2750;
  33. scene = new THREE.Scene();
  34. scene.background = new THREE.Color( 0x050505 );
  35. scene.fog = new THREE.Fog( 0x050505, 2000, 3500 );
  36. //
  37. const particles = 500000;
  38. const geometry = new THREE.BufferGeometry();
  39. const positions = [];
  40. const colors = [];
  41. const color = new THREE.Color();
  42. const n = 1000, n2 = n / 2; // particles spread in the cube
  43. for ( let i = 0; i < particles; i ++ ) {
  44. // positions
  45. const x = Math.random() * n - n2;
  46. const y = Math.random() * n - n2;
  47. const z = Math.random() * n - n2;
  48. positions.push( x, y, z );
  49. // colors
  50. const vx = ( x / n ) + 0.5;
  51. const vy = ( y / n ) + 0.5;
  52. const vz = ( z / n ) + 0.5;
  53. color.setRGB( vx, vy, vz, THREE.SRGBColorSpace );
  54. colors.push( color.r, color.g, color.b );
  55. }
  56. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  57. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  58. geometry.computeBoundingSphere();
  59. //
  60. const material = new THREE.PointsMaterial( { size: 15, vertexColors: true } );
  61. points = new THREE.Points( geometry, material );
  62. scene.add( points );
  63. //
  64. renderer = new THREE.WebGLRenderer();
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. renderer.setAnimationLoop( animate );
  68. container.appendChild( renderer.domElement );
  69. //
  70. stats = new Stats();
  71. container.appendChild( stats.dom );
  72. //
  73. window.addEventListener( 'resize', onWindowResize );
  74. }
  75. function onWindowResize() {
  76. camera.aspect = window.innerWidth / window.innerHeight;
  77. camera.updateProjectionMatrix();
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. }
  80. //
  81. function animate() {
  82. const time = Date.now() * 0.001;
  83. points.rotation.x = time * 0.25;
  84. points.rotation.y = time * 0.5;
  85. renderer.render( scene, camera );
  86. stats.update();
  87. }
  88. </script>
  89. </body>
  90. </html>