webgl_buffergeometry_glbufferattribute.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. <meta property="og:title" content="three.js webgl - buffergeometry - custom VBOs">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_buffergeometry_glbufferattribute.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_buffergeometry_glbufferattribute.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - buffergeometry - custom VBOs</div>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import Stats from 'three/addons/libs/stats.module.js';
  27. let container, stats;
  28. let camera, scene, renderer;
  29. let points;
  30. const particles = 300000;
  31. let drawCount = 10000;
  32. init();
  33. animate();
  34. function init() {
  35. container = document.getElementById( 'container' );
  36. //
  37. renderer = new THREE.WebGLRenderer();
  38. renderer.setPixelRatio( window.devicePixelRatio );
  39. renderer.setSize( window.innerWidth, window.innerHeight );
  40. renderer.setAnimationLoop( animate );
  41. container.appendChild( renderer.domElement );
  42. //
  43. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 5, 3500 );
  44. camera.position.z = 2750;
  45. scene = new THREE.Scene();
  46. scene.background = new THREE.Color( 0x050505 );
  47. scene.fog = new THREE.Fog( 0x050505, 2000, 3500 );
  48. //
  49. const geometry = new THREE.BufferGeometry();
  50. const positions = [];
  51. const positions2 = [];
  52. const colors = [];
  53. const color = new THREE.Color();
  54. const n = 1000, n2 = n / 2; // particles spread in the cube
  55. for ( let i = 0; i < particles; i ++ ) {
  56. // positions
  57. const x = Math.random() * n - n2;
  58. const y = Math.random() * n - n2;
  59. const z = Math.random() * n - n2;
  60. positions.push( x, y, z );
  61. positions2.push( z * 0.3, x * 0.3, y * 0.3 );
  62. // colors
  63. const vx = ( x / n ) + 0.5;
  64. const vy = ( y / n ) + 0.5;
  65. const vz = ( z / n ) + 0.5;
  66. color.setRGB( vx, vy, vz, THREE.SRGBColorSpace );
  67. const hex = color.getHex( THREE.LinearSRGBColorSpace );
  68. colors.push( hex >> 16 & 255, hex >> 8 & 255, hex & 255 );
  69. }
  70. const gl = renderer.getContext();
  71. const pos = gl.createBuffer();
  72. gl.bindBuffer( gl.ARRAY_BUFFER, pos );
  73. gl.bufferData( gl.ARRAY_BUFFER, new Float32Array( positions ), gl.STATIC_DRAW );
  74. const pos2 = gl.createBuffer();
  75. gl.bindBuffer( gl.ARRAY_BUFFER, pos2 );
  76. gl.bufferData( gl.ARRAY_BUFFER, new Float32Array( positions2 ), gl.STATIC_DRAW );
  77. const rgb = gl.createBuffer();
  78. gl.bindBuffer( gl.ARRAY_BUFFER, rgb );
  79. gl.bufferData( gl.ARRAY_BUFFER, new Uint8Array( colors ), gl.STATIC_DRAW );
  80. const posAttr1 = new THREE.GLBufferAttribute( pos, gl.FLOAT, 3, 4, particles );
  81. const posAttr2 = new THREE.GLBufferAttribute( pos2, gl.FLOAT, 3, 4, particles );
  82. geometry.setAttribute( 'position', posAttr1 );
  83. setInterval( function () {
  84. const attr = geometry.getAttribute( 'position' );
  85. geometry.setAttribute( 'position', ( attr === posAttr1 ) ? posAttr2 : posAttr1 );
  86. }, 2000 );
  87. geometry.setAttribute( 'color', new THREE.GLBufferAttribute( rgb, gl.UNSIGNED_BYTE, 3, 1, particles, true ) );
  88. //
  89. const material = new THREE.PointsMaterial( { size: 15, vertexColors: true } );
  90. points = new THREE.Points( geometry, material );
  91. geometry.boundingSphere = new THREE.Sphere().set( new THREE.Vector3(), 500 );
  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. drawCount = ( Math.max( 5000, drawCount ) + Math.floor( 500 * Math.random() ) ) % particles;
  107. points.geometry.setDrawRange( 0, drawCount );
  108. const time = Date.now() * 0.001;
  109. points.rotation.x = time * 0.1;
  110. points.rotation.y = time * 0.2;
  111. renderer.render( scene, camera );
  112. stats.update();
  113. }
  114. </script>
  115. </body>
  116. </html>
粤ICP备19079148号