webgl_points_waves.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. <style>
  8. body {
  9. background-color: #000;
  10. margin: 0px;
  11. overflow: hidden;
  12. font-family:Monospace;
  13. font-size:13px;
  14. text-align:center;
  15. font-weight: bold;
  16. text-align:center;
  17. }
  18. a {
  19. color:#0078ff;
  20. }
  21. #info {
  22. color:#fff;
  23. position: absolute;
  24. top: 0px;
  25. width: 100%;
  26. padding: 5px;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="info">
  32. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl particles waves example
  33. </div>
  34. <script src="../build/three.js"></script>
  35. <script src="js/WebGL.js"></script>
  36. <script src="js/libs/stats.min.js"></script>
  37. <script>
  38. if ( WEBGL.isWebGLAvailable() === false ) {
  39. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  40. }
  41. var SEPARATION = 100, AMOUNTX = 50, AMOUNTY = 50;
  42. var container, stats;
  43. var camera, scene, renderer;
  44. var particles, particle, count = 0;
  45. var mouseX = 0, mouseY = 0;
  46. var windowHalfX = window.innerWidth / 2;
  47. var windowHalfY = window.innerHeight / 2;
  48. init();
  49. animate();
  50. function init() {
  51. container = document.createElement( 'div' );
  52. document.body.appendChild( container );
  53. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
  54. camera.position.z = 1000;
  55. scene = new THREE.Scene();
  56. particles = new Array();
  57. var material = new THREE.SpriteMaterial( {
  58. map: new THREE.TextureLoader().load( 'textures/sprites/circle.png' ),
  59. } );
  60. var i = 0;
  61. for ( var ix = 0; ix < AMOUNTX; ix ++ ) {
  62. for ( var iy = 0; iy < AMOUNTY; iy ++ ) {
  63. particle = particles[ i ++ ] = new THREE.Sprite( material );
  64. particle.position.x = ix * SEPARATION - ( ( AMOUNTX * SEPARATION ) / 2 );
  65. particle.position.z = iy * SEPARATION - ( ( AMOUNTY * SEPARATION ) / 2 );
  66. scene.add( particle );
  67. }
  68. }
  69. renderer = new THREE.WebGLRenderer();
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. container.appendChild( renderer.domElement );
  73. stats = new Stats();
  74. container.appendChild( stats.dom );
  75. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  76. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  77. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  78. //
  79. window.addEventListener( 'resize', onWindowResize, false );
  80. }
  81. function onWindowResize() {
  82. windowHalfX = window.innerWidth / 2;
  83. windowHalfY = window.innerHeight / 2;
  84. camera.aspect = window.innerWidth / window.innerHeight;
  85. camera.updateProjectionMatrix();
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. }
  88. //
  89. function onDocumentMouseMove( event ) {
  90. mouseX = event.clientX - windowHalfX;
  91. mouseY = event.clientY - windowHalfY;
  92. }
  93. function onDocumentTouchStart( event ) {
  94. if ( event.touches.length === 1 ) {
  95. event.preventDefault();
  96. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  97. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  98. }
  99. }
  100. function onDocumentTouchMove( event ) {
  101. if ( event.touches.length === 1 ) {
  102. event.preventDefault();
  103. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  104. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  105. }
  106. }
  107. //
  108. function animate() {
  109. requestAnimationFrame( animate );
  110. render();
  111. stats.update();
  112. }
  113. function render() {
  114. camera.position.x += ( mouseX - camera.position.x ) * .05;
  115. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  116. camera.lookAt( scene.position );
  117. var i = 0;
  118. for ( var ix = 0; ix < AMOUNTX; ix ++ ) {
  119. for ( var iy = 0; iy < AMOUNTY; iy ++ ) {
  120. particle = particles[ i ++ ];
  121. particle.position.y = ( Math.sin( ( ix + count ) * 0.3 ) * 50 ) +
  122. ( Math.sin( ( iy + count ) * 0.5 ) * 50 );
  123. particle.scale.x = particle.scale.y = ( Math.sin( ( ix + count ) * 0.3 ) + 1 ) * 4 +
  124. ( Math.sin( ( iy + count ) * 0.5 ) + 1 ) * 4;
  125. }
  126. }
  127. renderer.render( scene, camera );
  128. count += 0.1;
  129. }
  130. </script>
  131. </body>
  132. </html>