Prompt specification
Build or customize the Three.js 3D scene "Reef den-building vignette" 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: Reef den-building vignette
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/reef-den-building-vignette-x0699
Scene Description Prompt:
Reef den-building vignette. Build a rocky seabed with octopus den, shells, pebbles, and algae; the animal moves one object to the entrance while other arms maintain plausible support contacts. Please build this with Three.js from cdnjs in one self-contained HTML file, using visible voxel blocks.
Working Three.js Starter (reef-den-building-vignette-x0699):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Reef Den Builders — Voxel Octopus Vignette</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(4, 28, 34, 0.78);
--border: rgba(137, 231, 220, 0.24);
--mint: #9ef2dc;
--text: #e6fffa;
--muted: #9cc4bf;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 52% 28%, rgba(25, 119, 125, 0.34), transparent 38%),
linear-gradient(180deg, #0b5260 0%, #082f3c 46%, #041923 100%);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
canvas:active {
cursor: grabbing;
}
.overlay {
position: fixed;
inset: 0;
pointer-events: none;
}
.title-card {
position: absolute;
top: 22px;
left: 22px;
width: min(340px, calc(100vw - 44px));
padding: 17px 19px;
border: 1px solid var(--border);
border-radius: 16px;
background: linear-gradient(145deg, rgba(8, 49, 56, 0.88), var(--panel));
box-shadow: 0 18px 45px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(12px);
}
.eyebrow {
margin-bottom: 7px;
color: var(--mint);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 31px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.controls {
position: absolute;
top: 22px;
right: 22px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
min-height: 39px;
padding: 0 14px;
border: 1px solid rgba(158, 242, 220, 0.28);
border-radius: 11px;
background: rgba(5, 37, 44, 0.82);
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
backdrop-filter: blur(10px);
transition:
background 160ms ease,
transform 160ms ease,
border-color 160ms ease;
}
button:hover {
border-color: rgba(158, 242, 220, 0.65);
background: rgba(12, 67, 72, 0.92);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
.status-wrap {
position: absolute;
left: 50%;
bottom: 22px;
width: min(600px, calc(100vw - 36px));
transform: translateX(-50%);
text-align: center;
}
.status {
display: inline-flex;
align-items: center;
gap: 9px;
padding: 10px 15px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(4, 29, 35, 0.78);
color: #dcfff6;
font-size: 12px;
font-weight: 650;
box-shadow: 0 12px 32px rgba(0, 0, 0, 0.23);
backdrop-filter: blur(10px);
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: #83f1cf;
box-shadow: 0 0 13px #83f1cf;
}
.hint {
margin-top: 9px;
color: rgba(205, 238, 233, 0.65);
font-size: 10px;
letter-spacing: 0.06em;
text-shadow: 0 2px 8px rgba(0, 0, 0, 0.6);
}
@media (max-width: 650px) {
.title-card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 15px;
}
.subtitle {
display: none;
}
.controls {
top: auto;
right: 12px;
bottom: 78px;
}
.status-wrap {
bottom: 12px;
}
}
</style>
</head>
<body>
<div class="overlay">
<section class="title-card">
<div class="eyebrow">Reef behavior study 04</div>
<h1>Den Builders</h1>
<p class="subtitle">
A voxel octopus braces against the rocky seabed while carefully moving an
amber shell to the entrance of its shelter.
</p>
</section>
<div class="controls">
<button id="pauseButton" type="button">Pause</button>
<button id="replayButton" type="button">Replay</button>
</div>
<div class="status-wrap">
<div class="status">
<span class="status-dot"></span>
<span id="statusText">Bracing against the reef</span>
</div>
<div class="hint">
Drag to orbit · Scroll to zoom · Arrow keys to move · Space to pause
</div>
</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 renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(0x000000, 0);
renderer.domElement.setAttribute("aria-label", "Interactive voxel reef scene");
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x082f3b, 0.035);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
90
);
const hemisphere = new THREE.HemisphereLight(0x9eefff, 0x172b29, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe2b0, 3.15);
sun.position.set(-7, 13, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -17;
sun.shadow.camera.right = 17;
sun.shadow.camera.top = 15;
sun.shadow.camera.bottom = -15;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 42;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x4bc8dc, 1.15);
fill.position.set(10, 6, -8);
scene.add(fill);
const denLight = new THREE.PointLight(0x63e6d5, 1.8, 11, 2);
denLight.position.set(3.2, 2.8, -1.5);
scene.add(denLight);
const warmLight = new THREE.PointLight(0xffa46d, 1.15, 8, 2);
warmLight.position.set(-2.5, 2.7, 3.1);
scene.add(warmLight);
// -------------------------------------------------------------------------
// Shared helpers and deterministic random generation
// -------------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
const materialCache = new Map();
function material(color, roughness = 0.88, metalness = 0.02) {
const key = `${color}-${roughness}-${metalness}`;
if (!materialCache.has(key)) {
materialCache.set(
key,
new THREE.MeshStandardMaterial({
color,
roughness,
metalness
})
);
}
return materialCache.get(key);
}
function mulberry32(seed) {
return function random() {
let value = (seed += 0x6d2b79f5);
value = Math.imul(value ^ (value >>> 15), value | 1);
value ^= value + Math.imul(value ^ (value >>> 7), value | 61);
return ((value ^ (value >>> 14)) >>> 0) / 4294967296;
};
}
const random = mulberry32(90210);
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function smooth01(value) {
const x = clamp(value, 0, 1);
return x * x * (3 - 2 * x);
}
function lerp(a, b, t) {
return a + (b - a) * t;
}
function makeBox(parent, x, y, z, sx, sy, sz, color, options = {}) {
const mesh = new THREE.Mesh(
unitBox,
options.material || material(color, options.roughness, options.metalness)
);
mesh.position.set(x, y, z);
mesh.scale.set(sx, sy, sz);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
parent.add(mesh);
return mesh;
}
function setInstance(instanceMesh, index, x, y, z, sx, sy, sz, color) {
dummy.position.set(x, y, z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(sx, sy, sz);
dummy.updateMatrix();
instanceMesh.setMatrixAt(index, dummy.matrix);
if (color !== undefined) {
instanceMesh.setColorAt(index, new THREE.Color(color));
}
}
// -------------------------------------------------------------------------
// Rocky seabed: stacked visible voxel columns
// -------------------------------------------------------------------------
const DEN_X = 3.2;
function terrainHeight(x, z) {
// A flattened approach makes the den entrance and carried-shell path legible.
if (x > 1.45 && x < 4.9 && z > -2.8 && z < 2.9) {
return 0;
}
const height =
0.65 +
0.55 * Math.sin(x * 0.48 + 0.4) +
0.45 * Math.cos(z * 0.55) +
0.25 * Math.sin((x - z) * 0.31);
return clamp(Math.floor(height), 0, 2);
}
function insideCave(x, z) {
return x >= 2 && x <= 4 && z >= -5 && z <= -3;
}
function buildGround() {
const blocks = [];
const topColors = [0x777867, 0x81806c, 0x6d7565, 0x8b836b];
const deepColors = [0x4e554e, 0x5c5a4e, 0x46534f, 0x68614f];
for (let x = -11; x <= 11; x++) {
for (let z = -8; z <= 8; z++) {
if (insideCave(x, z)) continue;
const top = terrainHeight(x, z);
const count = top + 6;
for (let layer = 0; layer < count; layer++) {
const y = top - 0.46 - layer;
const jitter = random();
const color =
layer === 0
? topColors[Math.floor(jitter * topColors.length)]
: deepColors[
Math.floor(jitter * deepColors.length)
];
blocks.push({ x, y, z, color });
}
}
}
const mesh = new THREE.InstancedMesh(
unitBox,
material(0xffffff, 0.96),
blocks.length
);
blocks.forEach((block, index) => {
setInstance(
mesh,
index,
block.x,
block.y,
block.z,
0.92,
0.92,
0.92,
block.color
);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.instanceColor.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
}
buildGround();
// -------------------------------------------------------------------------
// Den chamber and block-built stone entrance
// -------------------------------------------------------------------------
const den = new THREE.Group();
scene.add(den);
// Dark chamber behind the opening.
makeBox(den, DEN_X, 0.45, -5.28, 2.7, 2.75, 0.28, 0x07171d);
makeBox(den, DEN_X - 1.38, 0.35, -4.2, 0.32, 2.65, 2.25, 0x0a2025);
makeBox(den, DEN_X + 1.38, 0.35, -4.2, 0.32, 2.65, 2.25, 0x0a2025);
makeBox(den, DEN_X, -1.05, -4.15, 2.55, 0.22, 2.45, 0x10292b);
const stoneColors = [0x64706a, 0x788077, 0x505d59, 0x8a8877];
function denStone(x, y, z, sx, sy, sz, shadeIndex = 0) {
return makeBox(
den,
x,
y,
z,
sx,
sy,
sz,
stoneColors[shadeIndex % stoneColors.length],
{ roughness: 0.98 }
);
}
// Left and right uprights.
for (let i = 0; i < 5; i++) {
denStone(
DEN_X - 1.24,
0.18 + i * 0.48,
-2.73,
0.56 + (i % 2) * 0.08,
0.48,
0.64,
i
);
denStone(
DEN_X + 1.24,
0.18 + i * 0.48,
-2.73,
0.56 + ((i + 1) % 2) * 0.08,
0.48,
0.64,
i + 1
);
}
// Stepped lintel.
const lintel = [
[-0.88, 2.15, 0.56, 0.48],
[-0.46, 2.28, 0.54, 0.50],
[0, 2.28, 0.54, 0.50],
[0.46, 2.28, 0.54, 0.50],
[0.88, 2.15, 0.56, 0.48]
];
lintel.forEach((entry, index) => {
denStone(
DEN_X + entry[0],
entry[1],
-2.73,
entry[2],
entry[3],
0.68,
index + 2
);
});
// Loose blocks around the threshold.
[
[1.55, -2.3, 0.48],
[1.82, -2.92, 0.4],
[2.0, -2.1, 0.3],
[4.55, -2.25, 0.42],
[4.8, -3.0, 0.55],
[4.35, -1.95, 0.28],
[3.8, -2.45, 0.24]
].forEach((entry, index) => {
const ground = terrainHeight(entry[0], entry[1]);
denStone(
entry[0],
ground + entry[2] * 0.45,
entry[1],
entry[2],
entry[2] * 0.82,
entry[2],
index + 1
);
});
// -------------------------------------------------------------------------
// Pebbles and small blocky rocks
// -------------------------------------------------------------------------
function buildPebbles() {
const pebbleData = [];
for (let cluster = 0; cluster < 115; cluster++) {
const x = random() * 21 - 10.5;
const z = random() * 15 - 7.5;
if (
(x > 1.25 && x < 5.0 && z > -5.8 && z < -1.7) ||
(Math.abs(x + 0.8) < 1.45 && Math.abs(z - 1.1) < 1.35)
) {
continue;
}
const count = 1 + Math.floor(random() * 4);
const large = random() > 0.78;
const baseSize = large
? 0.46 + random() * 0.26
: 0.16 + random() * 0.24;
for (let i = 0; i < count; i++) {
const px = x + (random() - 0.5) * 0.55;
const pz = z + (random() - 0.5) * 0.55;
const size = baseSize * (0.68 + random() * 0.55);
const ground = terrainHeight(px, pz);
pebbleData.push({
x: px,
y: ground + size * 0.43,
z: pz,
sx: size,
sy: size * (0.55 + random() * 0.35),
sz: size * (0.7 + random() * 0.3),
color: [0x68726c, 0x7d8175, 0x596762, 0x8b8876][
Math.floor(random() * 4)
]
});
}
}
const mesh = new THREE.InstancedMesh(
unitBox,
material(0xffffff, 0.98),
pebbleData.length
);
pebbleData.forEach((pebble, index) => {
setInstance(
mesh,
index,
pebble.x,
pebble.y,
pebble.z,
pebble.sx,
pebble.sy,
pebble.sz,
pebble.color
);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.instanceColor.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
}
buildPebbles();
// -------------------------------------------------------------------------
// Block-built algae
// -------------------------------------------------------------------------
function buildAlgae() {
const algae = [];
for (let patch = 0; patch < 30; patch++) {
let x = random() * 20 - 10;
let z = random() * 14 - 7;
if (
(x > 1.4 && x < 5.1 && z > -5.8 && z < -1.6) ||
(Math.abs(x + 0.8) < 1.4 && Math.abs(z - 1.1) < 1.25)
) {
continue;
}
const ground = terrainHeight(x, z);
const stalkHeight = 2 + Math.floor(random() * 4);
const width = 0.11 + random() * 0.055;
for (let level = 0; level < stalkHeight; level++) {
algae.push({
x,
y: ground + 0.11 + level * 0.23,
z,
sx: width,
sy: 0.25,
sz: width,
color: level % 2 ? 0x287d67 : 0x369b73
});
}
for (let leaf = 0; leaf < 4; leaf++) {
const direction = leaf % 2 ? 1 : -1;
const leafY = ground + 0.35 + random() * stalkHeight * 0.22;
algae.push({
x: x + direction * (0.13 + random() * 0.12),
y: leafY,
z: z + (random() - 0.5) * 0.18,
sx: 0.32 + random() * 0.17,
sy: 0.09,
sz: 0.13,
color: leaf % 2 ? 0x45aa78 : 0x2d8f6e
});
}
}
const mesh = new THREE.InstancedMesh(
unitBox,
material(0xffffff, 0.9),
algae.length
);
algae.forEach((leaf, index) => {
setInstance(
mesh,
index,
leaf.x,
leaf.y,
leaf.z,
leaf.sx,
leaf.sy,
leaf.sz,
leaf.color
);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.instanceColor.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
}
buildAlgae();
// -------------------------------------------------------------------------
// Voxel shell construction
// -------------------------------------------------------------------------
function buildShell(scale = 1) {
const group = new THREE.Group();
const voxel = 0.18 * scale;
const pieces = [
[-2, 0, 0, 1],
[-1, 0, 0, 1],
[0, 0, 0, 1],
[1, 0, 0, 1],
[2, 0, 0, 1],
[-2, 0.16, 0.02, 1],
[-1, 0.16, 0.12, 1],
[0, 0.16, 0.18, 1],
[1, 0.16, 0.12, 1],
[2, 0.16, 0.02, 1],
[-1, 0.32, 0.05, 1],
[0, 0.32, 0.13, 1],
[1, 0.32, 0.05, 1]
];
pieces.forEach((piece, index) => {
makeBox(
group,
piece[0] * voxel,
piece[1] * voxel,
piece[2] * voxel,
voxel,
voxel,
voxel,
index % 4 === 0 ? 0xf2b968 : 0xd88b47,
{ roughness: 0.72 }
);
});
// Small contrasting ridge makes the carried object easier to recognize.
makeBox(
group,
0,
0.47 * scale,
0.05 * scale,
0.13 * scale,
0.13 * scale,
0.13 * scale,
0xffd38a
);
return group;
}
const carriedShell = buildShell(1.08);
scene.add(carriedShell);
[
[-5.8, 4.1, 0.76],
[-6.4, -2.2, 0.64],
[1.25, 5.15, 0.72],
[7.2, 1.8, 0.68],
[6.4, -5.2, 0.8],
[-8.3, 2.8, 0.62],
[8.3, -0.6, 0.7]
].forEach((entry) => {
const shell = buildShell(entry[2]);
shell.position.set(
entry[0],
terrainHeight(entry[0], entry[1]) + 0.02,
entry[1]
);
scene.add(shell);
});
// -------------------------------------------------------------------------
// Octopus body: a visible field of cubic voxels
// -------------------------------------------------------------------------
const octopus = new THREE.Group();
octopus.position.set(-0.8, terrainHeight(-0.8, 1.1), 1.1);
scene.add(octopus);
const bodyVoxels = [];
for (let ix = -3; ix <= 3; ix++) {
for (let iy = 0; iy <= 6; iy++) {
for (let iz = -2; iz <= 2; iz++) {
const x = ix * 0.28;
const y = 0.27 + iy * 0.29;
const z = iz * 0.28;
const shape =
(x / 1.02) ** 2 +
(z / 0.82) ** 2 +
((y - 1.18) / 1.05) ** 2;
if (shape <= 1) {
bodyVoxels.push({ x, y, z, ix, iy, iz });
}
}
}
}
const bodyMesh = new THREE.InstancedMesh(
unitBox,
material(0xffffff, 0.82),
bodyVoxels.length
);
bodyVoxels.forEach((voxel, index) => {
const spot = (voxel.ix * 13 + voxel.iy * 7 + voxel.iz * 5) % 12 === 0;
const upper = voxel.iy > 4;
const color = spot
? 0x873b6a
: upper
? 0xc65c91
: voxel.iy < 2
? 0x9e4478
: 0xb84d82;
setInstance(
bodyMesh,
index,
voxel.x,
voxel.y,
voxel.z,
0.255,
0.255,
0.255,
color
);
});
bodyMesh.instanceMatrix.needsUpdate = true;
bodyMesh.instanceColor.needsUpdate = true;
bodyMesh.castShadow = true;
bodyMesh.receiveShadow = true;
octopus.add(bodyMesh);
// Eyes, pupils, highlights and a blocky mouth.
[-0.41, 0.41].forEach((x, index) => {
makeBox(
octopus,
x,
1.3,
0.77,
0.35,
0.35,
0.24,
0xf5eadf,
{ roughness: 0.6 }
);
makeBox(
octopus,
x + (index === 0 ? 0.035 : -0.035),
1.28,
0.91,
0.145,
0.17,
0.08,
0x15171d,
{ roughness: 0.42 }
);
makeBox(
octopus,
x + (index === 0 ? 0.065 : -0.065),
1.34,
0.955,
0.045,
0.05,
0.035,
0xffffff
);
});
[-0.24, 0, 0.24].forEach((x) => {
makeBox(
octopus,
x,
0.74,
0.77,
0.16,
0.12,
0.12,
0x542541
);
});
// -------------------------------------------------------------------------
// Eight articulated voxel arms
// -------------------------------------------------------------------------
const MAX_ARM_VOXELS = 24;
const ARM_COUNT = 8;
const armMesh = new THREE.InstancedMesh(
unitBox,
material(0xffffff, 0.78),
MAX_ARM_VOXELS * ARM_COUNT
);
const suckerMesh = new THREE.InstancedMesh(
unitBox,
material(0xf2c4bd, 0.74),
MAX_ARM_VOXELS * ARM_COUNT
);
armMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
suckerMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
armMesh.frustumCulled = false;
suckerMesh.frustumCulled = false;
armMesh.castShadow = true;
armMesh.receiveShadow = true;
suckerMesh.castShadow = true;
for (let arm = 0; arm < ARM_COUNT; arm++) {
for (let segment = 0; segment < MAX_ARM_VOXELS; segment++) {
const index = arm * MAX_ARM_VOXELS + segment;
const color =
segment % 7 === 0
? 0x943d71
: segment > 16
? 0xc35a8c
: 0xb84e82;
armMesh.setColorAt(index, new THREE.Color(color));
}
}
scene.add(armMesh, suckerMesh);
const armBaseLocal = [
[0.72, 0.57, 0.36],
[0.64, 0.53, 0.7],
[0.0, 0.52, 0.86],
[-0.64, 0.53, 0.7],
[-0.72, 0.57, 0.36],
[-0.62, 0.54, -0.43],
[0.0, 0.54, -0.78],
[0.62, 0.54, -0.43]
];
const supportTargets = [
null,
[1.0, 2.75],
[-2.8, 2.45],
[-3.0, 0.75],
[-2.75, -0.8],
[-1.45, -1.35],
[0.25, -1.35],
[1.2, 0.05]
];
const arms = armBaseLocal.map((base, index) => {
const worldBase = new THREE.Vector3(
octopus.position.x + base[0],
octopus.position.y + base[1],
octopus.position.z + base[2]
);
let target;
if (index === 0) {
target = new THREE.Vector3(1.05, terrainHeight(1.05, 2.05) + 0.1, 2.05);
} else {
const [x, z] = supportTargets[index];
target = new THREE.Vector3(
x,
terrainHeight(x, z) + 0.1,
z
);
}
return { base: worldBase, target, rest: target.clone() };
});
const bezierPoint = new THREE.Vector3();
function cubicBezier(out, p0, p1, p2, p3, t) {
const u = 1 - t;
const uu = u * u;
const tt = t * t;
out.set(
uu * u * p0.x + 3 * uu * t * p1.x + 3 * u * tt * p2.x + tt * t * p3.x,
uu * u * p0.y + 3 * u * t * p1.y + 3 * u * tt * p2.y + tt * t * p3.y,
uu * u * p0.z + 3 * uu * t * p1.z + 3 * u * tt * p2.z + tt * t * p3.z
);
return out;
}
function updateArms(time) {
const bodyCenter = octopus.position;
const shellGrip = carriedShell.position.clone().add(new THREE.Vector3(0, 0.31, 0));
let carrierTarget = arms[0].rest;
if (time >= 1.15 && time < 2.4) {
const amount = smooth01((time - 1.15) / 1.25);
carrierTarget = arms[0].rest.clone().lerp(shellGrip, amount);
} else if (time >= 2.4 && time < 10.0) {
carrierTarget = shellGrip;
} else if (time >= 10.0 && time < 11.4) {
const amount = smooth01((time - 10.0) / 1.4);
carrierTarget = shellGrip.clone().lerp(arms[0].rest, amount);
}
arms.forEach((arm, armIndex) => {
const target = armIndex === 0 ? carrierTarget : arm.target;
const radial = target.clone().sub(bodyCenter);
radial.y = 0;
if (radial.lengthSq() < 0.001) {
radial.set(1, 0, 0);
} else {
radial.normalize();
}
const p0 = arm.base;
const p3 = target;
const p1 = p0.clone().addScaledVector(radial, 0.6);
p1.y = Math.max(p0.y, target.y) + 0.42;
const p2 = p3.clone().lerp(p0, 0.28);
p2.y = target.y + 0.2;
for (let segment = 0; segment < MAX_ARM_VOXELS; segment++) {
const t = segment / (MAX_ARM_VOXELS - 1);
const index = armIndex * MAX_ARM_VOXELS + segment;
cubicBezier(bezierPoint, p0, p1, p2, p3, t);
const size = lerp(0.29, 0.155, t);
setInstance(
armMesh,
index,
bezierPoint.x,
bezierPoint.y,
bezierPoint.z,
size,
size,
size
);
if (segment % 3 === 0 && t < 0.82) {
setInstance(
suckerMesh,
index,
bezierPoint.x,
bezierPoint.y - size * 0.42,
bezierPoint.z,
0.075,
0.055,
0.075
);
} else {
setInstance(suckerMesh, index, 0, -50, 0, 0, 0, 0);
}
}
});
armMesh.instanceMatrix.needsUpdate = true;
suckerMesh.instanceMatrix.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Carried shell animation
// -------------------------------------------------------------------------
const shellStart = new THREE.Vector3(
0.6,
terrainHeight(0.6, 2.6) + 0.02,
2.6
);
const shellEnd = new THREE.Vector3(
DEN_X,
terrainHeight(DEN_X, -1.95) + 0.02,
-1.95
);
function updateShell(time) {
if (time < 2.4) {
carriedShell.position.copy(shellStart);
} else if (time < 3.3) {
const amount = smooth01((time - 2.4) / 0.9);
carriedShell.position.set(
shellStart.x,
lerp(shellStart.y, 1.05, amount),
shellStart.z
);
} else if (time < 8.9) {
const amount = smooth01((time - 3.3) / 5.6);
carriedShell.position.set(
lerp(shellStart.x, shellEnd.x, amount),
1.05 + Math.sin(amount * Math.PI) * 0.28,
lerp(shellStart.z, shellEnd.z, amount)
);
} else if (time < 10.0) {
const amount = smooth01((time - 8.9) / 1.1);
carriedShell.position.set(
shellEnd.x,
lerp(1.05, shellEnd.y, amount),
shellEnd.z
);
} else {
carriedShell.position.copy(shellEnd);
}
}
// -------------------------------------------------------------------------
// Drifting cube particles
// -------------------------------------------------------------------------
const PARTICLE_COUNT = 58;
const particleMesh = new THREE.InstancedMesh(
unitBox,
new THREE.MeshStandardMaterial({
color: 0xbcefe8,
roughness: 0.5,
transparent: true,
opacity: 0.42,
depthWrite: false
}),
PARTICLE_COUNT
);
particleMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
particleMesh.frustumCulled = false;
scene.add(particleMesh);
const particles = Array.from({ length: PARTICLE_COUNT }, () => ({
x: random() * 22 - 11,
y: random() * 7 + 0.4,
z: random() * 16 - 8,
size: 0.025 + random() * 0.055,
speed: 0.08 + random() * 0.18,
drift: 0.08 + random() * 0.16
}));
function updateParticles(time) {
particles.forEach((particle, index) => {
const y = 0.3 + ((particle.y + time * particle.speed) % 7.2);
const x =
particle.x +
Math.sin(time * 0.24 + index * 1.7) * particle.drift;
setInstance(
particleMesh,
index,
x,
y,
particle.z,
particle.size,
particle.size,
particle.size
);
});
particleMesh.instanceMatrix.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Custom camera controls — no external control addons
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0.6, 0.25, -0.25);
const desiredCamera = {
theta: 0.69,
phi: 1.03,
radius: 16.2
};
const currentCamera = {
theta: desiredCamera.theta,
phi: desiredCamera.phi,
radius: desiredCamera.radius
};
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
desiredCamera.theta -= deltaX * 0.006;
desiredCamera.phi = clamp(
desiredCamera.phi + deltaY * 0.005,
0.42,
1.38
);
});
function stopDragging(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredCamera.radius = clamp(
desiredCamera.radius * Math.exp(event.deltaY * 0.001),
8.5,
26
);
},
{ passive: false }
);
function updateCamera(delta) {
const damping = 1 - Math.exp(-delta * 8);
currentCamera.theta = lerp(
currentCamera.theta,
desiredCamera.theta,
damping
);
currentCamera.phi = lerp(
currentCamera.phi,
desiredCamera.phi,
damping
);
currentCamera.radius = lerp(
currentCamera.radius,
desiredCamera.radius,
damping
);
const sinPhi = Math.sin(currentCamera.phi);
camera.position.set(
cameraTarget.x +
currentCamera.radius *
sinPhi *
Math.sin(currentCamera.theta),
cameraTarget.y + currentCamera.radius * Math.cos(currentCamera.phi),
cameraTarget.z +
currentCamera.radius *
sinPhi *
Math.cos(currentCamera.theta)
);
camera.lookAt(cameraTarget);
}
window.addEventListener("keydown", (event) => {
const step = 0.12;
if (event.code === "ArrowLeft") {
desiredCamera.theta += step;
} else if (event.code === "ArrowRight") {
desiredCamera.theta -= step;
} else if (event.code === "ArrowUp") {
desiredCamera.phi = clamp(desiredCamera.phi - step, 0.42, 1.38);
} else if (event.code === "ArrowDown") {
desiredCamera.phi = clamp(desiredCamera.phi + step, 0.42, 1.38);
} else if (event.code === "Space") {
event.preventDefault();
togglePause();
} else if (event.key.toLowerCase() === "r") {
replayAnimation();
}
});
// -------------------------------------------------------------------------
// Playback controls and animation loop
// -------------------------------------------------------------------------
const pauseButton = document.getElementById("pauseButton");
const replayButton = document.getElementById("replayButton");
const statusText = document.getElementById("statusText");
let paused = false;
let animationTime = 0;
let worldTime = 0;
function togglePause() {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
}
function replayAnimation() {
animationTime = 0;
paused = false;
pauseButton.textContent = "Pause";
updateShell(0);
}
pauseButton.addEventListener("click", togglePause);
replayButton.addEventListener("click", replayAnimation);
function updateStatus(time) {
let label;
if (time < 1.15) {
label = "Bracing against the reef";
} else if (time < 2.4) {
label = "Reaching for the amber shell";
} else if (time < 3.3) {
label = "Securing the shell with one arm";
} else if (time < 8.9) {
label = "Carrying the shell toward the den";
} else if (time < 10.0) {
label = "Lowering the shell at the entrance";
} else if (time < 11.4) {
label = "Releasing the shell";
} else {
label = "Shell placed — seven arms remain in contact";
}
statusText.textContent = label;
}
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) {
worldTime += delta;
animationTime += delta;
}
updateShell(animationTime);
updateArms(animationTime);
updateParticles(worldTime);
updateCamera(delta);
updateStatus(animationTime);
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
updateShell(0);
updateArms(0);
animate();
</script>
</body>
</html>
```