webgl_points_billboards.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - particles - billboards</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 billboards example
  12. </div>
  13. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import Stats from './jsm/libs/stats.module.js';
  26. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  27. let camera, scene, renderer, stats, material;
  28. let mouseX = 0, mouseY = 0;
  29. let windowHalfX = window.innerWidth / 2;
  30. let windowHalfY = window.innerHeight / 2;
  31. init();
  32. animate();
  33. function init() {
  34. camera = new THREE.PerspectiveCamera( 55, window.innerWidth / window.innerHeight, 2, 2000 );
  35. camera.position.z = 1000;
  36. scene = new THREE.Scene();
  37. scene.fog = new THREE.FogExp2( 0x000000, 0.001 );
  38. const geometry = new THREE.BufferGeometry();
  39. const vertices = [];
  40. const sprite = new THREE.TextureLoader().load( 'textures/sprites/disc.png' );
  41. for ( let i = 0; i < 10000; i ++ ) {
  42. const x = 2000 * Math.random() - 1000;
  43. const y = 2000 * Math.random() - 1000;
  44. const z = 2000 * Math.random() - 1000;
  45. vertices.push( x, y, z );
  46. }
  47. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  48. material = new THREE.PointsMaterial( { size: 35, sizeAttenuation: true, map: sprite, alphaTest: 0.5, transparent: true } );
  49. material.color.setHSL( 1.0, 0.3, 0.7 );
  50. const particles = new THREE.Points( geometry, material );
  51. scene.add( particles );
  52. //
  53. renderer = new THREE.WebGLRenderer();
  54. renderer.setPixelRatio( window.devicePixelRatio );
  55. renderer.setSize( window.innerWidth, window.innerHeight );
  56. document.body.appendChild( renderer.domElement );
  57. //
  58. stats = new Stats();
  59. document.body.appendChild( stats.dom );
  60. //
  61. const gui = new GUI();
  62. gui.add( material, 'sizeAttenuation' ).onChange( function () {
  63. material.needsUpdate = true;
  64. } );
  65. gui.open();
  66. //
  67. document.body.style.touchAction = 'none';
  68. document.body.addEventListener( 'pointermove', onPointerMove );
  69. //
  70. window.addEventListener( 'resize', onWindowResize );
  71. }
  72. function onWindowResize() {
  73. windowHalfX = window.innerWidth / 2;
  74. windowHalfY = window.innerHeight / 2;
  75. camera.aspect = window.innerWidth / window.innerHeight;
  76. camera.updateProjectionMatrix();
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. }
  79. function onPointerMove( event ) {
  80. if ( event.isPrimary === false ) return;
  81. mouseX = event.clientX - windowHalfX;
  82. mouseY = event.clientY - windowHalfY;
  83. }
  84. //
  85. function animate() {
  86. requestAnimationFrame( animate );
  87. render();
  88. stats.update();
  89. }
  90. function render() {
  91. const time = Date.now() * 0.00005;
  92. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  93. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  94. camera.lookAt( scene.position );
  95. const h = ( 360 * ( 1.0 + time ) % 360 ) / 360;
  96. material.color.setHSL( h, 0.5, 0.5 );
  97. renderer.render( scene, camera );
  98. }
  99. </script>
  100. </body>
  101. </html>