2
0

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. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import Stats from './jsm/libs/stats.module.js';
  28. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  29. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  30. let group;
  31. let container, stats;
  32. const particlesData = [];
  33. let camera, scene, renderer;
  34. let positions, colors;
  35. let particles;
  36. let pointCloud;
  37. let particlePositions;
  38. let linesMesh;
  39. const maxParticleCount = 1000;
  40. let particleCount = 500;
  41. const r = 800;
  42. const rHalf = r / 2;
  43. const effectController = {
  44. showDots: true,
  45. showLines: true,
  46. minDistance: 150,
  47. limitConnections: false,
  48. maxConnections: 20,
  49. particleCount: 500
  50. };
  51. init();
  52. animate();
  53. function initGUI() {
  54. const gui = new GUI();
  55. gui.add( effectController, 'showDots' ).onChange( function ( value ) {
  56. pointCloud.visible = value;
  57. } );
  58. gui.add( effectController, 'showLines' ).onChange( function ( value ) {
  59. linesMesh.visible = value;
  60. } );
  61. gui.add( effectController, 'minDistance', 10, 300 );
  62. gui.add( effectController, 'limitConnections' );
  63. gui.add( effectController, 'maxConnections', 0, 30, 1 );
  64. gui.add( effectController, 'particleCount', 0, maxParticleCount, 1 ).onChange( function ( value ) {
  65. particleCount = parseInt( value );
  66. particles.setDrawRange( 0, particleCount );
  67. } );
  68. }
  69. function init() {
  70. initGUI();
  71. container = document.getElementById( 'container' );
  72. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 4000 );
  73. camera.position.z = 1750;
  74. const controls = new OrbitControls( camera, container );
  75. controls.minDistance = 1000;
  76. controls.maxDistance = 3000;
  77. scene = new THREE.Scene();
  78. group = new THREE.Group();
  79. scene.add( group );
  80. const helper = new THREE.BoxHelper( new THREE.Mesh( new THREE.BoxGeometry( r, r, r ) ) );
  81. helper.material.color.setHex( 0x101010 );
  82. helper.material.blending = THREE.AdditiveBlending;
  83. helper.material.transparent = true;
  84. group.add( helper );
  85. const segments = maxParticleCount * maxParticleCount;
  86. positions = new Float32Array( segments * 3 );
  87. colors = new Float32Array( segments * 3 );
  88. const pMaterial = new THREE.PointsMaterial( {
  89. color: 0xFFFFFF,
  90. size: 3,
  91. blending: THREE.AdditiveBlending,
  92. transparent: true,
  93. sizeAttenuation: false
  94. } );
  95. particles = new THREE.BufferGeometry();
  96. particlePositions = new Float32Array( maxParticleCount * 3 );
  97. for ( let i = 0; i < maxParticleCount; i ++ ) {
  98. const x = Math.random() * r - r / 2;
  99. const y = Math.random() * r - r / 2;
  100. const z = Math.random() * r - r / 2;
  101. particlePositions[ i * 3 ] = x;
  102. particlePositions[ i * 3 + 1 ] = y;
  103. particlePositions[ i * 3 + 2 ] = z;
  104. // add it to the geometry
  105. particlesData.push( {
  106. velocity: new THREE.Vector3( - 1 + Math.random() * 2, - 1 + Math.random() * 2, - 1 + Math.random() * 2 ),
  107. numConnections: 0
  108. } );
  109. }
  110. particles.setDrawRange( 0, particleCount );
  111. particles.setAttribute( 'position', new THREE.BufferAttribute( particlePositions, 3 ).setUsage( THREE.DynamicDrawUsage ) );
  112. // create the particle system
  113. pointCloud = new THREE.Points( particles, pMaterial );
  114. group.add( pointCloud );
  115. const geometry = new THREE.BufferGeometry();
  116. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ).setUsage( THREE.DynamicDrawUsage ) );
  117. geometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3 ).setUsage( THREE.DynamicDrawUsage ) );
  118. geometry.computeBoundingSphere();
  119. geometry.setDrawRange( 0, 0 );
  120. const material = new THREE.LineBasicMaterial( {
  121. vertexColors: true,
  122. blending: THREE.AdditiveBlending,
  123. transparent: true
  124. } );
  125. linesMesh = new THREE.LineSegments( geometry, material );
  126. group.add( linesMesh );
  127. //
  128. renderer = new THREE.WebGLRenderer( { antialias: true } );
  129. renderer.setPixelRatio( window.devicePixelRatio );
  130. renderer.setSize( window.innerWidth, window.innerHeight );
  131. renderer.outputEncoding = THREE.sRGBEncoding;
  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>