Prompt specification
Build or customize the Three.js 3D scene "Workshop Character Uses a Simple Lever Tool" 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: Workshop Character Uses a Simple Lever Tool
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/workshop-character-uses-a-simple-lever-tool-x3731
Scene Description Prompt:
A voxel repair character operates a hand lever with synchronized hand, handle, pivot, and workpiece motion. Three.js from cdnjs, one HTML file.
Working Three.js Starter (workshop-character-uses-a-simple-lever-tool-x3731):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Repair Bay</title>
<style>
:root {
color-scheme: dark;
--cyan: #73f3dc;
--orange: #ffad42;
--panel: rgba(10, 18, 25, 0.78);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #101820;
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
color: #eafcff;
}
.title-card {
position: absolute;
top: 22px;
left: 22px;
padding: 15px 18px 14px;
border: 1px solid rgba(115, 243, 220, 0.28);
border-left: 4px solid var(--cyan);
background: var(--panel);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(10px);
}
.eyebrow {
margin-bottom: 5px;
color: var(--cyan);
font-size: 10px;
letter-spacing: 0.23em;
}
h1 {
margin: 0;
font-size: clamp(17px, 2.4vw, 25px);
font-weight: 800;
letter-spacing: 0.045em;
}
.subtitle {
margin-top: 7px;
color: #9eb6bf;
font-size: 10px;
letter-spacing: 0.08em;
}
.status-panel {
position: absolute;
top: 22px;
right: 22px;
width: min(255px, calc(100vw - 44px));
padding: 14px 16px;
border: 1px solid rgba(255, 173, 66, 0.28);
background: var(--panel);
backdrop-filter: blur(10px);
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
color: #9eb6bf;
font-size: 10px;
letter-spacing: 0.12em;
}
.status-row strong {
color: var(--orange);
font-size: 11px;
}
.meter {
height: 7px;
margin: 10px 0 6px;
overflow: hidden;
background: #26343d;
}
.meter-fill {
width: 0%;
height: 100%;
background: linear-gradient(90deg, var(--cyan), var(--orange));
box-shadow: 0 0 14px rgba(255, 173, 66, 0.5);
transition: width 70ms linear;
}
.meter-label {
display: flex;
justify-content: space-between;
color: #78909a;
font-size: 9px;
letter-spacing: 0.1em;
}
.controls {
position: absolute;
left: 50%;
bottom: 20px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 12px;
width: max-content;
max-width: calc(100vw - 28px);
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.12);
background: rgba(8, 14, 20, 0.76);
color: #a9bbc3;
font-size: 9px;
letter-spacing: 0.1em;
backdrop-filter: blur(10px);
}
button {
pointer-events: auto;
padding: 6px 9px;
border: 1px solid rgba(115, 243, 220, 0.35);
background: rgba(115, 243, 220, 0.08);
color: var(--cyan);
font: inherit;
font-size: 9px;
letter-spacing: 0.1em;
cursor: pointer;
}
button:hover {
background: rgba(115, 243, 220, 0.18);
}
@media (max-width: 650px) {
.title-card {
top: 12px;
left: 12px;
padding: 12px 14px;
}
.status-panel {
top: auto;
right: 12px;
bottom: 62px;
width: 190px;
}
.subtitle {
display: none;
}
.controls {
bottom: 12px;
white-space: nowrap;
}
}
</style>
</head>
<body>
<div class="hud">
<section class="title-card">
<div class="eyebrow">MAINTENANCE UNIT / BAY 04</div>
<h1>VOXEL REPAIR</h1>
<div class="subtitle">HAND-OPERATED LEVER · SYNCHRONIZED PRESS</div>
</section>
<section class="status-panel">
<div class="status-row">
<span>LEVER STATUS</span>
<strong id="statusText">READY</strong>
</div>
<div class="meter">
<div class="meter-fill" id="meterFill"></div>
</div>
<div class="meter-label">
<span>PRESSURE</span>
<span id="pressureText">0%</span>
</div>
</section>
<div class="controls">
<span>DRAG ORBIT</span>
<span>·</span>
<span>WHEEL / PINCH ZOOM</span>
<span>·</span>
<button id="resetCamera" type="button">RESET VIEW</button>
</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(0x101820);
scene.fog = new THREE.Fog(0x101820, 8, 18);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
40
);
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.12;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel repair workshop. Drag to orbit and use the wheel to zoom."
);
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xb9eaff, 0x182028, 2.2);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffefd2, 3.5);
keyLight.position.set(4.5, 7, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x62dfff, 1.8);
rimLight.position.set(-5, 4, -4);
scene.add(rimLight);
const pressLight = new THREE.PointLight(0xff9a35, 14, 4.2, 2);
pressLight.position.set(0.95, 1.8, 0.1);
scene.add(pressLight);
// -------------------------------------------------------------------------
// Reusable voxel geometry
// -------------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const edgeGeometry = new THREE.EdgesGeometry(unitBox);
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x071015,
transparent: true,
opacity: 0.28
});
const materials = {
floorA: new THREE.MeshStandardMaterial({
color: 0x17242c,
roughness: 0.95
}),
floorB: new THREE.MeshStandardMaterial({
color: 0x1b2a33,
roughness: 0.95
}),
wall: new THREE.MeshStandardMaterial({
color: 0x263640,
roughness: 0.88
}),
wallDark: new THREE.MeshStandardMaterial({
color: 0x17252e,
roughness: 0.9
}),
bench: new THREE.MeshStandardMaterial({
color: 0x52636a,
roughness: 0.75,
metalness: 0.2
}),
benchTop: new THREE.MeshStandardMaterial({
color: 0x889395,
roughness: 0.62,
metalness: 0.32
}),
darkMetal: new THREE.MeshStandardMaterial({
color: 0x26343a,
roughness: 0.5,
metalness: 0.65
}),
steel: new THREE.MeshStandardMaterial({
color: 0xb5c4c7,
roughness: 0.38,
metalness: 0.75
}),
teal: new THREE.MeshStandardMaterial({
color: 0x218f8b,
roughness: 0.72
}),
tealDark: new THREE.MeshStandardMaterial({
color: 0x17565b,
roughness: 0.8
}),
orange: new THREE.MeshStandardMaterial({
color: 0xef7f32,
roughness: 0.67
}),
orangeDark: new THREE.MeshStandardMaterial({
color: 0xa84826,
roughness: 0.8
}),
rubber: new THREE.MeshStandardMaterial({
color: 0x12191e,
roughness: 0.92
}),
skin: new THREE.MeshStandardMaterial({
color: 0xe6ae7b,
roughness: 0.82
}),
shirt: new THREE.MeshStandardMaterial({
color: 0xe86e38,
roughness: 0.78
}),
cream: new THREE.MeshStandardMaterial({
color: 0xf4e6c8,
roughness: 0.85
}),
cyan: new THREE.MeshStandardMaterial({
color: 0x69e4d2,
emissive: 0x123d39,
emissiveIntensity: 0.7,
roughness: 0.45
}),
screen: new THREE.MeshStandardMaterial({
color: 0x12313c,
emissive: 0x0b5d67,
emissiveIntensity: 0.8,
roughness: 0.38
}),
red: new THREE.MeshStandardMaterial({
color: 0xb94b43,
roughness: 0.75
}),
yellow: new THREE.MeshStandardMaterial({
color: 0xf0c64f,
roughness: 0.58
}),
spark: new THREE.MeshStandardMaterial({
color: 0xffc34a,
emissive: 0xff6d13,
emissiveIntensity: 2.5,
roughness: 0.3
})
};
function box(parent, size, position, material, outlined = false) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(size[0], size[1], size[2]);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
if (outlined) {
const edges = new THREE.LineSegments(edgeGeometry, edgeMaterial);
mesh.add(edges);
}
return mesh;
}
const UP = new THREE.Vector3(0, 1, 0);
function positionSegment(mesh, pointA, pointB, width, depth = width) {
const direction = new THREE.Vector3().subVectors(pointB, pointA);
const length = direction.length();
mesh.position.copy(pointA).add(pointB).multiplyScalar(0.5);
mesh.scale.set(width, Math.max(length, 0.001), depth);
if (length > 0.0001) {
direction.normalize();
mesh.quaternion.setFromUnitVectors(UP, direction);
}
}
function smooth01(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
// -------------------------------------------------------------------------
// Workshop floor, walls, shelves, and bench
// -------------------------------------------------------------------------
box(scene, [16, 0.22, 13], [0, -0.16, 0], materials.wallDark);
const tileColors = [materials.floorA, materials.floorB];
for (let x = -5; x <= 5; x++) {
for (let z = -4; z <= 4; z++) {
box(
scene,
[0.91, 0.07, 0.91],
[x * 0.95, -0.025, z * 0.95],
tileColors[(x + z + 20) % 2]
);
}
}
box(scene, [11.5, 5.1, 0.22], [0, 2.35, -2.65], materials.wall);
box(scene, [11.7, 0.18, 0.34], [0, 0.12, -2.5], materials.darkMetal);
box(scene, [11.7, 0.15, 0.34], [0, 4.82, -2.5], materials.darkMetal);
for (let x = -5; x <= 5; x++) {
box(scene, [0.025, 4.5, 0.025], [x, 2.4, -2.51], materials.wallDark);
}
// Back-wall shelving.
box(scene, [2.5, 0.12, 0.48], [-2.0, 3.5, -2.28], materials.bench);
box(scene, [2.5, 0.12, 0.48], [-2.0, 2.48, -2.28], materials.bench);
for (const x of [-3.05, -0.95]) {
box(scene, [0.12, 2.05, 0.12], [x, 2.48, -2.28], materials.darkMetal);
}
// Stored parts and boxes.
box(scene, [0.58, 0.38, 0.36], [-2.72, 3.75, -2.25], materials.orangeDark, true);
box(scene, [0.72, 0.28, 0.38], [-1.73, 3.68, -2.25], materials.teal, true);
box(scene, [0.42, 0.32, 0.35], [-2.63, 2.65, -2.25], materials.red, true);
box(scene, [0.25, 0.3, 0.25], [-1.98, 2.65, -2.25], materials.yellow, true);
box(scene, [0.3, 0.4, 0.3], [-1.56, 2.68, -2.25], materials.steel, true);
// Pegboard tools.
box(scene, [1.65, 1.15, 0.08], [1.12, 3.15, -2.48], materials.bench);
for (let i = 0; i < 7; i++) {
for (let j = 0; j < 5; j++) {
box(
scene,
[0.035, 0.035, 0.025],
[0.45 + i * 0.22, 2.73 + j * 0.2, -2.425],
materials.darkMetal
);
}
}
box(scene, [0.09, 0.68, 0.08], [0.62, 3.14, -2.38], materials.steel);
box(scene, [0.22, 0.09, 0.09], [0.62, 3.48, -2.38], materials.steel);
box(scene, [0.1, 0.58, 0.08], [1.02, 3.13, -2.38], materials.orange);
box(scene, [0.1, 0.4, 0.08], [1.42, 3.0, -2.38], materials.cyan);
// Main bench.
box(scene, [5.8, 0.26, 1.8], [-0.15, 0.95, 0.35], materials.benchTop, true);
for (const x of [-2.55, 2.15]) {
for (const z of [-0.28, 0.96]) {
box(scene, [0.25, 0.88, 0.25], [x, 0.43, z], materials.darkMetal, true);
}
}
box(scene, [5.25, 0.16, 0.18], [-0.2, 0.34, 0.31], materials.darkMetal);
box(scene, [5.25, 0.16, 0.18], [-0.2, 0.34, 0.68], materials.darkMetal);
// Drawers and handles.
for (let i = 0; i < 3; i++) {
const x = -1.45 + i * 1.32;
box(scene, [1.12, 0.45, 0.09], [x, 0.77, 1.28], materials.tealDark, true);
box(scene, [0.35, 0.055, 0.07], [x, 0.8, 1.35], materials.steel);
}
// Safety markings.
for (let i = 0; i < 12; i++) {
box(
scene,
[0.17, 0.12, 0.025],
[-2.48 + i * 0.42, 1.0, 1.265],
i % 2 ? materials.rubber : materials.yellow
);
}
// Diagnostic monitor.
box(scene, [0.1, 0.65, 0.1], [-2.15, 1.42, -0.25], materials.darkMetal);
box(scene, [0.8, 0.52, 0.12], [-2.15, 1.82, -0.25], materials.darkMetal, true);
box(scene, [0.66, 0.38, 0.035], [-2.15, 1.82, -0.175], materials.screen);
box(scene, [0.42, 0.035, 0.025], [-2.23, 1.91, -0.145], materials.cyan);
box(scene, [0.3, 0.035, 0.025], [-2.19, 1.8, -0.145], materials.cyan);
box(scene, [0.48, 0.035, 0.025], [-2.15, 1.69, -0.145], materials.orange);
// Toolbox and spare parts.
box(scene, [0.78, 0.42, 0.52], [-2.25, 1.31, 0.45], materials.red, true);
box(scene, [0.84, 0.08, 0.57], [-2.25, 1.56, 0.45], materials.orangeDark, true);
box(scene, [0.28, 0.08, 0.1], [-2.25, 1.61, 0.45], materials.steel);
for (let i = 0; i < 3; i++) {
box(
scene,
[0.21, 0.21, 0.21],
[1.87 + (i % 2) * 0.27, 1.2 + Math.floor(i / 2) * 0.23, 0.67],
i === 1 ? materials.cyan : materials.yellow,
true
);
}
// -------------------------------------------------------------------------
// Lever pivot and lever
// -------------------------------------------------------------------------
const pivotX = -0.65;
const pivotY = 2.2;
const pivotZ = 0.1;
for (const z of [-0.28, 0.48]) {
box(scene, [0.25, 1.12, 0.17], [pivotX, 1.64, z], materials.tealDark, true);
box(scene, [0.47, 0.18, 0.28], [pivotX, 1.14, z], materials.darkMetal, true);
}
box(scene, [0.22, 0.22, 1.12], [pivotX, pivotY, pivotZ], materials.steel);
const lever = new THREE.Group();
lever.position.set(pivotX, pivotY, pivotZ);
scene.add(lever);
box(lever, [2.35, 0.16, 0.18], [0.82, 0, 0], materials.steel, true);
box(lever, [0.54, 0.4, 0.4], [-0.4, 0, 0], materials.darkMetal, true);
box(lever, [0.48, 0.48, 0.32], [0, 0, 0], materials.orange, true);
box(lever, [0.09, 0.35, 0.35], [0, 0, 0], materials.yellow);
// Orange hand grip, aligned along Z.
box(lever, [0.19, 0.19, 0.72], [1.65, 0, 0], materials.orange, true);
for (let i = -2; i <= 2; i++) {
box(lever, [0.23, 0.12, 0.045], [1.65, 0, i * 0.12], materials.orangeDark);
}
const pivotIndicator = new THREE.Group();
pivotIndicator.position.set(0, 0, 0.19);
lever.add(pivotIndicator);
box(pivotIndicator, [0.36, 0.07, 0.035], [0, 0, 0], materials.cyan);
// -------------------------------------------------------------------------
// Press, guide rails, anvil, and compressible voxel workpiece
// -------------------------------------------------------------------------
const workX = pivotX + 1.65 * Math.cos(0.22);
const anvilTop = 1.22;
box(scene, [1.0, 0.14, 0.82], [workX, 1.15, pivotZ], materials.darkMetal, true);
box(scene, [0.78, 0.06, 0.62], [workX, 1.25, pivotZ], materials.steel);
for (const dx of [-0.46, 0.46]) {
box(scene, [0.1, 1.18, 0.1], [workX + dx, 1.73, pivotZ], materials.steel);
}
box(scene, [1.05, 0.12, 0.25], [workX, 2.3, pivotZ], materials.tealDark, true);
const platen = box(
scene,
[0.82, 0.18, 0.72],
[workX, 1.94, pivotZ],
materials.darkMetal,
true
);
box(platen, [0.66, 0.035, 0.56], [0, 0.1, 0], materials.steel);
const linkage = box(
scene,
[0.14, 0.5, 0.14],
[workX, 2.25, pivotZ],
materials.steel,
true
);
const workpiece = new THREE.Group();
workpiece.position.set(workX, anvilTop, pivotZ);
scene.add(workpiece);
box(workpiece, [0.44, 0.42, 0.44], [0, 0.21, 0], materials.orange, true);
box(workpiece, [0.48, 0.07, 0.48], [0, 0.35, 0], materials.yellow);
box(workpiece, [0.08, 0.43, 0.48], [-0.11, 0.21, 0], materials.orangeDark);
box(workpiece, [0.08, 0.43, 0.48], [0.11, 0.21, 0], materials.orangeDark);
// Voxel sparks emitted near the compression point.
const sparks = [];
for (let i = 0; i < 18; i++) {
const spark = box(
scene,
[0.07, 0.07, 0.07],
[workX, 1.7, pivotZ],
i % 3 === 0 ? materials.spark : materials.yellow
);
spark.visible = false;
sparks.push(spark);
}
// -------------------------------------------------------------------------
// Voxel repair character
// -------------------------------------------------------------------------
const character = new THREE.Group();
character.position.set(2.05, 0, -0.95);
scene.add(character);
// Boots and legs.
for (const x of [-0.23, 0.23]) {
box(character, [0.34, 0.28, 0.5], [x, 0.14, 0.05], materials.rubber, true);
box(character, [0.28, 0.78, 0.31], [x, 0.64, 0], materials.tealDark, true);
box(character, [0.31, 0.12, 0.34], [x, 1.03, 0], materials.teal);
}
box(character, [0.78, 0.28, 0.45], [0, 1.12, 0], materials.tealDark, true);
box(character, [0.8, 0.8, 0.48], [0, 1.64, 0], materials.shirt, true);
// Overalls and straps.
box(character, [0.55, 0.6, 0.08], [0, 1.57, 0.27], materials.teal, true);
box(character, [0.11, 0.72, 0.09], [-0.25, 1.93, 0.27], materials.teal);
box(character, [0.11, 0.72, 0.09], [0.25, 1.93, 0.27], materials.teal);
box(character, [0.82, 0.11, 0.52], [0, 1.4, 0], materials.rubber);
// Tool pouch and wrench on the belt.
box(character, [0.27, 0.34, 0.19], [0.51, 1.27, 0.05], materials.orangeDark, true);
box(character, [0.07, 0.36, 0.08], [0.48, 1.52, 0.2], materials.steel);
// Neck and head.
box(character, [0.25, 0.2, 0.25], [0, 2.08, 0], materials.skin);
const head = new THREE.Group();
head.position.set(0, 2.39, 0);
head.rotation.y = -0.25;
character.add(head);
box(head, [0.72, 0.68, 0.65], [0, 0, 0], materials.skin, true);
box(head, [0.76, 0.13, 0.7], [0, 0.38, 0], materials.tealDark, true);
box(head, [0.8, 0.08, 0.35], [0, 0.34, 0.45], materials.teal, true);
box(head, [0.11, 0.16, 0.1], [-0.43, 0.02, 0], materials.skin);
box(head, [0.11, 0.16, 0.1], [0.43, 0.02, 0], materials.skin);
const leftEye = box(head, [0.075, 0.09, 0.04], [-0.15, 0.08, 0.345], materials.rubber);
const rightEye = box(head, [0.075, 0.09, 0.04], [0.15, 0.08, 0.345], materials.rubber);
box(head, [0.09, 0.09, 0.08], [0, -0.02, 0.38], materials.skin);
box(head, [0.2, 0.045, 0.035], [0, -0.16, 0.346], materials.orangeDark);
// Hair at the back of the cap.
box(head, [0.69, 0.22, 0.09], [0, 0.05, -0.36], materials.rubber);
// Backpack.
box(character, [0.53, 0.62, 0.23], [0.02, 1.77, -0.36], materials.orangeDark, true);
box(character, [0.32, 0.25, 0.1], [0.02, 1.84, -0.51], materials.steel);
// Dynamic arms.
const leftUpperArm = box(scene, [1, 1, 1], [0, 0, 0], materials.shirt, true);
const leftForearm = box(scene, [1, 1, 1], [0, 0, 0], materials.skin, true);
const leftElbowMesh = box(scene, [0.22, 0.22, 0.22], [0, 0, 0], materials.skin, true);
const rightUpperArm = box(scene, [1, 1, 1], [0, 0, 0], materials.shirt, true);
const rightForearm = box(scene, [1, 1, 1], [0, 0, 0], materials.skin, true);
const rightElbowMesh = box(scene, [0.22, 0.22, 0.22], [0, 0, 0], materials.skin, true);
// Left glove follows the lever grip.
const leverHand = new THREE.Group();
scene.add(leverHand);
box(leverHand, [0.23, 0.25, 0.17], [0, 0, 0], materials.rubber, true);
box(leverHand, [0.09, 0.12, 0.18], [0.14, 0.03, 0.08], materials.rubber);
for (let i = -1; i <= 1; i++) {
box(leverHand, [0.065, 0.13, 0.18], [i * 0.07, 0.015, 0.15], materials.rubber);
}
// Right glove and voxel wrench.
const toolHand = new THREE.Group();
scene.add(toolHand);
box(toolHand, [0.22, 0.24, 0.17], [0, 0, 0], materials.rubber, true);
const wrench = new THREE.Group();
wrench.position.set(0, -0.38, 0.02);
wrench.rotation.z = -0.25;
toolHand.add(wrench);
box(wrench, [0.12, 0.72, 0.1], [0, 0, 0], materials.steel, true);
box(wrench, [0.42, 0.12, 0.1], [0.1, 0.38, 0], materials.steel);
box(wrench, [0.12, 0.28, 0.1], [-0.06, 0.5, 0], materials.steel);
box(wrench, [0.12, 0.28, 0.1], [0.26, 0.5, 0], materials.steel);
// -------------------------------------------------------------------------
// Arm IK
// -------------------------------------------------------------------------
function solveElbow(shoulder, target, upperLength, lowerLength, bendHint) {
const direction = new THREE.Vector3().subVectors(target, shoulder);
const distance = THREE.MathUtils.clamp(
direction.length(),
Math.abs(upperLength - lowerLength) + 0.001,
upperLength + lowerLength - 0.001
);
direction.normalize();
const along =
(
upperLength * upperLength -
lowerLength * lowerLength +
distance * distance
) / (2 * distance);
const height = Math.sqrt(
Math.max(0, upperLength * upperLength - along * along)
);
const bend = bendHint.clone();
bend.addScaledVector(direction, -bend.dot(direction));
if (bend.lengthSq() < 0.0001) {
bend.set(0, 0, 1);
bend.addScaledVector(direction, -bend.dot(direction));
}
bend.normalize();
return shoulder
.clone()
.addScaledVector(direction, along)
.addScaledVector(bend, height);
}
const leftShoulder = new THREE.Vector3();
const rightShoulder = new THREE.Vector3();
const leverHandTarget = new THREE.Vector3();
const rightHandTarget = new THREE.Vector3();
const leverAttachWorld = new THREE.Vector3();
const gripLocal = new THREE.Vector3(1.65, 0, -0.18);
const attachLocal = new THREE.Vector3(1.65, 0, 0);
const downHint = new THREE.Vector3(0, -1, 0);
// -------------------------------------------------------------------------
// Direct camera controls: orbit, wheel zoom, and touch pinch
// -------------------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(0.1, 1.5, 0),
theta: 0.58,
phi: 1.08,
radius: 7.7
};
const defaultCamera = {
theta: 0.58,
phi: 1.08,
radius: 7.7
};
function updateCamera() {
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);
}
function resetCamera() {
cameraState.theta = defaultCamera.theta;
cameraState.phi = defaultCamera.phi;
cameraState.radius = defaultCamera.radius;
updateCamera();
}
const pointers = new Map();
let previousPinchDistance = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.focus();
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 2) {
const values = [...pointers.values()];
previousPinchDistance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const current = { x: event.clientX, y: event.clientY };
pointers.set(event.pointerId, current);
if (pointers.size === 1) {
const dx = current.x - previous.x;
const dy = current.y - previous.y;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.005,
0.48,
1.43
);
} else if (pointers.size === 2) {
const values = [...pointers.values()];
const distance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
if (previousPinchDistance > 0 && distance > 0) {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * (previousPinchDistance / distance),
4.2,
11.5
);
}
previousPinchDistance = distance;
}
updateCamera();
});
function releasePointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = 0;
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
4.2,
11.5
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
renderer.domElement.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
cameraState.theta += 0.08;
break;
case "ArrowRight":
cameraState.theta -= 0.08;
break;
case "ArrowUp":
cameraState.phi = Math.max(0.48, cameraState.phi - 0.06);
break;
case "ArrowDown":
cameraState.phi = Math.min(1.43, cameraState.phi + 0.06);
break;
case "+":
case "=":
cameraState.radius = Math.max(4.2, cameraState.radius - 0.35);
break;
case "-":
cameraState.radius = Math.min(11.5, cameraState.radius + 0.35);
break;
case "r":
case "R":
resetCamera();
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
updateCamera();
}
});
document.getElementById("resetCamera").addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Synchronized repair-cycle animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const statusText = document.getElementById("statusText");
const meterFill = document.getElementById("meterFill");
const pressureText = document.getElementById("pressureText");
let lastStatus = "";
function animate() {
const elapsed = clock.getElapsedTime();
const cycleDuration = 6.4;
const phase = (elapsed % cycleDuration) / cycleDuration;
let pressAmount = 0;
let status = "READY";
if (phase >= 0.1 && phase < 0.38) {
pressAmount = smooth01((phase - 0.1) / 0.28);
status = "PULLING";
} else if (phase >= 0.38 && phase < 0.55) {
pressAmount = 1;
status = "HOLD";
} else if (phase >= 0.55 && phase < 0.84) {
pressAmount = 1 - smooth01((phase - 0.55) / 0.29);
status = "RELEASING";
}
// Lever and pivot rotate as one assembly.
const leverAngle = THREE.MathUtils.lerp(0.22, -0.2, pressAmount);
lever.rotation.z = leverAngle;
lever.updateMatrixWorld(true);
// Hand position is sampled directly from the moving grip.
leverHandTarget.copy(gripLocal);
lever.localToWorld(leverHandTarget);
leverAttachWorld.copy(attachLocal);
lever.localToWorld(leverAttachWorld);
// Platen descends with the same normalized press command.
const initialPlatenBottom = 1.85;
const finalPlatenBottom = 1.524;
const platenBottom = THREE.MathUtils.lerp(
initialPlatenBottom,
finalPlatenBottom,
pressAmount
);
// Compression is calculated from actual platen overlap.
const uncompressedTop = anvilTop + 0.42;
const maximumCompression = 0.126;
const compression = THREE.MathUtils.clamp(
(uncompressedTop - platenBottom) / maximumCompression,
0,
1
);
platen.position.set(leverAttachWorld.x, platenBottom + 0.09, pivotZ);
positionSegment(
linkage,
leverAttachWorld,
new THREE.Vector3(
leverAttachWorld.x,
platenBottom + 0.18,
pivotZ
),
0.14,
0.14
);
// The workpiece stays on the anvil while compressing vertically.
workpiece.position.set(leverAttachWorld.x, anvilTop, pivotZ);
workpiece.scale.set(
1 + compression * 0.06,
1 - compression * 0.3,
1 + compression * 0.06
);
const workpieceTop = anvilTop + 0.42 * (1 - compression * 0.3);
// Character motion and IK.
const bodyBob = Math.sin(elapsed * 2.1) * 0.014;
character.position.y = bodyBob;
head.rotation.y = -0.25 + Math.sin(elapsed * 0.72) * 0.035;
leftShoulder.set(
character.position.x - 0.43,
2.08 + bodyBob,
character.position.z + 0.04
);
rightShoulder.set(
character.position.x + 0.43,
2.08 + bodyBob,
character.position.z + 0.04
);
const leftElbow = solveElbow(
leftShoulder,
leverHandTarget,
0.67,
0.67,
downHint
);
positionSegment(
leftUpperArm,
leftShoulder,
leftElbow,
0.24,
0.24
);
positionSegment(
leftForearm,
leftElbow,
leverHandTarget,
0.19,
0.19
);
leftElbowMesh.position.copy(leftElbow);
leftElbowMesh.scale.set(0.22, 0.22, 0.22);
leverHand.position.copy(leverHandTarget);
leverHand.rotation.z = leverAngle;
// The other hand holds a wrench and gently shifts with the repair cycle.
rightHandTarget.set(
character.position.x + 0.53 + pressAmount * 0.035,
1.46 + bodyBob + pressAmount * 0.025,
character.position.z + 0.34
);
const rightElbow = solveElbow(
rightShoulder,
rightHandTarget,
0.65,
0.65,
downHint
);
positionSegment(
rightUpperArm,
rightShoulder,
rightElbow,
0.23,
0.23
);
positionSegment(
rightForearm,
rightElbow,
rightHandTarget,
0.18,
0.18
);
rightElbowMesh.position.copy(rightElbow);
rightElbowMesh.scale.set(0.21, 0.21, 0.21);
toolHand.position.copy(rightHandTarget);
toolHand.rotation.z = -0.12 + pressAmount * 0.08;
// Blink.
const blink = Math.sin(elapsed * 1.9) > 0.985 ? 0.14 : 1;
leftEye.scale.y = 0.09 * blink;
rightEye.scale.y = 0.09 * blink;
// Pivot and press lighting react to load.
materials.cyan.emissiveIntensity = 0.65 + compression * 1.8;
pressLight.intensity = 7 + compression * 28;
pressLight.position.set(leverAttachWorld.x, workpieceTop + 0.2, pivotZ + 0.15);
// Voxel sparks during the active compression stage.
const sparksVisible =
compression > 0.015 &&
phase > 0.24 &&
phase < 0.56;
for (let i = 0; i < sparks.length; i++) {
const spark = sparks[i];
spark.visible = sparksVisible;
if (!spark.visible) continue;
const seedA = Math.sin(i * 91.17) * 43758.5453;
const seedB = Math.sin(i * 47.73 + 1.2) * 24634.6345;
const randomA = seedA - Math.floor(seedA);
const randomB = seedB - Math.floor(seedB);
const life = (elapsed * 1.75 + i * 0.137) % 1;
const side = i % 2 === 0 ? -1 : 1;
spark.position.set(
leverAttachWorld.x +
side * (0.22 + randomA * 0.15) +
side * life * 0.35,
workpieceTop +
0.035 +
life * 0.34 -
life * life * 0.24,
pivotZ + (randomB - 0.5) * 0.5
);
const scale = (1 - life) * 0.075;
spark.scale.setScalar(Math.max(scale, 0.006));
spark.rotation.set(life * 7, life * 5, life * 3);
}
// HUD.
const displayedPressure = Math.round(pressAmount * 100);
meterFill.style.width = `${displayedPressure}%`;
pressureText.textContent = `${displayedPressure}%`;
if (status !== lastStatus) {
statusText.textContent = status;
lastStatus = status;
}
renderer.render(scene, camera);
}
// -------------------------------------------------------------------------
// Resize handling and startup
// -------------------------------------------------------------------------
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
updateCamera();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```