webgl_buffergeometry_drawrange.html 8.3 KB

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