Prompt specification
Build or customize the Three.js 3D scene "Battery Reservoir With Separate Energy and Power Scales" 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: Battery Reservoir With Separate Energy and Power Scales
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/battery-reservoir-with-separate-energy-and-power-scale-x3933
Scene Description Prompt:
Please make a cutaway Three.js scene in one standalone HTML file using cdnjs. Model a symbolic battery reservoir with an energy level and a separate power-flow arrow. Use units to distinguish capacity from instantaneous charging rate.
Working Three.js Starter (battery-reservoir-with-separate-energy-and-power-scale-x3933):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Three.js Cutaway Battery Reservoir</title>
<style>
:root {
color-scheme: dark;
--bg: #07111e;
--panel: rgba(10, 23, 39, 0.82);
--border: rgba(146, 190, 220, 0.18);
--text: #edf7ff;
--muted: #91a9bc;
--energy: #51f2d0;
--energy-soft: rgba(81, 242, 208, 0.14);
--power: #ff9d42;
--power-soft: rgba(255, 157, 66, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.title-block {
position: absolute;
top: 26px;
left: 28px;
max-width: 430px;
}
.eyebrow {
color: var(--energy);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 8px 0 9px;
font-size: clamp(25px, 3vw, 39px);
line-height: 1.05;
letter-spacing: -0.04em;
font-weight: 680;
}
h1 span {
color: var(--muted);
font-weight: 450;
}
.subtitle {
margin: 0;
max-width: 390px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.control-panel {
position: absolute;
left: 28px;
bottom: 28px;
width: min(340px, calc(100vw - 56px));
padding: 18px;
border: 1px solid var(--border);
border-radius: 16px;
background:
linear-gradient(135deg, rgba(19, 41, 62, 0.82), rgba(7, 18, 32, 0.88));
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px);
pointer-events: auto;
}
.control-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 17px;
}
.control-heading strong {
font-size: 12px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.control-heading small {
color: var(--muted);
font-size: 11px;
}
.control-group {
padding: 14px 0;
border-top: 1px solid var(--border);
}
.control-group:first-of-type {
padding-top: 0;
border-top: 0;
}
.control-row {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 16px;
margin-bottom: 10px;
}
.control-name {
font-size: 13px;
font-weight: 650;
}
.readout {
font-size: 20px;
font-weight: 720;
font-variant-numeric: tabular-nums;
}
.readout.energy {
color: var(--energy);
}
.readout.power {
color: var(--power);
}
.range-wrap {
position: relative;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 6px 0;
border-radius: 999px;
appearance: none;
background: rgba(255, 255, 255, 0.12);
outline: none;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid #07111e;
border-radius: 50%;
appearance: none;
background: var(--energy);
box-shadow: 0 0 0 1px rgba(81, 242, 208, 0.7);
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid #07111e;
border-radius: 50%;
background: var(--energy);
}
#rate::-webkit-slider-thumb {
background: var(--power);
box-shadow: 0 0 0 1px rgba(255, 157, 66, 0.7);
}
#rate::-moz-range-thumb {
background: var(--power);
}
.range-limits {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: var(--muted);
font-size: 10px;
}
.equation {
margin-top: 4px;
padding: 11px 12px;
border-radius: 10px;
background: var(--power-soft);
color: #ffd4aa;
font-size: 12px;
line-height: 1.45;
}
.equation strong {
color: #fff0df;
}
.legend {
position: absolute;
right: 28px;
bottom: 28px;
width: 280px;
padding: 16px 18px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
backdrop-filter: blur(14px);
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.legend-row {
display: flex;
align-items: center;
gap: 10px;
margin: 8px 0;
color: var(--text);
font-size: 12px;
}
.swatch {
width: 26px;
height: 9px;
border-radius: 4px;
background: var(--energy);
box-shadow: 0 0 14px rgba(81, 242, 208, 0.55);
}
.arrow-swatch {
position: relative;
width: 27px;
height: 4px;
margin-left: 3px;
border-radius: 4px;
background: var(--power);
box-shadow: 0 0 12px rgba(255, 157, 66, 0.5);
}
.arrow-swatch::after {
content: "";
position: absolute;
top: -4px;
right: -1px;
border-top: 6px solid transparent;
border-bottom: 6px solid transparent;
border-right: 9px solid var(--power);
}
.formula {
margin-top: 12px;
padding-top: 12px;
border-top: 1px solid var(--border);
}
.formula b {
color: var(--text);
font-weight: 650;
}
.camera-help {
position: absolute;
right: 28px;
top: 28px;
color: rgba(190, 211, 226, 0.66);
font-size: 11px;
text-align: right;
line-height: 1.7;
}
#world-labels {
position: fixed;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
transform: translate(-50%, -50%);
white-space: nowrap;
text-align: center;
filter: drop-shadow(0 5px 15px rgba(0, 0, 0, 0.45));
will-change: left, top;
}
.label-card {
padding: 9px 12px;
border: 1px solid var(--border);
border-radius: 10px;
background: rgba(7, 17, 30, 0.83);
backdrop-filter: blur(9px);
}
.energy-label .label-card {
border-color: rgba(81, 242, 208, 0.3);
}
.power-label .label-card {
border-color: rgba(255, 157, 66, 0.35);
}
.label-kicker {
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.label-value {
margin-top: 2px;
font-size: 22px;
font-weight: 760;
font-variant-numeric: tabular-nums;
}
.energy-label .label-value {
color: var(--energy);
}
.power-label .label-value {
color: var(--power);
}
.label-sub {
margin-top: 1px;
color: var(--muted);
font-size: 9px;
}
.tick-label {
color: rgba(190, 211, 226, 0.74);
font-size: 9px;
font-variant-numeric: tabular-nums;
}
@media (max-width: 760px) {
.title-block {
top: 18px;
left: 18px;
max-width: calc(100vw - 36px);
}
.subtitle,
.camera-help,
.legend {
display: none;
}
.control-panel {
left: 18px;
bottom: 18px;
width: calc(100vw - 36px);
padding: 14px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="hud">
<header class="title-block">
<div class="eyebrow">Symbolic battery reservoir</div>
<h1>Stored energy <span>≠</span> power flow</h1>
<p class="subtitle">
A cutaway view of a battery reservoir. The teal level measures stored
energy in kWh; the orange arrow shows instantaneous charging power in kW.
</p>
</header>
<section class="control-panel" aria-label="Reservoir controls">
<div class="control-heading">
<strong>Reservoir state</strong>
<small>Independent quantities</small>
</div>
<div class="control-group">
<div class="control-row">
<label class="control-name" for="energy">Stored energy</label>
<output id="energy-output" class="readout energy" for="energy">64 kWh</output>
</div>
<div class="range-wrap">
<input id="energy" type="range" min="0" max="80" step="1" value="64" />
<div class="range-limits">
<span>0 kWh</span>
<span>Maximum capacity: 80 kWh</span>
</div>
</div>
</div>
<div class="control-group">
<div class="control-row">
<label class="control-name" for="rate">Charging rate</label>
<output id="rate-output" class="readout power" for="rate">18 kW</output>
</div>
<div class="range-wrap">
<input id="rate" type="range" min="0" max="40" step="1" value="18" />
<div class="range-limits">
<span>0 kW</span>
<span>40 kW</span>
</div>
</div>
<div class="equation" aria-live="polite">
<strong id="rate-equation">18 kW × 1 h = 18 kWh</strong><br />
Energy that would be added during one hour at this rate.
</div>
</div>
</section>
<aside class="legend">
<div class="legend-row">
<span class="swatch"></span>
<span>Stored energy / capacity — kWh</span>
</div>
<div class="legend-row">
<span class="arrow-swatch"></span>
<span>Instantaneous charging rate — kW</span>
</div>
<div class="formula">
<b>Capacity:</b> amount of energy, measured in kWh.<br />
<b>Rate:</b> energy transferred per unit time, measured in kW.
</div>
</aside>
<div class="camera-help">
Drag to orbit · Wheel to zoom<br />
Shift-drag to pan · Double-click to reset
</div>
</div>
<div id="world-labels">
<div id="energy-label" class="world-label energy-label">
<div class="label-card">
<div class="label-kicker">Stored energy</div>
<div id="energy-label-value" class="label-value">64 kWh</div>
<div id="energy-label-sub" class="label-sub">80% of 80 kWh capacity</div>
</div>
</div>
<div id="power-label" class="world-label power-label">
<div class="label-card">
<div class="label-kicker">Charging power</div>
<div id="power-label-value" class="label-value">18 kW</div>
<div class="label-sub">instantaneous rate</div>
</div>
</div>
<div id="tick-0" class="world-label tick-label">0 kWh</div>
<div id="tick-20" class="world-label tick-label">20 kWh</div>
<div id="tick-40" class="world-label tick-label">40 kWh</div>
<div id="tick-60" class="world-label tick-label">60 kWh</div>
<div id="tick-80" class="world-label tick-label">80 kWh</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const sceneHost = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.1;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111e);
scene.fog = new THREE.FogExp2(0x07111e, 0.038);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.08, 0);
const targetGoal = cameraTarget.clone();
let radiusGoal = 8.4;
let thetaGoal = 0.72;
let phiGoal = 1.23;
let radius = radiusGoal;
let theta = thetaGoal;
let phi = phiGoal;
function updateCamera() {
const offset = new THREE.Vector3().setFromSpherical(
new THREE.Spherical(radius, phi, theta)
);
camera.position.copy(cameraTarget).add(offset);
camera.lookAt(cameraTarget);
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting and environment
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xa9dfff, 0x09121d, 1.55));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(4.5, 7, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 18;
keyLight.shadow.camera.left = -5;
keyLight.shadow.camera.right = 5;
keyLight.shadow.camera.top = 5;
keyLight.shadow.camera.bottom = -5;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x39d9ff, 24, 12, 2);
rimLight.position.set(-3.2, 2.3, -3);
scene.add(rimLight);
const energyLight = new THREE.PointLight(0x51f2d0, 12, 7, 2);
energyLight.position.set(0, -0.1, 1.1);
scene.add(energyLight);
const powerLight = new THREE.PointLight(0xff9d42, 9, 6, 2);
powerLight.position.set(2.8, 0.7, 1.1);
scene.add(powerLight);
// -------------------------------------------------------------------------
// Floor and pedestal
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.CircleGeometry(12, 96),
new THREE.MeshStandardMaterial({
color: 0x081522,
roughness: 0.92,
metalness: 0.05
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.68;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(20, 40, 0x23475d, 0x102b3d);
grid.position.y = -1.665;
grid.material.transparent = true;
grid.material.opacity = 0.26;
scene.add(grid);
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(1.78, 1.9, 0.22, 96),
new THREE.MeshStandardMaterial({
color: 0x12283a,
roughness: 0.48,
metalness: 0.65
})
);
pedestal.position.y = -1.56;
pedestal.castShadow = true;
pedestal.receiveShadow = true;
scene.add(pedestal);
const pedestalTrim = new THREE.Mesh(
new THREE.TorusGeometry(1.79, 0.035, 10, 96),
new THREE.MeshStandardMaterial({
color: 0x51f2d0,
emissive: 0x1d8d79,
emissiveIntensity: 0.8,
roughness: 0.3,
metalness: 0.65
})
);
pedestalTrim.rotation.x = Math.PI / 2;
pedestalTrim.position.y = -1.445;
scene.add(pedestalTrim);
const halo = new THREE.Mesh(
new THREE.RingGeometry(1.91, 2.28, 96),
new THREE.MeshBasicMaterial({
color: 0x1e7181,
transparent: true,
opacity: 0.13,
side: THREE.DoubleSide,
depthWrite: false
})
);
halo.rotation.x = -Math.PI / 2;
halo.position.y = -1.665;
scene.add(halo);
// -------------------------------------------------------------------------
// Cutaway battery reservoir
//
// The front half (positive Z side) is removed. The retained half is
// generated with CylinderGeometry thetaStart = PI / 2, thetaLength = PI.
// -------------------------------------------------------------------------
const tankRadius = 1.28;
const innerRadius = 1.15;
const tankHeight = 2.9;
const tankBottom = -1.34;
const tankTop = tankBottom + tankHeight;
const usableHeight = tankHeight - 0.16;
const shellMaterial = new THREE.MeshStandardMaterial({
color: 0x6ebad0,
emissive: 0x123a48,
emissiveIntensity: 0.35,
transparent: true,
opacity: 0.18,
roughness: 0.28,
metalness: 0.25,
side: THREE.DoubleSide,
depthWrite: false
});
const rearShell = new THREE.Mesh(
new THREE.CylinderGeometry(
tankRadius,
tankRadius,
tankHeight,
96,
1,
true,
Math.PI / 2,
Math.PI
),
shellMaterial
);
rearShell.position.y = (tankBottom + tankTop) / 2;
rearShell.receiveShadow = true;
scene.add(rearShell);
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0xb7d4df,
emissive: 0x193c4a,
emissiveIntensity: 0.5,
roughness: 0.26,
metalness: 0.82
});
for (const y of [tankBottom, tankTop]) {
const rim = new THREE.Mesh(
new THREE.TorusGeometry(tankRadius, 0.055, 14, 96),
rimMaterial
);
rim.rotation.x = Math.PI / 2;
rim.position.y = y;
rim.castShadow = true;
scene.add(rim);
}
// Cut edges along the exposed section.
const edgeGeometry = new THREE.BoxGeometry(0.065, tankHeight, 0.065);
for (const x of [-tankRadius, tankRadius]) {
const edge = new THREE.Mesh(edgeGeometry, rimMaterial);
edge.position.set(x, (tankBottom + tankTop) / 2, 0);
edge.castShadow = true;
scene.add(edge);
}
// Rear reinforcing rib.
const rearRib = new THREE.Mesh(
new THREE.BoxGeometry(0.06, tankHeight, 0.06),
rimMaterial
);
rearRib.position.set(0, (tankBottom + tankTop) / 2, -tankRadius);
scene.add(rearRib);
// Small top and bottom feet.
for (const y of [tankBottom - 0.04, tankTop + 0.04]) {
const foot = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.2, 0.09, 24),
rimMaterial
);
foot.position.y = y;
foot.castShadow = true;
scene.add(foot);
}
// -------------------------------------------------------------------------
// Energy fill
// -------------------------------------------------------------------------
const energyMaterial = new THREE.MeshStandardMaterial({
color: 0x34d8c0,
emissive: 0x0d947f,
emissiveIntensity: 0.48,
transparent: true,
opacity: 0.72,
roughness: 0.24,
metalness: 0.08,
side: THREE.DoubleSide,
depthWrite: false
});
const energyMesh = new THREE.Mesh(
new THREE.CylinderGeometry(
innerRadius,
innerRadius,
1,
96,
1,
false,
Math.PI / 2,
Math.PI
),
energyMaterial
);
scene.add(energyMesh);
// The flat rectangular face exposed by the cutaway.
const energyFaceMaterial = new THREE.MeshStandardMaterial({
color: 0x4be7cf,
emissive: 0x138c79,
emissiveIntensity: 0.4,
transparent: true,
opacity: 0.38,
roughness: 0.3,
metalness: 0.03,
depthWrite: false
});
const energyFace = new THREE.Mesh(
new THREE.BoxGeometry(innerRadius * 2, 1, 0.025),
energyFaceMaterial
);
energyFace.position.z = 0.018;
scene.add(energyFace);
const surfaceMaterial = new THREE.MeshStandardMaterial({
color: 0xc4fff5,
emissive: 0x51f2d0,
emissiveIntensity: 1.8,
transparent: true,
opacity: 0.92,
roughness: 0.18
});
const energySurface = new THREE.Mesh(
new THREE.BoxGeometry(innerRadius * 2, 0.035, 0.045),
surfaceMaterial
);
energySurface.position.z = 0.025;
scene.add(energySurface);
// Capacity ticks on the exposed edge.
const tickMaterial = new THREE.LineBasicMaterial({
color: 0x8eb4c8,
transparent: true,
opacity: 0.7
});
const tickYValues = [0, 20, 40, 60, 80];
for (const value of tickYValues) {
const y = tankBottom + (value / 80) * usableHeight;
const points = [
new THREE.Vector3(-tankRadius - 0.05, y, 0),
new THREE.Vector3(-tankRadius - 0.19, y, 0)
];
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
tickMaterial
);
scene.add(line);
}
// -------------------------------------------------------------------------
// Battery terminals
// -------------------------------------------------------------------------
function createTextSprite(text, color) {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, 128, 128);
context.font = "bold 92px sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.shadowColor = color;
context.shadowBlur = 18;
context.fillStyle = color;
context.fillText(text, 64, 66);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(0.3, 0.3, 0.3);
sprite.renderOrder = 10;
return sprite;
}
const terminalMaterial = new THREE.MeshStandardMaterial({
color: 0xb9cbd4,
roughness: 0.25,
metalness: 0.88
});
const positiveMaterial = new THREE.MeshStandardMaterial({
color: 0xff785f,
emissive: 0x7d170f,
emissiveIntensity: 0.7,
roughness: 0.25,
metalness: 0.45
});
const negativeMaterial = new THREE.MeshStandardMaterial({
color: 0x55aeea,
emissive: 0x123b61,
emissiveIntensity: 0.65,
roughness: 0.25,
metalness: 0.45
});
for (const [x, symbol, material] of [
[-0.48, "+", positiveMaterial],
[0.48, "−", negativeMaterial]
]) {
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.19, 0.25, 32),
material
);
post.position.set(x, tankTop + 0.13, 0);
post.castShadow = true;
scene.add(post);
const cap = new THREE.Mesh(
new THREE.CylinderGeometry(0.18, 0.18, 0.035, 32),
terminalMaterial
);
cap.position.set(x, tankTop + 0.27, 0);
scene.add(cap);
const sign = createTextSprite(symbol, symbol === "+" ? "#ff785f" : "#55aeea");
sign.position.set(x, tankTop + 0.52, 0);
scene.add(sign);
}
// -------------------------------------------------------------------------
// Separate charging-power arrow
// -------------------------------------------------------------------------
const arrowMaterial = new THREE.MeshStandardMaterial({
color: 0xff9d42,
emissive: 0xb54b08,
emissiveIntensity: 1.25,
roughness: 0.28,
metalness: 0.28,
transparent: true,
opacity: 1
});
const powerArrow = new THREE.Group();
powerArrow.position.z = 0.34;
scene.add(powerArrow);
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 1.48, 24),
arrowMaterial
);
shaft.rotation.z = Math.PI / 2;
shaft.position.x = 2.74;
powerArrow.add(shaft);
const arrowHead = new THREE.Mesh(
new THREE.ConeGeometry(0.17, 0.42, 32),
arrowMaterial
);
arrowHead.rotation.z = Math.PI / 2;
arrowHead.position.x = 1.82;
powerArrow.add(arrowHead);
const packetMaterial = new THREE.MeshBasicMaterial({
color: 0xffd4a2,
transparent: true,
opacity: 0.95
});
const powerPackets = [];
const packetCount = 8;
for (let i = 0; i < packetCount; i++) {
const packet = new THREE.Mesh(
new THREE.SphereGeometry(0.065, 12, 12),
packetMaterial
);
packet.position.y = 0.4;
packet.userData.phase = i / packetCount;
powerArrow.add(packet);
powerPackets.push(packet);
}
// -------------------------------------------------------------------------
// Energy particles inside the reservoir
// -------------------------------------------------------------------------
const particleCount = 90;
const particlePositions = new Float32Array(particleCount * 3);
for (let i = 0; i < particleCount; i++) {
const theta = Math.PI / 2 + Math.random() * Math.PI;
const radius = Math.sqrt(Math.random()) * innerRadius * 0.87;
const x = Math.sin(theta) * radius;
const z = Math.cos(theta) * radius;
const y = tankBottom + Math.random() * usableHeight;
particlePositions[i * 3] = x;
particlePositions[i * 3 + 1] = y;
particlePositions[i * 3 + 2] = z;
}
const particleGeometry = new THREE.BufferGeometry();
particleGeometry.setAttribute(
"position",
new THREE.BufferAttribute(particlePositions, 3)
);
const particles = new THREE.Points(
particleGeometry,
new THREE.PointsMaterial({
color: 0xc5fff5,
size: 0.045,
transparent: true,
opacity: 0.75,
blending: THREE.AdditiveBlending,
depthWrite: false
})
);
scene.add(particles);
// -------------------------------------------------------------------------
// UI and world-space labels
// -------------------------------------------------------------------------
const capacity = 80;
let storedEnergy = 64;
let chargingRate = 18;
let currentFillHeight = 0;
const energyInput = document.querySelector("#energy");
const rateInput = document.querySelector("#rate");
const energyOutput = document.querySelector("#energy-output");
const rateOutput = document.querySelector("#rate-output");
const rateEquation = document.querySelector("#rate-equation");
const energyLabelValue = document.querySelector("#energy-label-value");
const energyLabelSub = document.querySelector("#energy-label-sub");
const powerLabelValue = document.querySelector("#power-label-value");
function updateEnergy() {
const fraction = storedEnergy / capacity;
currentFillHeight = usableHeight * fraction;
energyMesh.visible = currentFillHeight > 0.001;
energyFace.visible = currentFillHeight > 0.001;
energySurface.visible = currentFillHeight > 0.001;
energyMesh.scale.y = Math.max(currentFillHeight, 0.001);
energyMesh.position.y = tankBottom + currentFillHeight / 2;
energyFace.scale.y = Math.max(currentFillHeight, 0.001);
energyFace.position.y = tankBottom + currentFillHeight / 2;
energySurface.position.y = tankBottom + currentFillHeight;
energyMaterial.emissiveIntensity = 0.35 + fraction * 0.25;
energyLight.intensity = 7 + fraction * 8;
const percent = Math.round(fraction * 100);
energyOutput.textContent = `${storedEnergy} kWh`;
energyLabelValue.textContent = `${storedEnergy} kWh`;
energyLabelSub.textContent = `${percent}% of ${capacity} kWh capacity`;
}
function updateRate() {
rateOutput.textContent = `${chargingRate} kW`;
powerLabelValue.textContent = `${chargingRate} kW`;
rateEquation.textContent = `${chargingRate} kW × 1 h = ${chargingRate} kWh`;
const normalized = chargingRate / 40;
arrowMaterial.opacity = chargingRate === 0 ? 0.22 : 0.55 + normalized * 0.45;
arrowMaterial.color.set(chargingRate === 0 ? 0x52616d : 0xff9d42);
arrowMaterial.emissive.set(chargingRate === 0 ? 0x000000 : 0xb54b08);
powerLight.intensity = chargingRate === 0 ? 1 : 5 + normalized * 10;
packetMaterial.opacity = chargingRate === 0 ? 0 : 0.95;
}
energyInput.addEventListener("input", () => {
storedEnergy = Number(energyInput.value);
updateEnergy();
});
rateInput.addEventListener("input", () => {
chargingRate = Number(rateInput.value);
updateRate();
});
updateEnergy();
updateRate();
const worldLabels = [
{
element: document.querySelector("#energy-label"),
position: new THREE.Vector3(0, 2.34, 0)
},
{
element: document.querySelector("#power-label"),
position: new THREE.Vector3(2.85, 1.38, 0.34)
},
{
element: document.querySelector("#tick-0"),
position: new THREE.Vector3(-1.54, tankBottom, 0)
},
{
element: document.querySelector("#tick-20"),
position: new THREE.Vector3(
-1.54,
tankBottom + (20 / capacity) * usableHeight,
0
)
},
{
element: document.querySelector("#tick-40"),
position: new THREE.Vector3(
-1.54,
tankBottom + (40 / capacity) * usableHeight,
0
)
},
{
element: document.querySelector("#tick-60"),
position: new THREE.Vector3(
-1.54,
tankBottom + (60 / capacity) * usableHeight,
0
)
},
{
element: document.querySelector("#tick-80"),
position: new THREE.Vector3(
-1.54,
tankBottom + (80 / capacity) * usableHeight,
0
)
}
];
const projected = new THREE.Vector3();
function updateWorldLabels() {
const width = renderer.domElement.clientWidth;
const height = renderer.domElement.clientHeight;
for (const label of worldLabels) {
projected.copy(label.position).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.25 &&
Math.abs(projected.y) < 1.25;
label.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
// -------------------------------------------------------------------------
// Custom camera controls implemented without Three.js addons
// -------------------------------------------------------------------------
const pointers = new Map();
let lastSinglePointer = null;
let lastPinchDistance = 0;
let panning = false;
const canvas = renderer.domElement;
function pinchDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
function resetCamera() {
radiusGoal = 8.4;
thetaGoal = 0.72;
phiGoal = 1.23;
targetGoal.set(0, 0.08, 0);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
lastSinglePointer = {
x: event.clientX,
y: event.clientY
};
panning = event.shiftKey || event.button === 1;
} else if (pointers.size === 2) {
lastPinchDistance = pinchDistance();
lastSinglePointer = null;
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1 && lastSinglePointer) {
const moveX = event.clientX - lastSinglePointer.x;
const moveY = event.clientY - lastSinglePointer.y;
if (panning) {
const right = new THREE.Vector3(
Math.cos(thetaGoal),
0,
-Math.sin(thetaGoal)
);
targetGoal.addScaledVector(right, -moveX * 0.009);
targetGoal.y += moveY * 0.009;
targetGoal.x = THREE.MathUtils.clamp(targetGoal.x, -3, 3);
targetGoal.y = THREE.MathUtils.clamp(targetGoal.y, -1.2, 2.4);
} else {
thetaGoal -= moveX * 0.006;
phiGoal = THREE.MathUtils.clamp(
phiGoal - moveY * 0.005,
0.28,
1.48
);
}
lastSinglePointer = {
x: event.clientX,
y: event.clientY
};
} else if (pointers.size === 2) {
const distance = pinchDistance();
if (lastPinchDistance > 0 && distance > 0) {
radiusGoal = THREE.MathUtils.clamp(
radiusGoal * (lastPinchDistance / distance),
5.2,
13
);
}
lastPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
lastSinglePointer = { x: remaining.x, y: remaining.y };
panning = false;
} else {
lastSinglePointer = null;
}
if (pointers.size < 2) {
lastPinchDistance = 0;
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
radiusGoal = THREE.MathUtils.clamp(
radiusGoal * Math.exp(event.deltaY * 0.001),
5.2,
13
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
if (event.key === "ArrowLeft") thetaGoal -= 0.1;
if (event.key === "ArrowRight") thetaGoal += 0.1;
if (event.key === "ArrowUp") phiGoal -= 0.08;
if (event.key === "ArrowDown") phiGoal += 0.08;
if (event.key === "+" || event.key === "=") radiusGoal -= 0.4;
if (event.key === "-") radiusGoal += 0.4;
if (event.key.toLowerCase() === "r") resetCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
// Smooth custom orbit controls.
const damping = 1 - Math.pow(0.001, delta);
radius += (radiusGoal - radius) * damping;
theta += (thetaGoal - theta) * damping;
phi += (phiGoal - phi) * damping;
cameraTarget.lerp(targetGoal, damping);
updateCamera();
// Flow packets travel in the direction of the arrow.
const packetSpeed = chargingRate === 0 ? 0 : 0.13 + chargingRate * 0.011;
for (const packet of powerPackets) {
const progress =
(packet.userData.phase + elapsed * packetSpeed) % 1;
packet.position.x = 3.4 - progress * 1.48;
packet.position.y = 0.4 + Math.sin(progress * Math.PI * 4) * 0.018;
packet.visible = chargingRate > 0;
}
// Particles rise slowly inside the stored-energy region.
const positions = particleGeometry.attributes.position.array;
const particleSpeed = 0.12 + chargingRate * 0.009;
const fillTop = tankBottom + currentFillHeight;
for (let i = 0; i < particleCount; i++) {
let y = positions[i * 3 + 1] + delta * particleSpeed;
if (y > fillTop) {
y = tankBottom;
}
positions[i * 3 + 1] = y;
}
particleGeometry.attributes.position.needsUpdate = true;
particles.visible = currentFillHeight > 0.02;
// Gentle surface animation.
energySurface.scale.y = 1 + Math.sin(elapsed * 2.4) * 0.12;
energySurface.material.opacity = 0.82 + Math.sin(elapsed * 2.4) * 0.1;
// Slight arrow glow pulse.
if (chargingRate > 0) {
arrowMaterial.emissiveIntensity =
1.05 + normalizedPulse(chargingRate) * 0.4;
}
updateWorldLabels();
renderer.render(scene, camera);
});
function normalizedPulse(rate) {
return (Math.sin(elapsed * 3.2) * 0.5 + 0.5) * (rate / 40);
}
</script>
</body>
</html>
```