webgl_interactive_points.html 5.1 KB

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