// Characters as single skinned meshes (one draw call each): villagers in // Jiangnan dress, the ox & cart, egrets. Parts are rigidly bound to bones. import * as THREE from 'three' import { std } from './materials' import { rrange } from './rng' type RGB = [number, number, number] class SkinBuilder { pos: number[] = [] nrm: number[] = [] col: number[] = [] idx: number[] = [] wgt: number[] = [] private v = new THREE.Vector3() private n = new THREE.Vector3() /** add geometry (already in bind pose / model space) bound to `bone` with a flat colour */ add(g: THREE.BufferGeometry, m: THREE.Matrix4, bone: number, color: RGB, shade = true) { const geo = g.index ? g.toNonIndexed() : g if (!geo.getAttribute('normal')) geo.computeVertexNormals() const p = geo.getAttribute('position'), nn = geo.getAttribute('normal') const nm = new THREE.Matrix3().getNormalMatrix(m) for (let i = 0; i < p.count; i++) { this.v.set(p.getX(i), p.getY(i), p.getZ(i)).applyMatrix4(m) this.n.set(nn.getX(i), nn.getY(i), nn.getZ(i)).applyMatrix3(nm).normalize() this.pos.push(this.v.x, this.v.y, this.v.z) this.nrm.push(this.n.x, this.n.y, this.n.z) // subtle ambient-occlusion-like darkening toward the bottom of each part const k = shade ? 0.85 + 0.15 * Math.min(1, Math.max(0, this.n.y * 0.5 + 0.5)) : 1 this.col.push(color[0] * k, color[1] * k, color[2] * k) this.idx.push(bone, 0, 0, 0) this.wgt.push(1, 0, 0, 0) } } build() { const g = new THREE.BufferGeometry() g.setAttribute('position', new THREE.Float32BufferAttribute(this.pos, 3)) g.setAttribute('normal', new THREE.Float32BufferAttribute(this.nrm, 3)) g.setAttribute('color', new THREE.Float32BufferAttribute(this.col, 3)) g.setAttribute('skinIndex', new THREE.Uint16BufferAttribute(this.idx, 4)) g.setAttribute('skinWeight', new THREE.Float32BufferAttribute(this.wgt, 4)) g.computeBoundingSphere() return g } } const M = (x = 0, y = 0, z = 0, rx = 0, ry = 0, rz = 0, sx = 1, sy = 1, sz = 1) => new THREE.Matrix4().compose(new THREE.Vector3(x, y, z), new THREE.Quaternion().setFromEuler(new THREE.Euler(rx, ry, rz)), new THREE.Vector3(sx, sy, sz)) function lathe(profile: [number, number][], seg = 14) { return new THREE.LatheGeometry(profile.map(([r, y]) => new THREE.Vector2(Math.max(0.001, r), y)), seg) } let charMat: THREE.Material | null = null export function characterMaterial() { if (!charMat) charMat = std({ vertexColors: true, roughness: 0.82, metalness: 0, side: THREE.DoubleSide }, { key: 'char' }) return charMat } function makeSkinned(sb: SkinBuilder, bones: THREE.Bone[]) { const mesh = new THREE.SkinnedMesh(sb.build(), characterMaterial()) mesh.add(bones[0]) mesh.updateMatrixWorld(true) mesh.bind(new THREE.Skeleton(bones)) mesh.castShadow = true mesh.receiveShadow = true mesh.frustumCulled = false return mesh } function bone(parent: THREE.Bone | null, x: number, y: number, z: number) { const b = new THREE.Bone() b.position.set(x, y, z) if (parent) parent.add(b) return b } // ---------------------------------------------------------------- villager const CLOTH: RGB[] = [[0.07, 0.1, 0.22], [0.12, 0.17, 0.3], [0.26, 0.25, 0.23], [0.22, 0.14, 0.08], [0.32, 0.3, 0.27], [0.08, 0.08, 0.09], [0.36, 0.12, 0.08]] const SKIN: RGB[] = [[0.62, 0.43, 0.31], [0.55, 0.37, 0.26], [0.68, 0.5, 0.38]] export interface Villager { mesh: THREE.SkinnedMesh root: THREE.Bone; hips: THREE.Bone; spine: THREE.Bone; head: THREE.Bone legL: THREE.Bone; legR: THREE.Bone; armL: THREE.Bone; armR: THREE.Bone; load: THREE.Bone hasPole: boolean hipY: number } export function makeVillager(rng: () => number, opts: { pole?: boolean; hat?: boolean; seated?: boolean; female?: boolean; robe?: boolean }): Villager { const female = opts.female ?? rng() < 0.4 const robe = opts.robe ?? (!female && rng() < 0.25) const cloth = CLOTH[Math.floor(rng() * CLOTH.length)] const cloth2 = CLOTH[Math.floor(rng() * CLOTH.length)] const trouser: RGB = female ? CLOTH[Math.floor(rng() * 3)] : [0.05, 0.05, 0.06] const skin = SKIN[Math.floor(rng() * SKIN.length)] const hair: RGB = [0.025, 0.022, 0.02] const straw: RGB = [0.62, 0.5, 0.28] const H = rrange(rng, 0.94, 1.04) * (female ? 0.95 : 1) // bones (bind pose = standing, facing +z) const root = bone(null, 0, 0, 0) const hips = bone(root, 0, 0.92 * H, 0) const spine = bone(hips, 0, 0.02, 0) const head = bone(spine, 0, 0.62 * H, 0) const legL = bone(hips, -0.09, 0, 0) const legR = bone(hips, 0.09, 0, 0) const armL = bone(spine, -0.2 * H, 0.54 * H, 0) const armR = bone(spine, 0.2 * H, 0.54 * H, 0) const load = bone(spine, 0, 0.6 * H, 0.0) const bones = [root, hips, spine, head, legL, legR, armL, armR, load] const B = { root: 0, hips: 1, spine: 2, head: 3, legL: 4, legR: 5, armL: 6, armR: 7, load: 8 } root.updateMatrixWorld(true) const sb = new SkinBuilder() const hy = 0.92 * H // legs: trouser + cloth shoe for (const [sx, b] of [[-0.09, B.legL], [0.09, B.legR]] as const) { const leg = lathe([[0.04, 0], [0.05, 0.08], [0.058, 0.4], [0.075, 0.82], [0.07, 0.9]], 10) sb.add(leg, M(sx, hy - 0.9 * H, 0, 0, 0, 0, 1, H, 1), b, trouser) const shoe = new THREE.SphereGeometry(0.06, 10, 6, 0, Math.PI * 2, 0, Math.PI / 2) sb.add(shoe, M(sx, hy - 0.9 * H, 0.03, 0, 0, 0, 0.9, 0.7, 1.9), b, [0.04, 0.035, 0.03]) } // torso (jacket) — flattened lathe with shoulders const torso = lathe([[0.14, 0], [0.155, 0.12], [0.175, 0.32], [0.19, 0.48], [0.17, 0.55], [0.07, 0.6], [0.045, 0.62]], 16) sb.add(torso, M(0, hy, 0, 0, 0, 0, 1, H, 0.72), B.spine, cloth) // jacket hem / robe skirt if (robe || female) { const len = robe ? 0.82 : 0.32 const skirt = lathe([[0.15, 0], [0.17, -len * 0.4], [robe ? 0.22 : 0.2, -len]], 16) sb.add(skirt, M(0, hy + 0.04, 0, 0, 0, 0, 1, H, 0.8), robe ? B.hips : B.spine, robe ? cloth2 : cloth) } else { const hem = lathe([[0.15, 0], [0.165, -0.12]], 16) sb.add(hem, M(0, hy + 0.03, 0, 0, 0, 0, 1, H, 0.78), B.spine, cloth) } // sash sb.add(new THREE.CylinderGeometry(0.152, 0.152, 0.05, 16, 1, true), M(0, hy + 0.06, 0, 0, 0, 0, 1, 1, 0.74), B.spine, [0.12, 0.09, 0.06]) // arms: sleeve + forearm + hand for (const [sx, b] of [[-1, B.armL], [1, B.armR]] as const) { const sx0 = sx * 0.2 * H const sleeve = lathe([[0.062, 0], [0.06, -0.25], [0.07, -0.42], [0.072, -0.45]], 10) sb.add(sleeve, M(sx0, hy + 0.54 * H, 0, 0, 0, sx * 0.06, 1, H, 1), b, cloth) const fore = lathe([[0.035, -0.4], [0.032, -0.55]], 8) sb.add(fore, M(sx0 + sx * 0.03, hy + 0.54 * H, 0, 0, 0, sx * 0.06, 1, H, 1), b, skin) sb.add(new THREE.SphereGeometry(0.04, 8, 6), M(sx0 + sx * 0.04, hy + 0.54 * H - 0.6 * H, 0.01, 0, 0, 0, 0.8, 1.15, 0.6), b, skin) } // head & neck const hyH = hy + 0.62 * H sb.add(new THREE.CylinderGeometry(0.035, 0.04, 0.08, 8), M(0, hyH + 0.02, 0), B.head, skin) sb.add(new THREE.SphereGeometry(0.1, 16, 12), M(0, hyH + 0.15, 0.005, 0, 0, 0, 0.92, 1.12, 1), B.head, skin) sb.add(new THREE.SphereGeometry(0.03, 6, 4), M(0, hyH + 0.14, 0.095, 0, 0, 0, 0.6, 0.9, 0.5), B.head, skin) // nose for (const ex of [-0.093, 0.093]) sb.add(new THREE.SphereGeometry(0.022, 6, 4), M(ex, hyH + 0.15, 0, 0, 0, 0, 0.5, 1, 0.8), B.head, skin) // ears if (female) { sb.add(new THREE.SphereGeometry(0.106, 14, 10, 0, Math.PI * 2, 0, Math.PI * 0.62), M(0, hyH + 0.16, -0.008, -0.25, 0, 0, 1, 1.05, 1.02), B.head, hair) sb.add(new THREE.SphereGeometry(0.05, 10, 8), M(0, hyH + 0.17, -0.11), B.head, hair) // bun if (rng() < 0.6) sb.add(new THREE.SphereGeometry(0.112, 14, 8, 0, Math.PI * 2, 0, Math.PI * 0.42), M(0, hyH + 0.165, -0.01, -0.35, 0, 0, 1.02, 1, 1.05), B.head, [0.15, 0.22, 0.42]) // indigo headscarf } else { sb.add(new THREE.SphereGeometry(0.104, 14, 10, 0, Math.PI * 2, 0, Math.PI * 0.5), M(0, hyH + 0.16, -0.01, -0.2, 0, 0, 0.96, 1.02, 1.02), B.head, hair) } if (opts.hat) { // conical bamboo hat (斗笠) with a slightly concave profile + chin strap rim const hat = lathe([[0.4, 0], [0.33, 0.035], [0.2, 0.09], [0.06, 0.15], [0.0, 0.17]], 22) sb.add(hat, M(0, hyH + 0.21, 0), B.head, straw) const under = lathe([[0.0, 0.16], [0.06, 0.14], [0.2, 0.08], [0.33, 0.025], [0.4, -0.005]], 22) sb.add(under, M(0, hyH + 0.21, 0), B.head, [0.42, 0.34, 0.18]) } let hasPole = false if (opts.pole) { hasPole = true // shoulder pole across the right shoulder with two baskets const ly = hy + 0.6 * H const poleG = new THREE.CylinderGeometry(0.022, 0.022, 1.9, 6) poleG.rotateX(Math.PI / 2) sb.add(poleG, M(0.12, ly + 0.02, 0), B.load, [0.5, 0.45, 0.24]) for (const z of [-0.85, 0.85]) { for (const dx of [-0.13, 0.13]) { const rope = new THREE.CylinderGeometry(0.004, 0.004, 0.72, 3) sb.add(rope, M(0.12 + dx * 0.6, ly - 0.36, z + dx * 0.2, 0, 0, dx * 0.35), B.load, [0.3, 0.25, 0.15], false) } const basket = lathe([[0.16, 0], [0.24, 0.08], [0.26, 0.26], [0.25, 0.28], [0.0, 0.28]], 14) sb.add(basket, M(0.12, ly - 0.98, z), B.load, [0.55, 0.42, 0.22]) const fill = new THREE.SphereGeometry(0.23, 10, 6, 0, Math.PI * 2, 0, Math.PI / 2) const c: RGB = rng() < 0.5 ? [0.2, 0.36, 0.08] : [0.72, 0.32, 0.06] sb.add(fill, M(0.12, ly - 0.71, z, 0, 0, 0, 1, 0.5, 1), B.load, c) } } const mesh = makeSkinned(sb, bones) const v: Villager = { mesh, root, hips, spine, head, legL, legR, armL, armR, load, hasPole, hipY: hy } if (opts.pole) { armR.rotation.set(-2.5, 0, 0.35) // hand up on the pole load.rotation.y = 0.25 } if (opts.seated) { hips.position.y = 0.55 legL.rotation.x = legR.rotation.x = -1.45 armL.rotation.x = armR.rotation.x = -0.6 spine.rotation.x = 0.12 } return v } export function walkVillager(v: Villager, phase: number, amp = 1) { const s = Math.sin(phase) v.legL.rotation.x = s * 0.46 * amp v.legR.rotation.x = -s * 0.46 * amp v.armL.rotation.x = -s * 0.36 * amp if (!v.hasPole) v.armR.rotation.x = s * 0.36 * amp v.hips.position.y = v.hipY + Math.abs(Math.cos(phase)) * 0.03 * amp v.hips.rotation.y = s * 0.06 * amp v.spine.rotation.y = -s * 0.08 * amp v.spine.rotation.x = v.hasPole ? 0.08 : 0.03 v.head.rotation.y = s * 0.03 if (v.hasPole) v.load.rotation.z = Math.sin(phase * 2) * 0.04 } // ---------------------------------------------------------------- ox & cart export interface OxCart { mesh: THREE.SkinnedMesh legs: THREE.Bone[]; head: THREE.Bone; tail: THREE.Bone; wheels: THREE.Bone[] } export function makeOxCart(): OxCart { const hide: RGB = [0.17, 0.11, 0.075], dark: RGB = [0.06, 0.045, 0.035], horn: RGB = [0.62, 0.56, 0.45] const wood: RGB = [0.3, 0.2, 0.12], hay: RGB = [0.6, 0.48, 0.24] const root = bone(null, 0, 0, 0) const body = bone(root, 0, 1.15, 0) const head = bone(body, 0, 0.12, 0.95) const tail = bone(body, 0, 0.15, -0.95) const legs = [[-0.24, 0.62], [0.24, 0.62], [-0.24, -0.62], [0.24, -0.62]].map(([x, z]) => bone(body, x, -0.15, z)) const wheels = [-0.82, 0.82].map((x) => bone(root, x, 0.62, -2.4)) const bones = [root, body, head, tail, ...legs, ...wheels] root.updateMatrixWorld(true) const sb = new SkinBuilder() // barrel body: lathe along z const barrel = lathe([[0.0, -0.95], [0.3, -0.88], [0.42, -0.5], [0.45, 0.1], [0.43, 0.6], [0.34, 0.9], [0.0, 1.0]], 18) sb.add(barrel, M(0, 1.15, 0, Math.PI / 2, 0, 0, 1, 1, 0.92), 1, hide) sb.add(new THREE.SphereGeometry(0.28, 12, 8), M(0, 1.42, 0.55, 0, 0, 0, 1, 0.8, 1.2), 1, hide) // hump sb.add(new THREE.SphereGeometry(0.2, 10, 8), M(0, 0.95, 0.75, 0, 0, 0, 1.3, 1, 1.4), 1, hide) // dewlap/chest // head with muzzle, horns, ears sb.add(new THREE.SphereGeometry(0.2, 14, 10), M(0, 1.3, 1.12, 0.3, 0, 0, 0.9, 1, 1.25), 2, hide) sb.add(new THREE.SphereGeometry(0.13, 12, 8), M(0, 1.14, 1.36, 0.3, 0, 0, 1.05, 0.85, 1), 2, dark) for (const sx of [-1, 1]) { const hornC = new THREE.TorusGeometry(0.16, 0.035, 6, 10, Math.PI * 0.7) sb.add(hornC, M(sx * 0.14, 1.48, 1.04, 0, Math.PI / 2, sx > 0 ? 0.2 : Math.PI - 0.2, 1, 1, 1), 2, horn) sb.add(new THREE.SphereGeometry(0.07, 8, 6), M(sx * 0.24, 1.38, 1.0, 0, 0, sx * 0.5, 1.4, 0.6, 0.5), 2, hide) sb.add(new THREE.SphereGeometry(0.03, 6, 4), M(sx * 0.12, 1.36, 1.27), 2, [0.02, 0.02, 0.02]) } // tail sb.add(new THREE.CylinderGeometry(0.02, 0.03, 0.7, 5), M(0, 0.95, -1.0, 0.15, 0, 0), 3, hide) sb.add(new THREE.SphereGeometry(0.05, 6, 6), M(0, 0.6, -1.05, 0, 0, 0, 1, 1.8, 1), 3, dark) // legs with hooves legs.forEach((_, i) => { const x = i % 2 ? 0.24 : -0.24, z = i < 2 ? 0.62 : -0.62 const leg = lathe([[0.05, -1.0], [0.06, -0.62], [0.075, -0.4], [0.11, -0.1], [0.12, 0.05]], 10) sb.add(leg, M(x, 1.0, z), 4 + i, hide) sb.add(new THREE.CylinderGeometry(0.06, 0.065, 0.08, 8), M(x, 0.04, z + 0.01), 4 + i, dark) }) // yoke + shafts + cart bed + hay + wheels sb.add(new THREE.CylinderGeometry(0.04, 0.04, 0.9, 6), M(0, 1.45, 0.75, 0, 0, Math.PI / 2), 1, wood) for (const sx of [-0.45, 0.45]) sb.add(new THREE.BoxGeometry(0.07, 0.07, 2.6), M(sx, 1.0, -1.0, -0.08, 0, 0), 0, wood) sb.add(new THREE.BoxGeometry(1.45, 0.1, 2.1), M(0, 0.98, -2.4), 0, wood) for (const sx of [-0.7, 0.7]) sb.add(new THREE.BoxGeometry(0.06, 0.32, 2.1), M(sx, 1.18, -2.4), 0, wood) sb.add(new THREE.SphereGeometry(0.75, 12, 8, 0, Math.PI * 2, 0, Math.PI / 2), M(0, 1.02, -2.4, 0, 0, 0, 0.88, 0.72, 1.25), 0, hay) ;[6, 7].forEach((bi, k) => { const sx = k ? 0.82 : -0.82 const rim = new THREE.TorusGeometry(0.6, 0.05, 6, 22) sb.add(rim, M(sx, 0.62, -2.4, 0, Math.PI / 2, 0), bi, wood) sb.add(new THREE.CylinderGeometry(0.1, 0.1, 0.16, 10), M(sx, 0.62, -2.4, 0, 0, Math.PI / 2), bi, wood) for (let s = 0; s < 8; s++) sb.add(new THREE.BoxGeometry(0.05, 1.15, 0.05), M(sx, 0.62, -2.4, (s / 8) * Math.PI, 0, 0), bi, [0.45, 0.35, 0.22]) }) const mesh = makeSkinned(sb, bones) return { mesh, legs, head, tail, wheels } } export function animateOx(o: OxCart, dist: number, t: number) { const ph = dist * 2.6 o.legs.forEach((l, i) => { l.rotation.x = Math.sin(ph + (i === 0 || i === 3 ? 0 : Math.PI)) * 0.32 }) o.head.rotation.x = Math.sin(ph * 2) * 0.04 + 0.05 o.head.rotation.y = Math.sin(t * 0.4) * 0.08 o.tail.rotation.z = Math.sin(t * 1.7) * 0.25 o.wheels.forEach((w) => { w.rotation.x = dist / 0.62 }) } // ---------------------------------------------------------------- egret export interface Egret { mesh: THREE.SkinnedMesh; root: THREE.Bone; wl: THREE.Bone; wr: THREE.Bone } export function makeEgret(): Egret { const white: RGB = [0.93, 0.93, 0.91], beak: RGB = [0.78, 0.62, 0.16], legC: RGB = [0.05, 0.05, 0.05] const root = bone(null, 0, 0, 0) const wl = bone(root, -0.06, 0.02, 0.02) const wr = bone(root, 0.06, 0.02, 0.02) root.updateMatrixWorld(true) const sb = new SkinBuilder() sb.add(new THREE.SphereGeometry(0.12, 14, 10), M(0, 0, 0, 0, 0, 0, 0.95, 0.85, 2.3), 0, white) // S-curved neck tucked back in flight, head, beak sb.add(new THREE.CylinderGeometry(0.035, 0.05, 0.22, 8), M(0, 0.07, 0.3, 1.0, 0, 0), 0, white) sb.add(new THREE.SphereGeometry(0.055, 10, 8), M(0, 0.12, 0.4, 0, 0, 0, 0.9, 0.9, 1.3), 0, white) sb.add(new THREE.ConeGeometry(0.018, 0.17, 6), M(0, 0.11, 0.53, Math.PI / 2, 0, 0), 0, beak) sb.add(new THREE.CylinderGeometry(0.01, 0.01, 0.5, 4), M(-0.025, -0.02, -0.5, Math.PI / 2, 0, 0), 0, legC) sb.add(new THREE.CylinderGeometry(0.01, 0.01, 0.5, 4), M(0.025, -0.02, -0.5, Math.PI / 2, 0, 0), 0, legC) // wings: curved feathered shape (extruded thin) for each side const shape = new THREE.Shape() shape.moveTo(0, 0.12) shape.bezierCurveTo(0.35, 0.22, 0.8, 0.2, 1.12, 0.02) shape.lineTo(1.0, -0.04) for (let k = 0; k < 6; k++) shape.lineTo(0.95 - k * 0.15, -0.1 - (k % 2) * 0.05) // primary feathers shape.lineTo(0, -0.14) const wg = new THREE.ExtrudeGeometry(shape, { depth: 0.012, bevelEnabled: false, curveSegments: 10 }) wg.rotateX(-Math.PI / 2) sb.add(wg, M(0.06, 0.02, 0.02), 2, white) sb.add(wg.clone(), M(-0.06, 0.02, 0.02, 0, 0, 0, -1, 1, 1), 1, white) const mesh = makeSkinned(sb, [root, wl, wr]) mesh.castShadow = true return { mesh, root, wl, wr } }