Prompt specification
Build or customize the Three.js 3D scene "Mill cap turning toward a changing wind" 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: Mill cap turning toward a changing wind
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/mill-cap-turning-toward-a-changing-wind-x2558
Scene Description Prompt:
Use Three.js from cdnjs to create a referenced windmill upper section with rotating cap and fixed tower. Animate reorientation when the assumed wind direction changes, keeping the internal shaft connection visible. Deliver one self-contained HTML file with visible voxel blocks.
Working Three.js Starter (mill-cap-turning-toward-a-changing-wind-x2558):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Windmill — Upper Section</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(10, 20, 32, 0.78);
--border: rgba(154, 207, 255, 0.18);
--text: #edf7ff;
--muted: #91a9bb;
--cyan: #60e7ff;
--amber: #ffc86a;
--green: #88efb4;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 30%, #1b3348 0%, #0a1421 48%, #050a11 100%);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
outline: none;
}
#scene:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--border);
background: var(--panel);
border-radius: 16px;
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.28),
inset 0 1px rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.brand {
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 18px 20px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 14px var(--cyan);
}
h1 {
margin: 8px 0 5px;
font-size: clamp(21px, 2.7vw, 31px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.brand p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.controls {
top: 22px;
right: 22px;
width: min(318px, calc(100vw - 44px));
padding: 17px;
}
.control-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14px;
}
.control-title {
font-size: 11px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.status {
display: inline-flex;
align-items: center;
gap: 7px;
color: var(--green);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.status::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 12px currentColor;
}
.status.moving {
color: var(--amber);
}
.wind-row {
display: grid;
grid-template-columns: 104px 1fr;
gap: 16px;
align-items: center;
margin-bottom: 16px;
}
.compass {
position: relative;
width: 96px;
height: 96px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 50%;
background:
linear-gradient(90deg, transparent 49.5%, rgba(255,255,255,.09) 50%, transparent 50.5%),
linear-gradient(transparent 49.5%, rgba(255,255,255,.09) 50%, transparent 50.5%),
radial-gradient(circle, #142a3b, #09131f);
box-shadow: inset 0 0 24px rgba(0, 0, 0, 0.45);
}
.compass::before {
content: "";
position: absolute;
inset: 9px;
border: 1px dashed rgba(142, 202, 240, 0.18);
border-radius: 50%;
}
.needle {
position: absolute;
left: 50%;
top: 50%;
width: 3px;
height: 34px;
border-radius: 4px;
background: linear-gradient(to top, var(--cyan), #e8fbff);
transform-origin: 50% 100%;
transform: translate(-50%, -100%) rotate(0deg);
box-shadow: 0 0 10px rgba(96, 231, 255, 0.65);
transition: transform 180ms ease-out;
}
.needle::before {
content: "";
position: absolute;
left: 50%;
top: -2px;
width: 10px;
height: 10px;
border-radius: 50%;
background: #e8fbff;
transform: translateX(-50%);
}
.needle::after {
content: "";
position: absolute;
left: 50%;
bottom: -2px;
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--cyan);
transform: translateX(-50%);
}
.cardinal {
position: absolute;
color: #7893a8;
font-size: 9px;
font-weight: 900;
}
.north { top: 5px; left: 50%; transform: translateX(-50%); }
.south { bottom: 5px; left: 50%; transform: translateX(-50%); }
.east { right: 7px; top: 50%; transform: translateY(-50%); }
.west { left: 7px; top: 50%; transform: translateY(-50%); }
.readout-label {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.readout {
margin: 3px 0 11px;
font-size: 27px;
font-weight: 750;
letter-spacing: -0.04em;
}
.readout small {
color: var(--muted);
font-size: 11px;
font-weight: 700;
letter-spacing: 0;
}
input[type="range"] {
width: 100%;
accent-color: var(--cyan);
cursor: pointer;
}
.presets {
display: grid;
grid-template-columns: repeat(8, 1fr);
gap: 5px;
margin: 12px 0 14px;
}
button {
min-height: 34px;
padding: 8px 10px;
border: 1px solid rgba(255, 255, 255, 0.11);
border-radius: 9px;
background: rgba(255, 255, 255, 0.045);
color: #c9d9e5;
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.04em;
cursor: pointer;
transition: 160ms ease;
}
button:hover {
border-color: rgba(96, 231, 255, 0.55);
background: rgba(96, 231, 255, 0.09);
color: white;
transform: translateY(-1px);
}
button.active {
border-color: var(--cyan);
background: rgba(96, 231, 255, 0.13);
color: #dffaff;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.legend {
bottom: 22px;
left: 22px;
padding: 12px 14px;
display: flex;
gap: 16px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 2px;
box-shadow: 0 0 8px currentColor;
}
.swatch.stone { color: #8b9a9e; background: currentColor; }
.swatch.cap { color: #b66c50; background: currentColor; }
.swatch.shaft { color: #ffd56f; background: currentColor; }
.swatch.xray { color: var(--cyan); background: currentColor; }
.hint {
bottom: 22px;
right: 22px;
padding: 11px 14px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.03em;
pointer-events: none;
}
.world-label {
position: fixed;
z-index: 8;
left: 0;
top: 0;
padding: 6px 9px;
border: 1px solid rgba(96, 231, 255, 0.35);
border-radius: 7px;
background: rgba(6, 17, 27, 0.82);
color: var(--cyan);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -50%);
box-shadow: 0 0 20px rgba(41, 205, 239, 0.11);
}
.world-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 24px;
background: linear-gradient(var(--cyan), transparent);
}
@media (max-width: 760px) {
.brand {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 15px;
}
.brand p {
display: none;
}
.controls {
top: auto;
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 13px;
}
.wind-row {
grid-template-columns: 78px 1fr;
margin-bottom: 10px;
}
.compass {
width: 72px;
height: 72px;
}
.needle {
height: 25px;
}
.readout {
font-size: 21px;
}
.presets {
margin: 8px 0 10px;
}
.legend,
.hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel windmill upper section"></canvas>
<header class="brand panel">
<div class="eyebrow">Mechanical study 04</div>
<h1>Voxel Windmill</h1>
<p>
A fixed masonry tower and animated yaw cap. The cyan x-ray guide keeps
the internal vertical drive and rotor connection readable at every angle.
</p>
</header>
<aside class="controls panel">
<div class="control-head">
<div class="control-title">Assumed wind</div>
<div id="status" class="status" aria-live="polite">Locked to wind</div>
</div>
<div class="wind-row">
<div class="compass" aria-hidden="true">
<span class="cardinal north">N</span>
<span class="cardinal east">E</span>
<span class="cardinal south">S</span>
<span class="cardinal west">W</span>
<div id="needle" class="needle"></div>
</div>
<div>
<div class="readout-label">Wind source</div>
<div class="readout">
<span id="angle">000°</span>
<small id="cardinal">N</small>
</div>
<input
id="windSlider"
type="range"
min="0"
max="359"
step="1"
value="0"
aria-label="Wind direction in degrees"
/>
</div>
</div>
<div id="presets" class="presets">
<button data-angle="0" class="active">N</button>
<button data-angle="45">NE</button>
<button data-angle="90">E</button>
<button data-angle="135">SE</button>
<button data-angle="180">S</button>
<button data-angle="225">SW</button>
<button data-angle="270">W</button>
<button data-angle="315">NW</button>
</div>
<div class="button-row">
<button id="changeWind">Change wind</button>
<button id="toggleRotor">Pause rotor</button>
<button id="autoWind">Auto wind</button>
<button id="resetCamera">Reset view</button>
</div>
</aside>
<div id="shaftLabel" class="world-label">Live cutaway shaft</div>
<div class="legend panel">
<span class="legend-item"><i class="swatch stone"></i>Fixed tower</span>
<span class="legend-item"><i class="swatch cap"></i>Yaw cap</span>
<span class="legend-item"><i class="swatch shaft"></i>Drive train</span>
<span class="legend-item"><i class="swatch xray"></i>X-ray guide</span>
</div>
<div class="hint panel">Drag to orbit · Scroll to zoom · Double-click to reset</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 scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x08131f);
scene.fog = new THREE.FogExp2(0x08131f, 0.027);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
120
);
const clock = new THREE.Clock();
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const hemisphere = new THREE.HemisphereLight(0xbfe6ff, 0x26303a, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe2b6, 3.4);
sun.position.set(-8, 16, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -13;
sun.shadow.camera.right = 13;
sun.shadow.camera.top = 15;
sun.shadow.camera.bottom = -7;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 42;
sun.shadow.bias = -0.0005;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x5acbff, 1.8);
rim.position.set(10, 9, -12);
scene.add(rim);
const mechanismLight = new THREE.PointLight(0xffc767, 7, 8, 2);
mechanismLight.position.set(0, 5.8, 1.2);
scene.add(mechanismLight);
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const stonePalette = [0x718084, 0x879397, 0x657479, 0x9aa3a3, 0x7e8a8d];
const stoneMaterials = stonePalette.map(
(color) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.92,
metalness: 0.02
})
);
const materials = {
mortar: new THREE.MeshStandardMaterial({
color: 0x36454b,
roughness: 1
}),
wood: new THREE.MeshStandardMaterial({
color: 0x8e5437,
roughness: 0.8
}),
woodDark: new THREE.MeshStandardMaterial({
color: 0x4c2c24,
roughness: 0.88
}),
roof: new THREE.MeshStandardMaterial({
color: 0x8e4336,
roughness: 0.8
}),
roofLight: new THREE.MeshStandardMaterial({
color: 0xb45e47,
roughness: 0.76
}),
iron: new THREE.MeshStandardMaterial({
color: 0x24343d,
roughness: 0.48,
metalness: 0.72
}),
sail: new THREE.MeshStandardMaterial({
color: 0xe4dfcf,
roughness: 0.86,
side: THREE.DoubleSide
}),
sailEdge: new THREE.MeshStandardMaterial({
color: 0x5c3b2c,
roughness: 0.82
}),
gold: new THREE.MeshStandardMaterial({
color: 0xe9ad3f,
emissive: 0x6a3c08,
emissiveIntensity: 0.45,
roughness: 0.3,
metalness: 0.72
}),
goldXray: new THREE.MeshStandardMaterial({
color: 0xffc95e,
emissive: 0x9a5708,
emissiveIntensity: 0.75,
roughness: 0.26,
metalness: 0.7,
transparent: true,
opacity: 0.94,
depthTest: false,
depthWrite: false
}),
cyanXray: new THREE.MeshBasicMaterial({
color: 0x60e7ff,
transparent: true,
opacity: 0.5,
depthTest: false,
depthWrite: false
}),
window: new THREE.MeshStandardMaterial({
color: 0x102b3a,
emissive: 0x0a3144,
emissiveIntensity: 0.5,
roughness: 0.36
}),
ground: new THREE.MeshStandardMaterial({
color: 0x17232b,
roughness: 1
}),
wind: new THREE.MeshBasicMaterial({
color: 0x8be8ff,
transparent: true,
opacity: 0.24,
depthWrite: false
})
};
function voxel(parent, sx, sy, sz, material, x, y, z, rotationY = 0) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(sx, sy, sz);
mesh.position.set(x, y, z);
mesh.rotation.y = rotationY;
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function seededNoise(value) {
const n = Math.sin(value * 12.9898 + 78.233) * 43758.5453;
return n - Math.floor(n);
}
// -------------------------------------------------------------------------
// Ground and fixed plinth
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(90, 90),
materials.ground
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.23;
ground.receiveShadow = true;
scene.add(ground);
const plinth = new THREE.Group();
scene.add(plinth);
for (let x = -3; x <= 3; x++) {
for (let z = -3; z <= 3; z++) {
if (x * x + z * z > 12) continue;
voxel(
plinth,
0.72,
0.28,
0.72,
stoneMaterials[(x + z + 6) % stoneMaterials.length],
x * 0.72,
-0.08,
z * 0.72
);
}
}
// -------------------------------------------------------------------------
// Fixed voxel tower with an architectural cutaway
// -------------------------------------------------------------------------
const tower = new THREE.Group();
tower.name = "Fixed tower";
scene.add(tower);
const levels = 18;
const cell = 0.48;
const levelHeight = 0.43;
for (let level = 0; level < levels; level++) {
const radius = 3.05 - level * 0.035;
const innerRadius = radius - 1.38;
for (let x = -3; x <= 3; x++) {
for (let z = -3; z <= 3; z++) {
const radialDistance = Math.hypot(x, z);
if (
radialDistance > radius ||
radialDistance < innerRadius
) {
continue;
}
// Remove a front-center slice above the entrance to reveal the shaft.
const isCutaway =
level >= 2 &&
level <= 15 &&
z >= 0 &&
Math.abs(x) <= 1.25;
if (isCutaway) continue;
const colorIndex =
Math.floor(seededNoise(level * 31 + x * 7 + z * 13) * stoneMaterials.length) %
stoneMaterials.length;
voxel(
tower,
0.435,
0.385,
0.435,
stoneMaterials[colorIndex],
x * cell,
0.215 + level * levelHeight,
z * cell
);
}
}
}
// Entrance and small inset windows.
voxel(tower, 0.82, 1.25, 0.08, materials.woodDark, 0, 0.66, 1.48);
voxel(tower, 0.08, 1.35, 0.1, materials.iron, -0.45, 0.66, 1.53);
voxel(tower, 0.08, 1.35, 0.1, materials.iron, 0.45, 0.66, 1.53);
voxel(tower, 0.08, 1.35, 0.1, materials.iron, 0, 0.66, 1.53);
voxel(tower, 0.07, 0.58, 0.43, materials.window, 1.47, 4.65, 0.12, Math.PI / 2);
voxel(tower, 0.43, 0.58, 0.07, materials.window, -0.15, 4.65, -1.46);
// Cutaway edge supports emphasize the engineering section.
for (const x of [-1.02, 1.02]) {
voxel(tower, 0.09, 6.12, 0.09, materials.iron, x, 3.93, 0.28);
voxel(tower, 0.09, 0.09, 0.55, materials.iron, x, 6.98, 0.05);
}
// -------------------------------------------------------------------------
// Fixed vertical shaft and bearing frames
// -------------------------------------------------------------------------
const shaft = new THREE.Group();
shaft.name = "Fixed vertical drive shaft";
scene.add(shaft);
for (let i = 0; i < 18; i++) {
const segment = voxel(
shaft,
0.15,
0.39,
0.15,
materials.goldXray,
0,
0.42 + i * 0.41,
0
);
segment.renderOrder = 12;
}
// A slim cyan line remains visible even when the cap rotates away.
const xrayShaft = voxel(
shaft,
0.035,
7.25,
0.035,
materials.cyanXray,
0,
3.82,
0
);
xrayShaft.castShadow = false;
xrayShaft.receiveShadow = false;
xrayShaft.renderOrder = 20;
for (const y of [2.7, 5.15, 7.05]) {
voxel(tower, 1.75, 0.12, 0.13, materials.iron, 0, y, 0.38);
voxel(tower, 0.13, 0.12, 1.05, materials.iron, -0.78, y, 0.02);
voxel(tower, 0.13, 0.12, 1.05, materials.iron, 0.78, y, 0.02);
voxel(tower, 0.28, 0.28, 0.28, materials.goldXray, 0, y, 0.02);
}
// -------------------------------------------------------------------------
// Rotating cap, gearbox, tail and rotor
// -------------------------------------------------------------------------
const cap = new THREE.Group();
cap.name = "Rotating yaw cap";
cap.position.y = 7.52;
scene.add(cap);
// Open deck ring.
for (let x = -2; x <= 2; x++) {
for (let z = -2; z <= 2; z++) {
if (Math.abs(x) !== 2 && Math.abs(z) !== 2) continue;
voxel(cap, 0.49, 0.16, 0.49, materials.wood, x * 0.5, 0.04, z * 0.5);
}
}
// Skeletal timber frame leaves the drive train readable.
for (const x of [-1.03, 1.03]) {
for (const z of [-0.85, 0.85]) {
voxel(cap, 0.18, 1.35, 0.18, materials.woodDark, x, 0.73, z);
}
}
voxel(cap, 2.25, 0.18, 0.18, materials.woodDark, 0, 1.32, -0.82);
voxel(cap, 0.18, 0.18, 1.8, materials.woodDark, -1.0, 1.32, 0);
voxel(cap, 0.18, 0.18, 1.8, materials.woodDark, 1.0, 1.32, 0);
// Stepped voxel roof, deliberately open on the front half.
const roofWidths = [5, 5, 4, 3, 2, 1];
for (let level = 0; level < roofWidths.length; level++) {
const widthCells = roofWidths[level];
const halfWidth = Math.floor(widthCells / 2);
for (let x = -halfWidth; x <= halfWidth; x++) {
for (let z = 0; z < 3; z++) {
const material =
(x + z + level) % 3 === 0 ? materials.roofLight : materials.roof;
voxel(
cap,
0.47,
0.27,
0.47,
material,
x * 0.48,
1.12 + level * 0.27,
-1.35 + z * 0.48
);
}
}
}
// Rear tail boom and vane.
voxel(cap, 0.16, 0.16, 2.7, materials.woodDark, 0, 0.48, -2.05);
voxel(cap, 0.12, 0.12, 1.6, materials.wood, 0, 0.48, -2.1);
voxel(cap, 0.12, 1.35, 0.12, materials.woodDark, 0, 0.75, -3.18);
voxel(cap, 1.35, 0.64, 0.09, materials.roof, 0, 1.0, -3.18);
// Central rotating coupling.
const crownGear = new THREE.Group();
crownGear.position.y = 0.4;
cap.add(crownGear);
voxel(crownGear, 0.48, 0.16, 0.48, materials.goldXray, 0, 0, 0);
for (let i = 0; i < 12; i++) {
const angle = (i / 12) * Math.PI * 2;
const tooth = voxel(
crownGear,
0.17,
0.14,
0.23,
materials.goldXray,
Math.cos(angle) * 0.39,
0,
Math.sin(angle) * 0.39,
-angle
);
tooth.renderOrder = 12;
}
// Horizontal drive shaft from the crown gear to the front rotor axle.
for (let i = 0; i < 7; i++) {
const segment = voxel(
cap,
0.13,
0.13,
0.22,
materials.goldXray,
0,
0.73,
0.16 + i * 0.22
);
segment.renderOrder = 12;
}
voxel(cap, 0.86, 0.18, 0.18, materials.iron, 0, 0.73, 0.72);
voxel(cap, 0.18, 0.86, 0.18, materials.iron, 0, 0.73, 0.72);
voxel(cap, 0.2, 0.2, 0.42, materials.goldXray, 0, 0.73, 1.16);
const rotor = new THREE.Group();
rotor.name = "Spinning voxel sails";
rotor.position.set(0, 0.73, 1.55);
cap.add(rotor);
// Block-built hub and gear teeth.
voxel(rotor, 0.54, 0.54, 0.34, materials.woodDark, 0, 0, 0);
voxel(rotor, 0.22, 0.22, 0.48, materials.goldXray, 0, 0, 0.08);
for (let i = 0; i < 12; i++) {
const angle = (i / 12) * Math.PI * 2;
voxel(
rotor,
0.13,
0.19,
0.19,
materials.goldXray,
Math.cos(angle) * 0.35,
Math.sin(angle) * 0.35,
0.12,
angle
);
}
// Four lattice sails, built entirely from voxel blocks.
for (let bladeIndex = 0; bladeIndex < 4; bladeIndex++) {
const blade = new THREE.Group();
blade.rotation.z = bladeIndex * Math.PI * 0.5;
rotor.add(blade);
voxel(blade, 0.12, 3.12, 0.12, materials.sailEdge, 0, 1.56, 0);
voxel(blade, 0.08, 2.72, 0.1, materials.sailEdge, 0.86, 1.67, 0);
const ribPositions = [0.52, 1.03, 1.54, 2.05, 2.56, 2.98];
for (const y of ribPositions) {
voxel(blade, 0.92, 0.085, 0.1, materials.sailEdge, 0.44, y, 0);
}
const panelPositions = [0.78, 1.29, 1.80, 2.31, 2.77];
for (const y of panelPositions) {
voxel(blade, 0.73, 0.38, 0.065, materials.sail, 0.43, y, 0.015);
}
}
// -------------------------------------------------------------------------
// Wind streaks
// -------------------------------------------------------------------------
const windGroup = new THREE.Group();
scene.add(windGroup);
const windStreaks = [];
for (let i = 0; i < 34; i++) {
const streak = voxel(
windGroup,
0.035,
0.035,
0.78,
materials.wind,
0,
0,
0
);
streak.castShadow = false;
streak.receiveShadow = false;
windStreaks.push({
mesh: streak,
phase: seededNoise(i * 3.17),
lateral: (seededNoise(i * 8.91) - 0.5) * 13,
height: 1.2 + seededNoise(i * 4.28) * 9.5,
speed: 0.035 + seededNoise(i * 5.71) * 0.035
});
}
// -------------------------------------------------------------------------
// Direct camera controls, implemented without addons
// -------------------------------------------------------------------------
const cameraState = {
yaw: 0.34,
pitch: 0.13,
distance: 15.8,
target: new THREE.Vector3(0, 5.55, 0)
};
const defaultCamera = {
yaw: 0.34,
pitch: 0.13,
distance: 15.8
};
function updateCamera() {
const horizontalDistance =
cameraState.distance * Math.cos(cameraState.pitch);
camera.position.set(
cameraState.target.x +
Math.sin(cameraState.yaw) * horizontalDistance,
cameraState.target.y +
Math.sin(cameraState.pitch) * cameraState.distance,
cameraState.target.z +
Math.cos(cameraState.yaw) * horizontalDistance
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.yaw = defaultCamera.yaw;
cameraState.pitch = defaultCamera.pitch;
cameraState.distance = defaultCamera.distance;
updateCamera();
}
let dragState = null;
canvas.addEventListener("pointerdown", (event) => {
dragState = {
id: event.pointerId,
x: event.clientX,
y: event.clientY
};
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragState || dragState.id !== event.pointerId) return;
const deltaX = event.clientX - dragState.x;
const deltaY = event.clientY - dragState.y;
cameraState.yaw -= deltaX * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + deltaY * 0.0045,
-0.03,
0.72
);
dragState.x = event.clientX;
dragState.y = event.clientY;
updateCamera();
});
function endDrag(event) {
if (dragState && dragState.id === event.pointerId) {
dragState = null;
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance + event.deltaY * 0.012,
8.5,
28
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
updateCamera();
// -------------------------------------------------------------------------
// Wind controls and animation
// -------------------------------------------------------------------------
const windSlider = document.querySelector("#windSlider");
const angleReadout = document.querySelector("#angle");
const cardinalReadout = document.querySelector("#cardinal");
const statusReadout = document.querySelector("#status");
const needle = document.querySelector("#needle");
const presetButtons = [...document.querySelectorAll("[data-angle]")];
const rotorButton = document.querySelector("#toggleRotor");
const autoButton = document.querySelector("#autoWind");
let targetYaw = 0;
let currentYaw = 0;
let rotorRunning = true;
let autoTimer = null;
const cardinalNames = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];
function cardinalFor(degrees) {
return cardinalNames[Math.round(degrees / 45) % 8];
}
function setWind(degrees, fromSlider = false) {
const normalized = ((degrees % 360) + 360) % 360;
targetYaw = THREE.MathUtils.degToRad(normalized);
if (!fromSlider) {
windSlider.value = String(Math.round(normalized));
}
angleReadout.textContent =
String(Math.round(normalized)).padStart(3, "0") + "°";
cardinalReadout.textContent = cardinalFor(normalized);
needle.style.transform = `translate(-50%, -100%) rotate(${normalized}deg)`;
presetButtons.forEach((button) => {
const buttonAngle = Number(button.dataset.angle);
const difference = Math.abs(
((normalized - buttonAngle + 540) % 360) - 180
);
button.classList.toggle("active", difference < 0.5);
});
}
windSlider.addEventListener("input", () => {
setWind(Number(windSlider.value), true);
});
presetButtons.forEach((button) => {
button.addEventListener("click", () => {
setWind(Number(button.dataset.angle));
});
});
const windSequence = [0, 45, 90, 180, 270, 315];
let windSequenceIndex = 0;
document.querySelector("#changeWind").addEventListener("click", () => {
windSequenceIndex = (windSequenceIndex + 1) % windSequence.length;
setWind(windSequence[windSequenceIndex]);
});
rotorButton.addEventListener("click", () => {
rotorRunning = !rotorRunning;
rotorButton.textContent = rotorRunning ? "Pause rotor" : "Start rotor";
});
autoButton.addEventListener("click", () => {
if (autoTimer) {
clearInterval(autoTimer);
autoTimer = null;
autoButton.textContent = "Auto wind";
autoButton.classList.remove("active");
} else {
autoTimer = setInterval(() => {
windSequenceIndex = (windSequenceIndex + 1) % windSequence.length;
setWind(windSequence[windSequenceIndex]);
}, 4200);
autoButton.textContent = "Auto wind on";
autoButton.classList.add("active");
}
});
document.querySelector("#resetCamera").addEventListener("click", resetCamera);
setWind(0);
const shaftLabel = document.querySelector("#shaftLabel");
const labelPosition = new THREE.Vector3();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
// Shortest-path yaw interpolation.
const yawDifference = Math.atan2(
Math.sin(targetYaw - currentYaw),
Math.cos(targetYaw - currentYaw)
);
currentYaw += yawDifference * (1 - Math.exp(-delta * 2.35));
if (Math.abs(yawDifference) < 0.001) {
currentYaw = targetYaw;
}
cap.rotation.y = currentYaw;
if (rotorRunning) {
rotor.rotation.z -= delta * 0.78;
}
crownGear.rotation.y += delta * (rotorRunning ? 1.45 : 0);
// Wind streaks follow the requested source direction immediately while
// the physical cap eases into its new orientation.
const sourceAngle = targetYaw;
const sourceX = Math.sin(sourceAngle);
const sourceZ = Math.cos(sourceAngle);
const perpendicularX = Math.cos(sourceAngle);
const perpendicularZ = -Math.sin(sourceAngle);
for (const streak of windStreaks) {
const progress = (streak.phase + elapsed * streak.speed) % 1;
const radius = 10 - progress * 20;
streak.mesh.position.set(
sourceX * radius + perpendicularX * streak.lateral,
streak.height,
sourceZ * radius + perpendicularZ * streak.lateral
);
streak.mesh.rotation.y = sourceAngle;
}
const angleError = Math.abs(yawDifference);
const isMoving = angleError > 0.012;
statusReadout.textContent = isMoving ? "Reorienting" : "Locked to wind";
statusReadout.classList.toggle("moving", isMoving);
// Project the cutaway label to the shaft.
labelPosition.set(0, 5.35, 0.08).project(camera);
const labelX = (labelPosition.x * 0.5 + 0.5) * window.innerWidth;
const labelY = (-labelPosition.y * 0.5 + 0.5) * window.innerHeight;
shaftLabel.style.left = `${labelX + 48}px`;
shaftLabel.style.top = `${labelY}px`;
shaftLabel.style.opacity =
labelPosition.z > -1 && labelPosition.z < 1 ? "1" : "0";
renderer.render(scene, camera);
}
animate();
// -------------------------------------------------------------------------
// Responsive resize
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```