Prompt specification
Build or customize the Three.js 3D scene "Square-drive adapter family" 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: Square-drive adapter family
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/square-drive-adapter-family-x0309
Scene Description Prompt:
Square-drive adapter family. Arrange three socket adapters with distinct male and female square interfaces; expose both ends so compatible sizes can be matched by geometry rather than color alone. Please build this with Three.js from cdnjs in one self-contained HTML file, using stylized solid shapes.
Working Three.js Starter (square-drive-adapter-family-x0309):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Square-Drive Adapter Family</title>
<style>
:root { color-scheme: dark; }
* { box-sizing: border-box; }
html, body { margin:0; height:100%; background:#0d1013; overflow:hidden;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; }
canvas { display:block; }
.card {
position:fixed; z-index:10; pointer-events:none;
background:rgba(12,15,18,.72); border:1px solid rgba(255,255,255,.10);
border-radius:12px; backdrop-filter:blur(6px);
padding:12px 15px; color:#e8eef3;
}
#title { top:16px; left:16px; max-width:340px; }
#title h1 { margin:0 0 4px; font-size:15px; letter-spacing:.14em; text-transform:uppercase; }
#title p { margin:0; font-size:12px; line-height:1.55; color:#93a3b0; }
#hint { bottom:16px; left:16px; font-size:11.5px; color:#93a3b0; letter-spacing:.03em; }
#hint b { color:#e8eef3; font-weight:600; }
#tools { top:16px; right:16px; display:flex; gap:8px; pointer-events:auto; }
button {
background:rgba(24,30,35,.9); color:#dfe7ee; border:1px solid rgba(255,255,255,.14);
border-radius:8px; padding:8px 11px; font-size:11px; letter-spacing:.08em;
text-transform:uppercase; cursor:pointer; font-family:inherit;
}
button:hover { background:#2b343b; }
button.off { opacity:.45; }
</style>
</head>
<body>
<div id="app"></div>
<div class="card" id="title">
<h1>Square-Drive Adapter Family</h1>
<p>Three adapters, each with a square <b>male drive</b> and a square <b>female socket</b>.
Match by geometry: compare the drive cross-sections on the gauge with the socket openings.</p>
</div>
<div class="card" id="hint">
<b>Drag</b> orbit · <b>Wheel / pinch</b> zoom · <b>Double-click</b> reset
</div>
<div id="tools">
<button id="btnTop">Top view</button>
<button id="btnReset">Reset</button>
<button id="btnGauge">Gauge</button>
</div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
/* ---------------------------------------------------------------- renderer */
const app = document.getElementById('app');
const renderer = new THREE.WebGLRenderer({ antialias: true });
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.background = new THREE.Color(0x0d1013);
scene.fog = new THREE.Fog(0x0d1013, 13, 32);
const camera = new THREE.PerspectiveCamera(40, innerWidth / innerHeight, 0.1, 100);
/* ---------------------------------------------------------------- lighting */
scene.add(new THREE.HemisphereLight(0xa8bed2, 0x0a0c0e, 0.55));
const key = new THREE.DirectionalLight(0xfff2e0, 2.3);
key.position.set(4.5, 7.5, 5.5);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.near = 1;
key.shadow.camera.far = 22;
key.shadow.camera.left = -7; key.shadow.camera.right = 7;
key.shadow.camera.top = 7; key.shadow.camera.bottom = -7;
key.shadow.bias = -0.0012;
key.shadow.normalBias = 0.02;
scene.add(key);
const rim = new THREE.DirectionalLight(0x7fb2ff, 0.85);
rim.position.set(-5, 3.5, -5.5);
scene.add(rim);
const fill = new THREE.PointLight(0xffd9a8, 22, 14, 2);
fill.position.set(0, 3.2, 3.4);
scene.add(fill);
/* ---------------------------------------------------------------- materials */
const matBody = new THREE.MeshStandardMaterial({ color:0x333a41, metalness:0.72, roughness:0.40 });
const matSteel = new THREE.MeshStandardMaterial({ color:0xa9b4bd, metalness:0.88, roughness:0.30 });
const matPin = new THREE.MeshStandardMaterial({ color:0xe6ecf1, metalness:0.94, roughness:0.15 });
const matCavity = new THREE.MeshStandardMaterial({ color:0x1a1f24, metalness:0.45, roughness:0.70 });
const matGround = new THREE.MeshStandardMaterial({ color:0x11151a, metalness:0.05, roughness:0.95 });
const matStage = new THREE.MeshStandardMaterial({ color:0x181d22, metalness:0.15, roughness:0.80 });
const accentMats = [
new THREE.MeshStandardMaterial({ color:0x2f8f86, metalness:0.55, roughness:0.35 }),
new THREE.MeshStandardMaterial({ color:0xc08a2e, metalness:0.55, roughness:0.35 }),
new THREE.MeshStandardMaterial({ color:0xb4513f, metalness:0.55, roughness:0.35 })
];
/* ---------------------------------------------------------------- helpers */
function mesh(geo, mat, x = 0, y = 0, z = 0, cast = true) {
const m = new THREE.Mesh(geo, mat);
m.position.set(x, y, z);
m.castShadow = cast; m.receiveShadow = true;
return m;
}
function roundRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
}
function makeLabel(text, opts = {}) {
const fs = opts.fontSize || 64, pad = 20;
const probe = document.createElement('canvas').getContext('2d');
const font = `600 ${fs}px "Helvetica Neue", Helvetica, Arial, sans-serif`;
probe.font = font;
const w = Math.ceil(probe.measureText(text).width) + pad * 2;
const h = fs + pad * 2;
const c = document.createElement('canvas');
c.width = w; c.height = h;
const ctx = c.getContext('2d');
ctx.font = font;
ctx.fillStyle = opts.bg || 'rgba(10,13,16,0.82)';
roundRect(ctx, 0, 0, w, h, 12); ctx.fill();
if (opts.border) {
ctx.strokeStyle = opts.border; ctx.lineWidth = 4;
roundRect(ctx, 2, 2, w - 4, h - 4, 11); ctx.stroke();
}
ctx.fillStyle = opts.fg || '#eaf1f6';
ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
ctx.fillText(text, w / 2, h / 2 + 2);
const tex = new THREE.CanvasTexture(c);
tex.colorSpace = THREE.SRGBColorSpace;
tex.anisotropy = 4;
const spr = new THREE.Sprite(new THREE.SpriteMaterial({
map: tex, transparent: true, depthWrite: false
}));
const sc = opts.scale || 1;
spr.scale.set((w / h) * 0.24 * sc, 0.24 * sc, 1);
return spr;
}
/* ---------------------------------------------------------------- ground */
const ground = mesh(new THREE.PlaneGeometry(60, 60), matGround, 0, 0, 0, false);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
const stage = mesh(new THREE.CircleGeometry(5.6, 72), matStage, 0, 0.004, 0, false);
stage.rotation.x = -Math.PI / 2;
scene.add(stage);
/* ------------------------------------------------- adapter construction */
/*
Each adapter:
• chunky solid body (cylindrical barrel + hex base flange)
• FEMALE end: square socket opening cut into the top (4 solid collar
bars form a real square hole with a visible square floor)
• MALE end: square drive stub protruding sideways, with a shoulder
boss, chamfered tip and a detent hole
*/
function buildAdapter(cfg) {
const { s, accent, x, z, yaw, label } = cfg; // s = nominal drive side length
const open = s * 1.06; // socket mouth (slight clearance over drive)
const wall = 0.30 * s + 0.05; // collar wall thickness
const outer = open + 2 * wall; // collar outer square
const R = outer / Math.SQRT2 + 0.005; // barrel radius (collar corners flush)
const Hb = 1.05 + 0.40 * s; // total height
const depth = 0.50 * open + 0.12; // socket depth
const g = new THREE.Group();
g.position.set(x, 0, z);
g.rotation.y = yaw;
g.rotation.x = 0.17; // tip the socket mouth toward the viewer
/* hex base flange */
const flange = mesh(new THREE.CylinderGeometry(R + 0.14, R + 0.17, 0.20, 6), matBody, 0, 0.10, 0);
g.add(flange);
/* main barrel (top of barrel = socket floor level) */
const barrelH = Hb - depth - 0.20;
const barrel = mesh(new THREE.CylinderGeometry(R, R, barrelH, 20), matBody, 0, 0.20 + barrelH / 2, 0);
g.add(barrel);
/* accent grip ring */
const ring = mesh(new THREE.TorusGeometry(R + 0.025, 0.055, 10, 36), accent, 0, 0.20 + barrelH * 0.42, 0);
ring.rotation.x = Math.PI / 2;
g.add(ring);
/* FEMALE socket: four solid collar bars -> real square hole */
const y0 = Hb - depth, y1 = Hb, cy = (y0 + y1) / 2;
const front = mesh(new THREE.BoxGeometry(outer, depth, wall), matSteel, 0, cy, (open / 2 + wall / 2));
const back = mesh(new THREE.BoxGeometry(outer, depth, wall), matSteel, 0, cy, -(open / 2 + wall / 2));
const left = mesh(new THREE.BoxGeometry(wall, depth, open), matSteel, -(open / 2 + wall / 2), cy, 0);
const right = mesh(new THREE.BoxGeometry(wall, depth, open), matSteel, (open / 2 + wall / 2), cy, 0);
g.add(front, back, left, right);
/* square socket floor (makes the bottom read clearly as a square) */
const floor = mesh(new THREE.BoxGeometry(open + 0.02, 0.05, open + 0.02), matCavity, 0, y0 - 0.02, 0, false);
g.add(floor);
/* MALE square drive stub on the side */
const L = 0.60 + 0.70 * s;
const py = 0.55;
const x0 = -(R - 0.12), x1 = -(R + L);
const mainLen = (x1 - 0.12) - x0;
const main = mesh(new THREE.BoxGeometry(mainLen, s, s), matPin, x0 + mainLen / 2, py, 0);
const tip = mesh(new THREE.BoxGeometry(0.13, s * 0.78, s * 0.78), matPin, x1 - 0.065, py, 0);
g.add(main, tip);
/* shoulder boss where the drive enters the body */
const boss = mesh(new THREE.BoxGeometry(0.28, s + 0.26, s + 0.26), matSteel, -(R + 0.02), py, 0);
g.add(boss);
/* detent hole on the drive */
const det = mesh(new THREE.CylinderGeometry(s * 0.13, s * 0.13, 0.035, 14), matCavity,
x0 - L * 0.42, py + s / 2 + 0.008, 0, false);
g.add(det);
/* size label */
const lab = makeLabel(label, { scale: 1.0, border: 'rgba(255,255,255,0.22)' });
lab.position.set(0, Hb + 0.42, 0);
g.add(lab);
scene.add(g);
return g;
}
/* ---------------------------------------------------------------- family */
const SIZES = [
{ s: 0.35, label: '1/4"' },
{ s: 0.525, label: '3/8"' },
{ s: 0.70, label: '1/2"' }
];
const adapters = [];
const xs = [-2.15, 0, 2.15];
SIZES.forEach((sp, i) => {
adapters.push(buildAdapter({
s: sp.s, label: sp.label, accent: accentMats[i],
x: xs[i], z: [0.12, -0.06, 0.10][i], yaw: [-0.12, 0.07, -0.09][i]
}));
});
/* ------------------------------------------------- reference drive gauge */
const gauge = new THREE.Group();
gauge.position.set(0, 0, 1.75);
const rail = mesh(new THREE.BoxGeometry(4.1, 0.16, 0.62), matBody, 0, 0.08, 0);
gauge.add(rail);
const railTrim = mesh(new THREE.BoxGeometry(4.16, 0.05, 0.68), accentMats[1], 0, 0.155, 0);
gauge.add(railTrim);
SIZES.forEach((sp, i) => {
const px = [-1.25, 0, 1.25][i];
const peg = mesh(new THREE.BoxGeometry(sp.s, 0.40, sp.s), matPin, px, 0.36, 0);
const tip = mesh(new THREE.BoxGeometry(sp.s * 0.78, 0.10, sp.s * 0.78), matPin, px, 0.61, 0);
gauge.add(peg, tip);
const lab = makeLabel(sp.label, { fontSize: 52, scale: 0.62 });
lab.position.set(px, 0.30, 0.44);
gauge.add(lab);
});
const gaugeTitle = makeLabel('reference drives', { fontSize: 44, scale: 0.55, fg: '#9fb0bd', bg: 'rgba(10,13,16,0.6)' });
gaugeTitle.position.set(0, 0.30, -0.46);
gauge.add(gaugeTitle);
scene.add(gauge);
/* ---------------------------------------------------------------- camera rig */
const target = new THREE.Vector3(0, 0.85, 0.25);
const HOME = { theta: 0.34, phi: 0.94, r: 7.6 };
let cur = { theta: HOME.theta, phi: HOME.phi, r: HOME.r };
let dst = { ...HOME };
function applyCamera() {
const sp = Math.sin(cur.phi), cp = Math.cos(cur.phi);
camera.position.set(
target.x + cur.r * sp * Math.sin(cur.theta),
target.y + cur.r * cp,
target.z + cur.r * sp * Math.cos(cur.theta)
);
camera.lookAt(target);
}
applyCamera();
/* custom orbit controls (no addons) */
let dragging = false, lastX = 0, lastY = 0;
const pointers = new Map();
let pinchDist = 0;
renderer.domElement.addEventListener('pointerdown', e => {
pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
if (pointers.size === 1) { dragging = true; lastX = e.clientX; lastY = e.clientY; }
if (pointers.size === 2) {
const p = [...pointers.values()];
pinchDist = Math.hypot(p[0].x - p[1].x, p[0].y - p[1].y);
}
renderer.domElement.setPointerCapture(e.pointerId);
});
renderer.domElement.addEventListener('pointermove', e => {
if (!pointers.has(e.pointerId)) return;
pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
if (pointers.size === 2) {
const p = [...pointers.values()];
const d = Math.hypot(p[0].x - p[1].x, p[0].y - p[1].y);
if (pinchDist > 0) dst.r = THREE.MathUtils.clamp(dst.r * (pinchDist / d), 3.4, 14);
pinchDist = d;
return;
}
if (!dragging) return;
const dx = e.clientX - lastX, dy = e.clientY - lastY;
lastX = e.clientX; lastY = e.clientY;
dst.theta -= dx * 0.006;
dst.phi = THREE.MathUtils.clamp(dst.phi - dy * 0.005, 0.10, 1.42);
});
function endPointer(e) {
pointers.delete(e.pointerId);
if (pointers.size < 2) pinchDist = 0;
if (pointers.size === 0) dragging = false;
}
renderer.domElement.addEventListener('pointerup', endPointer);
renderer.domElement.addEventListener('pointercancel', endPointer);
renderer.domElement.addEventListener('pointerleave', endPointer);
renderer.domElement.addEventListener('wheel', e => {
e.preventDefault();
dst.r = THREE.MathUtils.clamp(dst.r * (1 + e.deltaY * 0.0012), 3.4, 14);
}, { passive: false });
renderer.domElement.addEventListener('dblclick', () => { dst = { ...HOME }; });
/* ---------------------------------------------------------------- buttons */
document.getElementById('btnReset').onclick = () => { dst = { ...HOME }; };
document.getElementById('btnTop').onclick = () => { dst.phi = 0.13; dst.theta = 0.0; dst.r = 6.6; };
const gaugeBtn = document.getElementById('btnGauge');
gaugeBtn.onclick = () => {
gauge.visible = !gauge.visible;
gaugeBtn.classList.toggle('off', !gauge.visible);
};
/* ---------------------------------------------------------------- loop */
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
}
addEventListener('resize', resize);
renderer.setAnimationLoop(() => {
const k = 0.12;
cur.theta += (dst.theta - cur.theta) * k;
cur.phi += (dst.phi - cur.phi) * k;
cur.r += (dst.r - cur.r) * k;
applyCamera();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```