All files / components/shared/three/models fighterMeshGeometry.ts

94.82% Statements 110/116
80.64% Branches 25/31
100% Functions 11/11
100% Lines 106/106

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189            9x                           13x 13x 13x 13x 13x     13x 13x 13x         1238x 1238x 1238x       939x 939x 939x                 195x 195x 195x 195x     195x 195x   195x 195x 195x 195x 195x 195x 195x 195x 663x 6936x 6936x 6936x 6936x 6936x 6936x 6936x 6936x 6936x 4896x 4896x 4896x       195x 195x 195x 195x 195x                     549x 549x 549x 549x 111x 111x   549x 549x 549x 549x 549x 549x 549x 549x 48228x 48228x 48228x   549x                 744x 744x 744x 744x         13x 744x 13x 13x 13x                     13x 13x 13x 13x 13x 364x 364x 364x 364x 364x 351x 351x 351x   364x 364x 364x   13x 13x 13x 26x 26x 26x 104x 104x 104x 104x 104x 104x     13x 13x 468x   13x    
import * as THREE from "three";
import { mergeGeometries } from "three/addons/utils/BufferGeometryUtils.js";
import type { SkeletalRig } from "../../../../types/skeletal";
 
type Pair = readonly [first: number, second: number];
 
export const FIGHTER_FINGERS = ["index", "middle", "ring", "pinky"] as const;
 
export interface CostumeRing {
  readonly center: THREE.Vector3;
  readonly radius: Pair;
  readonly blend: number;
  readonly shade?: number;
}
 
/**
 * Collects indexed costume surfaces in bind space. Each ring can blend between
 * two neighboring bones; rigid details use a single influence.
 */
export class FighterMeshGeometry {
  private readonly surfaces: THREE.BufferGeometry[] = [];
  private readonly color = new THREE.Color();
  private readonly transform = new THREE.Quaternion();
  private readonly axisU = new THREE.Vector3();
  private readonly axisV = new THREE.Vector3();
 
  constructor(
    private readonly bones: ReadonlyMap<string, number>,
    private readonly bindPositions: ReadonlyMap<string, THREE.Vector3>,
    private readonly sides: number,
  ) {}
 
  /** Position a costume detail relative to a named joint in bind space. */
  at(name: string, x = 0, y = 0, z = 0): THREE.Vector3 {
    const origin = this.bindPositions.get(name);
    Iif (!origin) throw new Error(`Missing fighter bind joint: ${name}`);
    return new THREE.Vector3(origin.x + x, origin.y + y, origin.z + z);
  }
 
  private index(name: string): number {
    const index = this.bones.get(name);
    Iif (index === undefined) throw new Error(`Missing fighter skin joint: ${name}`);
    return index;
  }
 
