Prompt specification
Build or customize the Three.js 3D scene "Rent Block and Material Blocks" 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: Rent Block and Material Blocks
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/rent-block-and-material-blocks-x3137
Scene Description Prompt:
Stylized rent and material blocks distinguish fixed cost from cost per cup produced. Build one HTML file using Three.js from cdnjs.
Working Three.js Starter (rent-block-and-material-blocks-x3137):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#f2eee5" />
<title>Cup Economics — Rent vs. Materials</title>
<style>
:root {
--page: #f2eee5;
--paper: #fffaf0;
--paper-2: #f7f1e5;
--ink: #193331;
--muted: #6f7d76;
--line: #ded8ca;
--rent: #ee9f50;
--rent-soft: #f8d6ac;
--material: #3e9b98;
--material-soft: #cce8e4;
--good: #3d8c69;
--shadow: 0 18px 45px rgba(38, 53, 45, 0.09);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
min-height: 100%;
margin: 0;
}
body {
background: var(--page);
color: var(--ink);
font-family:
Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI",
sans-serif;
font-variant-numeric: tabular-nums;
overflow: hidden;
}
button,
input {
font: inherit;
}
.app {
display: grid;
grid-template-columns: 370px minmax(0, 1fr);
width: 100%;
height: 100vh;
}
.panel {
position: relative;
z-index: 5;
height: 100vh;
padding: 28px 28px 24px;
overflow-y: auto;
background:
linear-gradient(180deg, rgba(255, 255, 255, 0.46), transparent 22%),
var(--paper);
border-right: 1px solid rgba(25, 51, 49, 0.1);
scrollbar-width: thin;
scrollbar-color: var(--line) transparent;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 28px;
color: var(--muted);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow-mark {
display: inline-grid;
width: 21px;
height: 21px;
place-items: center;
border-radius: 6px;
background: var(--ink);
box-shadow: inset -5px -5px 0 rgba(255, 255, 255, 0.12);
}
.eyebrow-mark::after {
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--rent);
content: "";
}
h1 {
margin: 0;
font-size: clamp(36px, 3.1vw, 48px);
font-weight: 760;
line-height: 0.99;
letter-spacing: -0.055em;
}
h1 em {
color: var(--material);
font-family: Georgia, "Times New Roman", serif;
font-weight: 500;
}
.intro {
max-width: 290px;
margin: 18px 0 27px;
color: var(--muted);
font-size: 14px;
line-height: 1.6;
}
.section-title {
display: flex;
align-items: center;
gap: 9px;
margin: 0 0 17px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.section-title::before {
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--rent);
content: "";
}
.controls {
display: grid;
gap: 19px;
margin-bottom: 25px;
}
.control-head {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 14px;
margin-bottom: 10px;
}
.control-head label {
color: #4f625c;
font-size: 13px;
font-weight: 650;
}
.control-value {
color: var(--ink);
font-size: 17px;
font-weight: 800;
letter-spacing: -0.025em;
}
input[type="range"] {
--fill: 50%;
--accent: var(--material);
width: 100%;
height: 5px;
margin: 0;
border: 0;
border-radius: 999px;
outline: none;
appearance: none;
background: linear-gradient(
to right,
var(--accent) 0,
var(--accent) var(--fill),
#dedfd7 var(--fill),
#dedfd7 100%
);
cursor: pointer;
}
input[type="range"].rent-range {
--accent: var(--rent);
}
input[type="range"]::-webkit-slider-thumb {
width: 19px;
height: 19px;
border: 4px solid var(--paper);
border-radius: 50%;
appearance: none;
background: var(--accent);
box-shadow: 0 2px 8px rgba(25, 51, 49, 0.25);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 4px solid var(--paper);
border-radius: 50%;
background: var(--accent);
box-shadow: 0 2px 8px rgba(25, 51, 49, 0.25);
}
input[type="range"]:focus-visible {
outline: 3px solid rgba(62, 155, 152, 0.25);
outline-offset: 7px;
}
.range-ends {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: #9aa29c;
font-size: 10px;
font-weight: 650;
}
.operating-note {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: -5px;
padding: 11px 13px;
border: 1px solid var(--line);
border-radius: 11px;
background: var(--paper-2);
color: var(--muted);
font-size: 11px;
}
.operating-note strong {
color: var(--ink);
font-size: 12px;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-bottom: 25px;
}
.metric {
min-height: 89px;
padding: 14px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(255, 255, 255, 0.62);
}
.metric-label {
display: block;
margin-bottom: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.metric strong {
display: block;
font-size: 22px;
font-weight: 780;
letter-spacing: -0.045em;
}
.metric small {
display: block;
margin-top: 4px;
color: #929b94;
font-size: 10px;
}
.metric.rent-card {
border-color: #f0c99e;
background: #fff5e7;
}
.metric.material-card {
border-color: #b6dcd6;
background: #edf8f5;
}
.allocation {
padding-top: 22px;
border-top: 1px solid var(--line);
}
.allocation-head {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.allocation-head h2 {
margin: 0;
font-size: 15px;
letter-spacing: -0.02em;
}
.allocation-head span {
color: var(--muted);
font-size: 11px;
}
.allocation-bar {
display: flex;
width: 100%;
height: 13px;
overflow: hidden;
border: 3px solid var(--paper);
border-radius: 999px;
background: #e8e4da;
box-shadow: 0 0 0 1px var(--line);
}
.allocation-bar span {
height: 100%;
transition: width 260ms ease;
}
#fixedBar {
background: var(--rent);
}
#materialBar {
background: var(--material);
}
.allocation-rows {
display: grid;
gap: 9px;
margin-top: 17px;
}
.allocation-row {
display: grid;
grid-template-columns: 11px 1fr auto;
align-items: center;
gap: 9px;
color: #53635d;
font-size: 12px;
}
.allocation-dot {
width: 9px;
height: 9px;
border-radius: 3px;
}
.allocation-row strong {
color: var(--ink);
font-size: 12px;
}
.per-cup {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 17px;
}
.per-cup > div {
padding: 11px 12px;
border-radius: 11px;
background: var(--paper-2);
}
.per-cup span {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.per-cup strong {
font-size: 17px;
}
.break-even {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
margin-top: 21px;
padding: 14px;
border: 1px solid var(--line);
border-radius: 14px;
}
.break-even-label {
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.break-even strong {
display: block;
margin-top: 4px;
font-size: 17px;
}
.status {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 7px 9px;
border-radius: 999px;
background: #e7f2ea;
color: var(--good);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.status::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
content: "";
}
.status.below {
background: #fae7e0;
color: #b6624c;
}
.panel-foot {
margin: 19px 0 0;
color: #9ba39d;
font-size: 10px;
line-height: 1.55;
}
.stage {
position: relative;
min-width: 0;
height: 100vh;
overflow: hidden;
background: var(--page);
isolation: isolate;
}
#scene {
position: absolute;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
#scene canvas:focus-visible {
outline: 3px solid rgba(62, 155, 152, 0.38);
outline-offset: -4px;
}
.stage-top {
position: absolute;
z-index: 4;
top: 25px;
right: 28px;
left: 28px;
display: flex;
align-items: center;
justify-content: space-between;
pointer-events: none;
}
.stage-kicker {
display: flex;
align-items: center;
gap: 9px;
color: #63726d;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.live-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--material);
box-shadow: 0 0 0 5px rgba(62, 155, 152, 0.12);
}
.stage-actions {
display: flex;
gap: 8px;
pointer-events: auto;
}
.icon-button {
display: inline-flex;
align-items: center;
gap: 8px;
min-height: 38px;
padding: 0 13px;
border: 1px solid rgba(25, 51, 49, 0.14);
border-radius: 999px;
background: rgba(255, 250, 240, 0.82);
color: var(--ink);
box-shadow: 0 6px 18px rgba(38, 53, 45, 0.07);
backdrop-filter: blur(10px);
cursor: pointer;
font-size: 11px;
font-weight: 750;
transition: transform 160ms ease, background 160ms ease;
}
.icon-button:hover {
transform: translateY(-1px);
background: #fffaf0;
}
.icon-button:focus-visible {
outline: 3px solid rgba(62, 155, 152, 0.25);
outline-offset: 2px;
}
.reset-icon {
position: relative;
width: 14px;
height: 14px;
border: 1.8px solid currentColor;
border-right-color: transparent;
border-radius: 50%;
}
.reset-icon::after {
position: absolute;
top: -2px;
right: -1px;
border-top: 4px solid transparent;
border-bottom: 4px solid transparent;
border-left: 5px solid currentColor;
content: "";
transform: rotate(-22deg);
}
.world-label {
position: absolute;
z-index: 3;
min-width: 148px;
padding: 11px 13px;
border: 1px solid rgba(25, 51, 49, 0.12);
border-top: 3px solid var(--rent);
border-radius: 11px;
background: rgba(255, 250, 240, 0.9);
box-shadow: var(--shadow);
pointer-events: none;
transform: translate(-50%, -100%);
backdrop-filter: blur(8px);
transition: opacity 120ms linear;
}
.world-label.material {
border-top-color: var(--material);
}
.world-label.output {
border-top-color: var(--ink);
}
.label-kicker {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 8px;
font-weight: 850;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.label-value {
display: block;
font-size: 19px;
font-weight: 820;
letter-spacing: -0.035em;
}
.label-note {
display: block;
margin-top: 2px;
color: #8a958e;
font-size: 9px;
}
.stage-bottom {
position: absolute;
z-index: 4;
right: 28px;
bottom: 24px;
left: 28px;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 20px;
pointer-events: none;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 8px 11px;
border: 1px solid rgba(25, 51, 49, 0.1);
border-radius: 999px;
background: rgba(255, 250, 240, 0.76);
color: #586861;
backdrop-filter: blur(8px);
font-size: 10px;
font-weight: 700;
}
.legend-swatch {
width: 10px;
height: 10px;
border-radius: 3px;
}
.legend-swatch.rent {
background: var(--rent);
}
.legend-swatch.material {
background: var(--material);
}
.legend-swatch.output {
background: var(--ink);
}
.gesture-hint {
color: #829088;
font-size: 10px;
text-align: right;
}
.gesture-hint strong {
color: #586861;
font-weight: 750;
}
@media (max-width: 900px) {
body {
overflow: auto;
}
.app {
display: flex;
height: auto;
min-height: 100vh;
flex-direction: column;
}
.stage {
order: 1;
width: 100%;
height: 62vh;
min-height: 480px;
}
.panel {
order: 2;
width: 100%;
height: auto;
overflow: visible;
border-top: 1px solid var(--line);
border-right: 0;
}
.intro {
max-width: 520px;
}
.metrics {
grid-template-columns: repeat(4, 1fr);
}
.metric {
min-height: 84px;
}
}
@media (max-width: 620px) {
.panel {
padding: 25px 20px 30px;
}
.stage {
height: 58vh;
min-height: 430px;
}
.stage-top {
top: 17px;
right: 17px;
left: 17px;
}
.stage-kicker {
font-size: 8px;
}
.icon-button {
width: 38px;
padding: 0;
justify-content: center;
}
.icon-button span:last-child {
display: none;
}
.stage-bottom {
right: 17px;
bottom: 17px;
left: 17px;
}
.gesture-hint {
display: none;
}
.metrics {
grid-template-columns: 1fr 1fr;
}
.world-label {
min-width: 122px;
padding: 8px 10px;
}
.label-value {
font-size: 16px;
}
.label-note {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
scroll-behavior: auto !important;
transition-duration: 0.01ms !important;
animation-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<div class="app">
<aside class="panel" aria-label="Cost model controls">
<div class="eyebrow">
<span class="eyebrow-mark" aria-hidden="true"></span>
Cost studio / 01
</div>
<h1>Every cup has<br /><em>two stories.</em></h1>
<p class="intro">
Rent is the anchor. Materials move with production. Adjust the model
and watch the cost of each cup change.
</p>
<h2 class="section-title">Model inputs</h2>
<section class="controls">
<div>
<div class="control-head">
<label for="cupsInput">Cups produced per day</label>
<output class="control-value" id="cupsValue">650</output>
</div>
<input
id="cupsInput"
type="range"
min="100"
max="2000"
step="50"
value="650"
/>
<div class="range-ends">
<span>100</span>
<span>2,000 cups</span>
</div>
</div>
<div>
<div class="control-head">
<label for="rentInput">Monthly rent</label>
<output class="control-value" id="rentValue">$4,800</output>
</div>
<input
id="rentInput"
class="rent-range"
type="range"
min="2000"
max="12000"
step="100"
value="4800"
/>
<div class="range-ends">
<span>$2,000</span>
<span>$12,000</span>
</div>
</div>
<div>
<div class="control-head">
<label for="materialInput">Material cost per cup</label>
<output class="control-value" id="materialValue">$0.42</output>
</div>
<input
id="materialInput"
type="range"
min="0.10"
max="1.50"
step="0.01"
value="0.42"
/>
<div class="range-ends">
<span>$0.10</span>
<span>$1.50</span>
</div>
</div>
<div class="operating-note">
<span>Operating days</span>
<strong>26 days / month</strong>
</div>
</section>
<h2 class="section-title">Monthly snapshot</h2>
<section class="metrics">
<article class="metric rent-card">
<span class="metric-label">Fixed rent</span>
<strong id="fixedMetric">$4,800</strong>
<small>same every month</small>
</article>
<article class="metric material-card">
<span class="metric-label">Materials</span>
<strong id="materialMetric">$7,098</strong>
<small id="outputSubtext">16,900 cups / month</small>
</article>
<article class="metric">
<span class="metric-label">Total cost</span>
<strong id="totalMetric">$11,898</strong>
<small>fixed + variable</small>
</article>
<article class="metric">
<span class="metric-label">All-in per cup</span>
<strong id="perCupMetric">$0.70</strong>
<small>including rent</small>
</article>
</section>
<section class="allocation">
<div class="allocation-head">
<h2>Where each monthly dollar goes</h2>
<span id="allocationTotal">$11,898</span>
</div>
<div class="allocation-bar" aria-hidden="true">
<span id="fixedBar"></span>
<span id="materialBar"></span>
</div>
<div class="allocation-rows">
<div class="allocation-row">
<span class="allocation-dot" style="background: var(--rent)"></span>
<span>Fixed rent</span>
<strong id="fixedAllocation">41%</strong>
</div>
<div class="allocation-row">
<span class="allocation-dot" style="background: var(--material)"></span>
<span>Variable materials</span>
<strong id="materialAllocation">59%</strong>
</div>
</div>
<div class="per-cup">
<div>
<span>Rent per cup</span>
<strong id="rentPerCup">$0.28</strong>
</div>
<div>
<span>Materials per cup</span>
<strong id="materialPerCup">$0.42</strong>
</div>
</div>
<div class="break-even">
<div>
<span class="break-even-label">Rent break-even</span>
<strong id="breakEvenValue">440 cups / day</strong>
</div>
<span class="status" id="statusBadge">Above break-even</span>
</div>
</section>
<p class="panel-foot">
Break-even is the daily output needed to cover monthly rent. It does
not include other operating expenses such as labor, utilities, or
packaging.
</p>
</aside>
<main class="stage" id="stage">
<div id="scene" aria-label="Interactive 3D cost model"></div>
<div class="stage-top">
<div class="stage-kicker">
<span class="live-dot"></span>
Live cost model
</div>
<div class="stage-actions">
<button class="icon-button" id="resetCamera" type="button">
<span class="reset-icon" aria-hidden="true"></span>
<span>Reset view</span>
</button>
</div>
</div>
<div class="world-label" id="rentLabel">
<span class="label-kicker">Monthly rent</span>
<strong class="label-value" id="rentLabelValue">$4,800</strong>
<span class="label-note">fixed, even when output changes</span>
</div>
<div class="world-label material" id="materialLabel">
<span class="label-kicker">Material blocks</span>
<strong class="label-value" id="materialLabelValue">$7,098</strong>
<span class="label-note">one block per 100 daily cups</span>
</div>
<div class="world-label output" id="outputLabel">
<span class="label-kicker">Monthly output</span>
<strong class="label-value" id="outputLabelValue">16,900 cups</strong>
<span class="label-note">finished product</span>
</div>
<div class="stage-bottom">
<div class="legend" aria-label="Scene legend">
<span class="legend-item">
<span class="legend-swatch rent"></span>
Fixed rent
</span>
<span class="legend-item">
<span class="legend-swatch material"></span>
Materials
</span>
<span class="legend-item">
<span class="legend-swatch output"></span>
Finished cups
</span>
</div>
<div class="gesture-hint">
<strong>Drag</strong> to orbit · <strong>Scroll</strong> to zoom
</div>
</div>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (selector) => document.querySelector(selector);
const state = {
cupsPerDay: 650,
daysPerMonth: 26,
monthlyRent: 4800,
materialPerCup: 0.42
};
const ui = {
scene: $("#scene"),
stage: $("#stage"),
cupsInput: $("#cupsInput"),
rentInput: $("#rentInput"),
materialInput: $("#materialInput"),
cupsValue: $("#cupsValue"),
rentValue: $("#rentValue"),
materialValue: $("#materialValue"),
fixedMetric: $("#fixedMetric"),
materialMetric: $("#materialMetric"),
totalMetric: $("#totalMetric"),
perCupMetric: $("#perCupMetric"),
outputSubtext: $("#outputSubtext"),
fixedBar: $("#fixedBar"),
materialBar: $("#materialBar"),
fixedAllocation: $("#fixedAllocation"),
materialAllocation: $("#materialAllocation"),
allocationTotal: $("#allocationTotal"),
rentPerCup: $("#rentPerCup"),
materialPerCup: $("#materialPerCup"),
breakEvenValue: $("#breakEvenValue"),
statusBadge: $("#statusBadge"),
rentLabel: $("#rentLabel"),
materialLabel: $("#materialLabel"),
outputLabel: $("#outputLabel"),
rentLabelValue: $("#rentLabelValue"),
materialLabelValue: $("#materialLabelValue"),
outputLabelValue: $("#outputLabelValue"),
resetCamera: $("#resetCamera")
};
const currency = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
maximumFractionDigits: 0
});
const currencyPrecise = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
const integer = new Intl.NumberFormat("en-US");
let renderer;
let scene;
let camera;
let fixedGroup;
let rentBlock;
let rentCap;
let rentFace;
let crateRoot;
let cupRoot;
let rentHeight = 3;
const beltSlats = [];
const cameraTarget = new THREE.Vector3(0.65, 1.05, 0);
let cameraTheta = 0.34;
let cameraPhi = 1.08;
let cameraRadius = 13.2;
let userAdjustedCamera = false;
const pointerMap = new Map();
let lastPinchDistance = null;
const crateGeometry = new THREE.BoxGeometry(0.67, 1, 0.34);
const crateEdgeGeometry = new THREE.EdgesGeometry(crateGeometry);
const crateBandGeometry = new THREE.BoxGeometry(0.43, 0.025, 0.35);
const cupGeometry = new THREE.CylinderGeometry(
0.205,
0.155,
0.46,
20,
1,
true
);
const coffeeGeometry = new THREE.CircleGeometry(0.182, 20);
const materials = {
floor: new THREE.MeshStandardMaterial({
color: 0xeee9de,
roughness: 1
}),
ink: new THREE.MeshStandardMaterial({
color: 0x183533,
roughness: 0.76
}),
rent: new THREE.MeshStandardMaterial({
color: 0xee9f50,
roughness: 0.62
}),
rentDark: new THREE.MeshStandardMaterial({
color: 0xd87e36,
roughness: 0.75
}),
material: new THREE.MeshStandardMaterial({
color: 0x3e9b98,
roughness: 0.57
}),
materialDark: new THREE.MeshStandardMaterial({
color: 0x2f7776,
roughness: 0.72
}),
cream: new THREE.MeshStandardMaterial({
color: 0xfff8e8,
roughness: 0.8
}),
cup: new THREE.MeshStandardMaterial({
color: 0xfffdf6,
roughness: 0.86,
side: THREE.DoubleSide
}),
coffee: new THREE.MeshStandardMaterial({
color: 0x6d4931,
roughness: 0.8
})
};
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x183533,
transparent: true,
opacity: 0.22
});
const rentTextureCanvas = document.createElement("canvas");
rentTextureCanvas.width = 512;
rentTextureCanvas.height = 300;
const crateTextureCanvas = document.createElement("canvas");
crateTextureCanvas.width = 256;
crateTextureCanvas.height = 128;
const rentTexture = new THREE.CanvasTexture(rentTextureCanvas);
rentTexture.colorSpace = THREE.SRGBColorSpace;
const crateTexture = new THREE.CanvasTexture(crateTextureCanvas);
crateTexture.colorSpace = THREE.SRGBColorSpace;
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function paintRentTexture() {
const ctx = rentTextureCanvas.getContext("2d");
ctx.clearRect(0, 0, 512, 300);
roundedRect(ctx, 8, 8, 496, 284, 30);
ctx.fillStyle = "#fff9ec";
ctx.fill();
ctx.lineWidth = 6;
ctx.strokeStyle = "#ed9d4d";
ctx.stroke();
ctx.textAlign = "center";
ctx.fillStyle = "#8b6748";
ctx.font = "800 28px Arial, sans-serif";
ctx.fillText("FIXED COST / MONTH", 256, 66);
ctx.fillStyle = "#193331";
ctx.font = "800 76px Arial, sans-serif";
ctx.fillText(currency.format(state.monthlyRent), 256, 157);
ctx.fillStyle = "#718079";
ctx.font = "700 23px Arial, sans-serif";
ctx.fillText("RENT STAYS PUT", 256, 220);
roundedRect(ctx, 168, 240, 176, 20, 10);
ctx.fillStyle = "#f6d6ae";
ctx.fill();
rentTexture.needsUpdate = true;
}
function paintCrateTexture() {
const ctx = crateTextureCanvas.getContext("2d");
ctx.clearRect(0, 0, 256, 128);
roundedRect(ctx, 4, 4, 248, 120, 18);
ctx.fillStyle = "#f8f4e9";
ctx.fill();
ctx.lineWidth = 4;
ctx.strokeStyle = "#2f7776";
ctx.stroke();
ctx.textAlign = "center";
ctx.fillStyle = "#397b78";
ctx.font = "800 18px Arial, sans-serif";
ctx.fillText("MATERIALS", 128, 43);
ctx.fillStyle = "#193331";
ctx.font = "800 37px Arial, sans-serif";
ctx.fillText(currencyPrecise.format(state.materialPerCup), 128, 91);
crateTexture.needsUpdate = true;
}
function addEdges(mesh) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry),
edgeMaterial
);
mesh.add(edges);
return edges;
}
function createBox(width, height, depth, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function createScene() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0xf2eee5);
scene.fog = new THREE.Fog(0xf2eee5, 17, 31);
camera = new THREE.PerspectiveCamera(38, 1, 0.1, 80);
renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"3D cost model. Drag to orbit, scroll to zoom, and use arrow keys to rotate."
);
ui.scene.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xffffff, 0x9b8f7d, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff6e5, 4.1);
sun.position.set(-5, 10, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -11;
sun.shadow.camera.right = 11;
sun.shadow.camera.top = 11;
sun.shadow.camera.bottom = -11;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 30;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const rim = new THREE.DirectionalLight(0xb9e7e4, 1.25);
rim.position.set(7, 5, -6);
scene.add(rim);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
materials.floor
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.02;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 30, 0xb9c2b8, 0xd5d8cf);
grid.position.y = 0.002;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
createFixedCostBlock();
createConveyor();
createOutputPlatform();
const decorOne = new THREE.Mesh(
new THREE.CylinderGeometry(2.2, 2.2, 0.08, 64),
new THREE.MeshStandardMaterial({
color: 0xe8e1d4,
roughness: 1
})
);
decorOne.position.set(-4.0, 0.03, 2.8);
decorOne.receiveShadow = true;
scene.add(decorOne);
const decorTwo = new THREE.Mesh(
new THREE.CylinderGeometry(1.35, 1.35, 0.06, 48),
new THREE.MeshStandardMaterial({
color: 0xe3ece5,
roughness: 1
})
);
decorTwo.position.set(6.8, 0.025, -3.2);
decorTwo.receiveShadow = true;
scene.add(decorTwo);
updateCamera();
setupCameraControls();
setupResizeObserver();
}
function createFixedCostBlock() {
fixedGroup = new THREE.Group();
fixedGroup.position.set(-3.45, 0, 0);
scene.add(fixedGroup);
const foundation = createBox(3.25, 0.28, 2.9, materials.rentDark);
foundation.position.y = 0.14;
addEdges(foundation);
fixedGroup.add(foundation);
const lowerFoot = createBox(2.85, 0.13, 2.5, materials.cream);
lowerFoot.position.y = 0.345;
fixedGroup.add(lowerFoot);
rentBlock = createBox(2.45, 1, 2.08, materials.rent);
rentBlock.position.y = 2;
addEdges(rentBlock);
fixedGroup.add(rentBlock);
rentCap = createBox(2.64, 0.16, 2.25, materials.rentDark);
rentCap.position.y = 3.65;
addEdges(rentCap);
fixedGroup.add(rentCap);
const faceMaterial = new THREE.MeshBasicMaterial({
map: rentTexture,
transparent: true,
toneMapped: false
});
rentFace = new THREE.Mesh(
new THREE.PlaneGeometry(2.02, 1.18),
faceMaterial
);
rentFace.position.set(0, 2, 1.047);
fixedGroup.add(rentFace);
const anchorGeometry = new THREE.CylinderGeometry(
0.075,
0.075,
0.14,
12
);
[-1.36, 1.36].forEach((x) => {
[-1.15, 1.15].forEach((z) => {
const anchor = new THREE.Mesh(anchorGeometry, materials.ink);
anchor.position.set(x, 0.34, z);
anchor.castShadow = true;
fixedGroup.add(anchor);
});
});
const sideStripe = createBox(0.06, 1, 1.72, materials.rentDark);
sideStripe.position.set(1.238, 2, 0);
sideStripe.scale.y = rentHeight;
fixedGroup.add(sideStripe);
}
function createConveyor() {
const conveyor = new THREE.Group();
conveyor.position.set(1.65, 0, 0);
scene.add(conveyor);
const frame = createBox(5.45, 0.27, 1.98, materials.ink);
frame.position.y = 0.51;
addEdges(frame);
conveyor.add(frame);
const belt = createBox(5.16, 0.12, 1.72, materials.materialDark);
belt.position.y = 0.705;
conveyor.add(belt);
const rollerGeometry = new THREE.CylinderGeometry(
0.16,
0.16,
1.92,
16
);
for (let x = -2.48; x <= 2.5; x += 0.5) {
const roller = new THREE.Mesh(rollerGeometry, materials.ink);
roller.rotation.x = Math.PI / 2;
roller.position.set(x, 0.55, 0);
roller.castShadow = true;
conveyor.add(roller);
}
const legGeometry = new THREE.BoxGeometry(0.16, 0.74, 0.16);
[-2.2, 2.2].forEach((x) => {
[-0.7, 0.7].forEach((z) => {
const leg = new THREE.Mesh(legGeometry, materials.ink);
leg.position.set(x, 0.25, z);
leg.castShadow = true;
conveyor.add(leg);
});
});
const slatGeometry = new THREE.BoxGeometry(0.055, 0.025, 1.64);
for (let x = -2.5; x <= 2.5; x += 0.37) {
const slat = new THREE.Mesh(slatGeometry, materials.cream);
slat.position.set(x, 0.78, 0);
slat.userData.baseX = x;
slat.castShadow = true;
beltSlats.push(slat);
conveyor.add(slat);
}
crateRoot = new THREE.Group();
conveyor.add(crateRoot);
}
function createOutputPlatform() {
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(1.34, 1.42, 0.18, 48),
materials.ink
);
platform.position.set(5.55, 0.09, 0);
platform.castShadow = true;
platform.receiveShadow = true;
scene.add(platform);
const top = new THREE.Mesh(
new THREE.CylinderGeometry(1.19, 1.19, 0.045, 48),
materials.cream
);
top.position.set(5.55, 0.2, 0);
top.receiveShadow = true;
scene.add(top);
cupRoot = new THREE.Group();
cupRoot.position.set(5.55, 0, 0);
scene.add(cupRoot);
}
function rebuildCrates() {
while (crateRoot.children.length) {
crateRoot.remove(crateRoot.children[0]);
}
const count = Math.max(1, Math.ceil(state.cupsPerDay / 100));
const columns = 5;
const rows = 4;
const height =
0.43 +
((state.materialPerCup - 0.1) / 1.4) * 0.28;
const labelMaterial = new THREE.MeshBasicMaterial({
map: crateTexture,
transparent: true,
toneMapped: false,
depthWrite: false
});
for (let i = 0; i < count; i++) {
const group = new THREE.Group();
const column = i % columns;
const row = Math.floor(i / columns);
const x = -2 + column;
const z = -0.63 + row * 0.42;
group.position.set(x, 0, z);
const block = new THREE.Mesh(crateGeometry, materials.material);
block.scale.y = height;
block.position.y = 0.79 + height / 2;
block.castShadow = true;
block.receiveShadow = true;
const edges = new THREE.LineSegments(crateEdgeGeometry, edgeMaterial);
block.add(edges);
group.add(block);
const band = new THREE.Mesh(crateBandGeometry, materials.cream);
band.position.y = 0.79 + height + 0.012;
group.add(band);
const label = new THREE.Mesh(
new THREE.PlaneGeometry(0.5, 0.24),
labelMaterial
);
label.position.set(0, 0.79 + height * 0.52, 0.176);
group.add(label);
crateRoot.add(group);
}
}
function rebuildCups() {
while (cupRoot.children.length) {
cupRoot.remove(cupRoot.children[0]);
}
const stackCount = Math.max(1, Math.ceil(state.cupsPerDay / 400));
const positions = [-0.68, -0.34, 0, 0.34, 0.68];
for (let stack = 0; stack < stackCount; stack++) {
const x = positions[stack];
const z = stack % 2 === 0 ? -0.18 : 0.18;
const cupsInStack = 2 + ((stack + 1) % 3);
for (let level = 0; level < cupsInStack; level++) {
const cup = new THREE.Mesh(cupGeometry, materials.cup);
cup.position.set(x, 0.47 + level * 0.34, z);
cup.castShadow = true;
cupRoot.add(cup);
const coffee = new THREE.Mesh(coffeeGeometry, materials.coffee);
coffee.rotation.x = -Math.PI / 2;
coffee.position.set(x, 0.692 + level * 0.34, z);
cupRoot.add(coffee);
}
}
}
function updateScene() {
const rentRatio = THREE.MathUtils.clamp(
(state.monthlyRent - 2000) / 10000,
0,
1
);
rentHeight = 2.15 + rentRatio * 1.45;
rentBlock.scale.y = rentHeight;
rentBlock.position.y = 0.41 + rentHeight / 2;
rentCap.position.y = 0.41 + rentHeight + 0.08;
rentFace.position.y = 0.41 + rentHeight * 0.55;
const sideStripe = fixedGroup.children.find(
(child) => child.geometry?.parameters?.width === 0.06
);
if (sideStripe) {
sideStripe.scale.y = rentHeight;
sideStripe.position.y = 0.41 + rentHeight / 2;
}
paintRentTexture();
paintCrateTexture();
rebuildCrates();
rebuildCups();
}
function updateRangeFill(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
const percentage = ((value - min) / (max - min)) * 100;
input.style.setProperty("--fill", `${percentage}%`);
}
function refreshModel() {
const monthlyCups = state.cupsPerDay * state.daysPerMonth;
const variableCost = monthlyCups * state.materialPerCup;
const totalCost = state.monthlyRent + variableCost;
const rentPerCup = state.monthlyRent / monthlyCups;
const allInPerCup = totalCost / monthlyCups;
const breakEvenDaily = state.monthlyRent /
(state.materialPerCup * state.daysPerMonth);
const fixedShare = state.monthlyRent / totalCost;
const materialShare = 1 - fixedShare;
ui.cupsValue.textContent = integer.format(state.cupsPerDay);
ui.rentValue.textContent = currency.format(state.monthlyRent);
ui.materialValue.textContent = currencyPrecise.format(
state.materialPerCup
);
ui.fixedMetric.textContent = currency.format(state.monthlyRent);
ui.materialMetric.textContent = currency.format(variableCost);
ui.totalMetric.textContent = currency.format(totalCost);
ui.perCupMetric.textContent = currencyPrecise.format(allInPerCup);
ui.outputSubtext.textContent = `${integer.format(monthlyCups)} cups / month`;
ui.fixedBar.style.width = `${fixedShare * 100}%`;
ui.materialBar.style.width = `${materialShare * 100}%`;
ui.fixedAllocation.textContent = `${Math.round(fixedShare * 100)}%`;
ui.materialAllocation.textContent = `${Math.round(materialShare * 100)}%`;
ui.allocationTotal.textContent = currency.format(totalCost);
ui.rentPerCup.textContent = currencyPrecise.format(rentPerCup);
ui.materialPerCup.textContent = currencyPrecise.format(
state.materialPerCup
);
ui.breakEvenValue.textContent = `${integer.format(
Math.ceil(breakEvenDaily)
)} cups / day`;
const aboveBreakEven = state.cupsPerDay >= breakEvenDaily;
ui.statusBadge.classList.toggle("below", !aboveBreakEven);
ui.statusBadge.textContent = aboveBreakEven
? "Above break-even"
: "Below break-even";
ui.rentLabelValue.textContent = currency.format(state.monthlyRent);
ui.materialLabelValue.textContent = currency.format(variableCost);
ui.outputLabelValue.textContent = `${integer.format(monthlyCups)} cups`;
updateRangeFill(ui.cupsInput);
updateRangeFill(ui.rentInput);
updateRangeFill(ui.materialInput);
updateScene();
}
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraTheta = 0.34;
cameraPhi = 1.08;
const aspect = ui.stage.clientWidth / ui.stage.clientHeight;
cameraRadius = aspect < 0.8 ? 19 : aspect < 1.1 ? 16 : 13.2;
userAdjustedCamera = false;
updateCamera();
}
function setupCameraControls() {
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointerMap.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointerMap.size === 2) {
lastPinchDistance = getPinchDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointerMap.get(event.pointerId);
if (!previous) return;
pointerMap.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointerMap.size === 1) {
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi + dy * 0.005,
0.46,
1.42
);
userAdjustedCamera = true;
updateCamera();
} else if (pointerMap.size === 2) {
const distance = getPinchDistance();
if (lastPinchDistance && distance > 0) {
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * (lastPinchDistance / distance),
8,
26
);
userAdjustedCamera = true;
updateCamera();
}
lastPinchDistance = distance;
}
});
const releasePointer = (event) => {
pointerMap.delete(event.pointerId);
lastPinchDistance =
pointerMap.size === 2 ? getPinchDistance() : null;
};
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("lostpointercapture", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.008,
8,
26
);
userAdjustedCamera = true;
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
cameraTheta += 0.09;
break;
case "ArrowRight":
cameraTheta -= 0.09;
break;
case "ArrowUp":
cameraPhi = Math.max(0.46, cameraPhi - 0.07);
break;
case "ArrowDown":
cameraPhi = Math.min(1.42, cameraPhi + 0.07);
break;
case "+":
case "=":
cameraRadius = Math.max(8, cameraRadius - 0.6);
break;
case "-":
cameraRadius = Math.min(26, cameraRadius + 0.6);
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
userAdjustedCamera = true;
updateCamera();
}
});
ui.resetCamera.addEventListener("click", resetCamera);
}
function getPinchDistance() {
const points = [...pointerMap.values()];
if (points.length < 2) return 0;
return Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
function setupResizeObserver() {
const resize = () => {
const width = ui.stage.clientWidth;
const height = ui.stage.clientHeight;
const aspect = width / height;
renderer.setSize(width, height, false);
camera.aspect = aspect;
camera.updateProjectionMatrix();
if (!userAdjustedCamera) {
cameraRadius = aspect < 0.8 ? 19 : aspect < 1.1 ? 16 : 13.2;
}
updateCamera();
};
new ResizeObserver(resize).observe(ui.stage);
resize();
}
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.25 &&
Math.abs(projected.y) < 1.25;
element.style.opacity = visible ? "1" : "0";
const x = (projected.x * 0.5 + 0.5) * ui.stage.clientWidth;
const y = (-projected.y * 0.5 + 0.5) * ui.stage.clientHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateLabels() {
positionLabel(
ui.rentLabel,
new THREE.Vector3(-3.45, rentHeight + 1.1, 0)
);
positionLabel(
ui.materialLabel,
new THREE.Vector3(1.0, 2.22, 0)
);
positionLabel(
ui.outputLabel,
new THREE.Vector3(5.55, 1.75, 0)
);
}
function bindControls() {
ui.cupsInput.addEventListener("input", () => {
state.cupsPerDay = Number(ui.cupsInput.value);
refreshModel();
});
ui.rentInput.addEventListener("input", () => {
state.monthlyRent = Number(ui.rentInput.value);
refreshModel();
});
ui.materialInput.addEventListener("input", () => {
state.materialPerCup = Number(ui.materialInput.value);
refreshModel();
});
}
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
for (const slat of beltSlats) {
const wrapped =
((slat.userData.baseX + elapsed * 0.42 + 2.62) % 5.24) -
2.62;
slat.position.x = wrapped;
}
updateLabels();
renderer.render(scene, camera);
}
createScene();
bindControls();
refreshModel();
animate();
</script>
</body>
</html>
```