2
0

webgl_buffergeometry_glbufferattribute.html 4.3 KB

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