Prompt specification
Build or customize the Three.js 3D scene "Pizza sector fraction board" 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: Pizza sector fraction board
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/pizza-sector-fraction-board-x0133
Scene Description Prompt:
Please make a stylized Three.js scene in one standalone HTML file using cdnjs. Build a circular pizza divided into eight equal removable sectors, with raised crust and simple toppings; display one quarter as exactly two aligned sectors.
Working Three.js Starter (pizza-sector-fraction-board-x0133):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Stylized Eight-Sector Pizza</title>
<style>
:root {
color-scheme: light;
--ink: #39251d;
--muted: #775b4f;
--cream: #fff8eb;
--accent: #167c70;
--accent-bright: #36cbb5;
--orange: #ef743f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
font-family: ui-rounded, "Avenir Next", "Segoe UI", sans-serif;
color: var(--ink);
}
body {
background:
radial-gradient(circle at 50% 35%, #fff5dc 0%, #f3c99d 48%, #d98d73 100%);
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.intro {
position: fixed;
top: 28px;
left: 30px;
z-index: 10;
width: min(330px, calc(100vw - 60px));
pointer-events: none;
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 12px;
padding: 7px 11px;
border: 1px solid rgba(57, 37, 29, 0.16);
border-radius: 999px;
background: rgba(255, 248, 235, 0.72);
backdrop-filter: blur(10px);
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--accent-bright);
box-shadow: 0 0 0 4px rgba(54, 203, 181, 0.17);
}
h1 {
margin: 0;
font-size: clamp(38px, 5vw, 66px);
font-weight: 850;
line-height: 0.92;
letter-spacing: -0.065em;
}
h1 span {
color: var(--orange);
}
.intro p {
max-width: 290px;
margin: 16px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.panel {
position: fixed;
top: 28px;
right: 28px;
z-index: 10;
width: 252px;
padding: 18px;
border: 1px solid rgba(255, 255, 255, 0.64);
border-radius: 22px;
background: rgba(255, 248, 235, 0.86);
box-shadow:
0 18px 50px rgba(80, 42, 28, 0.14),
inset 0 1px 0 rgba(255, 255, 255, 0.8);
backdrop-filter: blur(14px);
}
.panel-title {
margin: 0 0 13px;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.count {
display: flex;
align-items: baseline;
gap: 7px;
margin-bottom: 14px;
}
.count strong {
font-size: 43px;
line-height: 1;
letter-spacing: -0.05em;
}
.count span {
color: var(--muted);
font-size: 13px;
}
.equation {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-bottom: 14px;
padding: 11px 12px;
border-radius: 13px;
background: rgba(22, 124, 112, 0.09);
color: var(--accent);
font-size: 13px;
font-weight: 800;
}
.equation b {
font-size: 20px;
}
.buttons {
display: grid;
gap: 8px;
}
button {
width: 100%;
padding: 11px 12px;
border: 0;
border-radius: 12px;
font: inherit;
font-size: 12px;
font-weight: 800;
cursor: pointer;
transition:
transform 140ms ease,
opacity 140ms ease,
background 140ms ease;
}
button:hover:not(:disabled) {
transform: translateY(-1px);
}
button:active:not(:disabled) {
transform: translateY(1px);
}
button:disabled {
cursor: default;
opacity: 0.42;
}
#removeQuarter {
color: white;
background: var(--accent);
}
#reset {
color: var(--ink);
background: rgba(57, 37, 29, 0.08);
}
.quarter-badge {
position: fixed;
z-index: 8;
left: 50%;
top: 50%;
padding: 8px 12px;
border: 1px solid rgba(255, 255, 255, 0.48);
border-radius: 999px;
background: rgba(14, 69, 63, 0.92);
box-shadow: 0 9px 25px rgba(20, 70, 63, 0.25);
color: #eafff9;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.11em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -50%);
transition: opacity 180ms ease;
}
.quarter-badge::after {
content: "";
position: absolute;
left: 50%;
bottom: -5px;
width: 9px;
height: 9px;
background: rgba(14, 69, 63, 0.92);
transform: translateX(-50%) rotate(45deg);
}
.quarter-badge.muted {
opacity: 0.38;
}
.hint {
position: fixed;
left: 50%;
bottom: 24px;
z-index: 10;
padding: 9px 14px;
border: 1px solid rgba(255, 255, 255, 0.45);
border-radius: 999px;
background: rgba(255, 248, 235, 0.76);
color: var(--muted);
font-size: 11px;
font-weight: 700;
backdrop-filter: blur(10px);
transform: translateX(-50%);
pointer-events: none;
white-space: nowrap;
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: #f3c99d;
color: var(--ink);
font-size: 12px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.intro {
top: 18px;
left: 18px;
}
.intro p {
display: none;
}
h1 {
font-size: 40px;
}
.panel {
top: auto;
right: 14px;
bottom: 58px;
width: 210px;
padding: 14px;
}
.count strong {
font-size: 34px;
}
.hint {
bottom: 14px;
font-size: 9px;
}
.quarter-badge {
display: none;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<header class="intro">
<div class="eyebrow">Procedural Three.js</div>
<h1>Perfect<br /><span>Quarter.</span></h1>
<p>
Eight equal removable sectors. The highlighted 90° quarter is made from
exactly two aligned 45° slices.
</p>
</header>
<aside class="panel">
<p class="panel-title">Pizza assembly</p>
<div class="count">
<strong id="remaining">8</strong>
<span>of 8 sectors remaining</span>
</div>
<div class="equation">
<b>90°</b>
<span>=</span>
<span>2 × 45°</span>
</div>
<div class="buttons">
<button id="removeQuarter" type="button">Remove 2-slice quarter</button>
<button id="reset" type="button" disabled>Reset pizza</button>
</div>
</aside>
<div id="quarterBadge" class="quarter-badge">Highlighted quarter · 2 sectors</div>
<div class="hint">Drag to orbit · Scroll to zoom · Click a slice to remove it</div>
<div id="loading">Building the pizza…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const remainingLabel = document.getElementById("remaining");
const resetButton = document.getElementById("reset");
const removeQuarterButton = document.getElementById("removeQuarter");
const quarterBadge = document.getElementById("quarterBadge");
const loading = document.getElementById("loading");
const SECTOR_COUNT = 8;
const SECTOR_ANGLE = (Math.PI * 2) / SECTOR_COUNT;
const QUARTER_SLICE_INDICES = [3, 4];
const QUARTER_ANGLE = Math.PI / 2;
const QUARTER_START = QUARTER_SLICE_INDICES[0] * SECTOR_ANGLE;
const QUARTER_END = QUARTER_START + QUARTER_ANGLE;
const QUARTER_MIDDLE = (QUARTER_START + QUARTER_END) / 2;
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xe7b38b, 14, 30);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
stage.appendChild(renderer.domElement);
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional pizza divided into eight removable sectors"
);
const camera = new THREE.PerspectiveCamera(
34,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.48, 0);
let desiredTheta = 0.72;
let desiredElevation = 0.73;
let desiredRadius = 11.7;
let currentTheta = desiredTheta;
let currentElevation = desiredElevation;
let currentRadius = desiredRadius;
function updateCamera() {
const horizontalRadius = currentRadius * Math.cos(currentElevation);
camera.position.set(
cameraTarget.x + Math.sin(currentTheta) * horizontalRadius,
cameraTarget.y + Math.sin(currentElevation) * currentRadius,
cameraTarget.z + Math.cos(currentTheta) * horizontalRadius
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphereLight = new THREE.HemisphereLight(
0xfff4dc,
0x7d493b,
2.1
);
scene.add(hemisphereLight);
const keyLight = new THREE.DirectionalLight(0xffedcf, 3.4);
keyLight.position.set(-5.5, 9, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 24;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const fillLight = new THREE.PointLight(0xff9d68, 1.1, 18, 2);
fillLight.position.set(5, 4, -4);
scene.add(fillLight);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0xd98e70,
roughness: 1
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.015;
ground.receiveShadow = true;
scene.add(ground);
const pizzaAssembly = new THREE.Group();
scene.add(pizzaAssembly);
/*
The featured quarter contains exactly two sibling sector groups:
sector 4 covers 135°–180°, and sector 5 covers 180°–225°.
They remain adjacent and aligned while the assembly is moved.
*/
const quarterAssembly = new THREE.Group();
quarterAssembly.position.x = -0.38;
pizzaAssembly.add(quarterAssembly);
const plateMaterial = new THREE.MeshStandardMaterial({
color: 0xfff7e8,
roughness: 0.32,
metalness: 0.02
});
const plate = new THREE.Mesh(
new THREE.CylinderGeometry(3.62, 3.54, 0.22, 96),
plateMaterial
);
plate.position.y = 0.17;
plate.castShadow = true;
plate.receiveShadow = true;
pizzaAssembly.add(plate);
const plateRim = new THREE.Mesh(
new THREE.TorusGeometry(3.46, 0.075, 12, 96),
new THREE.MeshStandardMaterial({
color: 0xf2dfc7,
roughness: 0.38
})
);
plateRim.rotation.x = Math.PI / 2;
plateRim.position.y = 0.285;
plateRim.castShadow = true;
pizzaAssembly.add(plateRim);
const crustSpotMaterial = new THREE.MeshStandardMaterial({
color: 0x9e5623,
roughness: 0.9
});
const pepperoniGeometry = new THREE.CylinderGeometry(
0.18,
0.18,
0.055,
20
);
const pepperoniMaterial = new THREE.MeshStandardMaterial({
color: 0xc43e2f,
roughness: 0.72
});
const oliveGeometry = new THREE.TorusGeometry(0.105, 0.035, 8, 18);
const oliveMaterial = new THREE.MeshStandardMaterial({
color: 0x313526,
roughness: 0.72
});
const basilGeometry = new THREE.SphereGeometry(0.16, 14, 10);
const basilMaterial = new THREE.MeshStandardMaterial({
color: 0x4b8b43,
roughness: 0.86
});
const meltedGeometry = new THREE.SphereGeometry(0.18, 14, 10);
const meltedMaterial = new THREE.MeshStandardMaterial({
color: 0xffd66d,
roughness: 0.78
});
const records = [];
const interactiveMeshes = [];
function seededRandom(index, salt = 0) {
const value = Math.sin((index + 1) * 12.9898 + salt * 78.233) * 43758.5453;
return value - Math.floor(value);
}
/*
Creates a closed wedge prism directly in the XZ plane.
Every pizza sector uses the same exact 45° angular span.
*/
function createWedgePrism(
startAngle,
endAngle,
radius,
bottomY,
topY,
segments = 40
) {
const positions = [];
const indices = [];
const topCenter = positions.length / 3;
positions.push(0, topY, 0);
const topStart = positions.length / 3;
for (let i = 0; i <= segments; i++) {
const angle = THREE.MathUtils.lerp(startAngle, endAngle, i / segments);
positions.push(
Math.cos(angle) * radius,
topY,
Math.sin(angle) * radius
);
}
const bottomCenter = positions.length / 3;
positions.push(0, bottomY, 0);
const bottomStart = positions.length / 3;
for (let i = 0; i <= segments; i++) {
const angle = THREE.MathUtils.lerp(startAngle, endAngle, i / segments);
positions.push(
Math.cos(angle) * radius,
bottomY,
Math.sin(angle) * radius
);
}
const topAt = (i) => topStart + i;
const bottomAt = (i) => bottomStart + i;
for (let i = 0; i < segments; i++) {
// Top and bottom fans.
indices.push(topCenter, topAt(i + 1), topAt(i));
indices.push(bottomCenter, bottomAt(i), bottomAt(i + 1));
// Rounded outer wall.
indices.push(topAt(i), topAt(i + 1), bottomAt(i));
indices.push(topAt(i + 1), bottomAt(i + 1), bottomAt(i));
}
// The two radial cut faces.
indices.push(
topCenter,
topAt(0),
bottomCenter,
topAt(0),
bottomAt(0),
bottomCenter
);
indices.push(
topCenter,
bottomCenter,
topAt(segments),
topAt(segments),
bottomCenter,
bottomAt(segments)
);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function createArcTube(
radius,
startAngle,
endAngle,
tubeRadius,
y,
tubularSegments = 48,
radialSegments = 10
) {
const points = [];
for (let i = 0; i <= tubularSegments; i++) {
const angle = THREE.MathUtils.lerp(
startAngle,
endAngle,
i / tubularSegments
);
points.push(
new THREE.Vector3(
Math.cos(angle) * radius,
y,
Math.sin(angle) * radius
)
);
}
const curve = new THREE.CatmullRomCurve3(
points,
false,
"centripetal"
);
return new THREE.TubeGeometry(
curve,
tubularSegments,
tubeRadius,
radialSegments,
false
);
}
function polarPosition(angle, radius, y) {
return new THREE.Vector3(
Math.cos(angle) * radius,
y,
Math.sin(angle) * radius
);
}
function addTopping(group, mesh, angle, radius, y) {
mesh.position.copy(polarPosition(angle, radius, y));
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
}
function createSlice(index) {
const start = index * SECTOR_ANGLE;
const end = (index + 1) * SECTOR_ANGLE;
const middle = (start + end) / 2;
const highlighted = QUARTER_SLICE_INDICES.includes(index);
const group = new THREE.Group();
group.name = `Removable sector ${index + 1}`;
group.userData.sliceIndex = index;
const bodyMaterial = new THREE.MeshStandardMaterial({
color: highlighted ? 0xf1b85b : 0xe6a64e,
roughness: 0.78,
side: THREE.DoubleSide
});
const sauceMaterial = new THREE.MeshStandardMaterial({
color: 0xb93829,
roughness: 0.8,
side: THREE.DoubleSide,
emissive: 0x6b120b,
emissiveIntensity: 0
});
const cheeseMaterial = new THREE.MeshStandardMaterial({
color: highlighted ? 0xffd85f : 0xf1c15a,
roughness: 0.72,
side: THREE.DoubleSide,
emissive: 0x8a5b12,
emissiveIntensity: highlighted ? 0.07 : 0
});
const body = new THREE.Mesh(
createWedgePrism(start, end, 3.05, 0.3, 0.54),
bodyMaterial
);
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const sauce = new THREE.Mesh(
createWedgePrism(
start + 0.006,
end - 0.006,
2.93,
0.535,
0.575,
36
),
sauceMaterial
);
sauce.castShadow = true;
sauce.receiveShadow = true;
group.add(sauce);
const cheese = new THREE.Mesh(
createWedgePrism(
start + 0.025,
end - 0.025,
2.77,
0.57,
0.605,
36
),
cheeseMaterial
);
cheese.castShadow = true;
cheese.receiveShadow = true;
group.add(cheese);
const crustMaterial = new THREE.MeshStandardMaterial({
color: highlighted ? 0xf2b65b : 0xe6a44b,
roughness: 0.86
});
const crust = new THREE.Mesh(
createArcTube(
2.96,
start + 0.005,
end - 0.005,
0.18,
0.6,
34,
10
),
crustMaterial
);
crust.castShadow = true;
crust.receiveShadow = true;
group.add(crust);
// Three pepperoni pieces per sector.
const pepperoniLayout = [
{ radius: 0.95, offset: -0.13 },
{ radius: 1.72, offset: 0.12 },
{ radius: 2.32, offset: -0.07 }
];
pepperoniLayout.forEach((item, toppingIndex) => {
const jitter = (seededRandom(index, toppingIndex + 2) - 0.5) * 0.07;
const angle = middle + item.offset + jitter;
const pepperoni = new THREE.Mesh(
pepperoniGeometry,
pepperoniMaterial
);
addTopping(group, pepperoni, angle, item.radius, 0.636);
pepperoni.rotation.y = seededRandom(index, toppingIndex + 9) * Math.PI;
});
// Melted cheese details.
for (let detail = 0; detail < 2; detail++) {
const blob = new THREE.Mesh(meltedGeometry, meltedMaterial);
blob.scale.set(
1.25 + detail * 0.2,
0.2,
0.78 + detail * 0.12
);
blob.rotation.y = seededRandom(index, detail + 20) * Math.PI;
addTopping(
group,
blob,
middle + (detail === 0 ? -0.1 : 0.12),
detail === 0 ? 1.15 : 2.42,
0.62
);
}
// A basil leaf and an olive.
const basil = new THREE.Mesh(basilGeometry, basilMaterial);
basil.scale.set(1.25, 0.22, 0.72);
basil.rotation.y = seededRandom(index, 31) * Math.PI;
addTopping(group, basil, middle - 0.06, 1.42, 0.637);
const olive = new THREE.Mesh(oliveGeometry, oliveMaterial);
olive.rotation.x = Math.PI / 2;
addTopping(
group,
olive,
middle + 0.13,
2.05,
0.646
);
// Small toasted spots on the raised crust.
for (let spot = 0; spot < 3; spot++) {
const crustSpot = new THREE.Mesh(
new THREE.SphereGeometry(0.075, 10, 8),
crustSpotMaterial
);
crustSpot.scale.set(1.25, 0.17, 0.7);
crustSpot.rotation.y = seededRandom(index, spot + 40) * Math.PI;
addTopping(
group,
crustSpot,
THREE.MathUtils.lerp(start + 0.08, end - 0.08, (spot + 1) / 4),
2.99,
0.773
);
}
const parent = highlighted ? quarterAssembly : pizzaAssembly;
parent.add(group);
const record = {
index,
group,
middle,
highlighted,
sauceMaterial,
cheeseMaterial,
desiredRemoved: false,
animatingOut: false,
animatingIn: false,
progress: 0,
removeVector: new THREE.Vector3(
Math.cos(middle),
0,
Math.sin(middle)
)
};
records.push(record);
group.traverse((object) => {
if (object.isMesh) {
object.userData.sliceIndex = index;
interactiveMeshes.push(object);
}
});
}
for (let i = 0; i < SECTOR_COUNT; i++) {
createSlice(i);
}
// Teal outline identifying the highlighted 90° quarter.
const guideMaterial = new THREE.MeshBasicMaterial({
color: 0x36cbb5,
transparent: true,
opacity: 0.9
});
const outerGuide = new THREE.Mesh(
createArcTube(
3.22,
QUARTER_START,
QUARTER_END,
0.025,
0.315,
64,
8
),
guideMaterial
);
quarterAssembly.add(outerGuide);
for (const angle of [QUARTER_START, QUARTER_END]) {
const inner = polarPosition(angle, 0.2, 0.315);
const outer = polarPosition(angle, 3.22, 0.315);
const line = new THREE.CatmullRomCurve3([inner, outer]);
const radialGuide = new THREE.Mesh(
new THREE.TubeGeometry(line, 1, 0.018, 8, false),
guideMaterial
);
quarterAssembly.add(radialGuide);
}
// A few stylized crumbs on the table.
const crumbGeometry = new THREE.DodecahedronGeometry(0.055, 0);
const crumbMaterial = new THREE.MeshStandardMaterial({
color: 0xc47b37,
roughness: 1
});
for (let i = 0; i < 14; i++) {
const crumb = new THREE.Mesh(crumbGeometry, crumbMaterial);
const angle = seededRandom(i, 60) * Math.PI * 2;
const radius = 3.85 + seededRandom(i, 61) * 1.25;
crumb.position.set(
Math.cos(angle) * radius,
0.045,
Math.sin(angle) * radius
);
crumb.scale.setScalar(0.65 + seededRandom(i, 62) * 0.75);
crumb.rotation.set(
seededRandom(i, 63),
seededRandom(i, 64),
seededRandom(i, 65)
);
crumb.castShadow = true;
pizzaAssembly.add(crumb);
}
function updateStatus() {
const remaining = records.filter((record) => !record.desiredRemoved).length;
const quarterIntact =
!records[3].desiredRemoved && !records[4].desiredRemoved;
remainingLabel.textContent = String(remaining);
resetButton.disabled = remaining === SECTOR_COUNT;
removeQuarterButton.disabled = !quarterIntact;
quarterBadge.classList.toggle("muted", !quarterIntact);
guideMaterial.opacity = quarterIntact ? 0.9 : 0.16;
}
function removeSlice(index, sharedDirection = null) {
const record = records[index];
if (!record || record.desiredRemoved) return;
record.desiredRemoved = true;
record.animatingOut = true;
record.animatingIn = false;
record.progress = 0;
record.group.visible = true;
if (sharedDirection) {
record.removeVector.copy(sharedDirection).normalize();
} else {
record.removeVector.set(
Math.cos(record.middle),
0,
Math.sin(record.middle)
);
}
updateStatus();
}
function resetPizza() {
records.forEach((record) => {
record.desiredRemoved = false;
record.animatingOut = false;
record.animatingIn = true;
record.progress = 0;
record.group.visible = true;
});
updateStatus();
}
removeQuarterButton.addEventListener("click", () => {
// Both sectors use the same direction, preserving their alignment.
const sharedDirection = new THREE.Vector3(
Math.cos(QUARTER_MIDDLE),
0,
Math.sin(QUARTER_MIDDLE)
);
removeSlice(QUARTER_SLICE_INDICES[0], sharedDirection);
removeSlice(QUARTER_SLICE_INDICES[1], sharedDirection);
});
resetButton.addEventListener("click", resetPizza);
// Raycasting and direct camera controls.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = false;
let dragged = false;
let pointerStartX = 0;
let pointerStartY = 0;
let previousX = 0;
let previousY = 0;
let hoveredIndex = null;
function setHovered(index) {
if (hoveredIndex === index) return;
if (hoveredIndex !== null) {
const oldRecord = records[hoveredIndex];
oldRecord.sauceMaterial.emissiveIntensity = 0;
oldRecord.cheeseMaterial.emissiveIntensity =
oldRecord.highlighted ? 0.07 : 0;
}
hoveredIndex = index;
if (hoveredIndex !== null) {
const record = records[hoveredIndex];
record.sauceMaterial.emissiveIntensity = 0.12;
record.cheeseMaterial.emissiveIntensity = 0.19;
renderer.domElement.style.cursor = "pointer";
} else {
renderer.domElement.style.cursor = "grab";
}
}
function pickSector(clientX, clientY) {
const bounds = renderer.domElement.getBoundingClientRect();
pointer.x = ((clientX - bounds.left) / bounds.width) * 2 - 1;
pointer.y = -((clientY - bounds.top) / bounds.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(interactiveMeshes, false);
for (const intersection of intersections) {
const index = intersection.object.userData.sliceIndex;
if (
index !== undefined &&
!records[index].desiredRemoved &&
!records[index].animatingOut
) {
return index;
}
}
return null;
}
renderer.domElement.style.cursor = "grab";
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointerDown = true;
dragged = false;
pointerStartX = previousX = event.clientX;
pointerStartY = previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (pointerDown) {
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
desiredTheta -= dx * 0.006;
desiredElevation = THREE.MathUtils.clamp(
desiredElevation + dy * 0.005,
0.36,
1.18
);
previousX = event.clientX;
previousY = event.clientY;
const totalDistance = Math.hypot(
event.clientX - pointerStartX,
event.clientY - pointerStartY
);
if (totalDistance > 5) dragged = true;
} else {
setHovered(pickSector(event.clientX, event.clientY));
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!pointerDown) return;
if (!dragged) {
const selectedIndex = pickSector(event.clientX, event.clientY);
if (selectedIndex !== null) removeSlice(selectedIndex);
}
pointerDown = false;
setHovered(pickSector(event.clientX, event.clientY));
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = false;
dragged = false;
setHovered(null);
});
renderer.domElement.addEventListener("pointerleave", () => {
if (!pointerDown) setHovered(null);
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredRadius = THREE.MathUtils.clamp(
desiredRadius * Math.exp(event.deltaY * 0.001),
7.6,
17
);
},
{ passive: false }
);
function updateSliceAnimations(deltaTime) {
records.forEach((record) => {
if (record.animatingOut) {
record.progress = Math.min(record.progress + deltaTime / 0.68, 1);
const eased = 1 - Math.pow(1 - record.progress, 3);
record.group.position
.copy(record.removeVector)
.multiplyScalar(1.35 * eased);
record.group.position.y = Math.sin(eased * Math.PI) * 0.16;
record.group.scale.setScalar(1 - eased * 0.12);
if (record.progress >= 1) {
record.animatingOut = false;
record.group.visible = false;
}
}
if (record.animatingIn) {
record.progress = Math.min(record.progress + deltaTime / 0.68, 1);
const eased = 1 - Math.pow(1 - record.progress, 3);
record.group.position
.copy(record.removeVector)
.multiplyScalar(1.35 * (1 - eased));
record.group.position.y =
Math.sin((1 - eased) * Math.PI) * 0.16;
record.group.scale.setScalar(0.88 + eased * 0.12);
if (record.progress >= 1) {
record.animatingIn = false;
record.group.position.set(0, 0, 0);
record.group.scale.setScalar(1);
}
}
});
}
const badgeWorldPosition = new THREE.Vector3();
const badgeProjected = new THREE.Vector3();
function updateBadgePosition() {
quarterAssembly.updateWorldMatrix(true, false);
badgeWorldPosition.set(-1.65, 1.02, 0);
quarterAssembly.localToWorld(badgeWorldPosition);
badgeProjected.copy(badgeWorldPosition).project(camera);
const visible =
badgeProjected.z > -1 &&
badgeProjected.z < 1 &&
Math.abs(badgeProjected.x) < 1.2 &&
Math.abs(badgeProjected.y) < 1.2;
quarterBadge.style.opacity = visible ? "" : "0";
if (visible) {
const x = (badgeProjected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-badgeProjected.y * 0.5 + 0.5) * window.innerHeight;
quarterBadge.style.left = `${x}px`;
quarterBadge.style.top = `${y}px`;
}
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const deltaTime = Math.min(clock.getDelta(), 0.05);
const damping = 1 - Math.exp(-deltaTime * 9);
currentTheta += (desiredTheta - currentTheta) * damping;
currentElevation += (desiredElevation - currentElevation) * damping;
currentRadius += (desiredRadius - currentRadius) * damping;
updateCamera();
updateSliceAnimations(deltaTime);
updateBadgePosition();
renderer.render(scene, camera);
});
updateStatus();
loading.classList.add("hidden");
</script>
</body>
</html>
```