webgl_buffergeometry_custom_attributes_particles.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - buffer geometry custom attributes - particles</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="container"></div>
  11. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - buffergeometry custom attributes - particles</div>
  12. <script type="x-shader/x-vertex" id="vertexshader">
  13. attribute float size;
  14. varying vec3 vColor;
  15. void main() {
  16. vColor = color;
  17. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  18. gl_PointSize = size * ( 300.0 / -mvPosition.z );
  19. gl_Position = projectionMatrix * mvPosition;
  20. }
  21. </script>
  22. <script type="x-shader/x-fragment" id="fragmentshader">
  23. uniform sampler2D pointTexture;
  24. varying vec3 vColor;
  25. void main() {
  26. gl_FragColor = vec4( vColor, 1.0 );
  27. gl_FragColor = gl_FragColor * texture2D( pointTexture, gl_PointCoord );
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from '../build/three.module.js';
  32. import Stats from './jsm/libs/stats.module.js';
  33. var renderer, scene, camera, stats;
  34. var particleSystem, uniforms, geometry;
  35. var particles = 100000;
  36. init();
  37. animate();
  38. function init() {
  39. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
  40. camera.position.z = 300;
  41. scene = new THREE.Scene();
  42. uniforms = {
  43. pointTexture: { value: new THREE.TextureLoader().load( "textures/sprites/spark1.png" ) }
  44. };
  45. var shaderMaterial = new THREE.ShaderMaterial( {
  46. uniforms: uniforms,
  47. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  48. fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
  49. blending: THREE.AdditiveBlending,
  50. depthTest: false,
  51. transparent: true,
  52. vertexColors: THREE.VertexColors
  53. } );
  54. var radius = 200;
  55. geometry = new THREE.BufferGeometry();
  56. var positions = [];
  57. var colors = [];
  58. var sizes = [];
  59. var color = new THREE.Color();
  60. for ( var i = 0; i < particles; i ++ ) {
  61. positions.push( ( Math.random() * 2 - 1 ) * radius );
  62. positions.push( ( Math.random() * 2 - 1 ) * radius );
  63. positions.push( ( Math.random() * 2 - 1 ) * radius );
  64. color.setHSL( i / particles, 1.0, 0.5 );
  65. colors.push( color.r, color.g, color.b );
  66. sizes.push( 20 );
  67. }
  68. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  69. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  70. geometry.setAttribute( 'size', new THREE.Float32BufferAttribute( sizes, 1 ).setUsage( THREE.DynamicDrawUsage ) );
  71. particleSystem = new THREE.Points( geometry, shaderMaterial );
  72. scene.add( particleSystem );
  73. renderer = new THREE.WebGLRenderer();
  74. renderer.setPixelRatio( window.devicePixelRatio );
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. var container = document.getElementById( 'container' );
  77. container.appendChild( renderer.domElement );
  78. stats = new Stats();
  79. container.appendChild( stats.dom );
  80. //
  81. window.addEventListener( 'resize', onWindowResize, false );
  82. }
  83. function onWindowResize() {
  84. camera.aspect = window.innerWidth / window.innerHeight;
  85. camera.updateProjectionMatrix();
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. }
  88. function animate() {
  89. requestAnimationFrame( animate );
  90. render();
  91. stats.update();
  92. }
  93. function render() {
  94. var time = Date.now() * 0.005;
  95. particleSystem.rotation.z = 0.01 * time;
  96. var sizes = geometry.attributes.size.array;
  97. for ( var i = 0; i < particles; i ++ ) {
  98. sizes[ i ] = 10 * ( 1 + Math.sin( 0.1 * i + time ) );
  99. }
  100. geometry.attributes.size.needsUpdate = true;
  101. renderer.render( scene, camera );
  102. }
  103. </script>
  104. </body>
  105. </html>