webgl_buffergeometry_custom_attributes_particles.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. <meta property="og:title" content="three.js webgl - buffer geometry custom attributes - particles">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_buffergeometry_custom_attributes_particles.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_buffergeometry_custom_attributes_particles.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - buffergeometry custom attributes - particles</div>
  16. <script type="x-shader/x-vertex" id="vertexshader">
  17. attribute float size;
  18. varying vec3 vColor;
  19. void main() {
  20. vColor = color;
  21. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  22. gl_PointSize = size * ( 300.0 / -mvPosition.z );
  23. gl_Position = projectionMatrix * mvPosition;
  24. }
  25. </script>
  26. <script type="x-shader/x-fragment" id="fragmentshader">
  27. uniform sampler2D pointTexture;
  28. varying vec3 vColor;
  29. void main() {
  30. gl_FragColor = vec4( vColor, 1.0 );
  31. gl_FragColor = gl_FragColor * texture2D( pointTexture, gl_PointCoord );
  32. }
  33. </script>
  34. <script type="importmap">
  35. {
  36. "imports": {
  37. "three": "../build/three.module.js",
  38. "three/addons/": "./jsm/"
  39. }
  40. }
  41. </script>
  42. <script type="module">
  43. import * as THREE from 'three';
  44. import Stats from 'three/addons/libs/stats.module.js';
  45. let renderer, scene, camera, stats;
  46. let particleSystem, uniforms, geometry;
  47. const particles = 100000;
  48. init();
  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.setAnimationLoop( animate );
  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. const time = Date.now() * 0.005;
  102. particleSystem.rotation.z = 0.01 * time;
  103. const sizes = geometry.attributes.size.array;
  104. for ( let i = 0; i < particles; i ++ ) {
  105. sizes[ i ] = 10 * ( 1 + Math.sin( 0.1 * i + time ) );
  106. }
  107. geometry.attributes.size.needsUpdate = true;
  108. renderer.render( scene, camera );
  109. stats.update();
  110. }
  111. </script>
  112. </body>
  113. </html>
粤ICP备19079148号