2
0

webgl_buffergeometry_points.html 3.2 KB

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