Prompt specification
Build or customize the Three.js 3D scene "Spray trigger cutaway" 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: Spray trigger cutaway
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/spray-trigger-cutaway-x0066
Scene Description Prompt:
Create a low-poly spray bottle with an exposed piston chamber: squeezing the trigger pumps fluid toward a spraying nozzle. Three.js from cdnjs, one HTML file.
Working Three.js Starter (spray-trigger-cutaway-x0066):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Low-Poly Spray Bottle</title>
<style>
:root {
color-scheme: dark;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #071923;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
color: #e9fbff;
}
.title {
position: absolute;
top: 28px;
left: 30px;
max-width: 340px;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.45);
}
.eyebrow {
color: #61e4df;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 6px;
font-size: clamp(25px, 4vw, 42px);
line-height: 0.95;
letter-spacing: -0.045em;
}
.subtitle {
margin: 0;
color: rgba(233, 251, 255, 0.65);
font-size: 13px;
line-height: 1.5;
}
.pressure-panel {
position: absolute;
top: 30px;
right: 30px;
width: 170px;
padding: 13px 15px;
border: 1px solid rgba(151, 235, 239, 0.2);
border-radius: 14px;
background: rgba(7, 25, 35, 0.66);
backdrop-filter: blur(10px);
}
.pressure-label {
display: flex;
justify-content: space-between;
margin-bottom: 9px;
color: rgba(233, 251, 255, 0.7);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.pressure-track {
height: 6px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.09);
}
.pressure-fill {
width: 0%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #39d4d3, #a7fff4);
box-shadow: 0 0 15px rgba(57, 212, 211, 0.7);
}
.controls {
position: absolute;
left: 30px;
bottom: 28px;
color: rgba(233, 251, 255, 0.5);
font-size: 11px;
line-height: 1.65;
}
.controls b {
color: rgba(233, 251, 255, 0.82);
font-weight: 650;
}
#spray-button {
position: absolute;
left: 50%;
bottom: 28px;
min-width: 220px;
padding: 17px 28px;
transform: translateX(-50%);
border: 1px solid rgba(255, 255, 255, 0.22);
border-radius: 999px;
outline: none;
color: #102d36;
background: linear-gradient(135deg, #9ffff3, #4cddd7);
box-shadow:
0 12px 32px rgba(0, 0, 0, 0.35),
0 0 26px rgba(76, 221, 215, 0.22);
font: inherit;
font-size: 12px;
font-weight: 850;
letter-spacing: 0.13em;
text-transform: uppercase;
cursor: pointer;
pointer-events: auto;
touch-action: none;
user-select: none;
transition:
transform 100ms ease,
filter 100ms ease,
box-shadow 100ms ease;
}
#spray-button:hover {
filter: brightness(1.08);
box-shadow:
0 14px 36px rgba(0, 0, 0, 0.4),
0 0 38px rgba(76, 221, 215, 0.38);
}
#spray-button:active,
#spray-button.active {
transform: translateX(-50%) translateY(2px) scale(0.98);
filter: brightness(1.15);
}
@media (max-width: 700px) {
.title {
top: 20px;
left: 20px;
}
.subtitle {
max-width: 230px;
}
.pressure-panel {
top: 20px;
right: 20px;
width: 135px;
}
.controls {
display: none;
}
#spray-button {
bottom: 20px;
}
}
</style>
</head>
<body>
<div class="hud">
<div class="title">
<div class="eyebrow">Mechanical Study / 01</div>
<h1>Pressure<br />Sprayer</h1>
<p class="subtitle">
An exposed low-poly piston chamber. Hold the trigger to compress the
fluid and release a directional mist.
</p>
</div>
<div class="pressure-panel">
<div class="pressure-label">
<span>Piston pressure</span>
<span id="pressure-value">0%</span>
</div>
<div class="pressure-track">
<div class="pressure-fill" id="pressure-fill"></div>
</div>
</div>
<div class="controls">
<b>Drag</b> to orbit · <b>Scroll</b> to zoom<br />
<b>Space</b> or hold the trigger to spray
</div>
<button id="spray-button" type="button" aria-label="Hold to spray">
Press & Hold to Spray
</button>
</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 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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071923);
scene.fog = new THREE.Fog(0x071923, 9, 17);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Direct camera controls. No examples/addons are used.
const cameraState = {
target: new THREE.Vector3(0.12, 1.62, 0),
radius: 6.35,
yaw: 0.57,
pitch: 0.19
};
function updateCamera() {
const { target, radius, yaw, pitch } = cameraState;
camera.position.set(
target.x + Math.sin(yaw) * Math.cos(pitch) * radius,
target.y + Math.sin(pitch) * radius,
target.z + Math.cos(yaw) * Math.cos(pitch) * radius
);
camera.lookAt(target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xb9f8ff, 0x16313a, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.3);
keyLight.position.set(4, 7, 5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -4;
keyLight.shadow.camera.right = 4;
keyLight.shadow.camera.top = 5;
keyLight.shadow.camera.bottom = -2;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 16;
keyLight.shadow.bias = -0.0002;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x45e2e0, 2.5);
rimLight.position.set(-4, 4, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff865f, 8, 6, 2);
warmLight.position.set(1.5, 3.4, 2.3);
scene.add(warmLight);
const product = new THREE.Group();
scene.add(product);
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const glassMaterial = new THREE.MeshPhongMaterial({
color: 0xbcecf4,
transparent: true,
opacity: 0.34,
shininess: 100,
flatShading: true,
side: THREE.DoubleSide,
depthWrite: false
});
const fluidMaterial = new THREE.MeshPhongMaterial({
color: 0x39d4d3,
transparent: true,
opacity: 0.68,
shininess: 90,
flatShading: true,
depthWrite: false
});
const fittingMaterial = new THREE.MeshStandardMaterial({
color: 0x123946,
roughness: 0.58,
metalness: 0.12,
flatShading: true
});
const darkMaterial = new THREE.MeshStandardMaterial({
color: 0x092632,
roughness: 0.72,
metalness: 0.05,
flatShading: true
});
const accentMaterial = new THREE.MeshStandardMaterial({
color: 0xff7656,
roughness: 0.48,
metalness: 0.05,
flatShading: true
});
const metalMaterial = new THREE.MeshStandardMaterial({
color: 0xc7dadd,
roughness: 0.3,
metalness: 0.68,
flatShading: true
});
const tubeMaterial = new THREE.MeshPhongMaterial({
color: 0x8ddde5,
transparent: true,
opacity: 0.22,
shininess: 100,
side: THREE.DoubleSide,
depthWrite: false
});
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
function cylinderX(radius, length, material, radialSegments = 10) {
const geometry = new THREE.CylinderGeometry(
radius,
radius,
length,
radialSegments
);
geometry.rotateZ(Math.PI / 2);
return new THREE.Mesh(geometry, material);
}
function rodBetween(start, end, radius, material, radialSegments = 8) {
const direction = new THREE.Vector3().subVectors(end, start);
const geometry = new THREE.CylinderGeometry(
radius,
radius,
direction.length(),
radialSegments
);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
return mesh;
}
function setRodBetween(mesh, start, end) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = Math.max(direction.length(), 0.001);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.scale.y = length;
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
}
// -------------------------------------------------------------------------
// Display platform
// -------------------------------------------------------------------------
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(2.65, 2.85, 0.18, 32),
new THREE.MeshStandardMaterial({
color: 0x102c38,
roughness: 0.88,
metalness: 0.08
})
);
platform.position.y = -0.02;
platform.receiveShadow = true;
platform.castShadow = true;
scene.add(platform);
const platformRing = new THREE.Mesh(
new THREE.TorusGeometry(2.67, 0.025, 6, 64),
new THREE.MeshBasicMaterial({ color: 0x3adbd7 })
);
platformRing.rotation.x = Math.PI / 2;
platformRing.position.y = 0.078;
scene.add(platformRing);
const ground = new THREE.Mesh(
new THREE.CircleGeometry(12, 64),
new THREE.MeshStandardMaterial({
color: 0x081c27,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.115;
ground.receiveShadow = true;
scene.add(ground);
// -------------------------------------------------------------------------
// Faceted bottle and reservoir
// -------------------------------------------------------------------------
const bottleProfile = [
new THREE.Vector2(0.52, 0.12),
new THREE.Vector2(0.67, 0.2),
new THREE.Vector2(0.75, 0.38),
new THREE.Vector2(0.78, 1.35),
new THREE.Vector2(0.73, 1.65),
new THREE.Vector2(0.57, 1.94),
new THREE.Vector2(0.34, 2.14),
new THREE.Vector2(0.3, 2.23)
];
const bottleGeometry = new THREE.LatheGeometry(bottleProfile, 10);
const bottle = new THREE.Mesh(bottleGeometry, glassMaterial);
bottle.castShadow = true;
bottle.renderOrder = 3;
product.add(bottle);
const bottleEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(bottleGeometry, 18),
new THREE.LineBasicMaterial({
color: 0xd8fbff,
transparent: true,
opacity: 0.27
})
);
bottleEdges.renderOrder = 4;
product.add(bottleEdges);
const bottleBottom = new THREE.Mesh(
new THREE.CylinderGeometry(0.54, 0.56, 0.13, 10),
fittingMaterial
);
bottleBottom.position.y = 0.145;
bottleBottom.castShadow = true;
product.add(bottleBottom);
const liquidGroup = new THREE.Group();
product.add(liquidGroup);
const liquidProfile = [
new THREE.Vector2(0.47, 0.24),
new THREE.Vector2(0.6, 0.31),
new THREE.Vector2(0.67, 0.46),
new THREE.Vector2(0.69, 1.25),
new THREE.Vector2(0.64, 1.47),
new THREE.Vector2(0.49, 1.65)
];
const liquid = new THREE.Mesh(
new THREE.LatheGeometry(liquidProfile, 10),
fluidMaterial
);
liquid.renderOrder = 1;
liquidGroup.add(liquid);
const liquidSurface = new THREE.Mesh(
new THREE.CylinderGeometry(0.49, 0.49, 0.025, 10),
new THREE.MeshPhongMaterial({
color: 0x76f4eb,
transparent: true,
opacity: 0.72,
shininess: 120,
flatShading: true,
depthWrite: false
})
);
liquidSurface.position.y = 1.65;
liquidSurface.renderOrder = 2;
liquidGroup.add(liquidSurface);
const neck = new THREE.Mesh(
new THREE.CylinderGeometry(0.31, 0.33, 0.28, 10),
fittingMaterial
);
neck.position.y = 2.25;
neck.castShadow = true;
product.add(neck);
const neckCollar = new THREE.Mesh(
new THREE.CylinderGeometry(0.39, 0.39, 0.12, 10),
accentMaterial
);
neckCollar.position.y = 2.36;
neckCollar.castShadow = true;
product.add(neckCollar);
// -------------------------------------------------------------------------
// Product label generated locally with a canvas texture
// -------------------------------------------------------------------------
function createLabelTexture() {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 256;
const context = canvas.getContext("2d");
context.fillStyle = "#f4efdc";
context.fillRect(0, 0, 512, 256);
context.fillStyle = "#ff7656";
context.fillRect(32, 31, 74, 74);
context.fillStyle = "#123946";
context.font = "900 55px sans-serif";
context.fillText("MIST", 126, 92);
context.font = "700 20px sans-serif";
context.letterSpacing = "6px";
context.fillText("PRESSURE / 01", 38, 159);
context.fillStyle = "#78b9bc";
context.fillRect(38, 189, 430, 4);
context.font = "600 16px sans-serif";
context.fillStyle = "#43666a";
context.fillText("PLANT-BASED CLEANER", 38, 225);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
const label = new THREE.Mesh(
new THREE.PlaneGeometry(1.12, 0.56),
new THREE.MeshStandardMaterial({
map: createLabelTexture(),
roughness: 0.86,
metalness: 0,
side: THREE.DoubleSide
})
);
label.position.set(0.792, 1.13, 0);
label.rotation.y = Math.PI / 2;
label.renderOrder = 5;
product.add(label);
// -------------------------------------------------------------------------
// Pump head supports
// -------------------------------------------------------------------------
product.add(
rodBetween(
new THREE.Vector3(0, 2.35, 0),
new THREE.Vector3(0.74, 2.64, 0),
0.16,
fittingMaterial
)
);
product.add(
rodBetween(
new THREE.Vector3(0, 2.35, 0),
new THREE.Vector3(-0.82, 2.66, 0),
0.145,
fittingMaterial
)
);
// -------------------------------------------------------------------------
// Exposed piston chamber
// -------------------------------------------------------------------------
const chamberY = 2.78;
const chamberLeft = -0.91;
const chamberRight = 0.75;
const chamberShell = cylinderX(
0.255,
chamberRight - chamberLeft,
tubeMaterial,
12
);
chamberShell.position.set(
(chamberLeft + chamberRight) * 0.5,
chamberY,
0
);
chamberShell.renderOrder = 3;
product.add(chamberShell);
const chamberFluid = cylinderX(0.145, 1, fluidMaterial, 10);
chamberFluid.renderOrder = 1;
product.add(chamberFluid);
for (const x of [chamberLeft, chamberRight]) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.255, 0.045, 6, 14),
fittingMaterial
);
ring.rotation.y = Math.PI / 2;
ring.position.set(x, chamberY, 0);
ring.castShadow = true;
product.add(ring);
}
const rearCap = cylinderX(0.285, 0.13, fittingMaterial, 12);
rearCap.position.set(chamberLeft - 0.04, chamberY, 0);
product.add(rearCap);
const outletCollar = cylinderX(0.29, 0.14, fittingMaterial, 12);
outletCollar.position.set(chamberRight, chamberY, 0);
product.add(outletCollar);
const piston = cylinderX(0.207, 0.12, accentMaterial, 12);
piston.position.set(-0.68, chamberY, 0);
piston.castShadow = true;
product.add(piston);
const pistonBand = cylinderX(0.218, 0.035, darkMaterial, 12);
pistonBand.position.set(-0.68, chamberY, 0);
product.add(pistonBand);
// A visible orange linkage ties the trigger motion to the piston.
const linkage = new THREE.Mesh(
new THREE.CylinderGeometry(0.025, 0.025, 1, 6),
accentMaterial
);
product.add(linkage);
// -------------------------------------------------------------------------
// Trigger assembly
// -------------------------------------------------------------------------
const triggerPivot = new THREE.Group();
triggerPivot.position.set(0.68, 2.57, 0);
product.add(triggerPivot);
const pivotPin = new THREE.Mesh(
new THREE.CylinderGeometry(0.12, 0.12, 0.66, 10),
metalMaterial
);
pivotPin.rotation.x = Math.PI / 2;
pivotPin.position.copy(triggerPivot.position);
pivotPin.castShadow = true;
product.add(pivotPin);
const triggerHandle = new THREE.Mesh(
new THREE.BoxGeometry(0.25, 0.68, 0.35),
accentMaterial
);
triggerHandle.position.set(0, -0.34, 0);
triggerHandle.rotation.z = 0.08;
triggerHandle.castShadow = true;
triggerPivot.add(triggerHandle);
for (const y of [-0.18, -0.34, -0.5]) {
const groove = new THREE.Mesh(
new THREE.BoxGeometry(0.026, 0.035, 0.27),
darkMaterial
);
groove.position.set(0.136, y, 0);
triggerPivot.add(groove);
}
// Larger invisible target makes the trigger easy to click.
const triggerHitbox = new THREE.Mesh(
new THREE.BoxGeometry(0.48, 0.92, 0.58),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false
})
);
triggerHitbox.position.set(0, -0.34, 0);
triggerPivot.add(triggerHitbox);
// -------------------------------------------------------------------------
// Nozzle
// -------------------------------------------------------------------------
const nozzleBody = cylinderX(0.18, 0.38, fittingMaterial, 10);
nozzleBody.position.set(0.94, chamberY, 0);
nozzleBody.castShadow = true;
product.add(nozzleBody);
const nozzleConeGeometry = new THREE.ConeGeometry(0.16, 0.33, 8);
nozzleConeGeometry.rotateZ(-Math.PI / 2);
const nozzleCone = new THREE.Mesh(nozzleConeGeometry, accentMaterial);
nozzleCone.position.set(1.295, chamberY, 0);
nozzleCone.castShadow = true;
product.add(nozzleCone);
const nozzleOpening = cylinderX(0.064, 0.022, darkMaterial, 10);
nozzleOpening.position.set(1.47, chamberY, 0);
product.add(nozzleOpening);
const nozzleRing = new THREE.Mesh(
new THREE.TorusGeometry(0.175, 0.025, 6, 12),
metalMaterial
);
nozzleRing.rotation.y = Math.PI / 2;
nozzleRing.position.set(0.79, chamberY, 0);
product.add(nozzleRing);
// -------------------------------------------------------------------------
// Visible fluid passages
// -------------------------------------------------------------------------
const intakeCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(0.02, 2.31, 0),
new THREE.Vector3(-0.12, 2.49, 0),
new THREE.Vector3(-0.48, 2.53, 0),
new THREE.Vector3(-0.83, 2.67, 0),
new THREE.Vector3(-0.91, chamberY, 0)
]);
const intakeShell = new THREE.Mesh(
new THREE.TubeGeometry(intakeCurve, 28, 0.075, 8, false),
tubeMaterial
);
intakeShell.renderOrder = 3;
product.add(intakeShell);
const intakeFluid = new THREE.Mesh(
new THREE.TubeGeometry(intakeCurve, 28, 0.032, 7, false),
new THREE.MeshBasicMaterial({ color: 0x4fe6df })
);
product.add(intakeFluid);
const outletCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(0.74, chamberY, 0),
new THREE.Vector3(0.87, chamberY, 0),
new THREE.Vector3(1.04, chamberY, 0),
new THREE.Vector3(1.17, chamberY, 0)
]);
const outletFluid = new THREE.Mesh(
new THREE.TubeGeometry(outletCurve, 20, 0.038, 7, false),
new THREE.MeshBasicMaterial({ color: 0x4fe6df })
);
product.add(outletFluid);
// Moving low-poly packets make the pumping direction easy to understand.
const packetGeometry = new THREE.OctahedronGeometry(0.045, 0);
const packetMaterial = new THREE.MeshBasicMaterial({
color: 0xb7fff8
});
const intakePackets = [];
const outletPackets = [];
for (let i = 0; i < 7; i++) {
const mesh = new THREE.Mesh(packetGeometry, packetMaterial);
mesh.visible = false;
product.add(mesh);
intakePackets.push({ mesh, progress: i / 7 });
}
for (let i = 0; i < 6; i++) {
const mesh = new THREE.Mesh(packetGeometry, packetMaterial);
mesh.visible = false;
product.add(mesh);
outletPackets.push({ mesh, progress: i / 6 });
}
// -------------------------------------------------------------------------
// Spray plume and droplet pool
// -------------------------------------------------------------------------
const plumeGeometry = new THREE.ConeGeometry(0.36, 1.55, 12, 1, true);
plumeGeometry.rotateZ(-Math.PI / 2);
const plumeMaterial = new THREE.MeshBasicMaterial({
color: 0x8ff5f0,
transparent: true,
opacity: 0.04,
side: THREE.DoubleSide,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const plume = new THREE.Mesh(plumeGeometry, plumeMaterial);
plume.position.set(2.2, chamberY, 0);
product.add(plume);
const nozzleGlow = new THREE.PointLight(0x65eee7, 0, 3, 2);
nozzleGlow.position.set(1.52, chamberY, 0);
product.add(nozzleGlow);
const dropletCount = 100;
const dropletGeometry = new THREE.OctahedronGeometry(0.035, 0);
const dropletMaterial = new THREE.MeshBasicMaterial({
color: 0xb6fff8,
transparent: true,
opacity: 0.78,
depthWrite: false
});
const dropletMesh = new THREE.InstancedMesh(
dropletGeometry,
dropletMaterial,
dropletCount
);
dropletMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
dropletMesh.frustumCulled = false;
product.add(dropletMesh);
const droplets = Array.from({ length: dropletCount }, () => ({
alive: false,
position: new THREE.Vector3(),
velocity: new THREE.Vector3(),
life: 0,
maxLife: 1,
size: 1
}));
const dummy = new THREE.Object3D();
const hiddenScale = new THREE.Vector3(0, 0, 0);
const identityQuaternion = new THREE.Quaternion();
for (let i = 0; i < dropletCount; i++) {
dummy.position.set(0, -20, 0);
dummy.scale.copy(hiddenScale);
dummy.updateMatrix();
dropletMesh.setMatrixAt(i, dummy.matrix);
}
dropletMesh.instanceMatrix.needsUpdate = true;
const sprayOrigin = new THREE.Vector3(1.5, chamberY, 0);
function spawnDroplet() {
const droplet = droplets.find((item) => !item.alive);
if (!droplet) return;
droplet.alive = true;
droplet.position.copy(sprayOrigin);
droplet.position.x += Math.random() * 0.025;
droplet.position.y += (Math.random() - 0.5) * 0.035;
droplet.position.z += (Math.random() - 0.5) * 0.035;
const speed = 2.25 + Math.random() * 1.3;
const spread = 0.16 + Math.random() * 0.3;
droplet.velocity.set(
speed,
(Math.random() - 0.5) * spread,
(Math.random() - 0.5) * spread
);
droplet.maxLife = 0.72 + Math.random() * 0.72;
droplet.life = droplet.maxLife;
droplet.size = 0.55 + Math.random() * 1.15;
const color = new THREE.Color().setHSL(
0.47 + Math.random() * 0.07,
0.8,
0.72 + Math.random() * 0.2
);
dropletMesh.setColorAt(droplets.indexOf(droplet), color);
}
if (dropletMesh.instanceColor) {
dropletMesh.instanceColor.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Input: trigger, button, keyboard, and direct orbit controls
// -------------------------------------------------------------------------
const button = document.getElementById("spray-button");
const pressureFill = document.getElementById("pressure-fill");
const pressureValue = document.getElementById("pressure-value");
const held = {
trigger: false,
button: false,
key: false
};
function isSpraying() {
return held.trigger || held.button || held.key;
}
button.addEventListener("pointerdown", (event) => {
event.preventDefault();
held.button = true;
button.setPointerCapture(event.pointerId);
});
button.addEventListener("pointerup", () => {
held.button = false;
});
button.addEventListener("pointercancel", () => {
held.button = false;
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
held.key = true;
}
});
window.addEventListener("keyup", (event) => {
if (event.code === "Space") {
held.key = false;
}
});
window.addEventListener("blur", () => {
held.trigger = false;
held.button = false;
held.key = false;
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const canvas = renderer.domElement;
let orbiting = false;
let lastPointerX = 0;
let lastPointerY = 0;
function updatePointer(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
event.preventDefault();
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
const triggerHit = raycaster.intersectObject(triggerHitbox, false).length > 0;
if (triggerHit) {
held.trigger = true;
} else {
orbiting = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
}
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!orbiting) return;
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraState.yaw -= deltaX * 0.006;
cameraState.pitch += deltaY * 0.005;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch,
-0.08,
1.02
);
updateCamera();
});
function endCanvasPointer() {
orbiting = false;
held.trigger = false;
}
canvas.addEventListener("pointerup", endCanvasPointer);
canvas.addEventListener("pointercancel", endCanvasPointer);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
3.6,
10
);
updateCamera();
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let pressure = 0;
let emissionAccumulator = 0;
let elapsed = 0;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
const targetPressure = isSpraying() ? 1 : 0;
const pressureEase = 1 - Math.exp(-delta * (targetPressure ? 16 : 11));
pressure += (targetPressure - pressure) * pressureEase;
// Trigger and exposed piston move together.
triggerPivot.rotation.z = -0.43 * pressure;
const pistonX = -0.68 + pressure * 1.0;
piston.position.x = pistonX;
pistonBand.position.x = pistonX;
const fluidLength = Math.max(0.08, chamberRight - pistonX);
chamberFluid.scale.x = fluidLength;
chamberFluid.position.x = pistonX + fluidLength * 0.5;
setRodBetween(
linkage,
new THREE.Vector3(pistonX, chamberY, 0.23),
new THREE.Vector3(0.67, 2.57, 0.23)
);
// The reservoir gently moves while the mechanism is active.
liquidGroup.position.y = Math.sin(elapsed * 2.2) * 0.006 * pressure;
// Moving fluid packets through the inlet.
for (const packet of intakePackets) {
packet.mesh.visible = pressure > 0.02;
if (packet.mesh.visible) {
packet.progress += delta * (0.35 + pressure * 1.15);
if (packet.progress > 1) packet.progress -= 1;
const point = intakeCurve.getPointAt(packet.progress);
packet.mesh.position.copy(point);
packet.mesh.rotation.x += delta * 3;
packet.mesh.rotation.y += delta * 4;
}
}
// Moving fluid packets toward the nozzle.
for (const packet of outletPackets) {
packet.mesh.visible = pressure > 0.02;
if (packet.mesh.visible) {
packet.progress += delta * (0.8 + pressure * 2.4);
if (packet.progress > 1) packet.progress -= 1;
const point = outletCurve.getPointAt(packet.progress);
packet.mesh.position.copy(point);
packet.mesh.rotation.x += delta * 5;
packet.mesh.rotation.z += delta * 4;
}
}
// Spray emission.
if (isSpraying()) {
emissionAccumulator += delta * 82;
while (emissionAccumulator >= 1) {
spawnDroplet();
emissionAccumulator -= 1;
}
}
// Update pooled droplets.
for (let i = 0; i < droplets.length; i++) {
const droplet = droplets[i];
if (droplet.alive) {
droplet.life -= delta;
if (droplet.life <= 0) {
droplet.alive = false;
dummy.position.set(0, -20, 0);
dummy.scale.copy(hiddenScale);
} else {
droplet.velocity.y -= 0.34 * delta;
droplet.velocity.multiplyScalar(Math.exp(-0.22 * delta));
droplet.position.addScaledVector(droplet.velocity, delta);
const normalizedLife = droplet.life / droplet.maxLife;
const scale = droplet.size * Math.min(1, normalizedLife * 4);
dummy.position.copy(droplet.position);
dummy.quaternion.copy(identityQuaternion);
dummy.scale.setScalar(scale);
}
} else {
dummy.position.set(0, -20, 0);
dummy.scale.copy(hiddenScale);
}
dummy.updateMatrix();
dropletMesh.setMatrixAt(i, dummy.matrix);
}
dropletMesh.instanceMatrix.needsUpdate = true;
// A subtle translucent cone and light make the spray direction readable.
plumeMaterial.opacity = 0.025 + pressure * 0.075;
plume.scale.x = 0.88 + pressure * 0.18 + Math.sin(elapsed * 18) * 0.025;
plume.rotation.z = Math.sin(elapsed * 12) * 0.015;
nozzleGlow.intensity = pressure * 3.5;
// UI pressure readout.
const pressurePercent = Math.round(pressure * 100);
pressureFill.style.width = `${pressurePercent}%`;
pressureValue.textContent = `${pressurePercent}%`;
button.classList.toggle("active", isSpraying());
button.textContent = isSpraying()
? "Pumping & Spraying"
: "Press & Hold to Spray";
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
</script>
</body>
</html>
```