// Claudia herself: a slim young woman on her scooter. Smooth, rounded body parts; a face; long // dark hair made of strands that stream back in the wind of her speed; a sky-blue tank top, a // pleated navy skirt that flutters, and white sneakers on lime soles. // // Her arms and legs are posed by two-bone IK every frame, so her hands stay on the bar and her // feet on the deck however she leans and crouches. Everything that moves (hair, skirt, pose) // is a function of the game state, so a screenshot at t seconds always shows the same frame. // She faces -z; her left is -x. import * as THREE from "three"; import { LIME } from "../palette.js"; import { rng } from "../rng.js"; import { textTexture } from "./kit.js"; const SKIN = 0xf0c4a6; const SHORTS = 0x1e2438; const TANK = 0xf6f4f0; const SKIRT = 0x27304d; const SHOE = 0xf7f6f2; const ACCENT = 0x6fb0de; const HAIR_ROOT = new THREE.Color(0x060507); const HAIR_TIP = new THREE.Color(0x141218); const STREAK_ROOT = new THREE.Color(0x7a1d10); const STREAK_TIP = new THREE.Color(0xc2452a); const LIPS = 0xd97a72; const BROW = 0x3a2519; const IRIS = 0x5d7a62; const UP = new THREE.Vector3(0, 1, 0); const v3 = (x, y, z) => new THREE.Vector3(x, y, z); function material(color, o = {}) { return new THREE.MeshStandardMaterial({ color, roughness: o.roughness ?? 0.6, metalness: 0, side: o.side ?? THREE.FrontSide, vertexColors: !!o.vertexColors }); } function mesh(geometry, mat, x = 0, y = 0, z = 0) { const m = new THREE.Mesh(geometry, mat); m.position.set(x, y, z); m.castShadow = true; m.receiveShadow = true; return m; } /** A rounded limb along +y from 0 to len, with a radius profile [[t, r], ...]. */ function limbGeometry(len, profile, seg = 18) { const pts = []; const r0 = profile[0][1]; const r1 = profile[profile.length - 1][1]; for (let k = 0; k < 4; k++) { const a = -Math.PI / 2 + (k / 4) * (Math.PI / 2); pts.push(new THREE.Vector2(Math.max(1e-4, Math.cos(a) * r0), Math.sin(a) * r0 * 0.7)); } for (const [t, r] of profile) pts.push(new THREE.Vector2(r, t * len)); for (let k = 1; k <= 4; k++) { const a = (k / 4) * (Math.PI / 2); pts.push(new THREE.Vector2(Math.max(1e-4, Math.cos(a) * r1), len + Math.sin(a) * r1 * 0.7)); } return new THREE.LatheGeometry(pts, seg); } /** Rings of ellipses stacked bottom to top, closed at both ends, smooth. `front` deepens the * front half of a ring. Each ring may carry a color. */ function ringBody(rings, seg = 30) { const pos = []; const col = []; const index = []; const c = new THREE.Color(); for (const r of rings) { c.set(r.color ?? 0xffffff); for (let k = 0; k < seg; k++) { const a = (k / seg) * Math.PI * 2; const cz = Math.cos(a); pos.push(Math.sin(a) * r.rx, r.y, cz * (cz < 0 ? r.rz + (r.front ?? 0) : r.rz) + (r.z ?? 0)); col.push(c.r, c.g, c.b); } } for (let i = 0; i + 1 < rings.length; i++) for (let k = 0; k < seg; k++) { const a = i * seg + k; const b = i * seg + ((k + 1) % seg); index.push(a, b, b + seg, a, b + seg, a + seg); } const bottom = pos.length / 3; const first = rings[0]; const last = rings[rings.length - 1]; c.set(first.color ?? 0xffffff); pos.push(0, first.y - 0.01, first.z ?? 0); col.push(c.r, c.g, c.b); const top = pos.length / 3; c.set(last.color ?? 0xffffff); pos.push(0, last.y + 0.005, last.z ?? 0); col.push(c.r, c.g, c.b); const lastRow = (rings.length - 1) * seg; for (let k = 0; k < seg; k++) { index.push(bottom, (k + 1) % seg, k); index.push(top, lastRow + k, lastRow + ((k + 1) % seg)); } const g = new THREE.BufferGeometry(); g.setAttribute("position", new THREE.Float32BufferAttribute(pos, 3)); g.setAttribute("color", new THREE.Float32BufferAttribute(col, 3)); g.setIndex(index); g.computeVertexNormals(); return g; } // The torso, from below the hips to the base of the neck (y relative to the hip joint). const TORSO = [ { y: -0.11, rx: 0.122, rz: 0.084, color: SHORTS }, { y: -0.06, rx: 0.166, rz: 0.103, color: SHORTS }, { y: 0.0, rx: 0.176, rz: 0.105, color: SHORTS }, { y: 0.04, rx: 0.166, rz: 0.098, color: SKIN }, { y: 0.1, rx: 0.126, rz: 0.08, color: SKIN }, { y: 0.16, rx: 0.094, rz: 0.068, color: SKIN }, { y: 0.23, rx: 0.104, rz: 0.071, color: SKIN }, { y: 0.3, rx: 0.124, rz: 0.08, front: 0.016, color: SKIN }, { y: 0.355, rx: 0.132, rz: 0.082, front: 0.027, color: SKIN }, { y: 0.405, rx: 0.138, rz: 0.081, front: 0.012, color: SKIN }, { y: 0.445, rx: 0.144, rz: 0.075, color: SKIN }, { y: 0.478, rx: 0.124, rz: 0.062, color: SKIN }, { y: 0.505, rx: 0.07, rz: 0.046, color: SKIN }, { y: 0.53, rx: 0.04, rz: 0.037, color: SKIN }, ]; function torsoAt(y) { for (let i = 0; i + 1 < TORSO.length; i++) { const a = TORSO[i]; const b = TORSO[i + 1]; if (y >= a.y && y <= b.y) { const t = (y - a.y) / (b.y - a.y); return { rx: a.rx + (b.rx - a.rx) * t, rz: a.rz + (b.rz - a.rz) * t, front: (a.front ?? 0) + ((b.front ?? 0) - (a.front ?? 0)) * t }; } } return TORSO[TORSO.length - 1]; } /** The tank top: a shell over the torso from the waist up to a scooped neckline and low * armholes, with thin straps over the shoulders. */ function tankTop() { const seg = 36; const rows = 10; const pos = []; const index = []; const topAt = (a) => { const sx = Math.abs(Math.sin(a)); const front = Math.max(0, -Math.cos(a)); return 0.445 - 0.085 * sx ** 3 - 0.05 * front ** 2; }; for (let r = 0; r <= rows; r++) for (let k = 0; k < seg; k++) { const a = (k / seg) * Math.PI * 2; const y = 0.015 + (topAt(a) - 0.015) * (r / rows); const t = torsoAt(y); const cz = Math.cos(a); pos.push(Math.sin(a) * (t.rx * 1.03 + 0.003), y, cz * ((cz < 0 ? t.rz + t.front : t.rz) * 1.04 + 0.003)); } for (let r = 0; r < rows; r++) for (let k = 0; k < seg; k++) { const a = r * seg + k; const b = r * seg + ((k + 1) % seg); index.push(a, b, b + seg, a, b + seg, a + seg); } const g = new THREE.BufferGeometry(); g.setAttribute("position", new THREE.Float32BufferAttribute(pos, 3)); g.setIndex(index); g.computeVertexNormals(); const straps = []; for (const s of [-1, 1]) { const x = 0.088 * s; const yf = 0.445 - 0.05 * 0.83; const curve = new THREE.CatmullRomCurve3([v3(x, yf - 0.005, -0.095), v3(x * 1.08, 0.485, -0.062), v3(x * 1.12, 0.512, -0.005), v3(x * 1.08, 0.488, 0.05), v3(x, 0.44, 0.083)]); straps.push(new THREE.TubeGeometry(curve, 20, 0.0085, 6, false)); } return { shell: g, straps }; } /** The skull with a softer jaw and chin, face toward -z. */ function headGeometry() { const g = new THREE.SphereGeometry(1, 48, 36); const p = g.attributes.position; const colors = new Float32Array(p.count * 3); const skinC = new THREE.Color(SKIN); const blushC = new THREE.Color(0xf09a90); const c = new THREE.Color(); for (let i = 0; i < p.count; i++) { let x = p.getX(i); const y = p.getY(i); let z = p.getZ(i); // a soft, round face: gentle jaw, small round chin if (y < 0) { x *= 1 - 0.2 * (-y) ** 1.8; if (z < 0) z *= 1 + 0.04 * -y; } if (z > 0 && y > -0.3) z *= 1.04; const px = x * 0.077; const py = y * 0.1; const pz = z * 0.09; p.setXYZ(i, px, py, pz); // rosy cheeks let w = 0; if (pz < -0.04) for (const s of [-1, 1]) w = Math.max(w, Math.exp(-((px - 0.043 * s) ** 2 + (py + 0.02) ** 2) / 0.00035)); c.copy(skinC).lerp(blushC, 0.5 * w); colors.set([c.r, c.g, c.b], i * 3); } g.setAttribute("color", new THREE.BufferAttribute(colors, 3)); g.computeVertexNormals(); return g; } /** A seven-pointed star, for her hair clip. */ function starGeometry(r) { const s = new THREE.Shape(); for (let k = 0; k <= 14; k++) { const a = (k / 14) * Math.PI * 2 + Math.PI / 2; const rr = k % 2 === 0 ? r : r * 0.52; const x = Math.cos(a) * rr; const y = Math.sin(a) * rr; if (k === 0) s.moveTo(x, y); else s.lineTo(x, y); } const g = new THREE.ExtrudeGeometry(s, { depth: 0.003, bevelEnabled: true, bevelThickness: 0.0015, bevelSize: 0.0012, bevelSegments: 1 }); g.translate(0, 0, -0.0015); return g; } function ellipsoid(rx, ry, rz, segs = 14) { const g = new THREE.SphereGeometry(1, segs, Math.max(8, segs - 4)); g.scale(rx, ry, rz); return g; } /** A chunky designer sneaker: black upper on a thick sculpted white sole, a lime heel tab, * white laces. Toe toward -z, sole at y = 0. */ function sneaker() { const H = 0.034; // the sole's height: the upper sits on top of it const outline = new THREE.Shape(); outline.moveTo(-0.105, H); outline.lineTo(0.13, H); outline.quadraticCurveTo(0.168, H + 0.002, 0.162, H + 0.03); outline.quadraticCurveTo(0.152, H + 0.054, 0.1, H + 0.062); outline.lineTo(0.035, H + 0.075); outline.quadraticCurveTo(-0.01, H + 0.104, -0.05, H + 0.102); outline.lineTo(-0.098, H + 0.1); outline.quadraticCurveTo(-0.124, H + 0.05, -0.105, H); const upper = new THREE.ExtrudeGeometry(outline, { depth: 0.07, bevelEnabled: true, bevelThickness: 0.013, bevelSize: 0.011, bevelSegments: 3, curveSegments: 12 }); const soleShape = new THREE.Shape(); soleShape.moveTo(-0.118, 0.006); soleShape.quadraticCurveTo(-0.118, 0, -0.1, 0); soleShape.lineTo(0.15, 0); soleShape.quadraticCurveTo(0.186, 0.006, 0.178, H + 0.006); soleShape.quadraticCurveTo(0.12, H + 0.004, 0.06, H); soleShape.quadraticCurveTo(-0.02, H - 0.004, -0.08, H + 0.006); soleShape.quadraticCurveTo(-0.122, H + 0.012, -0.126, H * 0.6); soleShape.quadraticCurveTo(-0.128, 0.012, -0.118, 0.006); const sole = new THREE.ExtrudeGeometry(soleShape, { depth: 0.08, bevelEnabled: true, bevelThickness: 0.01, bevelSize: 0.008, bevelSegments: 3, curveSegments: 10 }); const tread = new THREE.Shape(); tread.moveTo(-0.118, 0); tread.lineTo(0.152, 0); tread.quadraticCurveTo(0.17, 0.002, 0.168, 0.007); tread.lineTo(-0.122, 0.007); tread.lineTo(-0.118, 0); const outsole = new THREE.ExtrudeGeometry(tread, { depth: 0.096, bevelEnabled: false, curveSegments: 6 }); const panel = new THREE.Shape(); panel.moveTo(-0.085, H + 0.012); panel.quadraticCurveTo(0.0, H + 0.006, 0.09, H + 0.02); panel.lineTo(0.06, H + 0.045); panel.quadraticCurveTo(-0.01, H + 0.03, -0.08, H + 0.05); panel.lineTo(-0.085, H + 0.012); const side = new THREE.ExtrudeGeometry(panel, { depth: 0.096, bevelEnabled: false, curveSegments: 8 }); const tab = new THREE.Shape(); tab.moveTo(-0.112, H + 0.05); tab.lineTo(-0.1, H + 0.05); tab.lineTo(-0.098, H + 0.118); tab.lineTo(-0.112, H + 0.116); tab.lineTo(-0.112, H + 0.05); const heel = new THREE.ExtrudeGeometry(tab, { depth: 0.03, bevelEnabled: false }); const group = new THREE.Group(); const fit = (geo, depth) => { geo.translate(0, 0, -depth / 2); geo.rotateY(Math.PI / 2); return geo; }; group.add(mesh(fit(upper, 0.07), material(0x17171a, { roughness: 0.42 }))); group.add(mesh(fit(sole, 0.08), material(0xf5f3ee, { roughness: 0.55 }))); group.add(mesh(fit(outsole, 0.096), material(0x2a2a2e, { roughness: 0.8 }))); group.add(mesh(fit(side, 0.096), material(0xd9dadc, { roughness: 0.3 }))); group.add(mesh(fit(heel, 0.03), material(LIME, { roughness: 0.4 }))); // laces and eyelets across the top of the foot const lace = material(0xffffff, { roughness: 0.7 }); for (let k = 0; k < 5; k++) { const l = mesh(new THREE.BoxGeometry(0.054, 0.006, 0.008), lace, 0, H + 0.08 + k * 0.006, -0.012 - k * 0.016); l.rotation.x = -0.4; group.add(l); } return group; } /** Two-bone IK: the middle joint for a chain from a toward target t, bending toward pole. */ function solve(a, t, l1, l2, pole, outMid, outEnd) { const d = new THREE.Vector3().subVectors(t, a); let len = d.length(); len = Math.min(l1 + l2 - 1e-4, Math.max(Math.abs(l1 - l2) + 1e-4, len)); d.normalize(); const along = (l1 * l1 - l2 * l2 + len * len) / (2 * len); const h = Math.sqrt(Math.max(0, l1 * l1 - along * along)); const pv = new THREE.Vector3().subVectors(pole, a); pv.addScaledVector(d, -pv.dot(d)).normalize(); outMid.copy(a).addScaledVector(d, along).addScaledVector(pv, h); outEnd.copy(a).addScaledVector(d, len); } const tmp = new THREE.Vector3(); function place(m, a, b) { m.position.copy(a); m.quaternion.setFromUnitVectors(UP, tmp.subVectors(b, a).normalize()); } export function buildRider() { const skin = material(SKIN, { roughness: 0.55 }); const skinVC = material(0xffffff, { roughness: 0.55, vertexColors: true }); const tankMat = material(TANK, { roughness: 0.75, side: THREE.DoubleSide }); const skirtMat = material(SKIRT, { roughness: 0.75, side: THREE.DoubleSide }); // black hair takes the sun's warm sheen but only a whisper of the blue sky const hairMat = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.34, side: THREE.DoubleSide, envMapIntensity: 0.22 }); const capMat = material(HAIR_ROOT.getHex(), { roughness: 0.5 }); const group = new THREE.Group(); const chest = new THREE.Group(); group.add(chest); // torso, tank top and straps, shoulders, neck chest.add(mesh(ringBody(TORSO), skinVC)); const tank = tankTop(); chest.add(mesh(tank.shell, tankMat)); for (const s of tank.straps) chest.add(mesh(s, tankMat)); for (const s of [-1, 1]) chest.add(mesh(ellipsoid(0.044, 0.04, 0.044, 16), skin, 0.141 * s, 0.448, 0.004)); const neck = mesh(limbGeometry(0.13, [[0, 0.038], [0.5, 0.033], [1, 0.035]], 16), skin, 0, 0.49, -0.004); neck.rotation.x = -0.12; chest.add(neck); // a black choker with a little silver ring const choker = mesh(new THREE.CylinderGeometry(0.0405, 0.041, 0.013, 24, 1, true), material(0x111114, { roughness: 0.35, side: THREE.DoubleSide }), 0, 0.055, 0); neck.add(choker); neck.add(mesh(new THREE.TorusGeometry(0.006, 0.0016, 6, 14), material(0xd8d8dc, { roughness: 0.2 }), 0, 0.048, -0.042)); // the tank's graphic: a lightning bolt and OFFLINE, printed across the chest const print = textTexture(512, 512, (ctx, w) => { ctx.clearRect(0, 0, w, w); ctx.lineJoin = "miter"; const bolt = [[300, 40], [170, 270], [262, 270], [200, 470], [352, 210], [258, 210], [318, 40]]; ctx.beginPath(); bolt.forEach(([x, y], i) => (i ? ctx.lineTo(x, y) : ctx.moveTo(x, y))); ctx.closePath(); ctx.fillStyle = "#141416"; ctx.fill(); ctx.lineWidth = 14; ctx.strokeStyle = "#a3e635"; ctx.stroke(); ctx.font = '900 104px "Helvetica Neue", Helvetica, Arial, sans-serif'; ctx.textAlign = "center"; ctx.textBaseline = "middle"; ctx.lineWidth = 10; ctx.strokeStyle = "#f6f4f0"; ctx.strokeText("OFFLINE", w / 2, 318); ctx.fillStyle = "#141416"; ctx.fillText("OFFLINE", w / 2, 318); }); const decal = new THREE.PlaneGeometry(0.15, 0.15, 10, 8); decal.rotateY(Math.PI); // face forward (-z), reading the right way round const dp = decal.attributes.position; for (let i = 0; i < dp.count; i++) { const x = dp.getX(i); const y = dp.getY(i) + 0.285; const tr = torsoAt(y); const rx = tr.rx * 1.03 + 0.003; const rz = (tr.rz + tr.front) * 1.04 + 0.003; dp.setXYZ(i, x, y, -rz * Math.sqrt(Math.max(0, 1 - (x / rx) ** 2)) - 0.0015); } decal.computeVertexNormals(); const printMat = new THREE.MeshStandardMaterial({ map: print, transparent: true, roughness: 0.7, polygonOffset: true, polygonOffsetFactor: -2, polygonOffsetUnits: -2 }); const graphic = new THREE.Mesh(decal, printMat); chest.add(graphic); // the skirt: pleated, flared, rebuilt every frame so it can flutter const SK = { seg: 40, rows: 7 }; const skirtGeo = new THREE.BufferGeometry(); const skirtPos = new Float32Array((SK.rows + 1) * SK.seg * 3); const skirtIdx = []; for (let r = 0; r < SK.rows; r++) for (let k = 0; k < SK.seg; k++) { const a = r * SK.seg + k; const b = r * SK.seg + ((k + 1) % SK.seg); skirtIdx.push(a, b, b + SK.seg, a, b + SK.seg, a + SK.seg); } skirtGeo.setIndex(skirtIdx); skirtGeo.setAttribute("position", new THREE.BufferAttribute(skirtPos, 3)); const skirt = mesh(skirtGeo, skirtMat); skirt.frustumCulled = false; chest.add(skirt); const band = mesh(new THREE.CylinderGeometry(0.162, 0.17, 0.03, 36, 1, true), skirtMat, 0, 0.055, 0); band.scale.z = 0.68; chest.add(band); // the head, the face, the hair const head = new THREE.Group(); head.position.set(0, 0.705, -0.024); const HEAD_SCALE = 1.34; // a cute, stylized proportion: a bigger head on a slim figure head.scale.setScalar(HEAD_SCALE); chest.add(head); head.add(mesh(headGeometry(), material(0xffffff, { roughness: 0.5, vertexColors: true }))); const white = material(0xf6f1ea, { roughness: 0.35 }); const iris = material(IRIS, { roughness: 0.3 }); const pupil = material(0x111111, { roughness: 0.3 }); const brow = material(BROW, { roughness: 0.8 }); const lips = material(LIPS, { roughness: 0.45 }); const spark = new THREE.MeshBasicMaterial({ color: 0xffffff }); for (const s of [-1, 1]) { const ex = 0.03 * s; head.add(mesh(ellipsoid(0.0158, 0.0106, 0.009, 16), white, ex, 0.006, -0.0772)); head.add(mesh(ellipsoid(0.0086, 0.0089, 0.004, 16), iris, ex, 0.0055, -0.0838)); head.add(mesh(ellipsoid(0.0043, 0.0043, 0.002), pupil, ex, 0.0055, -0.0868)); head.add(mesh(ellipsoid(0.0019, 0.0019, 0.001), spark, ex + 0.0032 * s * -1 + 0.0035, 0.0095, -0.0888)); const lash = mesh(ellipsoid(0.0175, 0.0026, 0.0045), pupil, ex + 0.0012 * s, 0.0158, -0.0832); lash.rotation.z = -0.16 * s; head.add(lash); const b = mesh(ellipsoid(0.0155, 0.0024, 0.004), brow, 0.031 * s, 0.031, -0.0858); b.rotation.z = 0.16 * s; head.add(b); head.add(mesh(ellipsoid(0.008, 0.022, 0.015), skin, 0.076 * s, -0.006, 0.006)); } head.add(mesh(ellipsoid(0.0042, 0.013, 0.0048), skin, 0, -0.009, -0.0878)); head.add(mesh(ellipsoid(0.0058, 0.0046, 0.0046), skin, 0, -0.0212, -0.0902)); head.add(mesh(ellipsoid(0.0112, 0.0032, 0.0048), lips, 0, -0.0442, -0.0826)); head.add(mesh(ellipsoid(0.0105, 0.0041, 0.0052), lips, 0, -0.0494, -0.0818)); // the scalp: a shell over the skull where hair grows const growsAt = (d) => { const front = -d.z; const t = Math.min(1, Math.max(0, (front + 0.3) / 0.9)); return d.y > -0.5 + 1.08 * t * t * (3 - 2 * t); }; const capSrc = new THREE.SphereGeometry(1, 40, 30).toNonIndexed(); const cp = capSrc.attributes.position; const capPos = []; const capNrm = []; for (let i = 0; i < cp.count; i += 3) { const tri = [0, 1, 2].map((k) => v3(cp.getX(i + k), cp.getY(i + k), cp.getZ(i + k))); if (!tri.every(growsAt)) continue; for (const d of tri) { capPos.push(d.x * 0.0785, d.y * 0.108 + 0.002, d.z * 0.095); capNrm.push(d.x, d.y, d.z); } } const capGeo = new THREE.BufferGeometry(); capGeo.setAttribute("position", new THREE.Float32BufferAttribute(capPos, 3)); capGeo.setAttribute("normal", new THREE.Float32BufferAttribute(capNrm, 3)); head.add(mesh(capGeo, capMat)); // a little lime star clipped above her left temple const star = mesh(starGeometry(0.017), material(LIME, { roughness: 0.35 }), -0.07, 0.062, -0.05); star.rotation.set(0.15, -2.2, 0.3); head.add(star); // strands const random = rng(1989); const roots = []; const total = 900; for (let i = 0; i < total && roots.length < 420; i++) { const k = i + 0.5; const y = 1 - (2 * k) / total; const r = Math.sqrt(1 - y * y); const phi = k * 2.399963; const d = v3(Math.cos(phi) * r, y, Math.sin(phi) * r); // a thick, rounded bob that ends at the chin; across the front, a heavy blunt fringe // that stops at the brows const fringe = -d.z > 0.12 && d.y > 0.26; if (!growsAt(d) || random() < (fringe ? 0.25 : 0.42)) continue; const len = fringe ? Math.max(0.02, (d.y * 0.111 - 0.038) * 1.25 + 0.01) + random() * 0.002 : Math.max(0.05, (d.y * 0.111 + 0.075) * 1.12 + 0.012 + random() * 0.004); // a copper-red streak through the fringe, on her right const streak = fringe && d.x > 0.08 && d.x < 0.36; roots.push({ p: v3(d.x * 0.081, d.y * 0.111 + 0.002, d.z * 0.098), n: d.clone(), len, w: 0.03 + random() * 0.012, phase: random() * Math.PI * 2, shade: 0.8 + random() * 0.45, fringe, streak, }); } const S = 14; const hairGeo = new THREE.BufferGeometry(); const hairPos = new Float32Array(roots.length * (S + 1) * 2 * 3); const hairCol = new Float32Array(roots.length * (S + 1) * 2 * 3); const hairNrm = new Float32Array(roots.length * (S + 1) * 2 * 3); const hairIdx = []; roots.forEach((root, j) => { const base = j * (S + 1) * 2; for (let i = 0; i < S; i++) { const a = base + i * 2; hairIdx.push(a, a + 1, a + 3, a, a + 3, a + 2); } for (let i = 0; i <= S; i++) { const c = root.streak ? STREAK_ROOT.clone().lerp(STREAK_TIP, (i / S) ** 1.1) : HAIR_ROOT.clone().lerp(HAIR_TIP, (i / S) ** 1.3).multiplyScalar(root.shade); hairCol.set([c.r, c.g, c.b, c.r, c.g, c.b], (base + i * 2) * 3); } }); hairGeo.setIndex(hairIdx); hairGeo.setAttribute("position", new THREE.BufferAttribute(hairPos, 3)); hairGeo.setAttribute("color", new THREE.BufferAttribute(hairCol, 3)); hairGeo.setAttribute("normal", new THREE.BufferAttribute(hairNrm, 3)); const hair = mesh(hairGeo, hairMat); hair.frustumCulled = false; head.add(hair); // limbs, placed by IK each frame const thigh = (s) => mesh(limbGeometry(0.41, [[0, 0.066], [0.15, 0.07], [0.5, 0.061], [0.85, 0.047], [1, 0.043]]), skin); const calf = () => mesh(limbGeometry(0.385, [[0, 0.042], [0.18, 0.049], [0.38, 0.046], [0.72, 0.031], [0.95, 0.025], [1, 0.026]]), skin); const upperArm = () => mesh(limbGeometry(0.29, [[0, 0.035], [0.2, 0.033], [0.6, 0.028], [1, 0.024]]), skin); const foreArm = () => mesh(limbGeometry(0.255, [[0, 0.024], [0.3, 0.026], [0.85, 0.018], [1, 0.017]]), skin); const fist = () => { const g = new THREE.Group(); g.add(mesh(ellipsoid(0.03, 0.045, 0.04, 14), skin, 0, 0.04, 0)); const thumb = mesh(limbGeometry(0.045, [[0, 0.011], [1, 0.009]], 10), skin, 0.022, 0.03, -0.022); thumb.rotation.set(-0.6, 0, -0.5); g.add(thumb); return g; }; const L = { thigh: thigh(-1), calf: calf(), upper: upperArm(), fore: foreArm(), fist: fist() }; const R = { thigh: thigh(1), calf: calf(), upper: upperArm(), fore: foreArm(), fist: fist() }; for (const side of [L, R]) for (const m of Object.values(side)) group.add(m); // feet: ankle socks and sneakers, planted on the deck // feet side by side, toes turned a touch out: a relaxed stance const FEET = { L: v3(-0.053, 0.165, 0.0), R: v3(0.053, 0.165, 0.0) }; const sock = material(0xffffff, { roughness: 0.85 }); for (const [k, f] of Object.entries(FEET)) { const shoe = sneaker(); shoe.position.copy(f); shoe.rotation.y = k === "L" ? 0.07 : -0.07; group.add(shoe); group.add(mesh(new THREE.CylinderGeometry(0.03, 0.034, 0.075, 16), sock, f.x, f.y + 0.152, f.z + 0.035)); } const ANKLE = { L: v3(-0.053, 0.3, 0.03), R: v3(0.053, 0.3, 0.03) }; const GRIP = { L: v3(-0.245, 1.17, -0.33), R: v3(0.245, 1.17, -0.33) }; // scratch vectors const hip = new THREE.Vector3(); const mid = new THREE.Vector3(); const end = new THREE.Vector3(); const shoulder = new THREE.Vector3(); const pole = new THREE.Vector3(); const target = new THREE.Vector3(); const q = new THREE.Quaternion(); const gH = new THREE.Vector3(); const wH = new THREE.Vector3(); const torsoC = new THREE.Vector3(); const shoulderC = [new THREE.Vector3(), new THREE.Vector3()]; const headInv = new THREE.Quaternion(); function pose(s) { const c = s.crouch; chest.position.set(0, 1.135 - c * 0.03 - (s.air ? 0.05 : 0), 0.035); const leanFwd = s.tumble ? 0.5 : 0.05 + c * 0.07; chest.rotation.set(-leanFwd, s.steer * 0.08, s.steer * 0.04); head.rotation.set(leanFwd * 0.75 - 0.05, s.steer * 0.3, 0); for (const [key, side, sx] of [["L", L, -1], ["R", R, 1]]) { // legs: hip to ankle, knee forward hip.set(0.092 * sx, -0.06, 0).add(chest.position); pole.copy(hip).add(v3(0.06 * sx, -0.2, -0.6)); solve(hip, ANKLE[key], 0.41, 0.385, pole, mid, end); place(side.thigh, hip, mid); place(side.calf, mid, end); // arms: shoulder to the grip, elbows out and back shoulder.set(0.151 * sx, 0.448, 0.005).applyEuler(chest.rotation).add(chest.position); if (s.tumble) target.copy(shoulder).add(v3(0.32 * sx, 0.28, -0.05)); else target.copy(GRIP[key]).sub(group.position).add(v3(0, 0.01, 0.042)); pole.copy(shoulder).add(v3(0.4 * sx, -0.25, 0.35)); solve(shoulder, target, 0.29, 0.255, pole, mid, end); place(side.upper, shoulder, mid); place(side.fore, mid, end); side.fist.position.copy(end); side.fist.quaternion.copy(side.fore.quaternion); } } function flutter(s) { // the skirt, in the chest's frame: wind blows it back (+z) and lifts the hem const ws = s.wind; let n = 0; for (let r = 0; r <= SK.rows; r++) { const u = r / SK.rows; const y = 0.06 - 0.31 * u; const rx = 0.17 + 0.125 * u ** 0.75; const rz = 0.108 + 0.11 * u ** 0.75; for (let k = 0; k < SK.seg; k++) { const a = (k / SK.seg) * Math.PI * 2; const pleat = 1 + 0.07 * u * Math.sin(a * 14); const ripple = 1 + ws * 0.035 * u * Math.sin(s.time * 13 + a * 5); const cz = Math.cos(a); const x = Math.sin(a) * rx * pleat * ripple; let z = cz * rz * pleat * ripple * (cz < 0 ? 1.22 + 0.25 * u : 1); let yy = y; z += ws * 0.07 * u * u; yy += ws * u * u * (0.03 + 0.015 * Math.sin(s.time * 9 + a * 3)); skirtPos[n++] = x; skirtPos[n++] = yy; skirtPos[n++] = z; } } skirtGeo.attributes.position.needsUpdate = true; skirtGeo.computeVertexNormals(); } function strands(s) { // gravity and wind, in the head's frame head.getWorldQuaternion(q); q.invert(); gH.set(0, -1, 0).applyQuaternion(q); wH.copy(s.windDir).applyQuaternion(q); // the body to keep hair out of, in the head's frame headInv.copy(head.quaternion).invert(); const hs = HEAD_SCALE; torsoC.set(0, 0.24, 0.01).sub(head.position).applyQuaternion(headInv).divideScalar(hs); shoulderC[0].set(-0.141, 0.448, 0.004).sub(head.position).applyQuaternion(headInv).divideScalar(hs); shoulderC[1].set(0.141, 0.448, 0.004).sub(head.position).applyQuaternion(headInv).divideScalar(hs); const ws = s.wind; const gust = s.gust ?? 0; // 0…1: how hard she is speeding up const dir = new THREE.Vector3(); const off = new THREE.Vector3(); const want = new THREE.Vector3(); const side = new THREE.Vector3(); const wv = new THREE.Vector3(); const nrm = new THREE.Vector3(); const rad = new THREE.Vector3(); const p = new THREE.Vector3(); const A = 0.083, B = 0.113, C = 0.1; roots.forEach((root, j) => { const base = j * (S + 1) * 2 * 3; const seg = root.len / S; p.copy(root.p); side.set(1, 0, 0); let free = false; for (let i = 0; i <= S; i++) { if (i > 0) { const u = i / S; nrm.set(p.x / (A * A), (p.y - 0.002) / (B * B), p.z / (C * C)).normalize(); if (!free && p.y > -0.03) { // on the scalp: slide down and back, away from a parting down the middle, swept // back from the forehead if (root.fringe) { // the fringe comes forward over the forehead want.copy(gH).multiplyScalar(0.7); want.z -= 1.0; want.addScaledVector(wH, ws * 0.18 + gust * 0.2); } else { // the rest falls down the back and sides from a parting down the middle want.copy(gH).multiplyScalar(0.9); want.z += 0.15; want.x += Math.sign(root.n.x || 0.01) * 0.3 * Math.min(1, Math.abs(root.n.x) * 3 + 0.2); want.addScaledVector(wH, ws * 0.35 + gust * 0.3); } want.addScaledVector(nrm, -want.dot(nrm)).normalize(); dir.copy(want); } else { // free: gravity, the wind of her speed, and a ripple that runs down the strand free = true; const wave = Math.sin(s.time * (8 + root.shade * 2) + root.phase + u * 5) * 0.18 * ws * u; want.copy(gH).multiplyScalar(Math.max(0.3, 1 - 0.55 * Math.min(1, ws) - 0.2 * gust)); want.addScaledVector(wH, ws * 0.75 + gust * 0.6).addScaledVector(side, wave); // the bob's shape: out from the head, then curling under at the ends rad.set(p.x, 0, p.z - 0.008); if (rad.lengthSq() > 1e-8) rad.normalize(); // (the wind straightens the curl, so at speed the ends stream back instead of folding) const calm = 1 - 0.7 * Math.min(1, ws * 0.8 + gust * 0.5); want.addScaledVector(rad, 0.5 * (1 - u) * (0.5 + 0.5 * calm) - 1.1 * calm * Math.max(0, (u - 0.62) / 0.38) ** 1.5); want.normalize(); dir.lerp(want, 0.38).normalize(); } p.addScaledVector(dir, seg); // volume: below the crown the bob stands off the head in a bell, not flat to it if (!root.fringe && p.y < 0.03 && p.y > -0.16) { const swell = 0.25 * Math.min(1, Math.max(0, (0.03 - p.y) / 0.09)); const rx = 0.088 * (1 + swell); const rz = 0.1 * (1 + swell * 0.8); const q2 = (p.x / rx) ** 2 + ((p.z - 0.008) / rz) ** 2; if (q2 < 1) { const k = 1 / Math.sqrt(q2); p.x *= k; p.z = 0.008 + (p.z - 0.008) * k; } } // rest on the head, never in it wv.set(p.x / A, (p.y - 0.002) / B, p.z / C); const dh = wv.length(); if (dh < 1) p.set(p.x / dh, (p.y - 0.002) / dh + 0.002, p.z / dh); // and out of the body and shoulders wv.set((p.x - torsoC.x) / (0.165 / hs), (p.y - torsoC.y) / (0.31 / hs), (p.z - torsoC.z) / (0.112 / hs)); const db = wv.length(); if (db < 1) p.set(torsoC.x + (p.x - torsoC.x) / db, torsoC.y + (p.y - torsoC.y) / db, torsoC.z + (p.z - torsoC.z) / db); for (const sc of shoulderC) { wv.subVectors(p, sc); const ds = wv.length(); if (ds < 0.062 / hs) p.copy(sc).addScaledVector(wv.normalize(), 0.062 / hs); } } else { nrm.copy(root.n); dir.copy(gH).addScaledVector(nrm, -gH.dot(nrm)); dir.z += 0.5; dir.addScaledVector(nrm, -dir.dot(nrm)).normalize(); } // the ribbon lies flat along the head: across the strand, along the surface // (once free, carry the last width direction down the strand instead: when the wind lays // a strand back along the head's normal that cross product spins, and the ribbons twist // edge-on into a shredded fringe) if (free) { side.addScaledVector(dir, -side.dot(dir)); if (side.lengthSq() < 1e-6) side.crossVectors(nrm, dir); } else side.crossVectors(nrm, dir); if (side.lengthSq() < 1e-6) side.set(1, 0, 0); side.normalize(); // blunt ends, not tapered points const w = root.w * (1 - 0.25 * (i / S)); // the ripple: a soft wave that runs from the roots to the ends, barely there at a // cruise and swelling when she speeds up. It moves only what's drawn, not the strand. off.set(0, 0, 0); if (free) { const u = i / S; const amp = (0.0015 + 0.003 * Math.min(1, ws) + 0.011 * gust) * u ** 1.2; const ph = s.time * 8 + root.phase - u * 6.5; off.copy(side).multiplyScalar(Math.sin(ph) * amp).addScaledVector(nrm, (0.5 + 0.5 * Math.sin(ph + 1.3)) * amp * 0.7); } const px = p.x + off.x, py = p.y + off.y, pz = p.z + off.z; hairPos.set([px - side.x * w, py - side.y * w, pz - side.z * w, px + side.x * w, py + side.y * w, pz + side.z * w], base + i * 6); // shade the hair as one sleek surface: every ribbon takes the outward normal of the // head's shape where it lies, so the shine runs in one band const nn = i === 0 ? root.n : nrm; hairNrm.set([nn.x, nn.y, nn.z, nn.x, nn.y, nn.z], base + i * 6); } }); hairGeo.attributes.position.needsUpdate = true; hairGeo.attributes.normal.needsUpdate = true; } return { group, chest, head, hairMaterial: hairMat, /** s: { crouch, air, tumble, steer, wind (0…1.3), windDir (world), gust (0…1), time }. Call pose() * first, then update the world matrices, then dress(). */ pose, dress(s) { flutter(s); strands(s); }, }; }