webgl_buffergeometry_glbufferattribute.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - buffergeometry - custom VBOs</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 - custom VBOs</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. const particles = 300000;
  27. let drawCount = 10000;
  28. init();
  29. animate();
  30. function init() {
  31. container = document.getElementById( 'container' );
  32. //
  33. renderer = new THREE.WebGLRenderer();
  34. renderer.setPixelRatio( window.devicePixelRatio );
  35. renderer.setSize( window.innerWidth, window.innerHeight );
  36. renderer.setAnimationLoop( animate );
  37. container.appendChild( renderer.domElement );
  38. //
  39. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 5, 3500 );
  40. camera.position.z = 2750;
  41. scene = new THREE.Scene();
  42. scene.background = new THREE.Color( 0x050505 );
  43. scene.fog = new THREE.Fog( 0x050505, 2000, 3500 );
  44. //
  45. const geometry = new THREE.BufferGeometry();
  46. const positions = [];
  47. const positions2 = [];
  48. const colors = [];
  49. const color = new THREE.Color();
  50. const n = 1000, n2 = n / 2; // particles spread in the cube
  51. for ( let i = 0; i < particles; i ++ ) {
  52. // positions
  53. const x = Math.random() * n - n2;
  54. const y = Math.random() * n - n2;
  55. const z = Math.random() * n - n2;
  56. positions.push( x, y, z );
  57. positions2.push( z * 0.3, x * 0.3, y * 0.3 );
  58. // colors
  59. const vx = ( x / n ) + 0.5;
  60. const vy = ( y / n ) + 0.5;
  61. const vz = ( z / n ) + 0.5;
  62. color.setRGB( vx, vy, vz, THREE.SRGBColorSpace );
  63. colors.push( color.r, color.g, color.b );
  64. }
  65. const gl = renderer.getContext();
  66. const pos = gl.createBuffer();
  67. gl.bindBuffer( gl.ARRAY_BUFFER, pos );
  68. gl.bufferData( gl.ARRAY_BUFFER, new Float32Array( positions ), gl.STATIC_DRAW );
  69. const pos2 = gl.createBuffer();
  70. gl.bindBuffer( gl.ARRAY_BUFFER, pos2 );
  71. gl.bufferData( gl.ARRAY_BUFFER, new Float32Array( positions2 ), gl.STATIC_DRAW );
  72. const rgb = gl.createBuffer();
  73. gl.bindBuffer( gl.ARRAY_BUFFER, rgb );
  74. gl.bufferData( gl.ARRAY_BUFFER, new Float32Array( colors ), gl.STATIC_DRAW );
  75. const posAttr1 = new THREE.GLBufferAttribute( pos, gl.FLOAT, 3, 4, particles );
  76. const posAttr2 = new THREE.GLBufferAttribute( pos2, gl.FLOAT, 3, 4, particles );
  77. geometry.setAttribute( 'position', posAttr1 );
  78. setInterval( function () {
  79. const attr = geometry.getAttribute( 'position' );
  80. geometry.setAttribute( 'position', ( attr === posAttr1 ) ? posAttr2 : posAttr1 );
  81. }, 2000 );
  82. geometry.setAttribute( 'color', new THREE.GLBufferAttribute( rgb, gl.FLOAT, 3, 4, particles ) );
  83. //
  84. const material = new THREE.PointsMaterial( { size: 15, vertexColors: true } );
  85. points = new THREE.Points( geometry, material );
  86. geometry.boundingSphere = new THREE.Sphere().set( new THREE.Vector3(), 500 );
  87. scene.add( points );
  88. //
  89. stats = new Stats();
  90. container.appendChild( stats.dom );
  91. //
  92. window.addEventListener( 'resize', onWindowResize );
  93. }
  94. function onWindowResize() {
  95. camera.aspect = window.innerWidth / window.innerHeight;
  96. camera.updateProjectionMatrix();
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. }
  99. //
  100. function animate() {
  101. drawCount = ( Math.max( 5000, drawCount ) + Math.floor( 500 * Math.random() ) ) % particles;
  102. points.geometry.setDrawRange( 0, drawCount );
  103. const time = Date.now() * 0.001;
  104. points.rotation.x = time * 0.1;
  105. points.rotation.y = time * 0.2;
  106. renderer.render( scene, camera );
  107. stats.update();
  108. }
  109. </script>
  110. </body>
  111. </html>