webgl_interactive_points.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. <!-- Import maps polyfill -->
  35. <!-- Remove this when import maps will be widely supported -->
  36. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  37. <script type="importmap">
  38. {
  39. "imports": {
  40. "three": "../build/three.module.js"
  41. }
  42. }
  43. </script>
  44. <script type="module">
  45. import * as THREE from 'three';
  46. import Stats from './jsm/libs/stats.module.js';
  47. import * as BufferGeometryUtils from './jsm/utils/BufferGeometryUtils.js';
  48. let renderer, scene, camera, stats;
  49. let particles;
  50. const PARTICLE_SIZE = 20;
  51. let raycaster, intersects;
  52. let pointer, INTERSECTED;
  53. init();
  54. animate();
  55. function init() {
  56. const container = document.getElementById( 'container' );
  57. scene = new THREE.Scene();
  58. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
  59. camera.position.z = 250;
  60. //
  61. let boxGeometry = new THREE.BoxGeometry( 200, 200, 200, 16, 16, 16 );
  62. // if normal and uv attributes are not removed, mergeVertices() can't consolidate indentical vertices with different normal/uv data
  63. boxGeometry.deleteAttribute( 'normal' );
  64. boxGeometry.deleteAttribute( 'uv' );
  65. boxGeometry = BufferGeometryUtils.mergeVertices( boxGeometry );
  66. //
  67. const positionAttribute = boxGeometry.getAttribute( 'position' );
  68. const colors = [];
  69. const sizes = [];
  70. const color = new THREE.Color();
  71. for ( let i = 0, l = positionAttribute.count; i < l; i ++ ) {
  72. color.setHSL( 0.01 + 0.1 * ( i / l ), 1.0, 0.5 );
  73. color.toArray( colors, i * 3 );
  74. sizes[ i ] = PARTICLE_SIZE * 0.5;
  75. }
  76. const geometry = new THREE.BufferGeometry();
  77. geometry.setAttribute( 'position', positionAttribute );
  78. geometry.setAttribute( 'customColor', new THREE.Float32BufferAttribute( colors, 3 ) );
  79. geometry.setAttribute( 'size', new THREE.Float32BufferAttribute( sizes, 1 ) );
  80. //
  81. const material = new THREE.ShaderMaterial( {
  82. uniforms: {
  83. color: { value: new THREE.Color( 0xffffff ) },
  84. pointTexture: { value: new THREE.TextureLoader().load( 'textures/sprites/disc.png' ) },
  85. alphaTest: { value: 0.9 }
  86. },
  87. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  88. fragmentShader: document.getElementById( 'fragmentshader' ).textContent
  89. } );
  90. //
  91. particles = new THREE.Points( geometry, material );
  92. scene.add( particles );
  93. //
  94. renderer = new THREE.WebGLRenderer();
  95. renderer.setPixelRatio( window.devicePixelRatio );
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. container.appendChild( renderer.domElement );
  98. //
  99. raycaster = new THREE.Raycaster();
  100. pointer = new THREE.Vector2();
  101. //
  102. stats = new Stats();
  103. container.appendChild( stats.dom );
  104. //
  105. window.addEventListener( 'resize', onWindowResize );
  106. document.addEventListener( 'pointermove', onPointerMove );
  107. }
  108. function onPointerMove( event ) {
  109. pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  110. pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  111. }
  112. function onWindowResize() {
  113. camera.aspect = window.innerWidth / window.innerHeight;
  114. camera.updateProjectionMatrix();
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  116. }
  117. function animate() {
  118. requestAnimationFrame( animate );
  119. render();
  120. stats.update();
  121. }
  122. function render() {
  123. particles.rotation.x += 0.0005;
  124. particles.rotation.y += 0.001;
  125. const geometry = particles.geometry;
  126. const attributes = geometry.attributes;
  127. raycaster.setFromCamera( pointer, camera );
  128. intersects = raycaster.intersectObject( particles );
  129. if ( intersects.length > 0 ) {
  130. if ( INTERSECTED != intersects[ 0 ].index ) {
  131. attributes.size.array[ INTERSECTED ] = PARTICLE_SIZE;
  132. INTERSECTED = intersects[ 0 ].index;
  133. attributes.size.array[ INTERSECTED ] = PARTICLE_SIZE * 1.25;
  134. attributes.size.needsUpdate = true;
  135. }
  136. } else if ( INTERSECTED !== null ) {
  137. attributes.size.array[ INTERSECTED ] = PARTICLE_SIZE;
  138. attributes.size.needsUpdate = true;
  139. INTERSECTED = null;
  140. }
  141. renderer.render( scene, camera );
  142. }
  143. </script>
  144. </body>
  145. </html>