Prompt specification
Build or customize the Three.js 3D scene "Knot slack removal demo" 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: Knot slack removal demo
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/knot-slack-removal-demo-x0278
Scene Description Prompt:
Illustrate a low-poly knot tightening while its crossings stay consistent. Make one HTML file with Three.js from cdnjs; geometric animation only.
Working Three.js Starter (knot-slack-removal-demo-x0278):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Geometric Knot</title>
<style>
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #090c12;
}
canvas {
display: block;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x090c12, 1);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
// The camera looks directly down the torus axis. This keeps the
// projected crossing locations fixed and makes their depth order
// unambiguous throughout the tightening animation.
const camera = new THREE.OrthographicCamera(-3, 3, 3, -3, 0.1, 100);
camera.position.set(0, 0, 8);
camera.lookAt(0, 0, 0);
scene.add(new THREE.AmbientLight(0xffffff, 0.75));
const keyLight = new THREE.DirectionalLight(0xffe3bd, 3.2);
keyLight.position.set(3.5, 4.5, 6);
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x6ea8ff, 1.25);
fillLight.position.set(-4, -2, 3);
scene.add(fillLight);
const rimLight = new THREE.DirectionalLight(0xff704d, 1.1);
rimLight.position.set(1, -4, -3);
scene.add(rimLight);
// ------------------------------------------------------------
// Low-poly tube geometry
// ------------------------------------------------------------
const PATH_SEGMENTS = 180;
const RADIAL_SEGMENTS = 7;
const ROPE_RADIUS = 0.105;
const vertexCount = PATH_SEGMENTS * RADIAL_SEGMENTS;
const positions = new Float32Array(vertexCount * 3);
const indices = [];
for (let i = 0; i < PATH_SEGMENTS; i++) {
const next = (i + 1) % PATH_SEGMENTS;
for (let j = 0; j < RADIAL_SEGMENTS; j++) {
const nextSide = (j + 1) % RADIAL_SEGMENTS;
const a = i * RADIAL_SEGMENTS + j;
const b = i * RADIAL_SEGMENTS + nextSide;
const c = next * RADIAL_SEGMENTS + nextSide;
const d = next * RADIAL_SEGMENTS + j;
indices.push(a, b, d);
indices.push(b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
const positionAttribute = new THREE.BufferAttribute(positions, 3);
positionAttribute.setUsage(THREE.DynamicDrawUsage);
geometry.setAttribute("position", positionAttribute);
geometry.setIndex(indices);
const material = new THREE.MeshStandardMaterial({
color: 0xffad55,
roughness: 0.68,
metalness: 0.08,
flatShading: true
});
const knot = new THREE.Mesh(geometry, material);
knot.frustumCulled = false;
scene.add(knot);
// Working buffers.
const centerline = new Float32Array(PATH_SEGMENTS * 3);
const tangents = new Float32Array(PATH_SEGMENTS * 3);
const normals = new Float32Array(PATH_SEGMENTS * 3);
const binormals = new Float32Array(PATH_SEGMENTS * 3);
const axisX = new THREE.Vector3(1, 0, 0);
const axisY = new THREE.Vector3(0, 1, 0);
const tangent = new THREE.Vector3();
const normal = new THREE.Vector3();
const binormal = new THREE.Vector3();
const reference = new THREE.Vector3();
const cross = new THREE.Vector3();
function smootherStep(value) {
const x = THREE.MathUtils.clamp(value, 0, 1);
return x * x * x * (x * (x * 6 - 15) + 10);
}
function signedAngle(a, b, axis) {
const cosine = THREE.MathUtils.clamp(a.dot(b), -1, 1);
const sine = axis.dot(cross.crossVectors(a, b));
return Math.atan2(sine, cosine);
}
function rotateAroundAxis(vector, axis, angle) {
const cosine = Math.cos(angle);
const sine = Math.sin(angle);
// Rodrigues' rotation formula.
return vector.clone()
.multiplyScalar(cosine)
.add(cross.crossVectors(axis, vector).multiplyScalar(sine))
.add(
axis.clone()
.multiplyScalar(axis.dot(vector))
.multiplyScalar(1 - cosine)
);
}
// ------------------------------------------------------------
// Knot deformation
// ------------------------------------------------------------
function updateKnot(tightness) {
const majorRadius = 1.16;
// The minor radius contracts toward zero but never reaches it.
// Thus the T(2,3) torus knot remains an embedded trefoil.
const minorRadius = THREE.MathUtils.lerp(0.68, 0.22, tightness);
// Standard T(2,3) torus-knot parameterization.
// Its projected crossings occur at cos(3t) = 0. At each such
// crossing the paired points have opposite z values, and because
// minorRadius stays positive their over/under order never flips.
for (let i = 0; i < PATH_SEGMENTS; i++) {
const t = (i / PATH_SEGMENTS) * Math.PI * 2;
const orbitAngle = t * 2;
const tubeAngle = t * 3;
const radialDistance =
majorRadius + minorRadius * Math.cos(tubeAngle);
const offset = i * 3;
centerline[offset] =
radialDistance * Math.cos(orbitAngle);
centerline[offset + 1] =
radialDistance * Math.sin(orbitAngle);
centerline[offset + 2] =
minorRadius * Math.sin(tubeAngle);
}
// Build a continuous, minimally twisting frame along the path.
for (let i = 0; i < PATH_SEGMENTS; i++) {
const previous = (i - 1 + PATH_SEGMENTS) % PATH_SEGMENTS;
const next = (i + 1) % PATH_SEGMENTS;
tangent.set(
centerline[next * 3] - centerline[previous * 3],
centerline[next * 3 + 1] - centerline[previous * 3 + 1],
centerline[next * 3 + 2] - centerline[previous * 3 + 2]
).normalize();
if (i === 0) {
reference.copy(Math.abs(tangent.y) < 0.9 ? axisY : axisX);
normal.crossVectors(tangent, reference).normalize();
} else {
const previousOffset = (i - 1) * 3;
normal.set(
normals[previousOffset],
normals[previousOffset + 1],
normals[previousOffset + 2]
);
// Project the previous normal onto the new tangent plane.
normal.addScaledVector(
tangent,
-normal.dot(tangent)
);
if (normal.lengthSq() < 1e-8) {
reference.copy(Math.abs(tangent.y) < 0.9 ? axisY : axisX);
normal.crossVectors(tangent, reference);
}
normal.normalize();
}
binormal.crossVectors(tangent, normal).normalize();
const offset = i * 3;
tangents[offset] = tangent.x;
tangents[offset + 1] = tangent.y;
tangents[offset + 2] = tangent.z;
normals[offset] = normal.x;
normals[offset + 1] = normal.y;
normals[offset + 2] = normal.z;
binormals[offset] = binormal.x;
binormals[offset + 1] = binormal.y;
binormals[offset + 2] = binormal.z;
}
// Correct the frame's holonomy so the closed tube has no seam.
const lastOffset = (PATH_SEGMENTS - 1) * 3;
const lastNormal = new THREE.Vector3(
normals[lastOffset],
normals[lastOffset + 1],
normals[lastOffset + 2]
);
const firstNormal = new THREE.Vector3(
normals[0],
normals[1],
normals[2]
);
const lastTangent = new THREE.Vector3(
tangents[lastOffset],
tangents[lastOffset + 1],
tangents[lastOffset + 2]
);
const closureAngle = signedAngle(
lastNormal,
firstNormal,
lastTangent
);
// Generate the faceted tube rings.
for (let i = 0; i < PATH_SEGMENTS; i++) {
const offset = i * 3;
const pathNormal = new THREE.Vector3(
normals[offset],
normals[offset + 1],
normals[offset + 2]
);
const pathTangent = new THREE.Vector3(
tangents[offset],
tangents[offset + 1],
tangents[offset + 2]
);
const correction =
-closureAngle * (i / (PATH_SEGMENTS - 1));
pathNormal.copy(
rotateAroundAxis(pathNormal, pathTangent, correction)
);
const pathBinormal = new THREE.Vector3()
.crossVectors(pathTangent, pathNormal)
.normalize();
for (let j = 0; j < RADIAL_SEGMENTS; j++) {
const angle = (j / RADIAL_SEGMENTS) * Math.PI * 2;
const cosine = Math.cos(angle);
const sine = Math.sin(angle);
const vertexOffset = (i * RADIAL_SEGMENTS + j) * 3;
positions[vertexOffset] =
centerline[offset] +
ROPE_RADIUS *
(cosine * pathNormal.x + sine * pathBinormal.x);
positions[vertexOffset + 1] =
centerline[offset + 1] +
ROPE_RADIUS *
(cosine * pathNormal.y + sine * pathBinormal.y);
positions[vertexOffset + 2] =
centerline[offset + 2] +
ROPE_RADIUS *
(cosine * pathNormal.z + sine * pathBinormal.z);
}
}
positionAttribute.needsUpdate = true;
geometry.computeVertexNormals();
}
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
const cycleDuration = 14;
function animate() {
const elapsed = clock.getElapsedTime();
const phase = (elapsed % cycleDuration) / cycleDuration;
let tightness;
if (phase < 0.58) {
// Gradual tightening.
tightness = smootherStep(phase / 0.58);
} else if (phase < 0.78) {
// Hold the tightened state.
tightness = 1;
} else {
// Smoothly return for the next cycle.
tightness = 1 - smootherStep((phase - 0.78) / 0.22);
}
updateKnot(tightness);
// Rotation around the viewing axis does not alter knot topology
// or the relative depth of any crossing.
knot.rotation.z = elapsed * 0.07;
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
// ------------------------------------------------------------
// Responsive framing
// ------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const halfHeight = Math.max(2.45, 2.35 / aspect);
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
resize();
</script>
</body>
</html>
```