webgl_interactive_points.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - interactive particles</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"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - interactive - particles</div>
  12. <script type="x-shader/x-vertex" id="vertexshader">
  13. attribute float size;
  14. attribute vec3 customColor;
  15. varying vec3 vColor;
  16. void main() {
  17. vColor = customColor;
  18. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  19. gl_PointSize = size * ( 300.0 / -mvPosition.z );
  20. gl_Position = projectionMatrix * mvPosition;
  21. }
  22. </script>
  23. <script type="x-shader/x-fragment" id="fragmentshader">
  24. uniform vec3 color;
  25. uniform sampler2D pointTexture;
  26. uniform float alphaTest;
  27. varying vec3 vColor;
  28. void main() {
  29. gl_FragColor = vec4( color * vColor, 1.0 );
  30. gl_FragColor = gl_FragColor * texture2D( pointTexture, gl_PointCoord );
  31. if ( gl_FragColor.a < alphaTest ) discard;
  32. }
  33. </script>
  34. <script type="importmap">
  35. {
  36. "imports": {
  37. "three": "../build/three.module.js",
  38. "three/addons/": "./jsm/"
  39. }
  40. }
  41. </script>
  42. <script type="module">
  43. import * as THREE from 'three';
  44. import Stats from 'three/addons/libs/stats.module.js';
  45. import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';
  46. let renderer, scene, camera, stats;
  47. let particles;
  48. const PARTICLE_SIZE = 20;
  49. let raycaster, intersects;
  50. let pointer, INTERSECTED;
  51. init();
  52. animate();
  53. function init() {
  54. const container = document.getElementById( 'container' );
  55. scene = new THREE.Scene();
  56. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
  57. camera.position.z = 250;
  58. //
  59. let boxGeometry = new THREE.BoxGeometry( 200, 200, 200, 16, 16, 16 );
  60. // if normal and uv attributes are not removed, mergeVertices() can't consolidate indentical vertices with different normal/uv data
  61. boxGeometry.deleteAttribute( 'normal' );
  62. boxGeometry.deleteAttribute( 'uv' );
  63. boxGeometry = BufferGeometryUtils.mergeVertices( boxGeometry );
  64. //
  65. const positionAttribute = boxGeometry.getAttribute( 'position' );
  66. const colors = [];
  67. const sizes = [];
  68. const color = new THREE.Color();
  69. for ( let i = 0, l = positionAttribute.count; i < l; i ++ ) {
  70. color.setHSL( 0.01 + 0.1 * ( i / l ), 1.0, 0.5 );
  71. color.toArray( colors, i * 3 );
  72. sizes[ i ] = PARTICLE_SIZE * 0.5;
  73. }
  74. const geometry = new THREE.BufferGeometry();
  75. geometry.setAttribute( 'position', positionAttribute );
  76. geometry.setAttribute( 'customColor', new THREE.Float32BufferAttribute( colors, 3 ) );
  77. geometry.setAttribute( 'size', new THREE.Float32BufferAttribute( sizes, 1 ) );
  78. //
  79. const material = new THREE.ShaderMaterial( {
  80. uniforms: {
  81. color: { value: new THREE.Color( 0xffffff ) },
  82. pointTexture: { value: new THREE.TextureLoader().load( 'textures/sprites/disc.png' ) },
  83. alphaTest: { value: 0.9 }
  84. },
  85. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  86. fragmentShader: document.getElementById( 'fragmentshader' ).textContent
  87. } );
  88. //
  89. particles = new THREE.Points( geometry, material );
  90. scene.add( particles );
  91. //
  92. renderer = new THREE.WebGLRenderer();
  93. renderer.setPixelRatio( window.devicePixelRatio );
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. container.appendChild( renderer.domElement );
  96. //
  97. raycaster = new THREE.Raycaster();
  98. pointer = new THREE.Vector2();
  99. //
  100. stats = new Stats();
  101. container.appendChild( stats.dom );
  102. //
  103. window.addEventListener( 'resize', onWindowResize );
  104. document.addEventListener( 'pointermove', onPointerMove );
  105. }
  106. function onPointerMove( event ) {
  107. pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  108. pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  109. }
  110. function onWindowResize() {
  111. camera.aspect = window.innerWidth / window.innerHeight;
  112. camera.updateProjectionMatrix();
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. }
  115. function animate() {
  116. requestAnimationFrame( animate );
  117. render();
  118. stats.update();
  119. }
  120. function render() {
  121. particles.rotation.x += 0.0005;
  122. particles.rotation.y += 0.001;
  123. const geometry = particles.geometry;
  124. const attributes = geometry.attributes;
  125. raycaster.setFromCamera( pointer, camera );
  126. intersects = raycaster.intersectObject( particles );
  127. if ( intersects.length > 0 ) {
  128. if ( INTERSECTED != intersects[ 0 ].index ) {
  129. attributes.size.array[ INTERSECTED ] = PARTICLE_SIZE;
  130. INTERSECTED = intersects[ 0 ].index;
  131. attributes.size.array[ INTERSECTED ] = PARTICLE_SIZE * 1.25;
  132. attributes.size.needsUpdate = true;
  133. }
  134. } else if ( INTERSECTED !== null ) {
  135. attributes.size.array[ INTERSECTED ] = PARTICLE_SIZE;
  136. attributes.size.needsUpdate = true;
  137. INTERSECTED = null;
  138. }
  139. renderer.render( scene, camera );
  140. }
  141. </script>
  142. </body>
  143. </html>