webgl_points_waves.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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="http://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. <script type="module">
  29. import {
  30. BufferAttribute,
  31. BufferGeometry,
  32. Color,
  33. PerspectiveCamera,
  34. Points,
  35. Scene,
  36. ShaderMaterial,
  37. WebGLRenderer
  38. } from "../build/three.module.js";
  39. import Stats from './jsm/libs/stats.module.js';
  40. var SEPARATION = 100, AMOUNTX = 50, AMOUNTY = 50;
  41. var container, stats;
  42. var camera, scene, renderer;
  43. var particles, count = 0;
  44. var mouseX = 0, mouseY = 0;
  45. var windowHalfX = window.innerWidth / 2;
  46. var windowHalfY = window.innerHeight / 2;
  47. init();
  48. animate();
  49. function init() {
  50. container = document.createElement( 'div' );
  51. document.body.appendChild( container );
  52. camera = new PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
  53. camera.position.z = 1000;
  54. scene = new Scene();
  55. //
  56. var numParticles = AMOUNTX * AMOUNTY;
  57. var positions = new Float32Array( numParticles * 3 );
  58. var scales = new Float32Array( numParticles );
  59. var i = 0, j = 0;
  60. for ( var ix = 0; ix < AMOUNTX; ix ++ ) {
  61. for ( var iy = 0; iy < AMOUNTY; iy ++ ) {
  62. positions[ i ] = ix * SEPARATION - ( ( AMOUNTX * SEPARATION ) / 2 ); // x
  63. positions[ i + 1 ] = 0; // y
  64. positions[ i + 2 ] = iy * SEPARATION - ( ( AMOUNTY * SEPARATION ) / 2 ); // z
  65. scales[ j ] = 1;
  66. i += 3;
  67. j ++;
  68. }
  69. }
  70. var geometry = new BufferGeometry();
  71. geometry.addAttribute( 'position', new BufferAttribute( positions, 3 ) );
  72. geometry.addAttribute( 'scale', new BufferAttribute( scales, 1 ) );
  73. var material = new ShaderMaterial( {
  74. uniforms: {
  75. color: { value: new Color( 0xffffff ) },
  76. },
  77. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  78. fragmentShader: document.getElementById( 'fragmentshader' ).textContent
  79. } );
  80. //
  81. particles = new Points( geometry, material );
  82. scene.add( particles );
  83. //
  84. renderer = new WebGLRenderer( { antialias: true } );
  85. renderer.setPixelRatio( window.devicePixelRatio );
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. container.appendChild( renderer.domElement );
  88. stats = new Stats();
  89. container.appendChild( stats.dom );
  90. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  91. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  92. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  93. //
  94. window.addEventListener( 'resize', onWindowResize, false );
  95. }
  96. function onWindowResize() {
  97. windowHalfX = window.innerWidth / 2;
  98. windowHalfY = window.innerHeight / 2;
  99. camera.aspect = window.innerWidth / window.innerHeight;
  100. camera.updateProjectionMatrix();
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. }
  103. //
  104. function onDocumentMouseMove( event ) {
  105. mouseX = event.clientX - windowHalfX;
  106. mouseY = event.clientY - windowHalfY;
  107. }
  108. function onDocumentTouchStart( event ) {
  109. if ( event.touches.length === 1 ) {
  110. event.preventDefault();
  111. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  112. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  113. }
  114. }
  115. function onDocumentTouchMove( event ) {
  116. if ( event.touches.length === 1 ) {
  117. event.preventDefault();
  118. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  119. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  120. }
  121. }
  122. //
  123. function animate() {
  124. requestAnimationFrame( animate );
  125. render();
  126. stats.update();
  127. }
  128. function render() {
  129. camera.position.x += ( mouseX - camera.position.x ) * .05;
  130. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  131. camera.lookAt( scene.position );
  132. var positions = particles.geometry.attributes.position.array;
  133. var scales = particles.geometry.attributes.scale.array;
  134. var i = 0, j = 0;
  135. for ( var ix = 0; ix < AMOUNTX; ix ++ ) {
  136. for ( var iy = 0; iy < AMOUNTY; iy ++ ) {
  137. positions[ i + 1 ] = ( Math.sin( ( ix + count ) * 0.3 ) * 50 ) +
  138. ( Math.sin( ( iy + count ) * 0.5 ) * 50 );
  139. scales[ j ] = ( Math.sin( ( ix + count ) * 0.3 ) + 1 ) * 8 +
  140. ( Math.sin( ( iy + count ) * 0.5 ) + 1 ) * 8;
  141. i += 3;
  142. j ++;
  143. }
  144. }
  145. particles.geometry.attributes.position.needsUpdate = true;
  146. particles.geometry.attributes.scale.needsUpdate = true;
  147. renderer.render( scene, camera );
  148. count += 0.1;
  149. }
  150. </script>
  151. </body>
  152. </html>