webgl_buffergeometry_points.html 2.9 KB

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