Prompt specification
Build or customize the Three.js 3D scene "Axle Rotates While Housing Stays Fixed" 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: Axle Rotates While Housing Stays Fixed
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/axle-rotates-while-housing-stays-fixed-x2006
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a supported axle rotating inside a stationary bearing housing in voxel style. Build the housing as a chunky cutaway block with a clearly visible bore, two support feet, and a shaft extending from both sides. Add a colored marker along the shaft and another on the fixed housing to make their relative motion obvious. The shaft turns about its own axis without sliding sideways, rising, or changing length. Keep the bearing region open on one side so the separation between moving and fixed parts is visible, even while the rotation is paused.
Working Three.js Starter (axle-rotates-while-housing-stays-fixed-x2006):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Axle in Cutaway Bearing Housing</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(15, 23, 34, 0.86);
--border: rgba(170, 207, 230, 0.2);
--orange: #ff9f1c;
--cyan: #2ee6d6;
--text: #e8f3fb;
--muted: #9db0c1;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 52% 38%, #23354a 0%, #111a26 45%, #080d14 100%);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
canvas {
position: fixed;
inset: 0;
display: block;
touch-action: none;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--border);
border-radius: 12px;
background: var(--panel);
box-shadow: 0 14px 35px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
}
#legend {
top: 18px;
left: 18px;
padding: 14px 16px;
pointer-events: none;
}
#legend h1 {
margin: 0 0 10px;
color: var(--text);
font-size: 13px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin-top: 7px;
color: var(--muted);
font-size: 12px;
}
.swatch {
width: 14px;
height: 14px;
border-radius: 3px;
box-shadow: 0 0 12px currentColor;
}
.swatch.orange {
color: var(--orange);
background: var(--orange);
}
.swatch.cyan {
color: var(--cyan);
background: var(--cyan);
}
#controls {
right: 18px;
bottom: 18px;
display: flex;
align-items: center;
gap: 9px;
padding: 10px;
}
button {
border: 1px solid rgba(160, 205, 235, 0.28);
border-radius: 8px;
padding: 10px 13px;
color: var(--text);
background: rgba(59, 83, 107, 0.72);
font: inherit;
font-size: 12px;
cursor: pointer;
transition: background 140ms ease, transform 140ms ease;
}
button:hover {
background: rgba(79, 111, 142, 0.9);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
#status {
min-width: 74px;
padding: 0 8px;
color: var(--muted);
font-size: 11px;
text-align: center;
letter-spacing: 0.08em;
}
#status.running {
color: #9fffa0;
}
#status.paused {
color: #ffd166;
}
#help {
position: fixed;
z-index: 10;
left: 18px;
bottom: 18px;
max-width: min(460px, calc(100vw - 36px));
padding: 10px 13px;
border: 1px solid var(--border);
border-radius: 9px;
color: var(--muted);
background: rgba(11, 18, 28, 0.68);
font-size: 11px;
line-height: 1.5;
pointer-events: none;
}
@media (max-width: 640px) {
#help {
display: none;
}
#legend {
top: 10px;
left: 10px;
padding: 10px 12px;
}
#controls {
right: 10px;
bottom: 10px;
}
}
</style>
</head>
<body>
<div id="legend" class="panel">
<h1>Axle / Bearing Cutaway</h1>
<div class="legend-row">
<span class="swatch orange"></span>
<span>Rotating shaft marker</span>
</div>
<div class="legend-row">
<span class="swatch cyan"></span>
<span>Fixed housing marker</span>
</div>
</div>
<div id="help">
Drag to orbit · Mouse wheel to zoom · Space to pause<br>
The camera-facing half of the housing remains cut away.
</div>
<div id="controls" class="panel">
<button id="pauseButton" type="button">Pause rotation</button>
<button id="resetButton" type="button">Reset view</button>
<span id="status" class="running" aria-live="polite">RUNNING</span>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const AXIS_Y = 1.55;
// ------------------------------------------------------------
// Renderer, scene, and camera
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(0x0b1018, 0);
renderer.domElement.style.touchAction = "none";
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x0b1018, 12, 27);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const orbit = {
target: new THREE.Vector3(0, 1.48, 0),
radius: 8.6,
theta: 0.72,
phi: 1.30
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
updateCamera();
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xc8e9ff, 0x111722, 2.15));
const keyLight = new THREE.DirectionalLight(0xfff0d8, 3.5);
keyLight.position.set(4.5, 8.5, 6.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x66cfff, 1.4);
fillLight.position.set(-6, 3, -4);
scene.add(fillLight);
const rimLight = new THREE.PointLight(0x2ee6d6, 16, 10, 2);
rimLight.position.set(-3.5, 2.3, 2.5);
scene.add(rimLight);
// ------------------------------------------------------------
// Materials
// ------------------------------------------------------------
const shaftMaterial = new THREE.MeshStandardMaterial({
color: 0xb9c9d4,
metalness: 0.82,
roughness: 0.26,
flatShading: true
});
const shaftDarkMaterial = new THREE.MeshStandardMaterial({
color: 0x263442,
metalness: 0.65,
roughness: 0.38,
flatShading: true
});
const bearingMaterial = new THREE.MeshStandardMaterial({
color: 0xc88d43,
metalness: 0.72,
roughness: 0.3,
flatShading: true
});
const boltMaterial = new THREE.MeshStandardMaterial({
color: 0x91a6b6,
metalness: 0.75,
roughness: 0.3,
flatShading: true
});
const footMaterial = new THREE.MeshStandardMaterial({
color: 0x354b60,
metalness: 0.28,
roughness: 0.72,
flatShading: true
});
const footTopMaterial = new THREE.MeshStandardMaterial({
color: 0x49657d,
metalness: 0.3,
roughness: 0.68,
flatShading: true
});
const movingMarkerMaterial = new THREE.MeshStandardMaterial({
color: 0xff9f1c,
emissive: 0xff6b00,
emissiveIntensity: 0.7,
metalness: 0.2,
roughness: 0.34
});
const fixedMarkerMaterial = new THREE.MeshStandardMaterial({
color: 0x2ee6d6,
emissive: 0x087e77,
emissiveIntensity: 0.8,
metalness: 0.18,
roughness: 0.34
});
// ------------------------------------------------------------
// Helpers
// ------------------------------------------------------------
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.copy(position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createAnnularCylinder(outerRadius, innerRadius, depth) {
const shape = new THREE.Shape();
shape.absarc(0, 0, outerRadius, 0, Math.PI * 2, false);
const hole = new THREE.Path();
hole.absarc(0, 0, innerRadius, 0, Math.PI * 2, true);
shape.holes.push(hole);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
steps: 1,
bevelEnabled: false,
curveSegments: 12
});
geometry.translate(0, 0, -depth / 2);
geometry.rotateY(Math.PI / 2);
return geometry;
}
// ------------------------------------------------------------
// Ground and grid
// ------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0x111a25,
metalness: 0.08,
roughness: 0.92
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.035;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 30, 0x416078, 0x253647);
grid.position.y = -0.018;
grid.material.transparent = true;
grid.material.opacity = 0.24;
scene.add(grid);
// ------------------------------------------------------------
// Voxel housing
//
// A rectangular voxel shell is built around the shaft.
// Cells intersecting the bore are removed, and the camera-facing
// side is cut away so the shaft/bearing separation is always clear.
// ------------------------------------------------------------
const housingCells = [];
const pitch = 0.38;
const nx = 9;
const ny = 7;
const nz = 5;
for (let ix = 0; ix < nx; ix++) {
for (let iy = 0; iy < ny; iy++) {
for (let iz = 0; iz < nz; iz++) {
const x = (ix - (nx - 1) / 2) * pitch;
const y = AXIS_Y + (iy - (ny - 1) / 2) * pitch;
const z = (iz - (nz - 1) / 2) * pitch;
const dy = y - AXIS_Y;
const radialDistance = Math.hypot(dy, z);
// Leave a continuous bore through the housing.
if (radialDistance < 0.56) continue;
// Open the camera-facing side around the bore.
if (z > 0 && Math.abs(dy) < 1.0) continue;
housingCells.push({
x,
y,
z,
ix,
iy,
iz,
cutEdge: z > 0 && Math.abs(dy) >= 1.0
});
}
}
}
const housingGeometry = new THREE.BoxGeometry(
pitch * 0.93,
pitch * 0.93,
pitch * 0.93
);
const housingMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
metalness: 0.18,
roughness: 0.72,
flatShading: true
});
const housing = new THREE.InstancedMesh(
housingGeometry,
housingMaterial,
housingCells.length
);
const dummy = new THREE.Object3D();
const housingPalette = [
0x344b60,
0x3b556b,
0x426077,
0x30475b
];
const cutPalette = [
0x52758a,
0x5b8194,
0x486d82
];
housingCells.forEach((cell, index) => {
dummy.position.set(cell.x, cell.y, cell.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
housing.setMatrixAt(index, dummy.matrix);
const palette = cell.cutEdge ? cutPalette : housingPalette;
const colorIndex =
(cell.ix * 7 + cell.iy * 11 + cell.iz * 5) % palette.length;
housing.setColorAt(
index,
new THREE.Color(palette[colorIndex])
);
});
housing.instanceMatrix.needsUpdate = true;
housing.instanceColor.needsUpdate = true;
housing.castShadow = true;
housing.receiveShadow = true;
housing.computeBoundingSphere();
scene.add(housing);
// ------------------------------------------------------------
// Two stepped bearing-housing support feet
// ------------------------------------------------------------
for (const side of [-1, 1]) {
const footX = side * 1.10;
addBox(
scene,
new THREE.Vector3(1.30, 0.28, 2.45),
new THREE.Vector3(footX, 0.14, 0),
footMaterial
);
addBox(
scene,
new THREE.Vector3(1.02, 0.30, 1.85),
new THREE.Vector3(footX, 0.42, 0),
footTopMaterial
);
addBox(
scene,
new THREE.Vector3(0.76, 0.34, 1.30),
new THREE.Vector3(footX, 0.70, 0),
footMaterial
);
// Four visible fasteners per foot.
for (const dx of [-0.43, 0.43]) {
for (const dz of [-0.83, 0.83]) {
const bolt = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.105, 0.10, 8),
boltMaterial
);
bolt.position.set(footX + dx, 0.32, dz);
bolt.castShadow = true;
bolt.receiveShadow = true;
scene.add(bolt);
}
}
}
// ------------------------------------------------------------
// Stationary bearing collars and visible circular bores
// ------------------------------------------------------------
const bearingGeometry = createAnnularCylinder(0.87, 0.48, 0.36);
for (const side of [-1, 1]) {
const bearing = new THREE.Mesh(
bearingGeometry,
bearingMaterial
);
bearing.position.set(side * 1.72, AXIS_Y, 0);
bearing.castShadow = true;
bearing.receiveShadow = true;
scene.add(bearing);
// Low-poly rim on the visible face.
const rimGeometry = new THREE.TorusGeometry(
0.61,
0.075,
6,
16
);
rimGeometry.rotateY(Math.PI / 2);
const rim = new THREE.Mesh(
rimGeometry,
boltMaterial
);
rim.position.set(side * 1.91, AXIS_Y, 0);
rim.castShadow = true;
scene.add(rim);
// Four fixed bolts around each bearing.
const faceX = side * 1.96;
for (let i = 0; i < 4; i++) {
const angle = i * Math.PI / 2 + Math.PI / 4;
addBox(
scene,
new THREE.Vector3(0.14, 0.14, 0.14),
new THREE.Vector3(
faceX,
AXIS_Y + Math.cos(angle) * 0.67,
Math.sin(angle) * 0.67
),
boltMaterial
);
}
}
// ------------------------------------------------------------
// Fixed cyan housing marker
// ------------------------------------------------------------
addBox(
scene,
new THREE.Vector3(0.54, 0.18, 0.12),
new THREE.Vector3(0, AXIS_Y + 1.02, 1.00),
fixedMarkerMaterial
);
const fixedPointer = new THREE.Mesh(
new THREE.ConeGeometry(0.11, 0.24, 4),
fixedMarkerMaterial
);
fixedPointer.position.set(0, AXIS_Y + 0.83, 1.02);
fixedPointer.rotation.z = Math.PI;
fixedPointer.castShadow = true;
scene.add(fixedPointer);
// ------------------------------------------------------------
// Rotating assembly
//
// Only rotor.rotation.x changes. Its position and scale remain
// constant, so the shaft cannot slide, rise, or change length.
// ------------------------------------------------------------
const rotor = new THREE.Group();
rotor.position.set(0, AXIS_Y, 0);
scene.add(rotor);
const shaftGeometry = new THREE.CylinderGeometry(
0.34,
0.34,
6.80,
12,
1,
false
);
shaftGeometry.rotateZ(Math.PI / 2);
const shaft = new THREE.Mesh(
shaftGeometry,
shaftMaterial
);
shaft.castShadow = true;
shaft.receiveShadow = true;
rotor.add(shaft);
// Stepped collars near both ends.
const collarGeometry = new THREE.CylinderGeometry(
0.44,
0.44,
0.34,
12
);
collarGeometry.rotateZ(Math.PI / 2);
for (const x of [-2.73, 2.73]) {
const collar = new THREE.Mesh(
collarGeometry,
shaftDarkMaterial
);
collar.position.x = x;
collar.castShadow = true;
collar.receiveShadow = true;
rotor.add(collar);
}
// End hubs, keeping the shaft visibly extended on both sides.
const hubGeometry = new THREE.CylinderGeometry(
0.38,
0.38,
0.28,
10
);
hubGeometry.rotateZ(Math.PI / 2);
for (const x of [-3.17, 3.17]) {
const hub = new THREE.Mesh(
hubGeometry,
shaftMaterial
);
hub.position.x = x;
hub.castShadow = true;
rotor.add(hub);
}
// Small dark longitudinal strips make the shaft's orientation clear.
const stripeA = addBox(
rotor,
new THREE.Vector3(0.30, 0.035, 0.15),
new THREE.Vector3(0.92, 0.356, 0),
shaftDarkMaterial
);
const stripeB = addBox(
rotor,
new THREE.Vector3(0.30, 0.035, 0.15),
new THREE.Vector3(-0.92, 0.356, 0),
shaftDarkMaterial
);
stripeB.rotation.x = Math.PI / 2;
// Bright orange marker attached to the shaft.
const movingMarker = addBox(
rotor,
new THREE.Vector3(0.64, 0.10, 0.22),
new THREE.Vector3(0, 0.39, 0),
movingMarkerMaterial
);
movingMarker.castShadow = true;
// ------------------------------------------------------------
// Direct camera controls: no addons
// ------------------------------------------------------------
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
orbit.theta -= deltaX * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - deltaY * 0.006,
0.30,
1.48
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
5.2,
14
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("dblclick", resetView);
function resetView() {
orbit.radius = 8.6;
orbit.theta = 0.72;
orbit.phi = 1.30;
updateCamera();
}
// ------------------------------------------------------------
// Rotation and pause controls
// ------------------------------------------------------------
const pauseButton = document.getElementById("pauseButton");
const resetButton = document.getElementById("resetButton");
const status = document.getElementById("status");
let paused = false;
let rotationAngle = 0.35;
function setPaused(value) {
paused = value;
pauseButton.textContent = paused
? "Resume rotation"
: "Pause rotation";
status.textContent = paused ? "PAUSED" : "RUNNING";
status.classList.toggle("paused", paused);
status.classList.toggle("running", !paused);
}
pauseButton.addEventListener("click", () => {
setPaused(!paused);
});
resetButton.addEventListener("click", resetView);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
setPaused(!paused);
}
});
// ------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) {
rotationAngle += delta * 0.85;
}
// The rotating assembly changes only its X-axis rotation.
rotor.rotation.x = rotationAngle;
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```