webgl_points_waves.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - particles - waves</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="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl particles waves example
  12. </div>
  13. <script type="x-shader/x-vertex" id="vertexshader">
  14. attribute float scale;
  15. void main() {
  16. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  17. gl_PointSize = scale * ( 300.0 / - mvPosition.z );
  18. gl_Position = projectionMatrix * mvPosition;
  19. }
  20. </script>
  21. <script type="x-shader/x-fragment" id="fragmentshader">
  22. uniform vec3 color;
  23. void main() {
  24. if ( length( gl_PointCoord - vec2( 0.5, 0.5 ) ) > 0.475 ) discard;
  25. gl_FragColor = vec4( color, 1.0 );
  26. }
  27. </script>
  28. <!-- Import maps polyfill -->
  29. <!-- Remove this when import maps will be widely supported -->
  30. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  31. <script type="importmap">
  32. {
  33. "imports": {
  34. "three": "../build/three.module.js",
  35. "three/addons/": "./jsm/"
  36. }
  37. }
  38. </script>
  39. <script type="module">
  40. import * as THREE from 'three';
  41. import Stats from 'three/addons/libs/stats.module.js';
  42. const SEPARATION = 100, AMOUNTX = 50, AMOUNTY = 50;
  43. let container, stats;
  44. let camera, scene, renderer;
  45. let particles, count = 0;
  46. let mouseX = 0, mouseY = 0;
  47. let windowHalfX = window.innerWidth / 2;
  48. let windowHalfY = window.innerHeight / 2;
  49. init();
  50. animate();
  51. function init() {
  52. container = document.createElement( 'div' );
  53. document.body.appendChild( container );
  54. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
  55. camera.position.z = 1000;
  56. scene = new THREE.Scene();
  57. //
  58. const numParticles = AMOUNTX * AMOUNTY;
  59. const positions = new Float32Array( numParticles * 3 );
  60. const scales = new Float32Array( numParticles );
  61. let i = 0, j = 0;
  62. for ( let ix = 0; ix < AMOUNTX; ix ++ ) {
  63. for ( let iy = 0; iy < AMOUNTY; iy ++ ) {
  64. positions[ i ] = ix * SEPARATION - ( ( AMOUNTX * SEPARATION ) / 2 ); // x
  65. positions[ i + 1 ] = 0; // y
  66. positions[ i + 2 ] = iy * SEPARATION - ( ( AMOUNTY * SEPARATION ) / 2 ); // z
  67. scales[ j ] = 1;
  68. i += 3;
  69. j ++;
  70. }
  71. }
  72. const geometry = new THREE.BufferGeometry();
  73. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  74. geometry.setAttribute( 'scale', new THREE.BufferAttribute( scales, 1 ) );
  75. const material = new THREE.ShaderMaterial( {
  76. uniforms: {
  77. color: { value: new THREE.Color( 0xffffff ) },
  78. },
  79. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  80. fragmentShader: document.getElementById( 'fragmentshader' ).textContent
  81. } );
  82. //
  83. particles = new THREE.Points( geometry, material );
  84. scene.add( particles );
  85. //
  86. renderer = new THREE.WebGLRenderer( { antialias: true } );
  87. renderer.setPixelRatio( window.devicePixelRatio );
  88. renderer.setSize( window.innerWidth, window.innerHeight );
  89. container.appendChild( renderer.domElement );
  90. stats = new Stats();
  91. container.appendChild( stats.dom );
  92. container.style.touchAction = 'none';
  93. container.addEventListener( 'pointermove', onPointerMove );
  94. //
  95. window.addEventListener( 'resize', onWindowResize );
  96. }
  97. function onWindowResize() {
  98. windowHalfX = window.innerWidth / 2;
  99. windowHalfY = window.innerHeight / 2;
  100. camera.aspect = window.innerWidth / window.innerHeight;
  101. camera.updateProjectionMatrix();
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. }
  104. //
  105. function onPointerMove( event ) {
  106. if ( event.isPrimary === false ) return;
  107. mouseX = event.clientX - windowHalfX;
  108. mouseY = event.clientY - windowHalfY;
  109. }
  110. //
  111. function animate() {
  112. requestAnimationFrame( animate );
  113. render();
  114. stats.update();
  115. }
  116. function render() {
  117. camera.position.x += ( mouseX - camera.position.x ) * .05;
  118. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  119. camera.lookAt( scene.position );
  120. const positions = particles.geometry.attributes.position.array;
  121. const scales = particles.geometry.attributes.scale.array;
  122. let i = 0, j = 0;
  123. for ( let ix = 0; ix < AMOUNTX; ix ++ ) {
  124. for ( let iy = 0; iy < AMOUNTY; iy ++ ) {
  125. positions[ i + 1 ] = ( Math.sin( ( ix + count ) * 0.3 ) * 50 ) +
  126. ( Math.sin( ( iy + count ) * 0.5 ) * 50 );
  127. scales[ j ] = ( Math.sin( ( ix + count ) * 0.3 ) + 1 ) * 20 +
  128. ( Math.sin( ( iy + count ) * 0.5 ) + 1 ) * 20;
  129. i += 3;
  130. j ++;
  131. }
  132. }
  133. particles.geometry.attributes.position.needsUpdate = true;
  134. particles.geometry.attributes.scale.needsUpdate = true;
  135. renderer.render( scene, camera );
  136. count += 0.1;
  137. }
  138. </script>
  139. </body>
  140. </html>