1
0

webgl_points_billboards.html 3.6 KB

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