Prompt specification
Build or customize the Three.js 3D scene "Hat brim shadow footprint" 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: Hat brim shadow footprint
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/hat-brim-shadow-footprint-x0589
Scene Description Prompt:
Make an HTML scene using Three.js from cdnjs where a voxel wide-brimmed hat casts directional shadows onto a head and shoulders.
Working Three.js Starter (hat-brim-shadow-footprint-x0589):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Hat — Directional Shadow</title>
<style>
:root {
color-scheme: dark;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #0b1020;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas.dragging {
cursor: grabbing;
}
.hud {
position: fixed;
top: 22px;
left: 22px;
z-index: 2;
max-width: min(360px, calc(100vw - 44px));
padding: 15px 17px;
color: #eef4ff;
background: rgba(12, 18, 35, 0.72);
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 14px;
box-shadow: 0 16px 45px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(14px);
pointer-events: none;
}
.hud h1 {
margin: 0 0 6px;
font-size: 17px;
font-weight: 700;
letter-spacing: 0.01em;
}
.hud p {
margin: 0;
color: #aebbd2;
font-size: 12px;
line-height: 1.55;
}
.hud strong {
color: #ffd794;
font-weight: 600;
}
#reset {
position: fixed;
top: 22px;
right: 22px;
z-index: 3;
padding: 10px 14px;
color: #eef4ff;
font: inherit;
font-size: 12px;
background: rgba(12, 18, 35, 0.72);
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 10px;
cursor: pointer;
backdrop-filter: blur(14px);
}
#reset:hover {
background: rgba(33, 48, 78, 0.9);
border-color: rgba(255, 255, 255, 0.25);
}
@media (max-width: 560px) {
.hud {
top: 14px;
left: 14px;
}
#reset {
top: auto;
right: 14px;
bottom: 14px;
}
}
</style>
</head>
<body>
<div class="hud">
<h1>Voxel Sun-Shadow Study</h1>
<p>
A wide-brimmed voxel hat blocks a warm directional light, casting a sharp
shadow across the face and shoulders.<br />
<strong>Drag</strong> to orbit · <strong>Scroll/pinch</strong> to zoom
</p>
</div>
<button id="reset" type="button">Reset view</button>
<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 renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel character wearing a wide-brimmed hat"
);
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0b1020);
scene.fog = new THREE.Fog(0x0b1020, 11, 24);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
50
);
const target = new THREE.Vector3(0, 1.9, 0);
// ------------------------------------------------------------
// Lighting: the main directional light creates the hat shadow
// ------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(
0xb9d8ff,
0x261b16,
0.48
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffdfad, 3.4);
sun.position.set(-4.5, 7.5, -3.8);
sun.target.position.set(0, 1.7, 0);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -5;
sun.shadow.camera.right = 5;
sun.shadow.camera.top = 5;
sun.shadow.camera.bottom = -5;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 20;
sun.shadow.bias = -0.0002;
sun.shadow.normalBias = 0.018;
scene.add(sun);
scene.add(sun.target);
// Very soft frontal fill keeps the shadow readable without erasing it.
const fill = new THREE.DirectionalLight(0x9fc8ff, 0.28);
fill.position.set(5, 4, 6);
scene.add(fill);
// ------------------------------------------------------------
// Materials and voxel helper
// ------------------------------------------------------------
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.84,
metalness: 0,
flatShading: true,
...options
});
}
function addBox(parent, size, position, mat, cast = true, receive = true) {
const mesh = new THREE.Mesh(cubeGeometry, mat);
mesh.scale.set(size[0], size[1], size[2]);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = cast;
mesh.receiveShadow = receive;
parent.add(mesh);
return mesh;
}
const coat = material(0x287b8c);
const coatDark = material(0x185766);
const coatLight = material(0x3d91a0);
const shirt = material(0xe7dfc7);
const belt = material(0x33251f);
const skin = material(0xd79d72);
const skinLight = material(0xe3ad82);
const hair = material(0x3b261c);
const eye = material(0x15151a);
const buckle = material(0xd9a94e, {
roughness: 0.42,
metalness: 0.65
});
const hatDark = material(0x382117);
const hatBrown = material(0x563321);
const hatMid = material(0x70452c);
const hatLight = material(0x81543a);
const hatBand = material(0x251510);
// ------------------------------------------------------------
// Ground
// ------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(36, 36),
material(0x131a2a, { roughness: 1 })
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.005;
ground.receiveShadow = true;
scene.add(ground);
// ------------------------------------------------------------
// Voxel character
// ------------------------------------------------------------
const character = new THREE.Group();
scene.add(character);
// Hips and torso
addBox(character, [1.36, 0.58, 0.82], [0, 0.29, 0], coatDark);
addBox(character, [1.55, 1.40, 0.90], [0, 1.15, 0], coat);
addBox(character, [1.58, 0.15, 0.93], [0, 0.56, 0], belt);
// Shoulders and arms
addBox(character, [0.64, 0.62, 0.91], [-0.96, 1.61, 0], coatLight);
addBox(character, [0.64, 0.62, 0.91], [0.96, 1.61, 0], coatLight);
addBox(character, [0.48, 1.02, 0.62], [-1.19, 0.91, 0], coatDark);
addBox(character, [0.48, 1.02, 0.62], [1.19, 0.91, 0], coatDark);
addBox(character, [0.42, 0.35, 0.50], [-1.19, 0.24, 0], skin);
addBox(character, [0.42, 0.35, 0.50], [1.19, 0.24, 0], skin);
// Shirt, lapels, and buttons
addBox(character, [0.50, 0.92, 0.09], [0, 1.36, 0.485], shirt);
addBox(character, [0.22, 0.77, 0.10], [-0.32, 1.40, 0.525], coatDark);
addBox(character, [0.22, 0.77, 0.10], [0.32, 1.40, 0.525], coatDark);
for (let y = 1.08; y <= 1.55; y += 0.19) {
addBox(character, [0.07, 0.07, 0.05], [0, y, 0.565], buckle);
}
// Neck
addBox(character, [0.49, 0.43, 0.47], [0, 1.94, 0], skin);
// Main head and cheek blocks
addBox(character, [1.12, 1.18, 0.92], [0, 2.55, 0], skin);
addBox(character, [0.18, 0.53, 0.77], [-0.50, 2.43, 0.08], skinLight);
addBox(character, [0.18, 0.53, 0.77], [0.50, 2.43, 0.08], skinLight);
// Ears
addBox(character, [0.18, 0.31, 0.28], [-0.63, 2.55, -0.01], skin);
addBox(character, [0.18, 0.31, 0.28], [0.63, 2.55, -0.01], skin);
// Hair behind and around the sides
addBox(character, [1.15, 0.88, 0.18], [0, 2.76, -0.43], hair);
addBox(character, [1.16, 0.18, 0.93], [0, 3.08, -0.04], hair);
addBox(character, [0.18, 0.66, 0.65], [-0.52, 2.76, -0.11], hair);
addBox(character, [0.18, 0.66, 0.65], [0.52, 2.76, -0.11], hair);
// Face details
addBox(character, [0.12, 0.13, 0.07], [-0.24, 2.72, 0.49], eye);
addBox(character, [0.12, 0.13, 0.07], [0.24, 2.72, 0.49], eye);
addBox(character, [0.21, 0.27, 0.19], [0, 2.51, 0.54], skinLight);
addBox(character, [0.23, 0.055, 0.07], [0, 2.25, 0.48], hair);
// ------------------------------------------------------------
// Wide-brimmed voxel hat
// ------------------------------------------------------------
const hat = new THREE.Group();
scene.add(hat);
const voxel = 0.255;
// Eleven-by-seven brim, with its four corner voxels removed.
for (let i = -5; i <= 5; i++) {
for (let j = -3; j <= 3; j++) {
if (Math.abs(i) === 5 && Math.abs(j) === 3) {
continue;
}
const edge = Math.abs(i) === 5 || Math.abs(j) === 3;
const variation = (i * 17 + j * 31 + 100) % 5;
let brimMaterial = hatBrown;
if (edge) brimMaterial = hatDark;
else if (variation === 0) brimMaterial = hatMid;
else if (variation === 1) brimMaterial = hatLight;
addBox(
hat,
[voxel, 0.16, voxel],
[i * voxel, 3.28, j * voxel],
brimMaterial
);
}
}
// Dark band around the crown
for (let i = -2; i <= 2; i++) {
for (let j = -1; j <= 1; j++) {
addBox(
hat,
[voxel, 0.14, voxel],
[i * voxel, 3.46, j * voxel],
hatBand
);
}
}
// First crown layer
for (let i = -2; i <= 2; i++) {
for (let j = -1; j <= 1; j++) {
const variation = (i * 7 + j * 11 + 20) % 4;
const crownMaterial =
variation === 0 ? hatMid :
variation === 1 ? hatLight :
hatBrown;
addBox(
hat,
[voxel, 0.26, voxel],
[i * voxel, 3.62, j * voxel],
crownMaterial
);
}
}
// Stepped upper crown
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
addBox(
hat,
[voxel, 0.20, voxel],
[i * voxel, 3.83, j * voxel],
hatMid
);
}
}
// Raised center ridge
for (let i = -1; i <= 1; i++) {
addBox(
hat,
[voxel, 0.16, voxel],
[i * voxel, 4.00, 0],
hatBrown
);
}
// Small front buckle
addBox(hat, [0.13, 0.14, 0.06], [0.35, 3.48, 0.405], buckle);
// ------------------------------------------------------------
// Custom camera controls — no examples/addons required
// ------------------------------------------------------------
const desired = {
yaw: 0.58,
pitch: 0.20,
distance: 8.1
};
const current = { ...desired };
const canvas = renderer.domElement;
function updateCamera(alpha) {
current.yaw += (desired.yaw - current.yaw) * alpha;
current.pitch += (desired.pitch - current.pitch) * alpha;
current.distance += (desired.distance - current.distance) * alpha;
const horizontalDistance =
Math.cos(current.pitch) * current.distance;
camera.position.set(
Math.sin(current.yaw) * horizontalDistance,
target.y + Math.sin(current.pitch) * current.distance,
Math.cos(current.yaw) * horizontalDistance
);
camera.lookAt(target);
}
const pointers = new Map();
let dragPointerId = null;
let lastX = 0;
let lastY = 0;
let pinchDistance = 0;
function getPinchDistance() {
const points = [...pointers.values()];
if (points.length < 2) return 0;
return Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
canvas.addEventListener("pointerdown", (event) => {
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
if (pointers.size === 1) {
dragPointerId = event.pointerId;
lastX = event.clientX;
lastY = event.clientY;
} else if (pointers.size === 2) {
dragPointerId = null;
pinchDistance = getPinchDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1 && dragPointerId === event.pointerId) {
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
desired.yaw -= dx * 0.006;
desired.pitch = THREE.MathUtils.clamp(
desired.pitch + dy * 0.005,
0.04,
0.72
);
lastX = event.clientX;
lastY = event.clientY;
} else if (pointers.size >= 2) {
const nextDistance = getPinchDistance();
if (pinchDistance > 0 && nextDistance > 0) {
desired.distance = THREE.MathUtils.clamp(
desired.distance * (pinchDistance / nextDistance),
5.2,
12
);
}
pinchDistance = nextDistance;
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 1) {
const [remainingId, point] = pointers.entries().next().value;
dragPointerId = remainingId;
lastX = point.x;
lastY = point.y;
} else {
dragPointerId = null;
pinchDistance = 0;
}
if (pointers.size === 0) {
canvas.classList.remove("dragging");
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desired.distance = THREE.MathUtils.clamp(
desired.distance * Math.exp(event.deltaY * 0.001),
5.2,
12
);
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "ArrowLeft":
desired.yaw += 0.08;
break;
case "ArrowRight":
desired.yaw -= 0.08;
break;
case "ArrowUp":
desired.pitch = Math.min(0.72, desired.pitch + 0.05);
break;
case "ArrowDown":
desired.pitch = Math.max(0.04, desired.pitch - 0.05);
break;
case "+":
case "=":
desired.distance = Math.max(5.2, desired.distance - 0.35);
break;
case "-":
desired.distance = Math.min(12, desired.distance + 0.35);
break;
}
});
document.getElementById("reset").addEventListener("click", () => {
desired.yaw = 0.58;
desired.pitch = 0.20;
desired.distance = 8.1;
});
// ------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const smoothing = 1 - Math.exp(-12 * dt);
updateCamera(smoothing);
renderer.render(scene, camera);
}
updateCamera(1);
animate();
</script>
</body>
</html>
```