particles.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js';
  2. export const particles = (function() {
  3. const _VS = `
  4. attribute float size;
  5. varying vec3 vColor;
  6. void main() {
  7. vColor = color;
  8. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  9. gl_PointSize = size * ( 300.0 / -mvPosition.z );
  10. gl_Position = projectionMatrix * mvPosition;
  11. }
  12. `;
  13. const _PS = `
  14. uniform sampler2D pointTexture;
  15. varying vec3 vColor;
  16. void main() {
  17. gl_FragColor = vec4(vColor * 4.0, 1.0);
  18. gl_FragColor = gl_FragColor * texture2D( pointTexture, gl_PointCoord );
  19. }
  20. `;
  21. return {
  22. ParticleSystem: class {
  23. constructor(game, params) {
  24. this._Initialize(game, params);
  25. }
  26. _Initialize(game, params) {
  27. const uniforms = {
  28. pointTexture: {
  29. value: new THREE.TextureLoader().load(params.texture)
  30. }
  31. };
  32. this._material = new THREE.ShaderMaterial( {
  33. uniforms: uniforms,
  34. vertexShader: _VS,
  35. fragmentShader: _PS,
  36. blending: THREE.AdditiveBlending,
  37. depthTest: true,
  38. depthWrite: false,
  39. transparent: true,
  40. vertexColors: true
  41. } );
  42. this._geometry = new THREE.BufferGeometry();
  43. this._particleSystem = new THREE.Points(
  44. this._geometry, this._material);
  45. game._graphics._scene.add(this._particleSystem);
  46. this._liveParticles = [];
  47. }
  48. CreateParticle() {
  49. const p = {
  50. Position: new THREE.Vector3(0, 0, 0),
  51. Colour: new THREE.Color(),
  52. Size: 1,
  53. Alive: true,
  54. };
  55. this._liveParticles.push(p);
  56. return p;
  57. }
  58. Update() {
  59. this._liveParticles = this._liveParticles.filter(p => {
  60. return p.Alive;
  61. });
  62. const positions = [];
  63. const colors = [];
  64. const sizes = [];
  65. for (const p of this._liveParticles) {
  66. positions.push(p.Position.x, p.Position.y, p.Position.z);
  67. colors.push(p.Colour.r, p.Colour.g, p.Colour.b);
  68. sizes.push(p.Size);
  69. }
  70. this._geometry.setAttribute(
  71. 'position', new THREE.Float32BufferAttribute(positions, 3));
  72. this._geometry.setAttribute(
  73. 'color', new THREE.Float32BufferAttribute(colors, 3));
  74. this._geometry.setAttribute(
  75. 'size', new THREE.Float32BufferAttribute(sizes, 1).setUsage(
  76. THREE.DynamicDrawUsage));
  77. this._geometry.attributes.position.needsUpdate = true;
  78. this._geometry.attributes.color.needsUpdate = true;
  79. this._geometry.attributes.size.needsUpdate = true;
  80. }
  81. }
  82. };
  83. })();