webgl_buffergeometry_glbufferattribute.html 4.5 KB

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