Prompt specification
Build or customize the Three.js 3D scene "Textile Pattern Workshop" 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: Textile Pattern Workshop
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/textile-pattern-workshop-x3359
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing woven crossings and repeating textile patterns. Create a workshop comparing plain and twill interlacing on enlarged sample looms. Display repeat units and avoid attributing invented patterns to specific cultural traditions. Voxel style.
Working Three.js Starter (textile-pattern-workshop-x3359):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interlace Workshop — Plain vs. Twill</title>
<style>
:root {
--ink: #252b2c;
--muted: #687171;
--paper: #f6f2e9;
--panel: #fbf8f1;
--line: #d8d1c4;
--accent: #167f83;
--accent-soft: #d9eeee;
--warp: #41658a;
--weft: #dfa64b;
--shadow: rgba(47, 43, 35, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#app {
position: relative;
width: 100%;
height: 100%;
height: 100dvh;
}
header {
position: absolute;
inset: 0 0 auto 0;
height: 82px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 14px 24px;
background: rgba(251, 248, 241, 0.94);
border-bottom: 1px solid var(--line);
z-index: 10;
backdrop-filter: blur(10px);
}
.brand {
display: flex;
align-items: center;
gap: 14px;
min-width: 0;
}
.brand-mark {
width: 42px;
height: 42px;
flex: 0 0 auto;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(2, 1fr);
gap: 3px;
padding: 5px;
background: #ebe5da;
border: 1px solid #cfc6b7;
box-shadow: 3px 3px 0 #d7cfc1;
}
.brand-mark span:nth-child(1) {
background: var(--warp);
}
.brand-mark span:nth-child(2) {
background: var(--weft);
}
.brand-mark span:nth-child(3) {
background: var(--weft);
}
.brand-mark span:nth-child(4) {
background: var(--warp);
}
h1 {
margin: 0;
font-size: clamp(17px, 2vw, 24px);
line-height: 1.05;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.subtitle {
margin-top: 5px;
color: var(--muted);
font-size: 12px;
letter-spacing: 0.025em;
}
.header-note {
max-width: 310px;
text-align: right;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
#panel {
position: absolute;
top: 82px;
bottom: 0;
left: 0;
width: 324px;
overflow-y: auto;
padding: 20px;
background: var(--panel);
border-right: 1px solid var(--line);
z-index: 5;
scrollbar-width: thin;
scrollbar-color: #bbb2a4 transparent;
}
.section {
padding: 0 0 18px;
margin: 0 0 18px;
border-bottom: 1px solid var(--line);
}
.section:last-child {
border-bottom: 0;
}
.section h2 {
margin: 0 0 11px;
color: #495252;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.segmented {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 5px;
padding: 4px;
background: #ebe5da;
border: 1px solid #d8d0c2;
}
.segmented button {
min-height: 34px;
border: 0;
color: #596161;
background: transparent;
cursor: pointer;
font-size: 11px;
font-weight: 700;
}
.segmented button[aria-pressed="true"] {
color: #fff;
background: #2e393a;
box-shadow: 2px 2px 0 #bfb5a6;
}
.control-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 13px;
}
.control-row label {
font-size: 12px;
font-weight: 650;
}
.value-readout {
min-width: 44px;
padding: 4px 7px;
color: #3d4c4d;
background: #e9f2f0;
border: 1px solid #c9dedb;
text-align: center;
font: 700 10px ui-monospace, SFMono-Regular, Menlo, monospace;
}
input[type="range"] {
width: 100%;
margin: 12px 0 2px;
accent-color: var(--accent);
cursor: pointer;
}
.range-labels {
display: flex;
justify-content: space-between;
color: #8a918f;
font-size: 9px;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.palettes {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.palette {
display: flex;
align-items: center;
justify-content: center;
gap: 6px;
height: 34px;
border: 1px solid #d5cdbf;
background: #fffaf2;
cursor: pointer;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.palette[aria-pressed="true"] {
border-color: var(--accent);
box-shadow: inset 0 0 0 1px var(--accent);
}
.palette i {
width: 12px;
height: 12px;
display: block;
border: 1px solid rgba(0, 0, 0, 0.15);
}
.check-row {
display: flex;
align-items: center;
gap: 9px;
margin-top: 14px;
color: #4e5959;
font-size: 12px;
cursor: pointer;
}
.check-row input {
width: 15px;
height: 15px;
margin: 0;
accent-color: var(--accent);
}
.repeat-cards {
display: grid;
gap: 10px;
}
.repeat-card {
display: grid;
grid-template-columns: 70px 1fr;
align-items: center;
gap: 12px;
padding: 10px;
background: #fffaf2;
border: 1px solid #ded6c8;
}
.mini-grid {
display: grid;
gap: 2px;
justify-content: center;
align-content: center;
padding: 5px;
background: #e9e3d8;
border: 1px solid #d1c8b9;
}
.mini-grid.plain {
grid-template-columns: repeat(2, 15px);
grid-template-rows: repeat(2, 15px);
}
.mini-grid.twill {
grid-template-columns: repeat(4, 11px);
grid-template-rows: repeat(4, 11px);
}
.mini-grid span {
display: block;
}
.warp-over {
background: var(--warp);
}
.weft-over {
background: var(--weft);
}
.repeat-card h3 {
margin: 0 0 4px;
font-size: 12px;
}
.repeat-card p {
margin: 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.repeat-size {
color: var(--accent);
font: 800 9px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.inspection {
padding: 12px;
background: #edf4f1;
border-left: 3px solid var(--accent);
}
.inspection strong {
display: block;
margin-bottom: 5px;
color: #265c5c;
font-size: 11px;
letter-spacing: 0.03em;
}
.inspection p {
margin: 0;
color: #526160;
font-size: 10px;
line-height: 1.55;
}
.attribution-note {
margin-top: 14px;
color: #7c8583;
font-size: 9px;
line-height: 1.55;
}
#stage {
position: absolute;
top: 82px;
right: 0;
bottom: 0;
left: 324px;
overflow: hidden;
background: #eee8dc;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
#loading {
position: absolute;
inset: 0;
display: grid;
place-items: center;
color: #697372;
background: #eee8dc;
z-index: 3;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.scene-label {
position: absolute;
z-index: 2;
min-width: 120px;
padding: 8px 10px;
color: #273131;
background: rgba(255, 252, 246, 0.93);
border: 1px solid rgba(87, 93, 89, 0.22);
box-shadow: 4px 4px 0 rgba(63, 62, 56, 0.11);
text-align: center;
pointer-events: none;
transform: translate(-50%, -100%);
}
.scene-label b {
display: block;
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.scene-label span {
display: block;
margin-top: 3px;
color: var(--accent);
font: 700 9px ui-monospace, SFMono-Regular, Menlo, monospace;
text-transform: uppercase;
}
.stage-hint {
position: absolute;
left: 50%;
bottom: 18px;
z-index: 2;
padding: 8px 12px;
color: #64706f;
background: rgba(251, 248, 241, 0.84);
border: 1px solid rgba(120, 115, 103, 0.2);
transform: translateX(-50%);
pointer-events: none;
white-space: nowrap;
font-size: 9px;
letter-spacing: 0.035em;
}
.legend {
position: absolute;
top: 16px;
left: 16px;
z-index: 2;
display: flex;
flex-wrap: wrap;
gap: 8px 13px;
max-width: calc(100% - 32px);
padding: 9px 11px;
background: rgba(251, 248, 241, 0.86);
border: 1px solid rgba(120, 115, 103, 0.2);
pointer-events: none;
color: #5c6665;
font-size: 9px;
letter-spacing: 0.025em;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 5px;
}
.swatch {
width: 10px;
height: 10px;
display: inline-block;
border: 1px solid rgba(0, 0, 0, 0.17);
}
.swatch.warp {
background: var(--warp);
}
.swatch.weft {
background: var(--weft);
}
.swatch.guide {
background: var(--accent);
}
@media (max-width: 860px) {
header {
height: 66px;
padding: 10px 14px;
}
.brand-mark {
width: 36px;
height: 36px;
}
.subtitle {
font-size: 10px;
}
.header-note {
display: none;
}
#panel {
top: auto;
right: 0;
bottom: 0;
left: 0;
width: auto;
height: 285px;
display: grid;
grid-template-columns: repeat(3, minmax(215px, 1fr));
gap: 14px;
padding: 14px;
border-top: 1px solid var(--line);
border-right: 0;
}
.section {
margin: 0;
padding: 0 12px 10px 0;
border-bottom: 0;
border-right: 1px solid var(--line);
}
.section:nth-child(3),
.section:nth-child(4) {
border-right: 0;
}
#stage {
top: 66px;
bottom: 285px;
left: 0;
}
.stage-hint {
bottom: 9px;
}
}
@media (max-width: 620px) {
#panel {
height: 260px;
grid-template-columns: repeat(2, minmax(205px, 1fr));
}
.section:nth-child(2) {
border-right: 0;
}
.section:nth-child(3),
.section:nth-child(4) {
border-top: 1px solid var(--line);
}
#stage {
bottom: 260px;
}
.subtitle {
max-width: 220px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.legend {
top: 9px;
left: 9px;
padding: 7px 8px;
}
.stage-hint {
max-width: calc(100% - 20px);
overflow: hidden;
text-overflow: ellipsis;
}
}
</style>
</head>
<body>
<div id="app">
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<span></span><span></span><span></span><span></span>
</div>
<div>
<h1>Interlace Workshop</h1>
<div class="subtitle">Enlarged voxel looms · plain and 2/2 twill structures</div>
</div>
</div>
<div class="header-note">
A structural comparison of interlacing, float direction, and repeat units.
</div>
</header>
<aside id="panel" aria-label="Workshop controls">
<section class="section">
<h2>Workshop View</h2>
<div class="segmented" aria-label="Choose loom view">
<button type="button" data-view="all" aria-pressed="true">Both</button>
<button type="button" data-view="plain" aria-pressed="false">Plain</button>
<button type="button" data-view="twill" aria-pressed="false">Twill</button>
</div>
<div class="control-row">
<label for="depth">Crossing separation</label>
<output id="depthValue" class="value-readout">0.34</output>
</div>
<input id="depth" type="range" min="0.18" max="0.62" step="0.01" value="0.34">
<div class="range-labels">
<span>Compact</span>
<span>Exaggerated</span>
</div>
<label class="check-row">
<input id="guides" type="checkbox" checked>
Show repeat guides
</label>
</section>
<section class="section">
<h2>Fiber Palette</h2>
<div class="palettes">
<button class="palette" type="button" data-palette="indigo" aria-pressed="true">
<i style="background:#41658a"></i><i style="background:#dfa64b"></i>
Indigo
</button>
<button class="palette" type="button" data-palette="moss" aria-pressed="false">
<i style="background:#54745c"></i><i style="background:#b96345"></i>
Moss
</button>
<button class="palette" type="button" data-palette="plum" aria-pressed="false">
<i style="background:#76516f"></i><i style="background:#d9c9a7"></i>
Plum
</button>
</div>
</section>
<section class="section">
<h2>Repeat Units</h2>
<div class="repeat-cards">
<article class="repeat-card">
<div id="plainGrid" class="mini-grid plain" aria-label="Plain weave two by two repeat"></div>
<div>
<h3>Plain</h3>
<div class="repeat-size">2 × 2 repeat</div>
<p>The over-under state reverses at every neighboring crossing.</p>
</div>
</article>
<article class="repeat-card">
<div id="twillGrid" class="mini-grid twill" aria-label="Two over two twill four by four repeat"></div>
<div>
<h3>2/2 Twill</h3>
<div class="repeat-size">4 × 4 repeat</div>
<p>Each row advances the warp-over sequence by one column, forming diagonal floats.</p>
</div>
</article>
</div>
</section>
<section class="section">
<h2>Crossing Inspector</h2>
<div class="inspection" aria-live="polite">
<strong id="inspectTitle">Select a crossing</strong>
<p id="inspectText">Click any square crossing on a loom to identify which thread is above and how the repeat is formed.</p>
</div>
<p class="attribution-note">
These are technical weave studies. No historic, ethnic, or cultural attribution is implied.
</p>
</section>
</aside>
<main id="stage" aria-label="Interactive three-dimensional loom comparison">
<div id="loading">Preparing the workshop…</div>
<div class="legend" aria-hidden="true">
<span><i class="swatch warp"></i>Warp / vertical</span>
<span><i class="swatch weft"></i>Weft / horizontal</span>
<span><i class="swatch guide"></i>Repeat guide</span>
</div>
<div id="labelPlain" class="scene-label">
<b>Plain</b>
<span>2 × 2 repeat</span>
</div>
<div id="labelTwill" class="scene-label">
<b>2/2 Twill</b>
<span>4 × 4 repeat</span>
</div>
<div class="stage-hint">
Drag to orbit · Shift-drag to pan · Scroll to zoom · Click a crossing to inspect
</div>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.querySelector("#stage");
const loading = document.querySelector("#loading");
const depthInput = document.querySelector("#depth");
const depthValue = document.querySelector("#depthValue");
const guidesInput = document.querySelector("#guides");
const inspectTitle = document.querySelector("#inspectTitle");
const inspectText = document.querySelector("#inspectText");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeee8dc);
scene.fog = new THREE.Fog(0xeee8dc, 19, 38);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 100);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("aria-label", "Two enlarged voxel-style weaving looms");
renderer.domElement.tabIndex = 0;
stage.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xfff9eb, 0x8b948d, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff3d7, 3.1);
keyLight.position.set(-6, 11, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xb8d4db, 1.0);
fillLight.position.set(8, 5, -7);
scene.add(fillLight);
const materials = {
warp: new THREE.MeshStandardMaterial({
color: 0x41658a,
roughness: 0.82,
metalness: 0,
flatShading: true
}),
weft: new THREE.MeshStandardMaterial({
color: 0xdfa64b,
roughness: 0.86,
metalness: 0,
flatShading: true
}),
frame: new THREE.MeshStandardMaterial({
color: 0x9b6a43,
roughness: 0.88,
flatShading: true
}),
frameDark: new THREE.MeshStandardMaterial({
color: 0x684c3a,
roughness: 0.92,
flatShading: true
}),
table: new THREE.MeshStandardMaterial({
color: 0xc3a77e,
roughness: 0.94,
flatShading: true
}),
floor: new THREE.MeshStandardMaterial({
color: 0xd9d3c7,
roughness: 1,
flatShading: true
}),
guide: new THREE.MeshBasicMaterial({
color: 0x1aa1a4,
transparent: true,
opacity: 0.95,
depthTest: false
})
};
const geometryCache = new Map();
function boxGeometry(w, h, d) {
const key = `${w.toFixed(4)}:${h.toFixed(4)}:${d.toFixed(4)}`;
if (!geometryCache.has(key)) {
geometryCache.set(key, new THREE.BoxGeometry(w, h, d));
}
return geometryCache.get(key);
}
function addBox(parent, w, h, d, x, y, z, material, castShadow = true) {
const mesh = new THREE.Mesh(boxGeometry(w, h, d), material);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Workshop table and floor.
addBox(scene, 50, 0.3, 40, 0, -1.65, 0, materials.floor, false);
addBox(scene, 16.7, 0.42, 5.4, 0, -0.24, 0.25, materials.table);
for (const x of [-7.2, 7.2]) {
for (const z of [-1.65, 2.15]) {
addBox(scene, 0.52, 1.55, 0.52, x, -0.93, z, materials.frameDark);
}
}
// Small voxel tools on the bench.
addBox(scene, 1.35, 0.14, 0.55, 0, 0.05, 2.0, materials.frameDark);
addBox(scene, 0.85, 0.13, 0.22, -0.1, 0.18, 2.0, materials.weft);
addBox(scene, 0.16, 0.5, 0.16, 0.48, 0.29, 2.0, materials.frame);
addBox(scene, 0.46, 0.16, 0.18, 0.48, 0.55, 2.0, materials.frameDark);
const crossingMeshes = [];
const loomGroups = {
plain: new THREE.Group(),
twill: new THREE.Group()
};
loomGroups.plain.position.set(-4.0, 0, 0);
loomGroups.twill.position.set(4.0, 0, 0);
scene.add(loomGroups.plain, loomGroups.twill);
let crossingDepth = Number(depthInput.value);
function warpIsOver(type, column, row) {
if (type === "plain") {
return (column + row) % 2 === 0;
}
return (((column - row) % 4) + 4) % 4 < 2;
}
function createLoom(type) {
const group = loomGroups[type];
const columns = 11;
const rows = 8;
const spacingX = 0.45;
const spacingY = 0.42;
const cells = [];
const pieces = [];
const xAt = c => (c - (columns - 1) / 2) * spacingX;
const yAt = r => 0.93 + r * spacingY;
// Voxel frame.
addBox(group, 6.15, 0.29, 0.52, 0, 0.48, 0, materials.frame);
addBox(group, 6.15, 0.29, 0.52, 0, 4.28, 0, materials.frame);
addBox(group, 0.28, 4.08, 0.52, -3.02, 2.38, 0, materials.frame);
addBox(group, 0.28, 4.08, 0.52, 3.02, 2.38, 0, materials.frame);
addBox(group, 0.42, 0.42, 0.62, -3.02, 0.48, 0, materials.frameDark);
addBox(group, 0.42, 0.42, 0.62, 3.02, 0.48, 0, materials.frameDark);
addBox(group, 0.42, 0.42, 0.62, -3.02, 4.28, 0, materials.frameDark);
addBox(group, 0.42, 0.42, 0.62, 3.02, 4.28, 0, materials.frameDark);
// Crossing blocks. Both yarns occupy every cell, but one is displaced forward.
for (let r = 0; r < rows; r++) {
cells[r] = [];
for (let c = 0; c < columns; c++) {
const over = warpIsOver(type, c, r);
const x = xAt(c);
const y = yAt(r);
const warpBlock = addBox(
group, 0.245, 0.245, 0.14, x, y,
over ? crossingDepth / 2 : -crossingDepth / 2,
materials.warp
);
const weftBlock = addBox(
group, 0.245, 0.245, 0.14, x, y,
over ? -crossingDepth / 2 : crossingDepth / 2,
materials.weft
);
const cellData = {
column: c,
row: r,
x,
y,
warpOver: over,
warpBlock,
weftBlock
};
for (const mesh of [warpBlock, weftBlock]) {
mesh.userData.loomType = type;
mesh.userData.cell = cellData;
crossingMeshes.push(mesh);
}
cells[r][c] = cellData;
}
}
function addPiece(role, endpointColumn, endpointRow, w, h, x, y) {
const piece = addBox(
group, w, h, 0.115, x, y,
0,
role === "warp" ? materials.warp : materials.weft
);
pieces.push({
mesh: piece,
role,
column: endpointColumn,
row: endpointRow
});
}
// Warp segments. Each segment is split into two voxel-depth steps.
for (let c = 0; c < columns; c++) {
for (let r = 0; r < rows - 1; r++) {
const y1 = yAt(r);
const y2 = yAt(r + 1);
const half = (y2 - y1) / 2;
addPiece("warp", c, r, 0.115, half, xAt(c), y1 + half / 2);
addPiece("warp", c, r + 1, 0.115, half, xAt(c), y2 - half / 2);
}
// Short warp tails to the upper and lower beams.
const firstY = yAt(0);
const bottomHalf = (firstY - 0.63) / 2;
addPiece("warp", c, 0, 0.115, bottomHalf, xAt(c), 0.63 + bottomHalf / 2);
const lastY = yAt(rows - 1);
const topHalf = (4.14 - lastY) / 2;
addPiece("warp", c, rows - 1, 0.115, topHalf, xAt(c), lastY + topHalf / 2);
}
// Weft segments, also stepped between crossing depths.
for (let r = 0; r < rows; r++) {
for (let c = 0; c < columns - 1; c++) {
const x1 = xAt(c);
const x2 = xAt(c + 1);
const half = (x2 - x1) / 2;
addPiece("weft", c, r, half, 0.115, x1 + half / 2, yAt(r));
addPiece("weft", c + 1, r, half, 0.115, x2 - half / 2, yAt(r));
}
const firstX = xAt(0);
const leftHalf = (firstX + 2.68) / 2;
addPiece("weft", 0, r, leftHalf, 0.115, -2.68 + leftHalf / 2, yAt(r));
const lastX = xAt(columns - 1);
const rightHalf = (2.68 - lastX) / 2;
addPiece("weft", columns - 1, r, rightHalf, 0.115, lastX + rightHalf / 2, yAt(r));
}
// Cyan corner brackets mark one complete repeat unit.
const guide = new THREE.Group();
group.add(guide);
const guideColumns = type === "plain" ? [4, 5] : [3, 4, 5, 6];
const guideRows = type === "plain" ? [3, 4] : [2, 3, 4, 5];
const minX = xAt(guideColumns[0]) - 0.17;
const maxX = xAt(guideColumns[guideColumns.length - 1]) + 0.17;
const minY = yAt(guideRows[0]) - 0.17;
const maxY = yAt(guideRows[guideRows.length - 1]) + 0.17;
const corner = 0.25;
const bar = 0.038;
for (const [x, sx] of [[minX, 1], [maxX, -1]]) {
for (const [y, sy] of [[minY, 1], [maxY, -1]]) {
addBox(guide, corner, bar, 0.035, x + sx * corner / 2, y, crossingDepth / 2 + 0.28, materials.guide, false);
addBox(guide, bar, corner, 0.035, x, y + sy * corner / 2, crossingDepth / 2 + 0.28, materials.guide, false);
}
}
group.userData = {
type,
columns,
rows,
xAt,
yAt,
cells,
pieces,
guide
};
updateLoomDepth(group);
return group;
}
function endpointZ(loom, role, column, row) {
const cell = loom.userData.cells[row][column];
const warpZ = cell.warpOver ? crossingDepth / 2 : -crossingDepth / 2;
return role === "warp" ? warpZ : -warpZ;
}
function updateLoomDepth(loom) {
const { cells, pieces, guide } = loom.userData;
for (const row of cells) {
for (const cell of row) {
cell.warpBlock.position.z = cell.warpOver ? crossingDepth / 2 : -crossingDepth / 2;
cell.weftBlock.position.z = cell.warpOver ? -crossingDepth / 2 : crossingDepth / 2;
}
}
for (const piece of pieces) {
piece.mesh.position.z = endpointZ(
loom,
piece.role,
piece.column,
piece.row
);
}
guide.position.z = crossingDepth / 2 + 0.28;
}
createLoom("plain");
createLoom("twill");
// Selection outline for inspected crossings.
const selectionOutline = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(0.44, 0.44, 0.9)),
new THREE.LineBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.95,
depthTest: false
})
);
selectionOutline.visible = false;
selectionOutline.renderOrder = 10;
scene.add(selectionOutline);
// Custom camera controls: orbit, pan, wheel zoom, and touch pinch.
const desiredTarget = new THREE.Vector3(0, 2.15, 0);
const currentTarget = desiredTarget.clone();
const desired = {
yaw: 0,
pitch: 0.24,
radius: 17.2
};
const current = {
yaw: desired.yaw,
pitch: desired.pitch,
radius: desired.radius
};
let activeView = "all";
function viewDistance() {
const narrow = camera.aspect < 1;
if (activeView === "all") return narrow ? 27 : 17.2;
return narrow ? 17.2 : 12.8;
}
function applyView(view) {
activeView = view;
loomGroups.plain.visible = view === "all" || view === "plain";
loomGroups.twill.visible = view === "all" || view === "twill";
desiredTarget.x = view === "plain" ? -4 : view === "twill" ? 4 : 0;
desiredTarget.y = 2.15;
desiredTarget.z = 0;
desired.yaw = 0;
desired.pitch = 0.24;
desired.radius = viewDistance();
document.querySelectorAll("[data-view]").forEach(button => {
button.setAttribute("aria-pressed", String(button.dataset.view === view));
});
}
document.querySelectorAll("[data-view]").forEach(button => {
button.addEventListener("click", () => applyView(button.dataset.view));
});
depthInput.addEventListener("input", () => {
crossingDepth = Number(depthInput.value);
depthValue.textContent = crossingDepth.toFixed(2);
updateLoomDepth(loomGroups.plain);
updateLoomDepth(loomGroups.twill);
});
guidesInput.addEventListener("change", () => {
loomGroups.plain.userData.guide.visible = guidesInput.checked;
loomGroups.twill.userData.guide.visible = guidesInput.checked;
});
const palettes = {
indigo: { warp: "#41658a", weft: "#dfa64b" },
moss: { warp: "#54745c", weft: "#b96345" },
plum: { warp: "#76516f", weft: "#d9c9a7" }
};
document.querySelectorAll("[data-palette]").forEach(button => {
button.addEventListener("click", () => {
const palette = palettes[button.dataset.palette];
materials.warp.color.set(palette.warp);
materials.weft.color.set(palette.weft);
document.documentElement.style.setProperty("--warp", palette.warp);
document.documentElement.style.setProperty("--weft", palette.weft);
document.querySelectorAll("[data-palette]").forEach(other => {
other.setAttribute("aria-pressed", String(other === button));
});
});
});
// Exact repeat diagrams generated from the same rules as the 3D looms.
function buildMiniGrid(element, type, columns, rows) {
element.replaceChildren();
for (let r = 0; r < rows; r++) {
for (let c = 0; c < columns; c++) {
const span = document.createElement("span");
span.className = warpIsOver(type, c, r) ? "warp-over" : "weft-over";
element.appendChild(span);
}
}
}
buildMiniGrid(document.querySelector("#plainGrid"), "plain", 2, 2);
buildMiniGrid(document.querySelector("#twillGrid"), "twill", 4, 4);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function visibleCrossingMeshes() {
return crossingMeshes.filter(mesh => loomGroups[mesh.userData.loomType].visible);
}
function intersectionsAt(clientX, clientY) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
return raycaster.intersectObjects(visibleCrossingMeshes(), false);
}
function inspectCrossing(clientX, clientY) {
const hit = intersectionsAt(clientX, clientY)[0];
if (!hit) return;
const cell = hit.object.userData.cell;
const type = hit.object.userData.loomType;
const loom = loomGroups[type];
const name = type === "plain" ? "Plain weave" : "2/2 twill";
selectionOutline.position.set(
loom.position.x + cell.x,
cell.y,
0
);
selectionOutline.visible = true;
inspectTitle.textContent =
`${name} · row ${cell.row + 1}, column ${cell.column + 1}`;
if (type === "plain") {
inspectText.textContent = cell.warpOver
? "The warp passes above the weft here. In plain weave, the over-under state reverses at every neighboring crossing."
: "The weft passes above the warp here. In plain weave, the over-under state reverses at every neighboring crossing.";
} else {
inspectText.textContent = cell.warpOver
? "The warp passes above the weft here. Advancing one row shifts the 2-over, 2-under sequence by one column, creating a diagonal float."
: "The weft passes above the warp here. Advancing one row shifts the 2-over, 2-under sequence by one column, creating a diagonal float.";
}
}
const pointers = new Map();
let dragDistance = 0;
let usedMultiTouch = false;
let pinchDistance = 0;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", event => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
dragDistance = 0;
if (pointers.size === 2) {
usedMultiTouch = true;
const values = [...pointers.values()];
pinchDistance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
});
canvas.addEventListener("pointermove", event => {
const previous = pointers.get(event.pointerId);
if (!previous) {
if (event.pointerType === "mouse") {
const hit = intersectionsAt(event.clientX, event.clientY);
canvas.style.cursor = hit.length ? "pointer" : "grab";
}
return;
}
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
previous.x = event.clientX;
previous.y = event.clientY;
dragDistance += Math.hypot(dx, dy);
if (pointers.size === 2) {
const values = [...pointers.values()];
const nextDistance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
if (pinchDistance > 0 && nextDistance > 0) {
desired.radius = THREE.MathUtils.clamp(
desired.radius * pinchDistance / nextDistance,
6.5,
36
);
}
pinchDistance = nextDistance;
canvas.style.cursor = "grabbing";
return;
}
canvas.style.cursor = "grabbing";
if (event.shiftKey) {
const factor = desired.radius * 0.00115;
const right = new THREE.Vector3(Math.cos(desired.yaw), 0, -Math.sin(desired.yaw));
desiredTarget.addScaledVector(right, -dx * factor);
desiredTarget.y = THREE.MathUtils.clamp(desiredTarget.y + dy * factor * 0.65, 0.5, 4.1);
desiredTarget.z = THREE.MathUtils.clamp(desiredTarget.z, -2.5, 2.5);
} else {
desired.yaw -= dx * 0.006;
desired.pitch = THREE.MathUtils.clamp(desired.pitch + dy * 0.005, 0.06, 0.78);
}
});
canvas.addEventListener("pointerup", event => {
const wasClick = dragDistance < 5 && !usedMultiTouch;
pointers.delete(event.pointerId);
if (pointers.size < 2) pinchDistance = 0;
if (wasClick) inspectCrossing(event.clientX, event.clientY);
if (pointers.size === 0) {
usedMultiTouch = false;
canvas.style.cursor = "grab";
}
});
canvas.addEventListener("pointercancel", event => {
pointers.delete(event.pointerId);
usedMultiTouch = false;
pinchDistance = 0;
canvas.style.cursor = "grab";
});
canvas.addEventListener("wheel", event => {
event.preventDefault();
desired.radius = THREE.MathUtils.clamp(
desired.radius * Math.exp(event.deltaY * 0.001),
6.5,
36
);
}, { passive: false });
canvas.addEventListener("dblclick", () => {
desiredTarget.set(
activeView === "plain" ? -4 :
activeView === "twill" ? 4 : 0,
2.15,
0
);
desired.yaw = 0;
desired.pitch = 0.24;
desired.radius = viewDistance();
});
canvas.style.cursor = "grab";
const plainLabel = document.querySelector("#labelPlain");
const twillLabel = document.querySelector("#labelTwill");
const projected = new THREE.Vector3();
function updateLabel(label, loom, localY) {
if (!loom.visible) {
label.style.display = "none";
return;
}
projected.set(0, localY, 0);
loom.localToWorld(projected);
projected.project(camera);
const width = stage.clientWidth;
const height = stage.clientHeight;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
const visible = projected.z > -1 && projected.z < 1;
label.style.display = visible ? "block" : "none";
label.style.left = `${x}px`;
label.style.top = `${y}px`;
}
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
if (!width || !height) return;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
resize();
applyView("all");
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const smoothing = 1 - Math.exp(-dt * 8);
currentTarget.lerp(desiredTarget, smoothing);
current.yaw = THREE.MathUtils.lerp(current.yaw, desired.yaw, smoothing);
current.pitch = THREE.MathUtils.lerp(current.pitch, desired.pitch, smoothing);
current.radius = THREE.MathUtils.lerp(current.radius, desired.radius, smoothing);
const cosPitch = Math.cos(current.pitch);
camera.position.set(
currentTarget.x + Math.sin(current.yaw) * cosPitch * current.radius,
currentTarget.y + Math.sin(current.pitch) * current.radius,
currentTarget.z + Math.cos(current.yaw) * cosPitch * current.radius
);
camera.lookAt(currentTarget);
camera.updateMatrixWorld();
if (selectionOutline.visible) {
selectionOutline.material.opacity = 0.72 + Math.sin(clock.elapsedTime * 4) * 0.22;
const pulse = 1 + Math.sin(clock.elapsedTime * 4) * 0.035;
selectionOutline.scale.set(pulse, pulse, 1);
}
updateLabel(plainLabel, loomGroups.plain, 4.68);
updateLabel(twillLabel, loomGroups.twill, 4.68);
renderer.render(scene, camera);
}
renderer.render(scene, camera);
loading.remove();
animate();
</script>
</body>
</html>
```