Skip to content
1        10        20        30        40        50        60        70        80        90        100       1101import * as THREE from 'three';23const G = 9.81;4const rand = (a, b) => a + Math.random() * (b - a);56const sphereGeo = new THREE.SphereGeometry(1, 6, 4);7const boxGeo = new THREE.BoxGeometry(1, 1, 1);8const casingGeo = new THREE.CylinderGeometry(0.008, 0.008, 0.05, 8);9const casingMat = new THREE.MeshStandardMaterial({ color: 0xc9a24a, metalness: 0.9, roughness: 0.3 });1011// Lightweight particle system. Each particle is one mesh with a velocity and a12// lifetime; anything that falls can bounce on the ground instead of passing13// through it. Fading particles get their own material so they can change14// opacity without affecting the others.15export class Particles {16  constructor(scene) {17    this.scene = scene;18    this.list = [];19  }2021  add(mesh, opts) {22    const p = {23      m: mesh,24      v: opts.v ? opts.v.clone() : new THREE.Vector3(),25      life: opts.life,26      t: 0,27      gravity: opts.gravity ?? 1,28      drag: opts.drag ?? 0,29      bounce: opts.bounce ?? false,30      radius: opts.radius ?? 0,31      spin: opts.spin ?? null,32      grow: opts.grow ?? 0,33      shrink: opts.shrink ?? false,34      fade: opts.fade ?? null,35      ownMat: opts.ownMat ?? null,36      onGround: false,37    };38    if (opts.scale) mesh.scale.setScalar(opts.scale);39    this.scene.add(mesh);40    this.list.push(p);41    return p;42  }4344  // A cloud of soft spheres (dust, smoke) that expands and fades out.45  puff(pos, color, { count = 3, size = 0.12, life = 0.6, spread = 0.4 } = {}) {46    for (let i = 0; i < count; i++) {47      const mat = new THREE.MeshBasicMaterial({ color, transparent: true, opacity: 0.55, depthWrite: false });48      const m = new THREE.Mesh(sphereGeo, mat);49      m.position.copy(pos).add(new THREE.Vector3(rand(-0.05, 0.05), rand(-0.05, 0.05), rand(-0.05, 0.05)));50      this.add(m, {51        v: new THREE.Vector3(rand(-spread, spread), rand(0.1, spread * 1.5), rand(-spread, spread)),52        life: life * rand(0.8, 1.2),53        gravity: -0.05,54        drag: 2.5,55        grow: 1.5,56        scale: size * rand(0.6, 1.2),57        fade: true,58        ownMat: mat,59      });60    }61  }6263  // Small chips and fragments thrown out by a hit, which fall and settle.64  debris(pos, normal, color, { count = 4, speed = 2, life = 1.4 } = {}) {65    for (let i = 0; i < count; i++) {66      const mat = new THREE.MeshLambertMaterial({ color });67      const m = new THREE.Mesh(boxGeo, mat);68      const s = rand(0.012, 0.03);69      m.position.copy(pos);70      const v = normal.clone().multiplyScalar(rand(0.3, 1) * speed);71      v.x += rand(-1, 1) * speed * 0.6;72      v.y += rand(0.2, 1) * speed * 0.7;73      v.z += rand(-1, 1) * speed * 0.6;74      this.add(m, {75        v,76        life: life * rand(0.7, 1.3),77        gravity: 1,78        bounce: true,79        radius: s / 2,80        scale: s,81        spin: new THREE.Vector3(rand(-12, 12), rand(-12, 12), rand(-12, 12)),82        shrink: false,83        ownMat: mat,84      });85    }86  }8788  // Blood flicks that arc out from a wound and drop away.89  spray(pos, dir, { count = 10, speed = 2.2 } = {}) {90    const mat = new THREE.MeshBasicMaterial({ color: 0x6e0c0c });91    for (let i = 0; i < count; i++) {92      const m = new THREE.Mesh(sphereGeo, mat);93      m.position.copy(pos);94      const v = dir.clone().multiplyScalar(rand(0.3, 1) * speed);95      v.x += rand(-1, 1) * speed * 0.5;96      v.y += rand(-0.2, 1) * speed * 0.6;97      v.z += rand(-1, 1) * speed * 0.5;98      this.add(m, {99        v,100        life: rand(0.5, 0.9),101        gravity: 1,102        drag: 0.4,103        scale: rand(0.012, 0.028),104        shrink: true,105        ownMat: null,106      });107    }108  }109110  // A brass case thrown out of the ejection port. It bounces once or twice.111  casing(pos, v) {112    const m = new THREE.Mesh(casingGeo, casingMat);113    m.position.copy(pos);114    this.add(m, {115      v,116      life: 2.6,117      gravity: 1,118      bounce: true,119      radius: 0.008,120      spin: new THREE.Vector3(rand(-30, 30), rand(-30, 30), rand(-30, 30)),121    });122  }123124  update(dt) {125    for (let i = this.list.length - 1; i >= 0; i--) {126      const p = this.list[i];127      p.t += dt;128      if (p.t >= p.life) {129        this.scene.remove(p.m);130        if (p.ownMat) p.ownMat.dispose();131        this.list.splice(i, 1);132        continue;133      }134      const k = p.t / p.life;135      p.v.y -= G * p.gravity * dt;136      if (p.drag) p.v.multiplyScalar(Math.exp(-p.drag * dt));137      p.m.position.addScaledVector(p.v, dt);138139      if (p.bounce && p.m.position.y < p.radius) {140        p.m.position.y = p.radius;141        if (p.v.y < -0.5) {142          p.v.y *= -0.35;143          p.v.x *= 0.55;144          p.v.z *= 0.55;145        } else {146          p.v.set(0, 0, 0);147          p.bounce = false;148          p.spin = null;149        }150      }151      if (p.spin) {152        p.m.rotation.x += p.spin.x * dt;153        p.m.rotation.y += p.spin.y * dt;154        p.m.rotation.z += p.spin.z * dt;155      }156      if (p.grow) p.m.scale.multiplyScalar(1 + p.grow * dt);157      if (p.shrink) p.m.scale.setScalar(Math.max(0.001, p.m.scale.x * (1 - dt * 2)));158      if (p.fade && p.ownMat) p.ownMat.opacity = 0.55 * (1 - k);159    }160  }161}