webgl_points_sprites.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. var camera, scene, renderer, stats, materials = [], parameters;
  19. var mouseX = 0, mouseY = 0;
  20. var windowHalfX = window.innerWidth / 2;
  21. var windowHalfY = window.innerHeight / 2;
  22. init();
  23. animate();
  24. function init() {
  25. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 2000 );
  26. camera.position.z = 1000;
  27. scene = new THREE.Scene();
  28. scene.fog = new THREE.FogExp2( 0x000000, 0.0008 );
  29. var geometry = new THREE.BufferGeometry();
  30. var vertices = [];
  31. var textureLoader = new THREE.TextureLoader();
  32. var sprite1 = textureLoader.load( 'textures/sprites/snowflake1.png' );
  33. var sprite2 = textureLoader.load( 'textures/sprites/snowflake2.png' );
  34. var sprite3 = textureLoader.load( 'textures/sprites/snowflake3.png' );
  35. var sprite4 = textureLoader.load( 'textures/sprites/snowflake4.png' );
  36. var sprite5 = textureLoader.load( 'textures/sprites/snowflake5.png' );
  37. for ( var i = 0; i < 10000; i ++ ) {
  38. var x = Math.random() * 2000 - 1000;
  39. var y = Math.random() * 2000 - 1000;
  40. var z = Math.random() * 2000 - 1000;
  41. vertices.push( x, y, z );
  42. }
  43. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  44. parameters = [
  45. [[ 1.0, 0.2, 0.5 ], sprite2, 20 ],
  46. [[ 0.95, 0.1, 0.5 ], sprite3, 15 ],
  47. [[ 0.90, 0.05, 0.5 ], sprite1, 10 ],
  48. [[ 0.85, 0, 0.5 ], sprite5, 8 ],
  49. [[ 0.80, 0, 0.5 ], sprite4, 5 ]
  50. ];
  51. for ( var i = 0; i < parameters.length; i ++ ) {
  52. var color = parameters[ i ][ 0 ];
  53. var sprite = parameters[ i ][ 1 ];
  54. var size = parameters[ i ][ 2 ];
  55. materials[ i ] = new THREE.PointsMaterial( { size: size, map: sprite, blending: THREE.AdditiveBlending, depthTest: false, transparent: true } );
  56. materials[ i ].color.setHSL( color[ 0 ], color[ 1 ], color[ 2 ] );
  57. var particles = new THREE.Points( geometry, materials[ i ] );
  58. particles.rotation.x = Math.random() * 6;
  59. particles.rotation.y = Math.random() * 6;
  60. particles.rotation.z = Math.random() * 6;
  61. scene.add( particles );
  62. }
  63. //
  64. renderer = new THREE.WebGLRenderer();
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. document.body.appendChild( renderer.domElement );
  68. //
  69. stats = new Stats();
  70. document.body.appendChild( stats.dom );
  71. //
  72. var gui = new GUI();
  73. var params = {
  74. texture: true
  75. };
  76. gui.add( params, 'texture' ).onChange( function ( value ) {
  77. for ( var i = 0; i < materials.length; i ++ ) {
  78. materials[ i ].map = ( value === true ) ? parameters[ i ][ 1 ] : null;
  79. materials[ i ].needsUpdate = true;
  80. }
  81. } );
  82. gui.open();
  83. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  84. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  85. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  86. //
  87. window.addEventListener( 'resize', onWindowResize, false );
  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 onDocumentMouseMove( event ) {
  97. mouseX = event.clientX - windowHalfX;
  98. mouseY = event.clientY - windowHalfY;
  99. }
  100. function onDocumentTouchStart( 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. function onDocumentTouchMove( event ) {
  108. if ( event.touches.length === 1 ) {
  109. event.preventDefault();
  110. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  111. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  112. }
  113. }
  114. //
  115. function animate() {
  116. requestAnimationFrame( animate );
  117. render();
  118. stats.update();
  119. }
  120. function render() {
  121. var time = Date.now() * 0.00005;
  122. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  123. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  124. camera.lookAt( scene.position );
  125. for ( var i = 0; i < scene.children.length; i ++ ) {
  126. var object = scene.children[ i ];
  127. if ( object instanceof THREE.Points ) {
  128. object.rotation.y = time * ( i < 4 ? i + 1 : - ( i + 1 ) );
  129. }
  130. }
  131. for ( var i = 0; i < materials.length; i ++ ) {
  132. var color = parameters[ i ][ 0 ];
  133. var h = ( 360 * ( color[ 0 ] + time ) % 360 ) / 360;
  134. materials[ i ].color.setHSL( h, color[ 1 ], color[ 2 ] );
  135. }
  136. renderer.render( scene, camera );
  137. }
  138. </script>
  139. </body>
  140. </html>