1
0

webgl_buffergeometry_custom_attributes_particles.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. function init() {
  46. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
  47. camera.position.z = 300;
  48. scene = new THREE.Scene();
  49. uniforms = {
  50. pointTexture: { value: new THREE.TextureLoader().load( 'textures/sprites/spark1.png' ) }
  51. };
  52. const shaderMaterial = new THREE.ShaderMaterial( {
  53. uniforms: uniforms,
  54. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  55. fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
  56. blending: THREE.AdditiveBlending,
  57. depthTest: false,
  58. transparent: true,
  59. vertexColors: true
  60. } );
  61. const radius = 200;
  62. geometry = new THREE.BufferGeometry();
  63. const positions = [];
  64. const colors = [];
  65. const sizes = [];
  66. const color = new THREE.Color();
  67. for ( let i = 0; i < particles; i ++ ) {
  68. positions.push( ( Math.random() * 2 - 1 ) * radius );
  69. positions.push( ( Math.random() * 2 - 1 ) * radius );
  70. positions.push( ( Math.random() * 2 - 1 ) * radius );
  71. color.setHSL( i / particles, 1.0, 0.5 );
  72. colors.push( color.r, color.g, color.b );
  73. sizes.push( 20 );
  74. }
  75. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  76. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  77. geometry.setAttribute( 'size', new THREE.Float32BufferAttribute( sizes, 1 ).setUsage( THREE.DynamicDrawUsage ) );
  78. particleSystem = new THREE.Points( geometry, shaderMaterial );
  79. scene.add( particleSystem );
  80. renderer = new THREE.WebGLRenderer();
  81. renderer.setPixelRatio( window.devicePixelRatio );
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. renderer.setAnimationLoop( animate );
  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. const time = Date.now() * 0.005;
  98. particleSystem.rotation.z = 0.01 * time;
  99. const sizes = geometry.attributes.size.array;
  100. for ( let i = 0; i < particles; i ++ ) {
  101. sizes[ i ] = 10 * ( 1 + Math.sin( 0.1 * i + time ) );
  102. }
  103. geometry.attributes.size.needsUpdate = true;
  104. renderer.render( scene, camera );
  105. stats.update();
  106. }
  107. </script>
  108. </body>
  109. </html>