Prompt specification
Build or customize the Three.js 3D scene "Robot Reaches a Shelf Within Joint Limits" 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: Robot Reaches a Shelf Within Joint Limits
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/robot-reaches-a-shelf-within-joint-limits-x3742
Scene Description Prompt:
A voxel robot moves an object to a shelf within joint limits, visibly rejecting unreachable targets. Three.js from cdnjs, one HTML file.
Working Three.js Starter (robot-reaches-a-shelf-within-joint-limits-x3742):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Pick & Place</title>
<style>
:root {
--bg: #071018;
--panel: rgba(10, 23, 32, 0.88);
--line: rgba(130, 205, 220, 0.18);
--text: #e7f2f4;
--muted: #78939d;
--cyan: #42d9df;
--green: #73e49d;
--amber: #ffc35c;
--red: #ff6674;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button { font: inherit; }
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#viewport canvas:active { cursor: grabbing; }
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.brand {
position: absolute;
top: 24px;
left: 26px;
display: flex;
align-items: center;
gap: 13px;
text-shadow: 0 2px 15px #000;
}
.brand-icon {
width: 39px;
height: 39px;
display: grid;
place-items: center;
border: 1px solid var(--cyan);
color: var(--cyan);
background: rgba(13, 38, 47, 0.75);
box-shadow: inset 0 0 18px rgba(66, 217, 223, 0.12);
font-size: 21px;
}
.brand h1 {
margin: 0;
font-size: 15px;
font-weight: 750;
letter-spacing: 0.18em;
}
.brand p {
margin: 5px 0 0;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.live {
display: inline-flex;
align-items: center;
gap: 7px;
margin-left: 10px;
padding: 5px 7px;
border: 1px solid rgba(115, 228, 157, 0.25);
color: var(--green);
background: rgba(115, 228, 157, 0.07);
font-size: 9px;
letter-spacing: 0.12em;
vertical-align: middle;
}
.live::before {
content: "";
width: 5px;
height: 5px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 10px var(--green);
}
.panel {
position: absolute;
top: 22px;
right: 22px;
width: 310px;
padding: 19px;
border: 1px solid var(--line);
background: var(--panel);
backdrop-filter: blur(18px);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
pointer-events: auto;
}
.eyebrow {
color: var(--cyan);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.panel h2 {
margin: 7px 0 5px;
font-size: 20px;
letter-spacing: -0.02em;
}
.panel-copy {
margin: 0 0 17px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.targets {
display: grid;
gap: 8px;
}
.target-card {
width: 100%;
display: grid;
grid-template-columns: 31px 1fr auto;
gap: 10px;
align-items: center;
padding: 11px;
color: var(--text);
text-align: left;
border: 1px solid rgba(135, 190, 200, 0.12);
background: rgba(255, 255, 255, 0.025);
cursor: pointer;
transition: 160ms ease;
}
.target-card:hover,
.target-card.active {
border-color: rgba(66, 217, 223, 0.55);
background: rgba(66, 217, 223, 0.07);
transform: translateX(-2px);
}
.target-card.rejected {
border-color: rgba(255, 102, 116, 0.65);
background: rgba(255, 102, 116, 0.08);
}
.target-number {
color: var(--muted);
font: 700 10px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.target-name {
display: block;
font-size: 12px;
font-weight: 700;
letter-spacing: 0.03em;
}
.target-meta {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.04em;
}
.badge {
padding: 5px 6px;
border: 1px solid currentColor;
color: var(--cyan);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.1em;
}
.badge.limit { color: var(--amber); }
.badge.bad { color: var(--red); }
.actions {
display: flex;
gap: 8px;
margin-top: 14px;
}
.button {
flex: 1;
padding: 10px;
border: 1px solid rgba(135, 190, 200, 0.22);
color: var(--text);
background: rgba(255, 255, 255, 0.035);
cursor: pointer;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.09em;
text-transform: uppercase;
transition: 150ms ease;
}
.button:hover {
border-color: var(--cyan);
color: var(--cyan);
background: rgba(66, 217, 223, 0.07);
}
.hint {
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid var(--line);
color: #5f7b85;
font-size: 10px;
line-height: 1.5;
}
.telemetry {
position: absolute;
left: 26px;
bottom: 25px;
width: 245px;
padding: 15px 16px;
border: 1px solid var(--line);
background: rgba(8, 20, 28, 0.78);
backdrop-filter: blur(14px);
}
.telemetry-title {
display: flex;
justify-content: space-between;
margin-bottom: 13px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.joint {
margin-top: 11px;
}
.joint-row {
display: flex;
justify-content: space-between;
margin-bottom: 6px;
font-size: 10px;
}
.joint-name { color: #a8bcc2; }
.joint-value { color: var(--cyan); font-family: ui-monospace, monospace; }
.joint-track {
position: relative;
height: 4px;
overflow: hidden;
background: rgba(255, 255, 255, 0.08);
}
.joint-track i {
position: absolute;
inset: 0 auto 0 0;
width: 0;
background: linear-gradient(90deg, #237f91, var(--cyan));
box-shadow: 0 0 12px rgba(66, 217, 223, 0.45);
transition: width 80ms linear;
}
.status {
position: absolute;
left: 50%;
bottom: 27px;
width: min(430px, calc(100vw - 620px));
min-width: 300px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 12px;
padding: 13px 15px;
border: 1px solid rgba(66, 217, 223, 0.25);
background: rgba(8, 20, 28, 0.84);
backdrop-filter: blur(14px);
box-shadow: 0 14px 45px rgba(0, 0, 0, 0.3);
transition: 180ms ease;
}
.status.rejected {
border-color: rgba(255, 102, 116, 0.65);
background: rgba(37, 12, 18, 0.9);
}
.status.complete {
border-color: rgba(115, 228, 157, 0.45);
}
.status-lamp {
flex: 0 0 auto;
width: 9px;
height: 9px;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
}
.status.rejected .status-lamp {
background: var(--red);
box-shadow: 0 0 12px var(--red);
animation: blink 0.5s infinite alternate;
}
.status.complete .status-lamp {
background: var(--green);
box-shadow: 0 0 12px var(--green);
}
.status strong {
display: block;
margin-bottom: 3px;
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.status span {
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
@keyframes blink {
to { opacity: 0.25; }
}
@media (max-width: 900px) {
.panel {
top: auto;
right: 12px;
bottom: 12px;
width: 270px;
padding: 14px;
}
.panel-copy, .hint { display: none; }
.panel h2 { font-size: 16px; }
.targets { gap: 5px; }
.target-card { padding: 8px; }
.telemetry { left: 12px; bottom: 12px; width: 205px; }
.status {
left: 12px;
bottom: 144px;
transform: none;
width: 260px;
min-width: 0;
}
.brand { left: 14px; top: 14px; }
}
@media (max-width: 560px) {
.panel {
left: 10px;
right: 10px;
width: auto;
}
.telemetry { display: none; }
.status {
left: 10px;
right: 10px;
bottom: 154px;
width: auto;
}
.brand p { display: none; }
}
</style>
</head>
<body>
<div id="viewport"></div>
<div class="hud">
<div class="brand">
<div class="brand-icon">V</div>
<div>
<h1>VOXEL / PICK & PLACE <span class="live">LIVE</span></h1>
<p>Joint-limited inverse kinematics</p>
</div>
</div>
<section class="panel">
<div class="eyebrow">Placement command</div>
<h2>Select a shelf target</h2>
<p class="panel-copy">
Valid requests are solved against the robot's reach envelope and joint limits.
Unreachable requests are rejected without moving the payload.
</p>
<div class="targets">
<button class="target-card" data-target="mid">
<span class="target-number">01</span>
<span>
<span class="target-name">SHELF A–02</span>
<span class="target-meta">Middle bay · within reach</span>
</span>
<span class="badge">READY</span>
</button>
<button class="target-card" data-target="high">
<span class="target-number">02</span>
<span>
<span class="target-name">SHELF B–01</span>
<span class="target-meta">Upper bay · beyond reach</span>
</span>
<span class="badge limit">LIMIT</span>
</button>
<button class="target-card" data-target="far">
<span class="target-number">03</span>
<span>
<span class="target-name">SHELF C–01</span>
<span class="target-meta">Far bay · outside workspace</span>
</span>
<span class="badge limit">LIMIT</span>
</button>
</div>
<div class="actions">
<button class="button" id="reset">Reset demo</button>
</div>
<div class="hint">
Click a glowing target in the scene or use the command list.
Drag to orbit and use the wheel to zoom.
</div>
</section>
<section class="telemetry">
<div class="telemetry-title">
<span>Joint telemetry</span>
<span>IK / 2R</span>
</div>
<div class="joint">
<div class="joint-row">
<span class="joint-name">J1 · SHOULDER</span>
<span class="joint-value" id="j1Value">0.0°</span>
</div>
<div class="joint-track"><i id="j1Bar"></i></div>
</div>
<div class="joint">
<div class="joint-row">
<span class="joint-name">J2 · ELBOW</span>
<span class="joint-value" id="j2Value">0.0°</span>
</div>
<div class="joint-track"><i id="j2Bar"></i></div>
</div>
</section>
<section class="status" id="status">
<div class="status-lamp"></div>
<div>
<strong id="statusTitle">Awaiting placement command</strong>
<span id="statusDetail">Robot ready. Select a shelf target to begin.</span>
</div>
</section>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------------
// Renderer, scene, camera, and direct camera controls
// ---------------------------------------------------------------------------
const viewport = document.getElementById("viewport");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071018);
scene.fog = new THREE.FogExp2(0x071018, 0.034);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
viewport.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const orbit = {
target: new THREE.Vector3(1.0, 2.0, 0),
theta: 0.47,
phi: 1.13,
radius: 8.7
};
function updateCamera() {
orbit.phi = THREE.MathUtils.clamp(orbit.phi, 0.48, 1.46);
orbit.radius = THREE.MathUtils.clamp(orbit.radius, 5.2, 14);
camera.position.set(
orbit.target.x + orbit.radius * Math.sin(orbit.phi) * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * Math.sin(orbit.phi) * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
updateCamera();
let pointerDown = null;
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerDown = {
x: event.clientX,
y: event.clientY,
moved: false
};
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const dx = event.clientX - pointerDown.x;
const dy = event.clientY - pointerDown.y;
if (Math.abs(dx) + Math.abs(dy) > 5) pointerDown.moved = true;
orbit.theta -= dx * 0.006;
orbit.phi -= dy * 0.005;
pointerDown.x = event.clientX;
pointerDown.y = event.clientY;
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (pointerDown && !pointerDown.moved) pickSceneTarget(event);
pointerDown = null;
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = null;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius *= 1 + Math.sign(event.deltaY) * 0.08;
updateCamera();
},
{ passive: false }
);
// ---------------------------------------------------------------------------
// Lighting and construction helpers
// ---------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xbfefff, 0x09121a, 2.0));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(3.5, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x39d9df, 18, 9, 2);
rimLight.position.set(-2.2, 3.8, 3);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffbd67, 10, 7, 2);
warmLight.position.set(2.5, 3.2, 2.3);
scene.add(warmLight);
const outlineMaterial = new THREE.LineBasicMaterial({
color: 0x071018,
transparent: true,
opacity: 0.72
});
function addBox(parent, size, position, color, options = {}) {
const geometry = new THREE.BoxGeometry(...size);
const material =
options.material ||
new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.72,
metalness: options.metalness ?? 0.08,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(...position);
mesh.castShadow = options.castShadow ?? true;
mesh.receiveShadow = options.receiveShadow ?? true;
parent.add(mesh);
if (options.edges !== false) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
options.edgeMaterial || outlineMaterial
);
mesh.add(edges);
}
return mesh;
}
function createTextSprite(text, color = "#dff8fa", width = 512, height = 128) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const context = canvas.getContext("2d");
context.clearRect(0, 0, width, height);
context.fillStyle = "rgba(6, 18, 25, 0.92)";
context.fillRect(0, 0, width, height);
context.strokeStyle = color;
context.lineWidth = 5;
context.strokeRect(3, 3, width - 6, height - 6);
context.fillStyle = color;
context.font = `700 ${Math.floor(height * 0.38)}px ui-monospace, monospace`;
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, width / 2, height / 2 + 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(1.6, 0.4, 1);
return sprite;
}
// ---------------------------------------------------------------------------
// Industrial voxel environment
// ---------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(22, 16),
new THREE.MeshStandardMaterial({
color: 0x0b1821,
roughness: 0.94,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.035;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(20, 40, 0x315766, 0x18303a);
grid.position.y = -0.025;
grid.material.transparent = true;
grid.material.opacity = 0.38;
scene.add(grid);
addBox(scene, [11.5, 6.2, 0.18], [1.1, 3.0, -1.42], 0x0c1a23);
addBox(scene, [10.8, 0.12, 0.08], [1.1, 4.75, -1.29], 0x203945, {
edges: false,
castShadow: false
});
for (let i = 0; i < 7; i++) {
addBox(
scene,
[0.035, 4.7, 0.05],
[-3.6 + i * 1.55, 2.5, -1.28],
0x18313d,
{ edges: false, castShadow: false }
);
}
// Robot pickup pedestal.
addBox(scene, [0.78, 0.28, 0.78], [-0.05, 0.14, 0.35], 0x193542);
addBox(scene, [0.66, 0.06, 0.66], [-0.05, 0.31, 0.35], 0x2b7180);
addBox(scene, [0.52, 0.018, 0.52], [-0.05, 0.35, 0.35], 0x45c7ce, {
edges: false,
castShadow: false
});
// Voxel shelving.
const shelfX = 1.75;
const shelfWidth = 4.2;
addBox(scene, [shelfWidth, 0.14, 1.1], [shelfX, 1.72, -0.15], 0x304754);
addBox(scene, [shelfWidth, 0.14, 1.1], [shelfX, 3.87, -0.15], 0x304754);
addBox(scene, [shelfWidth, 0.08, 0.1], [shelfX, 1.82, 0.43], 0x54d7dc, {
edges: false,
castShadow: false
});
addBox(scene, [shelfWidth, 0.08, 0.1], [shelfX, 3.97, 0.43], 0x54d7dc, {
edges: false,
castShadow: false
});
for (const x of [-0.42, 1.72, 3.91]) {
addBox(scene, [0.18, 4.55, 0.84], [x, 2.25, -0.25], 0x253d49);
addBox(scene, [0.25, 0.15, 0.94], [x, 0.075, -0.25], 0x152b36);
}
addBox(scene, [4.2, 0.12, 0.12], [shelfX, 2.78, -0.72], 0x49616c);
addBox(scene, [4.2, 0.12, 0.12], [shelfX, 4.92, -0.72], 0x49616c);
const labelA = createTextSprite("A–02", "#42d9df");
labelA.position.set(0.55, 1.48, 0.67);
labelA.scale.set(0.7, 0.18, 1);
scene.add(labelA);
const labelB = createTextSprite("B–01", "#ffc35c");
labelB.position.set(0.55, 3.63, 0.67);
labelB.scale.set(0.7, 0.18, 1);
scene.add(labelB);
const labelC = createTextSprite("C–01", "#ff9b62");
labelC.position.set(3.05, 1.48, 0.67);
labelC.scale.set(0.7, 0.18, 1);
scene.add(labelC);
// ---------------------------------------------------------------------------
// Robot: two revolute joints, voxel links, and a two-finger gripper
// ---------------------------------------------------------------------------
const ROBOT_X = -1.35;
const SHOULDER_Y = 1.25;
const L1 = 1.55;
const L2 = 1.35;
const CARRY_OFFSET = 0.40;
const J1_MIN = -20;
const J1_MAX = 90;
const J2_MIN = -145;
const J2_MAX = 125;
const robot = new THREE.Group();
robot.position.set(ROBOT_X, 0, 0.35);
scene.add(robot);
addBox(robot, [1.4, 0.28, 1.35], [0, 0.14, 0], 0x142a36);
addBox(robot, [1.12, 0.28, 1.08], [0, 0.4, 0], 0x24798a);
addBox(robot, [0.72, 0.75, 0.72], [0, 0.83, 0], 0xc7d5d8);
addBox(robot, [0.82, 0.16, 0.82], [0, 1.18, 0], 0x2a4654);
addBox(robot, [0.55, 0.52, 0.75], [0, 1.25, 0], 0x91a9af);
const beaconMaterial = new THREE.MeshStandardMaterial({
color: 0x42d9df,
emissive: 0x42d9df,
emissiveIntensity: 1.6,
roughness: 0.3
});
const beacon = addBox(
robot,
[0.15, 0.15, 0.15],
[0.46, 1.38, -0.32],
0x42d9df,
{ material: beaconMaterial }
);
const shoulderPivot = new THREE.Group();
shoulderPivot.position.set(0, SHOULDER_Y, 0);
robot.add(shoulderPivot);
addBox(
shoulderPivot,
[0.38, 0.42, 0.48],
[0, 0, 0],
0x42d9df
);
const segmentCount = 6;
const segmentLength = L1 / segmentCount;
for (let i = 0; i < segmentCount; i++) {
addBox(
shoulderPivot,
[segmentLength * 0.91, 0.25, 0.3],
[(i + 0.5) * segmentLength, 0, 0],
i % 2 === 0 ? 0xd4dfe1 : 0xa9bcc1
);
}
const elbowPivot = new THREE.Group();
elbowPivot.position.set(L1, 0, 0);
shoulderPivot.add(elbowPivot);
addBox(elbowPivot, [0.36, 0.4, 0.46], [0, 0, 0], 0x2b7080);
for (let i = 0; i < 5; i++) {
addBox(
elbowPivot,
[(L2 / 5) * 0.91, 0.21, 0.26],
[(i + 0.5) * (L2 / 5), 0, 0],
i % 2 === 0 ? 0xc7d5d8 : 0x8ea7ae
);
}
const wrist = new THREE.Group();
wrist.position.set(L2, 0, 0);
elbowPivot.add(wrist);
addBox(wrist, [0.23, 0.37, 0.38], [0.06, 0, 0], 0xffb653);
const upperFinger = new THREE.Group();
const lowerFinger = new THREE.Group();
wrist.add(upperFinger, lowerFinger);
addBox(upperFinger, [0.39, 0.1, 0.43], [0.3, 0.17, 0], 0xe2ebec);
addBox(lowerFinger, [0.39, 0.1, 0.43], [0.3, -0.17, 0], 0xe2ebec);
addBox(upperFinger, [0.12, 0.13, 0.47], [0.43, 0.17, 0], 0x2b4654);
addBox(lowerFinger, [0.12, 0.13, 0.47], [0.43, -0.17, 0], 0x2b4654);
// ---------------------------------------------------------------------------
// Payload
// ---------------------------------------------------------------------------
const payload = new THREE.Group();
payload.position.set(-0.05, 0.56, 0.35);
const payloadMaterial = new THREE.MeshStandardMaterial({
color: 0xffc857,
roughness: 0.58,
metalness: 0.04,
emissive: 0x000000,
emissiveIntensity: 0
});
addBox(payload, [0.44, 0.44, 0.44], [0, 0, 0], 0xffc857, {
material: payloadMaterial
});
addBox(payload, [0.46, 0.075, 0.46], [0, 0.13, 0], 0xb76a27);
addBox(payload, [0.075, 0.46, 0.46], [0.13, 0, 0], 0xb76a27);
addBox(payload, [0.1, 0.1, 0.1], [-0.14, 0.27, 0.14], 0xffe2a0);
addBox(payload, [0.1, 0.1, 0.1], [0.14, 0.27, 0.14], 0xffe2a0);
scene.add(payload);
// ---------------------------------------------------------------------------
// Targets and rejection visuals
// ---------------------------------------------------------------------------
const targets = {
mid: {
name: "SHELF A–02",
position: new THREE.Vector3(0.55, 2.02, 0.37),
color: 0x42d9df,
valid: true
},
high: {
name: "SHELF B–01",
position: new THREE.Vector3(0.55, 4.17, 0.37),
color: 0xffc35c,
valid: false
},
far: {
name: "SHELF C–01",
position: new THREE.Vector3(3.05, 2.02, 0.37),
color: 0xff9b62,
valid: false
}
};
const markers = [];
const targetMeshes = [];
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function createTargetMarker(key, target) {
const group = new THREE.Group();
group.position.copy(target.position);
scene.add(group);
const material = new THREE.MeshStandardMaterial({
color: target.color,
emissive: target.color,
emissiveIntensity: 0.35,
roughness: 0.4,
metalness: 0.12
});
const corner = 0.34;
const thickness = 0.075;
const length = 0.25;
for (const x of [-corner, corner]) {
for (const y of [-corner, corner]) {
addBox(
group,
[thickness, length, 0.09],
[x, y - Math.sign(y) * length * 0.28, 0],
target.color,
{ material, edges: false }
);
addBox(
group,
[length, thickness, 0.09],
[x - Math.sign(x) * length * 0.28, y, 0],
target.color,
{ material, edges: false }
);
}
}
addBox(group, [0.11, 0.11, 0.1], [0, 0, 0], target.color, {
material,
edges: false
});
const hitbox = new THREE.Mesh(
new THREE.BoxGeometry(0.9, 0.9, 0.25),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false
})
);
hitbox.userData.targetKey = key;
group.add(hitbox);
targetMeshes.push(hitbox);
const marker = {
key,
group,
material,
baseColor: target.color,
selected: false,
rejected: false
};
markers.push(marker);
}
for (const [key, target] of Object.entries(targets)) {
createTargetMarker(key, target);
}
const warningSprite = createTextSprite("OUT OF REACH", "#ff6674");
warningSprite.scale.set(2.2, 0.55, 1);
warningSprite.visible = false;
scene.add(warningSprite);
const rejectLine = new THREE.Line(
new THREE.BufferGeometry(),
new THREE.LineDashedMaterial({
color: 0xff6674,
transparent: true,
opacity: 0,
dashSize: 0.16,
gapSize: 0.11
})
);
rejectLine.visible = false;
scene.add(rejectLine);
function refreshMarkers() {
for (const marker of markers) {
let color = marker.baseColor;
if (marker.selected) color = marker.rejected ? 0xff6674 : 0x73e49d;
marker.material.color.setHex(color);
marker.material.emissive.setHex(color);
marker.material.emissiveIntensity = marker.selected ? 0.75 : 0.27;
}
}
function pickSceneTarget(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(targetMeshes, false);
if (intersections.length) {
requestTarget(intersections[0].object.userData.targetKey);
}
}
// ---------------------------------------------------------------------------
// Inverse kinematics and joint-limit validation
// ---------------------------------------------------------------------------
function solveCarryTarget(position) {
const dx = position.x - ROBOT_X;
const dy = position.y - SHOULDER_Y;
const distance = Math.hypot(dx, dy);
// The payload center is CARRY_OFFSET beyond the physical wrist.
const effectiveSecondLink = L2 + CARRY_OFFSET;
const maximumReach = L1 + effectiveSecondLink;
if (distance > maximumReach + 0.0001) {
return {
ok: false,
distance,
maximumReach,
reason:
`Target is ${(distance - maximumReach).toFixed(2)} m beyond the ` +
`${maximumReach.toFixed(2)} m reach envelope.`
};
}
const cosineElbow = THREE.MathUtils.clamp(
(distance * distance - L1 * L1 - effectiveSecondLink * effectiveSecondLink) /
(2 * L1 * effectiveSecondLink),
-1,
1
);
const elbowMagnitude = Math.acos(cosineElbow);
const candidates = [];
for (const elbow of [-elbowMagnitude, elbowMagnitude]) {
const shoulder =
Math.atan2(dy, dx) -
Math.atan2(
effectiveSecondLink * Math.sin(elbow),
L1 + effectiveSecondLink * Math.cos(elbow)
);
candidates.push({ shoulder, elbow });
}
const valid = candidates.find(
(candidate) =>
candidate.shoulder >= J1_MIN &&
candidate.shoulder <= J1_MAX &&
candidate.elbow >= J2_MIN &&
candidate.elbow <= J2_MAX
);
if (!valid) {
const best = candidates[0];
return {
ok: false,
distance,
maximumReach,
reason:
`Required angles exceed the allowed J1 [${J1_MIN}°, ${J1_MAX}°] ` +
`or J2 [${J2_MIN}°, ${J2_MAX}°] ranges.`
};
}
return {
ok: true,
shoulder: valid.shoulder,
elbow: valid.elbow,
distance,
maximumReach
};
}
// ---------------------------------------------------------------------------
// State machine: pickup -> close gripper -> transfer -> release
// ---------------------------------------------------------------------------
const currentAngles = [22, -38];
const homeAngles = [22, -38];
const pickupPosition = new THREE.Vector3(-0.05, 0.56, 0.35);
let phase = "idle";
let phaseStart = 0;
let phaseDuration = 0;
let fromAngles = [...currentAngles];
let toAngles = [...currentAngles];
let targetSolution = null;
let activeTargetKey = null;
let rejectionUntil = 0;
let gripperOpen = 1;
let completed = false;
let hasInteracted = false;
const statusElement = document.getElementById("status");
const statusTitle = document.getElementById("statusTitle");
const statusDetail = document.getElementById("statusDetail");
const j1Value = document.getElementById("j1Value");
const j2Value = document.getElementById("j2Value");
const j1Bar = document.getElementById("j1Bar");
const j2Bar = document.getElementById("j2Bar");
function setStatus(title, detail, type = "") {
statusTitle.textContent = title;
statusDetail.textContent = detail;
statusElement.className = `status ${type}`.trim();
}
function updateTelemetry() {
const j1 = THREE.MathUtils.radToDeg(currentAngles[0]);
const j2 = THREE.MathUtils.radToDeg(currentAngles[1]);
j1Value.textContent = `${j1.toFixed(1)}°`;
j2Value.textContent = `${j2.toFixed(1)}°`;
j1Bar.style.width = `${((j1 - J1_MIN) / (J1_MAX - J1_MIN)) * 100}%`;
j2Bar.style.width = `${((j2 - J2_MIN) / (J2_MAX - J2_MIN)) * 100}%`;
}
function beginPhase(name, destination, duration) {
phase = name;
phaseStart = performance.now();
phaseDuration = duration;
fromAngles = [...currentAngles];
toAngles = destination;
}
function requestTarget(key) {
hasInteracted = true;
if (phase !== "idle" && phase !== "done") {
setStatus(
"Motion sequence active",
"Wait for the current placement sequence to finish, or reset the demo.",
""
);
return;
}
if (completed) resetScene(false);
const target = targets[key];
const solution = solveCarryTarget(target.position);
activeTargetKey = key;
for (const marker of markers) {
marker.selected = marker.key === key;
marker.rejected = false;
}
document.querySelectorAll(".target-card").forEach((card) => {
card.classList.toggle("active", card.dataset.target === key);
card.classList.remove("rejected");
});
refreshMarkers();
if (!solution.ok) {
rejectTarget(key, solution.reason);
return;
}
targetSolution = solution;
completed = false;
setStatus(
"Target accepted",
`${target.name} passed reach and joint-limit checks. Moving to payload.`,
""
);
const pickupSolution = solveCarryTarget(pickupPosition);
beginPhase("pickup", [pickupSolution.shoulder, pickupSolution.elbow], 1200);
}
function rejectTarget(key, reason) {
const target = targets[key];
for (const marker of markers) {
marker.rejected = marker.key === key;
}
refreshMarkers();
const card = document.querySelector(`[data-target="${key}"]`);
if (card) {
card.classList.add("rejected");
const badge = card.querySelector(".badge");
badge.classList.add("bad");
badge.textContent = "REJECT";
}
warningSprite.position.copy(target.position).add(new THREE.Vector3(0, 0.75, 0.25));
warningSprite.visible = true;
rejectionUntil = performance.now() + 2100;
rejectLine.geometry.dispose();
rejectLine.geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(ROBOT_X, SHOULDER_Y, 0.38),
target.position
]);
rejectLine.computeLineDistances();
rejectLine.visible = true;
setStatus(
"Target rejected",
reason,
"rejected"
);
}
function completePickup() {
wrist.attach(payload);
payload.position.set(CARRY_OFFSET, 0, 0);
payload.rotation.set(0, 0, 0);
beginPhase("grip", [...currentAngles], 520);
setStatus(
"Payload secured",
"Gripper closed. Computing a collision-free transfer to the selected shelf.",
""
);
}
function completeGrip() {
beginPhase(
"transfer",
[targetSolution.shoulder, targetSolution.elbow],
1850
);
setStatus(
"Transferring payload",
`${targets[activeTargetKey].name} · J1 ${THREE.MathUtils.radToDeg(
targetSolution.shoulder
).toFixed(1)}° · J2 ${THREE.MathUtils.radToDeg(
targetSolution.elbow
).toFixed(1)}°`,
""
);
}
function completeTransfer() {
scene.attach(payload);
payload.position.copy(targets[activeTargetKey].position);
payload.rotation.set(0, 0, 0);
beginPhase("release", [...currentAngles], 520);
setStatus(
"Placement reached",
"Opening the gripper and settling the payload onto the shelf.",
""
);
}
function completeRelease() {
phase = "done";
completed = true;
setStatus(
"Placement complete",
`${targets[activeTargetKey].name} is occupied. The payload was placed within all joint limits.`,
"complete"
);
}
function resetScene(showStatus = true) {
phase = "idle";
completed = false;
targetSolution = null;
activeTargetKey = null;
rejectionUntil = 0;
gripperOpen = 1;
scene.attach(payload);
payload.position.copy(pickupPosition);
payload.rotation.set(0, 0, 0);
payloadMaterial.emissive.setHex(0x000000);
payloadMaterial.emissiveIntensity = 0;
currentAngles[0] = homeAngles[0];
currentAngles[1] = homeAngles[1];
warningSprite.visible = false;
rejectLine.visible = false;
for (const marker of markers) {
marker.selected = false;
marker.rejected = false;
}
refreshMarkers();
document.querySelectorAll(".target-card").forEach((card) => {
card.classList.remove("active", "rejected");
const badge = card.querySelector(".badge");
badge.classList.remove("bad");
badge.textContent = card.dataset.target === "mid" ? "READY" : "LIMIT";
});
if (showStatus) {
setStatus(
"Awaiting placement command",
"Robot ready. Select a shelf target to begin.",
""
);
}
}
document.querySelectorAll(".target-card").forEach((card) => {
card.addEventListener("click", () => requestTarget(card.dataset.target));
});
document.getElementById("reset").addEventListener("click", () => resetScene(true));
// ---------------------------------------------------------------------------
// Animation loop
// ---------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate(now) {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
if (phase === "pickup" || phase === "transfer") {
const t = THREE.MathUtils.clamp(
(now - phaseStart) / phaseDuration,
0,
1
);
const eased = t * t * (3 - 2 * t);
currentAngles[0] = THREE.MathUtils.lerp(fromAngles[0], toAngles[0], eased);
currentAngles[1] = THREE.MathUtils.lerp(fromAngles[1], toAngles[1], eased);
if (t >= 1) {
if (phase === "pickup") completePickup();
else completeTransfer();
}
}
if (phase === "grip" && now - phaseStart >= phaseDuration) {
completeGrip();
}
if (phase === "release" && now - phaseStart >= phaseDuration) {
completeRelease();
}
let targetGripperOpen = 1;
if (phase === "grip" || phase === "transfer") targetGripperOpen = 0;
if (phase === "release") targetGripperOpen = 1;
gripperOpen += (targetGripperOpen - gripperOpen) * 0.18;
upperFinger.position.y = 0.17 + gripperOpen * 0.17;
lowerFinger.position.y = -0.17 - gripperOpen * 0.17;
const shaking = now < rejectionUntil;
const shake = shaking
? Math.sin((rejectionUntil - now) * 0.045) * 0.045
: 0;
shoulderPivot.rotation.z = currentAngles[0] + shake;
elbowPivot.rotation.z = currentAngles[1] - shake * 0.6;
if (shaking) {
beaconMaterial.color.setHex(0xff6674);
beaconMaterial.emissive.setHex(0xff6674);
beaconMaterial.emissiveIntensity = 2.3;
payloadMaterial.emissive.setHex(0xff3344);
payloadMaterial.emissiveIntensity = 0.45 + Math.abs(shake) * 3;
rejectLine.material.opacity = 0.55 + Math.sin(elapsed * 13) * 0.25;
warningSprite.material.opacity = 0.75 + Math.sin(elapsed * 8) * 0.25;
warningSprite.position.y = targets[activeTargetKey].position.y + 0.75 + Math.sin(elapsed * 5) * 0.05;
} else {
beaconMaterial.color.setHex(0x42d9df);
beaconMaterial.emissive.setHex(0x42d9df);
beaconMaterial.emissiveIntensity = 1.5 + Math.sin(elapsed * 3) * 0.25;
payloadMaterial.emissive.setHex(0x000000);
payloadMaterial.emissiveIntensity = 0;
rejectLine.visible = false;
warningSprite.visible = false;
}
for (const marker of markers) {
const pulse = marker.selected ? 1 + Math.sin(elapsed * 5) * 0.045 : 1;
marker.group.scale.setScalar(pulse);
}
updateTelemetry();
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));
});
resetScene(false);
requestAnimationFrame(animate);
// Start with the valid placement so the scene demonstrates the complete motion.
setTimeout(() => {
if (!hasInteracted && phase === "idle") requestTarget("mid");
}, 1300);
</script>
</body>
</html>
```