webgl_buffergeometry_custom_attributes_particles.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. <!-- Import maps polyfill -->
  31. <!-- Remove this when import maps will be widely supported -->
  32. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  33. <script type="importmap">
  34. {
  35. "imports": {
  36. "three": "../build/three.module.js",
  37. "three/addons/": "./jsm/"
  38. }
  39. }
  40. </script>
  41. <script type="module">
  42. import * as THREE from 'three';
  43. import Stats from 'three/addons/libs/stats.module.js';
  44. let renderer, scene, camera, stats;
  45. let particleSystem, uniforms, geometry;
  46. const particles = 100000;
  47. init();
  48. animate();
  49. function init() {
  50. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
  51. camera.position.z = 300;
  52. scene = new THREE.Scene();
  53. uniforms = {
  54. pointTexture: { value: new THREE.TextureLoader().load( 'textures/sprites/spark1.png' ) }
  55. };
  56. const shaderMaterial = new THREE.ShaderMaterial( {
  57. uniforms: uniforms,
  58. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  59. fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
  60. blending: THREE.AdditiveBlending,
  61. depthTest: false,
  62. transparent: true,
  63. vertexColors: true
  64. } );
  65. const radius = 200;
  66. geometry = new THREE.BufferGeometry();
  67. const positions = [];
  68. const colors = [];
  69. const sizes = [];
  70. const color = new THREE.Color();
  71. for ( let i = 0; i < particles; i ++ ) {
  72. positions.push( ( Math.random() * 2 - 1 ) * radius );
  73. positions.push( ( Math.random() * 2 - 1 ) * radius );
  74. positions.push( ( Math.random() * 2 - 1 ) * radius );
  75. color.setHSL( i / particles, 1.0, 0.5 );
  76. colors.push( color.r, color.g, color.b );
  77. sizes.push( 20 );
  78. }
  79. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  80. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  81. geometry.setAttribute( 'size', new THREE.Float32BufferAttribute( sizes, 1 ).setUsage( THREE.DynamicDrawUsage ) );
  82. particleSystem = new THREE.Points( geometry, shaderMaterial );
  83. scene.add( particleSystem );
  84. renderer = new THREE.WebGLRenderer();
  85. renderer.setPixelRatio( window.devicePixelRatio );
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. renderer.useLegacyLights = false;
  88. const container = document.getElementById( 'container' );
  89. container.appendChild( renderer.domElement );
  90. stats = new Stats();
  91. container.appendChild( stats.dom );
  92. //
  93. window.addEventListener( 'resize', onWindowResize );
  94. }
  95. function onWindowResize() {
  96. camera.aspect = window.innerWidth / window.innerHeight;
  97. camera.updateProjectionMatrix();
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. }
  100. function animate() {
  101. requestAnimationFrame( animate );
  102. render();
  103. stats.update();
  104. }
  105. function render() {
  106. const time = Date.now() * 0.005;
  107. particleSystem.rotation.z = 0.01 * time;
  108. const sizes = geometry.attributes.size.array;
  109. for ( let i = 0; i < particles; i ++ ) {
  110. sizes[ i ] = 10 * ( 1 + Math.sin( 0.1 * i + time ) );
  111. }
  112. geometry.attributes.size.needsUpdate = true;
  113. renderer.render( scene, camera );
  114. }
  115. </script>
  116. </body>
  117. </html>