Prompt specification
Build or customize the Three.js 3D scene "Coordinate yarn supply for a weaving task" 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: Coordinate yarn supply for a weaving task
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/coordinate-yarn-supply-for-a-weaving-task-x2252
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts ancient textile production with spindle whorls. Let users allocate illustrative yarn bundles to warp and weft. Animate a small cloth section growing and stop when either supply is insufficient for another complete row. Low-poly.
Working Three.js Starter (coordinate-yarn-supply-for-a-weaving-task-x2252):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ancient Loom — Interactive Textile Study</title>
<style>
:root {
--ink: #35291f;
--muted: #746454;
--paper: rgba(248, 239, 216, 0.93);
--paper-strong: #f7ecd3;
--line: rgba(78, 57, 39, 0.2);
--accent: #a84f32;
--accent-dark: #713321;
--teal: #416d68;
--gold: #c6923f;
--shadow: 0 14px 38px rgba(55, 38, 23, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #cbb996;
color: var(--ink);
font-family: Georgia, "Times New Roman", serif;
}
button,
input {
font: inherit;
}
button {
cursor: pointer;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
border-radius: 15px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(9px);
}
.intro {
top: 18px;
left: 18px;
width: min(360px, calc(100vw - 36px));
padding: 18px 20px;
}
.eyebrow {
margin: 0 0 5px;
color: var(--accent);
font-family: Arial, sans-serif;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1,
h2,
p {
margin-top: 0;
}
h1 {
margin-bottom: 8px;
font-size: clamp(25px, 3vw, 34px);
font-weight: 500;
line-height: 1.05;
}
.intro p {
margin-bottom: 12px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px 14px;
color: #5c4c3d;
font-family: Arial, sans-serif;
font-size: 10px;
letter-spacing: 0.03em;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.dot {
width: 9px;
height: 9px;
border-radius: 50%;
box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.28);
}
.controls {
top: 18px;
right: 18px;
width: min(370px, calc(100vw - 36px));
max-height: calc(100vh - 36px);
overflow: auto;
padding: 18px;
}
.controls h2 {
margin-bottom: 4px;
font-size: 23px;
font-weight: 500;
}
.intro-text {
margin-bottom: 13px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.section-label {
display: flex;
justify-content: space-between;
align-items: baseline;
margin: 13px 0 7px;
font-family: Arial, sans-serif;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.section-label span:last-child {
color: var(--muted);
font-weight: 500;
letter-spacing: 0;
text-transform: none;
}
.bundle-grid,
.lane-list {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 7px;
}
.bundle-card {
position: relative;
display: flex;
align-items: center;
gap: 9px;
min-height: 48px;
padding: 8px 9px;
border: 1px solid rgba(75, 53, 34, 0.16);
border-radius: 10px;
background: rgba(255, 250, 235, 0.7);
user-select: none;
transition: transform 120ms ease, border-color 120ms ease, box-shadow 120ms ease;
}
.bundle-card:hover {
transform: translateY(-1px);
border-color: rgba(75, 53, 34, 0.38);
}
.bundle-card.selected {
border-color: var(--accent);
box-shadow: 0 0 0 2px rgba(168, 79, 50, 0.13);
}
.bundle-card.dragging {
opacity: 0.45;
}
.coil {
position: relative;
flex: 0 0 27px;
width: 27px;
height: 27px;
border-radius: 50%;
background: var(--yarn);
box-shadow:
inset -5px -5px 0 rgba(69, 38, 19, 0.14),
inset 4px 3px 0 rgba(255, 255, 255, 0.18);
}
.coil::before,
.coil::after {
content: "";
position: absolute;
inset: 5px 2px;
border-top: 2px solid rgba(255, 245, 220, 0.45);
border-bottom: 1px solid rgba(75, 43, 25, 0.24);
border-radius: 50%;
transform: rotate(23deg);
}
.coil::after {
transform: rotate(-20deg);
}
.bundle-card strong,
.bundle-card small {
display: block;
line-height: 1.1;
}
.bundle-card strong {
font-family: Arial, sans-serif;
font-size: 11px;
}
.bundle-card small {
margin-top: 3px;
color: var(--muted);
font-family: Arial, sans-serif;
font-size: 9px;
}
.remove-bundle {
position: absolute;
top: 3px;
right: 4px;
width: 18px;
height: 18px;
padding: 0;
border: 0;
border-radius: 50%;
background: transparent;
color: #8b7967;
font-size: 16px;
line-height: 16px;
}
.remove-bundle:hover {
background: rgba(168, 79, 50, 0.12);
color: var(--accent-dark);
}
.allocation-lane {
margin-top: 8px;
padding: 10px;
border: 1px dashed rgba(75, 53, 34, 0.3);
border-radius: 11px;
background: rgba(255, 250, 235, 0.42);
transition: border-color 120ms ease, background 120ms ease;
}
.allocation-lane.over {
border-color: var(--accent);
background: rgba(168, 79, 50, 0.08);
}
.lane-head {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 8px;
font-family: Arial, sans-serif;
font-size: 11px;
font-weight: 700;
}
.lane-head span {
color: var(--muted);
font-size: 10px;
font-weight: 500;
}
.lane-list:empty::after {
content: "Drop bundles here";
display: block;
padding: 9px 2px;
color: #998779;
font-family: Arial, sans-serif;
font-size: 10px;
text-align: center;
}
.add-selected {
width: 100%;
margin-top: 7px;
padding: 6px;
border: 1px solid rgba(75, 53, 34, 0.16);
border-radius: 7px;
background: rgba(255, 255, 255, 0.38);
color: var(--muted);
font-family: Arial, sans-serif;
font-size: 9px;
}
.add-selected:hover {
border-color: var(--accent);
color: var(--accent-dark);
}
.allocation-hint {
min-height: 30px;
margin: 8px 1px 0;
color: var(--muted);
font-family: Arial, sans-serif;
font-size: 10px;
line-height: 1.4;
}
.divider {
height: 1px;
margin: 15px 0;
background: var(--line);
}
.progress-label {
display: flex;
justify-content: space-between;
margin-bottom: 6px;
color: var(--muted);
font-family: Arial, sans-serif;
font-size: 10px;
}
.progress-track {
height: 7px;
overflow: hidden;
border-radius: 999px;
background: rgba(75, 53, 34, 0.12);
}
.progress-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--accent), var(--gold));
transition: width 280ms ease;
}
.weave-status {
min-height: 39px;
margin: 9px 0 12px;
color: #5d4d3d;
font-size: 12px;
line-height: 1.4;
}
.button-row {
display: grid;
grid-template-columns: 1.4fr 1fr;
gap: 7px;
}
.button-row + .button-row {
margin-top: 7px;
}
.action {
min-height: 39px;
padding: 9px 11px;
border: 1px solid rgba(75, 53, 34, 0.22);
border-radius: 9px;
background: rgba(255, 250, 235, 0.68);
color: var(--ink);
font-family: Arial, sans-serif;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.04em;
}
.action:hover:not(:disabled) {
border-color: var(--accent);
color: var(--accent-dark);
}
.action.primary {
border-color: var(--accent);
background: var(--accent);
color: #fff6e7;
}
.action.primary:hover:not(:disabled) {
background: var(--accent-dark);
color: white;
}
.action:disabled {
cursor: not-allowed;
opacity: 0.45;
}
.camera-hint {
position: fixed;
z-index: 5;
left: 50%;
bottom: 14px;
transform: translateX(-50%);
padding: 7px 12px;
border-radius: 999px;
background: rgba(53, 41, 31, 0.68);
color: #f5ead5;
font-family: Arial, sans-serif;
font-size: 9px;
letter-spacing: 0.07em;
pointer-events: none;
white-space: nowrap;
}
@media (max-width: 760px) {
.intro {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 13px 15px;
}
.intro p {
display: none;
}
.legend {
margin-top: 8px;
}
.controls {
top: auto;
right: 10px;
bottom: 10px;
width: calc(100vw - 20px);
max-height: 51vh;
padding: 14px;
}
.controls h2 {
font-size: 19px;
}
.bundle-grid,
.lane-list {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
.camera-hint {
display: none;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive ancient textile production scene"></div>
<header class="panel intro">
<p class="eyebrow">Interactive weaving study</p>
<h1>The Ancient Loom</h1>
<p>
Allocate yarn bundles between warp and weft, then send the shuttle across
the loom. Every complete row consumes one pick of each supply.
</p>
<div class="legend">
<span><i class="dot" style="background:#d9c38f"></i>Warp</span>
<span><i class="dot" style="background:#b85638"></i>Weft</span>
<span><i class="dot" style="background:#416d68"></i>Pattern</span>
</div>
</header>
<aside class="panel controls">
<h2>Yarn allocation</h2>
<p class="intro-text">
Drag bundles into a bay, or select a bundle and press “Add selected.”
Each bundle provides three picks.
</p>
<div class="section-label">
<span>Unassigned stock</span>
<span id="stockCount"></span>
</div>
<div id="bundleSupply" class="bundle-grid" aria-label="Unassigned yarn bundles"></div>
<div class="section-label">
<span>Loom supplies</span>
<span id="availableRows"></span>
</div>
<div id="warpLane" class="allocation-lane" data-lane="warp">
<div class="lane-head">
Warp bay <span id="warpSummary"></span>
</div>
<div id="warpBundles" class="lane-list"></div>
<button class="add-selected" data-add-lane="warp">+ Add selected to warp</button>
</div>
<div id="weftLane" class="allocation-lane" data-lane="weft">
<div class="lane-head">
Weft bay <span id="weftSummary"></span>
</div>
<div id="weftBundles" class="lane-list"></div>
<button class="add-selected" data-add-lane="weft">+ Add selected to weft</button>
</div>
<div id="allocationHint" class="allocation-hint">
Select or drag a bundle to begin allocating yarn.
</div>
<div class="divider"></div>
<div class="progress-label">
<span id="progressText">0 rows woven</span>
<span id="supplyText"></span>
</div>
<div class="progress-track">
<div id="progressFill" class="progress-fill"></div>
</div>
<p id="weaveStatus" class="weave-status" aria-live="polite"></p>
<div class="button-row">
<button id="playBtn" class="action primary">Start weaving</button>
<button id="resetClothBtn" class="action">Reset cloth</button>
</div>
<div class="button-row">
<button id="resetAllocBtn" class="action">Return all yarn</button>
<button id="resetCameraBtn" class="action">Reset camera</button>
</div>
</aside>
<div class="camera-hint">DRAG TO ORBIT · SCROLL OR PINCH TO ZOOM</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 camera
// -------------------------------------------------------------------------
const container = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({ antialias: true });
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;
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd4c39f);
scene.fog = new THREE.Fog(0xd4c39f, 13, 28);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraState = {
theta: 0.56,
phi: 0.98,
radius: 12.6,
target: new THREE.Vector3(0, 2.05, 0)
};
function updateCamera() {
const { theta, phi, radius, target } = cameraState;
camera.position.set(
target.x + radius * Math.sin(phi) * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * Math.sin(phi) * Math.cos(theta)
);
camera.lookAt(target);
}
function resetCamera() {
cameraState.theta = 0.56;
cameraState.phi = 0.98;
cameraState.radius = 12.6;
cameraState.target.set(0, 2.05, 0);
updateCamera();
}
// Direct camera controls: no addons or external controls are used.
const pointers = new Map();
let pinchDistance = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
renderer.domElement.setPointerCapture(event.pointerId);
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
pinchDistance = Math.hypot(a.x - b.x, a.y - b.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 >= 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (pinchDistance > 0 && distance > 0) {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * (pinchDistance / distance),
7,
18
);
}
pinchDistance = distance;
} else {
cameraState.theta -= (current.x - previous.x) * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - (current.y - previous.y) * 0.005,
0.45,
1.42
);
}
});
function removePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) pinchDistance = 0;
}
renderer.domElement.addEventListener("pointerup", removePointer);
renderer.domElement.addEventListener("pointercancel", removePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.008,
7,
18
);
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
function material(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0,
flatShading: true,
...extra
});
}
const materials = {
ground: material(0xa98b62),
groundEdge: material(0x7f6549),
wood: material(0x745037),
woodLight: material(0x936744),
woodDark: material(0x503925),
stone: material(0x8c806a),
stoneLight: material(0xaaa087),
reed: material(0xb59459),
skin: material(0x9b6845),
skinDark: material(0x774a31),
tunic: material(0x9e4d34),
tunicDark: material(0x526b60),
hair: material(0x34271d),
cream: material(0xe3cea0),
white: material(0xeee1be),
rust: material(0xb65335),
ochre: material(0xc58f3f),
teal: material(0x3e6b66),
darkTeal: material(0x284d4a)
};
function addMesh(geometry, mat, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(geometry, mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function box(w, h, d, mat, x, y, z, parent = scene) {
return addMesh(new THREE.BoxGeometry(w, h, d), mat, x, y, z, parent);
}
function cylinder(
topRadius,
bottomRadius,
height,
segments,
mat,
x,
y,
z,
parent = scene
) {
return addMesh(
new THREE.CylinderGeometry(topRadius, bottomRadius, height, segments),
mat,
x,
y,
z,
parent
);
}
function rodBetween(start, end, radius, mat, parent = scene) {
const midpoint = start.clone().add(end).multiplyScalar(0.5);
const direction = end.clone().sub(start);
const mesh = addMesh(
new THREE.CylinderGeometry(radius, radius, direction.length(), 6),
mat,
midpoint.x,
midpoint.y,
midpoint.z,
parent
);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
return mesh;
}
// -------------------------------------------------------------------------
// Environment
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xffe9c7, 0x5e4936, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe2ad, 3.2);
sun.position.set(-7, 11, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(1536, 1536);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 28;
sun.shadow.bias = -0.0004;
scene.add(sun);
const ground = cylinder(
8.7,
8.9,
0.38,
18,
materials.ground,
0,
-0.32,
0
);
ground.receiveShadow = true;
cylinder(
8.95,
9.05,
0.18,
18,
materials.groundEdge,
0,
-0.57,
0
);
// Low-poly stones and broken architectural pieces.
const stonePositions = [
[-5.6, -0.03, -2.4, 0.55],
[-6.1, -0.05, -1.7, 0.36],
[5.7, -0.02, -2.8, 0.52],
[6.3, -0.06, -2.0, 0.31],
[-2.9, -0.05, -4.6, 0.43],
[2.8, -0.03, -5.1, 0.48]
];
for (const [x, y, z, size] of stonePositions) {
const rock = addMesh(
new THREE.DodecahedronGeometry(size, 0),
materials.stone,
x,
y,
z
);
rock.scale.set(1.25, 0.65, 0.95);
rock.rotation.set(0.2, x * 0.3, 0.1);
}
// Ruined columns in the background.
for (const x of [-4.8, 4.5]) {
const column = cylinder(
0.32,
0.39,
2.6,
8,
materials.stoneLight,
x,
1.0,
-4.4
);
column.rotation.z = x < 0 ? 0.045 : -0.055;
box(0.9, 0.28, 0.9, materials.stone, x, 2.38, -4.4);
box(0.85, 0.26, 0.85, materials.stone, x, -0.08, -4.4);
}
// -------------------------------------------------------------------------
// Wooden ground loom
// -------------------------------------------------------------------------
const loom = new THREE.Group();
scene.add(loom);
const postHeight = 4.15;
const postY = 2.55;
for (const x of [-2.85, 2.85]) {
box(0.2, postHeight, 0.24, materials.woodDark, x, postY, -0.12, loom);
box(0.28, 0.32, 0.36, materials.wood, x, 0.42, -0.12, loom);
rodBetween(
new THREE.Vector3(x, 3.55, -0.12),
new THREE.Vector3(x * 0.79, 4.5, -0.12),
0.065,
materials.wood,
loom
);
}
box(6.0, 0.25, 0.3, materials.wood, 0, 4.55, -0.12, loom);
box(5.85, 0.18, 0.24, materials.woodLight, 0, 1.04, -0.12, loom);
// Decorative beam joints.
for (const x of [-2.84, 2.84]) {
cylinder(
0.14,
0.14,
0.36,
8,
materials.woodLight,
x,
4.55,
-0.12,
loom
).rotation.x = Math.PI / 2;
}
// Woven section dimensions.
const CLOTH_WIDTH = 3.55;
const ROW_HEIGHT = 0.16;
const CLOTH_BOTTOM = 1.18;
// Static warp threads.
for (let i = 0; i < 17; i++) {
const x = -1.68 + i * 0.21;
cylinder(
0.012,
0.012,
3.12,
5,
materials.cream,
x,
2.74,
-0.035,
loom
);
}
// Shuttle that travels across each new row.
const shuttle = new THREE.Group();
scene.add(shuttle);
box(0.42, 0.075, 0.085, materials.woodLight, 0, 0, 0, shuttle);
const shuttleTipA = addMesh(
new THREE.ConeGeometry(0.09, 0.17, 5),
materials.wood,
-0.27,
0,
0,
shuttle
);
const shuttleTipB = addMesh(
new THREE.ConeGeometry(0.09, 0.17, 5),
materials.wood,
0.27,
0,
0,
shuttle
);
shuttleTipA.rotation.z = Math.PI / 2;
shuttleTipB.rotation.z = -Math.PI / 2;
shuttle.visible = false;
// -------------------------------------------------------------------------
// Yarn stock basket and physical bundle meshes
// -------------------------------------------------------------------------
const basket = new THREE.Group();
basket.position.set(4.35, 0, 1.75);
scene.add(basket);
cylinder(0.86, 0.57, 0.57, 10, materials.reed, 0, 0.22, 0, basket);
addMesh(
new THREE.TorusGeometry(0.85, 0.055, 6, 14),
materials.woodLight,
0,
0.51,
0,
basket
).rotation.x = Math.PI / 2;
// A low bench used as a yarn stand.
box(1.35, 0.18, 0.8, materials.wood, 3.6, 0.12, 0.95);
for (const x of [3.05, 4.15]) {
box(0.13, 0.45, 0.55, materials.woodDark, x, -0.02, 0.95);
}
box(1.35, 0.18, 0.8, materials.wood, -3.55, 0.12, 0.95);
for (const x of [-4.1, -3.0]) {
box(0.13, 0.45, 0.55, materials.woodDark, x, -0.02, 0.95);
}
const bundlePalette = [
0xc98a47,
0xb95d3b,
0x6d8b6f,
0xd1aa5e,
0x8d6552,
0x4f7772
];
const bundles = bundlePalette.map((color, index) => ({
id: index + 1,
label: `Bundle ${String.fromCharCode(65 + index)}`,
picks: 3,
color
}));
const bundleMeshes = [];
for (const [index, bundle] of bundles.entries()) {
const group = new THREE.Group();
const yarnMat = material(bundle.color);
addMesh(
new THREE.DodecahedronGeometry(0.25, 0),
yarnMat,
0,
0,
0,
group
);
addMesh(
new THREE.TorusGeometry(0.19, 0.025, 5, 10),
materials.cream,
0,
0,
0,
group
).rotation.x = Math.PI / 2;
group.scale.setScalar(0.95);
scene.add(group);
bundleMeshes.push({
id: bundle.id,
group,
phase: index * 0.73
});
}
// -------------------------------------------------------------------------
// Spindle whorls and low-poly artisans
// -------------------------------------------------------------------------
function createSpindle(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
scene.add(group);
cylinder(0.026, 0.035, 1.45, 7, materials.woodLight, 0, 0.75, 0, group);
cylinder(
0.22,
0.18,
0.085,
10,
materials.stoneLight,
0,
0.045,
0,
group
);
addMesh(
new THREE.DodecahedronGeometry(0.15, 0),
materials.cream,
0,
0.86,
0,
group
);
rodBetween(
new THREE.Vector3(0, 0.16, 0),
new THREE.Vector3(0.34, 0.48, 0.03),
0.012,
materials.darkTeal,
group
);
group.rotation.z = -0.33;
return group;
}
const spindleA = createSpindle(-3.55, 2.05, 0.9);
const spindleB = createSpindle(3.8, 2.25, 0.78);
function createPerson(x, z, tunicMaterial, flip = 1) {
const person = new THREE.Group();
person.position.set(x, 0, z);
scene.add(person);
cylinder(0.42, 0.45, 0.18, 8, materials.wood, 0, 0.42, 0, person);
cylinder(0.25, 0.35, 0.72, 7, tunicMaterial, 0, 0.93, 0, person);
addMesh(
new THREE.DodecahedronGeometry(0.235, 0),
materials.skin,
0,
1.49,
0,
person
);
const hair = addMesh(
new THREE.DodecahedronGeometry(0.25, 0),
materials.hair,
0,
1.58,
-0.035,
person
);
hair.scale.set(1.02, 0.73, 0.96);
rodBetween(
new THREE.Vector3(-0.15, 0.67, 0.02),
new THREE.Vector3(-0.38 * flip, 0.22, 0.38),
0.095,
materials.skinDark,
person
);
rodBetween(
new THREE.Vector3(0.15, 0.67, 0.02),
new THREE.Vector3(0.39 * flip, 0.22, 0.4),
0.095,
materials.skinDark,
person
);
const shoulderL = new THREE.Vector3(-0.24, 1.18, 0);
const shoulderR = new THREE.Vector3(0.24, 1.18, 0);
const handL = new THREE.Vector3(-0.44 * flip, 0.82, 0.34);
const handR = new THREE.Vector3(0.44 * flip, 0.82, 0.34);
rodBetween(shoulderL, handL, 0.075, materials.skin, person);
rodBetween(shoulderR, handR, 0.075, materials.skin, person);
// A small hand spindle with a stone whorl.
const handSpindle = new THREE.Group();
handSpindle.position.copy(handR);
handSpindle.rotation.z = 0.65 * flip;
person.add(handSpindle);
cylinder(0.018, 0.018, 0.6, 5, materials.woodLight, 0, 0.18, 0, handSpindle);
cylinder(
0.105,
0.09,
0.055,
8,
materials.stoneLight,
0,
-0.08,
0,
handSpindle
);
return person;
}
createPerson(-4.25, 0.65, materials.tunic, 1);
createPerson(4.05, -1.15, materials.tunicDark, -1);
// A reed mat and a few lengths of undyed wool.
const mat = box(2.0, 0.045, 1.1, materials.reed, -4.45, -0.07, -0.8);
mat.rotation.y = 0.15;
for (let i = 0; i < 3; i++) {
const thread = cylinder(
0.022,
0.022,
1.25,
5,
materials.white,
-4.55 + i * 0.09,
0.04,
-0.55
);
thread.rotation.x = Math.PI / 2;
thread.rotation.y = 0.16;
}
// -------------------------------------------------------------------------
// Yarn allocation and weaving state
// -------------------------------------------------------------------------
const state = {
warp: [],
weft: [],
selected: null,
running: false,
paused: false,
rowsCreated: 0,
warpUsed: 0,
weftUsed: 0,
rowTimer: 0,
stopReason: ""
};
const clothRows = [];
const stockEl = document.getElementById("bundleSupply");
const warpListEl = document.getElementById("warpBundles");
const weftListEl = document.getElementById("weftBundles");
const warpLaneEl = document.getElementById("warpLane");
const weftLaneEl = document.getElementById("weftLane");
const allocationHintEl = document.getElementById("allocationHint");
const stockCountEl = document.getElementById("stockCount");
const warpSummaryEl = document.getElementById("warpSummary");
const weftSummaryEl = document.getElementById("weftSummary");
const availableRowsEl = document.getElementById("availableRows");
const progressTextEl = document.getElementById("progressText");
const supplyTextEl = document.getElementById("supplyText");
const progressFillEl = document.getElementById("progressFill");
const weaveStatusEl = document.getElementById("weaveStatus");
const playBtn = document.getElementById("playBtn");
function findBundle(id) {
return bundles.find((bundle) => bundle.id === id);
}
function laneOf(id) {
if (state.warp.includes(id)) return "warp";
if (state.weft.includes(id)) return "weft";
return null;
}
function picksIn(lane) {
return state[lane].reduce((sum, id) => sum + findBundle(id).picks, 0);
}
function availableRows() {
return Math.min(picksIn("warp"), picksIn("weft"));
}
function bundleCard(bundle, assignedLane = null) {
const selectedClass = state.selected === bundle.id ? "selected" : "";
const removeButton = assignedLane
? `<button class="remove-bundle" data-remove="${bundle.id}"
title="Return this bundle to stock" aria-label="Return ${bundle.label} to stock">×</button>`
: "";
const description = assignedLane
? `Assigned to ${assignedLane}. Drag it to move it.`
: "Unassigned. Drag it to a bay.";
return `
<div class="bundle-card ${selectedClass}"
draggable="true"
tabindex="0"
role="button"
data-id="${bundle.id}"
style="--yarn:#${bundle.color.toString(16).padStart(6, "0")}"
aria-label="${bundle.label}, ${bundle.picks} picks. ${description}">
<span class="coil" aria-hidden="true"></span>
<span>
<strong>${bundle.label}</strong>
<small>${bundle.picks} picks</small>
</span>
${removeButton}
</div>
`;
}
function renderAllocation() {
const unassigned = bundles.filter((bundle) => !laneOf(bundle.id));
stockEl.innerHTML = unassigned
.map((bundle) => bundleCard(bundle))
.join("");
warpListEl.innerHTML = state.warp
.map((id) => bundleCard(findBundle(id), "warp"))
.join("");
weftListEl.innerHTML = state.weft
.map((id) => bundleCard(findBundle(id), "weft"))
.join("");
stockCountEl.textContent = `${unassigned.length} remaining`;
warpSummaryEl.textContent = `${state.warp.length} bundles · ${picksIn("warp")} picks`;
weftSummaryEl.textContent = `${state.weft.length} bundles · ${picksIn("weft")} picks`;
availableRowsEl.textContent = `${availableRows()} possible rows`;
}
function updateProgress() {
const total = availableRows();
progressTextEl.textContent = `${state.rowsCreated} row${state.rowsCreated === 1 ? "" : "s"} woven`;
supplyTextEl.textContent = `Warp ${state.warpUsed}/${picksIn("warp")} · Weft ${state.weftUsed}/${picksIn("weft")}`;
progressFillEl.style.width = total ? `${(state.rowsCreated / total) * 100}%` : "0%";
}
function updateStatus() {
const total = availableRows();
if (state.running) {
weaveStatusEl.textContent =
`The shuttle is passing across row ${state.rowsCreated + 1} of ${total}.`;
} else if (state.paused) {
weaveStatusEl.textContent = "Weaving is paused. Resume when ready.";
} else if (state.stopReason) {
weaveStatusEl.textContent = state.stopReason;
} else if (total === 0) {
weaveStatusEl.textContent =
"Allocate at least one bundle to both warp and weft before weaving.";
} else {
weaveStatusEl.textContent =
`Ready to weave ${total} complete row${total === 1 ? "" : "s"}.`;
}
if (state.running) {
playBtn.textContent = "Pause weaving";
playBtn.disabled = false;
} else if (state.paused) {
playBtn.textContent = "Resume weaving";
playBtn.disabled = false;
} else if (total > state.rowsCreated) {
playBtn.textContent = state.rowsCreated ? "Continue weaving" : "Start weaving";
playBtn.disabled = false;
} else {
playBtn.textContent = total ? "Supply spent" : "Start weaving";
playBtn.disabled = true;
}
updateProgress();
}
function clearCloth() {
for (const row of clothRows) {
scene.remove(row.group);
row.group.traverse((object) => {
if (object.geometry) object.geometry.dispose();
});
}
clothRows.length = 0;
state.rowsCreated = 0;
state.warpUsed = 0;
state.weftUsed = 0;
state.running = false;
state.paused = false;
state.rowTimer = 0;
state.stopReason = "";
shuttle.visible = false;
}
function allocateBundle(id, lane) {
const currentLane = laneOf(id);
if (!findBundle(id) || currentLane === lane) return;
if (currentLane) {
state[currentLane] = state[currentLane].filter((bundleId) => bundleId !== id);
}
state[lane].push(id);
state.selected = null;
clearCloth();
renderAllocation();
updateStatus();
allocationHintEl.textContent = `${findBundle(id).label} assigned to ${lane}.`;
}
function returnBundle(id) {
const lane = laneOf(id);
if (!lane) return;
state[lane] = state[lane].filter((bundleId) => bundleId !== id);
state.selected = null;
clearCloth();
renderAllocation();
updateStatus();
allocationHintEl.textContent = `${findBundle(id).label} returned to stock.`;
}
function selectBundle(id) {
state.selected = id;
renderAllocation();
allocationHintEl.textContent =
`${findBundle(id).label} selected. Choose a warp or weft bay.`;
}
// Pointer-friendly drag and drop.
let draggedBundleId = null;
document.addEventListener("dragstart", (event) => {
const card = event.target.closest(".bundle-card");
if (!card) return;
draggedBundleId = Number(card.dataset.id);
card.classList.add("dragging");
event.dataTransfer.effectAllowed = "move";
event.dataTransfer.setData("text/plain", String(draggedBundleId));
});
document.addEventListener("dragend", (event) => {
const card = event.target.closest(".bundle-card");
if (card) card.classList.remove("dragging");
draggedBundleId = null;
warpLaneEl.classList.remove("over");
weftLaneEl.classList.remove("over");
stockEl.classList.remove("over");
});
for (const laneElement of [warpLaneEl, weftLaneEl]) {
laneElement.addEventListener("dragover", (event) => {
event.preventDefault();
laneElement.classList.add("over");
});
laneElement.addEventListener("dragleave", (event) => {
if (!laneElement.contains(event.relatedTarget)) {
laneElement.classList.remove("over");
}
});
laneElement.addEventListener("drop", (event) => {
event.preventDefault();
laneElement.classList.remove("over");
const id = Number(
event.dataTransfer.getData("text/plain") || draggedBundleId
);
if (Number.isFinite(id)) allocateBundle(id, laneElement.dataset.lane);
});
}
stockEl.addEventListener("dragover", (event) => {
event.preventDefault();
stockEl.classList.add("over");
});
stockEl.addEventListener("dragleave", () => stockEl.classList.remove("over"));
stockEl.addEventListener("drop", (event) => {
event.preventDefault();
stockEl.classList.remove("over");
const id = Number(
event.dataTransfer.getData("text/plain") || draggedBundleId
);
if (Number.isFinite(id)) returnBundle(id);
});
document.addEventListener("click", (event) => {
const removeButton = event.target.closest("[data-remove]");
if (removeButton) {
event.stopPropagation();
returnBundle(Number(removeButton.dataset.remove));
return;
}
const card = event.target.closest(".bundle-card");
if (card) {
selectBundle(Number(card.dataset.id));
return;
}
const addButton = event.target.closest("[data-add-lane]");
if (addButton) {
if (state.selected === null) {
allocationHintEl.textContent = "Select a bundle first.";
return;
}
allocateBundle(state.selected, addButton.dataset.addLane);
}
});
document.addEventListener("keydown", (event) => {
const card = event.target.closest(".bundle-card");
if (card && (event.key === "Enter" || event.key === " ")) {
event.preventDefault();
selectBundle(Number(card.dataset.id));
}
});
// -------------------------------------------------------------------------
// Weaving logic
// -------------------------------------------------------------------------
function createClothRow(rowIndex) {
const group = new THREE.Group();
group.position.set(0, CLOTH_BOTTOM + rowIndex * ROW_HEIGHT, 0.055);
group.scale.y = 0.015;
const weftMaterial = [materials.rust, materials.ochre, materials.teal][
rowIndex % 3
];
box(
CLOTH_WIDTH,
ROW_HEIGHT * 0.7,
0.075,
weftMaterial,
0,
ROW_HEIGHT * 0.5,
0,
group
);
for (let i = 0; i < 17; i++) {
const x = -1.68 + i * 0.21;
const warpMaterial =
(rowIndex + i) % 5 === 0 ? materials.darkTeal : materials.white;
box(
0.027,
ROW_HEIGHT,
0.047,
warpMaterial,
x,
ROW_HEIGHT * 0.5,
0.052,
group
);
}
scene.add(group);
const row = {
group,
rowIndex,
progress: 0,
duration: 0.56
};
clothRows.push(row);
return row;
}
function stopForSupply() {
const warpPicks = picksIn("warp");
const weftPicks = picksIn("weft");
state.running = false;
state.paused = false;
if (warpPicks <= weftPicks && weftPicks <= warpPicks) {
state.stopReason =
"Stopped: both warp and weft supplies are exhausted, so another complete row is not possible.";
} else if (warpPicks <= weftPicks) {
state.stopReason =
"Stopped: the warp supply cannot complete another full row.";
} else {
state.stopReason =
"Stopped: the weft supply cannot complete another full row.";
}
updateStatus();
}
function beginNextRow() {
// A complete row requires both one warp pick and one weft pick.
if (
state.warpUsed >= picksIn("warp") ||
state.weftUsed >= picksIn("weft")
) {
stopForSupply();
return;
}
const row = createClothRow(state.rowsCreated);
state.rowsCreated += 1;
state.warpUsed += 1;
state.weftUsed += 1;
updateStatus();
if (state.rowsCreated >= availableRows()) {
stopForSupply();
}
}
playBtn.addEventListener("click", () => {
if (state.running) {
state.running = false;
state.paused = true;
updateStatus();
return;
}
if (state.paused) {
state.running = true;
state.paused = false;
state.rowTimer = 0.15;
updateStatus();
return;
}
if (availableRows() > state.rowsCreated) {
state.running = true;
state.paused = false;
state.rowTimer = 0.15;
updateStatus();
}
});
document.getElementById("resetClothBtn").addEventListener("click", () => {
clearCloth();
renderAllocation();
updateStatus();
allocationHintEl.textContent = "Cloth reset. Yarn allocations were kept.";
});
document.getElementById("resetAllocBtn").addEventListener("click", () => {
state.warp = [];
state.weft = [];
state.selected = null;
clearCloth();
renderAllocation();
updateStatus();
allocationHintEl.textContent = "All bundles returned to the stock basket.";
});
document.getElementById("resetCameraBtn").addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const unassignedTargets = [
new THREE.Vector3(4.08, 0.65, 1.48),
new THREE.Vector3(4.48, 0.62, 1.38),
new THREE.Vector3(4.82, 0.60, 1.63),
new THREE.Vector3(4.22, 0.67, 1.98),
new THREE.Vector3(4.62, 0.64, 2.03),
new THREE.Vector3(4.04, 0.63, 2.08)
];
function updateBundleMeshes(dt) {
for (const meshData of bundleMeshes) {
const lane = laneOf(meshData.id);
let target;
if (!lane) {
const unassignedIndex = bundles
.filter((bundle) => !laneOf(bundle.id))
.findIndex((bundle) => bundle.id === meshData.id);
target = unassignedTargets[Math.max(0, unassignedIndex)];
} else {
const index = state[lane].indexOf(meshData.id);
const side = lane === "warp" ? -3.62 : 3.58;
target = new THREE.Vector3(
side + (index % 3) * 0.31,
0.48 + Math.floor(index / 3) * 0.34,
0.72 + Math.floor(index / 3) * 0.18
);
}
meshData.group.position.lerp(
target,
1 - Math.exp(-dt * 5)
);
meshData.group.rotation.y += dt * (0.25 + meshData.phase * 0.03);
}
}
function updateClothAnimation(dt) {
let activeShuttleRow = null;
for (const row of clothRows) {
if (row.progress >= 1) continue;
row.progress = Math.min(1, row.progress + dt / row.duration);
const eased = 1 - Math.pow(1 - row.progress, 3);
row.group.scale.y = Math.max(0.015, eased);
activeShuttleRow = row;
}
if (activeShuttleRow) {
const eased = 1 - Math.pow(1 - activeShuttleRow.progress, 2);
shuttle.visible = true;
shuttle.position.set(
THREE.MathUtils.lerp(-1.9, 1.9, eased),
CLOTH_BOTTOM +
activeShuttleRow.rowIndex * ROW_HEIGHT +
ROW_HEIGHT * 0.72,
0.23
);
shuttle.rotation.y = Math.sin(eased * Math.PI) * 0.08;
} else {
shuttle.visible = false;
}
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
renderAllocation();
updateStatus();
resetCamera();
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min(0.05, (now - previousTime) / 1000);
previousTime = now;
if (state.running) {
state.rowTimer -= dt;
if (state.rowTimer <= 0) {
beginNextRow();
state.rowTimer = 1.05;
}
}
updateClothAnimation(dt);
updateBundleMeshes(dt);
spindleA.rotation.y += dt * (state.running ? 4.2 : 0.85);
spindleB.rotation.y -= dt * (state.running ? 3.7 : 0.7);
spindleA.rotation.z = -0.33 + Math.sin(now * 0.0012) * 0.025;
spindleB.rotation.z = -0.33 + Math.sin(now * 0.0011 + 1.4) * 0.025;
updateCamera();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```