webgl_buffergeometry_points_interleaved.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 5, 3500 );
  23. camera.position.z = 2750;
  24. scene = new THREE.Scene();
  25. scene.background = new THREE.Color( 0x050505 );
  26. scene.fog = new THREE.Fog( 0x050505, 2000, 3500 );
  27. //
  28. var particles = 500000;
  29. var geometry = new THREE.BufferGeometry();
  30. // create a generic buffer of binary data (a single particle has 16 bytes of data)
  31. var arrayBuffer = new ArrayBuffer( particles * 16 );
  32. // the following typed arrays share the same buffer
  33. var interleavedFloat32Buffer = new Float32Array( arrayBuffer );
  34. var interleavedUint8Buffer = new Uint8Array( arrayBuffer );
  35. //
  36. var color = new THREE.Color();
  37. var n = 1000, n2 = n / 2; // particles spread in the cube
  38. for ( var i = 0; i < interleavedFloat32Buffer.length; i += 4 ) {
  39. // position (first 12 bytes)
  40. var x = Math.random() * n - n2;
  41. var y = Math.random() * n - n2;
  42. var z = Math.random() * n - n2;
  43. interleavedFloat32Buffer[ i + 0 ] = x;
  44. interleavedFloat32Buffer[ i + 1 ] = y;
  45. interleavedFloat32Buffer[ i + 2 ] = z;
  46. // color (last 4 bytes)
  47. var vx = ( x / n ) + 0.5;
  48. var vy = ( y / n ) + 0.5;
  49. var vz = ( z / n ) + 0.5;
  50. color.setRGB( vx, vy, vz );
  51. var j = ( i + 3 ) * 4;
  52. interleavedUint8Buffer[ j + 0 ] = color.r * 255;
  53. interleavedUint8Buffer[ j + 1 ] = color.g * 255;
  54. interleavedUint8Buffer[ j + 2 ] = color.b * 255;
  55. interleavedUint8Buffer[ j + 3 ] = 0; // not needed
  56. }
  57. var interleavedBuffer32 = new THREE.InterleavedBuffer( interleavedFloat32Buffer, 4 );
  58. var interleavedBuffer8 = new THREE.InterleavedBuffer( interleavedUint8Buffer, 16 );
  59. geometry.setAttribute( 'position', new THREE.InterleavedBufferAttribute( interleavedBuffer32, 3, 0, false ) );
  60. geometry.setAttribute( 'color', new THREE.InterleavedBufferAttribute( interleavedBuffer8, 3, 12, true ) );
  61. //
  62. var material = new THREE.PointsMaterial( { size: 15, vertexColors: THREE.VertexColors } );
  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, false );
  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. var 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>