webgl_points_billboards.html 3.4 KB

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