xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / web / js / figures.js 208 lines14.4 kB Raw
1import * as THREE from '../vendor/three.module.js';
2import { RACES, CLASSES } from './looks.js';
3
4const SCALE = 1.7;
5const STRIDES = 3;
6const SWING_SECONDS = 1.1;
7const FIGHT_SECONDS = 0.8;
8const CAST_SECONDS = 1.3;
9const RANGED = ['staff', 'longbow'];
10const BLEND_RATE = 8;
11const HATS = ['helmet', 'wizardHat', 'hood', 'crown', 'hornedHelm', 'tricorn'];
12const UNDER_HAT = ['hood', 'circlet', 'hair', 'slick', 'topknot'];
13
14const WOOD = '#7a5230', IRON = '#8a8f98', BELT = '#3a2a1e', MOUTH = '#6a2a22';
15const BARK = '#4a3220', STRAW = '#d9b85a', IVORY = '#f0ead6', SILVER = '#d8dce6', WAR_PAINT = '#a02a20', GOLD = '#d8b040', ARCANE = '#8a7aff', BONE = '#e6dec8';
16const HALO = new THREE.SphereGeometry(0.11, 10, 8);
17const haloMaterial = new THREE.MeshBasicMaterial({ color: '#a090ff', transparent: true, opacity: 0.35, depthWrite: false, blending: THREE.AdditiveBlending });
18
19const materials = new Map();
20export function mesh(geometry, color, x = 0, y = 0, z = 0, rx = 0, rz = 0) {
21  if (!materials.has(color)) {
22    const glow = color === ARCANE ? { emissive: color, emissiveIntensity: 1.2 } : {};
23    materials.set(color, new THREE.MeshStandardMaterial({ color, flatShading: true, roughness: 0.8, ...glow }));
24  }
25  const m = new THREE.Mesh(geometry, materials.get(color));
26  m.position.set(x, y, z);
27  m.rotation.set(rx, 0, rz);
28  m.castShadow = true;
29  return m;
30}
31export const box = (w, h, d) => new THREE.BoxGeometry(w, h, d);
32export const spike = (r, l, n = 4) => new THREE.ConeGeometry(r, l, n).translate(0, l / 2, 0);
33const sides = (make) => [-1, 1].flatMap(make);
34const cap = (r) => new THREE.SphereGeometry(r, 7, 4, 0, Math.PI * 2, 0, Math.PI / 2);
35
36const TOOLS = {
37  hammer: () => [mesh(box(0.025, 0.3, 0.025), WOOD, 0, 0.15), mesh(box(0.06, 0.06, 0.13), IRON, 0, 0.29, 0.02)],
38  pick: () => [mesh(box(0.025, 0.32, 0.025), WOOD, 0, 0.16), mesh(box(0.025, 0.035, 0.26), IRON, 0, 0.3, 0.02)],
39  axe: () => [mesh(box(0.028, 0.34, 0.028), WOOD, 0, 0.17), mesh(box(0.016, 0.11, 0.1), IRON, 0, 0.28, 0.06)],
40  sickle: () => [mesh(box(0.025, 0.14, 0.025), WOOD, 0, 0.07), mesh(new THREE.TorusGeometry(0.07, 0.01, 3, 8, Math.PI).rotateY(Math.PI / 2), SILVER, 0, 0.14, 0.07)],
41  mallet: () => [mesh(box(0.025, 0.22, 0.025), WOOD, 0, 0.11), mesh(new THREE.CylinderGeometry(0.05, 0.05, 0.12, 6), '#5a3a20', 0, 0.24, 0, Math.PI / 2)],
42  sword: () => [mesh(box(0.025, 0.08, 0.025), BELT, 0, 0.02), mesh(box(0.025, 0.025, 0.12), IRON, 0, 0.07), mesh(box(0.015, 0.34, 0.045), SILVER, 0, 0.25)],
43  staff: () => [mesh(box(0.03, 0.7, 0.03), WOOD, 0, 0.2), mesh(new THREE.OctahedronGeometry(0.09, 0), ARCANE, 0, 0.66), new THREE.Mesh(HALO, haloMaterial).translateY(0.66), mesh(new THREE.TorusGeometry(0.07, 0.014, 3, 6), GOLD, 0, 0.56, 0, Math.PI / 2), ...sides((s) => mesh(spike(0.012, 0.1, 3), GOLD, s * 0.07, 0.6, 0, 0, -s * 0.5))],
44  mace: () => [mesh(box(0.025, 0.24, 0.025), WOOD, 0, 0.12), mesh(new THREE.IcosahedronGeometry(0.055, 0), IRON, 0, 0.26)],
45  dagger: () => [mesh(box(0.022, 0.06, 0.022), BELT, 0, 0.02), mesh(box(0.012, 0.15, 0.035), SILVER, 0, 0.12)],
46  spear: () => [mesh(box(0.022, 0.62, 0.022), WOOD, 0, 0.2), mesh(spike(0.035, 0.12, 4), BONE, 0, 0.5)],
47  trunk: () => [mesh(new THREE.CylinderGeometry(0.06, 0.045, 0.75, 6), WOOD, 0, 0.3), mesh(box(0.04, 0.08, 0.04), BARK, 0.05, 0.45)],
48  longbow: () => [mesh(new THREE.TorusGeometry(0.3, 0.012, 3, 10, Math.PI).rotateZ(-Math.PI / 2).rotateY(Math.PI / 2), WOOD, 0, 0.05, -0.08), mesh(box(0.004, 0.6, 0.004), IVORY, 0, 0.05, -0.08)],
49  warhammer: () => [mesh(box(0.025, 0.4, 0.025), WOOD, 0, 0.2), mesh(box(0.08, 0.08, 0.18), IRON, 0, 0.38)],
50  greatsword: () => [mesh(box(0.025, 0.12, 0.025), BELT, 0, 0.04), mesh(box(0.03, 0.03, 0.18), GOLD, 0, 0.1), mesh(box(0.018, 0.5, 0.06), SILVER, 0, 0.36)],
51};
52
53const HEAD = {
54  hair: ({ r, hair }) => [mesh(cap(r * 1.08), hair, 0, r, -r * 0.1, -0.35)],
55  slick: ({ r, hair }) => [mesh(cap(r * 1.06), hair, 0, r * 1.02, -r * 0.05, -0.2), mesh(spike(r * 0.18, r * 0.45, 3), hair, 0, r * 1.75, r * 0.7, Math.PI * 0.95)],
56  longHair: (look) => [...HEAD.hair(look), mesh(box(look.r * 1.7, look.r * 2, look.r * 0.5), look.hair, 0, look.r * 0.5, -look.r * 0.7)],
57  hood: ({ r, cloth }) => [mesh(new THREE.ConeGeometry(r * 1.3, r * 2.2, 6), cloth, 0, r * 2, -r * 0.25, -0.3)],
58  beard: ({ r, hair }) => [mesh(new THREE.ConeGeometry(r * 0.8, r * 1.5, 5), hair, 0, r * 0.2, r * 0.55, Math.PI)],
59  circlet: ({ r }) => [mesh(new THREE.TorusGeometry(r, r * 0.08, 3, 10), SILVER, 0, r * 1.3, 0, Math.PI / 2)],
60  topknot: ({ r, hair }) => [mesh(spike(r * 0.3, r * 0.9, 5), hair, 0, r * 1.6, -r * 0.3, -0.4)],
61  warPaint: ({ r }) => [mesh(box(r * 1.75, r * 0.22, r * 0.9), WAR_PAINT, 0, r * 1.1, r * 0.35)],
62  mask: ({ r }) => [mesh(box(r * 1.8, r * 0.5, r * 0.9), '#2a2420', 0, r * 0.62, r * 0.4)],
63  fangs: ({ r }) => sides((s) => mesh(spike(r * 0.06, r * 0.22, 3), IVORY, s * r * 0.18, r * 0.7, r * 0.93, Math.PI)),
64  longEars: ({ r, skin }) => sides((s) => mesh(spike(r * 0.2, r * 1.1), skin, s * r * 0.75, r * 1.05, -r * 0.1, 0, -s * (Math.PI / 2 - 0.6))),
65  bigEars: ({ r, skin }) => sides((s) => mesh(spike(r * 0.45, r * 1.2).scale(1, 1, 0.35), skin, s * r * 0.75, r, 0, 0, -s * (Math.PI / 2 - 0.25))),
66  longNose: ({ r, skin }) => [mesh(spike(r * 0.18, r * 0.9), skin, 0, r * 0.9, r * 0.7, Math.PI / 2 + 0.3)],
67  snout: ({ r, skin }) => [mesh(box(r * 0.9, r * 0.6, r * 1.1), skin, 0, r * 0.75, r * 1.2), mesh(box(r * 0.3, r * 0.12, 0.012), '#1a1410', 0, r * 0.95, r * 1.76)],
68  frill: ({ r, hair }) => [0, 1, 2].map((i) => mesh(spike(r * 0.16, r * 0.8, 3), hair, 0, r * (1.7 - i * 0.35), -r * (0.5 + i * 0.25), -0.9 - i * 0.3)),
69  tusks: ({ r }) => sides((s) => mesh(spike(r * 0.1, r * 0.45), IVORY, s * r * 0.35, r * 0.3, r * 0.7)),
70  helmet: ({ r }) => [mesh(cap(r * 1.15), IRON, 0, r * 1.05), mesh(box(r * 0.15, r * 0.6, r * 0.1), IRON, 0, r * 0.95, r * 1.05)],
71  hornedHelm: ({ r }) => [mesh(cap(r * 1.15), IRON, 0, r * 1.05), mesh(box(r * 2.4, r * 0.18, r * 2.4), BELT, 0, r * 1.15),
72    ...sides((s) => [mesh(spike(r * 0.22, r * 1.3, 5), IVORY, s * r * 1.1, r * 1.5, 0, 0, -s * 0.9), mesh(spike(r * 0.16, r * 0.8, 5), IVORY, s * r * 1.75, r * 2.3, 0, 0, s * 0.3)]).flat()],
73  wizardHat: ({ r, cloth }) => [mesh(new THREE.CylinderGeometry(r * 1.8, r * 1.8, r * 0.1, 8), cloth, 0, r * 1.6), mesh(spike(r * 0.9, r * 2.6, 6), cloth, 0, r * 1.6, -r * 0.1, -0.25)],
74  crown: ({ r }) => [mesh(new THREE.CylinderGeometry(r * 0.95, r * 0.95, r * 0.35, 8, 1, true), GOLD, 0, r * 1.75), ...[0, 1, 2, 3, 4].map((i) => mesh(spike(r * 0.12, r * 0.4, 3), GOLD, Math.sin(i * 1.26) * r * 0.9, r * 1.9, Math.cos(i * 1.26) * r * 0.9))],
75  tricorn: ({ r }) => [mesh(new THREE.CylinderGeometry(r * 1.6, r * 1.6, r * 0.12, 3), '#1e1a18', 0, r * 1.65), mesh(new THREE.CylinderGeometry(r * 0.85, r, r * 0.55, 8), '#1e1a18', 0, r * 1.95), mesh(box(r * 0.9, r * 0.2, 0.02), GOLD, 0, r * 1.75, r * 0.95)],
76};
77
78const BODY = {
79  pauldrons: (b) => b.upper.add(...sides((s) => mesh(cap(b.armW * 1.5), IRON, s * (b.torsoR * 0.95 + b.armW * 0.4), b.torsoH * 0.98)), mesh(box(b.torsoR * 1.9, b.torsoH * 0.55, b.torsoR * 0.3), IRON, 0, b.torsoH * 0.62, b.torsoR * 0.85)),
80  shield: (b) => b.arms[1].add(mesh(new THREE.CylinderGeometry(0.17, 0.17, 0.035, 10).rotateZ(Math.PI / 2), b.look.accent, b.armW + 0.02, -b.armL * 0.7), mesh(new THREE.CylinderGeometry(0.19, 0.19, 0.02, 10).rotateZ(Math.PI / 2), IRON, b.armW, -b.armL * 0.7), mesh(new THREE.IcosahedronGeometry(0.045, 0), IRON, b.armW + 0.05, -b.armL * 0.7)),
81  robe: (b) => b.root.add(mesh(new THREE.CylinderGeometry(b.torsoR, b.torsoR * 1.6, b.legH, 8), b.look.cloth, 0, b.legH / 2)),
82  cloak: (b) => b.upper.add(mesh(box(b.torsoR * 2.1, b.torsoH + b.legH * 0.8, 0.02), b.look.cloth, 0, b.torsoH * 0.95 - (b.torsoH + b.legH * 0.8) / 2, -b.torsoR * 1.1)),
83  collar: (b) => b.upper.add(...sides((s) => mesh(box(b.torsoR * 0.9, b.torsoH * 0.6, 0.02), '#5a1420', s * b.torsoR * 0.55, b.torsoH * 1.15, -b.torsoR * 0.6, 0, s * 0.25))),
84  holySymbol: (b) => b.upper.add(mesh(box(0.02, 0.08, 0.012), GOLD, 0, b.torsoH * 0.62, b.torsoR * 0.95), mesh(box(0.055, 0.02, 0.012), GOLD, 0, b.torsoH * 0.68, b.torsoR * 0.95)),
85  quiver: (b) => b.upper.add(mesh(new THREE.CylinderGeometry(0.04, 0.035, 0.26, 6), BELT, b.torsoR * 0.5, b.torsoH * 0.7, -b.torsoR * 1.1, 0.3, -0.4), ...[0, 1, 2].map((i) => mesh(box(0.01, 0.06, 0.03), '#e8e0c8', b.torsoR * 0.5 + (i - 1) * 0.02 + 0.05, b.torsoH * 1.05, -b.torsoR * 1.2))),
86  bones: (b) => b.upper.add(...[0, 1, 2, 3].map((i) => mesh(spike(0.015, 0.07, 3), BONE, (i - 1.5) * b.torsoR * 0.45, b.torsoH * 0.78, b.torsoR * 1.02, Math.PI))),
87  tail: (b) => b.root.add(mesh(spike(b.torsoR * 0.55, b.legH * 2.2, 5), b.look.skin, 0, b.legH * 0.9, -b.torsoR * 0.8, -2.1)),
88  ribs: (b) => b.upper.add(...[0, 1, 2].map((i) => mesh(box(b.torsoR * 1.9, 0.016, 0.02), BONE, 0, b.torsoH * (0.4 + i * 0.17), b.torsoR * 1.04))),
89  mane: (b) => b.upper.add(mesh(box(b.torsoR * 1.2, b.torsoH * 0.5, b.torsoR * 0.8), b.look.hair, 0, b.torsoH * 0.95, -b.torsoR * 0.5)),
90};
91
92function random(seed) {
93  return () => {
94    seed = (seed + 0x6d2b79f5) | 0;
95    let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
96    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
97    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
98  };
99}
100
101const smooth = (x) => x * x * (3 - 2 * x);
102
103export function createFigure(raceName, kind, seed, accent) {
104  const race = RACES[raceName] || RACES.human;
105  const gear = CLASSES[kind];
106  const rand = random(seed + 1);
107  const pick = (list) => list[Math.floor(rand() * list.length)];
108  const look = { skin: pick(race.skin), hair: pick(race.hair), cloth: pick(gear?.cloth ?? race.cloth), r: 0.115 * race.head, accent };
109  const h = race.height * (0.95 + 0.1 * rand());
110  const w = race.width;
111  const [legH, legW, torsoH, torsoR, armL, armW] = [0.17 * h, 0.1 * w, 0.23 * h, 0.115 * w, 0.17 * h, 0.08 * w];
112  const [eyeWhite, eyeDark] = race.eyes || ['#f4f0e4', '#1a1410'];
113
114  const pivot = (x, y, ...children) => {
115    const group = new THREE.Group().add(...children);
116    group.position.set(x, y, 0);
117    return group;
118  };
119  const legColor = pick(race.legs);
120  const legs = [-1, 1].map((s) => pivot(s * legW * 0.6, legH, mesh(box(legW, legH, legW * 1.1), legColor, 0, -legH / 2), mesh(box(legW * 1.15, legH * 0.3, legW * 1.5), BELT, 0, -legH * 0.85, legW * 0.2)));
121  const arms = [-1, 1].map((s) => pivot(s * (torsoR * 0.9 + armW / 2), torsoH * 0.9, mesh(box(armW, armL, armW), look.cloth, 0, -armL / 2), mesh(box(armW * 1.1, armW * 1.1, armW * 1.1), look.skin, 0, -armL)));
122  const grip = pivot(0, -armL, ...TOOLS[gear?.tool ?? race.tool]());
123  grip.scale.setScalar(1.25);
124  grip.rotation.x = 1.9;
125  arms[0].add(grip);
126  const r = look.r;
127  const head = pivot(0, torsoH, mesh(new THREE.IcosahedronGeometry(r, 1), look.skin, 0, r),
128    mesh(box(r * 0.24, r * 0.4, r * 0.4), look.skin, 0, r * 0.92, r * 1.02),
129    mesh(box(r * 0.8, r * 0.1, 0.01), MOUTH, 0, r * 0.6, r * 0.9),
130    mesh(box(r * 1.7, r * 0.28, r * 0.2), look.skin, 0, r * 1.38, r * 0.82),
131    ...sides((s) => [mesh(box(r * 0.3, r * 0.26, 0.012), eyeWhite, s * r * 0.38, r * 1.12, r * 0.9),
132      mesh(box(r * 0.14, r * 0.2, 0.014), eyeDark, s * r * 0.38, r * 1.12, r * 0.91),
133      mesh(box(r * 0.46, r * 0.12, r * 0.1), look.hair, s * r * 0.38, r * 1.34, r * 0.9, 0, -s * 0.3)]).flat());
134  const upper = pivot(0, legH, ...arms, head,
135    mesh(new THREE.CylinderGeometry(torsoR * 0.9, torsoR * 1.1, torsoH, 8), look.cloth, 0, torsoH / 2),
136    mesh(new THREE.CylinderGeometry(torsoR * 1.12, torsoR * 1.12, torsoH * 0.12, 8), BELT, 0, torsoH * 0.15),
137    mesh(box(torsoR * 0.9, torsoH * 0.8, 0.012), accent, 0, torsoH * 0.5, torsoR * 1.02),
138    mesh(box(torsoR * 0.9, torsoH * 0.8, 0.012), accent, 0, torsoH * 0.5, -torsoR * 1.02),
139    ...sides((s) => mesh(new THREE.IcosahedronGeometry(armW * 0.95, 0), accent, s * (torsoR * 0.95 + armW * 0.4), torsoH * 0.95)));
140  const root = new THREE.Group().add(...legs, upper);
141  const body = { look, arms, upper, root, legH, torsoH, torsoR, armL, armW };
142  const hatted = (gear?.gear ?? []).some((g) => HATS.includes(g));
143  const wear = (name) => (HEAD[name] ? head.add(...HEAD[name](look)) : BODY[name]?.(body));
144  for (const [part, chance] of Object.entries(race.parts)) {
145    if (rand() < chance && !(hatted && UNDER_HAT.includes(part))) wear(part);
146  }
147  for (const name of gear?.gear ?? []) wear(name);
148  root.scale.setScalar(SCALE * (0.95 + 0.1 * rand()));
149
150  let clock = rand() * 10;
151  let stride = 0;
152  let strike = 0;
153  const blows = { fight: 0, cast: 0 };
154  const ranged = RANGED.includes(gear?.tool);
155  const poses = {
156    idle: () => {
157      const breath = Math.sin(clock * 2);
158      return [0, -0.06 * breath, 0.06 * breath, 0.02 * breath, 0.004 * breath, 0.6 * Math.sin(clock * 0.45) ** 3];
159    },
160    walk: () => {
161      const s = Math.sin(stride);
162      return [0.7 * s, -0.6 * s, 0.6 * s, 0.12, 0.02 * Math.abs(Math.cos(stride)), 0];
163    },
164    fight: () => {
165      const t = (clock / FIGHT_SECONDS) % 1;
166      const up = t < 0.5 ? smooth(t / 0.5) : t < 0.62 ? 1 - (t - 0.5) / 0.12 : 0;
167      blows.fight = t > 0.5 && t < 0.75 ? Math.sin(((t - 0.5) / 0.25) * Math.PI) : 0;
168      return [0.3 * blows.fight, -0.3 - 2.5 * up, 0.3 * up - 0.4, 0.45 - 0.5 * up, -0.01, 0];
169    },
170    cast: () => {
171      const t = (clock / CAST_SECONDS) % 1;
172      blows.cast = Math.max(0, Math.sin(t * Math.PI * 2)) ** 2;
173      return [0, -1.4 - 0.35 * blows.cast, -1.1, -0.08 + 0.1 * blows.cast, 0.01 * blows.cast, 0.15 * Math.sin(clock * 3)];
174    },
175    work: () => {
176      const t = (clock / SWING_SECONDS) % 1;
177      const raise = t < 0.7 ? smooth(t / 0.7) : 1 - ((t - 0.7) / 0.3) ** 2;
178      const arm = -0.3 - 2.5 * raise;
179      return [0.15, arm, arm * 0.7, 0.35 - 0.3 * raise, -0.02 * (1 - raise), 0];
180    },
181  };
182  const weights = { idle: 1, walk: 0, fight: 0, cast: 0, work: 0 };
183  const pose = [0, 0, 0, 0, 0, 0];
184
185  function animate(mode, dt, pace = 0) {
186    if (mode === 'fight' && ranged) mode = 'cast';
187    clock += dt;
188    stride += dt * pace * STRIDES * Math.PI;
189    pose.fill(0);
190    for (const m in weights) {
191      weights[m] += ((m === mode ? 1 : 0) - weights[m]) * Math.min(1, dt * BLEND_RATE);
192      poses[m]().forEach((v, i) => (pose[i] += v * weights[m]));
193    }
194    strike = blows.fight * weights.fight + blows.cast * weights.cast;
195    const [leg, right, left, lean, bob, turn] = pose;
196    legs[0].rotation.x = leg;
197    legs[1].rotation.x = -leg;
198    arms[0].rotation.x = right;
199    arms[1].rotation.x = left;
200    upper.rotation.x = lean;
201    upper.position.y = legH + bob;
202    head.rotation.y = turn;
203  }
204
205  const swing = () => (clock = ranged ? CAST_SECONDS * 0.05 : 0);
206
207  return { object: root, head, animate, swing, get strike() { return strike; }, radius: 0.3 * Math.sqrt(race.width) };
208}