webgl_buffergeometry_points.html 3.3 KB

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