Prompt specification
Build or customize the Three.js 3D scene "Hermit crab shell fit section" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Hermit crab shell fit section
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/hermit-crab-shell-fit-section-x0693
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Display a stylized hermit crab beside two empty spiral shells and a cutaway occupied shell; show the relationship between soft abdomen and protective borrowed structure.
Working Three.js Starter (hermit-crab-shell-fit-section-x0693):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Borrowed Armor — Voxel Hermit Crab</title>
<style>
:root {
color-scheme: dark;
--ink: #ecf5ef;
--muted: #9eb3b4;
--line: rgba(219, 241, 233, 0.18);
--panel: rgba(8, 23, 31, 0.76);
--coral: #f2755b;
--sand: #e6cf9f;
--mint: #83d3c2;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #081720;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
color: var(--ink);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.vignette {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
background:
radial-gradient(circle at 50% 43%, transparent 45%, rgba(2, 10, 15, 0.3) 100%),
linear-gradient(180deg, rgba(3, 13, 20, 0.12), transparent 35%, rgba(3, 13, 20, 0.18));
}
.header {
position: fixed;
z-index: 5;
top: 28px;
left: 30px;
width: min(440px, calc(100vw - 60px));
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 12px;
color: var(--mint);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.eyebrow::before {
width: 28px;
height: 2px;
background: var(--mint);
content: "";
}
h1 {
margin: 0;
max-width: 420px;
font-size: clamp(29px, 4vw, 54px);
font-weight: 850;
line-height: 0.96;
letter-spacing: -0.055em;
text-wrap: balance;
}
.intro {
max-width: 405px;
margin: 17px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.65;
}
.intro strong {
color: var(--ink);
font-weight: 700;
}
.legend {
position: fixed;
z-index: 5;
left: 30px;
bottom: 28px;
display: grid;
gap: 9px;
width: 235px;
padding: 15px 16px;
border: 1px solid var(--line);
border-radius: 12px;
background: var(--panel);
backdrop-filter: blur(14px);
pointer-events: none;
}
.legend-row {
display: grid;
grid-template-columns: 13px 1fr;
gap: 10px;
align-items: center;
color: var(--muted);
font-size: 11px;
line-height: 1.35;
}
.legend-row b {
display: block;
color: var(--ink);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 2px;
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.14);
}
.swatch.soft {
background: #f4a69a;
}
.swatch.shell {
background: #e6cf9f;
}
.swatch.empty {
background: #b9a77f;
}
.controls {
position: fixed;
z-index: 7;
right: 28px;
bottom: 28px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
max-width: min(520px, calc(100vw - 40px));
}
button {
appearance: none;
border: 1px solid rgba(227, 244, 236, 0.2);
border-radius: 9px;
padding: 10px 13px;
color: var(--ink);
background: rgba(8, 23, 31, 0.82);
backdrop-filter: blur(12px);
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(131, 211, 194, 0.65);
background: rgba(17, 43, 52, 0.94);
transform: translateY(-1px);
}
button.active {
border-color: var(--mint);
color: #07191d;
background: var(--mint);
}
.hint {
position: fixed;
z-index: 5;
right: 30px;
top: 30px;
color: rgba(220, 235, 232, 0.65);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.12em;
text-align: right;
line-height: 1.7;
text-transform: uppercase;
pointer-events: none;
}
.scene-label {
position: fixed;
z-index: 4;
top: 0;
left: 0;
display: flex;
align-items: center;
gap: 7px;
padding: 6px 8px;
border: 1px solid rgba(229, 244, 236, 0.2);
border-radius: 6px;
color: rgba(240, 248, 243, 0.94);
background: rgba(7, 21, 28, 0.78);
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.16);
backdrop-filter: blur(9px);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.11em;
line-height: 1;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
will-change: transform;
}
.scene-label::before {
width: 6px;
height: 6px;
flex: 0 0 auto;
border-radius: 1px;
background: var(--sand);
content: "";
}
.scene-label.soft::before {
background: #f4a69a;
}
.scene-label.borrowed::before {
background: var(--coral);
}
.scene-label.body::before {
background: #ff9a59;
}
.scene-label.empty::before {
background: #bba77c;
}
@media (max-width: 760px) {
.header {
top: 20px;
left: 20px;
width: calc(100vw - 40px);
}
h1 {
max-width: 310px;
font-size: 34px;
}
.intro {
max-width: 330px;
font-size: 12px;
}
.legend {
display: none;
}
.hint {
display: none;
}
.controls {
right: 20px;
bottom: 20px;
}
.scene-label {
font-size: 8px;
}
}
</style>
</head>
<body>
<div class="vignette"></div>
<header class="header">
<div class="eyebrow">Voxel field study · 07</div>
<h1>Borrowed<br />armor.</h1>
<p class="intro">
A hermit crab’s <strong>soft abdomen</strong> cannot grow into a hard shell.
Instead, it carries an empty gastropod shell as portable protection.
</p>
</header>
<aside class="legend" aria-label="Scene legend">
<div class="legend-row">
<span class="swatch soft"></span>
<span><b>Soft abdomen</b>Flexible and vulnerable</span>
</div>
<div class="legend-row">
<span class="swatch shell"></span>
<span><b>Borrowed shell</b>External protective armor</span>
</div>
<div class="legend-row">
<span class="swatch empty"></span>
<span><b>Vacant spirals</b>Potential future homes</span>
</div>
</aside>
<div class="hint">
Drag to orbit · Scroll to zoom<br />
Double-click to reset
</div>
<div class="controls">
<button id="explode" type="button">Separate abdomen</button>
<button id="reset" type="button">Reset view</button>
</div>
<div id="labels" aria-hidden="true">
<div class="scene-label empty" id="label-empty-a">Vacant shell A</div>
<div class="scene-label empty" id="label-empty-b">Vacant shell B</div>
<div class="scene-label borrowed" id="label-shell">Borrowed shell</div>
<div class="scene-label soft" id="label-soft">Soft abdomen</div>
<div class="scene-label body" id="label-body">Hard body</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x081720);
scene.fog = new THREE.FogExp2(0x081720, 0.026);
const camera = new THREE.PerspectiveCamera(
38,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel hermit crab and spiral shell exhibit"
);
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xc8f5ff, 0x53351f, 2.05);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffe2b0, 4.3);
keyLight.position.set(-4.5, 9, 6.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 28;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x72cbd5, 2.2);
rimLight.position.set(7, 5, -6);
scene.add(rimLight);
const warmFill = new THREE.PointLight(0xff805f, 1.15, 12, 2);
warmFill.position.set(-4, 3.2, 3);
scene.add(warmFill);
// -------------------------------------------------------------------------
// Voxel materials and instancing helpers
// -------------------------------------------------------------------------
function voxelMaterial(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
flatShading: true,
...extra
});
}
const MAT = {
crab: voxelMaterial(0xe95e3e),
crabLight: voxelMaterial(0xff8250),
crabDark: voxelMaterial(0x9f332b),
crabJoint: voxelMaterial(0xc54232),
clawTip: voxelMaterial(0xffb07a),
eyeWhite: voxelMaterial(0xf5f0d7),
eyeBlack: voxelMaterial(0x111b20),
eyeGlint: voxelMaterial(0xb9fff4, {
emissive: 0x5fc8ba,
emissiveIntensity: 0.45
}),
abdomenA: voxelMaterial(0xf1a08f),
abdomenB: voxelMaterial(0xd97872),
abdomenLight: voxelMaterial(0xffc0a8),
abdomenSpot: voxelMaterial(0xb95459),
shellCoral: voxelMaterial(0xd9634d),
shellCoralLight: voxelMaterial(0xee8061),
shellInner: voxelMaterial(0x7e3e3c),
shellCut: voxelMaterial(0xffd29a),
shellIvory: voxelMaterial(0xe8d4a3),
shellIvoryDark: voxelMaterial(0xc5a876),
shellIvoryInner: voxelMaterial(0x79664d),
shellSand: voxelMaterial(0xcbb47f),
shellSandDark: voxelMaterial(0x9f8258),
shellSandInner: voxelMaterial(0x5f503c),
pedestal: voxelMaterial(0x18333d),
tileA: voxelMaterial(0x9c8862),
tileB: voxelMaterial(0xad9770),
tileC: voxelMaterial(0x897655),
tileD: voxelMaterial(0xb59d74),
pebble: voxelMaterial(0x6f6857),
pebbleLight: voxelMaterial(0x91846a),
marker: voxelMaterial(0x83d3c2)
};
const unitBox = new THREE.BoxGeometry(1, 1, 1);
function createBatch() {
return new Map();
}
function pushMatrix(batch, material, matrix, dedupeKey = null) {
if (!batch.has(material)) {
batch.set(material, {
material,
matrices: [],
seen: new Set()
});
}
const entry = batch.get(material);
if (dedupeKey !== null) {
if (entry.seen.has(dedupeKey)) return;
entry.seen.add(dedupeKey);
}
entry.matrices.push(matrix.clone());
}
function addVoxel(
batch,
material,
x,
y,
z,
sx = 1,
sy = 1,
sz = 1,
rx = 0,
ry = 0,
rz = 0
) {
const position = new THREE.Vector3(x, y, z);
const quaternion = new THREE.Quaternion().setFromEuler(
new THREE.Euler(rx, ry, rz)
);
const scale = new THREE.Vector3(sx, sy, sz);
const matrix = new THREE.Matrix4().compose(
position,
quaternion,
scale
);
pushMatrix(batch, material, matrix);
}
function addSegment(batch, material, a, b, width, depth = width) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
if (length < 0.0001) return;
const midpoint = start.clone().add(end).multiplyScalar(0.5);
const quaternion = new THREE.Quaternion().setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
const matrix = new THREE.Matrix4().compose(
midpoint,
quaternion,
new THREE.Vector3(width, length, depth)
);
pushMatrix(batch, material, matrix);
}
function finalizeBatch(parent, batch) {
for (const entry of batch.values()) {
const mesh = new THREE.InstancedMesh(
unitBox,
entry.material,
entry.matrices.length
);
entry.matrices.forEach((matrix, index) => {
mesh.setMatrixAt(index, matrix);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.computeBoundingSphere();
parent.add(mesh);
}
}
// -------------------------------------------------------------------------
// Sand exhibit platform
// -------------------------------------------------------------------------
const platform = new THREE.Group();
scene.add(platform);
const platformBatch = createBatch();
addVoxel(
platformBatch,
MAT.pedestal,
0,
-0.37,
0,
11.8,
0.52,
9.3
);
function deterministicNoise(x, z) {
const value = Math.sin(x * 127.1 + z * 311.7) * 43758.5453;
return value - Math.floor(value);
}
for (let x = -5; x <= 5; x++) {
for (let z = -4; z <= 4; z++) {
const cornerDistance = Math.abs(x) + Math.abs(z);
if (cornerDistance > 8) continue;
const n = deterministicNoise(x, z);
const tileMaterial =
n < 0.25
? MAT.tileC
: n < 0.55
? MAT.tileA
: n < 0.83
? MAT.tileB
: MAT.tileD;
addVoxel(
platformBatch,
tileMaterial,
x * 0.98,
-0.08 + (n > 0.9 ? 0.015 : 0),
z * 0.98,
0.94,
0.16,
0.94
);
}
}
const pebbleLocations = [
[-4.65, 0.18, 2.75],
[-4.15, 0.12, 3.2],
[-2.85, 0.1, -2.85],
[-2.35, 0.13, -3.15],
[1.9, 0.1, 2.65],
[2.45, 0.17, 2.95],
[3.65, 0.11, 1.95],
[4.2, 0.14, -2.75],
[4.55, 0.1, -2.25],
[-0.15, 0.09, -3.45],
[1.15, 0.11, -3.55]
];
pebbleLocations.forEach((p, index) => {
const size = 0.16 + (index % 3) * 0.045;
addVoxel(
platformBatch,
index % 2 ? MAT.pebble : MAT.pebbleLight,
p[0],
p[1],
p[2],
size,
size * 0.7,
size,
0,
index * 0.31,
0
);
});
// Small turquoise survey markers around the exhibit.
[
[-4.65, 0.18, -3.35],
[4.65, 0.18, 3.3],
[0.05, 0.18, 3.75]
].forEach((p) => {
addVoxel(
platformBatch,
MAT.marker,
p[0],
p[1],
p[2],
0.16,
0.22,
0.16
);
});
finalizeBatch(platform, platformBatch);
// -------------------------------------------------------------------------
// Voxel spiral shell construction
// -------------------------------------------------------------------------
const SHELL_GRID = 0.14;
function shellPath(t) {
const theta = -Math.PI / 2 + t * 3.65 * Math.PI;
const radius = 1.28 * Math.pow(Math.max(0.0001, 1 - t), 0.72);
return new THREE.Vector3(
radius * Math.cos(theta),
radius * Math.sin(theta) + 1.12,
0
);
}
function quantized(value) {
return Math.round(value / SHELL_GRID) * SHELL_GRID;
}
function shellTangent(t) {
const previous = shellPath(Math.max(-0.04, t - 0.004));
const next = shellPath(Math.min(1.04, t + 0.004));
return next.sub(previous).normalize();
}
function buildShell({
cutaway = false,
outerMaterials,
innerMaterial,
cutMaterial
}) {
const group = new THREE.Group();
const batch = createBatch();
const samples = 70;
const ringSamples = 16;
for (let i = 0; i <= samples; i++) {
const t = i / samples;
const center = shellPath(t);
const tangent = shellTangent(t);
const normal = new THREE.Vector3(-tangent.y, tangent.x, 0).normalize();
const band = Math.floor(t * 10) % 2;
const outerMaterial = outerMaterials[band];
for (let j = 0; j < ringSamples; j++) {
const angle = (j / ringSamples) * Math.PI * 2;
const layers = [
{
radial: 0.27,
depth: 0.34,
material: outerMaterial
},
{
radial: 0.195,
depth: 0.255,
material: outerMaterial
},
{
radial: 0.12,
depth: 0.17,
material: innerMaterial
}
];
for (const layer of layers) {
const radialOffset = layer.radial * Math.cos(angle);
const z = layer.depth * Math.sin(angle);
if (cutaway && z > 0.005) continue;
const x = quantized(
center.x + normal.x * radialOffset
);
const y = quantized(
center.y + normal.y * radialOffset
);
const qz = quantized(z);
let material = layer.material;
if (cutaway && Math.abs(qz) < SHELL_GRID * 0.55) {
material = cutMaterial;
}
const key = `${material.uuid}:${x}:${y}:${qz}`;
addVoxel(
batch,
material,
x,
y,
qz,
SHELL_GRID * 1.03,
SHELL_GRID * 1.03,
SHELL_GRID * 1.03
);
// Record the dedupe key separately because addVoxel does not accept it.
const entry = batch.get(material);
if (entry) {
// Remove duplicate only if it was already present before this add.
// The matrix count remains correct because addVoxel already added it.
}
}
}
}
// Fill the tiny apex with a compact voxel cluster.
for (let ix = -1; ix <= 1; ix++) {
for (let iy = -1; iy <= 1; iy++) {
addVoxel(
batch,
outerMaterials[0],
ix * SHELL_GRID,
1.12 + iy * SHELL_GRID,
0,
SHELL_GRID * 1.05,
SHELL_GRID * 1.05,
SHELL_GRID * 1.05
);
}
}
finalizeBatch(group, batch);
return group;
}
// -------------------------------------------------------------------------
// Occupied cutaway shell and soft segmented abdomen
// -------------------------------------------------------------------------
const occupiedShell = buildShell({
cutaway: true,
outerMaterials: [MAT.shellCoral, MAT.shellCoralLight],
innerMaterial: MAT.shellInner,
cutMaterial: MAT.shellCut
});
occupiedShell.position.set(0.15, 0.27, -0.1);
occupiedShell.rotation.set(-0.05, 0.08, -0.08);
occupiedShell.scale.setScalar(1.12);
scene.add(occupiedShell);
const occupant = new THREE.Group();
occupiedShell.add(occupant);
const occupantBatch = createBatch();
for (let i = 0; i < 28; i++) {
const t = 0.025 + (i / 27) * 0.72;
const point = shellPath(t);
const scale = 0.27 - t * 0.08;
let material = i % 2 === 0 ? MAT.abdomenA : MAT.abdomenB;
if (i % 7 === 3) material = MAT.abdomenLight;
addVoxel(
occupantBatch,
material,
point.x * 0.93,
point.y,
0.055,
scale,
scale * 0.9,
scale,
0,
-t * 0.8,
0
);
if (i % 5 === 1) {
addVoxel(
occupantBatch,
MAT.abdomenSpot,
point.x * 0.93 - 0.07,
point.y + 0.07,
0.14,
0.075,
0.075,
0.075
);
}
}
// Abdomen segments leading from the shell opening toward the crab body.
const connector = [
[-0.78, 0.31, 0.19, 0.3],
[-0.59, 0.27, 0.21, 0.28],
[-0.42, 0.21, 0.23, 0.26],
[-0.26, 0.13, 0.24, 0.24],
[-0.12, 0.06, 0.25, 0.22]
];
connector.forEach((p, index) => {
addVoxel(
occupantBatch,
index % 2 ? MAT.abdomenA : MAT.abdomenB,
p[0],
p[1],
p[2],
p[3],
p[3] * 0.9,
p[3],
0,
0,
-0.18 * index
);
});
finalizeBatch(occupant, occupantBatch);
const softAnchor = new THREE.Object3D();
softAnchor.position.set(-0.08, 0.42, 0.08);
occupant.add(softAnchor);
const shellAnchor = new THREE.Object3D();
shellAnchor.position.set(0.62, 1.35, -0.25);
occupiedShell.add(shellAnchor);
// -------------------------------------------------------------------------
// Two empty spiral shells
// -------------------------------------------------------------------------
const emptyShellA = buildShell({
cutaway: false,
outerMaterials: [MAT.shellIvory, MAT.shellIvoryDark],
innerMaterial: MAT.shellIvoryInner
});
emptyShellA.position.set(-3.45, 0.25, 1.35);
emptyShellA.rotation.set(0.02, 0.52, 0.09);
emptyShellA.scale.setScalar(0.86);
scene.add(emptyShellA);
const emptyShellB = buildShell({
cutaway: false,
outerMaterials: [MAT.shellSand, MAT.shellSandDark],
innerMaterial: MAT.shellSandInner
});
emptyShellB.position.set(2.65, 0.23, -1.45);
emptyShellB.rotation.set(-0.03, -0.72, -0.12);
emptyShellB.scale.setScalar(0.78);
scene.add(emptyShellB);
const emptyAnchorA = new THREE.Object3D();
emptyAnchorA.position.set(0, 1.55, 0);
emptyShellA.add(emptyAnchorA);
const emptyAnchorB = new THREE.Object3D();
emptyAnchorB.position.set(0, 1.48, 0);
emptyShellB.add(emptyAnchorB);
// -------------------------------------------------------------------------
// Main voxel hermit crab
// -------------------------------------------------------------------------
const crab = new THREE.Group();
crab.position.set(-1.42, 0, 0.25);
crab.rotation.y = -0.08;
scene.add(crab);
const crabBatch = createBatch();
// Main carapace and abdomen transition.
addVoxel(crabBatch, MAT.crabDark, 0.28, 0.59, 0, 0.66, 0.42, 0.62);
addVoxel(crabBatch, MAT.crab, -0.02, 0.78, 0, 0.92, 0.57, 0.72);
addVoxel(crabBatch, MAT.crabLight, -0.12, 1.02, 0.02, 0.56, 0.13, 0.48);
addVoxel(crabBatch, MAT.crabDark, 0.13, 0.42, 0, 0.75, 0.16, 0.55);
// Head.
addVoxel(crabBatch, MAT.crab, -0.63, 0.77, 0, 0.54, 0.49, 0.58);
addVoxel(crabBatch, MAT.crabLight, -0.72, 0.91, 0.02, 0.32, 0.14, 0.43);
// Small carapace texture voxels.
[
[-0.24, 1.12, 0.18],
[0.02, 1.13, -0.16],
[0.24, 1.08, 0.08]
].forEach((p) => {
addVoxel(
crabBatch,
MAT.crabLight,
p[0],
p[1],
p[2],
0.13,
0.1,
0.13
);
});
// Eye stalks, eyes, and glints.
[-1, 1].forEach((side) => {
addSegment(
crabBatch,
MAT.crabDark,
[-0.66, 0.96, side * 0.24],
[-0.76, 1.23, side * 0.25],
0.1
);
addVoxel(
crabBatch,
MAT.eyeWhite,
-0.78,
1.29,
side * 0.25,
0.25,
0.25,
0.25
);
addVoxel(
crabBatch,
MAT.eyeBlack,
-0.91,
1.30,
side * 0.25,
0.12,
0.16,
0.18
);
addVoxel(
crabBatch,
MAT.eyeGlint,
-0.975,
1.37,
side * 0.25,
0.055,
0.055,
0.06
);
});
// Large left claw.
addSegment(
crabBatch,
MAT.crabDark,
[-0.43, 0.72, 0.31],
[-0.91, 0.85, 0.41],
0.22
);
addVoxel(
crabBatch,
MAT.crab,
-1.08,
0.91,
0.43,
0.58,
0.49,
0.52
);
addVoxel(
crabBatch,
MAT.crabLight,
-1.13,
1.04,
0.44,
0.42,
0.18,
0.45
);
addVoxel(
crabBatch,
MAT.crab,
-1.42,
0.91,
0.43,
0.48,
0.16,
0.4,
0,
0,
-0.12
);
addVoxel(
crabBatch,
MAT.crab,
-1.4,
0.69,
0.43,
0.44,
0.15,
0.37,
0,
0,
0.08
);
addVoxel(
crabBatch,
MAT.clawTip,
-1.64,
0.91,
0.43,
0.16,
0.13,
0.33
);
addVoxel(
crabBatch,
MAT.clawTip,
-1.61,
0.7,
0.43,
0.14,
0.12,
0.31
);
// Smaller right claw, set farther back.
addSegment(
crabBatch,
MAT.crabDark,
[-0.38, 0.66, -0.3],
[-0.82, 0.65, -0.42],
0.18
);
addVoxel(
crabBatch,
MAT.crab,
-0.97,
0.69,
-0.43,
0.43,
0.38,
0.43
);
addVoxel(
crabBatch,
MAT.crabLight,
-1.2,
0.78,
-0.43,
0.34,
0.14,
0.34,
0,
0,
-0.12
);
addVoxel(
crabBatch,
MAT.crab,
-1.18,
0.58,
-0.43,
0.3,
0.13,
0.31,
0,
0,
0.1
);
// Four visible pairs of jointed walking legs.
const legAnchors = [-0.25, 0.18, 0.58];
[-1, 1].forEach((side) => {
legAnchors.forEach((anchorX, index) => {
const kneeX = anchorX + (index - 1) * 0.08 + 0.08;
const kneeZ = side * (0.72 + index * 0.05);
const footX = kneeX + 0.22 + index * 0.1;
const footZ = side * (1.02 + index * 0.05);
addSegment(
crabBatch,
MAT.crabDark,
[anchorX, 0.65, side * 0.28],
[kneeX, 0.43, kneeZ],
0.14 + index * 0.008
);
addVoxel(
crabBatch,
MAT.crabJoint,
kneeX,
0.42,
kneeZ,
0.19,
0.18,
0.19
);
addSegment(
crabBatch,
MAT.crab,
[kneeX, 0.42, kneeZ],
[footX, 0.13, footZ],
0.12
);
addVoxel(
crabBatch,
MAT.crabDark,
footX,
0.1,
footZ,
0.23,
0.11,
0.2
);
// Tiny claw-like foot tip.
addVoxel(
crabBatch,
MAT.clawTip,
footX - 0.08,
0.12,
footZ + side * 0.06,
0.13,
0.08,
0.09
);
});
});
finalizeBatch(crab, crabBatch);
const bodyAnchor = new THREE.Object3D();
bodyAnchor.position.set(-0.1, 1.37, 0);
crab.add(bodyAnchor);
// -------------------------------------------------------------------------
// Manual camera controls: orbit, zoom, touch, keyboard, and reset
// -------------------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(-0.25, 0.95, 0),
radius: 10.8,
targetRadius: 10.8,
theta: 0.56,
targetTheta: 0.56,
phi: 0.98,
targetPhi: 0.98
};
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
let activePointerId = null;
function resetCamera() {
cameraState.targetRadius = 10.8;
cameraState.targetTheta = 0.56;
cameraState.targetPhi = 0.98;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
activePointerId = event.pointerId;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== activePointerId) return;
const dx = event.clientX - previousPointerX;
const dy = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraState.targetTheta -= dx * 0.006;
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi - dy * 0.005,
0.48,
1.34
);
});
function endDrag(event) {
if (event.pointerId !== activePointerId) return;
dragging = false;
activePointerId = null;
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius + event.deltaY * 0.008,
6.3,
16
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
addEventListener("keydown", (event) => {
switch (event.key) {
case "ArrowLeft":
cameraState.targetTheta -= 0.08;
break;
case "ArrowRight":
cameraState.targetTheta += 0.08;
break;
case "ArrowUp":
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi - 0.06,
0.48,
1.34
);
break;
case "ArrowDown":
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi + 0.06,
0.48,
1.34
);
break;
case "+":
case "=":
cameraState.targetRadius = Math.max(
6.3,
cameraState.targetRadius - 0.5
);
break;
case "-":
cameraState.targetRadius = Math.min(
16,
cameraState.targetRadius + 0.5
);
break;
case "r":
case "R":
resetCamera();
break;
}
});
// -------------------------------------------------------------------------
// Anatomy separation control
// -------------------------------------------------------------------------
const explodeButton = document.getElementById("explode");
const resetButton = document.getElementById("reset");
let anatomySeparated = false;
const occupantTargetPosition = new THREE.Vector3();
const occupantGoal = new THREE.Vector3();
explodeButton.addEventListener("click", () => {
anatomySeparated = !anatomySeparated;
explodeButton.classList.toggle("active", anatomySeparated);
explodeButton.textContent = anatomySeparated
? "Rejoin abdomen"
: "Separate abdomen";
});
resetButton.addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Projected HTML labels
// -------------------------------------------------------------------------
const labelDefinitions = [
{
element: document.getElementById("label-empty-a"),
object: emptyAnchorA,
offset: [-56, -35]
},
{
element: document.getElementById("label-empty-b"),
object: emptyAnchorB,
offset: [48, -30]
},
{
element: document.getElementById("label-shell"),
object: shellAnchor,
offset: [58, -14]
},
{
element: document.getElementById("label-soft"),
object: softAnchor,
offset: [-85, -14]
},
{
element: document.getElementById("label-body"),
object: bodyAnchor,
offset: [-30, -20]
}
];
const projectedPosition = new THREE.Vector3();
const worldPosition = new THREE.Vector3();
function updateLabels() {
const width = innerWidth;
const height = innerHeight;
for (const label of labelDefinitions) {
label.object.getWorldPosition(worldPosition);
projectedPosition.copy(worldPosition).project(camera);
const x = (projectedPosition.x * 0.5 + 0.5) * width + label.offset[0];
const y = (-projectedPosition.y * 0.5 + 0.5) * height + label.offset[1];
const visible =
projectedPosition.z > -1 &&
projectedPosition.z < 1 &&
x > -120 &&
x < width + 120 &&
y > -50 &&
y < height + 50;
label.element.style.opacity = visible ? "1" : "0";
label.element.style.transform =
`translate(${x}px, ${y}px) translate(-50%, -50%)`;
}
}
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
cameraState.radius = THREE.MathUtils.lerp(
cameraState.radius,
cameraState.targetRadius,
1 - Math.pow(0.001, delta)
);
cameraState.theta = THREE.MathUtils.lerp(
cameraState.theta,
cameraState.targetTheta,
1 - Math.pow(0.001, delta)
);
cameraState.phi = THREE.MathUtils.lerp(
cameraState.phi,
cameraState.targetPhi,
1 - Math.pow(0.001, delta)
);
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
camera.updateMatrixWorld();
occupantGoal.set(
anatomySeparated ? 0.04 : 0,
anatomySeparated ? 0.16 : 0,
anatomySeparated ? 0.72 : 0
);
occupantTargetPosition.lerp(
occupantGoal,
1 - Math.pow(0.002, delta)
);
occupant.position.copy(occupantTargetPosition);
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```