2
0

webgl_buffergeometry_custom_attributes_particles.html 4.1 KB

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