Skip to content
1        10        20        30        40        50        60        70        80        90        100       110       120       1301import * as THREE from 'three';23const { BoxGeometry, CylinderGeometry, TorusGeometry, SphereGeometry, Mesh, Group, MeshStandardMaterial, MeshBasicMaterial } = THREE;4const HALF_PI = Math.PI / 2;56const matCache = new Map();7function mat(kind, color = 0xffffff) {8  const key = kind + ':' + color;9  if (!matCache.has(key)) {10    let m;11    switch (kind) {12      case 'steel':13        m = new MeshStandardMaterial({ color, metalness: 0.85, roughness: 0.32 });14        break;15      case 'wood':16        m = new MeshStandardMaterial({ color, metalness: 0, roughness: 0.7 });17        break;18      case 'poly':19        m = new MeshStandardMaterial({ color, metalness: 0.05, roughness: 0.8 });20        break;21      case 'rubber':22        m = new MeshStandardMaterial({ color: 0x141414, metalness: 0, roughness: 0.95 });23        break;24      case 'brass':25        m = new MeshStandardMaterial({ color: 0xc0a04a, metalness: 0.9, roughness: 0.25 });26        break;27      case 'glass':28        m = new MeshStandardMaterial({29          color: 0x1d4a6b,30          metalness: 0.1,31          roughness: 0.08,32          transparent: true,33          opacity: 0.75,34          emissive: 0x0b2a40,35          emissiveIntensity: 0.6,36        });37        break;38      case 'glow':39        m = new MeshStandardMaterial({ color, emissive: color, emissiveIntensity: 1.6, roughness: 0.4 });40        break;41    }42    matCache.set(key, m);43  }44  return matCache.get(key);45}4647const box = (w, h, d) => new BoxGeometry(w, h, d);48const yCyl = (rt, rb, len, seg = 16) => new CylinderGeometry(rt, rb, len, seg);49const zCyl = (rBack, rFront, len, seg = 16) => new CylinderGeometry(rBack, rFront, len, seg);5051function part(g, geom, m, x = 0, y = 0, z = 0, rot = [0, 0, 0]) {52  const mesh = new Mesh(geom, m);53  mesh.position.set(x, y, z);54  mesh.rotation.set(rot[0], rot[1], rot[2]);55  g.add(mesh);56  return mesh;57}5859function receiver(g, { len, w, h, z, m, top = 0.012 }) {60  part(g, box(w, h, len), m, 0, 0, z);61  part(g, box(w * 0.7, top, len * 0.9), m, 0, h / 2 + top / 2, z);62  return z - len / 2;63}6465function barrel(g, { rBreech, rMuzzle, len, z0, m }) {66  part(g, zCyl(rBreech, rMuzzle, len), m, 0, 0, z0 - len / 2, [HALF_PI, 0, 0]);67  return z0 - len;68}6970function stock(g, { len, m, gripM, pad = true, cheek = true, grip = true, rubberM = mat('rubber') }) {71  const z0 = 0.1;72  part(g, box(0.07, 0.13, len), m, 0, -0.025, z0 + len / 2);73  if (cheek) part(g, box(0.075, 0.03, len * 0.55), m, 0, 0.045, z0 + len * 0.35);74  if (pad) part(g, box(0.078, 0.15, 0.03), rubberM, 0, -0.025, z0 + len + 0.015);75  if (grip) part(g, box(0.05, 0.14, 0.055), gripM || m, 0, -0.13, -0.02, [-0.3, 0, 0]);76}7778function triggerGroup(g, m) {79  part(g, new TorusGeometry(0.035, 0.007, 8, 16, Math.PI), m, 0, -0.075, -0.02, [0, HALF_PI, Math.PI]);80  part(g, box(0.012, 0.03, 0.012), mat('rubber'), 0, -0.05, -0.03);81}8283function scope(g, { y, z, tube = 0.028, tubeLen = 0.26, bell = 0.036, bellLen = 0.07, eye = 0.04, eyeLen = 0.06 }) {84  const sm = mat('steel', 0x1a1d20);85  part(g, box(0.035, 0.04, 0.085), sm, 0, y - tube - 0.012, z - 0.06);86  part(g, box(0.035, 0.04, 0.085), sm, 0, y - tube - 0.012, z + 0.06);87  part(g, zCyl(tube, tube, tubeLen), sm, 0, y, z, [HALF_PI, 0, 0]);88  const frontZ = z - tubeLen / 2 - bellLen / 2;89  part(g, zCyl(bell * 0.85, bell, bellLen), sm, 0, y, frontZ, [HALF_PI, 0, 0]);90  part(g, zCyl(bell * 0.9, bell * 0.9, 0.004), mat('glass'), 0, y, z - tubeLen / 2 - bellLen - 0.002, [HALF_PI, 0, 0]);91  const eyeZ = z + tubeLen / 2 + eyeLen / 2;92  part(g, zCyl(eye * 1.1, eye * 0.95, eyeLen), sm, 0, y, eyeZ, [HALF_PI, 0, 0]);93  part(g, zCyl(eye * 0.9, eye * 0.9, 0.004), mat('glass'), 0, y, z + tubeLen / 2 + eyeLen + 0.002, [HALF_PI, 0, 0]);94  part(g, yCyl(0.014, 0.014, 0.03, 12), mat('brass'), 0, y + tube + 0.015, z);95  part(g, yCyl(0.012, 0.012, 0.03, 12), mat('brass'), tube + 0.012, y, z + 0.03, [0, 0, HALF_PI]);96}9798function boltHandle(g, { z, x = 0.05, m, brass }) {99  const bolt = new Group();100  g.add(bolt);101  part(bolt, zCyl(0.014, 0.014, 0.12), m, 0, 0.018, z);102  part(bolt, yCyl(0.006, 0.006, 0.05, 8), m, x, 0.018, z, [0, 0, HALF_PI]);103  part(bolt, new SphereGeometry(0.011, 10, 8), brass, x + 0.028, 0.018, z);104  return bolt;105}106107function magBox(g, { w, h, d, y, z, m, rot = [0, 0, 0] }) {108  const grp = new Group();109  grp.position.set(0, y, z);110  grp.rotation.set(rot[0], rot[1], rot[2]);111  g.add(grp);112  part(grp, box(w, h, d), m, 0, 0, 0);113  return grp;114}115116function flash(g, z, color = 0xffd08a, r = 0.035) {117  const m = new Mesh(new SphereGeometry(r, 8, 6), new MeshBasicMaterial({ color, transparent: true, opacity: 0.9 }));118  m.position.set(0, 0, z);119  m.visible = false;120  g.add(m);121  return m;122}123124function buildHunter() {125  const g = new Group();126  const steel = mat('steel', 0x2a2e33);127  const dark = mat('steel', 0x111417);128  const wood = mat('wood', 0x6b4a2b);129  const brass = mat('brass');130  receiver(g, { len: 0.3, w: 0.07, h: 0.1, z: -0.05, m: steel });131  for (let i = 0; i < 7; i++) part(g, box(0.012, 0.008, 0.016), dark, 0, 0.062, -0.2 + i * 0.04);132  part(g, box(0.004, 0.034, 0.1), dark, 0.036, 0.008, -0.06);133  const muzzleZ = barrel(g, { rBreech: 0.017, rMuzzle: 0.013, len: 0.62, z0: -0.2, m: steel });134  part(g, zCyl(0.018, 0.018, 0.05, 16), steel, 0, 0, muzzleZ + 0.025, [HALF_PI, 0, 0]);135  part(g, zCyl(0.019, 0.019, 0.008, 16), dark, 0, 0, muzzleZ + 0.004, [HALF_PI, 0, 0]);136  part(g, box(0.03, 0.03, 0.006), dark, 0, 0, muzzleZ + 0.052);137  part(g, box(0.06, 0.05, 0.26), wood, 0, -0.045, -0.33);138  part(g, yCyl(0.008, 0.008, 0.024, 10), steel, 0, -0.09, -0.46);139  stock(g, { len: 0.36, m: wood, pad: true, cheek: true });140  part(g, yCyl(0.008, 0.008, 0.024, 10), steel, 0, -0.1, 0.37);141  triggerGroup(g, steel);142  part(g, box(0.05, 0.06, 0.08), steel, 0, -0.085, -0.06);143  part(g, box(0.052, 0.012, 0.082), dark, 0, -0.115, -0.06);144  scope(g, { y: 0.12, z: -0.05, tube: 0.026, tubeLen: 0.26, bell: 0.034, bellLen: 0.06, eye: 0.034, eyeLen: 0.05 });145  const bolt = boltHandle(g, { z: -0.07, m: steel, brass });146  const f = flash(g, muzzleZ - 0.06);147  return { group: g, bolt, mag: null, flash: f, cycle: 'bolt', cycleTime: 0.5, travel: 0.05 };148}149150function buildDmr() {151  const g = new Group();152  const dark = mat('steel', 0x22262a);153  const poly = mat('poly', 0x1f2a1f);154  receiver(g, { len: 0.32, w: 0.075, h: 0.1, z: -0.06, m: dark });155  const muzzleZ = barrel(g, { rBreech: 0.02, rMuzzle: 0.018, len: 0.56, z0: -0.22, m: dark });156  part(g, zCyl(0.03, 0.03, 0.3, 20), poly, 0, 0, -0.38, [HALF_PI, 0, 0]);157  part(g, zCyl(0.024, 0.024, 0.06, 12), dark, 0, 0, muzzleZ + 0.03, [HALF_PI, 0, 0]);158  part(g, box(0.07, 0.07, 0.16), poly, 0, -0.04, -0.28);159  for (const x of [-0.02, 0.02]) {160    part(g, box(0.012, 0.14, 0.012), dark, x, -0.1, -0.5, [0.25, 0, 0]);161  }162  stock(g, { len: 0.34, m: poly, gripM: poly, pad: true, cheek: true });163  triggerGroup(g, dark);164  magBox(g, { w: 0.05, h: 0.13, d: 0.06, y: -0.11, z: -0.07, m: dark, rot: [0.1, 0, 0] });165  scope(g, { y: 0.13, z: -0.06, tube: 0.03, tubeLen: 0.3, bell: 0.04, bellLen: 0.07, eye: 0.04, eyeLen: 0.06 });166  const bolt = new Group();167  g.add(bolt);168  part(bolt, box(0.02, 0.025, 0.06), dark, 0.04, 0.035, -0.02);169  part(bolt, box(0.035, 0.012, 0.012), dark, 0.055, 0.035, -0.02);170  const f = flash(g, muzzleZ - 0.06, 0xffc26b, 0.04);171  return { group: g, bolt, mag: null, flash: f, cycle: 'slide', cycleTime: 0.1, travel: 0.02 };172}173174function buildGhost() {175  const g = new Group();176  const body = mat('steel', 0x3b3f44);177  const black = mat('poly', 0x141418);178  receiver(g, { len: 0.24, w: 0.07, h: 0.1, z: -0.04, m: body });179  const muzzleZ = barrel(g, { rBreech: 0.012, rMuzzle: 0.012, len: 0.25, z0: -0.16, m: body });180  part(g, zCyl(0.026, 0.026, 0.3, 20), black, 0, 0, muzzleZ - 0.14, [HALF_PI, 0, 0]);181  part(g, zCyl(0.028, 0.028, 0.02, 20), body, 0, 0, muzzleZ - 0.29, [HALF_PI, 0, 0]);182  stock(g, { len: 0.28, m: black, gripM: black, pad: true, cheek: false });183  triggerGroup(g, body);184  magBox(g, { w: 0.05, h: 0.07, d: 0.06, y: -0.08, z: -0.05, m: body });185  scope(g, { y: 0.11, z: -0.04, tube: 0.02, tubeLen: 0.18, bell: 0.026, bellLen: 0.05, eye: 0.026, eyeLen: 0.04 });186  const bolt = boltHandle(g, { z: -0.06, m: body, brass: mat('steel', 0x222222) });187  const f = flash(g, muzzleZ - 0.25, 0x9fd8ff, 0.02);188  return { group: g, bolt, mag: null, flash: f, cycle: 'bolt', cycleTime: 0.45, travel: 0.04 };189}190191function buildCarbine() {192  const g = new Group();193  const olive = mat('poly', 0x3a3f2a);194  const black = mat('poly', 0x17171a);195  receiver(g, { len: 0.3, w: 0.065, h: 0.09, z: -0.06, m: olive, top: 0.01 });196  for (let i = 0; i < 6; i++) part(g, box(0.035, 0.012, 0.02), black, 0, 0.055, -0.27 + i * 0.045);197  const muzzleZ = barrel(g, { rBreech: 0.011, rMuzzle: 0.011, len: 0.42, z0: -0.2, m: black });198  part(g, zCyl(0.04, 0.04, 0.3, 16), black, 0, 0, -0.38, [HALF_PI, 0, 0]);199  part(g, zCyl(0.014, 0.014, 0.04, 10), black, 0, 0, muzzleZ + 0.02, [HALF_PI, 0, 0]);200  part(g, box(0.035, 0.1, 0.04), black, 0, -0.08, -0.36);201  stock(g, { len: 0.26, m: black, gripM: black, pad: true, cheek: false });202  triggerGroup(g, black);203  part(g, box(0.04, 0.05, 0.06), black, 0, 0.08, -0.06);204  part(g, box(0.03, 0.03, 0.004), mat('glass'), 0, 0.08, -0.095);205  part(g, new SphereGeometry(0.005, 6, 4), mat('glow', 0xff2020), 0, 0.08, -0.06);206  magBox(g, { w: 0.045, h: 0.15, d: 0.06, y: -0.13, z: -0.04, m: olive, rot: [0.15, 0, 0] });207  const mag = g.children[g.children.length - 1];208  const bolt = new Group();209  g.add(bolt);210  part(bolt, box(0.02, 0.02, 0.1), black, 0, 0.065, -0.02);211  const f = flash(g, muzzleZ - 0.04, 0xffd08a, 0.03);212  return { group: g, bolt, mag, flash: f, cycle: 'slide', cycleTime: 0.08, travel: 0.012 };213}214215function buildLrm() {216  const g = new Group();217  const steel = mat('steel', 0x2f2a2a);218  const tan = mat('poly', 0x4a3f33);219  const brass = mat('brass');220  receiver(g, { len: 0.36, w: 0.11, h: 0.12, z: -0.05, m: steel, top: 0.014 });221  const muzzleZ = barrel(g, { rBreech: 0.026, rMuzzle: 0.024, len: 0.85, z0: -0.23, m: steel });222  part(g, zCyl(0.034, 0.034, 0.09, 16), steel, 0, 0, muzzleZ + 0.05, [HALF_PI, 0, 0]);223  part(g, box(0.07, 0.012, 0.09), mat('rubber'), 0, 0.032, muzzleZ + 0.05);224  part(g, box(0.07, 0.012, 0.09), mat('rubber'), 0, -0.032, muzzleZ + 0.05);225  part(g, box(0.08, 0.08, 0.3), tan, 0, -0.06, -0.4);226  for (const x of [-0.03, 0.03]) part(g, box(0.014, 0.15, 0.014), steel, x, -0.12, -0.56, [0.2, 0, 0]);227  stock(g, { len: 0.34, m: tan, gripM: tan, pad: true, cheek: true });228  triggerGroup(g, steel);229  magBox(g, { w: 0.07, h: 0.12, d: 0.08, y: -0.12, z: -0.08, m: steel });230  scope(g, { y: 0.16, z: -0.06, tube: 0.034, tubeLen: 0.34, bell: 0.05, bellLen: 0.09, eye: 0.05, eyeLen: 0.07 });231  const bolt = boltHandle(g, { z: -0.08, x: 0.065, m: steel, brass });232  const f = flash(g, muzzleZ - 0.08, 0xffb060, 0.06);233  return { group: g, bolt, mag: null, flash: f, cycle: 'bolt', cycleTime: 0.8, travel: 0.06 };234}235236function buildRailgun() {237  const g = new Group();238  const hull = mat('steel', 0x1d2b3a);239  const cyan = mat('glow', 0x40e0ff);240  receiver(g, { len: 0.36, w: 0.09, h: 0.12, z: -0.1, m: hull, top: 0.01 });241  const muzzleZ = barrel(g, { rBreech: 0.024, rMuzzle: 0.02, len: 0.85, z0: -0.28, m: hull });242  for (let i = 0; i < 10; i++) {243    part(g, new TorusGeometry(0.042, 0.007, 8, 20), cyan, 0, 0, -0.32 - i * 0.07);244  }245  for (const x of [-0.046, 0.046]) part(g, box(0.004, 0.03, 0.8), cyan, x, 0, -0.62);246  stock(g, { len: 0.3, m: hull, gripM: hull, pad: true, cheek: false });247  for (let i = 0; i < 3; i++) part(g, box(0.05, 0.02, 0.04), cyan, 0, 0.075, 0.26 + i * 0.07);248  triggerGroup(g, hull);249  magBox(g, { w: 0.055, h: 0.1, d: 0.06, y: -0.11, z: -0.05, m: hull });250  scope(g, { y: 0.13, z: -0.06, tube: 0.02, tubeLen: 0.22, bell: 0.03, bellLen: 0.05, eye: 0.026, eyeLen: 0.04 });251  const f = flash(g, muzzleZ - 0.05, 0x66f0ff, 0.06);252  return { group: g, bolt: null, mag: null, flash: f, cycle: 'none', cycleTime: 0.3, travel: 0 };253}254255const BUILDERS = {256  hunter: buildHunter,257  dmr: buildDmr,258  ghost: buildGhost,259  carbine: buildCarbine,260  lrm: buildLrm,261  railgun: buildRailgun,262};263264export function buildViewModel(id) {265  const vm = BUILDERS[id]();266  vm.group.position.set(0.24, -0.22, -0.46);267  vm.restZ = vm.group.position.z;268  vm.restY = vm.group.position.y;269  vm.magBaseY = vm.mag ? vm.mag.position.y : 0;270  vm.shotT = 10;271  return vm;272}273274export function animateViewModel(vm, dt, { recoil, reloadProgress, scoped, sprinting }) {275  vm.shotT += dt;276  vm.group.visible = !scoped;277  vm.sprintK = (vm.sprintK ?? 0) + ((sprinting ? 1 : 0) - (vm.sprintK ?? 0)) * Math.min(1, dt * 8);278  vm.group.rotation.z = vm.sprintK * 0.35;279  vm.group.position.y = vm.restY - vm.sprintK * 0.12;280  const kick = Math.max(0, 1 - vm.shotT / 0.1);281  vm.group.rotation.x = recoil * 1.5 + kick * 0.04;282  vm.group.position.z = vm.restZ + kick * 0.02;283284  if (vm.bolt && vm.cycle !== 'none') {285    const c = Math.min(1, vm.shotT / vm.cycleTime);286    vm.bolt.position.z = Math.sin(c * Math.PI) * vm.travel;287  }288289  if (vm.mag) {290    vm.mag.position.y = vm.magBaseY - Math.sin(reloadProgress * Math.PI) * 0.16;291  }292293  vm.flash.visible = vm.shotT < 0.05;294  if (vm.flash.visible) vm.flash.scale.setScalar(0.8 + Math.random() * 0.6);295}