Prompt specification
Build or customize the Three.js 3D scene "Scarf loop and free ends" 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: Scarf loop and free ends
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/scarf-loop-and-free-ends-x0613
Scene Description Prompt:
Please use Three.js from cdnjs for a stylized scene based on this idea: scarf loop and free ends. Model a thick scarf draped around a cylindrical neck stand with two hanging ends; show continuous fabric path and avoid intersections where the loop overlaps. Return one self-contained HTML file.
Working Three.js Starter (scarf-loop-and-free-ends-x0613):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Scarf Study — procedural knit, Three.js</title>
<style>
:root{
--ink:#3b332c; --muted:#8a7c6d; --line:rgba(59,51,44,.18);
}
*{box-sizing:border-box}
html,body{height:100%;margin:0}
body{
background:radial-gradient(120% 90% at 50% 12%, #f7f1e7 0%, #ece3d5 45%, #d8cbb8 100%);
color:var(--ink);
font-family:"Helvetica Neue",Inter,system-ui,-apple-system,"Segoe UI",sans-serif;
overflow:hidden;
-webkit-font-smoothing:antialiased;
}
#app{position:fixed;inset:0}
canvas{display:block}
.vignette{
position:fixed;inset:0;pointer-events:none;
background:radial-gradient(115% 85% at 50% 42%, rgba(0,0,0,0) 52%, rgba(60,45,30,.16) 100%);
}
.hud{
position:fixed;left:34px;bottom:30px;max-width:330px;pointer-events:none;
text-shadow:0 1px 12px rgba(255,250,240,.6);
}
.kicker{
font-size:10.5px;letter-spacing:.34em;text-transform:uppercase;color:var(--muted);
margin-bottom:9px;
}
h1{
margin:0 0 10px;font-size:30px;font-weight:400;letter-spacing:.045em;
font-family:"Iowan Old Style","Palatino Linotype",Palatino,Georgia,serif;
}
.hud p{margin:0;font-size:12.5px;line-height:1.65;color:#6d6053;max-width:300px}
.rule{height:1px;background:var(--line);margin:14px 0 11px;width:190px}
.hint{font-size:10.5px;letter-spacing:.16em;text-transform:uppercase;color:#9a8c7c}
.credit{
position:fixed;right:30px;bottom:32px;font-size:10px;letter-spacing:.2em;
text-transform:uppercase;color:#9a8c7c;pointer-events:none;
}
@media (max-width:700px){ .hud{left:20px;bottom:20px} h1{font-size:23px} .credit{display:none} }
</style>
</head>
<body>
<div id="app"></div>
<div class="vignette"></div>
<div class="hud">
<div class="kicker">Three.js · procedural knit</div>
<h1>Scarf Study</h1>
<p>One continuous band — a single helical wrap around the neck form with two free ends, tuned so the overlapping turns never touch.</p>
<div class="rule"></div>
<div class="hint">drag to orbit · scroll to zoom</div>
</div>
<div class="credit">r160 · single sweep</div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
/* ------------------------------------------------------------------ */
/* renderer / scene */
/* ------------------------------------------------------------------ */
const app = document.getElementById('app');
const renderer = new THREE.WebGLRenderer({ antialias:true, alpha:false });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.06;
renderer.outputColorSpace = THREE.SRGBColorSpace;
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xe3d8c8, 11, 30);
const camera = new THREE.PerspectiveCamera(40, innerWidth/innerHeight, 0.1, 120);
/* ------------------------------------------------------------------ */
/* backdrop + ground */
/* ------------------------------------------------------------------ */
function backdropTexture(){
const c = document.createElement('canvas'); c.width = 8; c.height = 256;
const g = c.getContext('2d');
const grad = g.createLinearGradient(0,0,0,256);
grad.addColorStop(0.00,'#f8f2e8');
grad.addColorStop(0.42,'#eee5d6');
grad.addColorStop(0.72,'#ded1be');
grad.addColorStop(1.00,'#cbbda8');
g.fillStyle = grad; g.fillRect(0,0,8,256);
const t = new THREE.CanvasTexture(c);
t.colorSpace = THREE.SRGBColorSpace;
return t;
}
const backdrop = new THREE.Mesh(
new THREE.SphereGeometry(45, 32, 24),
new THREE.MeshBasicMaterial({ map: backdropTexture(), side: THREE.BackSide, fog:false })
);
scene.add(backdrop);
const FLOOR = -1.45;
const ground = new THREE.Mesh(
new THREE.CircleGeometry(20, 72),
new THREE.MeshStandardMaterial({ color:0xe7ddcd, roughness:1, metalness:0 })
);
ground.rotation.x = -Math.PI/2;
ground.position.y = FLOOR;
ground.receiveShadow = true;
scene.add(ground);
/* ------------------------------------------------------------------ */
/* lighting */
/* ------------------------------------------------------------------ */
scene.add(new THREE.HemisphereLight(0xfff4e6, 0x9c8d7a, 0.62));
const key = new THREE.DirectionalLight(0xfff0da, 2.15);
key.position.set(4.6, 6.2, 4.2);
key.castShadow = true;
key.shadow.mapSize.set(2048,2048);
key.shadow.camera.left = -4; key.shadow.camera.right = 4;
key.shadow.camera.top = 4; key.shadow.camera.bottom = -4;
key.shadow.camera.near = 1; key.shadow.camera.far = 22;
key.shadow.bias = -0.0006;
key.shadow.normalBias = 0.022;
key.shadow.radius = 2.2;
scene.add(key, key.target);
const fill = new THREE.DirectionalLight(0xd9e6f2, 0.55);
fill.position.set(-4.5, 2.4, -3.2);
scene.add(fill);
const rim = new THREE.DirectionalLight(0xffd9b0, 0.85);
rim.position.set(-1.2, 3.0, -5.2);
scene.add(rim);
/* ------------------------------------------------------------------ */
/* textures */
/* ------------------------------------------------------------------ */
function knitTexture(){
const S = 256;
const c = document.createElement('canvas'); c.width = c.height = S;
const g = c.getContext('2d');
g.fillStyle = '#efe4d3'; g.fillRect(0,0,S,S);
const period = 64; // 4 repeats per tile -> seamless
for (let y = 0; y < S; y++){
const t = (y % period) / period;
let col = '#e9dcc6';
if (t >= 0.26 && t < 0.47) col = '#bd5a44'; // terracotta band
else if (t >= 0.47 && t < 0.545) col = '#35565a'; // deep teal hairline
else if (t >= 0.745 && t < 0.80) col = '#c78a5c'; // sand hairline
g.fillStyle = col; g.fillRect(0, y, S, 1);
}
// soft V-stitches
g.strokeStyle = 'rgba(112,92,72,0.15)';
g.lineWidth = 2.2; g.lineJoin = 'round';
for (let yy = 4; yy < S; yy += 16)
for (let xx = 0; xx < S; xx += 16){
g.beginPath();
g.moveTo(xx+2, yy+2); g.lineTo(xx+8, yy+13); g.lineTo(xx+14, yy+2);
g.stroke();
}
// fibre speckle
const img = g.getImageData(0,0,S,S), d = img.data;
for (let i = 0; i < d.length; i += 4){
const n = (Math.random()-0.5) * 14;
d[i]+=n; d[i+1]+=n; d[i+2]+=n;
}
g.putImageData(img,0,0);
const t = new THREE.CanvasTexture(c);
t.wrapS = t.wrapT = THREE.RepeatWrapping;
t.colorSpace = THREE.SRGBColorSpace;
t.anisotropy = renderer.capabilities.getMaxAnisotropy();
return t;
}
function clayBump(){
const S = 256;
const c = document.createElement('canvas'); c.width = c.height = S;
const g = c.getContext('2d');
g.fillStyle = '#808080'; g.fillRect(0,0,S,S);
for (let i = 0; i < 700; i++){
g.strokeStyle = `rgba(${Math.random()>.5?255:0},${Math.random()>.5?255:0},${Math.random()>.5?255:0},0.045)`;
g.lineWidth = Math.random()*2.4 + 0.4;
const x = Math.random()*S;
g.beginPath(); g.moveTo(x, 0); g.lineTo(x + (Math.random()-0.5)*8, S); g.stroke();
}
const t = new THREE.CanvasTexture(c);
t.wrapS = t.wrapT = THREE.RepeatWrapping;
t.repeat.set(3,2);
return t;
}
/* ------------------------------------------------------------------ */
/* neck stand */
/* ------------------------------------------------------------------ */
const standGroup = new THREE.Group();
scene.add(standGroup);
const clayMat = new THREE.MeshStandardMaterial({
color: 0x93a1a4, roughness: 0.82, metalness: 0.0,
bumpMap: clayBump(), bumpScale: 0.035
});
const clayDark = new THREE.MeshStandardMaterial({ color:0x6f7c80, roughness:0.9 });
// base
const base = new THREE.Mesh(new THREE.CylinderGeometry(0.60, 0.66, 0.11, 56), clayDark);
base.position.y = FLOOR + 0.055;
base.castShadow = base.receiveShadow = true;
standGroup.add(base);
// column (the "neck")
const colH = 2.62;
const column = new THREE.Mesh(new THREE.CylinderGeometry(0.44, 0.50, colH, 56, 1), clayMat);
column.position.y = FLOOR + 0.11 + colH/2;
column.castShadow = column.receiveShadow = true;
standGroup.add(column);
// rounded top
const dome = new THREE.Mesh(new THREE.SphereGeometry(0.44, 48, 24, 0, Math.PI*2, 0, Math.PI/2), clayMat);
dome.scale.set(1, 0.62, 1);
dome.position.y = FLOOR + 0.11 + colH;
dome.castShadow = true;
standGroup.add(dome);
/* ------------------------------------------------------------------ */
/* THE SCARF — one continuous path */
/* */
/* path = tail A (hangs down, front-left) */
/* -> helical wrap of 360 deg around the neck form */
/* (pitch + radial growth guarantee the turns separate) */
/* -> tail B (hangs down, front-right, swung outward) */
/* ------------------------------------------------------------------ */
const P = (aDeg, r, y) => new THREE.Vector3(
r * Math.cos(aDeg * Math.PI/180),
y,
r * Math.sin(aDeg * Math.PI/180)
);
const pathPts = [];
// --- tail A : from its free end upward ---
[
[162, 0.64, -1.15],
[158, 0.62, -0.90],
[152, 0.60, -0.62],
[145, 0.57, -0.34],
[137, 0.55, -0.06],
[128, 0.54, 0.20],
[120, 0.54, 0.42],
[113, 0.54, 0.60]
].forEach(p => pathPts.push(P(p[0], p[1], p[2])));
// --- the wrap : a helix, 360 deg, rising 0.28 and growing 0.08 in radius ---
for (let a = 104; a <= 464; a += 30){
const t = (a - 104) / 360;
pathPts.push(P(a, 0.52 + 0.08*t, 0.78 + 0.28*t));
}
// --- tail B : down the front, swinging outward to clear the wrap ---
[
[106, 0.63, 1.00],
[110, 0.69, 0.92],
[116, 0.77, 0.78],
[121, 0.84, 0.56],
[124, 0.87, 0.28],
[123, 0.87, 0.00],
[119, 0.85, -0.28],
[113, 0.82, -0.55],
[107, 0.79, -0.82],
[101, 0.76, -1.02],
[ 96, 0.74, -1.12]
].forEach(p => pathPts.push(P(p[0], p[1], p[2])));
const scarfCurve = new THREE.CatmullRomCurve3(pathPts, false, 'centripetal', 0.5);
/* ------------------------------------------------------------------ */
/* sweep the band */
/* ------------------------------------------------------------------ */
function buildScarf(curve){
const N = 620; // samples along the path
const M = 26; // samples around the section
const HW = 0.205; // half width (band width)
const HT = 0.049; // half thickness
const SEC_N = 3.1; // superellipse exponent (soft rounded band)
const pts = curve.getSpacedPoints(N);
const n = pts.length;
// arc length
const arc = [0];
for (let i = 1; i < n; i++) arc.push(arc[i-1] + pts[i].distanceTo(pts[i-1]));
const total = arc[n-1];
// frames: tangent T, face normal N (radially outward), width axis B = N x T
const T = [], FN = [], FB = [];
for (let i = 0; i < n; i++){
const p0 = pts[Math.max(0, i-1)], p1 = pts[Math.min(n-1, i+1)];
const t = new THREE.Vector3().subVectors(p1, p0).normalize();
const Pn = pts[i];
let rl = Math.hypot(Pn.x, Pn.z) || 1;
const R = new THREE.Vector3(Pn.x/rl, 0, Pn.z/rl);
let nv = R.clone().addScaledVector(t, -R.dot(t));
if (nv.lengthSq() < 1e-8) nv.set(0,1,0).addScaledVector(t, -t.y);
nv.normalize();
const bv = new THREE.Vector3().crossVectors(nv, t).normalize();
T.push(t); FN.push(nv); FB.push(bv);
}
const pos = new Float32Array((n*M + 2) * 3);
const uvs = new Float32Array((n*M + 2) * 2);
const idx = [];
for (let i = 0; i < n; i++){
const p = pts[i], b = FB[i], f = FN[i], s = arc[i];
// knit ripple + slight thickness breathing
const swell = 1 + 0.16 * Math.sin(s * 13.0) + 0.06 * Math.sin(s * 41.0);
const ripple = 0.0075 * Math.sin(s * 21.0) + 0.0045 * Math.sin(s * 8.0 + 1.7);
for (let j = 0; j < M; j++){
const th = j / M * Math.PI * 2;
const cu = Math.cos(th), su = Math.sin(th);
const u = HW * Math.sign(cu) * Math.pow(Math.abs(cu), 2/SEC_N);
const v = HT * Math.sign(su) * Math.pow(Math.abs(su), 2/SEC_N) * swell + ripple * Math.cos(th);
const k = (i*M + j) * 3;
pos[k] = p.x + b.x*u + f.x*v;
pos[k+1] = p.y + b.y*u + f.y*v;
pos[k+2] = p.z + b.z*u + f.z*v;
const t2 = (i*M + j) * 2;
uvs[t2] = j / M;
uvs[t2+1] = s / total;
}
}
for (let i = 0; i < n-1; i++){
for (let j = 0; j < M; j++){
const j2 = (j+1) % M;
const a = i*M + j, b = i*M + j2, c = (i+1)*M + j2, d = (i+1)*M + j;
idx.push(a,b,c, a,c,d);
}
}
// end caps (fan)
const capA = n*M, capB = n*M + 1;
{
const p = pts[0], b = FB[0], f = FN[0];
const k = capA*3;
pos[k]=p.x; pos[k+1]=p.y; pos[k+2]=p.z;
const k2 = capB*3;
const q = pts[n-1], b2 = FB[n-1], f2 = FN[n-1];
pos[k2]=q.x; pos[k2+1]=q.y; pos[k2+2]=q.z;
uvs[capA*2]=0.5; uvs[capA*2+1]=0;
uvs[capB*2]=0.5; uvs[capB*2+1]=1;
for (let j = 0; j < M; j++){
const j2 = (j+1)%M;
idx.push(capA, j2, j);
idx.push(capB, (n-1)*M + j, (n-1)*M + j2);
}
}
const geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.BufferAttribute(pos, 3));
geo.setAttribute('uv', new THREE.BufferAttribute(uvs, 2));
geo.setIndex(idx);
geo.computeVertexNormals();
geo.computeBoundingSphere();
const tex = knitTexture();
tex.repeat.set(1, total / 0.55);
const mat = new THREE.MeshStandardMaterial({
map: tex, roughness: 0.92, metalness: 0.0,
side: THREE.FrontSide
});
const mesh = new THREE.Mesh(geo, mat);
mesh.castShadow = true;
mesh.receiveShadow = true;
const group = new THREE.Group();
group.add(mesh);
/* --- fringe at both free ends --- */
const fringeMat = new THREE.MeshStandardMaterial({ color:0xe8dcc6, roughness:0.95 });
const fringeGeo = new THREE.CylinderGeometry(0.0085, 0.005, 1, 6, 1, true);
const strandCount = 15;
for (const end of [0, n-1]){
const p = pts[end], b = FB[end], f = FN[end], t = T[end];
const dir = end === 0 ? t.clone().negate() : t.clone();
for (let s = 0; s < strandCount; s++){
const u = (s/(strandCount-1) - 0.5) * (HW*1.72);
const jitter = (Math.random()-0.5)*0.02;
const base = p.clone().addScaledVector(b, u).addScaledVector(f, jitter*0.4);
const len = 0.15 + Math.random()*0.07;
const m = new THREE.Mesh(fringeGeo, fringeMat);
m.scale.set(1, len, 1);
m.position.copy(base).addScaledVector(dir, len*0.5);
const q = new THREE.Quaternion().setFromUnitVectors(new THREE.Vector3(0,1,0), dir.clone().normalize());
m.quaternion.copy(q);
m.rotateOnAxis(new THREE.Vector3(0,1,0), 0);
m.castShadow = true;
group.add(m);
}
}
return group;
}
const scarf = buildScarf(scarfCurve);
scene.add(scarf);
/* ------------------------------------------------------------------ */
/* props: yarn ball + needles */
/* ------------------------------------------------------------------ */
function yarnTexture(){
const S = 256;
const c = document.createElement('canvas'); c.width = c.height = S;
const g = c.getContext('2d');
g.fillStyle = '#b8533f'; g.fillRect(0,0,S,S);
g.strokeStyle = 'rgba(255,225,205,0.28)'; g.lineWidth = 7;
for (let i = -S; i < S*2; i += 26){
g.beginPath(); g.moveTo(i, 0); g.lineTo(i - S, S); g.stroke();
}
g.strokeStyle = 'rgba(70,25,18,0.25)'; g.lineWidth = 3;
for (let i = -S; i < S*2; i += 26){
g.beginPath(); g.moveTo(i, S); g.lineTo(i - S, 0); g.stroke();
}
const t = new THREE.CanvasTexture(c);
t.colorSpace = THREE.SRGBColorSpace;
return t;
}
const yarn = new THREE.Mesh(
new THREE.SphereGeometry(0.21, 40, 28),
new THREE.MeshStandardMaterial({ map: yarnTexture(), roughness: 0.95 })
);
yarn.position.set(-1.22, FLOOR + 0.20, 0.86);
yarn.castShadow = yarn.receiveShadow = true;
scene.add(yarn);
const needleMat = new THREE.MeshStandardMaterial({ color:0x8a6642, roughness:0.6 });
for (let i = 0; i < 2; i++){
const nd = new THREE.Mesh(new THREE.CylinderGeometry(0.0095, 0.0075, 0.78, 10), needleMat);
nd.position.set(-1.05 + i*0.07, FLOOR + 0.30, 0.72 + i*0.05);
nd.rotation.set(1.16, 0.4 + i*0.3, 0.25 - i*0.5);
nd.castShadow = true;
scene.add(nd);
}
/* ------------------------------------------------------------------ */
/* camera orbit (implemented by hand — no addons) */
/* ------------------------------------------------------------------ */
const target = new THREE.Vector3(0, -0.05, 0);
let az = 0.62, pol = 1.24, rad = 5.4;
let azT = az, polT = pol, radT = rad;
let dragging = false, lastX = 0, lastY = 0, idle = 0;
const dom = renderer.domElement;
dom.style.touchAction = 'none';
dom.addEventListener('pointerdown', e => {
dragging = true; lastX = e.clientX; lastY = e.clientY; idle = 0;
dom.setPointerCapture(e.pointerId);
});
dom.addEventListener('pointermove', e => {
if (!dragging) return;
const dx = e.clientX - lastX, dy = e.clientY - lastY;
lastX = e.clientX; lastY = e.clientY; idle = 0;
azT -= dx * 0.0055;
polT = THREE.MathUtils.clamp(polT - dy * 0.0045, 0.30, 1.48);
});
const endDrag = e => { dragging = false; idle = 0; };
dom.addEventListener('pointerup', endDrag);
dom.addEventListener('pointercancel', endDrag);
dom.addEventListener('pointerleave', endDrag);
dom.addEventListener('wheel', e => {
e.preventDefault();
radT = THREE.MathUtils.clamp(radT + e.deltaY * 0.0024, 3.4, 13);
idle = 0;
}, { passive:false });
/* ------------------------------------------------------------------ */
addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
const clock = new THREE.Clock();
function animate(){
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
idle += dt;
if (idle > 2.6 && !dragging) azT += dt * 0.055; // slow idle drift
az += (azT - az ) * Math.min(1, dt * 7);
pol += (polT - pol) * Math.min(1, dt * 7);
rad += (radT - rad) * Math.min(1, dt * 7);
camera.position.set(
target.x + rad * Math.sin(pol) * Math.sin(az),
target.y + rad * Math.cos(pol),
target.z + rad * Math.sin(pol) * Math.cos(az)
);
camera.lookAt(target);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```