webgl_buffergeometry_custom_attributes_particles.html 4.1 KB

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