Prompt specification
Build or customize the Three.js 3D scene "Cable payout workbench" 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: Cable payout workbench
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/cable-payout-workbench-x0026
Scene Description Prompt:
A low-poly cable reel unspools toward a workbench lamp while the cable stays continuously connected. Make one HTML scene using Three.js from cdnjs.
Technical checks: Keep the cable physically connected to the lamp throughout payout. Render the plug, cable, and lamp connection as a continuous path, including at intermediate progress values.
Return one complete HTML document that works with the cdnjs Three.js r160 module inside a sandbox with scripts allowed. Make the requested subject visible at the default camera. Use procedural assets and local button handlers. Do not use native form submission, pointer lock, external assets or runtime requests other than Three.js.
Working Three.js Starter (cable-payout-workbench-x0026):
```html
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Carretel de cabo — ligação contínua</title>
<style>
:root {
--ink: #173238;
--muted: #587075;
--paper: rgba(250, 247, 238, 0.92);
--accent: #dd5d3f;
--green: #238567;
--gold: #d7a33d;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d8d1c2;
color: var(--ink);
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
}
.panel {
position: fixed;
z-index: 10;
top: 22px;
left: 22px;
width: min(350px, calc(100vw - 44px));
padding: 20px;
border: 1px solid rgba(23, 50, 56, 0.14);
border-radius: 20px;
background: var(--paper);
box-shadow: 0 18px 50px rgba(35, 48, 48, 0.18);
backdrop-filter: blur(10px);
}
.eyebrow {
margin: 0 0 7px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 25px;
line-height: 1.08;
letter-spacing: -0.035em;
}
.objective {
margin: 11px 0 16px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.progress-wrap {
position: relative;
padding: 13px 0 10px;
border-top: 1px solid rgba(23, 50, 56, 0.12);
border-bottom: 1px solid rgba(23, 50, 56, 0.12);
}
.progress-head {
display: flex;
align-items: baseline;
justify-content: space-between;
margin-bottom: 9px;
}
.progress-head span {
color: var(--muted);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
#percent {
font-size: 25px;
font-weight: 850;
line-height: 1;
font-variant-numeric: tabular-nums;
}
.track {
position: relative;
height: 10px;
overflow: visible;
border-radius: 999px;
background: rgba(23, 50, 56, 0.12);
}
#fill {
position: absolute;
inset: 0 auto 0 0;
width: 30%;
border-radius: inherit;
background: linear-gradient(90deg, var(--gold), var(--accent));
transition: width 80ms linear;
}
.critical-marker {
position: absolute;
z-index: 2;
top: -4px;
left: 30%;
width: 2px;
height: 18px;
background: var(--ink);
opacity: 0.55;
}
.readouts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 12px;
}
.readout {
padding: 9px 10px;
border-radius: 12px;
background: rgba(23, 50, 56, 0.055);
}
.readout small {
display: block;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.readout strong {
display: block;
margin-top: 3px;
font-size: 14px;
font-variant-numeric: tabular-nums;
}
.connection {
display: flex;
align-items: center;
gap: 9px;
margin-top: 13px;
padding: 10px 12px;
border-radius: 12px;
color: #17664f;
background: rgba(35, 133, 103, 0.11);
font-size: 12px;
font-weight: 750;
}
.connection::before {
content: "";
width: 10px;
height: 10px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 5px rgba(35, 133, 103, 0.13);
}
.connection.invalid {
color: #9b3126;
background: rgba(180, 55, 42, 0.12);
}
.connection.invalid::before {
background: #b83c2c;
box-shadow: 0 0 0 5px rgba(184, 60, 44, 0.13);
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 14px;
}
button {
min-height: 42px;
padding: 9px 11px;
border: 0;
border-radius: 12px;
color: var(--ink);
background: rgba(23, 50, 56, 0.09);
font: inherit;
font-size: 12px;
font-weight: 800;
cursor: pointer;
transition: transform 140ms ease, background 140ms ease, box-shadow 140ms ease;
}
button:hover {
transform: translateY(-1px);
background: rgba(23, 50, 56, 0.14);
}
button:focus-visible {
outline: 3px solid rgba(221, 93, 63, 0.35);
outline-offset: 2px;
}
button.primary {
color: #fffaf0;
background: var(--ink);
box-shadow: 0 6px 15px rgba(23, 50, 56, 0.18);
}
button.primary:hover { background: #26484e; }
button.test {
grid-column: 1 / -1;
color: #8b2e22;
background: rgba(221, 93, 63, 0.12);
}
.note {
margin: 10px 0 0;
color: var(--muted);
font-size: 10.5px;
line-height: 1.4;
}
#criticalBadge {
position: fixed;
z-index: 20;
left: 50%;
bottom: 24px;
display: flex;
align-items: center;
gap: 10px;
max-width: calc(100vw - 32px);
padding: 12px 17px;
border: 1px solid rgba(35, 133, 103, 0.26);
border-radius: 999px;
color: #145d48;
background: rgba(244, 255, 248, 0.95);
box-shadow: 0 12px 35px rgba(30, 78, 64, 0.2);
font-size: 12px;
opacity: 0;
transform: translate(-50%, 18px);
transition: opacity 220ms ease, transform 220ms ease;
pointer-events: none;
}
#criticalBadge.show {
opacity: 1;
transform: translate(-50%, 0);
}
#criticalBadge .check {
display: grid;
width: 25px;
height: 25px;
place-items: center;
flex: 0 0 auto;
border-radius: 50%;
color: white;
background: var(--green);
font-weight: 900;
}
#criticalBadge b { white-space: nowrap; }
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
color: var(--ink);
background: #d8d1c2;
font-size: 13px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.hidden { opacity: 0; pointer-events: none; }
@media (max-width: 700px) {
.panel {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 14px;
}
h1 { font-size: 21px; }
.objective { margin-bottom: 10px; }
.readouts { display: none; }
#criticalBadge { bottom: 10px; }
}
</style>
</head>
<body>
<div id="scene"></div>
<section class="panel" aria-label="Controles do ensaio">
<p class="eyebrow">Ensaio de continuidade</p>
<h1>Carretel → luminária de bancada</h1>
<p class="objective">
Verifique que o plugue permanece fisicamente encaixado na luminária e que o
cabo permanece uma peça contínua durante todo o desenrolamento.
</p>
<div class="progress-wrap">
<div class="progress-head">
<span>Desenrolamento</span>
<strong id="percent">30%</strong>
</div>
<div class="track" aria-hidden="true">
<div id="fill"></div>
<div class="critical-marker"></div>
</div>
</div>
<div class="readouts">
<div class="readout">
<small>Voltas no carretel</small>
<strong id="turns">9,8</strong>
</div>
<div class="readout">
<small>Estado da luminária</small>
<strong>Acesa</strong>
</div>
</div>
<div id="connection" class="connection" aria-live="polite">
Ligação física contínua
</div>
<div class="buttons">
<button id="play" class="primary" type="button">▶ Continuar</button>
<button id="reset" type="button">↺ Reiniciar</button>
<button id="test30" class="test" type="button">Testar geometria exatamente em 30%</button>
</div>
<p class="note">
O plugue não é reposicionado pela animação: sua ponta permanece dentro do
soquete, enquanto o cabo entre o carretel e o plugue é reconstruído sem lacunas.
</p>
</section>
<div id="criticalBadge" role="status">
<span class="check">✓</span>
<span><b>Estado crítico de 30%:</b> plugue encaixado e cabo contínuo.</span>
</div>
<div id="loading">Preparando a bancada…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneContainer = document.getElementById("scene");
const percentEl = document.getElementById("percent");
const fillEl = document.getElementById("fill");
const turnsEl = document.getElementById("turns");
const connectionEl = document.getElementById("connection");
const playButton = document.getElementById("play");
const resetButton = document.getElementById("reset");
const testButton = document.getElementById("test30");
const criticalBadge = document.getElementById("criticalBadge");
const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: "high-performance" });
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;
sceneContainer.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd8d1c2);
scene.fog = new THREE.Fog(0xd8d1c2, 17, 31);
const camera = new THREE.PerspectiveCamera(42, window.innerWidth / window.innerHeight, 0.1, 100);
camera.position.set(6.5, 5.7, 11.7);
camera.lookAt(-0.4, 2.15, 0);
const hemi = new THREE.HemisphereLight(0xf8ead1, 0x526b70, 2.25);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xfff0d5, 3.1);
keyLight.position.set(-5, 9, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1536, 1536);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 24;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x8cc7cf, 1.4);
rimLight.position.set(7, 5, -6);
scene.add(rimLight);
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.76,
metalness: 0.03,
flatShading: true,
...options
});
}
const woodA = material(0xb87443);
const woodB = material(0xc58650);
const woodEdge = material(0x684331);
const darkMetal = material(0x25484c, { metalness: 0.32, roughness: 0.5 });
const reelGold = material(0xd59b3e, { metalness: 0.16, roughness: 0.54 });
const lampGreen = material(0x39786f, { metalness: 0.14, roughness: 0.5 });
const plugOrange = material(0xe05c3f, { roughness: 0.55 });
const blackRubber = material(0x17272b, { roughness: 0.88 });
const metal = material(0xa8aaa2, { metalness: 0.68, roughness: 0.35 });
function mesh(geometry, mat, x = 0, y = 0, z = 0, cast = true, receive = true) {
const object = new THREE.Mesh(geometry, mat);
object.position.set(x, y, z);
object.castShadow = cast;
object.receiveShadow = receive;
scene.add(object);
return object;
}
function box(w, h, d, mat, x, y, z) {
return mesh(new THREE.BoxGeometry(w, h, d), mat, x, y, z);
}
function cylinder(radiusTop, radiusBottom, height, sides, mat, x, y, z) {
return mesh(new THREE.CylinderGeometry(radiusTop, radiusBottom, height, sides), mat, x, y, z);
}
function beamBetween(a, b, radius, mat, sides = 8) {
const direction = new THREE.Vector3().subVectors(b, a);
const object = mesh(
new THREE.CylinderGeometry(radius, radius, direction.length(), sides),
mat
);
object.position.copy(a).add(b).multiplyScalar(0.5);
object.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), direction.normalize());
return object;
}
// Oficina e piso.
box(22, 0.18, 18, material(0xbfb7a7), 0, -0.12, 0);
const floor = mesh(new THREE.PlaneGeometry(22, 18), material(0xbfb7a7), 0, -0.02, 0, false, true);
floor.rotation.x = -Math.PI / 2;
// Tampo de madeira, dividido em ripas para manter o estilo low-poly.
const plankDepth = 0.88;
for (let i = 0; i < 5; i++) {
const z = (i - 2) * 0.91;
box(10, 0.40, plankDepth, i % 2 ? woodA : woodB, 0, 1.40, z);
box(10, 0.11, 0.09, woodEdge, 0, 1.40, z + 0.455);
}
box(10.15, 0.46, 0.17, woodEdge, 0, 1.38, 2.34);
box(10.15, 0.46, 0.17, woodEdge, 0, 1.38, -2.34);
for (const x of [-4.2, 4.2]) {
for (const z of [-1.75, 1.75]) {
box(0.48, 1.35, 0.48, darkMetal, x, 0.72, z);
}
}
box(8.8, 0.28, 0.32, darkMetal, 0, 0.62, -1.75);
box(8.8, 0.28, 0.32, darkMetal, 0, 0.62, 1.75);
// Pequenos detalhes da bancada.
for (const x of [-4.65, 4.65]) {
for (const z of [-2.05, 2.05]) {
const bolt = cylinder(0.055, 0.055, 0.025, 8, metal, x, 1.615, z);
bolt.castShadow = false;
}
}
const notebook = box(1.25, 0.055, 0.78, material(0x6f8e88), 0.25, 1.64, -0.85);
notebook.rotation.y = -0.16;
box(1.12, 0.018, 0.65, material(0xe7ddc7), 0.25, 1.68, -0.85).rotation.y = -0.16;
box(0.52, 0.07, 0.07, reelGold, 3.85, 1.67, -1.25).rotation.y = 0.35;
// Carretel low-poly: dois aros, raios, tambor e marcador giratório.
const reelCenter = new THREE.Vector3(-2.65, 2.66, 0.02);
const reelStatic = new THREE.Group();
scene.add(reelStatic);
function addToGroup(object, group, x = 0, y = 0, z = 0) {
object.position.set(x, y, z);
group.add(object);
return object;
}
for (const z of [-0.46, 0.68]) {
addToGroup(
new THREE.Mesh(new THREE.TorusGeometry(1.06, 0.095, 5, 16), reelGold),
reelStatic,
reelCenter.x,
reelCenter.y,
z
);
}
const reelRotator = new THREE.Group();
reelRotator.position.copy(reelCenter);
scene.add(reelRotator);
for (const z of [-0.46, 0.68]) {
for (let i = 0; i < 3; i++) {
const spoke = new THREE.Mesh(new THREE.BoxGeometry(1.95, 0.115, 0.085), darkMetal);
spoke.position.z = z;
spoke.rotation.z = i * Math.PI / 3;
reelRotator.add(spoke);
}
}
const reelHub = new THREE.Mesh(new THREE.CylinderGeometry(0.27, 0.27, 1.22, 12), reelGold);
reelHub.rotation.x = Math.PI / 2;
reelRotator.add(reelHub);
const reelDrum = new THREE.Mesh(new THREE.CylinderGeometry(0.66, 0.66, 0.86, 12), darkMetal);
reelDrum.rotation.x = Math.PI / 2;
reelRotator.add(reelDrum);
const reelMarker = new THREE.Mesh(new THREE.BoxGeometry(0.28, 0.16, 0.12), plugOrange);
reelMarker.position.set(0.78, 0, 0.77);
reelRotator.add(reelMarker);
for (const x of [-3.42, -1.88]) {
box(0.32, 0.20, 1.34, darkMetal, x, 1.69, 0.10);
}
// Luminária articulada.
const lampX = 2.75;
const lampZ = -0.12;
cylinder(0.72, 0.82, 0.18, 10, lampGreen, lampX, 1.69, lampZ);
cylinder(0.62, 0.68, 0.055, 10, blackRubber, lampX, 1.795, lampZ);
const armA = new THREE.Vector3(lampX, 1.82, lampZ);
const armB = new THREE.Vector3(lampX, 3.05, lampZ);
const armC = new THREE.Vector3(2.02, 3.55, lampZ);
const armD = new THREE.Vector3(1.43, 3.96, lampZ);
beamBetween(armA, armB, 0.095, lampGreen);
beamBetween(armB, armC, 0.095, lampGreen);
beamBetween(armC, armD, 0.085, lampGreen);
for (const p of [armB, armC]) {
const joint = new THREE.Mesh(new THREE.IcosahedronGeometry(0.17, 0), reelGold);
joint.position.copy(p);
joint.castShadow = true;
scene.add(joint);
}
const shade = new THREE.Mesh(
new THREE.CylinderGeometry(0.29, 0.72, 0.78, 8, 1, true),
new THREE.MeshStandardMaterial({
color: 0x39786f,
roughness: 0.5,
metalness: 0.12,
flatShading: true,
side: THREE.DoubleSide
})
);
shade.position.set(1.43, 3.65, lampZ);
shade.castShadow = true;
scene.add(shade);
const shadeInner = new THREE.Mesh(
new THREE.CylinderGeometry(0.255, 0.64, 0.70, 8, 1, true),
new THREE.MeshStandardMaterial({
color: 0xffd996,
emissive: 0xffb44d,
emissiveIntensity: 0.72,
roughness: 0.72,
side: THREE.BackSide,
flatShading: true
})
);
shadeInner.position.copy(shade.position);
scene.add(shadeInner);
const glowingDisk = cylinder(0.59, 0.59, 0.035, 16, new THREE.MeshStandardMaterial({
color: 0xffe5a7,
emissive: 0xffb84f,
emissiveIntensity: 2.1,
roughness: 0.5
}), 1.43, 3.275, lampZ);
glowingDisk.castShadow = false;
const bulb = mesh(
new THREE.IcosahedronGeometry(0.16, 1),
new THREE.MeshStandardMaterial({
color: 0xfff3ca,
emissive: 0xffc76a,
emissiveIntensity: 3.2,
roughness: 0.35
}),
1.43, 3.22, lampZ,
false,
false
);
const lampSpot = new THREE.SpotLight(0xffbd69, 34, 9, 0.72, 0.48, 1.25);
lampSpot.position.set(1.43, 3.18, lampZ);
lampSpot.target.position.set(1.10, 1.57, 0.55);
lampSpot.castShadow = true;
lampSpot.shadow.mapSize.set(1024, 1024);
lampSpot.shadow.bias = -0.0004;
scene.add(lampSpot, lampSpot.target);
const lightPool = mesh(
new THREE.CircleGeometry(1.02, 24),
new THREE.MeshBasicMaterial({
color: 0xffc46b,
transparent: true,
opacity: 0.12,
depthWrite: false
}),
1.25, 1.607, 0.42,
false,
false
);
lightPool.rotation.x = -Math.PI / 2;
// Soquete e plugue: o plugue fica dentro do soquete em TODOS os estados.
const SOCKET_REAR_Z = 0.60;
const SOCKET_FRONT_Z = 0.84;
const PLUG_REAR_Z = 0.32;
const PLUG_FRONT_Z = 0.65;
const socket = new THREE.Mesh(new THREE.CylinderGeometry(0.225, 0.225, 0.24, 10), blackRubber);
socket.rotation.x = Math.PI / 2;
socket.position.set(lampX, 1.80, 0.72);
socket.castShadow = true;
scene.add(socket);
const socketMouth = new THREE.Mesh(new THREE.CylinderGeometry(0.145, 0.145, 0.025, 10), material(0x0d1719));
socketMouth.rotation.x = Math.PI / 2;
socketMouth.position.set(lampX, 1.80, 0.845);
scene.add(socketMouth);
const plugBody = new THREE.Mesh(new THREE.CylinderGeometry(0.145, 0.15, 0.33, 8), plugOrange);
plugBody.rotation.x = Math.PI / 2;
plugBody.position.set(lampX, 1.80, 0.485);
plugBody.castShadow = true;
scene.add(plugBody);
const plugTip = new THREE.Mesh(new THREE.CylinderGeometry(0.102, 0.102, 0.20, 8), metal);
plugTip.rotation.x = Math.PI / 2;
plugTip.position.set(lampX, 1.80, 0.67);
scene.add(plugTip);
const plugBoot = new THREE.Mesh(new THREE.CylinderGeometry(0.115, 0.075, 0.13, 8), blackRubber);
plugBoot.rotation.x = Math.PI / 2;
plugBoot.position.set(lampX, 1.80, 0.285);
scene.add(plugBoot);
const connectionRing = new THREE.Mesh(
new THREE.TorusGeometry(0.17, 0.024, 5, 16),
new THREE.MeshStandardMaterial({
color: 0x69e0ae,
emissive: 0x1c9a6c,
emissiveIntensity: 1.35,
roughness: 0.4
})
);
connectionRing.position.set(lampX, 1.80, 0.625);
scene.add(connectionRing);
const guide = new THREE.Mesh(new THREE.TorusGeometry(0.105, 0.025, 5, 12), reelGold);
guide.position.set(-1.50, 2.22, 1.02);
guide.rotation.y = -0.45;
scene.add(guide);
// Rótulo 3D procedural, sem arquivos externos.
function makeLabel(text, accent = "#238567") {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 192;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "rgba(250,247,238,0.96)";
ctx.strokeStyle = "rgba(23,50,56,0.18)";
ctx.lineWidth = 8;
ctx.beginPath();
ctx.roundRect(8, 8, 752, 176, 34);
ctx.fill();
ctx.stroke();
ctx.fillStyle = accent;
ctx.beginPath();
ctx.arc(68, 96, 31, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = "#ffffff";
ctx.font = "900 42px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("✓", 68, 94);
ctx.fillStyle = "#173238";
ctx.font = "800 38px system-ui, sans-serif";
ctx.textAlign = "left";
ctx.fillText(text, 120, 95);
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.65, 0.42, 1);
sprite.renderOrder = 20;
return sprite;
}
const plugLabel = makeLabel("PLUGUE ENCAIXADO");
plugLabel.position.set(2.75, 2.33, 0.88);
scene.add(plugLabel);
// Tubo procedural com topologia fixa: permite reconstruir o cabo sem lacunas.
class DynamicTube {
constructor(maxPoints, radius, radialSegments, mat) {
this.maxPoints = maxPoints;
this.radius = radius;
this.radialSegments = radialSegments;
this.geometry = new THREE.BufferGeometry();
this.positions = new Float32Array(maxPoints * radialSegments * 3);
this.normals = new Float32Array(maxPoints * radialSegments * 3);
const uvs = new Float32Array(maxPoints * radialSegments * 2);
const indices = new Uint16Array((maxPoints - 1) * radialSegments * 6);
let k = 0;
for (let i = 0; i < maxPoints - 1; i++) {
for (let j = 0; j < radialSegments; j++) {
const jn = (j + 1) % radialSegments;
const a = i * radialSegments + j;
const b = i * radialSegments + jn;
const c = (i + 1) * radialSegments + j;
const d = (i + 1) * radialSegments + jn;
indices[k++] = a;
indices[k++] = b;
indices[k++] = d;
indices[k++] = b;
indices[k++] = c;
indices[k++] = d;
}
}
for (let i = 0; i < maxPoints; i++) {
for (let j = 0; j < radialSegments; j++) {
const index = i * radialSegments + j;
uvs[index * 2] = i / (maxPoints - 1);
uvs[index * 2 + 1] = j / radialSegments;
}
}
this.geometry.setAttribute("position", new THREE.BufferAttribute(this.positions, 3).setUsage(THREE.DynamicDrawUsage));
this.geometry.setAttribute("normal", new THREE.BufferAttribute(this.normals, 3).setUsage(THREE.DynamicDrawUsage));
this.geometry.setAttribute("uv", new THREE.BufferAttribute(uvs, 2));
this.geometry.setIndex(new THREE.BufferAttribute(indices, 1));
this.mesh = new THREE.Mesh(this.geometry, mat);
this.mesh.castShadow = true;
this.mesh.receiveShadow = true;
scene.add(this.mesh);
}
update(points) {
const count = points.length;
const r = this.radialSegments;
const tangents = [];
const up = new THREE.Vector3(0, 1, 0);
const xAxis = new THREE.Vector3(1, 0, 0);
let previousNormal = null;
for (let i = 0; i < count; i++) {
const before = points[Math.max(0, i - 1)];
const after = points[Math.min(count - 1, i + 1)];
const tangent = after.clone().sub(before).normalize();
tangents.push(tangent);
let normal;
if (!previousNormal) {
normal = new THREE.Vector3().crossVectors(tangent, up);
if (normal.lengthSq() < 0.00001) normal.crossVectors(tangent, xAxis);
} else {
normal = previousNormal.clone().addScaledVector(tangent, -previousNormal.dot(tangent));
if (normal.lengthSq() < 0.00001) {
normal.crossVectors(tangent, up);
if (normal.lengthSq() < 0.00001) normal.crossVectors(tangent, xAxis);
}
}
normal.normalize();
previousNormal = normal;
}
for (let i = 0; i < count; i++) {
const tangent = tangents[i];
const normal = previousNormal = tangents[i].dot(new THREE.Vector3()) === 0
? normalFromTransport(tangents, i)
: normalFromTransport(tangents, i);
const binormal = new THREE.Vector3().crossVectors(tangent, normal).normalize();
for (let j = 0; j < r; j++) {
const angle = j / r * Math.PI * 2;
const nx = normal.x * Math.cos(angle) + binormal.x * Math.sin(angle);
const ny = normal.y * Math.cos(angle) + binormal.y * Math.sin(angle);
const nz = normal.z * Math.cos(angle) + binormal.z * Math.sin(angle);
const index = (i * r + j) * 3;
this.positions[index] = points[i].x + nx * this.radius;
this.positions[index + 1] = points[i].y + ny * this.radius;
this.positions[index + 2] = points[i].z + nz * this.radius;
this.normals[index] = nx;
this.normals[index + 1] = ny;
this.normals[index + 2] = nz;
}
}
this.geometry.attributes.position.needsUpdate = true;
this.geometry.attributes.normal.needsUpdate = true;
this.geometry.computeBoundingSphere();
}
}
// Transporte paralelo para manter a seção do tubo estável nas curvas.
function normalFromTransport(tangents, index) {
if (index === 0) {
const n = new THREE.Vector3().crossVectors(tangents[0], new THREE.Vector3(0, 1, 0));
if (n.lengthSq() < 0.00001) n.crossVectors(tangents[0], new THREE.Vector3(1, 0, 0));
return n.normalize();
}
const n = normalCache[index - 1].clone();
const tangent = tangents[index];
n.addScaledVector(tangent, -n.dot(tangent));
if (n.lengthSq() < 0.00001) {
n.crossVectors(tangent, new THREE.Vector3(0, 1, 0));
if (n.lengthSq() < 0.00001) n.crossVectors(tangent, new THREE.Vector3(1, 0, 0));
}
return n.normalize();
}
const normalCache = [];
const cable = new DynamicTube(280, 0.055, 7, blackRubber);
let currentCablePoints = [];
let progress = 0.30;
let playing = false;
let criticalTimer = 0;
function buildCableGeometry() {
const coilSamples = 190;
const bridgeSamples = 76;
const points = [];
const turns = 11 - progress * 4;
// A espiral é parte do mesmo tubo que conduz ao plugue.
for (let i = 0; i < coilSamples; i++) {
const q = i / (coilSamples - 1);
const angle = Math.PI * 2 * turns * (1 - q);
const radius = THREE.MathUtils.lerp(1.015, 0.775, q);
points.push(new THREE.Vector3(
reelCenter.x + Math.cos(angle) * radius,
reelCenter.y + Math.sin(angle) * radius,
THREE.MathUtils.lerp(-0.28, 0.58, q)
));
}
const coilEnd = points[points.length - 1].clone();
const bridgeControl = [
coilEnd,
new THREE.Vector3(-1.50, 2.22, 1.02),
new THREE.Vector3(-0.94, 1.77, 1.17),
new THREE.Vector3(-0.28, 1.70 + 0.03 * progress, 1.20 + 0.61 * progress),
new THREE.Vector3(0.53, 1.70 + 0.14 * progress, 1.18 + 0.73 * progress),
new THREE.Vector3(1.34, 1.70 + 0.27 * progress, 1.08 + 0.67 * progress),
new THREE.Vector3(2.08, 1.72 + 0.23 * progress, 0.87 + 0.32 * progress),
new THREE.Vector3(lampX, 1.80, PLUG_REAR_Z)
];
const bridgeCurve = new THREE.CatmullRomCurve3(bridgeControl, false, "centripetal", 0.5);
const bridgePoints = bridgeCurve.getPoints(bridgeSamples);
points.push(...bridgePoints);
// Normais calculadas separadamente para transporte paralelo consistente.
normalCache.length = 0;
const tangents = [];
for (let i = 0; i < points.length; i++) {
const before = points[Math.max(0, i - 1)];
const after = points[Math.min(points.length - 1, i + 1)];
tangents.push(after.clone().sub(before).normalize());
}
let transported = null;
for (let i = 0; i < points.length; i++) {
if (!transported) {
transported = new THREE.Vector3().crossVectors(tangents[i], new THREE.Vector3(0, 1, 0));
if (transported.lengthSq() < 0.00001) {
transported.crossVectors(tangents[i], new THREE.Vector3(1, 0, 0));
}
} else {
transported = transported.clone().addScaledVector(
tangents[i],
-transported.dot(tangents[i])
);
if (transported.lengthSq() < 0.00001) {
transported.crossVectors(tangents[i], new THREE.Vector3(0, 1, 0));
if (transported.lengthSq() < 0.00001) {
transported.crossVectors(tangents[i], new THREE.Vector3(1, 0, 0));
}
}
}
transported.normalize();
normalCache.push(transported.clone());
}
currentCablePoints = points;
cable.update(points);
reelRotator.rotation.z = -progress * Math.PI * 4.2;
const percent = Math.round(progress * 100);
percentEl.textContent = `${percent}%`;
fillEl.style.width = `${percent}%`;
turnsEl.textContent = turns.toFixed(1).replace(".", ",");
validateConnection();
}
function validateConnection() {
const end = currentCablePoints[currentCablePoints.length - 1];
const finite = currentCablePoints.every(p => Number.isFinite(p.x) && Number.isFinite(p.y) && Number.isFinite(p.z));
const reachesPlug = end.distanceTo(new THREE.Vector3(lampX, 1.80, PLUG_REAR_Z)) < 0.002;
const plugEngaged = PLUG_FRONT_Z >= SOCKET_REAR_Z && PLUG_FRONT_Z <= SOCKET_FRONT_Z;
const valid = finite && reachesPlug && plugEngaged;
connectionEl.classList.toggle("invalid", !valid);
connectionEl.textContent = valid
? "Ligação física contínua"
: "Falha geométrica na ligação";
return valid;
}
function showCriticalBadge(duration = 2800) {
clearTimeout(criticalTimer);
criticalBadge.classList.add("show");
criticalTimer = setTimeout(() => criticalBadge.classList.remove("show"), duration);
}
function setProgress(value, showBadge = false) {
progress = THREE.MathUtils.clamp(value, 0, 1);
buildCableGeometry();
if (showBadge && Math.abs(progress - 0.30) < 0.001) showCriticalBadge();
}
playButton.addEventListener("click", () => {
if (progress >= 0.999) {
progress = 0;
buildCableGeometry();
}
playing = !playing;
playButton.textContent = playing ? "Ⅱ Pausar" : "▶ Continuar";
});
resetButton.addEventListener("click", () => {
playing = false;
playButton.textContent = "▶ Continuar";
setProgress(0);
});
testButton.addEventListener("click", () => {
playing = false;
playButton.textContent = "▶ Continuar";
setProgress(0.30, true);
});
setProgress(0.30);
setTimeout(() => showCriticalBadge(3300), 600);
const clock = new THREE.Clock();
let crossedCritical = true;
renderer.setAnimationLoop(() => {
const dt = Math.min(clock.getDelta(), 0.05);
if (playing) {
progress = Math.min(1, progress + dt * 0.055);
buildCableGeometry();
if (!crossedCritical && progress >= 0.30) {
crossedCritical = true;
showCriticalBadge(3000);
}
if (progress >= 1) {
playing = false;
playButton.textContent = "↻ Repetir";
}
}
if (progress < 0.295) crossedCritical = false;
const pulse = 1 + Math.sin(performance.now() * 0.006) * 0.08;
connectionRing.scale.setScalar(pulse);
plugLabel.position.y = 2.33 + Math.sin(performance.now() * 0.002) * 0.018;
renderer.render(scene, camera);
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
document.getElementById("loading").classList.add("hidden");
</script>
</body>
</html>
```