webgl_buffergeometry_drawrange.html 8.0 KB

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