1
0

webgl_interactive_points.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. function init() {
  53. const container = document.getElementById( 'container' );
  54. scene = new THREE.Scene();
  55. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
  56. camera.position.z = 250;
  57. //
  58. let boxGeometry = new THREE.BoxGeometry( 200, 200, 200, 16, 16, 16 );
  59. // if normal and uv attributes are not removed, mergeVertices() can't consolidate indentical vertices with different normal/uv data
  60. boxGeometry.deleteAttribute( 'normal' );
  61. boxGeometry.deleteAttribute( 'uv' );
  62. boxGeometry = BufferGeometryUtils.mergeVertices( boxGeometry );
  63. //
  64. const positionAttribute = boxGeometry.getAttribute( 'position' );
  65. const colors = [];
  66. const sizes = [];
  67. const color = new THREE.Color();
  68. for ( let i = 0, l = positionAttribute.count; i < l; i ++ ) {
  69. color.setHSL( 0.01 + 0.1 * ( i / l ), 1.0, 0.5 );
  70. color.toArray( colors, i * 3 );
  71. sizes[ i ] = PARTICLE_SIZE * 0.5;
  72. }
  73. const geometry = new THREE.BufferGeometry();
  74. geometry.setAttribute( 'position', positionAttribute );
  75. geometry.setAttribute( 'customColor', new THREE.Float32BufferAttribute( colors, 3 ) );
  76. geometry.setAttribute( 'size', new THREE.Float32BufferAttribute( sizes, 1 ) );
  77. //
  78. const material = new THREE.ShaderMaterial( {
  79. uniforms: {
  80. color: { value: new THREE.Color( 0xffffff ) },
  81. pointTexture: { value: new THREE.TextureLoader().load( 'textures/sprites/disc.png' ) },
  82. alphaTest: { value: 0.9 }
  83. },
  84. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  85. fragmentShader: document.getElementById( 'fragmentshader' ).textContent
  86. } );
  87. //
  88. particles = new THREE.Points( geometry, material );
  89. scene.add( particles );
  90. //
  91. renderer = new THREE.WebGLRenderer();
  92. renderer.setPixelRatio( window.devicePixelRatio );
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. renderer.setAnimationLoop( animate );
  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. render();
  117. stats.update();
  118. }
  119. function render() {
  120. particles.rotation.x += 0.0005;
  121. particles.rotation.y += 0.001;
  122. const geometry = particles.geometry;
  123. const attributes = geometry.attributes;
  124. raycaster.setFromCamera( pointer, camera );
  125. intersects = raycaster.intersectObject( particles );
  126. if ( intersects.length > 0 ) {
  127. if ( INTERSECTED != intersects[ 0 ].index ) {
  128. attributes.size.array[ INTERSECTED ] = PARTICLE_SIZE;
  129. INTERSECTED = intersects[ 0 ].index;
  130. attributes.size.array[ INTERSECTED ] = PARTICLE_SIZE * 1.25;
  131. attributes.size.needsUpdate = true;
  132. }
  133. } else if ( INTERSECTED !== null ) {
  134. attributes.size.array[ INTERSECTED ] = PARTICLE_SIZE;
  135. attributes.size.needsUpdate = true;
  136. INTERSECTED = null;
  137. }
  138. renderer.render( scene, camera );
  139. }
  140. </script>
  141. </body>
  142. </html>