Prompt specification
Build or customize the Three.js 3D scene "L-Block and Matching Recess" 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: L-Block and Matching Recess
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/l-block-and-matching-recess-x3473
Scene Description Prompt:
Model an L-shaped block above a matching shallow recess with a small clearance. Show its footprint and permitted rotations without allowing diagonal penetration. Build this with Three.js from cdnjs as a voxel scene in one self-contained HTML file.
Working Three.js Starter (l-block-and-matching-recess-x3473):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>L-Block Voxel Insertion</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(8, 20, 35, 0.88);
--border: rgba(126, 218, 255, 0.22);
--text: #e6f6ff;
--muted: #8ca9bd;
--cyan: #58dcff;
--green: #4df0a6;
--amber: #ffbd69;
--red: #ff6674;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#brand {
position: fixed;
top: 20px;
left: 22px;
z-index: 2;
pointer-events: none;
text-shadow: 0 2px 15px #000;
}
#brand h1 {
margin: 0;
font-size: clamp(19px, 2vw, 28px);
letter-spacing: 0.05em;
font-weight: 750;
}
#brand p {
margin: 7px 0 0;
color: var(--muted);
font: 600 11px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#guard-badge {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 12px;
padding: 7px 10px;
border: 1px solid rgba(77, 240, 166, 0.3);
border-radius: 999px;
background: rgba(8, 29, 32, 0.72);
color: var(--green);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
#guard-badge::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
#panel {
position: fixed;
z-index: 3;
top: 18px;
right: 18px;
width: min(310px, calc(100vw - 36px));
max-height: calc(100vh - 36px);
overflow: auto;
padding: 17px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 20px 55px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(16px);
scrollbar-width: thin;
scrollbar-color: #31516b transparent;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 4px;
font-size: 14px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.tag {
padding: 4px 7px;
border: 1px solid rgba(88, 220, 255, 0.25);
border-radius: 6px;
color: var(--cyan);
background: rgba(88, 220, 255, 0.07);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
.subtitle {
margin: 0 0 14px;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.section-label {
display: flex;
align-items: center;
justify-content: space-between;
margin: 15px 0 8px;
color: #bad2e2;
font: 700 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.map-wrap {
display: grid;
grid-template-columns: 132px 1fr;
align-items: center;
gap: 12px;
padding: 10px;
border: 1px solid rgba(126, 218, 255, 0.12);
border-radius: 11px;
background: rgba(1, 10, 20, 0.38);
}
#footprint-map {
display: block;
width: 132px;
height: 132px;
border-radius: 8px;
background: #06101c;
}
.legend {
display: grid;
gap: 9px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.legend-item {
display: grid;
grid-template-columns: 10px 1fr;
align-items: start;
gap: 7px;
}
.swatch {
width: 10px;
height: 10px;
margin-top: 2px;
border-radius: 2px;
}
.swatch.recess {
border: 1px solid var(--amber);
background: rgba(255, 189, 105, 0.12);
}
.swatch.footprint {
background: rgba(88, 220, 255, 0.72);
}
.swatch.aligned {
background: rgba(77, 240, 166, 0.72);
}
.orientation-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 7px;
}
.orientation {
position: relative;
min-width: 0;
padding: 8px 4px 6px;
border: 1px solid rgba(126, 218, 255, 0.16);
border-radius: 9px;
background: rgba(255, 255, 255, 0.025);
color: var(--muted);
cursor: pointer;
transition:
border-color 150ms ease,
background 150ms ease,
color 150ms ease,
transform 150ms ease;
}
.orientation:hover:not(:disabled) {
transform: translateY(-1px);
border-color: rgba(88, 220, 255, 0.5);
color: var(--text);
}
.orientation.active {
border-color: var(--cyan);
background: rgba(88, 220, 255, 0.11);
color: var(--cyan);
box-shadow: inset 0 0 0 1px rgba(88, 220, 255, 0.12);
}
.orientation.fit::after {
content: "";
position: absolute;
top: 5px;
right: 5px;
width: 5px;
height: 5px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 7px rgba(77, 240, 166, 0.8);
}
.orientation svg {
display: block;
width: 36px;
height: 36px;
margin: 0 auto 3px;
}
.orientation span {
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.micro {
margin: 8px 0 0;
color: var(--muted);
font-size: 9.5px;
line-height: 1.4;
}
.micro strong {
color: var(--green);
font-weight: 700;
}
.status {
display: flex;
align-items: flex-start;
gap: 9px;
min-height: 48px;
margin: 14px 0 10px;
padding: 10px 11px;
border: 1px solid rgba(88, 220, 255, 0.15);
border-radius: 9px;
background: rgba(88, 220, 255, 0.05);
color: #b9d4e4;
font-size: 10.5px;
line-height: 1.45;
}
.status::before {
content: "";
flex: 0 0 auto;
width: 8px;
height: 8px;
margin-top: 3px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 8px rgba(88, 220, 255, 0.65);
}
.status.ok {
border-color: rgba(77, 240, 166, 0.2);
background: rgba(77, 240, 166, 0.055);
}
.status.ok::before {
background: var(--green);
box-shadow: 0 0 8px rgba(77, 240, 166, 0.65);
}
.status.warn {
border-color: rgba(255, 189, 105, 0.28);
background: rgba(255, 189, 105, 0.065);
color: #ffd9ae;
}
.status.warn::before {
background: var(--amber);
box-shadow: 0 0 8px rgba(255, 189, 105, 0.65);
}
.status.busy::before {
animation: pulse 900ms ease-in-out infinite;
}
@keyframes pulse {
50% {
opacity: 0.35;
transform: scale(0.75);
}
}
#lower-button {
width: 100%;
padding: 11px 12px;
border: 1px solid rgba(77, 240, 166, 0.42);
border-radius: 9px;
background: linear-gradient(135deg, #16745b, #135a56);
color: white;
cursor: pointer;
font: 750 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.07em;
text-transform: uppercase;
transition: filter 150ms ease, transform 150ms ease;
}
#lower-button:hover:not(:disabled) {
filter: brightness(1.13);
transform: translateY(-1px);
}
#lower-button.blocked {
border-color: rgba(255, 189, 105, 0.45);
background: linear-gradient(135deg, #694827, #533a28);
}
button:disabled {
cursor: not-allowed;
opacity: 0.42;
transform: none !important;
}
.clearances {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-top: 10px;
}
.clearance {
padding: 8px;
border: 1px solid rgba(126, 218, 255, 0.1);
border-radius: 8px;
background: rgba(255, 255, 255, 0.02);
}
.clearance small {
display: block;
margin-bottom: 3px;
color: var(--muted);
font: 700 8px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.clearance strong {
color: #d7f3ff;
font: 700 11px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
}
#hint {
position: fixed;
z-index: 2;
left: 22px;
bottom: 18px;
pointer-events: none;
padding: 8px 11px;
border: 1px solid rgba(126, 218, 255, 0.13);
border-radius: 8px;
background: rgba(4, 14, 26, 0.65);
color: var(--muted);
font: 600 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
}
@media (max-width: 700px) {
#brand p {
display: none;
}
#guard-badge {
margin-top: 8px;
}
#panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 48vh;
}
.map-wrap {
grid-template-columns: 104px 1fr;
}
#footprint-map {
width: 104px;
height: 104px;
}
#hint {
display: none;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<header id="brand">
<h1>L-BLOCK / SHALLOW RECESS</h1>
<p>21-cell voxel footprint · guarded vertical insertion</p>
<div id="guard-badge">CARDINAL YAW ONLY · NO DIAGONAL INSERTION</div>
</header>
<section id="panel" aria-label="Insertion controls">
<div class="panel-title">
<span>Insertion Check</span>
<span class="tag">1 VOXEL HIGH</span>
</div>
<p class="subtitle">
Rotate only in 90° steps while raised. A descent is permitted only
when the projected footprint exactly matches the recess.
</p>
<div class="section-label">
<span>Footprint</span>
<span>5 × 5 cells</span>
</div>
<div class="map-wrap">
<svg id="footprint-map" viewBox="0 0 125 125" aria-label="Recess and current footprint map"></svg>
<div class="legend">
<div class="legend-item">
<span class="swatch recess"></span>
<span>Amber cells form the shallow L-shaped recess.</span>
</div>
<div class="legend-item">
<span class="swatch footprint"></span>
<span>Cyan cells show the block’s projected footprint.</span>
</div>
<div class="legend-item">
<span class="swatch aligned"></span>
<span>Green means the footprint is insertion-compatible.</span>
</div>
</div>
</div>
<div class="section-label">
<span>Permitted Rotations</span>
<span>Above recess only</span>
</div>
<div class="orientation-grid">
<button class="orientation active fit" data-q="0" aria-label="Rotate to 0 degrees">
<svg viewBox="0 0 40 40" aria-hidden="true">
<path d="M5 4 H16 V24 H36 V35 H5 Z" fill="currentColor" opacity="0.82"/>
</svg>
<span>0°</span>
</button>
<button class="orientation" data-q="1" aria-label="Rotate to 90 degrees">
<svg viewBox="0 0 40 40" aria-hidden="true">
<path d="M5 4 H16 V24 H36 V35 H5 Z" fill="currentColor" opacity="0.82"
transform="rotate(90 20 20)"/>
</svg>
<span>90°</span>
</button>
<button class="orientation" data-q="2" aria-label="Rotate to 180 degrees">
<svg viewBox="0 0 40 40" aria-hidden="true">
<path d="M5 4 H16 V24 H36 V35 H5 Z" fill="currentColor" opacity="0.82"
transform="rotate(180 20 20)"/>
</svg>
<span>180°</span>
</button>
<button class="orientation" data-q="3" aria-label="Rotate to 270 degrees">
<svg viewBox="0 0 40 40" aria-hidden="true">
<path d="M5 4 H16 V24 H36 V35 H5 Z" fill="currentColor" opacity="0.82"
transform="rotate(270 20 20)"/>
</svg>
<span>270°</span>
</button>
</div>
<p class="micro">
Only the green <strong>0°</strong> footprint matches this asymmetric recess.
Diagonal yaw states are never created.
</p>
<div id="status" class="status" role="status" aria-live="polite">
<span id="status-text">Raised · footprint aligned.</span>
</div>
<button id="lower-button">Lower into recess</button>
<div class="clearances">
<div class="clearance">
<small>Floor gap</small>
<strong>0.15 voxel</strong>
</div>
<div class="clearance">
<small>Side gap</small>
<strong>0.06 voxel</strong>
</div>
</div>
</section>
<div id="hint">DRAG TO ORBIT · WHEEL / PINCH TO ZOOM · DOUBLE-CLICK TO RESET VIEW</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, lighting
// ---------------------------------------------------------------------
const stage = document.getElementById("stage");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 19, 38);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
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.15;
stage.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xa9ddff, 0x07101c, 1.65);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(7, 12, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x3ac8ff, 22, 18, 2);
rimLight.position.set(-6, 5, -5);
scene.add(rimLight);
const greenLight = new THREE.PointLight(0x42f0a5, 9, 13, 2);
greenLight.position.set(4, 2, 4);
scene.add(greenLight);
// ---------------------------------------------------------------------
// Dimensions and voxel occupancy
// ---------------------------------------------------------------------
const CELL = 1.0;
const BLOCK_WIDTH = 0.88;
const BLOCK_HEIGHT = 0.72;
const FLOOR_CLEARANCE = 0.15;
const RAISED_Y = 1.45;
const SEATED_Y = FLOOR_CLEARANCE;
// L footprint: a 5x5 cell region missing its front-right 2x2 quadrant.
const baseFootprint = [];
for (let z = -2; z <= 2; z++) {
for (let x = -2; x <= 2; x++) {
if (x <= 0 || z <= 0) {
baseFootprint.push([x, z]);
}
}
}
const cellKey = (x, z) => `${x},${z}`;
const cavitySet = new Set(baseFootprint.map(([x, z]) => cellKey(x, z)));
function rotateCell([x, z], quarterTurn) {
// Same cardinal transforms as an exact 90° rotation around the Y axis.
switch (quarterTurn) {
case 1:
return [z, -x];
case 2:
return [-x, -z];
case 3:
return [-z, x];
default:
return [x, z];
}
}
// ---------------------------------------------------------------------
// Ground and voxel platform
// ---------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(42, 42),
new THREE.MeshStandardMaterial({
color: 0x081421,
roughness: 0.95,
metalness: 0.05
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.03;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(40, 40, 0x1b3b55, 0x10283d);
grid.position.y = -1.015;
grid.material.transparent = true;
grid.material.opacity = 0.38;
scene.add(grid);
const platform = new THREE.Group();
scene.add(platform);
const wallGeometry = new THREE.BoxGeometry(0.96, 0.96, 0.96);
const floorGeometry = new THREE.BoxGeometry(0.96, 0.96, 0.96);
const wallMaterials = [
new THREE.MeshStandardMaterial({
color: 0x19374d,
roughness: 0.66,
metalness: 0.16
}),
new THREE.MeshStandardMaterial({
color: 0x21435a,
roughness: 0.62,
metalness: 0.16
}),
new THREE.MeshStandardMaterial({
color: 0x163047,
roughness: 0.68,
metalness: 0.13
})
];
const floorMaterials = [
new THREE.MeshStandardMaterial({
color: 0x102b40,
roughness: 0.76,
metalness: 0.12
}),
new THREE.MeshStandardMaterial({
color: 0x133147,
roughness: 0.76,
metalness: 0.12
})
];
for (let z = -4; z <= 4; z++) {
for (let x = -4; x <= 4; x++) {
const isCavity = cavitySet.has(cellKey(x, z));
const material = isCavity
? floorMaterials[(x + z + 10) % floorMaterials.length]
: wallMaterials[(x * 3 + z + 12) % wallMaterials.length];
const voxel = new THREE.Mesh(
isCavity ? floorGeometry : wallGeometry,
material
);
// The surrounding platform top is y = 0.97.
// The matching recess floor top is y = 0.
voxel.position.set(x, isCavity ? -0.48 : 0.49, z);
voxel.castShadow = true;
voxel.receiveShadow = true;
platform.add(voxel);
}
}
// ---------------------------------------------------------------------
// Cavity outline and floor tint
// ---------------------------------------------------------------------
const cavityOutlinePoints = [
new THREE.Vector3(-2.5, 0, -2.5),
new THREE.Vector3(2.5, 0, -2.5),
new THREE.Vector3(2.5, 0, 0.5),
new THREE.Vector3(0.5, 0, 0.5),
new THREE.Vector3(0.5, 0, 2.5),
new THREE.Vector3(-2.5, 0, 2.5)
];
const cavityLineMaterial = new THREE.LineBasicMaterial({
color: 0xffbd69,
transparent: true,
opacity: 0.95
});
const cavityTopOutline = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(
cavityOutlinePoints.map((p) => new THREE.Vector3(p.x, 1.005, p.z))
),
cavityLineMaterial
);
scene.add(cavityTopOutline);
const cavityFloorOutline = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(
cavityOutlinePoints.map((p) => new THREE.Vector3(p.x, 0.055, p.z))
),
cavityLineMaterial
);
scene.add(cavityFloorOutline);
const cavityShape = new THREE.Shape();
cavityShape.moveTo(-2.5, -2.5);
cavityShape.lineTo(2.5, -2.5);
cavityShape.lineTo(2.5, 0.5);
cavityShape.lineTo(0.5, 0.5);
cavityShape.lineTo(0.5, 2.5);
cavityShape.lineTo(-2.5, 2.5);
cavityShape.closePath();
const cavityFillGeometry = new THREE.ShapeGeometry(cavityShape);
cavityFillGeometry.rotateX(Math.PI / 2);
const cavityFill = new THREE.Mesh(
cavityFillGeometry,
new THREE.MeshBasicMaterial({
color: 0xffb75f,
transparent: true,
opacity: 0.075,
side: THREE.DoubleSide,
depthWrite: false
})
);
cavityFill.position.y = 0.018;
scene.add(cavityFill);
// ---------------------------------------------------------------------
// L-shaped voxel block
// ---------------------------------------------------------------------
const block = new THREE.Group();
block.position.y = RAISED_Y;
scene.add(block);
const blockGeometry = new THREE.BoxGeometry(
BLOCK_WIDTH,
BLOCK_HEIGHT,
BLOCK_WIDTH
);
const blockEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x073047,
transparent: true,
opacity: 0.72
});
const blockPalette = [
0x36b9df,
0x48c9e9,
0x2aa5cf,
0x5ad6ed,
0x39aed3
];
for (const [x, z] of baseFootprint) {
const material = new THREE.MeshStandardMaterial({
color: blockPalette[Math.abs(x * 3 + z * 5) % blockPalette.length],
roughness: 0.36,
metalness: 0.12
});
const voxel = new THREE.Mesh(blockGeometry, material);
voxel.position.set(x, BLOCK_HEIGHT / 2, z);
voxel.castShadow = true;
voxel.receiveShadow = true;
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(blockGeometry),
blockEdgeMaterial
);
voxel.add(edges);
block.add(voxel);
}
// ---------------------------------------------------------------------
// Projected footprint overlay
// ---------------------------------------------------------------------
const footprintFill = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.9, 0.024, 0.9),
new THREE.MeshBasicMaterial({
color: 0x4df0a6,
transparent: true,
opacity: 0.34,
depthWrite: false
}),
baseFootprint.length
);
footprintFill.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
footprintFill.renderOrder = 2;
scene.add(footprintFill);
const footprintOutlineMaterial = new THREE.LineBasicMaterial({
color: 0x4df0a6,
transparent: true,
opacity: 0.95
});
let footprintOutline = null;
function buildFootprintBoundary(cells) {
const edgeMap = new Map();
for (const [x, z] of cells) {
const corners = [
[x - 0.5, z - 0.5],
[x + 0.5, z - 0.5],
[x + 0.5, z + 0.5],
[x - 0.5, z + 0.5]
];
for (let i = 0; i < 4; i++) {
const a = corners[i];
const b = corners[(i + 1) % 4];
const key = [a, b]
.map(([cx, cz]) => `${cx},${cz}`)
.sort()
.join("|");
// Remove edges shared by adjacent cells.
if (edgeMap.has(key)) {
edgeMap.delete(key);
} else {
edgeMap.set(key, [a, b]);
}
}
}
const vertices = [];
for (const [a, b] of edgeMap.values()) {
vertices.push(
new THREE.Vector3(a[0], 0.065, a[1]),
new THREE.Vector3(b[0], 0.065, b[1])
);
}
return new THREE.BufferGeometry().setFromPoints(vertices);
}
// ---------------------------------------------------------------------
// Cardinal rotation indicators
// ---------------------------------------------------------------------
function makeLabelSprite(text, color = "#8ee9ff", width = 1.3, height = 0.42) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 144;
const context = canvas.getContext("2d");
context.fillStyle = "rgba(4, 16, 29, 0.88)";
context.fillRect(4, 4, 504, 136);
context.strokeStyle = color;
context.lineWidth = 5;
context.strokeRect(4, 4, 504, 136);
context.fillStyle = color;
context.font = "700 58px ui-monospace, SFMono-Regular, Menlo, monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 74);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(width, height, 1);
sprite.renderOrder = 10;
return sprite;
}
const rotationIndicators = [];
for (let q = 0; q < 4; q++) {
const baseAngle = q * Math.PI / 2;
const start = baseAngle - 0.34;
const end = baseAngle + 0.34;
const radius = 5.25;
const points = [];
for (let i = 0; i <= 36; i++) {
const angle = THREE.MathUtils.lerp(start, end, i / 36);
points.push(
new THREE.Vector3(
Math.cos(angle) * radius,
1.12,
Math.sin(angle) * radius
)
);
}
const arcMaterial = new THREE.LineBasicMaterial({
color: 0x58dcff,
transparent: true,
opacity: 0.45
});
const arc = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
arcMaterial
);
scene.add(arc);
const tangent = new THREE.Vector3(
-Math.sin(end),
0,
Math.cos(end)
).normalize();
const cone = new THREE.Mesh(
new THREE.ConeGeometry(0.14, 0.38, 7),
new THREE.MeshBasicMaterial({
color: 0x58dcff,
transparent: true,
opacity: 0.55
})
);
cone.position.copy(points[points.length - 1]);
cone.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent
);
scene.add(cone);
const label = makeLabelSprite(`${q * 90}°`);
label.position.set(
Math.cos(baseAngle) * 5.85,
1.5,
Math.sin(baseAngle) * 5.85
);
scene.add(label);
rotationIndicators.push({ arc, cone, label });
}
const blockLabel = makeLabelSprite(
"L-21 / CLEARANCE SAFE",
"#70e6ff",
3.0,
0.66
);
blockLabel.position.set(0, 3.0, 0);
scene.add(blockLabel);
// ---------------------------------------------------------------------
// HTML footprint map
// ---------------------------------------------------------------------
const SVG_NS = "http://www.w3.org/2000/svg";
const footprintMap = document.getElementById("footprint-map");
const currentMapLayer = document.createElementNS(SVG_NS, "g");
footprintMap.appendChild(currentMapLayer);
for (const [x, z] of baseFootprint) {
const rect = document.createElementNS(SVG_NS, "rect");
rect.setAttribute("x", String((x + 2) * 25 + 1));
rect.setAttribute("y", String((z + 2) * 25 + 1));
rect.setAttribute("width", "23");
rect.setAttribute("height", "23");
rect.setAttribute("rx", "2");
rect.setAttribute("fill", "#ffbd69");
rect.setAttribute("fill-opacity", "0.11");
rect.setAttribute("stroke", "#ffbd69");
rect.setAttribute("stroke-width", "1.5");
rect.setAttribute("stroke-opacity", "0.78");
footprintMap.appendChild(rect);
}
// ---------------------------------------------------------------------
// State, collision checks, and UI
// ---------------------------------------------------------------------
let orientation = 0;
let verticalState = "raised"; // raised, lowering, seated, raising
let transition = null;
let blockedUntil = 0;
const statusBox = document.getElementById("status");
const statusText = document.getElementById("status-text");
const lowerButton = document.getElementById("lower-button");
const orientationButtons = [
...document.querySelectorAll(".orientation")
];
let currentFootprint = [];
let footprintAligned = true;
function updateFootprint() {
currentFootprint = baseFootprint.map(([x, z]) =>
rotateCell([x, z], orientation)
);
footprintAligned =
currentFootprint.length === cavitySet.size &&
currentFootprint.every(([x, z]) => cavitySet.has(cellKey(x, z)));
const matrix = new THREE.Matrix4();
currentFootprint.forEach(([x, z], index) => {
matrix.makeTranslation(x, 0.037, z);
footprintFill.setMatrixAt(index, matrix);
});
footprintFill.instanceMatrix.needsUpdate = true;
const color = footprintAligned ? 0x4df0a6 : 0x58dcff;
footprintFill.material.color.setHex(color);
footprintOutlineMaterial.color.setHex(color);
if (footprintOutline) {
scene.remove(footprintOutline);
footprintOutline.geometry.dispose();
}
footprintOutline = new THREE.LineSegments(
buildFootprintBoundary(currentFootprint),
footprintOutlineMaterial
);
footprintOutline.renderOrder = 3;
scene.add(footprintOutline);
// Update the two-dimensional footprint inset.
currentMapLayer.replaceChildren();
for (const [x, z] of currentFootprint) {
const rect = document.createElementNS(SVG_NS, "rect");
rect.setAttribute("x", String((x + 2) * 25 + 1));
rect.setAttribute("y", String((z + 2) * 25 + 1));
rect.setAttribute("width", "23");
rect.setAttribute("height", "23");
rect.setAttribute("rx", "2");
rect.setAttribute("fill", footprintAligned ? "#4df0a6" : "#58dcff");
rect.setAttribute("fill-opacity", "0.74");
rect.setAttribute("stroke", footprintAligned ? "#8dffd0" : "#bdefff");
rect.setAttribute("stroke-width", "1");
currentMapLayer.appendChild(rect);
}
orientationButtons.forEach((button, index) => {
button.classList.toggle("active", index === orientation);
});
rotationIndicators.forEach((indicator, index) => {
const active = index === orientation;
indicator.arc.material.opacity = active ? 1 : 0.32;
indicator.cone.material.opacity = active ? 1 : 0.32;
indicator.label.material.opacity = active ? 1 : 0.45;
});
}
function updateStatus() {
statusBox.className = "status";
lowerButton.classList.remove("blocked");
if (performance.now() < blockedUntil) {
statusBox.classList.add("warn");
statusText.textContent =
"BLOCKED — the rotated footprint does not match the recess.";
lowerButton.classList.add("blocked");
} else if (verticalState === "lowering") {
statusBox.classList.add("busy");
statusText.textContent =
"Lowering vertically — the fixed cardinal footprint is collision-checked.";
} else if (verticalState === "raising") {
statusBox.classList.add("busy");
statusText.textContent =
"Raising vertically — rotation remains locked until fully clear.";
} else if (verticalState === "seated") {
statusBox.classList.add("ok");
statusText.textContent =
"Seated safely — 0.15 voxel floor clearance and 0.06 voxel side clearance.";
} else if (footprintAligned) {
statusBox.classList.add("ok");
statusText.textContent =
"Raised · footprint aligned. Vertical lowering is permitted.";
} else {
statusBox.classList.add("warn");
statusText.textContent =
"Raised · footprint mismatch. Lowering is blocked until 0° is restored.";
lowerButton.classList.add("blocked");
}
const busy = verticalState === "lowering" || verticalState === "raising";
lowerButton.disabled = busy;
lowerButton.textContent =
verticalState === "seated"
? "Raise block"
: verticalState === "lowering"
? "Lowering…"
: verticalState === "raising"
? "Raising…"
: "Lower into recess";
orientationButtons.forEach((button) => {
button.disabled = verticalState !== "raised";
});
}
function beginVerticalTransition(nextState, targetY, duration) {
verticalState =
nextState === "seated" ? "lowering" : "raising";
transition = {
from: block.position.y,
to: targetY,
start: performance.now(),
duration,
nextState
};
updateStatus();
}
orientationButtons.forEach((button) => {
button.addEventListener("click", () => {
// Rotations are only allowed while fully raised.
if (verticalState !== "raised") {
return;
}
orientation = Number(button.dataset.q);
// Deliberately exact cardinal rotations only.
// There is no interpolated diagonal yaw state.
block.rotation.y = orientation * Math.PI / 2;
updateFootprint();
updateStatus();
});
});
lowerButton.addEventListener("click", () => {
if (verticalState === "raised") {
if (!footprintAligned) {
blockedUntil = performance.now() + 1200;
cavityLineMaterial.color.setHex(0xff6674);
window.setTimeout(() => {
cavityLineMaterial.color.setHex(0xffbd69);
updateStatus();
}, 1200);
updateStatus();
return;
}
beginVerticalTransition("seated", SEATED_Y, 760);
} else if (verticalState === "seated") {
beginVerticalTransition("raised", RAISED_Y, 650);
}
});
updateFootprint();
updateStatus();
// ---------------------------------------------------------------------
// Camera controls implemented directly, without addons
// ---------------------------------------------------------------------
const orbit = {
theta: 0.68,
phi: 0.88,
radius: 14.5
};
const orbitTarget = new THREE.Vector3(0, 0.45, 0);
const pointers = new Map();
let lastPointerX = 0;
let lastPointerY = 0;
let pinchDistance = 0;
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
const cosPhi = Math.cos(orbit.phi);
camera.position.set(
orbitTarget.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbitTarget.y + orbit.radius * cosPhi,
orbitTarget.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbitTarget);
}
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) {
return 0;
}
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
lastPointerX = event.clientX;
lastPointerY = event.clientY;
} else if (pointers.size === 2) {
pinchDistance = pointerDistance();
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) {
return;
}
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - dy * 0.006,
0.34,
1.36
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
} else if (pointers.size === 2) {
const distance = pointerDistance();
if (pinchDistance > 0 && distance > 0) {
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * (pinchDistance / distance),
8.5,
24
);
}
pinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
lastPointerX = remaining.x;
lastPointerY = remaining.y;
}
if (pointers.size < 2) {
pinchDistance = 0;
}
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
8.5,
24
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
orbit.theta = 0.68;
orbit.phi = 0.88;
orbit.radius = 14.5;
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// ---------------------------------------------------------------------
// Animation loop
// ---------------------------------------------------------------------
function smoothstep(t) {
return t * t * (3 - 2 * t);
}
function animate(now) {
requestAnimationFrame(animate);
if (transition) {
const t = Math.min(
1,
(now - transition.start) / transition.duration
);
block.position.y = THREE.MathUtils.lerp(
transition.from,
transition.to,
smoothstep(t)
);
if (t >= 1) {
block.position.y = transition.to;
verticalState = transition.nextState;
transition = null;
updateStatus();
}
}
updateCamera();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```