webgl_buffergeometry_drawrange.html 8.1 KB

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