webgl_points_billboards.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. var camera, scene, renderer, stats, material;
  18. var mouseX = 0, mouseY = 0;
  19. var windowHalfX = window.innerWidth / 2;
  20. var 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. var geometry = new THREE.BufferGeometry();
  29. var vertices = [];
  30. var sprite = new THREE.TextureLoader().load( 'textures/sprites/disc.png' );
  31. for ( var i = 0; i < 10000; i ++ ) {
  32. var x = 2000 * Math.random() - 1000;
  33. var y = 2000 * Math.random() - 1000;
  34. var 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: false, map: sprite, alphaTest: 0.5, transparent: true } );
  39. material.color.setHSL( 1.0, 0.3, 0.7 );
  40. var 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. var gui = new GUI();
  52. gui.add( material, 'sizeAttenuation' ).onChange( function () {
  53. material.needsUpdate = true;
  54. } );
  55. gui.open();
  56. //
  57. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  58. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  59. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  60. //
  61. window.addEventListener( 'resize', onWindowResize, false );
  62. }
  63. function onWindowResize() {
  64. windowHalfX = window.innerWidth / 2;
  65. windowHalfY = window.innerHeight / 2;
  66. camera.aspect = window.innerWidth / window.innerHeight;
  67. camera.updateProjectionMatrix();
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. }
  70. function onDocumentMouseMove( event ) {
  71. mouseX = event.clientX - windowHalfX;
  72. mouseY = event.clientY - windowHalfY;
  73. }
  74. function onDocumentTouchStart( event ) {
  75. if ( event.touches.length == 1 ) {
  76. event.preventDefault();
  77. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  78. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  79. }
  80. }
  81. function onDocumentTouchMove( event ) {
  82. if ( event.touches.length == 1 ) {
  83. event.preventDefault();
  84. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  85. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  86. }
  87. }
  88. //
  89. function animate() {
  90. requestAnimationFrame( animate );
  91. render();
  92. stats.update();
  93. }
  94. function render() {
  95. var time = Date.now() * 0.00005;
  96. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  97. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  98. camera.lookAt( scene.position );
  99. var h = ( 360 * ( 1.0 + time ) % 360 ) / 360;
  100. material.color.setHSL( h, 0.5, 0.5 );
  101. renderer.render( scene, camera );
  102. }
  103. </script>
  104. </body>
  105. </html>