webgl_points_sprites.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - particles - sprites</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 particle sprites example<br/>
  12. snowflakes by <a href="http://en.wikipedia.org/wiki/File:Sketch_of_snow_crystal_by_Ren%C3%A9_Descartes.jpg">Ren&eacute; Descartes</a>
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import Stats from './jsm/libs/stats.module.js';
  17. import { GUI } from './jsm/libs/dat.gui.module.js';
  18. let camera, scene, renderer, stats, parameters;
  19. let mouseX = 0, mouseY = 0;
  20. let windowHalfX = window.innerWidth / 2;
  21. let windowHalfY = window.innerHeight / 2;
  22. const materials = [];
  23. init();
  24. animate();
  25. function init() {
  26. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 2000 );
  27. camera.position.z = 1000;
  28. scene = new THREE.Scene();
  29. scene.fog = new THREE.FogExp2( 0x000000, 0.0008 );
  30. const geometry = new THREE.BufferGeometry();
  31. const vertices = [];
  32. const textureLoader = new THREE.TextureLoader();
  33. const sprite1 = textureLoader.load( 'textures/sprites/snowflake1.png' );
  34. const sprite2 = textureLoader.load( 'textures/sprites/snowflake2.png' );
  35. const sprite3 = textureLoader.load( 'textures/sprites/snowflake3.png' );
  36. const sprite4 = textureLoader.load( 'textures/sprites/snowflake4.png' );
  37. const sprite5 = textureLoader.load( 'textures/sprites/snowflake5.png' );
  38. for ( let i = 0; i < 10000; i ++ ) {
  39. const x = Math.random() * 2000 - 1000;
  40. const y = Math.random() * 2000 - 1000;
  41. const z = Math.random() * 2000 - 1000;
  42. vertices.push( x, y, z );
  43. }
  44. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  45. parameters = [
  46. [[ 1.0, 0.2, 0.5 ], sprite2, 20 ],
  47. [[ 0.95, 0.1, 0.5 ], sprite3, 15 ],
  48. [[ 0.90, 0.05, 0.5 ], sprite1, 10 ],
  49. [[ 0.85, 0, 0.5 ], sprite5, 8 ],
  50. [[ 0.80, 0, 0.5 ], sprite4, 5 ]
  51. ];
  52. for ( let i = 0; i < parameters.length; i ++ ) {
  53. const color = parameters[ i ][ 0 ];
  54. const sprite = parameters[ i ][ 1 ];
  55. const size = parameters[ i ][ 2 ];
  56. materials[ i ] = new THREE.PointsMaterial( { size: size, map: sprite, blending: THREE.AdditiveBlending, depthTest: false, transparent: true } );
  57. materials[ i ].color.setHSL( color[ 0 ], color[ 1 ], color[ 2 ] );
  58. const particles = new THREE.Points( geometry, materials[ i ] );
  59. particles.rotation.x = Math.random() * 6;
  60. particles.rotation.y = Math.random() * 6;
  61. particles.rotation.z = Math.random() * 6;
  62. scene.add( particles );
  63. }
  64. //
  65. renderer = new THREE.WebGLRenderer();
  66. renderer.setPixelRatio( window.devicePixelRatio );
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. document.body.appendChild( renderer.domElement );
  69. //
  70. stats = new Stats();
  71. document.body.appendChild( stats.dom );
  72. //
  73. const gui = new GUI();
  74. const params = {
  75. texture: true
  76. };
  77. gui.add( params, 'texture' ).onChange( function ( value ) {
  78. for ( let i = 0; i < materials.length; i ++ ) {
  79. materials[ i ].map = ( value === true ) ? parameters[ i ][ 1 ] : null;
  80. materials[ i ].needsUpdate = true;
  81. }
  82. } );
  83. gui.open();
  84. document.body.style.touchAction = 'none';
  85. document.body.addEventListener( 'pointermove', onPointerMove );
  86. //
  87. window.addEventListener( 'resize', onWindowResize );
  88. }
  89. function onWindowResize() {
  90. windowHalfX = window.innerWidth / 2;
  91. windowHalfY = window.innerHeight / 2;
  92. camera.aspect = window.innerWidth / window.innerHeight;
  93. camera.updateProjectionMatrix();
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. }
  96. function onPointerMove( event ) {
  97. if ( event.isPrimary === false ) return;
  98. mouseX = event.clientX - windowHalfX;
  99. mouseY = event.clientY - windowHalfY;
  100. }
  101. //
  102. function animate() {
  103. requestAnimationFrame( animate );
  104. render();
  105. stats.update();
  106. }
  107. function render() {
  108. const time = Date.now() * 0.00005;
  109. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  110. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  111. camera.lookAt( scene.position );
  112. for ( let i = 0; i < scene.children.length; i ++ ) {
  113. const object = scene.children[ i ];
  114. if ( object instanceof THREE.Points ) {
  115. object.rotation.y = time * ( i < 4 ? i + 1 : - ( i + 1 ) );
  116. }
  117. }
  118. for ( let i = 0; i < materials.length; i ++ ) {
  119. const color = parameters[ i ][ 0 ];
  120. const h = ( 360 * ( color[ 0 ] + time ) % 360 ) / 360;
  121. materials[ i ].color.setHSL( h, color[ 1 ], color[ 2 ] );
  122. }
  123. renderer.render( scene, camera );
  124. }
  125. </script>
  126. </body>
  127. </html>