// RigLM sculpt engine: signed-distance "clay", a surface-nets mesher, and genome builders // for six body plans plus scenery. Pure JS (no three.js) so it runs in the browser and in Node. (function (root) { "use strict"; // ------------------------------------------------------------------ small math const V = { add: (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]], sub: (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]], mul: (a, s) => [a[0] * s, a[1] * s, a[2] * s], dot: (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2], len: (a) => Math.hypot(a[0], a[1], a[2]), norm: (a) => { const l = Math.hypot(a[0], a[1], a[2]) || 1; return [a[0] / l, a[1] / l, a[2] / l]; }, lerp: (a, b, t) => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t], }; function rotY(a) { const c = Math.cos(a), s = Math.sin(a); return [c, 0, s, 0, 1, 0, -s, 0, c]; } function rotX(a) { const c = Math.cos(a), s = Math.sin(a); return [1, 0, 0, 0, c, -s, 0, s, c]; } function rotZ(a) { const c = Math.cos(a), s = Math.sin(a); return [c, -s, 0, s, c, 0, 0, 0, 1]; } function mm(A, B) { const C = new Array(9).fill(0); for (let i = 0; i < 3; i++) for (let j = 0; j < 3; j++) for (let k = 0; k < 3; k++) C[i * 3 + j] += A[i * 3 + k] * B[k * 3 + j]; return C; } function mv(A, v) { return [A[0] * v[0] + A[1] * v[1] + A[2] * v[2], A[3] * v[0] + A[4] * v[1] + A[5] * v[2], A[6] * v[0] + A[7] * v[1] + A[8] * v[2]]; } function mtv(A, v) { return [A[0] * v[0] + A[3] * v[1] + A[6] * v[2], A[1] * v[0] + A[4] * v[1] + A[7] * v[2], A[2] * v[0] + A[5] * v[1] + A[8] * v[2]]; } function RNG(seed) { let s = (seed >>> 0) || 1; const r = () => { s |= 0; s = (s + 0x6D2B79F5) | 0; let t = Math.imul(s ^ (s >>> 15), 1 | s); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; r.u = (a, b) => a + (b - a) * r(); r.i = (n) => Math.floor(r() * n); r.n = () => { let u = 0, v = 0; while (u === 0) u = r(); v = r(); return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v); }; r.pick = (a) => a[Math.floor(r() * a.length)]; return r; } function hashStr(s) { let h = 2166136261; for (let i = 0; i < s.length; i++) { h ^= s.charCodeAt(i); h = Math.imul(h, 16777619); } return h >>> 0; } // 3D value noise (for fur patterns, rock lumps) function hash3(x, y, z, seed) { let h = (x * 374761393 + y * 668265263 + z * 2147483647 + seed * 1274126177) | 0; h = Math.imul(h ^ (h >>> 13), 1274126177); return ((h ^ (h >>> 16)) >>> 0) / 4294967296; } function noise3(p, seed) { const xi = Math.floor(p[0]), yi = Math.floor(p[1]), zi = Math.floor(p[2]); const xf = p[0] - xi, yf = p[1] - yi, zf = p[2] - zi; const s = (t) => t * t * (3 - 2 * t); const u = s(xf), v = s(yf), w = s(zf); let r = 0; for (let dx = 0; dx < 2; dx++) for (let dy = 0; dy < 2; dy++) for (let dz = 0; dz < 2; dz++) r += hash3(xi + dx, yi + dy, zi + dz, seed) * (dx ? u : 1 - u) * (dy ? v : 1 - v) * (dz ? w : 1 - w); return r; } // ------------------------------------------------------------------ colour helpers function hex2rgb(h) { h = h.replace("#", ""); if (h.length === 3) h = h.split("").map(c => c + c).join(""); const n = parseInt(h, 16); return [(n >> 16 & 255) / 255, (n >> 8 & 255) / 255, (n & 255) / 255]; } function rgb2hsl([r, g, b]) { const mx = Math.max(r, g, b), mn = Math.min(r, g, b); let h = 0, s = 0; const l = (mx + mn) / 2; if (mx !== mn) { const d = mx - mn; s = l > 0.5 ? d / (2 - mx - mn) : d / (mx + mn); h = mx === r ? (g - b) / d + (g < b ? 6 : 0) : mx === g ? (b - r) / d + 2 : (r - g) / d + 4; h /= 6; } return [h, s, l]; } function hsl2rgb([h, s, l]) { if (s === 0) return [l, l, l]; const f = (p, q, t) => { if (t < 0) t += 1; if (t > 1) t -= 1; if (t < 1 / 6) return p + (q - p) * 6 * t; if (t < 1 / 2) return q; if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6; return p; }; const q = l < 0.5 ? l * (1 + s) : l + s - l * s, p = 2 * l - q; return [f(p, q, h + 1 / 3), f(p, q, h), f(p, q, h - 1 / 3)]; } function jitterColor(c, rng, amt) { const hsl = rgb2hsl(c); hsl[0] = (hsl[0] + rng.n() * 0.02 * amt + 1) % 1; hsl[1] = Math.min(1, Math.max(0, hsl[1] + rng.n() * 0.06 * amt)); hsl[2] = Math.min(0.95, Math.max(0.05, hsl[2] + rng.n() * 0.06 * amt)); return hsl2rgb(hsl); } const col = (c) => typeof c === "string" ? hex2rgb(c) : c; // ------------------------------------------------------------------ SDF primitives // cap: round cone a->b radii r1,r2 | ell: ellipsoid c, r[3], optional rotation R | box: rounded box c, h[3], R, rr function primBound(p) { if (p.t === "cap" || p.t === "cone") { const c = V.lerp(p.a, p.b, 0.5); return { c, R: V.len(V.sub(p.b, p.a)) / 2 + Math.max(p.r1, p.r2) }; } if (p.t === "ell") return { c: p.c, R: Math.max(p.r[0], p.r[1], p.r[2]) }; return { c: p.c, R: Math.hypot(p.h[0], p.h[1], p.h[2]) + (p.rr || 0) }; } function primSDF(p, x) { if (p.t === "cap") { const ab = V.sub(p.b, p.a), ap = V.sub(x, p.a); const t = Math.min(1, Math.max(0, V.dot(ap, ab) / (V.dot(ab, ab) + 1e-12))); const q = V.sub(ap, V.mul(ab, t)); return V.len(q) - (p.r1 + (p.r2 - p.r1) * t); } if (p.t === "cone") { // flat-capped cone / cylinder (r1 at a, r2 at b) const ba = V.sub(p.b, p.a), pa = V.sub(x, p.a), baba = V.dot(ba, ba), papa = V.dot(pa, pa), paba = V.dot(pa, ba) / baba; const xx = Math.sqrt(Math.max(0, papa - paba * paba * baba)), rba = p.r2 - p.r1; const cax = Math.max(0, xx - (paba < 0.5 ? p.r1 : p.r2)), cay = Math.abs(paba - 0.5) - 0.5; const kk = rba * rba + baba, f = Math.min(1, Math.max(0, (rba * (xx - p.r1) + paba * baba) / kk)); const cbx = xx - p.r1 - f * rba, cby = paba - f; const sg = (cbx < 0 && cay < 0) ? -1 : 1; return sg * Math.sqrt(Math.min(cax * cax + cay * cay * baba, cbx * cbx + cby * cby * baba)); } if (p.t === "ell") { let d = V.sub(x, p.c); if (p.R) d = mtv(p.R, d); const q = [d[0] / p.r[0], d[1] / p.r[1], d[2] / p.r[2]]; const k0 = V.len(q), k1 = Math.hypot(q[0] / p.r[0], q[1] / p.r[1], q[2] / p.r[2]); return k0 * (k0 - 1) / (k1 + 1e-9); } let d = V.sub(x, p.c); if (p.R) d = mtv(p.R, d); const rr = p.rr || 0; const q = [Math.abs(d[0]) - p.h[0] + rr, Math.abs(d[1]) - p.h[1] + rr, Math.abs(d[2]) - p.h[2] + rr]; const o = Math.hypot(Math.max(q[0], 0), Math.max(q[1], 0), Math.max(q[2], 0)); return o + Math.min(Math.max(q[0], q[1], q[2]), 0) - rr; } class Sculpt { constructor(prims, k) { this.prims = prims; this.k = k; this.b = prims.map(primBound); } sdf(x) { const k = this.k; let d = 1e9; for (let i = 0; i < this.prims.length; i++) { const b = this.b[i]; const lb = Math.hypot(x[0] - b.c[0], x[1] - b.c[1], x[2] - b.c[2]) - b.R; if (lb > d + k) continue; const d2 = primSDF(this.prims[i], x); const kk = this.prims[i].hard ? 1e-4 : k; const h = Math.min(1, Math.max(0, 0.5 + 0.5 * (d2 - d) / kk)); d = d2 + (d - d2) * h - kk * h * (1 - h); } return d; } grad(x) { const e = 1e-3 * this.scale, d = this.sdf(x); return V.norm([this.sdf([x[0] + e, x[1], x[2]]) - d, this.sdf([x[0], x[1] + e, x[2]]) - d, this.sdf([x[0], x[1], x[2] + e]) - d]); } bbox() { let lo = [1e9, 1e9, 1e9], hi = [-1e9, -1e9, -1e9]; for (const b of this.b) for (let i = 0; i < 3; i++) { lo[i] = Math.min(lo[i], b.c[i] - b.R); hi[i] = Math.max(hi[i], b.c[i] + b.R); } return [lo, hi]; } } // ------------------------------------------------------------------ surface nets mesher function mesh(sc, res, floorRes) { const [lo0, hi0] = sc.bbox(); const ext = Math.max(hi0[0] - lo0[0], hi0[1] - lo0[1], hi0[2] - lo0[2]); sc.scale = ext; let minR = 1e9; for (const p of sc.prims) { if (p.deco) continue; const r = (p.t === "cap" || p.t === "cone") ? (p.r1 + p.r2) / 2 : p.t === "ell" ? Math.min(p.r[0], p.r[1], p.r[2]) : Math.min(p.h[0], p.h[1], p.h[2]); minR = Math.min(minR, r); } let h = Math.max(Math.min(ext / res, sc.k * 0.9, minR * 0.95), ext / (floorRes || 76)), n, lo, hi; for (;;) { const pad = 2 * h + sc.k; lo = lo0.map(v => v - pad); hi = hi0.map(v => v + pad); n = [0, 1, 2].map(i => Math.ceil((hi[i] - lo[i]) / h) + 1); if (n[0] * n[1] * n[2] < 650000) break; h *= 1.12; } const nx = n[0], ny = n[1], nz = n[2]; const F = new Float32Array(nx * ny * nz); const id = (i, j, k) => (i * ny + j) * nz + k; // narrow band: sample a coarse grid first, refine only near the surface const cs = 4, cx = Math.ceil(nx / cs) + 1, cy = Math.ceil(ny / cs) + 1, cz = Math.ceil(nz / cs) + 1; const Fc = new Float32Array(cx * cy * cz); for (let i = 0; i < cx; i++) for (let j = 0; j < cy; j++) for (let k = 0; k < cz; k++) Fc[(i * cy + j) * cz + k] = sc.sdf([lo[0] + i * cs * h, lo[1] + j * cs * h, lo[2] + k * cs * h]); const band = 2.6 * cs * h; for (let i = 0; i < nx; i++) for (let j = 0; j < ny; j++) for (let k = 0; k < nz; k++) { const v = Fc[(Math.round(i / cs) * cy + Math.round(j / cs)) * cz + Math.round(k / cs)]; F[id(i, j, k)] = Math.abs(v) > band ? v : sc.sdf([lo[0] + i * h, lo[1] + j * h, lo[2] + k * h]); } const vidx = new Int32Array((nx - 1) * (ny - 1) * (nz - 1)).fill(-1); const cid = (i, j, k) => (i * (ny - 1) + j) * (nz - 1) + k; const verts = []; const corners = [[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 0, 1], [1, 0, 1], [0, 1, 1], [1, 1, 1]]; const edges = [[0, 1], [2, 3], [4, 5], [6, 7], [0, 2], [1, 3], [4, 6], [5, 7], [0, 4], [1, 5], [2, 6], [3, 7]]; const cv = new Float32Array(8); for (let i = 0; i < nx - 1; i++) for (let j = 0; j < ny - 1; j++) for (let k = 0; k < nz - 1; k++) { let mask = 0; for (let c = 0; c < 8; c++) { cv[c] = F[id(i + corners[c][0], j + corners[c][1], k + corners[c][2])]; if (cv[c] < 0) mask |= 1 << c; } if (mask === 0 || mask === 255) continue; let sx = 0, sy = 0, sz = 0, cnt = 0; for (const [a, b] of edges) { const va = cv[a], vb = cv[b]; if ((va < 0) === (vb < 0)) continue; const t = va / (va - vb); sx += corners[a][0] + (corners[b][0] - corners[a][0]) * t; sy += corners[a][1] + (corners[b][1] - corners[a][1]) * t; sz += corners[a][2] + (corners[b][2] - corners[a][2]) * t; cnt++; } vidx[cid(i, j, k)] = verts.length; verts.push([lo[0] + (i + sx / cnt) * h, lo[1] + (j + sy / cnt) * h, lo[2] + (k + sz / cnt) * h]); } const faces = []; const quad = (a, b, c, d, flip) => { if (a < 0 || b < 0 || c < 0 || d < 0) return; if (flip) { faces.push([a, c, b], [a, d, c]); } else { faces.push([a, b, c], [a, c, d]); } }; for (let i = 0; i < nx - 1; i++) for (let j = 0; j < ny - 1; j++) for (let k = 0; k < nz - 1; k++) { const v0 = F[id(i, j, k)]; const inside = v0 < 0; if (j > 0 && k > 0 && (F[id(i + 1, j, k)] < 0) !== inside) quad(vidx[cid(i, j - 1, k - 1)], vidx[cid(i, j, k - 1)], vidx[cid(i, j, k)], vidx[cid(i, j - 1, k)], !inside); if (i > 0 && k > 0 && (F[id(i, j + 1, k)] < 0) !== inside) quad(vidx[cid(i - 1, j, k - 1)], vidx[cid(i - 1, j, k)], vidx[cid(i, j, k)], vidx[cid(i, j, k - 1)], !inside); if (i > 0 && j > 0 && (F[id(i, j, k + 1)] < 0) !== inside) quad(vidx[cid(i - 1, j - 1, k)], vidx[cid(i, j - 1, k)], vidx[cid(i, j, k)], vidx[cid(i - 1, j, k)], !inside); } // snap vertices onto the true surface and get normals from the SDF gradient const normals = []; for (let v = 0; v < verts.length; v++) { let p = verts[v]; for (let it = 0; it < 2; it++) { const d = sc.sdf(p); const g = sc.grad(p); p = V.sub(p, V.mul(g, d)); } verts[v] = p; normals.push(sc.grad(p)); } // make winding agree with outward normals for (const f of faces) { const a = verts[f[0]], b = verts[f[1]], c = verts[f[2]]; const fn = [(b[1] - a[1]) * (c[2] - a[2]) - (b[2] - a[2]) * (c[1] - a[1]), (b[2] - a[2]) * (c[0] - a[0]) - (b[0] - a[0]) * (c[2] - a[2]), (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])]; const nn = V.add(V.add(normals[f[0]], normals[f[1]]), normals[f[2]]); if (V.dot(fn, nn) < 0) { const t = f[1]; f[1] = f[2]; f[2] = t; } } return { verts, faces, normals }; } // ------------------------------------------------------------------ painting function paint(sc, M, mats, eyes, pattern, seed) { const k = sc.k * 0.6; const out = []; for (const p of M.verts) { let best = 1e9; const ds = []; for (let i = 0; i < sc.prims.length; i++) { const b = sc.b[i]; const lb = Math.hypot(p[0] - b.c[0], p[1] - b.c[1], p[2] - b.c[2]) - b.R; if (lb > best + 3 * k) continue; const d = primSDF(sc.prims[i], p); ds.push([i, d]); if (d < best) best = d; } let c = [0, 0, 0], ws = 0, patw = 0; for (const [i, d] of ds) { const pr = sc.prims[i]; const w = Math.exp(-(d - best) / k); if (w < 1e-3) continue; const m = col(mats[pr.mat] || mats.base); c = V.add(c, V.mul(m, w)); ws += w; if (pr.pat) patw += w; } c = V.mul(c, 1 / (ws || 1)); patw /= ws || 1; if (pattern && pattern.type !== "none" && patw > 0) { const s = pattern.scale; const q = [p[0] / s, p[1] / s, p[2] / s]; let m = 0; if (pattern.type === "spots") m = noise3(q, seed) > 0.68 ? 1 : 0; else if (pattern.type === "stripes") m = Math.sin((p[2] * 1.0 + 0.4 * noise3(q, seed)) / s * 6.28 + p[1] / s) > 0.35 ? 1 : 0; else if (pattern.type === "patches") m = noise3(V.mul(q, 0.5), seed) > 0.55 ? 1 : 0; else if (pattern.type === "rings") m = Math.sin(p[2] / s * 6.28) > 0.3 ? 1 : 0; else if (pattern.type === "speckle") m = noise3(V.mul(q, 3), seed) > 0.7 ? 1 : 0; if (m) c = V.lerp(c, col(mats.accent), patw * 0.9); } // subtle clay variation const nz = noise3(V.mul(p, 9 / sc.scale), seed + 7) - 0.5; c = c.map(v => Math.min(1, Math.max(0, v * (1 + 0.12 * nz)))); for (const e of eyes) { const dd = V.len(V.sub(p, e.c)); if (dd < e.r) c = col(mats.eye || "#15141a"); else if (dd < e.r * 1.55) c = V.lerp(c, col(mats.eyeW || "#f5f2ea"), 0.95); const hl = V.add(e.c, [0, e.r * 0.35, 0]); if (dd < e.r && V.len(V.sub(p, hl)) < e.r * 0.33) c = [1, 1, 1]; } out.push(c); } return out; } // ------------------------------------------------------------------ body plans const FAMILIES = { biped: { joints: ["pelvis", "spine", "chest", "neck", "head", "L_shoulder", "L_elbow", "L_wrist", "R_shoulder", "R_elbow", "R_wrist", "L_hip", "L_knee", "L_ankle", "R_hip", "R_knee", "R_ankle"], parent: [-1, 0, 1, 2, 3, 2, 5, 6, 2, 8, 9, 0, 11, 12, 0, 14, 15], canon: { 5: [1, 0, 0], 8: [-1, 0, 0], 11: [0, -1, 0], 14: [0, -1, 0] }, feet: [13, 16], actions: ["idle", "walk", "run", "wave", "jump", "dance"], }, quadruped: { joints: ["pelvis", "spine", "chest", "neck", "head", "FL_shoulder", "FL_elbow", "FL_foot", "FR_shoulder", "FR_elbow", "FR_foot", "BL_hip", "BL_knee", "BL_foot", "BR_hip", "BR_knee", "BR_foot", "tail1", "tail2"], parent: [-1, 0, 1, 2, 3, 2, 5, 6, 2, 8, 9, 0, 11, 12, 0, 14, 15, 0, 17], canon: { 5: [0, -1, 0], 8: [0, -1, 0], 11: [0, -1, 0], 14: [0, -1, 0] }, feet: [7, 10, 13, 16], actions: ["idle", "walk", "run", "jump", "sit", "eat"], }, bird: { joints: ["pelvis", "chest", "neck", "head", "L_wing1", "L_wing2", "L_wingtip", "R_wing1", "R_wing2", "R_wingtip", "L_thigh", "L_shin", "L_foot", "R_thigh", "R_shin", "R_foot", "tail"], parent: [-1, 0, 1, 2, 1, 4, 5, 1, 7, 8, 0, 10, 11, 0, 13, 14, 0], canon: { 4: [1, 0, 0], 7: [-1, 0, 0], 10: [0, -1, 0], 13: [0, -1, 0] }, feet: [12, 15], actions: ["idle", "hop", "peck", "fly", "glide"], }, fish: { joints: ["body", "front", "head", "back1", "back2", "tail", "L_fin", "R_fin"], parent: [-1, 0, 1, 0, 3, 4, 0, 0], canon: {}, feet: [], actions: ["idle", "swim", "dart"], }, snake: { joints: ["head", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9"], parent: [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8], canon: {}, feet: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], actions: ["idle", "slither"], }, insect: { joints: ["thorax", "head", "abdomen", "L1_hip", "L1_knee", "L1_foot", "R1_hip", "R1_knee", "R1_foot", "L2_hip", "L2_knee", "L2_foot", "R2_hip", "R2_knee", "R2_foot", "L3_hip", "L3_knee", "L3_foot", "R3_hip", "R3_knee", "R3_foot"], parent: [-1, 0, 0, 0, 3, 4, 0, 6, 7, 0, 9, 10, 0, 12, 13, 0, 15, 16, 0, 18, 19], canon: {}, feet: [5, 8, 11, 14, 17, 20], actions: ["idle", "crawl", "run"], }, }; const FAMILY_NAMES = Object.keys(FAMILIES); // parameter ranges [lo, hi] per family (a genome is one point in this space) const RANGES = { biped: { leg: [0.5, 1.05], torso: [0.42, 0.8], neck: [0.04, 0.16], head: [0.11, 0.28], shoulder: [0.14, 0.32], hip: [0.08, 0.18], arm: [0.42, 0.88], limb: [0.03, 0.09], body: [0.11, 0.27], belly: [0, 0.13], arm_drop: [0, 0.75], leg_spread: [-0.04, 0.2], hand: [0.035, 0.09], foot: [0.08, 0.17], ears: [0, 0.06], ear_type: [0, 2.99], snout: [0, 0.15], horns: [0, 0.12], tail: [0, 0.45], boxy: [0, 1], hat: [0, 1] }, quadruped: { body_len: [0.45, 1.6], body_w: [0.12, 0.38], body_h: [0.16, 0.42], leg_len: [0.15, 1.0], leg_r: [0.03, 0.12], neck_len: [0.08, 0.75], neck_up: [0.15, 1.35], head: [0.09, 0.25], snout_len: [0.03, 0.4], snout_r: [0.03, 0.11], snout_droop: [0, 1.3], ear_type: [0, 3.99], ear_size: [0.03, 0.16], tail_len: [0.03, 0.9], tail_r: [0.015, 0.09], tail_up: [-0.6, 1.2], horn_type: [0, 3.99], horn_len: [0.04, 0.4], belly: [0, 0.14], hump: [0, 0.2], mane: [0, 1] }, bird: { body_r: [0.08, 0.3], body_len: [0.14, 0.5], up: [0, 1.25], neck_len: [0.03, 0.55], head: [0.045, 0.12], beak_len: [0.02, 0.28], beak_w: [0.012, 0.06], beak_droop: [0, 0.9], wing_len: [0.18, 1.3], wing_w: [0.05, 0.22], leg_len: [0.04, 0.65], leg_r: [0.015, 0.04], tail_len: [0.04, 0.4], tail_w: [0.03, 0.18], crest: [0, 0.1] }, fish: { length: [0.2, 3.5], height: [0.12, 0.55], width: [0.08, 0.3], head: [0.15, 0.3], tail_fin: [0.1, 0.4], dorsal: [0, 0.35], pec: [0.04, 0.2], pointy: [0, 1], round: [0, 1] }, snake: { length: [0.8, 4.5], thick: [0.02, 0.11], head: [1.2, 2.2], taper: [0.2, 0.7] }, insect: { size: [0.05, 0.6], head_r: [0.12, 0.3], thorax_r: [0.15, 0.3], abd_len: [0.25, 0.9], abd_r: [0.15, 0.45], leg_len: [0.4, 1.4], leg_r: [0.02, 0.05], antenna: [0, 0.7], mandible: [0, 0.2], shell: [0, 1], wings: [0, 0.8] }, }; // neutral values for optional features: a species that doesn't mention horns gets no horns const NEUTRAL = { biped: { belly: 0.02, arm_drop: 0.3, leg_spread: 0.05, ears: 0, ear_type: 2.5, snout: 0, horns: 0, tail: 0, boxy: 0, hat: 0, hand: 0.05, foot: 0.12, neck: 0.08 }, quadruped: { snout_droop: 0.15, ear_type: 0, horn_type: 0, horn_len: 0.1, belly: 0.02, hump: 0, mane: 0, tail_up: 0.2, tail_r: 0.03 }, bird: { crest: 0, beak_droop: 0.2, up: 0.4 }, fish: { pointy: 0, round: 0, dorsal: 0.1 }, snake: {}, insect: { antenna: 0.3, mandible: 0, shell: 0, wings: 0 }, }; const CATEGORICAL = new Set(["ear_type", "horn_type", "boxy", "hat", "shell", "mane"]); function sampleGenome(family, rng, base) { const R = RANGES[family], g = {}; for (const k in R) { const [lo, hi] = R[k]; if (base && base[k] !== undefined && CATEGORICAL.has(k)) g[k] = base[k]; else if (base && base[k] !== undefined) { const span = (hi - lo) * 0.07; g[k] = Math.min(hi, Math.max(lo, base[k] + rng.n() * span)); } else g[k] = rng.u(lo, hi); } return g; } // --- builders: each returns {joints, prims, ext (extra skin segments for leaves), eyes, unit} function buildBiped(p) { const j = []; const hipY = p.leg; j[0] = [0, hipY, 0]; j[1] = [0, hipY + p.torso * 0.35, 0]; j[2] = [0, hipY + p.torso * 0.8, 0]; j[3] = [0, hipY + p.torso, 0]; j[4] = [0, hipY + p.torso + p.neck + p.head * 0.7, 0]; const a = p.arm_drop; [[1, 5, 6, 7], [-1, 8, 9, 10]].forEach(([s, sh, el, wr]) => { const S = [s * p.shoulder, j[2][1] + p.torso * 0.12, 0], d = [s * Math.cos(a), -Math.sin(a), 0]; j[sh] = S; j[el] = V.add(S, V.mul(d, p.arm * 0.48)); j[wr] = V.add(S, V.mul(d, p.arm)); }); [[1, 11, 12, 13], [-1, 14, 15, 16]].forEach(([s, hp, kn, an]) => { const H = [s * p.hip, hipY - 0.03, 0], Fo = [s * (p.hip + Math.sin(p.leg_spread) * p.leg), p.foot * 0.35, 0]; j[hp] = H; j[kn] = V.lerp(H, Fo, 0.5); j[an] = Fo; }); const L = p.limb, b = p.body, hr = p.head, P = []; const boxy = p.boxy > 0.75; if (boxy) { P.push({ t: "box", c: V.lerp(j[0], j[2], 0.55), h: [b * 1.0, p.torso * 0.5, b * 0.7], rr: 0.03, mat: "base", pat: 1 }); P.push({ t: "box", c: j[4], h: [hr * 0.9, hr * 0.85, hr * 0.85], rr: 0.04, mat: "base" }); } else { P.push({ t: "ell", c: V.add(V.lerp(j[0], j[2], 0.5), [0, 0.02, 0]), r: [b * 1.05, p.torso * 0.55, b * 0.75], mat: "base", pat: 1 }); P.push({ t: "ell", c: V.add(j[2], [0, 0.03, 0]), r: [p.shoulder * 0.95, b * 0.7, b * 0.7], mat: "base", pat: 1 }); P.push({ t: "ell", c: j[4], r: [hr, hr * 1.05, hr * 0.95], mat: "skin" }); } if (p.belly > 0.01) P.push({ t: "ell", c: V.add(j[1], [0, 0, b * 0.3]), r: [b * 0.9 + p.belly, b * 0.9, b * 0.6 + p.belly], mat: "belly" }); P.push({ t: "cap", a: j[2], b: j[3], r1: L * 1.1, r2: L, mat: "skin" }); [[5, 6, 7], [8, 9, 10]].forEach(([s, e, w]) => { P.push({ t: "cap", a: j[s], b: j[e], r1: L * 1.15, r2: L * 0.95, mat: "base", pat: 1 }); P.push({ t: "cap", a: j[e], b: j[w], r1: L * 0.95, r2: L * 0.75, mat: "skin" }); const d = V.norm(V.sub(j[w], j[e])); P.push({ t: "ell", c: V.add(j[w], V.mul(d, p.hand * 0.9)), r: [p.hand, p.hand * 0.6, p.hand * 0.75], mat: "skin" }); }); [[11, 12, 13], [14, 15, 16]].forEach(([h, k, an]) => { P.push({ t: "cap", a: j[h], b: j[k], r1: L * 1.4, r2: L * 1.1, mat: "legs" }); P.push({ t: "cap", a: j[k], b: j[an], r1: L * 1.1, r2: L * 0.85, mat: "legs" }); P.push({ t: "cap", a: j[an], b: V.add(j[an], [0, -p.foot * 0.2, p.foot]), r1: L * 0.9, r2: L * 0.75, mat: "dark" }); }); if (p.ears > 0.01) { const et = Math.floor(p.ear_type); [1, -1].forEach(s => { if (et === 0) P.push({ t: "cap", a: V.add(j[4], [s * hr * 0.6, hr * 0.5, 0]), b: V.add(j[4], [s * hr * 1.25, hr * 1.1, -0.03]), r1: p.ears, r2: p.ears * 0.9, mat: "skin" }); else if (et === 1) P.push({ t: "cap", a: V.add(j[4], [s * hr * 0.8, 0, 0]), b: V.add(j[4], [s * (hr + p.ears * 3), hr * 0.2, -0.02]), r1: p.ears * 0.8, r2: p.ears * 0.15, mat: "skin" }); else P.push({ t: "ell", c: V.add(j[4], [s * hr * 0.95, hr * 0.1, 0]), r: [p.ears * 0.5, p.ears * 1.2, p.ears], mat: "skin" }); }); } if (p.snout > 0.01) P.push({ t: "ell", c: V.add(j[4], [0, -hr * 0.25, hr * 0.85]), r: [hr * 0.45, hr * 0.32, p.snout], mat: "belly" }); if (p.horns > 0.01) [1, -1].forEach(s => P.push({ t: "cap", a: V.add(j[4], [s * hr * 0.35, hr * 0.8, 0]), b: V.add(j[4], [s * hr * 0.5, hr * 1.25 + p.horns, -0.05]), r1: 0.035, r2: 0.008, mat: "horn", deco: 1 })); if (p.tail > 0.02) { const t0 = V.add(j[0], [0, -0.02, -b * 0.6]); P.push({ t: "cap", a: t0, b: V.add(t0, [0, 0.15, -p.tail]), r1: L * 0.8, r2: L * 0.3, mat: "base" }); } if (p.hat > 0.8) { P.push({ t: "cone", a: V.add(j[4], [0, hr * 0.85, 0]), b: V.add(j[4], [0.02, hr * 2.6, -0.04]), r1: hr * 0.8, r2: hr * 0.06, mat: "accent", hard: 1 }); P.push({ t: "cone", a: V.add(j[4], [0, hr * 0.8, 0]), b: V.add(j[4], [0, hr * 0.86, 0]), r1: hr * 1.3, r2: hr * 1.3, mat: "accent", hard: 1 }); } const eyeR = hr * 0.17; const eyes = [1, -1].map(s => { const dn = V.norm([s * 0.38, 0.12, 0.92]); return { c: V.add(j[4], V.mul(dn, boxy ? hr * 0.86 : hr * 0.97)), r: eyeR }; }); const ext = { 4: V.add(j[4], [0, hr, 0]), 7: V.add(j[7], V.mul(V.norm(V.sub(j[7], j[6])), p.hand * 1.8)), 10: V.add(j[10], V.mul(V.norm(V.sub(j[10], j[9])), p.hand * 1.8)), 13: V.add(j[13], [0, -0.03, p.foot]), 16: V.add(j[16], [0, -0.03, p.foot]) }; return { joints: j, prims: P, ext, eyes, unit: p.leg, k: 0.06 }; } function buildQuadruped(p) { const j = []; const H = p.leg_len + p.body_h * 0.35, L = p.body_len, w = p.body_w, bh = p.body_h; j[0] = [0, H, -L * 0.38]; j[1] = [0, H + 0.01, 0]; j[2] = [0, H, L * 0.36]; j[3] = V.add(j[2], [0, bh * 0.3, L * 0.12]); const nd = [0, Math.sin(p.neck_up), Math.cos(p.neck_up)]; j[4] = V.add(j[3], V.mul(nd, p.neck_len)); const footY = p.leg_r * 0.8; [[1, 5, 6, 7], [-1, 8, 9, 10]].forEach(([s, a, b, c]) => { j[a] = [s * w * 0.62, H - bh * 0.15, L * 0.36]; j[b] = [s * w * 0.62, (H - bh * 0.15 + footY) * 0.5, L * 0.36]; j[c] = [s * w * 0.62, footY, L * 0.38]; }); [[1, 11, 12, 13], [-1, 14, 15, 16]].forEach(([s, a, b, c]) => { j[a] = [s * w * 0.62, H - bh * 0.15, -L * 0.38]; j[b] = [s * w * 0.62, (H - bh * 0.15 + footY) * 0.5, -L * 0.36]; j[c] = [s * w * 0.62, footY, -L * 0.38]; }); const td = V.norm([0, Math.sin(p.tail_up), -Math.cos(p.tail_up)]); j[17] = [0, H + bh * 0.2, -L * 0.55]; j[18] = V.add(j[17], V.mul(td, p.tail_len * 0.5)); const P = []; P.push({ t: "ell", c: [0, H + 0.01, -0.02 * L], r: [w, bh * 0.55, L * 0.55], mat: "base", pat: 1 }); P.push({ t: "ell", c: V.add(j[2], [0, 0.01, 0]), r: [w * 1.0 + 0.2 * p.mane * w, bh * 0.6 + p.mane * bh * 0.2, L * 0.25], mat: p.mane > 0.75 ? "accent" : "base", pat: 1 }); P.push({ t: "ell", c: j[0], r: [w * 0.95, bh * 0.55, L * 0.25], mat: "base", pat: 1 }); if (p.belly > 0.01) P.push({ t: "ell", c: [0, H - bh * 0.15, 0], r: [w * 0.85 + p.belly * 0.5, bh * 0.4 + p.belly * 0.6, L * 0.4], mat: "belly" }); if (p.hump > 0.03) P.push({ t: "ell", c: [0, H + bh * 0.5, 0], r: [w * 0.6, p.hump + bh * 0.2, L * 0.2], mat: "base" }); const nr = Math.max(p.leg_r * 1.3, bh * 0.3) * (1 + 0.5 * p.mane); P.push({ t: "cap", a: j[3], b: j[4], r1: nr, r2: p.head * 0.7, mat: p.mane > 0.75 ? "accent" : "base", pat: 1 }); const hr = p.head; P.push({ t: "ell", c: j[4], r: [hr * 0.9, hr * 0.9, hr], mat: "base" }); const sd = [0, -Math.sin(p.snout_droop), Math.cos(p.snout_droop)]; const s0 = V.add(j[4], [0, -hr * 0.2, hr * 0.5]); P.push({ t: "cap", a: s0, b: V.add(s0, V.mul(sd, p.snout_len)), r1: p.snout_r * 1.15, r2: p.snout_r * (p.snout_droop > 0.8 ? 0.6 : 0.9), mat: "belly" }); P.push({ t: "ell", c: V.add(s0, V.mul(sd, p.snout_len + p.snout_r * 0.3)), r: [p.snout_r * 0.5, p.snout_r * 0.4, p.snout_r * 0.3], mat: "dark", deco: 1 }); [[5, 6, 7], [8, 9, 10], [11, 12, 13], [14, 15, 16]].forEach(([a, b, c], li) => { const r = p.leg_r * (li < 2 ? 1.0 : 1.15); P.push({ t: "cap", a: j[a], b: j[b], r1: r * 1.3, r2: r, mat: "base", pat: 1 }); P.push({ t: "cap", a: j[b], b: j[c], r1: r, r2: r * 0.8, mat: "legs" }); P.push({ t: "ell", c: V.add(j[c], [0, -footY * 0.3, r * 0.4]), r: [r * 1.0, r * 0.6, r * 1.3], mat: "dark" }); }); const tip = V.add(j[17], V.mul(td, p.tail_len)); P.push({ t: "cap", a: j[17], b: j[18], r1: p.tail_r, r2: p.tail_r * 0.85, mat: "base", pat: 1 }); P.push({ t: "cap", a: j[18], b: tip, r1: p.tail_r * 0.85, r2: p.tail_r * 0.4, mat: "accent2" }); const et = Math.floor(p.ear_type), es = p.ear_size; [1, -1].forEach(s => { const base = V.add(j[4], [s * hr * 0.55, hr * 0.6, -hr * 0.1]); if (et === 1) P.push({ t: "cap", a: base, b: V.add(base, [s * es * 0.3, es * 1.2, -es * 0.2]), r1: es * 0.45, r2: es * 0.05, mat: "base", deco: 1 }); else if (et === 2) P.push({ t: "ell", c: V.add(j[4], [s * (hr * 0.85 + es * 0.15), -hr * 0.1 - es * 0.3, -hr * 0.15]), r: [Math.max(es * 0.18, 0.02), es * 0.95, es * 0.75], R: rotZ(s * 0.25), mat: "accent2" }); else if (et === 3) P.push({ t: "cap", a: base, b: V.add(base, [s * es * 0.3, es * 3.2, -es * 0.5]), r1: es * 0.4, r2: es * 0.3, mat: "base" }); else P.push({ t: "ell", c: base, r: [es * 0.4, es * 0.4, es * 0.25], mat: "base" }); }); const ht = Math.floor(p.horn_type), hl = p.horn_len; if (ht > 0) [1, -1].forEach(s => { const b0 = V.add(j[4], [s * hr * 0.4, hr * 0.75, 0]); if (ht === 1) P.push({ t: "cap", a: b0, b: V.add(b0, [s * hl * 0.2, hl, -hl * 0.3]), r1: hr * 0.13, r2: 0.006, mat: "horn", deco: 1 }); else if (ht === 2) { const m = V.add(b0, [s * hl * 0.4, hl * 0.8, -hl * 0.3]); P.push({ t: "cap", a: b0, b: m, r1: 0.02, r2: 0.014, mat: "horn", deco: 1 }); P.push({ t: "cap", a: m, b: V.add(m, [s * hl * 0.5, hl * 0.5, hl * 0.2]), r1: 0.014, r2: 0.006, mat: "horn", deco: 1 }); P.push({ t: "cap", a: V.lerp(b0, m, 0.5), b: V.add(V.lerp(b0, m, 0.5), [0, hl * 0.4, hl * 0.35]), r1: 0.013, r2: 0.005, mat: "horn", deco: 1 }); P.push({ t: "cap", a: m, b: V.add(m, [-s * hl * 0.1, hl * 0.6, -hl * 0.2]), r1: 0.012, r2: 0.005, mat: "horn", deco: 1 }); } else { const m = V.add(b0, [s * hl * 0.7, hl * 0.1, 0]); P.push({ t: "cap", a: b0, b: m, r1: hr * 0.14, r2: hr * 0.1, mat: "horn", deco: 1 }); P.push({ t: "cap", a: m, b: V.add(m, [s * hl * 0.2, hl * 0.5, -hl * 0.1]), r1: hr * 0.1, r2: 0.006, mat: "horn", deco: 1 }); } }); const eyes = [1, -1].map(s => ({ c: V.add(j[4], V.mul(V.norm([s * 0.55, 0.3, 0.75]), hr * 0.92)), r: hr * 0.16 })); const ext = { 4: V.add(s0, V.mul(sd, p.snout_len)), 7: V.add(j[7], [0, 0, p.leg_r]), 10: V.add(j[10], [0, 0, p.leg_r]), 13: V.add(j[13], [0, 0, p.leg_r]), 16: V.add(j[16], [0, 0, p.leg_r]), 18: tip }; return { joints: j, prims: P, ext, eyes, unit: p.leg_len + bh * 0.3, k: Math.min(0.06, 0.25 * bh) }; } function buildBird(p) { const j = []; const br = p.body_r, H = p.leg_len + br * 0.75; const d = [0, Math.sin(p.up), Math.cos(p.up)]; j[0] = [0, H, 0]; j[1] = V.add(j[0], V.mul(d, p.body_len * 0.45)); j[2] = V.add(j[1], V.add(V.mul(d, br * 0.4), [0, br * 0.35, 0])); j[3] = V.add(j[2], [0, p.neck_len + p.head * 0.4, p.neck_len * 0.25]); const wy = j[1][1] + br * 0.35, wz = j[1][2] - br * 0.1; [[1, 4, 5, 6], [-1, 7, 8, 9]].forEach(([s, a, b, c]) => { j[a] = [s * br * 0.75, wy, wz]; j[b] = [s * (br * 0.75 + p.wing_len * 0.4), wy, wz]; j[c] = [s * (br * 0.75 + p.wing_len * 0.75), wy, wz]; }); [[1, 10, 11, 12], [-1, 13, 14, 15]].forEach(([s, a, b, c]) => { j[a] = [s * br * 0.35, H - br * 0.45, 0]; j[b] = [s * br * 0.35, (H - br * 0.45) * 0.5, 0.0]; j[c] = [s * br * 0.35, 0.01, 0.01]; }); j[16] = V.sub(j[0], V.mul(d, p.body_len * 0.45)); const P = []; const bc = V.lerp(j[0], j[1], 0.4); P.push({ t: "ell", c: bc, r: [br, br * 0.95, p.body_len * 0.55 + br * 0.4], R: rotX(-p.up), mat: "base", pat: 1 }); P.push({ t: "ell", c: V.add(bc, V.mul(d, br * 0.3)), r: [br * 0.8, br * 0.8, br * 0.8], mat: "belly" }); P.push({ t: "cap", a: j[2], b: j[3], r1: Math.max(br * 0.4, p.head * 0.7), r2: p.head * 0.75, mat: "base" }); P.push({ t: "ell", c: j[3], r: [p.head, p.head, p.head * 1.05], mat: "head" }); const bd = V.norm([0, -Math.sin(p.beak_droop), Math.cos(p.beak_droop)]); const b0 = V.add(j[3], [0, -p.head * 0.1, p.head * 0.85]); P.push({ t: "cap", a: b0, b: V.add(b0, V.mul(bd, p.beak_len)), r1: p.beak_w, r2: p.beak_w * 0.15, mat: "beak" }); if (p.crest > 0.02) P.push({ t: "cap", a: V.add(j[3], [0, p.head * 0.8, 0]), b: V.add(j[3], [0, p.head + p.crest * 1.5, -p.crest]), r1: p.head * 0.25, r2: 0.005, mat: "accent", deco: 1 }); [[4, 5, 6], [7, 8, 9]].forEach(([a, b, c]) => { const s = Math.sign(j[a][0]); const tip = V.add(j[c], [s * p.wing_len * 0.25, 0, -p.wing_w * 0.4]); P.push({ t: "ell", c: V.lerp(j[a], j[b], 0.5), r: [p.wing_len * 0.24, 0.022 + br * 0.08, p.wing_w], mat: "wing", pat: 1 }); P.push({ t: "ell", c: V.lerp(j[b], j[c], 0.5), r: [p.wing_len * 0.2, 0.018 + br * 0.05, p.wing_w * 0.85], mat: "wing", pat: 1 }); P.push({ t: "ell", c: V.lerp(j[c], tip, 0.4), r: [p.wing_len * 0.16, 0.014 + br * 0.03, p.wing_w * 0.6], mat: "accent" }); }); [[10, 11, 12], [13, 14, 15]].forEach(([a, b, c]) => { P.push({ t: "cap", a: j[a], b: j[b], r1: p.leg_r * 2.2, r2: p.leg_r * 1.2, mat: "base" }); P.push({ t: "cap", a: j[b], b: j[c], r1: p.leg_r, r2: p.leg_r, mat: "beak" }); [-0.6, 0, 0.6].forEach(a2 => P.push({ t: "cap", a: j[c], b: V.add(j[c], [Math.sin(a2) * p.leg_r * 6, 0, Math.cos(a2) * p.leg_r * 6]), r1: p.leg_r * 0.8, r2: p.leg_r * 0.5, mat: "beak", deco: 1 })); }); const tt = V.sub(j[16], V.mul(d, p.tail_len)); P.push({ t: "ell", c: V.lerp(j[16], tt, 0.55), r: [p.tail_w, 0.02 + br * 0.05, p.tail_len * 0.6], R: rotX(-p.up * 0.5), mat: "accent" }); const eyes = [1, -1].map(s => ({ c: V.add(j[3], V.mul(V.norm([s * 0.75, 0.25, 0.55]), p.head * 0.97)), r: p.head * 0.2 })); const ext = { 3: V.add(b0, V.mul(bd, p.beak_len)), 6: V.add(j[6], [Math.sign(j[6][0]) * p.wing_len * 0.25, 0, 0]), 9: V.add(j[9], [Math.sign(j[9][0]) * p.wing_len * 0.25, 0, 0]), 12: V.add(j[12], [0, 0, p.leg_r * 6]), 15: V.add(j[15], [0, 0, p.leg_r * 6]), 16: tt }; return { joints: j, prims: P, ext, eyes, unit: Math.max(p.leg_len, br), k: Math.min(0.04, br * 0.25) }; } function buildFish(p) { const L = p.length, h = L * p.height * 0.5, w = L * p.width * 0.5, y = h + 0.02; const j = [[0, y, 0], [0, y, L * 0.22], [0, y, L * 0.38], [0, y, -L * 0.15], [0, y, -L * 0.3], [0, y, -L * 0.42], [w * 0.9, y - h * 0.3, L * 0.12], [-w * 0.9, y - h * 0.3, L * 0.12]]; const P = [], rnd = 1 + p.round * 0.8; P.push({ t: "ell", c: [0, y, L * 0.05], r: [w * rnd, h * rnd, L * 0.3], mat: "base", pat: 1 }); P.push({ t: "ell", c: [0, y - h * 0.25, L * 0.05], r: [w * 0.85 * rnd, h * 0.7 * rnd, L * 0.27], mat: "belly" }); P.push({ t: "ell", c: [0, y, L * 0.3], r: [w * 0.85, h * 0.8, L * (0.12 + p.head * 0.3) * (1 + p.pointy * 0.6)], mat: "base" }); P.push({ t: "cap", a: [0, y, -L * 0.1], b: [0, y, -L * 0.44], r1: Math.min(w, h) * 0.85, r2: Math.min(w, h) * 0.4, mat: "base", pat: 1 }); const tf = L * p.tail_fin; const ft = Math.max(w * 0.22, L * 0.014); P.push({ t: "ell", c: [0, y + tf * 0.3, -L * 0.43 - tf * 0.18], r: [ft, tf * 0.5, tf * 0.28], R: rotX(0.7), mat: "fin", deco: 1 }); P.push({ t: "ell", c: [0, y - tf * 0.3, -L * 0.43 - tf * 0.18], r: [ft, tf * 0.5, tf * 0.28], R: rotX(-0.7), mat: "fin", deco: 1 }); if (p.dorsal > 0.03) P.push({ t: "ell", c: [0, y + h * 0.85, -L * 0.02], r: [ft * 0.8, L * p.dorsal * 0.35, L * 0.12], R: rotX(0.5), mat: "fin", deco: 1 }); [1, -1].forEach(s => P.push({ t: "ell", c: [s * w * 1.05, y - h * 0.35, L * 0.08], r: [L * p.pec * 0.5, ft * 0.7, L * p.pec * 0.35], R: rotZ(-s * 0.4), mat: "fin", deco: 1 })); const eyes = [1, -1].map(s => ({ c: [s * w * 0.8, y + h * 0.2, L * 0.33], r: Math.max(0.006, h * 0.18) })); const ext = { 2: [0, y, L * 0.5 * (1 + p.pointy * 0.1)], 5: [0, y, -L * 0.55 - tf * 0.4], 6: [w * 1.05 + L * p.pec * 0.5, y - h * 0.4, L * 0.05], 7: [-w * 1.05 - L * p.pec * 0.5, y - h * 0.4, L * 0.05] }; return { joints: j, prims: P, ext, eyes, unit: L * 0.5, k: Math.min(0.05, h * 0.3) }; } function buildSnake(p) { const L = p.length, t = p.thick, j = [], P = []; for (let i = 0; i < 10; i++) { const z = L * 0.5 - i * L / 10.5, x = Math.sin(i * 0.9) * L * 0.035; j.push([x, t, z]); } const rad = (i) => t * (1 - p.taper * Math.max(0, (i - 4) / 6)); P.push({ t: "ell", c: V.add(j[0], [0, t * 0.15, t * 0.6]), r: [t * p.head * 0.85, t * p.head * 0.6, t * p.head * 1.2], mat: "head" }); for (let i = 0; i < 9; i++) P.push({ t: "cap", a: j[i], b: j[i + 1], r1: rad(i), r2: rad(i + 1), mat: i === 0 ? "head" : "base", pat: 1 }); const tip = V.add(j[9], [0, -t * 0.3, -L * 0.08]); P.push({ t: "cap", a: j[9], b: tip, r1: rad(9), r2: 0.003, mat: "base", pat: 1 }); const eyes = [1, -1].map(s => ({ c: V.add(j[0], [s * t * p.head * 0.6, t * 0.45, t * 1.1]), r: t * 0.28 })); const ext = { 0: V.add(j[0], [0, 0, t * p.head * 1.6]), 9: tip }; return { joints: j, prims: P, ext, eyes, unit: L * 0.1, k: t * 0.4 }; } function buildInsect(p) { const S = 1, tr = p.thorax_r * 0.4 * S, hr = p.head_r * 0.4 * S, al = p.abd_len * 0.5, ar = p.abd_r * 0.4, ll = p.leg_len * 0.5, lh = ll * 0.45; const j = []; j[0] = [0, lh + tr * 0.4, 0]; j[1] = V.add(j[0], [0, tr * 0.2, tr + hr * 0.8]); j[2] = V.add(j[0], [0, tr * 0.15, -(tr + al * 0.45)]); const zs = [tr * 0.6, 0, -tr * 0.6], spread = [0.9, 0.0, -0.9]; let n = 3; for (let k = 0; k < 3; k++) for (const s of [1, -1]) { const hip = [s * tr * 0.7, j[0][1] - tr * 0.2, zs[k]]; const dirOut = V.norm([s * Math.cos(spread[k] * 0.6), 0, Math.sin(spread[k] * 0.6)]); const knee = V.add(hip, V.add(V.mul(dirOut, ll * 0.5), [0, ll * 0.35, 0])); const foot = V.add(V.add(hip, V.mul(dirOut, ll * 1.05)), [0, -hip[1] + 0.004, 0]); j[n] = hip; j[n + 1] = knee; j[n + 2] = foot; n += 3; } // reorder legs to L1,R1,L2,R2,L3,R3 already (loop order k then side) const P = []; P.push({ t: "ell", c: j[0], r: [tr, tr * 0.85, tr * 1.1], mat: "base" }); P.push({ t: "ell", c: j[1], r: [hr, hr * 0.85, hr * 0.9], mat: "head" }); const shell = p.shell > 0.6; P.push({ t: "ell", c: j[2], r: [ar * (shell ? 1.25 : 1), ar * (shell ? 0.95 : 0.85), al * 0.55], mat: "base", pat: 1 }); if (shell) P.push({ t: "cap", a: V.add(j[2], [0, ar * 0.85, al * 0.45]), b: V.add(j[2], [0, ar * 0.85, -al * 0.5]), r1: 0.002, r2: 0.002, mat: "dark" }); for (let L = 3; L < 21; L += 3) { P.push({ t: "cap", a: j[L], b: j[L + 1], r1: p.leg_r * 0.9, r2: p.leg_r * 0.75, mat: "legs" }); P.push({ t: "cap", a: j[L + 1], b: j[L + 2], r1: p.leg_r * 0.75, r2: p.leg_r * 0.5, mat: "legs" }); } if (p.antenna > 0.05) [1, -1].forEach(s => { const a0 = V.add(j[1], [s * hr * 0.4, hr * 0.6, hr * 0.5]); P.push({ t: "cap", a: a0, b: V.add(a0, [s * p.antenna * 0.15, p.antenna * 0.3, p.antenna * 0.25]), r1: 0.012, r2: 0.008, mat: "legs", deco: 1 }); }); if (p.mandible > 0.03) [1, -1].forEach(s => { const m0 = V.add(j[1], [s * hr * 0.4, -hr * 0.3, hr * 0.8]); P.push({ t: "cap", a: m0, b: V.add(m0, [-s * p.mandible * 0.1, 0, p.mandible * 0.25]), r1: 0.014, r2: 0.006, mat: "dark", deco: 1 }); }); if (p.wings > 0.15) [1, -1].forEach(s => P.push({ t: "ell", c: V.add(j[0], [s * p.wings * 0.12, tr * 0.9, -p.wings * 0.12]), r: [p.wings * 0.1, 0.012, p.wings * 0.22], R: rotY(s * 0.4), mat: "wing", hard: 1, deco: 1 })); const eyes = [1, -1].map(s => ({ c: V.add(j[1], V.mul(V.norm([s * 0.6, 0.3, 0.7]), hr * 0.95)), r: hr * 0.28 })); const ext = { 1: V.add(j[1], [0, 0, hr]), 2: V.add(j[2], [0, 0, -al * 0.55]) }; const sc = p.size; return { joints: j, prims: P, ext, eyes, unit: ll, k: Math.min(tr, ar) * 0.35, size: sc }; } const BUILDERS = { biped: buildBiped, quadruped: buildQuadruped, bird: buildBird, fish: buildFish, snake: buildSnake, insect: buildInsect }; function scaleBuild(B, s) { const sp = (v) => V.mul(v, s); B.joints = B.joints.map(sp); for (const k in B.ext) B.ext[k] = sp(B.ext[k]); B.eyes = B.eyes.map(e => ({ c: sp(e.c), r: e.r * s })); B.prims = B.prims.map(p => { const q = Object.assign({}, p); if (p.t === "cap" || p.t === "cone") { q.a = sp(p.a); q.b = sp(p.b); q.r1 = p.r1 * s; q.r2 = p.r2 * s; } else if (p.t === "ell") { q.c = sp(p.c); q.r = sp(p.r); } else { q.c = sp(p.c); q.h = sp(p.h); q.rr = (p.rr || 0) * s; } return q; }); B.unit *= s; B.k *= s; return B; } // skin "segments": each joint owns the bones it moves function segments(fam, J, ext) { const par = FAMILIES[fam].parent, segs = []; const kids = J.map((_, i) => par.map((p, c) => p === i ? c : -1).filter(c => c >= 0)); for (let i = 0; i < J.length; i++) { for (const c of kids[i]) segs.push([i, J[i], J[c]]); if (ext[i]) segs.push([i, J[i], ext[i]]); if (!kids[i].length && !ext[i]) segs.push([i, J[i], J[i]]); } return segs; } function segDist(p, a, b) { const ab = V.sub(b, a), ap = V.sub(p, a); const t = Math.min(1, Math.max(0, V.dot(ap, ab) / (V.dot(ab, ab) + 1e-12))); return V.len(V.sub(ap, V.mul(ab, t))); } function gtWeights(fam, J, ext, pts, scale) { const segs = segments(fam, J, ext), nJ = J.length, sharp = 0.03 * scale / 1.7; return pts.map(p => { const D = new Array(nJ).fill(1e9); for (const [o, a, b] of segs) D[o] = Math.min(D[o], segDist(p, a, b)); const mn = Math.min(...D); let W = D.map(d => Math.exp(-(d - mn) / sharp)).map(w => w < 0.05 ? 0 : w); const s = W.reduce((a, b) => a + b, 0); return W.map(w => w / s); }); } // area-weighted surface samples function samplePoints(M, n, rng) { const A = [], cum = []; let tot = 0; for (const f of M.faces) { const a = M.verts[f[0]], b = M.verts[f[1]], c = M.verts[f[2]]; const ab = V.sub(b, a), ac = V.sub(c, a); const ar = 0.5 * V.len([ab[1] * ac[2] - ab[2] * ac[1], ab[2] * ac[0] - ab[0] * ac[2], ab[0] * ac[1] - ab[1] * ac[0]]); tot += ar; cum.push(tot); } const out = []; for (let i = 0; i < n; i++) { const r = rng() * tot; let lo = 0, hi = cum.length - 1; while (lo < hi) { const m = (lo + hi) >> 1; if (cum[m] < r) lo = m + 1; else hi = m; } const f = M.faces[lo]; let u = rng(), v = rng(); if (u + v > 1) { u = 1 - u; v = 1 - v; } const a = M.verts[f[0]], b = M.verts[f[1]], c = M.verts[f[2]]; out.push(V.add(a, V.add(V.mul(V.sub(b, a), u), V.mul(V.sub(c, a), v)))); } return out; } function normalizeFrame(pts) { let lo = [1e9, 1e9, 1e9], hi = [-1e9, -1e9, -1e9]; for (const p of pts) for (let i = 0; i < 3; i++) { lo[i] = Math.min(lo[i], p[i]); hi[i] = Math.max(hi[i], p[i]); } const c = [(lo[0] + hi[0]) / 2, lo[1], (lo[2] + hi[2]) / 2]; const s = Math.max(hi[0] - lo[0], hi[1] - lo[1], hi[2] - lo[2]); return { c, s }; } const DEFAULT_MATS = { base: "#7aa65a", belly: "#c9d6a0", accent: "#4d6b3a", accent2: "#5e7f45", skin: "#7aa65a", legs: "#6d9550", dark: "#2c2a30", horn: "#efe6cf", beak: "#e8a93a", wing: "#6d8fb0", head: "#7aa65a", fin: "#8fb8d8", eye: "#16151b", eyeW: "#f6f3ea" }; // Build a creature from a description: {family, genome?, species?, colors?, pattern?, size?, seed} function makeCreature(desc, res) { const seed = desc.seed !== undefined ? desc.seed : hashStr(JSON.stringify(desc)); const rng = RNG(seed); const fam = desc.family; const sp = desc.species && root.RigSpecies && root.RigSpecies.SPECIES[desc.species]; const base = Object.assign({}, (sp || desc.genome) ? NEUTRAL[fam] : {}, sp ? sp.g : {}, desc.genome || {}); const g = sampleGenome(fam, rng, base); if (desc.genome) Object.assign(g, desc.genome); let B = BUILDERS[fam](g); // species carry their real-world size; desc.size is a multiplier on it const size = (desc.size || 1) * ((sp && sp.size) || (fam === "insect" ? g.size : 1)); B = scaleBuild(B, size * (1 + (desc.vary === false ? 0 : rng.n() * 0.05))); const mats = Object.assign({}, DEFAULT_MATS); const pal = Object.assign({}, sp ? sp.c : {}, desc.colors || {}); for (const k in pal) mats[k] = pal[k]; if (!pal.skin) mats.skin = mats.base; if (!pal.head) mats.head = mats.base; if (!pal.legs) mats.legs = mats.base; if (!pal.accent) { const h = rgb2hsl(col(mats.base)); h[2] = Math.min(0.92, h[2] + 0.28); h[1] *= 0.6; mats.accent = hsl2rgb(h); } if (!pal.accent2) mats.accent2 = mats.base; if (!pal.wing) mats.wing = mats.base; const jr = RNG(seed + 99), jseed = jr.i(1e9); for (const k in mats) mats[k] = jitterColor(col(mats[k]), RNG(jseed), desc.vary === false ? 0 : 1); const pattern = desc.pattern || (sp && sp.p) || { type: "none" }; const sc = new Sculpt(B.prims, B.k); let M = mesh(sc, res || 50, res && res < 40 ? 46 : 76); if (M.faces.length < 200) M = mesh(sc, 90, 160); // very thin creature: retry finer const colors = desc.noPaint ? null : paint(sc, M, mats, B.eyes, pattern.type ? { type: pattern.type, scale: (pattern.scale || 0.12) * size } : pattern, seed); return { family: fam, genome: g, joints: B.joints, parent: FAMILIES[fam].parent, ext: B.ext, unit: B.unit, verts: M.verts, faces: M.faces, normals: M.normals, colors, sculpt: sc, seed, height: Math.max(...M.verts.map(v => v[1])) }; } root.RigSculpt = { V, RNG, hashStr, noise3, rotX, rotY, rotZ, mm, mv, Sculpt, mesh, paint, FAMILIES, FAMILY_NAMES, RANGES, sampleGenome, BUILDERS, scaleBuild, segments, gtWeights, samplePoints, normalizeFrame, makeCreature, col, jitterColor, DEFAULT_MATS, primSDF }; })(typeof globalThis !== "undefined" ? globalThis : this);