File size: 16,795 Bytes
dd53693 4a0e872 dd53693 4a0e872 dd53693 4a0e872 dd53693 4a0e872 dd53693 6b5ce6d dd53693 | 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | // Procedural animation: every pose is a pure function of time, so nothing drifts and any frame can be rebuilt.
import * as THREE from 'three';
const { sin, cos, abs, PI, min, max, floor, atan2, sqrt } = Math;
const clamp = (v, a, b) => min(b, max(a, v));
const smooth = (u) => u * u * (3 - 2 * u);
export const SPEED = { walk: 1.5, run: 4.2, drive: 5.5, swim: 1.7, roll: 2.4, circle: 1.6, fly: 4.0, jump: 1.6, dance: 1.2 };
export const LOCO = new Set(['walk', 'run', 'drive', 'swim', 'roll']);
export const SPD_MUL = { slow: 0.45, fast: 2.0 };
export function speedOf(inst, act) {
const f = clamp(sqrt((inst.rig.h * inst.scale) / 1.3), 0.55, 2.2);
return (SPEED[act] ?? 1.5) * f;
}
// ------------------------------------------------------------------ paths
function path(inst, T, s) {
const mo = inst.motion;
if (mo.kind === 'patrol') {
const leg = (2 * mo.A) / mo.v, tt = 0.7, P = 2 * (leg + tt);
let u = (T + leg / 2) % P;
if (u < leg) { s.x += -mo.A + mo.v * u; s.yaw = PI / 2; }
else if (u < leg + tt) { s.x += mo.A; s.yaw = PI / 2 - PI * smooth((u - leg) / tt); }
else if (u < 2 * leg + tt) { s.x += mo.A - mo.v * (u - leg - tt); s.yaw = -PI / 2; }
else { s.x -= mo.A; s.yaw = -PI / 2 + PI * smooth((u - 2 * leg - tt) / tt); }
s.dist = mo.v * T;
} else if (mo.kind === 'circle') {
const a = mo.ph + (mo.v / mo.R) * T;
s.x = mo.cx + inst.off.x + mo.R * cos(a);
s.z = mo.cz + inst.off.z + mo.R * sin(a);
s.yaw = atan2(-sin(a), cos(a));
s.dist = mo.v * T;
} else if (mo.kind === 'fly') {
if (inst.rig.vertical) {
s.y += mo.alt * (0.55 + 0.45 * sin(T * 0.8));
s.x += sin(T * 0.5) * 0.6;
} else {
const a = mo.ph + mo.w * T;
s.x = mo.cx + inst.off.x + mo.Rx * cos(a);
s.z = mo.cz + inst.off.z + mo.Rz * sin(a);
s.yaw = atan2(-mo.Rx * sin(a), mo.Rz * cos(a));
s.y += mo.alt + sin(T * 1.3) * 0.25;
s.roll = inst.rig.floaty ? 0 : 0.28;
}
s.dist = 4 * T;
} else if (mo.kind === 'hopover') {
const k = floor(T / mo.P), u = T / mo.P - k, dir = k % 2 ? -1 : 1;
if (u < 0.62) {
const a = u / 0.62;
s.x = mo.cx + inst.off.x + dir * (-mo.d + 2 * mo.d * a);
s.y += mo.H * 4 * a * (1 - a);
s.yaw = dir * PI / 2;
s.air = a;
} else {
s.x = mo.cx + inst.off.x + dir * mo.d;
s.yaw = dir * (PI / 2) * (1 - 2 * smooth((u - 0.62) / 0.38));
s.air = -1;
}
s.z = mo.cz + inst.off.z;
}
}
// ------------------------------------------------------------------ gaits
function gait(g, ph, amp, run, dist) {
const b = g.body, L = g.legs, A = g.arms, s1 = sin(ph);
if (g.kind === 'quad') {
if (run) {
L[0].rotation.x += s1 * amp; L[1].rotation.x += sin(ph + 0.4) * amp;
L[2].rotation.x += sin(ph + 2.4) * amp; L[3].rotation.x += sin(ph + 2.8) * amp;
b.rotation.x += sin(ph + 1) * 0.07;
b.position.y += (1 - cos(ph)) * 0.06 * g.leg;
} else {
L[0].rotation.x += s1 * amp; L[3].rotation.x += s1 * amp;
L[1].rotation.x -= s1 * amp; L[2].rotation.x -= s1 * amp;
b.position.y += (1 - cos(2 * ph)) * 0.02 * g.leg;
}
if (g.head) g.head.rotation.x += sin(2 * ph) * 0.04;
if (g.tail) g.tail.rotation.z += sin(ph) * 0.25;
} else if (g.kind === 'biped') {
L[0].rotation.x += s1 * amp; L[1].rotation.x -= s1 * amp;
if (A) { A[0].rotation.x -= s1 * amp * 0.8; A[1].rotation.x += s1 * amp * 0.8; }
b.position.y += (1 - cos(2 * ph)) * 0.025 * g.leg;
if (run) b.rotation.x += 0.16;
if (g.tail) g.tail.rotation.y += sin(ph) * 0.2;
} else if (g.kind === 'bird') {
L[0].rotation.x += s1 * amp; L[1].rotation.x -= s1 * amp;
if (g.upright) b.rotation.z += s1 * 0.13;
else if (g.head) g.head.position.z += sin(2 * ph) * 0.03;
b.position.y += abs(s1) * 0.02;
} else if (g.kind === 'fish') {
g.tail.rotation.y += sin(ph * 1.4) * 0.55;
b.rotation.y += sin(ph * 1.4 - 1) * 0.12;
} else if (g.kind === 'vehicle') {
const r = g.board ? 0.06 : 0.36;
if (g.wheels) for (const w of g.wheels) w.rotation.x += dist / r;
else b.rotation.z += sin(ph * 0.5) * 0.03;
} else {
b.rotation.z += s1 * 0.1;
b.position.y += abs(s1) * 0.07 * g.h;
if (A) { A[0].rotation.x += s1 * 0.5; A[1].rotation.x -= s1 * 0.5; }
}
}
/** Rotate `body` by `a` around the X axis through its centre, keeping that centre at height c2. */
function tumble(g, a, c2) {
const c = g.h / 2;
g.body.rotation.x += a;
g.body.position.y += c2 - c * cos(a);
g.body.position.z += -c * sin(a);
}
function flap(g, T, rate, amp) {
if (!g.wings) return false;
g.wings.forEach((w, i) => {
const sx = i ? -1 : 1;
if (g.kind === 'bird' && !g.upright) w.rotation.z = sx * sin(T * rate) * amp;
else w.rotation.z += sx * sin(T * rate) * amp;
});
return true;
}
// ------------------------------------------------------------------ poses
const POSE = {
idle(i, g, T, s) {
g.body.scale.y *= 1 + sin(T * 2) * 0.012;
if (g.tail && g.kind !== 'fish') g.tail.rotation.z += sin(T * 3) * 0.18;
if (g.head && g.kind !== 'prop') g.head.rotation.y += sin(T * 0.7) * 0.25;
if (g.sway) g.head.rotation.z += sin(T * 1.1) * 0.035;
if (g.kind === 'fish') { g.tail.rotation.y += sin(T * 3) * 0.3; s.y += sin(T * 1.2) * 0.06; }
if (g.floaty) { s.y += (g.hover ?? 0.3) * 0.5 + sin(T * 1.4) * 0.12; s.roll += sin(T * 0.9) * 0.04; }
if (g.trunk) g.trunk.rotation.x += sin(T * 1.3) * 0.12;
},
move(i, g, T, s, act) {
const stride = max(0.22, (g.leg ?? g.h * 0.35) * i.scale * 1.25);
const ph = (s.dist / stride) * (act === 'run' ? 0.8 : 1);
gait(g, ph, act === 'run' ? 0.85 : act === 'swim' ? 0.35 : 0.5, act === 'run', s.dist / i.scale);
if (g.kind === 'fish') s.y += sin(T * 1.2) * 0.06;
},
swim(i, g, T, s) {
POSE.move(i, g, T, s, 'swim');
if (g.kind === 'biped' && g.arms) {
g.body.rotation.x += 1.2;
g.body.position.y += g.leg * 0.25;
g.arms[0].rotation.x += PI + T * 5;
g.arms[1].rotation.x += T * 5;
}
if (!s.water) s.y += g.kind === 'fish' ? 0 : 0.2;
s.yaw += sin(T * 3) * 0.08;
},
roll(i, g, T, s) {
if (g.roller) { g.roller.rotation.x += s.dist / i.scale / g.round; return; }
if (g.kind === 'vehicle' && g.wheels) { gait(g, 0, 0, false, s.dist / i.scale); return; }
tumble(g, s.dist / i.scale / max(0.4, g.h * 0.5), max(g.h / 2, g.r * 0.75));
},
jump(i, g, T, s) {
let a = s.air;
if (a == null) {
const u = ((s.beat ?? T * 1.8) / 2) % 1; // one jump every two beats
a = u < 0.62 ? u / 0.62 : -1;
if (a >= 0) s.y += clamp(g.h * 0.8, 0.5, 2.2) * i.scale * 4 * a * (1 - a);
}
if (a < 0) { g.body.scale.y *= 0.9; return; }
const k = sin(a * PI);
if (g.kind === 'quad') {
g.legs[0].rotation.x -= k * 0.9; g.legs[1].rotation.x -= k * 0.9;
g.legs[2].rotation.x += k * 0.9; g.legs[3].rotation.x += k * 0.9;
g.body.rotation.x += (a - 0.5) * 0.5;
} else if (g.kind === 'biped' || g.kind === 'bird') {
g.legs[0].rotation.x -= k * 0.7; g.legs[1].rotation.x += k * 0.5;
if (g.arms) { g.arms[0].rotation.z += k * 2.4; g.arms[1].rotation.z -= k * 2.4; }
flap(g, T, 18, 0.7);
} else {
g.body.scale.y *= 1 + k * 0.08;
}
},
spin(i, g, T, s) { s.yaw += T * 2.6; POSE.idle(i, g, T, s); },
dance(i, g, T, s) {
const b = (s.beat ?? T * 1.5) * 2 * PI; // one bounce per beat
g.body.position.y += abs(sin(b / 2)) * 0.1 * g.h;
g.body.rotation.z += sin(b / 2) * 0.12;
s.yaw += sin(b / 4) * 0.55;
if (g.arms) {
g.arms[0].rotation.z += 2.3 + sin(b) * 0.5;
g.arms[1].rotation.z -= 2.3 + sin(b + PI) * 0.5;
}
if (g.kind === 'quad') {
g.legs[0].rotation.x -= max(0, sin(b / 2)) * 0.9;
g.legs[1].rotation.x -= max(0, -sin(b / 2)) * 0.9;
} else if (g.legs) {
g.legs[0].rotation.x -= max(0, sin(b / 2)) * 0.5;
g.legs[1].rotation.x -= max(0, -sin(b / 2)) * 0.5;
}
if (g.head && g.kind !== 'prop') g.head.rotation.z += sin(b / 2 + 1) * 0.16;
if (g.tail && g.kind !== 'fish') g.tail.rotation.z += sin(b) * 0.4;
flap(g, T, 9, 0.6);
},
fly(i, g, T, s) {
const winged = flap(g, T, g.kind === 'bird' ? 16 : 6, g.kind === 'bird' ? 0.75 : 0.5);
if (g.kind === 'quad' && !winged) {
g.legs[0].rotation.x -= 1.2; g.legs[1].rotation.x -= 1.2; g.legs[2].rotation.x += 1.2; g.legs[3].rotation.x += 1.2;
} else if (g.kind === 'quad') {
for (const l of g.legs) l.rotation.x += 0.5;
} else if (g.kind === 'biped' && g.arms) {
g.body.rotation.x += 1.2;
g.body.position.y += g.leg * 0.2;
g.arms[0].rotation.x += PI - 0.2; g.arms[1].rotation.x += 0.4;
g.legs[0].rotation.x += sin(T * 2) * 0.1;
} else if (g.kind === 'bird') {
for (const l of g.legs) l.rotation.x += 1.2;
}
if (g.flame) { g.flame.visible = true; g.flame.scale.y = 1 + sin(T * 40) * 0.2; }
if (g.tail && g.kind !== 'fish') g.tail.rotation.z += sin(T * 4) * 0.15;
},
float(i, g, T, s) {
s.y += 0.5 + 0.35 * g.h * i.scale + sin(T * 1.3) * 0.22;
s.yaw += T * 0.35;
s.roll += sin(T * 0.8) * 0.12;
s.pitch += sin(T * 0.6 + 1) * 0.1;
if (g.arms) { g.arms[0].rotation.z += 0.6 + sin(T) * 0.2; g.arms[1].rotation.z -= 0.6 + sin(T + 1) * 0.2; }
if (g.legs) g.legs.forEach((l, k) => { l.rotation.x += sin(T * 1.1 + k) * 0.2; });
flap(g, T, 3, 0.2);
},
sit(i, g, T, s) {
if (g.kind === 'quad') {
const phi = clamp(Math.asin(clamp((g.leg - 0.3 * g.bodyH) / (2 * g.hipZ), 0.1, 0.65)), 0.15, 0.7);
g.body.rotation.x -= phi;
g.body.position.y -= g.leg * cos(phi) - g.hipZ * sin(phi) - g.bodyH * 0.3;
g.legs[0].rotation.x += phi; g.legs[1].rotation.x += phi;
g.legs[2].rotation.x += phi - PI / 2; g.legs[3].rotation.x += phi - PI / 2;
if (g.head) g.head.rotation.x += phi * 0.7;
if (g.tail) g.tail.rotation.z += sin(T * 4) * 0.25;
} else if (g.kind === 'biped') {
g.legs[0].rotation.x -= PI / 2; g.legs[1].rotation.x -= PI / 2;
g.body.position.y -= g.leg - 0.12;
if (g.arms) { g.arms[0].rotation.x -= 0.5; g.arms[1].rotation.x -= 0.5; }
if (g.head) g.head.rotation.y += sin(T * 0.7) * 0.3;
} else if (g.kind === 'bird') {
for (const l of g.legs) l.rotation.x -= PI / 2;
g.body.position.y -= g.leg * 0.9;
} else {
g.body.scale.y *= 0.93;
}
},
sleep(i, g, T, s) {
const br = 1 + sin(T * 1.6) * 0.025;
if (g.kind === 'quad') {
g.body.position.y -= g.leg * 0.9;
for (const l of g.legs) l.rotation.x -= PI / 2;
if (g.head) { g.head.rotation.x += 0.6; g.head.rotation.z += 0.3; }
g.body.scale.y *= br;
} else if (g.kind === 'biped') {
g.body.rotation.x -= PI / 2;
g.body.position.y += 0.18;
g.body.position.z += g.h * 0.5;
g.body.scale.z *= br;
} else if (g.kind === 'bird') {
for (const l of g.legs) l.rotation.x -= PI / 2;
g.body.position.y -= g.leg * 0.9;
if (g.head) g.head.rotation.x += 0.5;
} else {
s.roll += g.kind === 'prop' || g.kind === 'vehicle' ? 0.06 : 0.25;
g.body.scale.y *= br;
}
i.zzz = true;
},
wave(i, g, T, s) {
const w = sin(T * 8) * 0.35;
if (g.arms) g.arms[1].rotation.z -= 2.5 + w;
else if (g.kind === 'quad') { g.legs[1].rotation.x -= 1.5 + w; g.body.rotation.x -= 0.06; }
else if (g.wings) g.wings[1].rotation.z -= 1.0 + w * 2;
else g.body.rotation.z += sin(T * 5) * 0.16;
if (g.head && g.kind !== 'prop') g.head.rotation.z += 0.12;
if (g.tail && g.kind !== 'fish') g.tail.rotation.z += sin(T * 6) * 0.3;
},
bounce(i, g, T, s) {
const H = clamp(g.h * 0.7, 0.5, 1.8) * i.scale, k = abs(sin((s.beat ?? T * 1.3) * PI));
s.y += H * k;
const sq = max(0, 1 - k * 6) * 0.22;
g.body.scale.y *= 1 - sq;
g.body.scale.x *= 1 + sq * 0.6;
g.body.scale.z *= 1 + sq * 0.6;
},
shake(i, g, T, s) {
g.body.rotation.z += sin(T * 41) * 0.06;
g.body.position.x += sin(T * 53) * 0.03 * g.h;
if (g.head && g.kind !== 'prop') g.head.rotation.y += sin(T * 37) * 0.1;
if (g.arms) { g.arms[0].rotation.z += 0.3 + sin(T * 45) * 0.1; g.arms[1].rotation.z -= 0.3 + sin(T * 47) * 0.1; }
},
};
POSE.walk = POSE.run = POSE.drive = POSE.circle = POSE.move;
function ridePose(i, g, T) {
if (g.kind === 'biped') {
g.legs[0].rotation.z += 0.5; g.legs[1].rotation.z -= 0.5;
g.legs[0].rotation.x -= 0.5; g.legs[1].rotation.x -= 0.5;
if (g.arms) { g.arms[0].rotation.x -= 0.7; g.arms[1].rotation.x -= 0.7; }
return -g.leg * 0.7;
}
if (g.kind === 'quad') { POSE.sit(i, g, T, {}); return 0; }
if (g.kind === 'bird') { for (const l of g.legs) l.rotation.x -= PI / 2; return -g.leg * 0.9; }
return 0;
}
// ------------------------------------------------------------------ per-frame update
const _s = {};
export function updateInstance(inst, t, ctx = {}) {
const g = inst.rig, o = inst.obj;
for (const p of g.all) {
const r = p.userData.rest;
p.position.copy(r.p); p.rotation.copy(r.r); p.scale.copy(r.s);
}
if (g.flame) g.flame.visible = false;
const T = t * inst.spdMul + inst.phase, act = inst.act;
const s = _s;
s.x = inst.base.x; s.y = inst.base.y; s.z = inst.base.z;
s.yaw = inst.yaw0; s.roll = 0; s.pitch = 0; s.dist = 0; s.air = null; s.water = false;
// musical time: dancing, bouncing and jumping lock to it; 'slow' is half time and 'fast' double time
s.beat = ctx.beat == null ? null : ctx.beat * (inst.spdMul < 1 ? 0.5 : inst.spdMul > 1 ? 2 : 1);
if (s.beat == null) s.beat = undefined;
inst.zzz = false;
if (inst.mount) {
s.x = inst.seat[0]; s.y = inst.seat[1]; s.z = inst.seat[2]; s.yaw = 0;
if (inst.mode === 'ride') {
s.y += ridePose(inst, g, T) * (inst.scale / inst.mount.scale);
if (act === 'wave' || act === 'dance' || act === 'shake') POSE[act](inst, g, T, {});
} else {
const loco = { walk: 1, run: 1, drive: 1, circle: 1, swim: 1, roll: 0 }[act];
if (loco) { s.dist = speedOf(inst, act) * T; POSE.move(inst, g, T, s, act); }
else if (act === 'fly') { POSE.fly(inst, g, T, s); s.y += (0.4 + sin(T * 1.5) * 0.1) / inst.mount.scale; }
else if (act !== 'roll') {
const y0 = s.y;
(POSE[act] ?? POSE.idle)(inst, g, T, s, act);
s.y = y0 + (s.y - y0) / inst.mount.scale;
}
}
} else {
path(inst, T, s);
if (ctx.waterY != null && (g.swims || act === 'swim')) {
const sink = g.floats ? 0.1 : g.kind === 'fish' ? (g.hover + 0.05) * inst.scale
: g.kind === 'bird' ? (g.leg + 0.04) * inst.scale : g.h * inst.scale * 0.55;
if (act !== 'fly' && act !== 'jump' && act !== 'bounce' && act !== 'float') {
s.y = ctx.waterY - sink + sin(T * 1.5) * 0.05;
s.water = true;
s.roll += g.floats ? sin(T * 1.1) * 0.05 : 0;
}
}
(POSE[act] ?? POSE.idle)(inst, g, T, s, act);
if (inst.motion.kind !== 'none' && act !== 'idle' && !POSE[act]) POSE.move(inst, g, T, s, 'walk');
}
if (g.spinners) {
const on = act === 'fly' || act === 'drive' || LOCO.has(act) || inst.motion.kind !== 'none';
for (const sp of g.spinners) if (on || sp.userData.always) sp.rotation[sp.userData.axis] += T * (sp.userData.always && !on ? 2 : 24);
}
if (inst.shift && !inst.mount) { s.x += inst.shift.x; s.z += inst.shift.z; } // live drag preview
o.position.set(s.x, s.y, s.z);
o.rotation.set(s.pitch, s.yaw, s.roll);
if (inst.sprites) updateZzz(inst, T);
}
// ------------------------------------------------------------------ sleeping "z z z"
let zTex = null;
function zTexture() {
if (zTex || typeof document === 'undefined') return zTex;
const c = document.createElement('canvas');
c.width = c.height = 64;
const x = c.getContext('2d');
x.font = 'bold 54px sans-serif';
x.textAlign = 'center';
x.textBaseline = 'middle';
x.lineWidth = 6;
x.strokeStyle = '#1c2540';
x.strokeText('z', 32, 30);
x.fillStyle = '#ffffff';
x.fillText('z', 32, 30);
zTex = new THREE.CanvasTexture(c);
return zTex;
}
export function addZzz(inst) {
const tex = zTexture();
if (!tex) return;
inst.sprites = [0, 1, 2].map(() => {
const sp = new THREE.Sprite(new THREE.SpriteMaterial({ map: tex, transparent: true, depthWrite: false }));
inst.obj.add(sp);
return sp;
});
}
function updateZzz(inst, T) {
const g = inst.rig;
inst.sprites.forEach((sp, k) => {
const u = (T * 0.35 + k / 3) % 1;
sp.visible = inst.zzz;
const m = clamp(g.h, 0.6, 2), y0 = g.kind === 'biped' ? 0.45 : g.h * 0.45;
sp.position.set(0.15 + u * 0.3 * m + sin(u * 6) * 0.06, y0 + u * (0.5 + 0.35 * m), g.kind === 'biped' ? -g.h * 0.2 : 0.25);
sp.scale.setScalar((0.1 + u * 0.16) * m);
sp.material.opacity = sin(u * PI);
});
}
|