webgl_points_sprites.html 5.5 KB

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