Prompt specification
Build or customize the Three.js 3D scene "Air leak bubble localization" 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: Air leak bubble localization
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/air-leak-bubble-localization-x0242
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring bicycle inner-tube repair. Place the tube segment in a transparent water tray and emit bubbles only from the defect; rotating the segment keeps the bubble source attached to that location. Voxel style.
Working Three.js Starter (air-leak-bubble-localization-x0242):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Inner-Tube Repair</title>
<style>
:root {
color-scheme: light;
--ink: #183334;
--muted: #607678;
--panel: rgba(248, 253, 252, 0.82);
--line: rgba(31, 79, 80, 0.14);
--accent: #e95d4f;
--accent-dark: #ba3b33;
--teal: #287d7b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce8e7;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.glass {
background: var(--panel);
border: 1px solid rgba(255, 255, 255, 0.76);
box-shadow:
0 18px 55px rgba(35, 68, 68, 0.14),
inset 0 0 0 1px var(--line);
backdrop-filter: blur(18px) saturate(125%);
-webkit-backdrop-filter: blur(18px) saturate(125%);
}
.intro {
position: fixed;
top: 24px;
left: 24px;
width: min(390px, calc(100vw - 48px));
padding: 19px 21px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 34px);
line-height: 1.04;
letter-spacing: -0.045em;
}
.intro p {
margin: 10px 0 13px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 13px;
color: #52696a;
font-size: 11px;
font-weight: 650;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.legend i {
width: 9px;
height: 9px;
border-radius: 2px;
box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.7);
}
.legend .defect {
background: var(--accent);
}
.legend .patch {
background: #c99a67;
}
.status {
position: fixed;
top: 24px;
right: 24px;
display: flex;
align-items: center;
gap: 9px;
padding: 11px 14px;
border-radius: 999px;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.13em;
text-transform: uppercase;
pointer-events: none;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 4px rgba(233, 93, 79, 0.13);
animation: pulse 1.6s ease-in-out infinite;
}
.status.sealed {
color: #216c61;
}
.status.sealed .status-dot {
background: #39a998;
box-shadow: 0 0 0 4px rgba(57, 169, 152, 0.14);
animation: none;
}
@keyframes pulse {
0%, 100% { transform: scale(0.82); opacity: 0.68; }
50% { transform: scale(1.16); opacity: 1; }
}
.controls {
position: fixed;
left: 50%;
bottom: 22px;
width: min(900px, calc(100vw - 32px));
transform: translateX(-50%);
display: grid;
grid-template-columns: minmax(260px, 1.5fr) auto auto auto;
gap: 18px;
align-items: center;
padding: 15px 17px;
border-radius: 18px;
}
.control-title {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 8px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
}
output {
color: var(--teal);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
border-radius: 99px;
appearance: none;
background: linear-gradient(
90deg,
var(--teal) 0%,
var(--teal) 5%,
#d9e6e4 5%,
#d9e6e4 100%
);
outline: none;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid #f8fdfc;
border-radius: 5px;
appearance: none;
background: var(--teal);
box-shadow: 0 2px 8px rgba(27, 72, 71, 0.25);
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid #f8fdfc;
border-radius: 5px;
background: var(--teal);
box-shadow: 0 2px 8px rgba(27, 72, 71, 0.25);
}
.switch {
display: flex;
align-items: center;
gap: 8px;
color: #52696a;
font-size: 12px;
font-weight: 750;
white-space: nowrap;
cursor: pointer;
user-select: none;
}
.switch input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.switch-track {
position: relative;
width: 37px;
height: 21px;
border-radius: 999px;
background: #cbdad8;
transition: background 160ms ease;
}
.switch-track::after {
content: "";
position: absolute;
top: 3px;
left: 3px;
width: 15px;
height: 15px;
border-radius: 50%;
background: white;
box-shadow: 0 2px 5px rgba(35, 60, 60, 0.22);
transition: transform 160ms ease;
}
.switch input:checked + .switch-track {
background: var(--teal);
}
.switch input:checked + .switch-track::after {
transform: translateX(16px);
}
button {
min-height: 39px;
padding: 0 15px;
border: 1px solid rgba(34, 91, 90, 0.16);
border-radius: 10px;
background: rgba(255, 255, 255, 0.7);
color: #315b5b;
font: inherit;
font-size: 11px;
font-weight: 800;
white-space: nowrap;
cursor: pointer;
transition:
transform 140ms ease,
background 140ms ease,
color 140ms ease;
}
button:hover {
transform: translateY(-1px);
background: white;
}
button:active {
transform: translateY(0);
}
button.primary {
border-color: transparent;
background: var(--accent);
color: white;
box-shadow: 0 7px 18px rgba(204, 67, 56, 0.22);
}
button.primary:hover {
background: var(--accent-dark);
}
button.sealed {
background: #d9f2ec;
color: #246c62;
box-shadow: none;
}
.hint {
position: fixed;
right: 25px;
bottom: 27px;
color: rgba(35, 67, 67, 0.62);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.05em;
pointer-events: none;
}
#loading {
position: fixed;
inset: 0;
display: grid;
place-items: center;
background: #dce8e7;
color: #4f6c6b;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 850px) {
.intro {
top: 14px;
left: 14px;
width: min(340px, calc(100vw - 28px));
padding: 15px 17px;
}
.intro p {
display: none;
}
.status {
top: 14px;
right: 14px;
}
.controls {
bottom: 12px;
width: calc(100vw - 24px);
grid-template-columns: 1fr 1fr;
gap: 12px;
padding: 13px;
}
.range-block {
grid-column: 1 / -1;
}
.hint {
display: none;
}
}
@media (max-width: 500px) {
.status {
top: auto;
right: 12px;
bottom: 152px;
}
.legend {
display: none;
}
.controls {
grid-template-columns: 1fr 1fr;
}
.switch {
justify-content: center;
}
button {
padding: 0 9px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<header class="intro glass">
<div class="eyebrow">Workshop / diagnostics</div>
<h1>Inner-Tube Repair</h1>
<p>
Rotate the voxel tube to inspect the puncture. Air bubbles originate
only at the red defect marker and remain anchored as the tube turns.
</p>
<div class="legend">
<span><i class="defect"></i>Defect / bubble origin</span>
<span><i class="patch"></i>Repair patch</span>
</div>
</header>
<div id="status" class="status glass">
<span class="status-dot"></span>
<span id="statusText">Leak test · active</span>
</div>
<section class="controls glass" aria-label="Scene controls">
<div class="range-block">
<div class="control-title">
<label for="rotation">Segment rotation</label>
<output id="angleValue" for="rotation">18°</output>
</div>
<input
id="rotation"
type="range"
min="0"
max="360"
step="1"
value="18"
aria-label="Rotate the inner tube"
/>
</div>
<label class="switch">
<input id="spin" type="checkbox" checked />
<span class="switch-track"></span>
<span>Auto rotate</span>
</label>
<button id="patchButton" class="primary">Seat repair patch</button>
<button id="resetCamera">Reset camera</button>
</section>
<div class="hint">Drag to orbit · wheel to zoom · arrow keys rotate tube</div>
<div id="loading">Building voxel workshop…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const loading = document.getElementById("loading");
const rotationInput = document.getElementById("rotation");
const angleValue = document.getElementById("angleValue");
const spinInput = document.getElementById("spin");
const patchButton = document.getElementById("patchButton");
const resetCameraButton = document.getElementById("resetCamera");
const status = document.getElementById("status");
const statusText = document.getElementById("statusText");
const renderer = new THREE.WebGLRenderer({
antialias: true,
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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel bicycle inner-tube repair scene"
);
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce8e7);
scene.fog = new THREE.Fog(0xdce8e7, 17, 31);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
80
);
const hemisphere = new THREE.HemisphereLight(
0xf4ffff,
0x7d8b87,
2.25
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.0);
keyLight.position.set(5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 24;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x9ee9e5, 1.1);
fillLight.position.set(-7, 5, -5);
scene.add(fillLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
new THREE.MeshStandardMaterial({
color: 0xcbd9d7,
roughness: 0.96,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.09;
floor.receiveShadow = true;
scene.add(floor);
function addBox(parent, size, position, material, options = {}) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.copy(position);
mesh.castShadow = options.castShadow ?? false;
mesh.receiveShadow = options.receiveShadow ?? false;
mesh.renderOrder = options.renderOrder ?? 0;
parent.add(mesh);
return mesh;
}
// Transparent tray and water
const tray = new THREE.Group();
scene.add(tray);
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: 0x9adfdc,
transparent: true,
opacity: 0.18,
roughness: 0.18,
metalness: 0,
clearcoat: 0.7,
clearcoatRoughness: 0.2,
side: THREE.DoubleSide,
depthWrite: false
});
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0x4b9d9b,
transparent: true,
opacity: 0.42,
roughness: 0.35,
metalness: 0.12,
depthWrite: false
});
addBox(
tray,
new THREE.Vector3(11.7, 0.16, 8.1),
new THREE.Vector3(0, 0.03, 0),
glassMaterial,
{ receiveShadow: true, renderOrder: 4 }
);
const wallHeight = 1.26;
const wallY = wallHeight / 2 + 0.08;
addBox(
tray,
new THREE.Vector3(11.7, wallHeight, 0.11),
new THREE.Vector3(0, wallY, -4.0),
glassMaterial,
{ renderOrder: 4 }
);
addBox(
tray,
new THREE.Vector3(11.7, wallHeight, 0.11),
new THREE.Vector3(0, wallY, 4.0),
glassMaterial,
{ renderOrder: 4 }
);
addBox(
tray,
new THREE.Vector3(0.11, wallHeight, 8.1),
new THREE.Vector3(-5.8, wallY, 0),
glassMaterial,
{ renderOrder: 4 }
);
addBox(
tray,
new THREE.Vector3(0.11, wallHeight, 8.1),
new THREE.Vector3(5.8, wallY, 0),
glassMaterial,
{ renderOrder: 4 }
);
const rimY = 1.35;
addBox(
tray,
new THREE.Vector3(11.9, 0.08, 0.15),
new THREE.Vector3(0, rimY, -4.02),
rimMaterial,
{ renderOrder: 5 }
);
addBox(
tray,
new THREE.Vector3(11.9, 0.08, 0.15),
new THREE.Vector3(0, rimY, 4.02),
rimMaterial,
{ renderOrder: 5 }
);
addBox(
tray,
new THREE.Vector3(0.15, 0.08, 8.15),
new THREE.Vector3(-5.82, rimY, 0),
rimMaterial,
{ renderOrder: 5 }
);
addBox(
tray,
new THREE.Vector3(0.15, 0.08, 8.15),
new THREE.Vector3(5.82, rimY, 0),
rimMaterial,
{ renderOrder: 5 }
);
const WATER_LEVEL = 1.16;
const waterBody = addBox(
tray,
new THREE.Vector3(11.42, 1.04, 7.78),
new THREE.Vector3(0, 0.65, 0),
new THREE.MeshBasicMaterial({
color: 0x68d4d0,
transparent: true,
opacity: 0.045,
side: THREE.BackSide,
depthWrite: false
}),
{ renderOrder: 1 }
);
waterBody.castShadow = false;
const waterSurface = new THREE.Mesh(
new THREE.PlaneGeometry(11.42, 7.78),
new THREE.MeshPhysicalMaterial({
color: 0x73d9d5,
transparent: true,
opacity: 0.22,
roughness: 0.16,
metalness: 0.03,
clearcoat: 1,
clearcoatRoughness: 0.16,
side: THREE.DoubleSide,
depthWrite: false
})
);
waterSurface.rotation.x = -Math.PI / 2;
waterSurface.position.y = WATER_LEVEL;
waterSurface.renderOrder = 2;
tray.add(waterSurface);
// Rotatable tube segment
const tubeGroup = new THREE.Group();
tubeGroup.position.y = 0.44;
scene.add(tubeGroup);
const ARC_RADIUS = 3.12;
const ARC_START = THREE.MathUtils.degToRad(128);
const ARC_END = THREE.MathUtils.degToRad(398);
const SAMPLE_COUNT = 150;
const tubeGeometry = new THREE.BoxGeometry(0.155, 0.155, 0.155);
const tubeMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.88,
metalness: 0.025,
flatShading: true,
vertexColors: true
});
const crossSection = [
[0, 0],
[1, 0],
[-1, 0],
[0, 1],
[0, -1],
[1, 1],
[-1, 1],
[1, -1],
[-1, -1]
];
const tubeMesh = new THREE.InstancedMesh(
tubeGeometry,
tubeMaterial,
SAMPLE_COUNT * crossSection.length
);
tubeMesh.castShadow = true;
tubeMesh.receiveShadow = true;
const dummy = new THREE.Object3D();
const instanceColor = new THREE.Color();
let instanceIndex = 0;
for (let i = 0; i < SAMPLE_COUNT; i++) {
const t = i / (SAMPLE_COUNT - 1);
const angle = THREE.MathUtils.lerp(ARC_START, ARC_END, t);
const point = new THREE.Vector3(
Math.cos(angle) * ARC_RADIUS,
0,
Math.sin(angle) * ARC_RADIUS
);
const radial = new THREE.Vector3(
Math.cos(angle),
0,
Math.sin(angle)
);
for (const [sideOffset, verticalOffset] of crossSection) {
dummy.position.copy(point);
dummy.position.addScaledVector(
radial,
sideOffset === 0 ? 0 : sideOffset * 0.14
);
dummy.position.y =
verticalOffset === 0 ? 0 : verticalOffset * 0.145;
dummy.rotation.set(0, -angle, 0);
dummy.updateMatrix();
tubeMesh.setMatrixAt(instanceIndex, dummy.matrix);
const variation = Math.sin(i * 12.713 + instanceIndex * 4.17);
instanceColor.setHSL(
0.57,
0.08,
0.075 + variation * 0.012 + Math.random() * 0.012
);
tubeMesh.setColorAt(instanceIndex, instanceColor);
instanceIndex++;
}
}
tubeMesh.instanceMatrix.needsUpdate = true;
tubeMesh.instanceColor.needsUpdate = true;
tubeMesh.computeBoundingSphere();
tubeGroup.add(tubeMesh);
// Voxel valve identifies the object as a bicycle inner tube.
const valveAngle = THREE.MathUtils.degToRad(205);
const valve = new THREE.Group();
valve.position.set(
Math.cos(valveAngle) * ARC_RADIUS,
0.2,
Math.sin(valveAngle) * ARC_RADIUS
);
tubeGroup.add(valve);
const brassMaterial = new THREE.MeshStandardMaterial({
color: 0xc59a52,
roughness: 0.42,
metalness: 0.68
});
const valveDarkMaterial = new THREE.MeshStandardMaterial({
color: 0x263332,
roughness: 0.72,
metalness: 0.12
});
addBox(
valve,
new THREE.Vector3(0.25, 0.08, 0.25),
new THREE.Vector3(0, 0, 0),
valveDarkMaterial,
{ castShadow: true }
);
addBox(
valve,
new THREE.Vector3(0.09, 0.48, 0.09),
new THREE.Vector3(0, 0.27, 0),
brassMaterial,
{ castShadow: true }
);
addBox(
valve,
new THREE.Vector3(0.22, 0.1, 0.22),
new THREE.Vector3(0, 0.54, 0),
valveDarkMaterial,
{ castShadow: true }
);
addBox(
valve,
new THREE.Vector3(0.32, 0.07, 0.08),
new THREE.Vector3(0, 0.58, 0),
brassMaterial,
{ castShadow: true }
);
addBox(
valve,
new THREE.Vector3(0.07, 0.13, 0.07),
new THREE.Vector3(0, 0.65, 0),
valveDarkMaterial,
{ castShadow: true }
);
// Defect marker and bubble source
const defectAngle = THREE.MathUtils.degToRad(352);
const defectPoint = new THREE.Vector3(
Math.cos(defectAngle) * ARC_RADIUS,
0.255,
Math.sin(defectAngle) * ARC_RADIUS
);
const defectTangent = new THREE.Vector3(
-Math.sin(defectAngle),
0,
Math.cos(defectAngle)
);
const defectSide = new THREE.Vector3(
Math.cos(defectAngle),
0,
Math.sin(defectAngle)
);
const defectMarker = new THREE.Group();
defectMarker.position.copy(defectPoint);
tubeGroup.add(defectMarker);
const defectMaterial = new THREE.MeshStandardMaterial({
color: 0xff5c4f,
emissive: 0x8f1711,
emissiveIntensity: 0.85,
roughness: 0.48,
metalness: 0.05
});
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0xffb13b,
emissive: 0x8c3d00,
emissiveIntensity: 0.4,
roughness: 0.6
});
const markerGeometry = new THREE.BoxGeometry(0.095, 0.055, 0.095);
const markerOffsets = [
[0, 0],
[1, 0],
[-1, 0],
[0, 1],
[0, -1]
];
let defectCore;
for (const [along, across] of markerOffsets) {
const marker = new THREE.Mesh(
markerGeometry,
along === 0 && across === 0
? defectMaterial
: markerMaterial
);
marker.position
.copy(defectTangent)
.multiplyScalar(along * 0.105)
.addScaledVector(defectSide, across * 0.105);
marker.position.y = 0;
marker.castShadow = true;
defectMarker.add(marker);
if (along === 0 && across === 0) {
defectCore = marker;
}
}
// This object is a child of the rotating tube. Its world position
// therefore stays locked to the puncture at every rotation.
const bubbleSource = new THREE.Object3D();
bubbleSource.position.y = 0.08;
defectMarker.add(bubbleSource);
// Voxel repair patch
const patchGroup = new THREE.Group();
patchGroup.rotation.y = -defectAngle - Math.PI / 2;
tubeGroup.add(patchGroup);
const patchMaterial = new THREE.MeshStandardMaterial({
color: 0xc89a68,
roughness: 0.94,
metalness: 0
});
const patchMaterialLight = new THREE.MeshStandardMaterial({
color: 0xd8af7c,
roughness: 0.94,
metalness: 0
});
const patchGeometry = new THREE.BoxGeometry(0.115, 0.045, 0.115);
for (let x = -1; x <= 1; x++) {
for (let z = -1; z <= 1; z++) {
const tile = new THREE.Mesh(
patchGeometry,
(x + z) % 2 === 0
? patchMaterial
: patchMaterialLight
);
tile.position.set(x * 0.125, 0, z * 0.125);
tile.castShadow = true;
patchGroup.add(tile);
}
}
const patchHome = defectPoint
.clone()
.addScaledVector(defectTangent, -0.9);
patchHome.y = 0.31;
const patchSeat = defectPoint.clone();
patchSeat.y = 0.31;
patchGroup.position.copy(patchHome);
let patched = false;
let patchTarget = patchHome.clone();
function updateStatus() {
const seated =
patched && patchGroup.position.distanceTo(patchSeat) < 0.025;
status.classList.toggle("sealed", seated);
if (seated) {
statusText.textContent = "Patched · sealed";
} else if (patched) {
statusText.textContent = "Patch seating…";
} else {
statusText.textContent = "Leak test · active";
}
}
patchButton.addEventListener("click", () => {
patched = !patched;
patchTarget.copy(patched ? patchSeat : patchHome);
patchButton.textContent = patched
? "Reopen puncture"
: "Seat repair patch";
patchButton.classList.toggle("sealed", patched);
updateStatus();
});
// Pooled voxel bubbles. Every bubble is spawned from bubbleSource.
const bubbleGeometry = new THREE.BoxGeometry(1, 1, 1);
const bubbleMaterial = new THREE.MeshStandardMaterial({
color: 0xeaffff,
emissive: 0x8ddfe5,
emissiveIntensity: 0.25,
roughness: 0.28,
metalness: 0,
transparent: true,
opacity: 0.86,
depthWrite: false
});
const bubblePool = [];
const BUBBLE_COUNT = 84;
for (let i = 0; i < BUBBLE_COUNT; i++) {
const mesh = new THREE.Mesh(bubbleGeometry, bubbleMaterial);
mesh.visible = false;
mesh.renderOrder = 3;
scene.add(mesh);
bubblePool.push({
mesh,
active: false,
age: 0,
speed: 0,
phase: 0,
driftX: 0,
driftZ: 0,
baseSize: 0.08
});
}
const sourceWorldPosition = new THREE.Vector3();
function spawnBubble() {
const bubble = bubblePool.find((item) => !item.active);
if (!bubble) return;
tubeGroup.updateMatrixWorld(true);
bubbleSource.getWorldPosition(sourceWorldPosition);
const size = 0.065 + Math.random() * 0.052;
bubble.active = true;
bubble.age = 0;
bubble.speed = 0.31 + Math.random() * 0.2;
bubble.phase = Math.random() * Math.PI * 2;
bubble.driftX = (Math.random() - 0.5) * 0.035;
bubble.driftZ = (Math.random() - 0.5) * 0.035;
bubble.baseSize = size;
bubble.mesh.position.copy(sourceWorldPosition);
bubble.mesh.position.x += (Math.random() - 0.5) * 0.018;
bubble.mesh.position.z += (Math.random() - 0.5) * 0.018;
bubble.mesh.scale.setScalar(size);
bubble.mesh.rotation.set(
Math.random() * 0.4,
Math.random() * Math.PI,
Math.random() * 0.4
);
bubble.mesh.visible = true;
}
function updateBubbles(dt) {
for (const bubble of bubblePool) {
if (!bubble.active) continue;
bubble.age += dt;
bubble.mesh.position.y += bubble.speed * dt;
bubble.mesh.position.x +=
(bubble.driftX + Math.sin(bubble.age * 7 + bubble.phase) * 0.012) *
dt;
bubble.mesh.position.z +=
(bubble.driftZ + Math.cos(bubble.age * 6 + bubble.phase) * 0.009) *
dt;
const growth = 1 + bubble.age * 0.12;
bubble.mesh.scale.set(
bubble.baseSize * growth,
bubble.baseSize * (0.88 + bubble.age * 0.1),
bubble.baseSize * growth
);
bubble.mesh.rotation.y += dt * 0.8;
if (bubble.mesh.position.y >= WATER_LEVEL - 0.025) {
bubble.active = false;
bubble.mesh.visible = false;
}
}
}
// Custom camera controls — no Three.js addons are used.
const cameraState = {
theta: 0.72,
phi: 1.03,
radius: 12.5,
goalTheta: 0.72,
goalPhi: 1.03,
goalRadius: 12.5
};
const cameraTarget = new THREE.Vector3(0, 0.55, 0);
function updateCamera(dt) {
const smoothing = 1 - Math.exp(-dt * 11);
cameraState.theta +=
(cameraState.goalTheta - cameraState.theta) * smoothing;
cameraState.phi +=
(cameraState.goalPhi - cameraState.phi) * smoothing;
cameraState.radius +=
(cameraState.goalRadius - cameraState.radius) * smoothing;
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y + cameraState.radius * Math.cos(cameraState.phi),
cameraTarget.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.goalTheta -= deltaX * 0.006;
cameraState.goalPhi = THREE.MathUtils.clamp(
cameraState.goalPhi - deltaY * 0.005,
0.38,
1.43
);
});
function stopDragging(event) {
dragging = false;
if (
event &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.goalRadius = THREE.MathUtils.clamp(
cameraState.goalRadius * Math.exp(event.deltaY * 0.001),
8.5,
18
);
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
resetCameraButton.addEventListener("click", () => {
cameraState.goalTheta = 0.72;
cameraState.goalPhi = 1.03;
cameraState.goalRadius = 12.5;
});
// Tube controls
let tubeRotation = THREE.MathUtils.degToRad(18);
function updateRangeFill() {
const percent = (Number(rotationInput.value) / 360) * 100;
rotationInput.style.background =
`linear-gradient(90deg, var(--teal) 0%, ` +
`var(--teal) ${percent}%, #d9e6e4 ${percent}%, #d9e6e4 100%)`;
}
rotationInput.addEventListener("input", () => {
tubeRotation = THREE.MathUtils.degToRad(
Number(rotationInput.value)
);
spinInput.checked = false;
angleValue.textContent = `${rotationInput.value}°`;
updateRangeFill();
});
window.addEventListener("keydown", (event) => {
if (
event.key === "ArrowLeft" ||
event.key === "ArrowRight"
) {
event.preventDefault();
spinInput.checked = false;
const direction = event.key === "ArrowRight" ? 1 : -1;
const current =
((Number(rotationInput.value) + direction * 5) % 360 + 360) %
360;
rotationInput.value = String(current);
tubeRotation = THREE.MathUtils.degToRad(current);
angleValue.textContent = `${current}°`;
updateRangeFill();
}
if (event.key.toLowerCase() === "r") {
cameraState.goalTheta = 0.72;
cameraState.goalPhi = 1.03;
cameraState.goalRadius = 12.5;
}
});
updateRangeFill();
// Animation
const clock = new THREE.Clock();
let bubbleTimer = 0;
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (spinInput.checked) {
tubeRotation += dt * 0.17;
}
const normalizedAngle =
((THREE.MathUtils.radToDeg(tubeRotation) % 360) + 360) % 360;
tubeGroup.rotation.y = tubeRotation;
if (spinInput.checked) {
rotationInput.value = String(Math.round(normalizedAngle));
angleValue.textContent = `${Math.round(normalizedAngle)}°`;
updateRangeFill();
}
// Ease the patch toward its seated or removed position.
const patchSmoothing = 1 - Math.exp(-dt * 7);
patchGroup.position.lerp(patchTarget, patchSmoothing);
if (
patchGroup.position.distanceTo(patchTarget) < 0.002
) {
patchGroup.position.copy(patchTarget);
}
updateStatus();
// Pulse only the defect marker; this is not an additional emitter.
if (defectCore) {
const pulse = 1 + Math.sin(elapsed * 5.2) * 0.12;
defectCore.scale.setScalar(pulse);
}
if (!patched) {
bubbleTimer -= dt;
while (bubbleTimer <= 0) {
spawnBubble();
bubbleTimer += 0.095;
}
}
updateBubbles(dt);
updateCamera(dt);
renderer.render(scene, camera);
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
renderer.setAnimationLoop(animate);
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 400);
});
</script>
</body>
</html>
```