Prompt specification
Build or customize the Three.js 3D scene "Piston cylinder cycle model" 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: Piston cylinder cycle model
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/piston-cylinder-cycle-model-x1477
Scene Description Prompt:
I'd like to see this as a low-poly Three.js scene: piston cylinder cycle model. Build a gas-filled cylinder with piston, heat-transfer arrows, and a simple pressure-volume panel; distinguish piston position from gas temperature and label the cycle idealized. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (piston-cylinder-cycle-model-x1477):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Idealized Piston–Cylinder Cycle</title>
<style>
:root {
color-scheme: dark;
--bg: #08101d;
--panel: #101a2b;
--panel-2: #142238;
--line: #26364d;
--text: #eaf1fb;
--muted: #93a4ba;
--amber: #f4b84a;
--cold: #45b7ff;
--hot: #ff654d;
--green: #63e6b2;
}
* { box-sizing: border-box; }
html, body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
height: 100vh;
overflow: hidden;
}
button, input { font: inherit; }
button {
border: 1px solid #34465f;
background: #17243a;
color: var(--text);
border-radius: 9px;
cursor: pointer;
transition: background .18s ease, border-color .18s ease, transform .18s ease;
}
button:hover {
background: #21324d;
border-color: #55708f;
}
button:active { transform: translateY(1px); }
button:focus-visible,
input:focus-visible {
outline: 2px solid #76bfff;
outline-offset: 3px;
}
.app {
height: 100vh;
display: grid;
grid-template-columns: minmax(0, 1.25fr) minmax(390px, .75fr);
}
.scene {
position: relative;
min-width: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 38%, #172945 0, #0d192b 42%, #08111e 100%);
border-right: 1px solid #24354d;
}
#sceneCanvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#sceneCanvas:active { cursor: grabbing; }
.scene-top {
position: absolute;
z-index: 5;
top: 22px;
left: 24px;
right: 24px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 18px;
pointer-events: none;
}
.eyebrow {
color: #9bb0c9;
font-size: 11px;
font-weight: 800;
letter-spacing: .16em;
text-transform: uppercase;
}
h1 {
margin: 5px 0 4px;
font-size: clamp(22px, 2.6vw, 36px);
line-height: 1.05;
letter-spacing: -.035em;
}
.subtitle {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.ideal-badge {
flex: 0 0 auto;
padding: 9px 12px;
border: 1px solid #ffb85c70;
border-radius: 999px;
color: #ffd08c;
background: #392b18aa;
box-shadow: 0 8px 30px #0004;
font-size: 11px;
font-weight: 850;
letter-spacing: .13em;
text-transform: uppercase;
}
.scene-help {
position: absolute;
z-index: 5;
left: 24px;
bottom: 20px;
color: #8298b2;
font-size: 11px;
pointer-events: none;
}
.view-button {
position: absolute;
z-index: 6;
right: 20px;
bottom: 17px;
padding: 8px 11px;
background: #101c2dd9;
font-size: 11px;
font-weight: 700;
}
.loading {
position: absolute;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
color: #a9bdd4;
background: #08111e;
font-size: 13px;
letter-spacing: .08em;
text-transform: uppercase;
}
.tag {
position: absolute;
z-index: 4;
left: 0;
top: 0;
transform: translate(-50%, -50%);
min-width: 128px;
padding: 9px 11px;
border: 1px solid #3b4c64;
border-radius: 10px;
background: #0a1424e8;
box-shadow: 0 10px 30px #0006;
pointer-events: none;
backdrop-filter: blur(8px);
}
.tag::after {
content: "";
position: absolute;
width: 7px;
height: 7px;
left: 50%;
bottom: -5px;
transform: translateX(-50%) rotate(45deg);
border-right: 1px solid #3b4c64;
border-bottom: 1px solid #3b4c64;
background: #0a1424;
}
.tag strong {
display: block;
margin-top: 3px;
font-size: 13px;
white-space: nowrap;
}
.tag span {
font-size: 10px;
font-weight: 800;
letter-spacing: .11em;
text-transform: uppercase;
}
#pistonTag {
border-color: #f4b84a80;
}
#pistonTag span { color: var(--amber); }
#tempTag {
border-color: color-mix(in srgb, var(--temp-color) 65%, transparent);
}
#tempTag span { color: var(--temp-color); }
.heat-tag {
min-width: auto;
padding: 7px 10px;
font-weight: 850;
letter-spacing: .1em;
font-size: 10px;
text-transform: uppercase;
}
#heatInTag {
color: #ffad69;
border-color: #ff854b88;
}
#heatOutTag {
color: #7bcaff;
border-color: #4caeff88;
}
.panel {
overflow-y: auto;
padding: 24px 24px 28px;
background:
linear-gradient(180deg, #111d30 0, #0d1727 100%);
scrollbar-color: #354a67 #101a2b;
}
.panel-heading {
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 12px;
margin-bottom: 17px;
}
.panel h2 {
margin: 3px 0 0;
font-size: 21px;
letter-spacing: -.02em;
}
.live-pill {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 9px;
border: 1px solid #315b54;
border-radius: 999px;
color: #8ce9c5;
background: #15342e99;
font-size: 10px;
font-weight: 800;
letter-spacing: .09em;
text-transform: uppercase;
white-space: nowrap;
}
.live-pill::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 10px var(--green);
}
.controls {
padding: 14px;
border: 1px solid var(--line);
border-radius: 14px;
background: #121f33;
margin-bottom: 14px;
}
.control-row {
display: flex;
align-items: center;
gap: 8px;
}
.control-row button {
min-height: 36px;
padding: 7px 11px;
font-size: 12px;
font-weight: 750;
}
#playButton {
min-width: 88px;
color: #07131c;
background: #6ce2b3;
border-color: #6ce2b3;
}
#playButton:hover { background: #91edc8; }
#speedButton { margin-left: auto; min-width: 48px; }
.scrubber-label {
display: flex;
justify-content: space-between;
margin: 13px 0 6px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: .08em;
text-transform: uppercase;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 7px 0 3px;
border-radius: 999px;
appearance: none;
background:
linear-gradient(90deg, #6ce2b3 var(--progress, 0%), #2a3b54 var(--progress, 0%));
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
border: 2px solid #d8fff1;
border-radius: 50%;
appearance: none;
background: #6ce2b3;
box-shadow: 0 0 0 4px #6ce2b322;
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 2px solid #d8fff1;
border-radius: 50%;
background: #6ce2b3;
}
.phase {
display: grid;
grid-template-columns: auto 1fr;
align-items: center;
gap: 12px;
margin-top: 13px;
padding-top: 13px;
border-top: 1px solid #27374e;
}
.phase-letter {
display: grid;
place-items: center;
width: 36px;
height: 36px;
border: 1px solid #53708e;
border-radius: 50%;
color: #d7e9fa;
background: #182a41;
font-weight: 850;
}
.phase strong {
display: block;
font-size: 13px;
}
.phase p {
margin: 3px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
.readouts {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 10px;
margin-bottom: 14px;
}
.readout {
min-height: 96px;
padding: 13px;
border: 1px solid var(--line);
border-radius: 12px;
background: #121e31;
}
.readout.position { border-color: #f4b84a3d; }
.readout.temperature { border-color: color-mix(in srgb, var(--temp-color) 35%, #26364d); }
.readout-label {
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: .12em;
text-transform: uppercase;
}
.readout-value {
margin-top: 7px;
font-size: 24px;
font-weight: 800;
letter-spacing: -.04em;
}
.readout-value small {
color: var(--muted);
font-size: 11px;
font-weight: 650;
letter-spacing: 0;
}
.position .readout-value { color: var(--amber); }
.temperature .readout-value { color: var(--temp-color); }
.meter {
position: relative;
height: 5px;
margin-top: 10px;
overflow: hidden;
border-radius: 99px;
background: #26364d;
}
.meter > span {
position: absolute;
inset: 0 auto 0 0;
width: 50%;
border-radius: inherit;
background: var(--amber);
}
.temperature .meter > span { background: var(--temp-color); }
.meter-label {
margin-top: 6px;
color: #73879f;
font-size: 9px;
}
.chart-card,
.encoding-card,
.process-card {
border: 1px solid var(--line);
border-radius: 14px;
background: #111d30;
overflow: hidden;
}
.chart-card { margin-bottom: 14px; }
.card-title {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
padding: 13px 14px 0;
}
.card-title h3 {
margin: 0;
font-size: 13px;
letter-spacing: -.01em;
}
.card-title span {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: .1em;
text-transform: uppercase;
}
.chart-wrap { padding: 0 8px 3px; }
#pvChart {
display: block;
width: 100%;
height: auto;
overflow: visible;
}
#pvChart text {
fill: #8297af;
font-family: inherit;
font-size: 10px;
}
#pvChart .axis-label {
fill: #b9c9da;
font-size: 11px;
font-weight: 750;
}
#pvChart .grid {
stroke: #26374d;
stroke-width: 1;
stroke-dasharray: 3 5;
}
#pvChart .axis {
stroke: #52657d;
stroke-width: 1;
}
#pvChart .cycle-path {
fill: none;
stroke: #3c5068;
stroke-width: 2;
stroke-linejoin: round;
stroke-linecap: round;
transition: stroke .15s ease, stroke-width .15s ease;
}
#pvChart .cycle-path.active {
stroke: #7ee8bd;
stroke-width: 4;
filter: drop-shadow(0 0 5px #6ce2b355);
}
#pvChart .state-dot {
fill: #0f1b2d;
stroke: #8aa1ba;
stroke-width: 2;
}
#pvChart .state-letter {
fill: #d8e5f2;
font-size: 10px;
font-weight: 850;
}
#pvChart #crossX,
#pvChart #crossY {
stroke: #7ee8bd66;
stroke-width: 1;
stroke-dasharray: 4 5;
}
#pvChart #currentDot {
fill: #79edbd;
stroke: #e3fff4;
stroke-width: 2.5;
filter: drop-shadow(0 0 7px #6ce2b3aa);
}
.encoding-card {
display: grid;
grid-template-columns: 1fr 1fr;
margin-bottom: 14px;
}
.encoding-item {
padding: 13px;
}
.encoding-item + .encoding-item {
border-left: 1px solid var(--line);
}
.encoding-item strong {
display: flex;
align-items: center;
gap: 7px;
font-size: 11px;
}
.encoding-item strong::before {
content: "";
width: 9px;
height: 9px;
border-radius: 3px;
background: var(--amber);
}
.encoding-item.temperature strong::before {
background: var(--temp-color);
}
.encoding-item p {
margin: 7px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.process-card { padding: 13px 14px; }
.process-list {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 7px;
margin-top: 11px;
}
.process-step {
min-width: 0;
padding: 9px 7px;
border: 1px solid #2a3a50;
border-radius: 9px;
color: #7f94ad;
background: #0e192a;
transition: .18s ease;
}
.process-step b {
display: block;
margin-bottom: 4px;
color: #a9bdd2;
font-size: 10px;
}
.process-step span {
display: block;
font-size: 9px;
line-height: 1.35;
}
.process-step.active {
border-color: #6ce2b3;
color: #d9fff0;
background: #17352f;
box-shadow: inset 0 0 0 1px #6ce2b322;
}
.process-step.active b { color: #87edc2; }
.footnote {
margin: 13px 2px 0;
color: #7489a2;
font-size: 10px;
line-height: 1.5;
}
@media (max-width: 900px) {
body {
height: auto;
overflow: auto;
}
.app {
height: auto;
grid-template-columns: 1fr;
}
.scene {
height: 60vh;
min-height: 470px;
border-right: 0;
border-bottom: 1px solid #24354d;
}
.panel {
overflow: visible;
padding: 20px 16px 28px;
}
}
@media (max-width: 520px) {
.scene {
height: 58vh;
min-height: 430px;
}
.scene-top {
top: 15px;
left: 15px;
right: 15px;
}
.subtitle { max-width: 230px; }
.ideal-badge { padding: 7px 9px; font-size: 9px; }
.scene-help { left: 15px; }
.readouts { gap: 8px; }
.readout { padding: 11px; }
.readout-value { font-size: 21px; }
.encoding-card { grid-template-columns: 1fr; }
.encoding-item + .encoding-item {
border-left: 0;
border-top: 1px solid var(--line);
}
.process-list { grid-template-columns: 1fr 1fr; }
}
</style>
</head>
<body>
<main class="app">
<section class="scene" id="sceneWrap" aria-label="Interactive low-poly piston cylinder model">
<canvas id="sceneCanvas"></canvas>
<div class="scene-top">
<div>
<div class="eyebrow">Piston–cylinder / mixed cycle</div>
<h1>Thermal Cycle Explorer</h1>
<p class="subtitle">Mechanical position and gas temperature are shown independently.</p>
</div>
<div class="ideal-badge">Idealized cycle</div>
</div>
<div class="tag" id="pistonTag">
<span>Piston position</span>
<strong id="pistonTagValue">0% stroke</strong>
</div>
<div class="tag" id="tempTag">
<span>Gas temperature</span>
<strong id="tempTagValue">329 K</strong>
</div>
<div class="tag heat-tag" id="heatInTag">Heat in</div>
<div class="tag heat-tag" id="heatOutTag">Heat out</div>
<div class="scene-help">Drag to orbit · scroll to zoom · shift-drag to pan</div>
<button class="view-button" id="resetCamera" type="button">Reset view</button>
<div class="loading" id="loading">Loading 3D model…</div>
</section>
<aside class="panel" aria-label="Cycle controls and pressure-volume diagram">
<div class="panel-heading">
<div>
<div class="eyebrow">Educational visualization</div>
<h2>Cycle controls</h2>
</div>
<div class="live-pill">Schematic</div>
</div>
<div class="controls">
<div class="control-row">
<button id="playButton" type="button">Pause</button>
<button id="resetCycle" type="button" title="Restart cycle">↺ Restart</button>
<button id="speedButton" type="button" title="Change animation speed">1×</button>
</div>
<div class="scrubber-label">
<span>Cycle position</span>
<span id="progressText">0%</span>
</div>
<input id="cycleSlider" type="range" min="0" max="1" step="0.001" value="0"
aria-label="Cycle progress">
<div class="phase">
<div class="phase-letter" id="phaseLetter">A</div>
<div>
<strong id="phaseName">A → B · Polytropic expansion</strong>
<p id="phaseDetail">Gas expands while external heat raises its temperature.</p>
</div>
</div>
</div>
<div class="readouts">
<div class="readout position">
<div class="readout-label">Piston position</div>
<div class="readout-value"><span id="positionValue">0</span><small> % stroke</small></div>
<div class="meter"><span id="positionMeter"></span></div>
<div class="meter-label">Mechanical position · independent variable</div>
</div>
<div class="readout temperature">
<div class="readout-label">Gas temperature</div>
<div class="readout-value"><span id="temperatureValue">329</span><small> K</small></div>
<div class="meter"><span id="temperatureMeter"></span></div>
<div class="meter-label">Thermal state · color encoded</div>
</div>
<div class="readout">
<div class="readout-label">Pressure</div>
<div class="readout-value"><span id="pressureValue">3.29</span><small> p.u.</small></div>
<div class="meter-label">Normalized model units</div>
</div>
<div class="readout">
<div class="readout-label">Volume</div>
<div class="readout-value"><span id="volumeValue">1.00</span><small> V</small></div>
<div class="meter-label">Normalized cylinder volume</div>
</div>
</div>
<section class="chart-card" aria-labelledby="chartTitle">
<div class="card-title">
<h3 id="chartTitle">Pressure–volume diagram</h3>
<span>Current state</span>
</div>
<div class="chart-wrap">
<svg id="pvChart" viewBox="0 0 540 350" role="img"
aria-label="Pressure volume cycle with state A, B, C and D">
<g id="chartGrid"></g>
<path id="seg0" class="cycle-path"></path>
<path id="seg1" class="cycle-path"></path>
<path id="seg2" class="cycle-path"></path>
<path id="seg3" class="cycle-path"></path>
<line id="crossX" x1="0" x2="0" y1="24" y2="292"></line>
<line id="crossY" x1="58" x2="500" y1="0" y2="0"></line>
<g id="stateDots"></g>
<circle id="currentDot" cx="0" cy="0" r="6"></circle>
<text class="axis-label" x="282" y="333" text-anchor="middle">Volume V →</text>
<text class="axis-label" transform="translate(17 160) rotate(-90)" text-anchor="middle">
Pressure P →
</text>
</svg>
</div>
</section>
<section class="encoding-card" aria-label="Visual encoding explanation">
<div class="encoding-item">
<strong>Piston position</strong>
<p>Encoded by the amber piston, rod, and ruler. It is not the gas color.</p>
</div>
<div class="encoding-item temperature">
<strong>Gas temperature</strong>
<p>Encoded by the blue-to-red gas hue and thermometer value, not piston motion.</p>
</div>
</section>
<section class="process-card" aria-labelledby="processTitle">
<div class="card-title" style="padding:0">
<h3 id="processTitle">Four idealized processes</h3>
<span>Heat arrows show direction</span>
</div>
<div class="process-list">
<div class="process-step active" data-stage="0">
<b>A → B</b><span>Expansion<br>Heat in</span>
</div>
<div class="process-step" data-stage="1">
<b>B → C</b><span>Adiabatic<br>compression</span>
</div>
<div class="process-step" data-stage="2">
<b>C → D</b><span>Fixed V<br>Heat out</span>
</div>
<div class="process-step" data-stage="3">
<b>D → A</b><span>Adiabatic<br>expansion</span>
</div>
</div>
</section>
<p class="footnote">
Idealized, educational mixed cycle with normalized pressure and volume. Process paths and
cylinder dimensions are schematic rather than to scale. Adiabatic means idealized zero heat
transfer during that process.
</p>
</aside>
</main>
<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 canvas = $("#sceneCanvas");
const sceneWrap = $("#sceneWrap");
// -------------------------------------------------------------------------
// Renderer, scene, lighting
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true, alpha: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 100);
const hemi = new THREE.HemisphereLight(0xcce7ff, 0x182236, 2.2);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xfff1d2, 3.1);
key.position.set(4.5, 7, 5);
key.castShadow = true;
key.shadow.mapSize.set(1024, 1024);
key.shadow.camera.left = -5;
key.shadow.camera.right = 5;
key.shadow.camera.top = 5;
key.shadow.camera.bottom = -5;
scene.add(key);
const rim = new THREE.DirectionalLight(0x4ba8ff, 2.2);
rim.position.set(-5, 3, -4);
scene.add(rim);
const warm = new THREE.PointLight(0xff7d48, 12, 7, 2);
warm.position.set(-2.4, 1.1, 2.3);
scene.add(warm);
// -------------------------------------------------------------------------
// Direct camera controls: no addons or example modules
// -------------------------------------------------------------------------
class OrbitCamera {
constructor(camera, element) {
this.camera = camera;
this.element = element;
this.target = new THREE.Vector3(0, 0.25, 0);
this.theta = 0.58;
this.phi = 1.13;
this.radius = 7.7;
this.dragging = false;
this.lastX = 0;
this.lastY = 0;
element.addEventListener("pointerdown", (event) => {
this.dragging = true;
this.lastX = event.clientX;
this.lastY = event.clientY;
element.setPointerCapture(event.pointerId);
});
element.addEventListener("pointermove", (event) => {
if (!this.dragging) return;
const dx = event.clientX - this.lastX;
const dy = event.clientY - this.lastY;
this.lastX = event.clientX;
this.lastY = event.clientY;
if (event.shiftKey) {
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 0);
const up = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 1);
const scale = this.radius * 0.0014;
this.target.addScaledVector(right, -dx * scale);
this.target.addScaledVector(up, dy * scale);
this.target.x = THREE.MathUtils.clamp(this.target.x, -2.2, 2.2);
this.target.y = THREE.MathUtils.clamp(this.target.y, -1.3, 2.2);
} else {
this.theta -= dx * 0.006;
this.phi = THREE.MathUtils.clamp(this.phi - dy * 0.005, 0.48, 1.47);
}
});
const stop = () => { this.dragging = false; };
element.addEventListener("pointerup", stop);
element.addEventListener("pointercancel", stop);
element.addEventListener("lostpointercapture", stop);
element.addEventListener("wheel", (event) => {
event.preventDefault();
this.radius = THREE.MathUtils.clamp(
this.radius * Math.exp(event.deltaY * 0.001),
4.7,
11.5
);
}, { passive: false });
this.update();
}
update() {
const sinPhi = Math.sin(this.phi);
this.camera.position.set(
this.target.x + this.radius * sinPhi * Math.sin(this.theta),
this.target.y + this.radius * Math.cos(this.phi),
this.target.z + this.radius * sinPhi * Math.cos(this.theta)
);
this.camera.lookAt(this.target);
}
reset() {
this.target.set(0, 0.25, 0);
this.theta = 0.58;
this.phi = 1.13;
this.radius = 7.7;
this.update();
}
}
const controls = new OrbitCamera(camera, canvas);
$("#resetCamera").addEventListener("click", () => controls.reset());
// -------------------------------------------------------------------------
// Low-poly cylinder and mechanism
// -------------------------------------------------------------------------
const machine = new THREE.Group();
machine.position.x = -0.15;
scene.add(machine);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({ color: 0x0a1423, roughness: 0.96, metalness: 0.02 })
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.82;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(18, 30, 0x34506c, 0x1b2b40);
grid.position.y = -1.805;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(1.72, 1.88, 0.28, 12),
new THREE.MeshStandardMaterial({
color: 0x24354b,
roughness: 0.7,
metalness: 0.35,
flatShading: true
})
);
pedestal.position.y = -1.65;
pedestal.castShadow = true;
pedestal.receiveShadow = true;
machine.add(pedestal);
const base = new THREE.Mesh(
new THREE.CylinderGeometry(1.42, 1.48, 0.16, 16),
new THREE.MeshStandardMaterial({
color: 0x344a62,
roughness: 0.38,
metalness: 0.72,
flatShading: true
})
);
base.position.y = -1.48;
base.castShadow = true;
machine.add(base);
const cylinderMaterial = new THREE.MeshPhysicalMaterial({
color: 0xaed9f5,
transparent: true,
opacity: 0.17,
roughness: 0.16,
metalness: 0,
transmission: 0.05,
side: THREE.DoubleSide,
depthWrite: false
});
const cylinderBody = new THREE.Mesh(
new THREE.CylinderGeometry(1.35, 1.35, 3.05, 12, 1, true),
cylinderMaterial
);
cylinderBody.position.y = 0.025;
machine.add(cylinderBody);
const cylinderEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(
new THREE.CylinderGeometry(1.355, 1.355, 3.05, 12, 1, true)
),
new THREE.LineBasicMaterial({ color: 0x87b9d9, transparent: true, opacity: 0.36 })
);
cylinderEdges.position.y = 0.025;
machine.add(cylinderEdges);
const ringMaterial = new THREE.MeshStandardMaterial({
color: 0x5c7189,
roughness: 0.32,
metalness: 0.78,
flatShading: true
});
[-1.5, 1.55].forEach((y) => {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(1.36, 0.065, 6, 16),
ringMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.y = y;
ring.castShadow = true;
machine.add(ring);
});
// Gas volume: low-poly transparent volume with a faceted wire outline.
const gasBottom = -1.39;
const gasMaterial = new THREE.MeshStandardMaterial({
color: 0x45b7ff,
emissive: 0x173b58,
emissiveIntensity: 0.25,
transparent: true,
opacity: 0.56,
roughness: 0.72,
metalness: 0,
depthWrite: false,
flatShading: true
});
const gasAssembly = new THREE.Group();
gasAssembly.position.y = gasBottom;
machine.add(gasAssembly);
const gasGeometry = new THREE.CylinderGeometry(1.13, 1.13, 1, 12);
const gasMesh = new THREE.Mesh(gasGeometry, gasMaterial);
gasMesh.position.y = 0.5;
gasAssembly.add(gasMesh);
const gasEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x8fdcff,
transparent: true,
opacity: 0.34
});
const gasEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(gasGeometry),
gasEdgeMaterial
);
gasEdges.position.y = 0.5;
gasAssembly.add(gasEdges);
// Small tetrahedral particles make the gas read as a volume rather than a solid block.
const particleGeometry = new THREE.TetrahedronGeometry(0.055, 0);
const particleMaterial = new THREE.MeshStandardMaterial({
color: 0x9edfff,
emissive: 0x183d57,
emissiveIntensity: 0.45,
roughness: 0.5,
flatShading: true
});
const particles = [];
for (let i = 0; i < 30; i++) {
const particle = new THREE.Mesh(particleGeometry, particleMaterial);
const angle = Math.random() * Math.PI * 2;
const radius = 0.18 + Math.sqrt(Math.random()) * 0.82;
particle.userData = {
angle,
radius,
height: 0.08 + Math.random() * 0.84,
speed: 0.18 + Math.random() * 0.38,
phase: Math.random() * Math.PI * 2
};
particles.push(particle);
gasAssembly.add(particle);
}
// Piston, rod, and mechanical position indicator.
const pistonMaterial = new THREE.MeshStandardMaterial({
color: 0xf4b84a,
emissive: 0x4a2800,
emissiveIntensity: 0.28,
roughness: 0.34,
metalness: 0.58,
flatShading: true
});
const piston = new THREE.Mesh(
new THREE.CylinderGeometry(1.18, 1.18, 0.18, 16),
pistonMaterial
);
piston.castShadow = true;
machine.add(piston);
const pistonBand = new THREE.Mesh(
new THREE.TorusGeometry(1.14, 0.055, 6, 16),
new THREE.MeshStandardMaterial({
color: 0xffd887,
roughness: 0.3,
metalness: 0.65,
flatShading: true
})
);
pistonBand.rotation.x = Math.PI / 2;
machine.add(pistonBand);
const rod = new THREE.Mesh(
new THREE.CylinderGeometry(0.095, 0.095, 2.05, 10),
new THREE.MeshStandardMaterial({
color: 0xcbd7e2,
roughness: 0.25,
metalness: 0.85,
flatShading: true
})
);
rod.castShadow = true;
machine.add(rod);
// A fixed ruler emphasizes that piston position is a mechanical coordinate.
const rulerMaterial = new THREE.MeshStandardMaterial({
color: 0x71869c,
roughness: 0.5,
metalness: 0.55
});
const ruler = new THREE.Mesh(
new THREE.BoxGeometry(0.045, 2.72, 0.045),
rulerMaterial
);
ruler.position.set(1.62, -0.08, 0.02);
machine.add(ruler);
for (let i = 0; i <= 8; i++) {
const length = i % 2 === 0 ? 0.28 : 0.16;
const tick = new THREE.Mesh(
new THREE.BoxGeometry(length, 0.025, 0.025),
rulerMaterial
);
tick.position.set(1.62 - length / 2, -1.39 + i * 0.34, 0.02);
machine.add(tick);
}
const positionIndicator = new THREE.Mesh(
new THREE.BoxGeometry(1, 0.035, 0.035),
new THREE.MeshBasicMaterial({ color: 0xf4b84a })
);
machine.add(positionIndicator);
// Heat arrows.
function createHeatArrow(color) {
const group = new THREE.Group();
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.58,
transparent: true,
opacity: 0.1,
roughness: 0.45,
metalness: 0.08,
flatShading: true
});
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 1.0, 8),
material
);
shaft.position.y = 0.5;
group.add(shaft);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.17, 0.34, 8),
material
);
head.position.y = 1.17;
group.add(head);
group.userData.material = material;
return group;
}
const heatInArrow = createHeatArrow(0xff874d);
heatInArrow.position.set(-1.85, 1.05, 0.2);
heatInArrow.rotation.z = Math.PI;
machine.add(heatInArrow);
const heatOutArrow = createHeatArrow(0x4eb7ff);
heatOutArrow.position.set(1.88, -0.75, 0.2);
machine.add(heatOutArrow);
// -------------------------------------------------------------------------
// Thermodynamic model
// -------------------------------------------------------------------------
const gamma = 1.4;
const V_MIN = 1.0;
const V_MAX = 1.8;
const stateA = { V: 1.0, T: 300 * Math.pow(1.25, gamma - 1), P: 0 };
stateA.P = stateA.T / stateA.V;
const stateB = { V: 1.8, T: 900, P: 500 };
const stateC = {
V: 1.25,
T: 900 * Math.pow(1.8 / 1.25, gamma - 1),
P: 0
};
stateC.P = stateC.T / stateC.V;
const stateD = { V: 1.25, T: 300, P: 240 };
const polytropicN = Math.log(stateB.P / stateA.P) / Math.log(stateB.V / stateA.V);
const phaseInfo = [
{
letter: "A",
name: "A → B · Polytropic expansion",
detail: "Gas expands while external heat raises its temperature.",
heatIn: true
},
{
letter: "B",
name: "B → C · Adiabatic compression",
detail: "The piston moves down; compression raises the gas temperature.",
heatIn: false
},
{
letter: "C",
name: "C → D · Isochoric cooling",
detail: "The piston is fixed while heat leaves the gas.",
heatIn: false
},
{
letter: "D",
name: "D → A · Adiabatic expansion",
detail: "The piston rises and the gas returns toward state A.",
heatIn: false
}
];
function stateAt(progress) {
const scaled = Math.min(progress, 0.999999) * 4;
const stage = Math.min(3, Math.floor(scaled));
const u = scaled - stage;
let V, T, P;
if (stage === 0) {
V = THREE.MathUtils.lerp(stateA.V, stateB.V, u);
P = stateA.P * Math.pow(stateA.V / V, polytropicN);
T = P * V;
} else if (stage === 1) {
V = THREE.MathUtils.lerp(stateB.V, stateC.V, u);
T = stateB.T * Math.pow(stateB.V / V, gamma - 1);
P = T / V;
} else if (stage === 2) {
V = stateC.V;
T = THREE.MathUtils.lerp(stateC.T, stateD.T, u);
P = T / V;
} else {
V = THREE.MathUtils.lerp(stateD.V, stateA.V, u);
T = stateD.T * Math.pow(stateD.V / V, gamma - 1);
P = T / V;
}
return { V, T, P, stage, u };
}
const coldColor = new THREE.Color(0x45b7ff);
const middleColor = new THREE.Color(0xf0d35b);
const hotColor = new THREE.Color(0xff654d);
function temperatureColor(T) {
const t = THREE.MathUtils.clamp((T - 270) / (950 - 270), 0, 1);
if (t < 0.5) {
return new THREE.Color().lerpColors(coldColor, middleColor, t * 2);
}
return new THREE.Color().lerpColors(middleColor, hotColor, (t - 0.5) * 2);
}
// -------------------------------------------------------------------------
// Pressure-volume chart
// -------------------------------------------------------------------------
const SVG_NS = "http://www.w3.org/2000/svg";
const chart = $("#pvChart");
const chartGrid = $("#chartGrid");
const plot = { left: 58, right: 500, top: 24, bottom: 292, vMin: 0.8, vMax: 2.0, pMax: 1000 };
function chartX(V) {
return plot.left + ((V - plot.vMin) / (plot.vMax - plot.vMin)) * (plot.right - plot.left);
}
function chartY(P) {
return plot.bottom - (P / plot.pMax) * (plot.bottom - plot.top);
}
function svgElement(name, attributes = {}) {
const element = document.createElementNS(SVG_NS, name);
Object.entries(attributes).forEach(([key, value]) => element.setAttribute(key, value));
return element;
}
[0, 250, 500, 750, 1000].forEach((P) => {
const y = chartY(P);
chartGrid.appendChild(svgElement("line", {
x1: plot.left, x2: plot.right, y1: y, y2: y, class: "grid"
}));
const text = svgElement("text", {
x: plot.left - 10, y: y + 3, "text-anchor": "end"
});
text.textContent = P;
chartGrid.appendChild(text);
});
[0.8, 1.2, 1.6, 2.0].forEach((V) => {
const x = chartX(V);
chartGrid.appendChild(svgElement("line", {
x1: x, x2: x, y1: plot.top, y2: plot.bottom, class: "grid"
}));
const text = svgElement("text", {
x, y: plot.bottom + 18, "text-anchor": "middle"
});
text.textContent = V.toFixed(1);
chartGrid.appendChild(text);
});
chartGrid.appendChild(svgElement("line", {
x1: plot.left, x2: plot.left, y1: plot.top, y2: plot.bottom, class: "axis"
}));
chartGrid.appendChild(svgElement("line", {
x1: plot.left, x2: plot.right, y1: plot.bottom, y2: plot.bottom, class: "axis"
}));
function pathFromPoints(points) {
return points.map((point, i) => {
const command = i === 0 ? "M" : "L";
return `${command}${chartX(point.V).toFixed(2)},${chartY(point.P).toFixed(2)}`;
}).join(" ");
}
const segmentPoints = [[], [], [], []];
for (let i = 0; i <= 60; i++) {
const u = i / 60;
const V = THREE.MathUtils.lerp(stateA.V, stateB.V, u);
const P = stateA.P * Math.pow(stateA.V / V, polytropicN);
segmentPoints[0].push({ V, P });
}
for (let i = 0; i <= 60; i++) {
const u = i / 60;
const V = THREE.MathUtils.lerp(stateB.V, stateC.V, u);
const T = stateB.T * Math.pow(stateB.V / V, gamma - 1);
segmentPoints[1].push({ V, P: T / V });
}
for (let i = 0; i <= 30; i++) {
const u = i / 30;
const T = THREE.MathUtils.lerp(stateC.T, stateD.T, u);
segmentPoints[2].push({ V: stateC.V, P: T / stateC.V });
}
for (let i = 0; i <= 60; i++) {
const u = i / 60;
const V = THREE.MathUtils.lerp(stateD.V, stateA.V, u);
const T = stateD.T * Math.pow(stateD.V / V, gamma - 1);
segmentPoints[3].push({ V, P: T / V });
}
segmentPoints.forEach((points, index) => {
$(`#seg${index}`).setAttribute("d", pathFromPoints(points));
});
const stateDots = $("#stateDots");
[stateA, stateB, stateC, stateD].forEach((state, index) => {
const x = chartX(state.V);
const y = chartY(state.P);
const circle = svgElement("circle", {
cx: x, cy: y, r: 5, class: "state-dot"
});
stateDots.appendChild(circle);
const label = svgElement("text", {
x: x + (index === 1 ? 9 : index === 3 ? -9 : 0),
y: y + (index === 0 ? 17 : index === 2 ? -10 : -9),
"text-anchor": index === 1 ? "start" : index === 3 ? "end" : "middle",
class: "state-letter"
});
label.textContent = "ABCD"[index];
stateDots.appendChild(label);
});
// -------------------------------------------------------------------------
// UI and animation state
// -------------------------------------------------------------------------
let progress = 0;
let playing = true;
let playbackSpeed = 1;
const speeds = [0.5, 1, 2];
const cycleSlider = $("#cycleSlider");
function syncPlayButton() {
$("#playButton").textContent = playing ? "Pause" : "Play";
$("#playButton").setAttribute("aria-pressed", String(!playing));
}
$("#playButton").addEventListener("click", () => {
playing = !playing;
syncPlayButton();
});
$("#resetCycle").addEventListener("click", () => {
progress = 0;
playing = true;
syncPlayButton();
});
$("#speedButton").addEventListener("click", () => {
const next = (speeds.indexOf(playbackSpeed) + 1) % speeds.length;
playbackSpeed = speeds[next];
$("#speedButton").textContent = `${playbackSpeed}×`;
});
cycleSlider.addEventListener("input", () => {
progress = Number(cycleSlider.value);
playing = false;
syncPlayButton();
});
document.addEventListener("keydown", (event) => {
if (event.code === "Space" && !event.target.matches("input, button")) {
event.preventDefault();
playing = !playing;
syncPlayButton();
}
});
// Project a world position to an HTML overlay label.
const projected = new THREE.Vector3();
function placeLabel(element, world, offsetX, offsetY) {
projected.copy(world).project(camera);
const width = sceneWrap.clientWidth;
const height = sceneWrap.clientHeight;
const x = (projected.x * 0.5 + 0.5) * width + offsetX;
const y = (-projected.y * 0.5 + 0.5) * height + offsetY;
element.style.left = `${THREE.MathUtils.clamp(x, 78, width - 78)}px`;
element.style.top = `${THREE.MathUtils.clamp(y, 34, height - 40)}px`;
}
const pistonWorld = new THREE.Vector3();
const temperatureWorld = new THREE.Vector3();
const heatInWorld = new THREE.Vector3();
const heatOutWorld = new THREE.Vector3();
function updateModel(dt) {
const state = stateAt(progress);
const stroke = THREE.MathUtils.clamp((state.V - V_MIN) / (V_MAX - V_MIN), 0, 1);
const gasHeight = 0.86 + (state.V - V_MIN) * 1.03;
const pistonY = gasBottom + gasHeight;
const color = temperatureColor(state.T);
gasAssembly.scale.y = gasHeight;
gasMaterial.color.copy(color);
gasMaterial.emissive.copy(color).multiplyScalar(0.18);
gasEdgeMaterial.color.copy(color).lerp(new THREE.Color(0xffffff), 0.34);
particleMaterial.color.copy(color).lerp(new THREE.Color(0xffffff), 0.42);
particleMaterial.emissive.copy(color).multiplyScalar(0.2);
piston.position.y = pistonY;
pistonBand.position.y = pistonY;
rod.position.y = pistonY + 1.02;
const indicatorLength = 1.62 - 1.18;
positionIndicator.scale.x = indicatorLength;
positionIndicator.position.set(1.18 + indicatorLength / 2, pistonY, 0.02);
const speedFactor = 0.45 + THREE.MathUtils.clamp((state.T - 270) / 700, 0, 1);
particles.forEach((particle) => {
const data = particle.userData;
data.angle += dt * data.speed * speedFactor;
const breathing = 1 + 0.08 * Math.sin(performance.now() * 0.001 + data.phase);
const radius = data.radius * breathing;
particle.position.set(
Math.cos(data.angle) * radius,
data.height,
Math.sin(data.angle) * radius
);
particle.rotation.x += dt * 0.8;
particle.rotation.y += dt * 1.1;
});
// Heat is shown only during the idealized heat-addition and heat-rejection legs.
let heatIn = 0;
let heatOut = 0;
if (state.stage === 0) {
heatIn = 0.28 + 0.72 * Math.sin(Math.PI * state.u);
} else if (state.stage === 2) {
heatOut = 0.28 + 0.72 * Math.sin(Math.PI * state.u);
}
heatInArrow.userData.material.opacity = 0.07 + heatIn * 0.9;
heatOutArrow.userData.material.opacity = 0.07 + heatOut * 0.9;
heatInArrow.scale.y = 0.82 + heatIn * 0.24;
heatOutArrow.scale.y = 0.82 + heatOut * 0.24;
$("#heatInTag").style.opacity = String(0.15 + heatIn * 0.85);
$("#heatOutTag").style.opacity = String(0.15 + heatOut * 0.85);
// Readouts.
document.documentElement.style.setProperty("--temp-color", color.getStyle());
$("#positionValue").textContent = Math.round(stroke * 100);
$("#temperatureValue").textContent = Math.round(state.T);
$("#pressureValue").textContent = (state.P / 100).toFixed(2);
$("#volumeValue").textContent = state.V.toFixed(2);
$("#positionMeter").style.width = `${stroke * 100}%`;
$("#temperatureMeter").style.width = `${THREE.MathUtils.clamp((state.T - 250) / 900, 0, 1) * 100}%`;
$("#pistonTagValue").textContent = `${Math.round(stroke * 100)}% stroke`;
$("#tempTagValue").textContent = `${Math.round(state.T)} K`;
// Current chart marker and active process.
const cx = chartX(state.V);
const cy = chartY(state.P);
$("#currentDot").setAttribute("cx", cx);
$("#currentDot").setAttribute("cy", cy);
$("#crossX").setAttribute("x1", cx);
$("#crossX").setAttribute("x2", cx);
$("#crossY").setAttribute("y1", cy);
$("#crossY").setAttribute("y2", cy);
document.querySelectorAll(".cycle-path").forEach((path, index) => {
path.classList.toggle("active", index === state.stage);
});
const phase = phaseInfo[state.stage];
$("#phaseLetter").textContent = phase.letter;
$("#phaseName").textContent = phase.name;
$("#phaseDetail").textContent = phase.detail;
document.querySelectorAll(".process-step").forEach((step, index) => {
step.classList.toggle("active", index === state.stage);
});
cycleSlider.value = progress;
cycleSlider.style.setProperty("--progress", `${progress * 100}%`);
$("#progressText").textContent = `${Math.round(progress * 100)}%`;
// Position the overlay labels after the camera and mechanism have moved.
pistonWorld.set(1.0, pistonY, 0.12);
temperatureWorld.set(-0.85, gasBottom + gasHeight * 0.46, 0.58);
heatInWorld.set(-1.85, 0.95, 0.2);
heatOutWorld.set(1.88, -0.62, 0.2);
placeLabel($("#pistonTag"), pistonWorld, 90, -48);
placeLabel($("#tempTag"), temperatureWorld, -82, -30);
placeLabel($("#heatInTag"), heatInWorld, -28, -35);
placeLabel($("#heatOutTag"), heatOutWorld, 30, 38);
}
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
function resize() {
const width = sceneWrap.clientWidth;
const height = sceneWrap.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / Math.max(height, 1);
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(sceneWrap);
resize();
let previousTime = performance.now();
let firstFrame = true;
function animate(now) {
requestAnimationFrame(animate);
const dt = THREE.MathUtils.clamp((now - previousTime) / 1000, 0, 0.05);
previousTime = now;
if (playing) {
progress = (progress + dt * playbackSpeed / 10) % 1;
}
controls.update();
updateModel(dt);
renderer.render(scene, camera);
if (firstFrame) {
$("#loading").style.display = "none";
firstFrame = false;
}
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```