  tube(options: {
    readonly joints: readonly [string, string];
    readonly rings: readonly CostumeRing[];
    readonly axis: "x" | "y";
    readonly color: number;
  }): void {
    const { joints, rings, axis, color } = options;
    this.axisU.set(axis === "y" ? 1 : 0, axis === "x" ? 1 : 0, 0);
    const first = rings[0];
    const last = rings[rings.length - 1];
    // Choose the second axis so the same index winding faces outward on
    // descending legs, ascending torso surfaces, and both mirrored sleeves.
    const sign = last.center[axis] >= first.center[axis] ? 1 : -1;
    this.axisV.set(0, 0, axis === "y" ? -sign : sign);
 
    const positions: number[] = [];
    const indices: number[] = [];
    const skinIndices: number[] = [];
    const weights: number[] = [];
    const colors: number[] = [];
    const firstIndex = this.index(joints[0]);
    const secondIndex = this.index(joints[1]);
    rings.forEach(({ center, radius, blend, shade = 1 }, row) => {
      for (let column = 0; column < this.sides; column++) {
        const angle = (column / this.sides) * Math.PI * 2;
        const u = Math.cos(angle) * radius[0];
        const v = Math.sin(angle) * radius[1];
        positions.push(center.x + u * this.axisU.x, center.y + u * this.axisU.y, center.z + v * this.axisV.z);
        skinIndices.push(firstIndex, secondIndex, 0, 0);
        weights.push(1 - blend, blend, 0, 0);
        this.color.setHex(color).multiplyScalar(shade);
        colors.push(this.color.r, this.color.g, this.color.b);
        if (row === 0) continue;
        const a = (row - 1) * this.sides + column;
        const b = (row - 1) * this.sides + (column + 1) % this.sides;
        indices.push(a, b, a + this.sides, b, b + this.sides, a + this.sides);
      }
    });
 
    const geometry = new THREE.BufferGeometry();
    geometry.setIndex(indices);
    geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3));
    geometry.computeVertexNormals();
    this.addSurface(geometry, colors, skinIndices, weights);
  }
 
  detail(options: {
    readonly joint: string;
    readonly geometry: THREE.BufferGeometry;
    readonly at: THREE.Vector3;
    readonly size: readonly [number, number, number];
    readonly color: number;
    readonly roll?: number;
  }): void {
    const { joint, geometry, at, size, color, roll = 0 } = options;
    const surface = geometry.clone();
    surface.scale(...size);
    if (roll !== 0) {
      this.transform.setFromAxisAngle(this.axisV.set(0, 0, 1), roll);
      surface.applyQuaternion(this.transform);
    }
    surface.translate(at.x, at.y, at.z);
    surface.deleteAttribute("uv");
    const index = this.index(joint);
    const colors: number[] = [];
    const skinIndices: number[] = [];
    const weights: number[] = [];
    this.color.setHex(color);
    for (let i = 0; i < surface.getAttribute("position").count; i++) {
      colors.push(this.color.r, this.color.g, this.color.b);
      skinIndices.push(index, 0, 0, 0);
      weights.push(1, 0, 0, 0);
    }
    this.addSurface(surface, colors, skinIndices, weights);
  }
 
  private addSurface(
    geometry: THREE.BufferGeometry,
    colors: number[],
    skinIndices: number[],
    weights: number[],
  ): void {
    geometry.setAttribute("color", new THREE.Float32BufferAttribute(colors, 3));
    geometry.setAttribute("skinIndex", new THREE.Uint16BufferAttribute(skinIndices, 4));
    geometry.setAttribute("skinWeight", new THREE.Float32BufferAttribute(weights, 4));
    this.surfaces.push(geometry);
  }
 
  /** Merge surfaces into one draw and release all construction geometry. */
  finish(): THREE.BufferGeometry {
    const merged = mergeGeometries(this.surfaces);
    this.surfaces.forEach((surface) => surface.dispose());
    this.surfaces.length = 0;
    Iif (!merged) throw new Error("Fighter costume surfaces use incompatible attributes");
    return merged;
  }
}
 
/** Create independent Three.js bones in the animation rig's bind pose. */
export function bindFighterRig(rig: SkeletalRig): {
  readonly root: THREE.Bone;
  readonly bones: THREE.Bone[];
  readonly indices: ReadonlyMap<string, number>;
  readonly positions: ReadonlyMap<string, THREE.Vector3>;
} {
  const bones: THREE.Bone[] = [];
  const byName = new Map<string, THREE.Bone>();
  const indices = new Map<string, number>();
  const positions = new Map<string, THREE.Vector3>();
  for (const [name, source] of rig.bones) {
    const bone = new THREE.Bone();
    bone.name = name;
    bone.position.copy(source.restPosition);
    bone.rotation.copy(source.restRotation);
    if (source.parent) {
      const parent = byName.get(source.parent.name);
      Iif (!parent) throw new Error(`Fighter bind hierarchy is not parent-first: ${name}`);
      parent.add(bone);
    }
    byName.set(name, bone);
    indices.set(name, bones.length);
    bones.push(bone);
  }
  const root = byName.get(rig.root.name);
  Iif (!root) throw new Error("Fighter rig has no root");
  for (const [suffix, sign] of [["L", -1], ["R", 1]] as const) {
    const hand = byName.get(`hand_${suffix}`);
    Iif (!hand) throw new Error(`Fighter rig has no ${suffix} hand`);
    FIGHTER_FINGERS.forEach((finger, index) => {
      const joint = new THREE.Bone();
      joint.name = `${finger}_${suffix}`;
      joint.position.set(sign * 0.049, 0, (index - 1.5) * 0.02);
      hand.add(joint);
      indices.set(joint.name, bones.length);
      bones.push(joint);
    });
  }
  root.updateMatrixWorld(true);
  for (const bone of bones) {
    positions.set(bone.name, new THREE.Vector3().setFromMatrixPosition(bone.matrixWorld));
  }
  return { root, bones, indices, positions };
}