WIREFRAME SCORPION SOURCE CODE
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Aether — Scorpion</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { background: #000; overflow: hidden; height: 100vh; cursor: grab; touch-action: none; }
body.dragging { cursor: grabbing; }
#bgCanvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 1; }
#animationCanvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 2; }
</style>
</head>
<body>
<canvas id="bgCanvas"></canvas>
<canvas id="animationCanvas"></canvas>
<script>
const colors = {
primary: 0x4db8d4,
mid: 0x2a7f99,
dark1: 0x0d2a3a,
dark3: 0x000000,
light: 0xaee8f5
};
// ── THEME SYNC ──
function syncTheme() {
try {
let isDark = true;
if (window.parent && window.parent.document) {
isDark = window.parent.document.documentElement.classList.contains('dark');
}
if (!isDark) {
document.body.style.backgroundColor = '#ffffff';
colors.dark3 = 0xffffff;
colors.dark1 = 0xcccccc;
if (typeof scene !== 'undefined' && scene) {
scene.background = new THREE.Color(colors.dark3);
}
} else {
document.body.style.backgroundColor = '#000000';
colors.dark3 = 0x000000;
colors.dark1 = 0x0d2a3a;
if (typeof scene !== 'undefined' && scene) {
scene.background = new THREE.Color(colors.dark3);
}
}
} catch (e) {}
}
syncTheme();
if (window.parent && window.parent.document) {
try {
var observer = new MutationObserver(syncTheme);
observer.observe(window.parent.document.documentElement, { attributes: true, attributeFilter: ['class'] });
} catch(e) {}
}
// ── Background ──
const bgCanvas = document.getElementById('bgCanvas');
const bgCtx = bgCanvas.getContext('2d');
const pixelSize = 8;
let cols, rows, pixels = [];
const RAMP_STEPS = 48;
const ramp = Array.from({ length: RAMP_STEPS + 1 }, (_, i) => {
const b = (i / RAMP_STEPS) * 0.4;
return `rgb(${Math.floor(b*10)},${Math.floor(b*30)},${Math.floor(b*50)})`;
});
function initBackground() {
bgCanvas.width = window.innerWidth;
bgCanvas.height = window.innerHeight;
cols = Math.ceil(bgCanvas.width / pixelSize);
rows = Math.ceil(bgCanvas.height / pixelSize);
pixels = [];
for (let i = 0; i < cols * rows; i++)
pixels.push({ brightness: Math.random() * 0.3, speed: 0.001 + Math.random() * 0.003 });
}
let bgFrame = 0;
function animateBackground() {
if ((bgFrame++ & 1) === 0) {
for (let y = 0; y < rows; y++) {
for (let x = 0; x < cols; x++) {
const p = pixels[y * cols + x];
p.brightness += p.speed * 2;
if (p.brightness > 0.4) p.brightness = 0;
bgCtx.fillStyle = ramp[Math.min(RAMP_STEPS, (p.brightness * RAMP_STEPS / 0.4) | 0)];
bgCtx.fillRect(x * pixelSize, y * pixelSize, pixelSize, pixelSize);
}
}
}
requestAnimationFrame(animateBackground);
}
// ── Three.js ──
const renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('animationCanvas'), antialias: true, alpha: true });
let scene, camera, scorpion, body;
const legs = [], arms = [], tail = [], chelicerae = [], pectines = [];
let telsonJoint, venomMat, eyeMat;
// Anatomy frames (body-local)
const C_PRO = new THREE.Vector3(0, 0.1, 0.95);
const R_PRO = new THREE.Vector3(0.48, 0.2, 0.56);
const GROUND_Y = -0.3;
// Seeded RNG so every granule, hair and pebble is the same on every load
function mulberry32(a) {
return function () {
a |= 0; a = a + 0x6D2B79F5 | 0;
let t = Math.imul(a ^ a >>> 15, 1 | a);
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
const rand = mulberry32(20261128);
const V = (x, y, z) => new THREE.Vector3(x, y, z);
const smooth = (a, b, x) => { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); };
// ── Materials ── wireframe basic materials ignore lights, so share per colour
const matCache = new Map();
function mat(color) {
if (!matCache.has(color))
matCache.set(color, new THREE.MeshBasicMaterial({ color, wireframe: true }));
return matCache.get(color);
}
const lineCache = new Map();
function lmat(color, opacity = 1) {
const key = color + ':' + opacity;
if (!lineCache.has(key))
lineCache.set(key, new THREE.LineBasicMaterial({ color, transparent: opacity < 1, opacity }));
return lineCache.get(key);
}
const pointCache = new Map();
function pmat(color, size, opacity) {
const key = color + ':' + size + ':' + opacity;
if (!pointCache.has(key))
pointCache.set(key, new THREE.PointsMaterial({ color, size, transparent: true, opacity }));
return pointCache.get(key);
}
// ── Geometry helpers ──
function ball(r, color, x, y, z, sx = 1, sy = 1, sz = 1, ws = 16, hs = 12) {
const m = new THREE.Mesh(new THREE.SphereGeometry(r, ws, hs), mat(color));
m.position.set(x, y, z);
m.scale.set(sx, sy, sz);
return m;
}
function lineSegs(arr, material) {
const g = new THREE.BufferGeometry();
g.setAttribute('position', new THREE.Float32BufferAttribute(arr, 3));
return new THREE.LineSegments(g, material);
}
function polyline(points, material, loop = false) {
const g = new THREE.BufferGeometry().setFromPoints(points);
return new (loop ? THREE.LineLoop : THREE.Line)(g, material);
}
function points(arr, material) {
const g = new THREE.BufferGeometry();
g.setAttribute('position', new THREE.Float32BufferAttribute(arr, 3));
return new THREE.Points(g, material);
}
function qb(a, m, b, u) { const v = 1 - u; return v * v * a + 2 * u * v * m + u * u * b; }
// Lathed limb segment along local +X with a tapered, bulging profile
function limb(len, r0, rm, r1, color, radial = 8) {
const pts = [];
for (let i = 0; i <= 8; i++) {
const u = i / 8;
pts.push(new THREE.Vector2(Math.max(qb(r0, rm, r1, u), 0.0015), u * len));
}
const geo = new THREE.LatheGeometry(pts, radial);
geo.rotateZ(-Math.PI / 2);
return new THREE.Mesh(geo, mat(color));
}
// Hairs / spines / teeth on a limb segment (local +X axis, +Y dorsal)
function limbHairs(len, r0, rm, r1, o) {
const pos = [];
for (let i = 0; i < o.count; i++) {
const u = o.ordered ? 0.1 + (i / Math.max(1, o.count - 1)) * 0.8 : 0.04 + rand() * 0.92;
const r = qb(r0, rm, r1, u) * (o.sy && Math.abs(Math.cos(0)) ? 1 : 1);
let phi;
if (o.ordered) phi = o.arc[0] + (o.alt === false ? 0 : (i % 2 ? 0.5 : -0.5) * o.arc[1]);
else phi = o.arc ? o.arc[0] + (rand() - 0.5) * o.arc[1] : rand() * Math.PI * 2;
const cy = Math.cos(phi) * (o.sy || 1), cz = Math.sin(phi);
const x = u * len, y = cy * r, z = cz * r;
const L = o.l0 + rand() * (o.l1 - o.l0);
const d = V(o.distal + (rand() - 0.5) * 0.3, Math.cos(phi), cz).normalize();
pos.push(x, y, z, x + d.x * L, y + d.y * L, z + d.z * L);
}
return lineSegs(pos, lmat(o.color, o.opacity));
}
// Carinae: keel ridges running along a segment, studded with granules
function carinae(d, phis, granules) {
const seg = [], gr = [], sy = d.sy || 1;
phis.forEach(phi => {
let prev = null;
for (let k = 0; k <= 10; k++) {
const u = 0.06 + k / 10 * 0.88, r = qb(d.r0, d.rm, d.r1, u) * 1.04;
const p = [u * d.len, Math.cos(phi) * r * sy, Math.sin(phi) * r];
if (prev) seg.push(...prev, ...p);
prev = p;
if (granules && k % 2 === 0) gr.push(p[0], p[1] * 1.03, p[2] * 1.03);
}
});
const g = new THREE.Group();
g.add(lineSegs(seg, lmat(colors.light, 0.7)));
if (granules) g.add(points(gr, pmat(colors.light, 0.02, 0.9)));
return g;
}
function ring(radius, color, tubeR = 0.12) {
const geo = new THREE.TorusGeometry(radius, radius * tubeR, 3, 16);
geo.rotateY(Math.PI / 2);
return new THREE.Mesh(geo, mat(color));
}
function surfaceRing(r, color, p, n) {
const m = new THREE.Mesh(new THREE.TorusGeometry(r, r * 0.18, 3, 14), mat(color));
m.position.copy(p);
m.quaternion.setFromUnitVectors(V(0, 0, 1), n);
return m;
}
// Ellipsoid helpers (local to the ellipsoid centre)
function ellY(R, x, z) {
const k = 1 - (x / R.x) ** 2 - (z / R.z) ** 2;
return k > 0 ? R.y * Math.sqrt(k) : null;
}
function ellNormal(R, p) {
return V(p.x / (R.x * R.x), p.y / (R.y * R.y), p.z / (R.z * R.z)).normalize();
}
function surfLine(R, xz, color, opacity, loop = false, below = false) {
const pts = [];
for (const [x, z] of xz) {
const y = ellY(R, x, z);
if (y === null) continue;
pts.push(V(x, below ? -y : y, z).multiplyScalar(1.015));
}
return polyline(pts, lmat(color, opacity), loop);
}
// Granules scattered over the top of an ellipsoid
function granuleField(R, count, size) {
const arr = [];
for (let i = 0; i < count; i++) {
const x = (rand() * 2 - 1) * R.x * 0.92, z = (rand() * 2 - 1) * R.z * 0.92;
const y = ellY(R, x, z);
if (y === null || y < R.y * 0.35) { i--; continue; }
arr.push(x * 1.01, y * 1.03, z * 1.01);
}
return points(arr, pmat(colors.light, size, 0.75));
}
function ellipsoid(R, color, ws, hs) {
const m = new THREE.Mesh(new THREE.SphereGeometry(1, ws, hs).rotateX(Math.PI / 2), mat(color));
m.scale.copy(R);
return m;
}
// ── Articulated chains ──
// Each joint is a Group; its segment extends along local +X, the next
// joint sits at its tip; bending is rotation.z, turning is rotation.y.
function knuckle(r) {
return new THREE.Mesh(new THREE.SphereGeometry(r, 8, 6), mat(colors.light));
}
function buildChain(defs) {
const root = new THREE.Group();
const joints = [], meshes = [];
let parent = root;
defs.forEach((d, i) => {
const j = new THREE.Group();
if (i > 0) j.position.x = defs[i - 1].len;
j.rotation.z = d.angle;
if (d.yaw) j.rotation.y = d.yaw;
parent.add(j);
joints.push(j);
const m = limb(d.len, d.r0, d.rm, d.r1, d.color, d.radial || 8);
if (d.sy) m.scale.y = d.sy;
j.add(m); meshes.push(m);
if (d.knuckle !== false) j.add(knuckle(d.r0 * 1.12));
if (d.hairs) j.add(limbHairs(d.len, d.r0, d.rm, d.r1, {
count: d.hairs, sy: d.sy, l0: d.r0 * 0.8, l1: d.r0 * 1.8, distal: 1.3,
color: colors.primary, opacity: 0.42 }));
if (d.spines) j.add(limbHairs(d.len, d.r0, d.rm, d.r1, {
count: d.spines, ordered: true, arc: [Math.PI, 1.5], sy: d.sy,
l0: d.r0 * 1.4, l1: d.r0 * 2, distal: 1.8, color: colors.light, opacity: 0.95 }));
if (d.tricho) j.add(limbHairs(d.len, d.r0, d.rm, d.r1, {
count: d.tricho, arc: [0, 2.4], sy: d.sy, l0: 0.09, l1: 0.15, distal: 0.2,
color: colors.light, opacity: 0.3 }));
if (d.carinae) j.add(carinae(d, d.carinae, d.granules));
if (d.bands) d.bands.forEach(u => {
const b = ring(qb(d.r0, d.rm, d.r1, u) * 1.12, colors.light);
b.position.x = u * d.len;
j.add(b);
});
if (d.extra) d.extra(j, d);
parent = j;
});
return { root, joints, meshes };
}
function yawFor(side, fa) {
return side > 0 ? fa - Math.PI / 2 : -(Math.PI / 2 + fa);
}
// Foot height & reach for a joint-angle set, and one-joint IK on the femur
function footReach(ang, lens) {
let c = 0, y = 0, h = 0;
for (let i = 0; i < ang.length; i++) {
c += ang[i];
y += lens[i] * Math.sin(c);
h += lens[i] * Math.cos(c);
}
return [y, h];
}
function solveFemur(leg, targetY) {
const a = leg.ang;
let lo = -0.6, hi = 1.4;
for (let k = 0; k < 16; k++) {
a[2] = (lo + hi) * 0.5;
if (footReach(a, leg.lens)[0] < targetY) lo = a[2]; else hi = a[2];
}
a[2] = (lo + hi) * 0.5;
leg.h = footReach(a, leg.lens)[1];
}
// ═════════════════════════ GROUND ═════════════════════════
// Rippled sand that fades out at the edge, plus rocks, pebbles and grains
function buildGround() {
const g = new THREE.Group();
g.position.y = GROUND_Y;
const R = 3.4, pos = [], col = [];
const fg = new THREE.Color(colors.primary), bg = new THREE.Color(colors.dark3), c = new THREE.Color();
const fade = (x, z) => { c.copy(fg).lerp(bg, Math.min(1, Math.pow(Math.sqrt(x * x + z * z) / R, 1.6))); return [c.r, c.g, c.b]; };
const H = (x, z) => 0.012 * Math.sin(x * 3.1 + z * 1.4) + 0.008 * Math.sin(x * 1.3 - z * 2.2);
for (let z0 = -R; z0 <= R; z0 += 0.11) {
let prev = null;
for (let x = -R; x <= R + 0.001; x += 0.07) {
const z = z0 + 0.05 * Math.sin(x * 1.7 + z0 * 2.3);
if (x * x + z * z > R * R) { prev = null; continue; }
const p = [x, H(x, z), z];
if (prev) { pos.push(...prev, ...p); col.push(...fade(prev[0], prev[2]), ...fade(x, z)); }
prev = p;
}
}
const geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3));
geo.setAttribute('color', new THREE.Float32BufferAttribute(col, 3));
g.add(new THREE.LineSegments(geo, new THREE.LineBasicMaterial({ vertexColors: true, transparent: true, opacity: 0.35 })));
// Rocks and pebbles outside the scorpion's footprint
for (let i = 0; i < 34; i++) {
const big = i < 5;
const a = rand() * Math.PI * 2, r = (big ? 2.1 : 1.8) + rand() * (big ? 0.9 : 1.4);
const s = big ? 0.12 + rand() * 0.16 : 0.02 + rand() * 0.04;
const rock = new THREE.Mesh(new THREE.IcosahedronGeometry(s, big ? 1 : 0), mat(big ? colors.mid : colors.primary));
rock.position.set(Math.cos(a) * r, s * 0.25, Math.sin(a) * r);
rock.scale.set(1 + rand() * 0.5, 0.45 + rand() * 0.3, 1 + rand() * 0.4);
rock.rotation.y = rand() * 6;
g.add(rock);
}
const grains = [];
for (let i = 0; i < 500; i++) {
const a = rand() * Math.PI * 2, r = Math.sqrt(rand()) * R * 0.85;
grains.push(Math.cos(a) * r, 0.01, Math.sin(a) * r);
}
g.add(points(grains, pmat(colors.light, 0.014, 0.35)));
return g;
}
// ═════════════════════════ PROSOMA ═════════════════════════
function buildProsoma(g) {
const pro = new THREE.Group();
pro.position.copy(C_PRO);
g.add(pro);
pro.add(ellipsoid(R_PRO, colors.primary, 30, 20));
// Median furrow, superciliary keels, posterior & lateral carinae
const med = [], post = [];
for (let k = 0; k <= 16; k++) med.push([0, 0.45 - k * 0.06]);
pro.add(surfLine(R_PRO, med, colors.light, 0.6));
for (let k = 0; k <= 16; k++) { const u = -1 + 2 * k / 16; post.push([u * 0.38, -0.42 + Math.abs(u) * 0.06]); }
pro.add(surfLine(R_PRO, post, colors.light, 0.65));
[-1, 1].forEach(s => {
const sc = [], lat = [], diag = [];
for (let k = 0; k <= 12; k++) { const a = k / 12 * Math.PI; sc.push([s * (0.035 + 0.06 * Math.sin(a)), 0.32 - k * 0.018]); }
for (let k = 0; k <= 14; k++) lat.push([s * (0.3 + k * 0.006), 0.4 - k * 0.06]);
for (let k = 0; k <= 10; k++) diag.push([s * (0.06 + k * 0.022), 0.05 - k * 0.04]);
pro.add(surfLine(R_PRO, sc, colors.light, 0.85));
pro.add(surfLine(R_PRO, lat, colors.light, 0.45));
pro.add(surfLine(R_PRO, diag, colors.light, 0.55));
});
pro.add(granuleField(R_PRO, 160, 0.016));
// Ocular tubercle with the two median eyes
const ty = ellY(R_PRO, 0, 0.24);
pro.add(ball(0.07, colors.mid, 0, ty, 0.24, 1, 0.45, 1.2, 12, 8));
eyeMat = new THREE.MeshBasicMaterial({ color: colors.light, wireframe: true, transparent: true });
[-1, 1].forEach(s => {
const p = V(s * 0.04, ty + 0.03, 0.25);
const eye = new THREE.Mesh(new THREE.SphereGeometry(0.028, 12, 10), eyeMat);
eye.position.copy(p);
pro.add(eye);
pro.add(surfaceRing(0.034, colors.light, p, V(s * 0.8, 0.6, 0).normalize()));
});
// Lateral eyes: a row of three on each front corner
[-1, 1].forEach(s => {
for (let k = 0; k < 3; k++) {
const x = s * (0.26 + k * 0.035), z = 0.45 - k * 0.03;
const y = ellY(R_PRO, x, z);
if (y === null) continue;
const e = new THREE.Mesh(new THREE.SphereGeometry(0.016 - k * 0.002, 8, 6), eyeMat);
e.position.set(x, y * 1.02, z);
pro.add(e);
}
});
// Underside: sternum and genital operculum
g.add(ball(1, colors.mid, 0, -0.1, 0.78, 0.09, 0.025, 0.1, 10, 6));
g.add(ball(1, colors.mid, 0, -0.11, 0.5, 0.14, 0.025, 0.07, 12, 6));
}
function buildChelicerae(g) {
[-1, 1].forEach(s => {
const ch = buildChain([
{ len: .12, r0: .046, rm: .052, r1: .04, color: colors.mid, angle: -0.15, hairs: 10 },
{ len: .08, r0: .022, rm: .018, r1: .005, color: colors.light, angle: 0.15, radial: 6 },
]);
// Movable finger alongside the fixed one
const mv = buildChain([{ len: .085, r0: .018, rm: .016, r1: .004, color: colors.light, angle: -0.35, radial: 6 }]);
mv.root.position.set(0.12, -0.02, 0);
ch.joints[0].add(mv.root);
ch.root.position.set(s * 0.09, 0.03, 1.45);
ch.root.rotation.y = yawFor(s, 0.12);
g.add(ch.root);
chelicerae.push({ side: s, finger: mv.joints[0] });
});
}
// ═════════════════════════ PEDIPALPS ═════════════════════════
// trochanter · femur · patella · manus, then two toothed fingers.
function buildFingers(j, d, side, arm) {
const L = d.len;
const fixed = buildChain([
{ len: .22, r0: .036, rm: .032, r1: .022, color: colors.mid, angle: 0.1, carinae: [0.4, -0.4] },
{ len: .17, r0: .022, rm: .016, r1: .005, color: colors.light, angle: -0.3, radial: 6 },
]);
const mov = buildChain([
{ len: .22, r0: .032, rm: .03, r1: .02, color: colors.light, angle: -0.1, carinae: [2.7, -2.7] },
{ len: .16, r0: .02, rm: .015, r1: .005, color: colors.light, angle: 0.3, radial: 6 },
]);
// Denticle rows along the cutting edges
const teeth = (ch, arc) => ch.joints.forEach((jj, k) => {
const dd = k ? { len: .17, r0: .022, rm: .016, r1: .006 } : { len: .22, r0: .036, rm: .032, r1: .022 };
jj.add(limbHairs(dd.len, dd.r0, dd.rm, dd.r1, {
count: k ? 7 : 10, ordered: true, alt: false, arc: [arc, 0], l0: 0.016, l1: 0.024, distal: 0.5,
color: colors.light, opacity: 0.95 }));
});
teeth(fixed, Math.PI);
teeth(mov, 0);
fixed.root.position.set(L * 0.97, 0.03, side * 0.01);
mov.root.position.set(L * 0.92, -0.035, -side * 0.01);
j.add(fixed.root);
j.add(mov.root);
arm.fixed = fixed.joints[0];
arm.movable = mov.joints[0];
}
function buildArms(g) {
[-1, 1].forEach(s => {
const arm = { side: s };
const defs = [
{ len: .10, r0: .045, rm: .05, r1: .045, color: colors.mid, angle: .10 },
{ len: .42, r0: .045, rm: .054, r1: .046, color: colors.primary, angle: .28,
carinae: [0.5, -0.5, 2.4, -2.4], granules: true, tricho: 3, hairs: 12 },
{ len: .36, r0: .05, rm: .066, r1: .05, color: colors.primary, angle: -.20, yaw: -s * 1.25,
carinae: [0.4, -0.4, 1.6, -1.6, 2.7], granules: true, tricho: 4, hairs: 12 },
{ len: .36, r0: .07, rm: .15, r1: .078, color: colors.primary, angle: -.12, yaw: s * 0.25, sy: 0.72,
radial: 14, carinae: [0.3, -0.3, 1.2, -1.2, 2.2, -2.2], granules: true, tricho: 6, hairs: 22,
extra: (j, d) => buildFingers(j, d, s, arm) },
];
const ch = buildChain(defs);
ch.root.position.set(s * 0.34, 0.02, 1.32);
ch.root.rotation.y = yawFor(s, 1.0);
g.add(ch.root);
Object.assign(arm, { root: ch.root, joints: ch.joints, rest: defs.map(d => d.angle), yaws: defs.map(d => d.yaw || 0) });
arms.push(arm);
});
}
// ═════════════════════════ MESOSOMA ═════════════════════════
function buildMesosoma(g) {
// Soft pleural body under the armour plates
const soft = ellipsoid(V(0.5, 0.13, 0.92), colors.mid, 22, 12);
soft.position.set(0, -0.01, -0.38);
g.add(soft);
// Pleural folds along each flank
[-1, 1].forEach(s => {
const f = [];
for (let k = 0; k <= 30; k++) {
const z = 0.5 - k * 0.058, u = (z + 0.38) / 0.92;
f.push(V(s * 0.5 * Math.sqrt(Math.max(0, 1 - u * u)) * 1.01, -0.01 + 0.012 * Math.sin(k * 1.3), z));
}
g.add(polyline(f, lmat(colors.light, 0.5)));
});
for (let i = 0; i < 7; i++) {
const w = 0.46 + 0.1 * Math.sin(Math.PI * (i + 0.5) / 7) - (i > 4 ? (i - 4) * 0.07 : 0);
const R = V(w, 0.11 - i * 0.003, 0.17);
const tg = new THREE.Group();
tg.position.set(0, 0.07 - i * 0.004, 0.36 - i * 0.245);
g.add(tg);
tg.add(ellipsoid(R, colors.primary, 26, 10));
// Posterior margin, three keels, granules
const rim = [];
for (let k = 0; k <= 20; k++) { const u = -1 + 2 * k / 20; rim.push([u * w * 0.86, -R.z * 0.5]); }
tg.add(surfLine(R, rim, colors.light, 0.75));
[0, -0.34, 0.34].forEach(f => {
const kl = [];
for (let k = 0; k <= 8; k++) kl.push([f * w, R.z * 0.6 - k * R.z * 0.14]);
tg.add(surfLine(R, kl, colors.light, f ? 0.5 : 0.7));
});
tg.add(granuleField(R, 26, 0.014));
// Sternite plate beneath, with breathing stigmata on 3–6
g.add(ball(1, colors.mid, 0, -0.1, tg.position.z, w * 0.78, 0.025, 0.15, 14, 6));
if (i >= 2 && i <= 5) [-1, 1].forEach(s => {
g.add(lineSegs([s * w * 0.32, -0.128, tg.position.z + 0.03, s * w * 0.5, -0.128, tg.position.z + 0.01],
lmat(colors.light, 0.85)));
});
}
// Pectines: comb-like sense organs that sweep the ground
[-1, 1].forEach(s => {
const pg = new THREE.Group();
pg.position.set(s * 0.06, -0.13, 0.42);
const spine = [], teeth = [];
for (let k = 0; k <= 14; k++) {
const u = k / 14, x = s * u * 0.3, z = -u * 0.08, y = -u * 0.015;
spine.push(V(x, y, z));
if (k > 0) teeth.push(x, y, z, x - s * 0.005, y - 0.03, z - 0.05);
}
pg.add(polyline(spine, lmat(colors.light, 0.95)));
pg.add(lineSegs(teeth, lmat(colors.light, 0.8)));
g.add(pg);
pectines.push({ side: s, group: pg });
});
}
// ═════════════════════════ METASOMA (TAIL) ═════════════════════════
function buildTelson(j, d) {
// Vesicle (venom bulb) continues from the last joint, then the
// hooked aculeus curves toward local +Y — in the curled pose that
// points forward and down into the strike.
const L = d.len;
const glands = [];
for (let k = 0; k < 6; k++) {
const phi = (k / 6) * Math.PI * 2;
let prev = null;
for (let m = 0; m <= 8; m++) {
const u = 0.1 + m / 8 * 0.8, r = qb(d.r0, d.rm, d.r1, u) * 1.03;
const p = [u * L, Math.cos(phi) * r, Math.sin(phi) * r];
if (prev) glands.push(...prev, ...p);
prev = p;
}
}
j.add(lineSegs(glands, lmat(colors.light, 0.45)));
// Subaculear tubercle
j.add(ball(0.018, colors.light, L * 0.9, d.r1 * 0.9, 0, 1, 1, 1, 8, 6));
const ac = new THREE.CatmullRomCurve3([
V(L - 0.01, 0, 0), V(L + 0.1, 0.012, 0), V(L + 0.19, 0.05, 0), V(L + 0.25, 0.12, 0), V(L + 0.27, 0.2, 0),
]);
const STEPS = 6;
for (let i = 0; i < STEPS; i++) {
const sub = new THREE.CatmullRomCurve3([ac.getPoint(i / STEPS), ac.getPoint((i + 0.5) / STEPS), ac.getPoint((i + 1) / STEPS)]);
const r = 0.04 * (1 - i / STEPS) + 0.009 * (i / STEPS);
j.add(new THREE.Mesh(new THREE.TubeGeometry(sub, 4, r, 6, false), mat(colors.light)));
}
const tip = ac.getPoint(1), dir = ac.getTangent(1).normalize();
const needle = new THREE.Mesh(new THREE.ConeGeometry(0.009, 0.1, 6), mat(colors.light));
needle.position.copy(tip).addScaledVector(dir, 0.05);
needle.quaternion.setFromUnitVectors(V(0, 1, 0), dir);
j.add(needle);
// Bead of venom at the tip
venomMat = new THREE.PointsMaterial({ color: colors.light, size: 0.05, transparent: true, opacity: 0 });
const vp = tip.clone().addScaledVector(dir, 0.105);
j.add(points([vp.x, vp.y, vp.z], venomMat));
}
function buildTail(g) {
const lens = [.26, .28, .30, .32, .38];
const rads = [.13, .12, .115, .11, .105];
const rest = [1.0, 0.55, 0.5, 0.55, 0.5];
const defs = lens.map((len, i) => ({
len, r0: rads[i] * 0.95, rm: rads[i] * 1.05, r1: rads[i] * 0.9,
color: i % 2 ? colors.mid : colors.primary, angle: rest[i], radial: 12, knuckle: false,
carinae: [0.35, -0.35, 1.05, -1.05, 1.7, -1.7, 2.6, -2.6], granules: true, hairs: 14,
extra: (j, d) => j.add(ring(d.r0 * 1.02, colors.light, 0.08)),
}));
defs.push({ len: .3, r0: .07, rm: .13, r1: .035, color: colors.primary, angle: 0.35, radial: 14,
knuckle: false, hairs: 16, extra: buildTelson });
const ch = buildChain(defs);
ch.root.position.set(0, 0.08, -1.24);
ch.root.rotation.y = Math.PI / 2; // chain +X → body −Z; +rotation.z curls it up
g.add(ch.root);
ch.joints.forEach((j, i) => tail.push({ joint: j, rest: defs[i].angle, i }));
telsonJoint = ch.joints[5];
}
// ═════════════════════════ LEGS ═════════════════════════
// coxa · trochanter · femur · patella · tibia · basitarsus · tarsus
function addClaws(j, d) {
const L = d.len;
[-1, 1].forEach(s => {
const c = new THREE.QuadraticBezierCurve3(V(L, 0, s * 0.005), V(L + 0.035, 0.004, s * 0.012), V(L + 0.042, -0.028, s * 0.018));
j.add(new THREE.Mesh(new THREE.TubeGeometry(c, 6, 0.0045, 4, false), mat(colors.light)));
});
// Pedal spurs at the tarsus base
j.add(lineSegs([0.01, -d.r0, 0.005, 0.05, -d.r0 * 2.6, 0.01, 0.01, -d.r0, -0.005, 0.05, -d.r0 * 2.6, -0.012],
lmat(colors.light, 0.95)));
}
function legDefs(sc) {
const rs = Math.sqrt(sc);
return [
{ len: .12*sc, r0: .045*rs, rm: .05*rs, r1: .04*rs, color: colors.mid, angle: -.10 },
{ len: .07*sc, r0: .038*rs, rm: .038*rs, r1: .034*rs, color: colors.light, angle: .20 },
{ len: .42*sc, r0: .034*rs, rm: .04*rs, r1: .03*rs, color: colors.primary, angle: .55,
hairs: 20, carinae: [0.3, -0.3, 2.8], granules: true, tricho: 2 },
{ len: .20*sc, r0: .03*rs, rm: .033*rs, r1: .028*rs, color: colors.mid, angle: -.70, hairs: 8, carinae: [0, 3.1] },
{ len: .32*sc, r0: .026*rs, rm: .028*rs, r1: .022*rs, color: colors.primary, angle: -.60,
hairs: 18, carinae: [0.4, -0.4], tricho: 2 },
{ len: .22*sc, r0: .019*rs, rm: .02*rs, r1: .016*rs, color: colors.primary, angle: -.35, hairs: 14, spines: 5 },
{ len: .12*sc, r0: .014*rs, rm: .013*rs, r1: .009*rs, color: colors.light, angle: -.25, hairs: 10, spines: 3,
extra: addClaws },
];
}
function buildLegs(g) {
// fanAngle from +Z: the pedipalps own the forward space, so the
// front pair starts wider, and every leg keeps its own sector
const pairs = [
{ attachZ: 1.12, fanAngle: Math.PI * 0.34, scale: 0.95 },
{ attachZ: 0.94, fanAngle: Math.PI * 0.48, scale: 1.00 },
{ attachZ: 0.76, fanAngle: Math.PI * 0.62, scale: 1.05 },
{ attachZ: 0.58, fanAngle: Math.PI * 0.76, scale: 1.12 },
];
pairs.forEach((pair, pi) => {
[-1, 1].forEach(side => {
const defs = legDefs(pair.scale);
const ch = buildChain(defs);
const dz = (pair.attachZ - C_PRO.z) / R_PRO.z;
const attach = V(side * R_PRO.x * Math.sqrt(Math.max(0.1, 1 - dz * dz)) * 0.86, -0.04, pair.attachZ);
ch.root.position.copy(attach);
ch.root.rotation.y = yawFor(side, pair.fanAngle);
ch.joints[0].add(ring(defs[0].r0 * 1.25, colors.light, 0.15));
g.add(ch.root);
const phase = ((pi + (side > 0 ? 0 : 1)) % 2) * Math.PI + pi * 0.35;
legs.push({ root: ch.root, joints: ch.joints, side, pair: pi, phase, attach,
fa: pair.fanAngle, lens: defs.map(d => d.len),
rest: defs.map(d => d.angle), ang: defs.map(d => d.angle), h: 1.0 });
});
});
}
function createScorpion() {
const root = new THREE.Group();
root.add(buildGround());
body = new THREE.Group();
body.position.z = 0.05; // spin about the middle of the animal
root.add(body);
buildProsoma(body);
buildChelicerae(body);
buildArms(body);
buildMesosoma(body);
buildTail(body);
buildLegs(body);
return root;
}
// ── Pointer interaction: drag to spin (with inertia) and tilt ──
let dragging = false, lastX = 0, lastY = 0, spinVel = 0, userSpin = 0, pitch = 0;
function onDown(x, y) { dragging = true; lastX = x; lastY = y; document.body.classList.add('dragging'); }
function onMove(x, y) {
if (!dragging) return;
spinVel = (x - lastX) * 0.006;
userSpin += spinVel;
pitch = Math.max(-0.4, Math.min(0.55, pitch + (y - lastY) * 0.004));
lastX = x; lastY = y;
}
function onUp() { dragging = false; document.body.classList.remove('dragging'); }
window.addEventListener('pointerdown', e => onDown(e.clientX, e.clientY));
window.addEventListener('pointermove', e => onMove(e.clientX, e.clientY));
window.addEventListener('pointerup', onUp);
window.addEventListener('pointercancel', onUp);
// ── Camera: distance chosen so the whole animal fits any aspect ratio ──
let camDist = 5, camEl = 0.5;
function updateSize() {
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
function updateCamera() {
const w = window.innerWidth, aspect = w / window.innerHeight;
camera.fov = w < 480 ? 55 : w < 768 ? 50 : 45;
const tf = Math.tan(camera.fov * Math.PI / 360);
camDist = Math.min(10.5, Math.max(5.2, 2.8 / (tf * aspect), 2.0 / tf));
camera.aspect = aspect;
camera.updateProjectionMatrix();
}
function init() {
initBackground();
animateBackground();
updateSize();
scene = new THREE.Scene();
scene.background = new THREE.Color(colors.dark3);
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
updateCamera();
scorpion = createScorpion();
scene.add(scorpion);
animate();
}
function animate() {
requestAnimationFrame(animate);
const t = performance.now();
if (!dragging) {
userSpin += spinVel;
spinVel *= 0.95;
pitch *= 0.97;
}
const el = camEl + pitch;
camera.position.set(0, Math.sin(el) * camDist, Math.cos(el) * camDist);
camera.lookAt(0, 0.15, 0);
scorpion.rotation.y = userSpin + t * 0.0003;
// Behaviour cycle (16 s): idle → threat posture → strike → settle
const cyc = (t / 1000) % 16;
const threat = smooth(9, 10, cyc) * (1 - smooth(13.6, 14.6, cyc));
const cock = smooth(10.6, 11.6, cyc) * (1 - smooth(11.75, 11.85, cyc));
const strike = Math.exp(-(((cyc - 11.95) / 0.14) ** 2));
const gait = (0.3 + 0.7 * Math.max(0, Math.sin(t * 0.0004))) * (1 - threat);
body.position.y = Math.sin(t * 0.0012) * 0.015 + threat * 0.09;
body.rotation.x = -threat * 0.07 + Math.sin(t * 0.0008) * 0.01 + strike * 0.04;
body.rotation.z = Math.sin(t * 0.0018) * 0.008;
const by = body.position.y, sa = Math.sin(body.rotation.x), ca = Math.cos(body.rotation.x);
// Legs: tetrapod gait, IK keeps planted feet on the sand
for (const L of legs) {
const x = t * 0.0032 + L.phase;
const lift = Math.pow(Math.max(0, Math.sin(x)), 1.5) * 0.07 * gait;
const swing = Math.cos(x) * 0.05 * gait;
const a = L.ang;
for (let i = 0; i < a.length; i++) a[i] = L.rest[i];
a[4] -= lift * 1.2;
a[5] -= lift * 0.5;
const faEff = L.fa + swing + (L.pair === 3 ? threat * 0.08 : 0);
const zb = L.attach.z + L.h * Math.cos(faEff);
const target = (GROUND_Y + lift - by + zb * sa) / ca - L.attach.y;
solveFemur(L, target);
for (let i = 0; i < a.length; i++) L.joints[i].rotation.z = a[i];
L.root.rotation.y = yawFor(L.side, faEff);
}
// Tail: curled pose with a travelling wave; cocks back then strikes
for (const S of tail) {
let a = S.rest + Math.sin(t * 0.0012 + S.i * 0.35) * 0.04;
if (S.i <= 1) a += threat * 0.1 - cock * 0.18 + strike * 0.15; // base swings the tail forward
else a += threat * 0.04 + cock * 0.1 - strike * 0.2; // tip extends into the jab
S.joint.rotation.z = a;
S.joint.rotation.y = Math.sin(t * 0.0007 + S.i * 0.3) * 0.025 * (1 - threat);
}
telsonJoint.rotation.z = 0.35 + Math.sin(t * 0.002) * 0.06 + threat * 0.1 - cock * 0.15 + strike * 0.55;
venomMat.opacity = Math.max(threat * (0.6 + 0.4 * Math.sin(t * 0.008)), 0.15 * (0.5 + 0.5 * Math.sin(t * 0.0013)));
// Pedipalps: gentle sweep; claws SNAP (slow open, fast shut, hold),
// raised and held wide in the threat posture
for (const A of arms) {
const c = (t * 0.00035 + (A.side > 0 ? 0 : 0.5)) % 1;
let open;
if (c < 0.62) open = c / 0.62;
else if (c < 0.70) open = 1 - (c - 0.62) / 0.08;
else open = 0;
open = open * open * (3 - 2 * open);
open = open * (1 - threat) + threat * (0.85 + 0.15 * Math.sin(t * 0.006));
A.movable.rotation.z = -0.1 - open * 0.55;
A.fixed.rotation.z = 0.1 + open * 0.12;
A.root.rotation.y = yawFor(A.side, 1.0 + Math.sin(t * 0.0011 + A.side) * 0.05 + threat * 0.3);
A.joints[1].rotation.z = A.rest[1] + Math.sin(t * 0.0009) * 0.03 + threat * 0.22;
A.joints[2].rotation.y = A.yaws[2] + A.side * threat * 0.35;
A.joints[2].rotation.z = A.rest[2] - threat * 0.15;
A.joints[3].rotation.z = A.rest[3] + Math.sin(t * 0.0013 + A.side) * 0.04 - threat * 0.1;
}
// Chelicerae nibble, pectines sweep the ground
for (const C of chelicerae) C.finger.rotation.z = -0.35 + Math.max(0, Math.sin(t * 0.004 + C.side)) * 0.25;
for (const P of pectines) P.group.rotation.y = P.side * Math.sin(t * 0.003 + P.side) * 0.12;
eyeMat.opacity = 0.65 + 0.35 * (0.5 + 0.5 * Math.sin(t * 0.0021));
renderer.render(scene, camera);
}
window.addEventListener('resize', () => {
updateSize(); initBackground();
if (camera) updateCamera();
});
window.addEventListener('load', init);
</script>
</body>
</html>