2
0

webgl_buffergeometry_glbufferattribute.html 4.2 KB

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