Prompt specification
Build or customize the Three.js 3D scene "Squares on a right triangle" 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: Squares on a right triangle
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/squares-on-a-right-triangle-x1725
Scene Description Prompt:
Squares on a right triangle. Build a right triangle with a square attached to each side, subdivided into area units where practical; mark the right angle clearly. Please build this with Three.js from cdnjs in one self-contained HTML file, using low-poly forms.
Working Three.js Starter (squares-on-a-right-triangle-x1725):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Squares on a Right Triangle</title>
<style>
:root {
color-scheme: light;
--ink: #18343d;
--panel: rgba(248, 251, 249, 0.86);
--border: rgba(24, 52, 61, 0.16);
--coral: #ef6f5b;
--gold: #edc158;
--teal: #3ca89c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce8ea;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.ui {
position: fixed;
z-index: 2;
pointer-events: none;
}
.title-card {
top: 22px;
left: 22px;
max-width: 390px;
padding: 18px 20px 16px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 14px 40px rgba(37, 67, 73, 0.12);
backdrop-filter: blur(12px);
}
.eyebrow {
margin-bottom: 7px;
color: #527078;
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.45rem, 3vw, 2.2rem);
line-height: 1.02;
letter-spacing: -0.045em;
}
.subtitle {
margin: 10px 0 0;
color: #526c73;
font-size: 0.92rem;
line-height: 1.45;
}
.legend {
right: 22px;
bottom: 22px;
width: min(300px, calc(100vw - 44px));
padding: 17px 18px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 14px 40px rgba(37, 67, 73, 0.12);
backdrop-filter: blur(12px);
}
.legend-title {
margin-bottom: 11px;
font-size: 0.75rem;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.key-row {
display: grid;
grid-template-columns: 13px 1fr auto;
gap: 10px;
align-items: center;
margin: 9px 0;
font-size: 0.91rem;
}
.swatch {
width: 13px;
height: 13px;
border-radius: 4px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
}
.key-row strong {
font-variant-numeric: tabular-nums;
}
.equation {
margin-top: 13px;
padding-top: 13px;
border-top: 1px solid var(--border);
text-align: center;
font-size: 1.15rem;
font-weight: 850;
letter-spacing: -0.02em;
}
.unit-note {
margin-top: 7px;
color: #60787e;
font-size: 0.75rem;
line-height: 1.4;
text-align: center;
}
.controls {
left: 50%;
bottom: 22px;
display: flex;
align-items: center;
gap: 10px;
transform: translateX(-50%);
padding: 9px 10px 9px 14px;
border: 1px solid var(--border);
border-radius: 999px;
background: var(--panel);
box-shadow: 0 10px 30px rgba(37, 67, 73, 0.12);
color: #526c73;
font-size: 0.78rem;
white-space: nowrap;
backdrop-filter: blur(12px);
}
#reset {
pointer-events: auto;
border: 0;
border-radius: 999px;
padding: 7px 11px;
background: var(--ink);
color: white;
font: inherit;
font-weight: 750;
cursor: pointer;
}
#reset:hover {
background: #2a515b;
}
#reset:focus-visible {
outline: 3px solid rgba(60, 168, 156, 0.45);
outline-offset: 2px;
}
@media (max-width: 720px) {
.title-card {
top: 12px;
left: 12px;
max-width: calc(100vw - 24px);
padding: 13px 15px;
}
.subtitle {
display: none;
}
.legend {
right: 12px;
bottom: 68px;
width: 220px;
padding: 13px 14px;
}
.controls {
bottom: 12px;
max-width: calc(100vw - 24px);
}
.desktop-hint {
display: none;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive three-dimensional right triangle with a unit-square grid on each attached square"></main>
<section class="ui title-card">
<div class="eyebrow">Pythagorean construction</div>
<h1>Squares on a<br>Right Triangle</h1>
<p class="subtitle">
A 3–4–5 right triangle with a square attached to every side.
The squares are divided into one-unit cells, while the triangle
is divided into six equal-area regions.
</p>
</section>
<aside class="ui legend" aria-label="Area key">
<div class="legend-title">Area key</div>
<div class="key-row">
<span class="swatch" style="background:#e9dfc4"></span>
<span>Right triangle</span>
<strong>6 unit²</strong>
</div>
<div class="key-row">
<span class="swatch" style="background:var(--coral)"></span>
<span>Square on side 3</span>
<strong>9 unit²</strong>
</div>
<div class="key-row">
<span class="swatch" style="background:var(--gold)"></span>
<span>Square on side 4</span>
<strong>16 unit²</strong>
</div>
<div class="key-row">
<span class="swatch" style="background:var(--teal)"></span>
<span>Square on side 5</span>
<strong>25 unit²</strong>
</div>
<div class="equation">9 + 16 = 25</div>
<div class="unit-note">Each outlined square cell has area 1 unit².</div>
</aside>
<div class="ui controls">
<span class="desktop-hint">Drag to orbit · Scroll to zoom · R to reset</span>
<button id="reset" type="button">Reset view</button>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene, and camera
// -------------------------------------------------------------------------
const container = document.getElementById("scene");
const resetButton = document.getElementById("reset");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(container.clientWidth, container.clientHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Drag to orbit the geometric construction. Scroll or pinch to zoom."
);
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce8ea);
scene.fog = new THREE.Fog(0xdce8ea, 25, 46);
const camera = new THREE.PerspectiveCamera(
42,
container.clientWidth / container.clientHeight,
0.1,
100
);
const target = new THREE.Vector3(1.8, 0.15, 1.5);
const initialView = {
theta: 0.78,
phi: 0.88,
radius: 19.5
};
const desired = { ...initialView };
const current = { ...initialView };
function updateCamera() {
const sinPhi = Math.sin(current.phi);
camera.position.set(
target.x + current.radius * sinPhi * Math.cos(current.theta),
target.y + current.radius * Math.cos(current.phi),
target.z + current.radius * sinPhi * Math.sin(current.theta)
);
camera.lookAt(target);
}
// -------------------------------------------------------------------------
// Lighting and ground
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xf2fbff, 0x776955, 2.15));
const keyLight = new THREE.DirectionalLight(0xfff1d2, 3.2);
keyLight.position.set(-4, 13, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.00025;
keyLight.target.position.set(1.5, 0, 1.5);
scene.add(keyLight, keyLight.target);
const rimLight = new THREE.DirectionalLight(0x9acbff, 1.15);
rimLight.position.set(8, 6, -8);
scene.add(rimLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0xdde5e3,
roughness: 0.96,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.12;
ground.receiveShadow = true;
scene.add(ground);
const groundGrid = new THREE.GridHelper(
50,
50,
0x82999f,
0xaebfc1
);
groundGrid.position.y = -0.115;
groundGrid.material.transparent = true;
groundGrid.material.opacity = 0.22;
groundGrid.material.depthWrite = false;
scene.add(groundGrid);
// -------------------------------------------------------------------------
// Shared materials
// -------------------------------------------------------------------------
const borderMaterial = new THREE.MeshStandardMaterial({
color: 0x24434a,
roughness: 0.84,
metalness: 0.02,
flatShading: true
});
const triangleSideMaterial = new THREE.MeshStandardMaterial({
color: 0xb7a57d,
roughness: 0.9,
flatShading: true
});
const sectorMaterials = [
new THREE.MeshStandardMaterial({
color: 0xf7f0dc,
roughness: 0.88,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0xeadfbe,
roughness: 0.88,
side: THREE.DoubleSide
})
];
// -------------------------------------------------------------------------
// Canvas-backed labels
// -------------------------------------------------------------------------
function roundedRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
}
function makeBadge(title, subtitle, accent) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 180;
const ctx = canvas.getContext("2d");
roundedRect(ctx, 8, 8, 496, 164, 28);
ctx.fillStyle = "rgba(20, 46, 54, 0.93)";
ctx.fill();
ctx.lineWidth = 7;
ctx.strokeStyle = accent;
ctx.stroke();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#ffffff";
ctx.font = "800 54px system-ui, sans-serif";
ctx.fillText(title, 256, subtitle ? 66 : 90);
if (subtitle) {
ctx.fillStyle = accent;
ctx.font = "700 32px system-ui, sans-serif";
ctx.fillText(subtitle, 256, 126);
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
fog: false
});
const sprite = new THREE.Sprite(material);
sprite.renderOrder = 20;
return sprite;
}
function makeAngleBadge() {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(128, 64, 58, 0, Math.PI * 2);
ctx.fillStyle = "rgba(20, 46, 54, 0.94)";
ctx.fill();
ctx.lineWidth = 6;
ctx.strokeStyle = "#ffffff";
ctx.stroke();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#ffffff";
ctx.font = "800 48px system-ui, sans-serif";
ctx.fillText("90°", 128, 66);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
fog: false
})
);
sprite.scale.set(0.9, 0.45, 1);
sprite.renderOrder = 21;
return sprite;
}
// -------------------------------------------------------------------------
// Right triangle: vertices are measured in the XZ ground plane.
// A = right-angle vertex, B and C are the other two vertices.
// -------------------------------------------------------------------------
const A = new THREE.Vector2(0, 0);
const B = new THREE.Vector2(4, 0);
const C = new THREE.Vector2(0, 3);
const triangleShape = new THREE.Shape();
triangleShape.moveTo(A.x, A.y);
triangleShape.lineTo(B.x, B.y);
triangleShape.lineTo(C.x, C.y);
triangleShape.closePath();
const triangleGeometry = new THREE.ExtrudeGeometry(triangleShape, {
depth: 0.18,
bevelEnabled: true,
bevelSegments: 1,
bevelSize: 0.035,
bevelThickness: 0.025
});
// Shape Y becomes world Z; extrusion depth becomes downward world Y.
triangleGeometry.rotateX(Math.PI / 2);
const triangleBody = new THREE.Mesh(
triangleGeometry,
triangleSideMaterial
);
triangleBody.position.y = 0.18;
triangleBody.castShadow = true;
triangleBody.receiveShadow = true;
scene.add(triangleBody);
const triangleEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(triangleGeometry, 25),
new THREE.LineBasicMaterial({
color: 0x49646a,
transparent: true,
opacity: 0.8
})
);
triangleBody.add(triangleEdges);
// Divide the hypotenuse into six equal parts. Connecting those points
// to A creates six triangles of equal area: 6 total / 6 = 1 unit² each.
const fanPoints = [];
for (let i = 0; i <= 6; i++) {
fanPoints.push(B.clone().lerp(C, i / 6));
}
for (let i = 0; i < 6; i++) {
const p1 = fanPoints[i];
const p2 = fanPoints[i + 1];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[
A.x, 0.188, A.y,
p1.x, 0.188, p1.y,
p2.x, 0.188, p2.y
],
3
)
);
geometry.setIndex([0, 2, 1]);
geometry.computeVertexNormals();
const sector = new THREE.Mesh(geometry, sectorMaterials[i % 2]);
sector.receiveShadow = true;
scene.add(sector);
}
// Subtle radial subdivision lines.
const fanPositions = [];
for (let i = 1; i < 6; i++) {
const p = fanPoints[i];
fanPositions.push(A.x, 0.205, A.y, p.x, 0.205, p.y);
}
const fanGeometry = new THREE.BufferGeometry();
fanGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(fanPositions, 3)
);
scene.add(
new THREE.LineSegments(
fanGeometry,
new THREE.LineBasicMaterial({
color: 0x60777a,
transparent: true,
opacity: 0.42
})
)
);
// Clear right-angle marker: square corner plus quarter-circle arc.
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0x24434a,
roughness: 0.72,
flatShading: true
});
const markerHorizontal = new THREE.Mesh(
new THREE.BoxGeometry(0.55, 0.045, 0.055),
markerMaterial
);
markerHorizontal.position.set(0.275, 0.225, 0.55);
const markerVertical = new THREE.Mesh(
new THREE.BoxGeometry(0.055, 0.045, 0.55),
markerMaterial
);
markerVertical.position.set(0.55, 0.225, 0.275);
scene.add(markerHorizontal, markerVertical);
const arcPoints = [];
for (let i = 0; i <= 16; i++) {
const angle = (i / 16) * Math.PI / 2;
arcPoints.push(
new THREE.Vector3(
0.55 + 0.22 * Math.cos(angle),
0.235,
0.55 + 0.22 * Math.sin(angle)
)
);
}
const arc = new THREE.Mesh(
new THREE.TubeGeometry(
new THREE.CatmullRomCurve3(arcPoints),
18,
0.016,
4,
false
),
markerMaterial
);
scene.add(arc);
const angleBadge = makeAngleBadge();
angleBadge.position.set(0.92, 0.55, 0.88);
scene.add(angleBadge);
const triangleBadge = makeBadge(
"RIGHT TRIANGLE",
"6 unit² · six area units",
"#e6d8b6"
);
triangleBadge.scale.set(3.45, 1.21, 1);
triangleBadge.position.set(1.35, 0.82, 1.25);
scene.add(triangleBadge);
// -------------------------------------------------------------------------
// Unit-grid squares
//
// A local group uses local +X along an edge and local -Z outward.
// Every visible tile is a 1 × 1 unit cell.
// -------------------------------------------------------------------------
function createUnitSquare(start, end, baseColor, accent, title, subtitle) {
const dx = end.x - start.x;
const dz = end.y - start.y;
const length = Math.hypot(dx, dz);
const count = Math.round(length);
const group = new THREE.Group();
group.position.set(start.x, 0, start.y);
// With this rotation, local +X follows the edge and local -Z is outward.
group.rotation.y = Math.atan2(-dz, dx);
const base = new THREE.Mesh(
new THREE.BoxGeometry(length, 0.16, length),
borderMaterial
);
base.position.set(length / 2, 0.02, -length / 2);
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
const gap = 0.035;
const color = new THREE.Color(baseColor);
for (let row = 0; row < count; row++) {
for (let column = 0; column < count; column++) {
const variation = ((row * 3 + column * 5) % 7 - 3) * 0.012;
const tileColor = color.clone().offsetHSL(
((row + column) % 2) * 0.006,
0,
variation
);
const material = new THREE.MeshStandardMaterial({
color: tileColor,
roughness: 0.78,
metalness: 0.015,
flatShading: true
});
const tile = new THREE.Mesh(
new THREE.BoxGeometry(
1 - gap,
0.13,
1 - gap
),
material
);
tile.position.set(
column + 0.5,
0.165,
-(row + 0.5)
);
tile.castShadow = true;
tile.receiveShadow = true;
group.add(tile);
}
}
// Raised perimeter strips make the square boundaries and grid legible.
function borderStrip(width, depth, x, z) {
const strip = new THREE.Mesh(
new THREE.BoxGeometry(width, 0.052, depth),
borderMaterial
);
strip.position.set(x, 0.244, z);
strip.castShadow = true;
group.add(strip);
}
borderStrip(length + 0.055, 0.055, length / 2, 0);
borderStrip(length + 0.055, 0.055, length / 2, -length);
borderStrip(0.055, length, 0, -length / 2);
borderStrip(0.055, length, length, -length / 2);
scene.add(group);
group.updateMatrixWorld(true);
const badge = makeBadge(title, subtitle, accent);
badge.scale.set(3.05, 1.07, 1);
badge.position.copy(
group.localToWorld(
new THREE.Vector3(length / 2, 0.88, -length / 2)
)
);
scene.add(badge);
}
// Counterclockwise edge order ensures every square points outward.
createUnitSquare(
A, B,
0xe1aa3f,
"#ffe29a",
"4 × 4",
"16 unit²"
);
createUnitSquare(
B, C,
0x23867d,
"#80e1d5",
"5 × 5",
"25 unit²"
);
createUnitSquare(
C, A,
0xdb5b49,
"#ffad9d",
"3 × 3",
"9 unit²"
);
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, touch pinch, keyboard reset.
// No external controls or Three.js addons are used.
// -------------------------------------------------------------------------
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function resetView() {
desired.theta = initialView.theta;
desired.phi = initialView.phi;
desired.radius = initialView.radius;
}
const pointers = new Map();
let lastPinchDistance = 0;
function pinchDistance() {
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) => {
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
renderer.domElement.setPointerCapture(event.pointerId);
if (pointers.size === 2) {
lastPinchDistance = pinchDistance();
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) {
return;
}
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
previous.x = event.clientX;
previous.y = event.clientY;
if (pointers.size === 1) {
desired.theta -= dx * 0.006;
desired.phi = clamp(
desired.phi - dy * 0.006,
0.28,
1.42
);
} else if (pointers.size === 2) {
const distance = pinchDistance();
if (distance > 0 && lastPinchDistance > 0) {
desired.radius = clamp(
desired.radius * (lastPinchDistance / distance),
10,
30
);
}
lastPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
lastPinchDistance = pointers.size === 2 ? pinchDistance() : 0;
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desired.radius = clamp(
desired.radius * Math.exp(event.deltaY * 0.001),
10,
30
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
resetView();
}
});
resetButton.addEventListener("click", resetView);
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
function resize() {
const width = container.clientWidth;
const height = container.clientHeight;
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
function animate() {
requestAnimationFrame(animate);
current.theta += (desired.theta - current.theta) * 0.12;
current.phi += (desired.phi - current.phi) * 0.12;
current.radius += (desired.radius - current.radius) * 0.12;
updateCamera();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```