webgl_buffergeometry_drawcalls.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - buffergeometry - lines drawcalls</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. <style>
  9. a {
  10. color: #08f;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div id="container"></div>
  16. <div id="info">
  17. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - buffergeometry drawcalls<br/>
  18. by <a href="https://twitter.com/fernandojsg">fernandojsg</a>
  19. </div>
  20. <script type="module">
  21. import {
  22. AdditiveBlending,
  23. BoxBufferGeometry,
  24. BoxHelper,
  25. BufferAttribute,
  26. BufferGeometry,
  27. Group,
  28. LineBasicMaterial,
  29. LineSegments,
  30. Mesh,
  31. PerspectiveCamera,
  32. Points,
  33. PointsMaterial,
  34. Scene,
  35. Vector3,
  36. VertexColors,
  37. WebGLRenderer
  38. } from "../build/three.module.js";
  39. import Stats from './jsm/libs/stats.module.js';
  40. import { GUI } from './jsm/libs/dat.gui.module.js';
  41. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  42. var group;
  43. var container, stats;
  44. var particlesData = [];
  45. var camera, scene, renderer;
  46. var positions, colors;
  47. var particles;
  48. var pointCloud;
  49. var particlePositions;
  50. var linesMesh;
  51. var maxParticleCount = 1000;
  52. var particleCount = 500;
  53. var r = 800;
  54. var rHalf = r / 2;
  55. var effectController = {
  56. showDots: true,
  57. showLines: true,
  58. minDistance: 150,
  59. limitConnections: false,
  60. maxConnections: 20,
  61. particleCount: 500
  62. };
  63. init();
  64. animate();
  65. function initGUI() {
  66. var gui = new GUI();
  67. gui.add( effectController, "showDots" ).onChange( function ( value ) {
  68. pointCloud.visible = value;
  69. } );
  70. gui.add( effectController, "showLines" ).onChange( function ( value ) {
  71. linesMesh.visible = value;
  72. } );
  73. gui.add( effectController, "minDistance", 10, 300 );
  74. gui.add( effectController, "limitConnections" );
  75. gui.add( effectController, "maxConnections", 0, 30, 1 );
  76. gui.add( effectController, "particleCount", 0, maxParticleCount, 1 ).onChange( function ( value ) {
  77. particleCount = parseInt( value );
  78. particles.setDrawRange( 0, particleCount );
  79. } );
  80. }
  81. function init() {
  82. initGUI();
  83. container = document.getElementById( 'container' );
  84. camera = new PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 4000 );
  85. camera.position.z = 1750;
  86. var controls = new OrbitControls( camera, container );
  87. scene = new Scene();
  88. group = new Group();
  89. scene.add( group );
  90. var helper = new BoxHelper( new Mesh( new BoxBufferGeometry( r, r, r ) ) );
  91. helper.material.color.setHex( 0x101010 );
  92. helper.material.blending = AdditiveBlending;
  93. helper.material.transparent = true;
  94. group.add( helper );
  95. var segments = maxParticleCount * maxParticleCount;
  96. positions = new Float32Array( segments * 3 );
  97. colors = new Float32Array( segments * 3 );
  98. var pMaterial = new PointsMaterial( {
  99. color: 0xFFFFFF,
  100. size: 3,
  101. blending: AdditiveBlending,
  102. transparent: true,
  103. sizeAttenuation: false
  104. } );
  105. particles = new BufferGeometry();
  106. particlePositions = new Float32Array( maxParticleCount * 3 );
  107. for ( var i = 0; i < maxParticleCount; i ++ ) {
  108. var x = Math.random() * r - r / 2;
  109. var y = Math.random() * r - r / 2;
  110. var z = Math.random() * r - r / 2;
  111. particlePositions[ i * 3 ] = x;
  112. particlePositions[ i * 3 + 1 ] = y;
  113. particlePositions[ i * 3 + 2 ] = z;
  114. // add it to the geometry
  115. particlesData.push( {
  116. velocity: new Vector3( - 1 + Math.random() * 2, - 1 + Math.random() * 2, - 1 + Math.random() * 2 ),
  117. numConnections: 0
  118. } );
  119. }
  120. particles.setDrawRange( 0, particleCount );
  121. particles.addAttribute( 'position', new BufferAttribute( particlePositions, 3 ).setDynamic( true ) );
  122. // create the particle system
  123. pointCloud = new Points( particles, pMaterial );
  124. group.add( pointCloud );
  125. var geometry = new BufferGeometry();
  126. geometry.addAttribute( 'position', new BufferAttribute( positions, 3 ).setDynamic( true ) );
  127. geometry.addAttribute( 'color', new BufferAttribute( colors, 3 ).setDynamic( true ) );
  128. geometry.computeBoundingSphere();
  129. geometry.setDrawRange( 0, 0 );
  130. var material = new LineBasicMaterial( {
  131. vertexColors: VertexColors,
  132. blending: AdditiveBlending,
  133. transparent: true
  134. } );
  135. linesMesh = new LineSegments( geometry, material );
  136. group.add( linesMesh );
  137. //
  138. renderer = new WebGLRenderer( { antialias: true } );
  139. renderer.setPixelRatio( window.devicePixelRatio );
  140. renderer.setSize( window.innerWidth, window.innerHeight );
  141. renderer.gammaInput = true;
  142. renderer.gammaOutput = true;
  143. container.appendChild( renderer.domElement );
  144. //
  145. stats = new Stats();
  146. container.appendChild( stats.dom );
  147. window.addEventListener( 'resize', onWindowResize, false );
  148. }
  149. function onWindowResize() {
  150. camera.aspect = window.innerWidth / window.innerHeight;
  151. camera.updateProjectionMatrix();
  152. renderer.setSize( window.innerWidth, window.innerHeight );
  153. }
  154. function animate() {
  155. var vertexpos = 0;
  156. var colorpos = 0;
  157. var numConnected = 0;
  158. for ( var i = 0; i < particleCount; i ++ )
  159. particlesData[ i ].numConnections = 0;
  160. for ( var i = 0; i < particleCount; i ++ ) {
  161. // get the particle
  162. var particleData = particlesData[ i ];
  163. particlePositions[ i * 3 ] += particleData.velocity.x;
  164. particlePositions[ i * 3 + 1 ] += particleData.velocity.y;
  165. particlePositions[ i * 3 + 2 ] += particleData.velocity.z;
  166. if ( particlePositions[ i * 3 + 1 ] < - rHalf || particlePositions[ i * 3 + 1 ] > rHalf )
  167. particleData.velocity.y = - particleData.velocity.y;
  168. if ( particlePositions[ i * 3 ] < - rHalf || particlePositions[ i * 3 ] > rHalf )
  169. particleData.velocity.x = - particleData.velocity.x;
  170. if ( particlePositions[ i * 3 + 2 ] < - rHalf || particlePositions[ i * 3 + 2 ] > rHalf )
  171. particleData.velocity.z = - particleData.velocity.z;
  172. if ( effectController.limitConnections && particleData.numConnections >= effectController.maxConnections )
  173. continue;
  174. // Check collision
  175. for ( var j = i + 1; j < particleCount; j ++ ) {
  176. var particleDataB = particlesData[ j ];
  177. if ( effectController.limitConnections && particleDataB.numConnections >= effectController.maxConnections )
  178. continue;
  179. var dx = particlePositions[ i * 3 ] - particlePositions[ j * 3 ];
  180. var dy = particlePositions[ i * 3 + 1 ] - particlePositions[ j * 3 + 1 ];
  181. var dz = particlePositions[ i * 3 + 2 ] - particlePositions[ j * 3 + 2 ];
  182. var dist = Math.sqrt( dx * dx + dy * dy + dz * dz );
  183. if ( dist < effectController.minDistance ) {
  184. particleData.numConnections ++;
  185. particleDataB.numConnections ++;
  186. var alpha = 1.0 - dist / effectController.minDistance;
  187. positions[ vertexpos ++ ] = particlePositions[ i * 3 ];
  188. positions[ vertexpos ++ ] = particlePositions[ i * 3 + 1 ];
  189. positions[ vertexpos ++ ] = particlePositions[ i * 3 + 2 ];
  190. positions[ vertexpos ++ ] = particlePositions[ j * 3 ];
  191. positions[ vertexpos ++ ] = particlePositions[ j * 3 + 1 ];
  192. positions[ vertexpos ++ ] = particlePositions[ j * 3 + 2 ];
  193. colors[ colorpos ++ ] = alpha;
  194. colors[ colorpos ++ ] = alpha;
  195. colors[ colorpos ++ ] = alpha;
  196. colors[ colorpos ++ ] = alpha;
  197. colors[ colorpos ++ ] = alpha;
  198. colors[ colorpos ++ ] = alpha;
  199. numConnected ++;
  200. }
  201. }
  202. }
  203. linesMesh.geometry.setDrawRange( 0, numConnected * 2 );
  204. linesMesh.geometry.attributes.position.needsUpdate = true;
  205. linesMesh.geometry.attributes.color.needsUpdate = true;
  206. pointCloud.geometry.attributes.position.needsUpdate = true;
  207. requestAnimationFrame( animate );
  208. stats.update();
  209. render();
  210. }
  211. function render() {
  212. var time = Date.now() * 0.001;
  213. group.rotation.y = time * 0.1;
  214. renderer.render( scene, camera );
  215. }
  216. </script>
  217. </body>
  218. </html>