Prompt specification
Build or customize the Three.js 3D scene "Trigger spray bottle" 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: Trigger spray bottle
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/trigger-spray-bottle-x0065
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a voxel trigger spray bottle. Build a broad translucent bottle body, a narrow neck, a curved trigger, and an adjustable front nozzle. Show a pickup tube reaching near the bottom of the bottle, with a clear connection to the pump housing above. Use a slight cutaway in the housing to expose the trigger hinge without making the whole object transparent. A small trigger movement can show where the user's force enters the mechanism. Keep the silhouette recognizable from a three-quarter view, with denser cubes around the hinge and coarser blocks on the bottle walls. Place the bottle alone on a small plain base.
Working Three.js Starter (trigger-spray-bottle-x0065):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Trigger Spray Bottle</title>
<style>
:root {
color-scheme: light;
--ink: #23323a;
--muted: #66777f;
--panel: rgba(249, 251, 251, 0.88);
--border: rgba(40, 58, 68, 0.15);
--accent: #ed743d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9eef0;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.title-card {
position: fixed;
top: 22px;
left: 24px;
padding: 14px 17px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
backdrop-filter: blur(12px);
box-shadow: 0 12px 30px rgba(37, 55, 65, 0.09);
pointer-events: none;
}
.title-card h1 {
margin: 0 0 4px;
font-size: 16px;
font-weight: 750;
letter-spacing: 0.015em;
}
.title-card p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.controls {
position: fixed;
right: 22px;
bottom: 22px;
width: min(270px, calc(100vw - 44px));
padding: 15px;
border: 1px solid var(--border);
border-radius: 15px;
background: var(--panel);
backdrop-filter: blur(12px);
box-shadow: 0 14px 36px rgba(37, 55, 65, 0.12);
user-select: none;
}
.control-label {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 7px;
color: var(--ink);
font-size: 12px;
font-weight: 700;
}
.value {
color: var(--accent);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
margin: 0 0 14px;
accent-color: var(--accent);
cursor: ew-resize;
}
.button-row {
display: flex;
gap: 8px;
}
button {
min-height: 38px;
border: 1px solid rgba(39, 55, 64, 0.14);
border-radius: 10px;
background: rgba(255, 255, 255, 0.82);
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
box-shadow 120ms ease;
}
button:hover {
background: #fff;
box-shadow: 0 5px 14px rgba(35, 50, 58, 0.08);
}
button:active {
transform: translateY(1px);
}
#squeezeButton {
flex: 1;
color: #fff;
border-color: transparent;
background: var(--accent);
}
#squeezeButton:hover {
background: #f5824c;
}
#resetButton {
width: 74px;
}
.hint {
margin: 12px 1px 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.hint strong {
color: var(--ink);
font-weight: 700;
}
@media (max-width: 650px) {
.title-card {
top: 12px;
left: 12px;
right: 12px;
}
.controls {
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
}
}
</style>
</head>
<body>
<canvas id="scene" tabindex="0" aria-label="Interactive voxel trigger spray bottle"></canvas>
<div class="title-card">
<h1>Voxel Trigger Spray Bottle</h1>
<p>Drag empty space to orbit · Scroll to zoom</p>
</div>
<div class="controls">
<label class="control-label" for="nozzleAngle">
<span>Adjustable nozzle</span>
<span class="value" id="nozzleValue">0°</span>
</label>
<input
id="nozzleAngle"
type="range"
min="-55"
max="55"
step="1"
value="0"
aria-label="Rotate the front nozzle"
/>
<div class="button-row">
<button id="squeezeButton" type="button">Hold to squeeze</button>
<button id="resetButton" type="button">Reset view</button>
</div>
<p class="hint">
<strong>Drag the orange trigger</strong> inward, or hold the space bar.
The arrow shows force entering the exposed hinge. Drag the nozzle to swivel it.
</p>
</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 canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
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.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9eef0);
scene.fog = new THREE.Fog(0xe9eef0, 17, 36);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 2.78, 0);
let cameraRadius = 10.7;
let cameraTheta = 0.64;
let cameraPhi = 1.25;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xf4fbff, 0x8b9295, 2.15));
const keyLight = new THREE.DirectionalLight(0xfff4e8, 3.4);
keyLight.position.set(5.5, 10, 7.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -3;
keyLight.shadow.bias = -0.0007;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xb8e8ff, 1.45);
fillLight.position.set(-6, 5, 4);
scene.add(fillLight);
const rimLight = new THREE.DirectionalLight(0xffd4aa, 1.1);
rimLight.position.set(2, 6, -7);
scene.add(rimLight);
// ------------------------------------------------------------
// Materials
// ------------------------------------------------------------
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: 0x9edcdf,
roughness: 0.22,
metalness: 0,
transparent: true,
opacity: 0.23,
transmission: 0.12,
thickness: 0.35,
ior: 1.33,
depthWrite: false,
side: THREE.DoubleSide
});
const neckMaterial = new THREE.MeshPhysicalMaterial({
color: 0xa9e2e0,
roughness: 0.2,
metalness: 0,
transparent: true,
opacity: 0.34,
transmission: 0.1,
thickness: 0.28,
depthWrite: false,
side: THREE.DoubleSide
});
const liquidMaterial = new THREE.MeshPhysicalMaterial({
color: 0x4bc2cf,
roughness: 0.18,
metalness: 0,
transparent: true,
opacity: 0.16,
transmission: 0.08,
thickness: 0.5,
depthWrite: false
});
const liquidSurfaceMaterial = new THREE.MeshStandardMaterial({
color: 0x64d5df,
roughness: 0.2,
transparent: true,
opacity: 0.3,
depthWrite: false,
side: THREE.DoubleSide
});
const housingMaterial = new THREE.MeshStandardMaterial({
color: 0x293a47,
roughness: 0.36,
metalness: 0.25
});
const housingEdgeMaterial = new THREE.MeshStandardMaterial({
color: 0x587180,
roughness: 0.32,
metalness: 0.3
});
const triggerMaterial = new THREE.MeshStandardMaterial({
color: 0xed743d,
roughness: 0.4,
metalness: 0.08
});
const triggerHighlightMaterial = new THREE.MeshStandardMaterial({
color: 0xff9b5e,
roughness: 0.34,
metalness: 0.05
});
const tubeMaterial = new THREE.MeshStandardMaterial({
color: 0xf4e8b6,
roughness: 0.42,
metalness: 0.02
});
const hingeMaterial = new THREE.MeshStandardMaterial({
color: 0xd5a642,
roughness: 0.25,
metalness: 0.72,
emissive: 0x633000,
emissiveIntensity: 0.12
});
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0xcfd6d8,
roughness: 0.78,
metalness: 0.02
});
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0xe9eef0,
roughness: 1,
metalness: 0
});
// ------------------------------------------------------------
// Utility
// ------------------------------------------------------------
function addBox(
width,
height,
depth,
x,
y,
z,
material,
parent = scene,
castShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Plain studio floor and small base
// ------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(100, 100),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.01;
ground.receiveShadow = true;
scene.add(ground);
const base = new THREE.Mesh(
new THREE.CylinderGeometry(1.93, 1.98, 0.2, 64),
baseMaterial
);
base.position.y = 0.1;
base.castShadow = true;
base.receiveShadow = true;
scene.add(base);
// ------------------------------------------------------------
// Coarse voxel bottle shell
// ------------------------------------------------------------
const wallPositions = [];
function addWallRow(y, xCount, zCount) {
for (let ix = 0; ix < xCount; ix++) {
for (let iz = 0; iz < zCount; iz++) {
const isEdge =
ix === 0 ||
ix === xCount - 1 ||
iz === 0 ||
iz === zCount - 1;
if (!isEdge) continue;
wallPositions.push(
new THREE.Vector3(
(ix - (xCount - 1) / 2) * 0.4,
y,
(iz - (zCount - 1) / 2) * 0.4
)
);
}
}
}
// Broad rectangular lower body.
addWallRow(0.48, 7, 4);
addWallRow(0.88, 7, 4);
addWallRow(1.28, 7, 4);
addWallRow(1.68, 7, 4);
addWallRow(2.08, 7, 4);
addWallRow(2.48, 7, 4);
addWallRow(2.88, 7, 4);
// Stepped shoulders and narrow upper bottle.
addWallRow(3.28, 6, 3);
addWallRow(3.68, 4, 2);
addWallRow(4.08, 2, 2);
const shell = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.37, 0.37, 0.37),
glassMaterial,
wallPositions.length
);
const instanceDummy = new THREE.Object3D();
wallPositions.forEach((position, index) => {
instanceDummy.position.copy(position);
instanceDummy.updateMatrix();
shell.setMatrixAt(index, instanceDummy.matrix);
});
shell.instanceMatrix.needsUpdate = true;
shell.renderOrder = 3;
shell.castShadow = false;
scene.add(shell);
// Translucent bottom slab.
addBox(
2.62,
0.14,
1.42,
0,
0.27,
0,
glassMaterial,
scene,
false
);
// Liquid volume and its slightly brighter top surface.
const liquid = addBox(
2.43,
2.46,
1.18,
0,
1.55,
0,
liquidMaterial,
scene,
false
);
liquid.renderOrder = 1;
const liquidSurface = addBox(
2.43,
0.045,
1.18,
0,
2.79,
0,
liquidSurfaceMaterial,
scene,
false
);
liquidSurface.renderOrder = 2;
// Narrow voxel neck.
const neckPositions = [];
for (let level = 0; level < 3; level++) {
for (let ix = 0; ix < 2; ix++) {
for (let iz = 0; iz < 2; iz++) {
neckPositions.push(
new THREE.Vector3(
(ix - 0.5) * 0.34,
4.3 + level * 0.3,
(iz - 0.5) * 0.34
)
);
}
}
}
const neck = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.31, 0.28, 0.31),
neckMaterial,
neckPositions.length
);
neckPositions.forEach((position, index) => {
instanceDummy.position.copy(position);
instanceDummy.updateMatrix();
neck.setMatrixAt(index, instanceDummy.matrix);
});
neck.instanceMatrix.needsUpdate = true;
neck.renderOrder = 3;
scene.add(neck);
// ------------------------------------------------------------
// Pickup tube: square-section segmented path through the bottle
// ------------------------------------------------------------
const tubeCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(0.18, 0.42, 0.02),
new THREE.Vector3(0.18, 0.84, -0.03),
new THREE.Vector3(0.13, 1.55, -0.08),
new THREE.Vector3(0.03, 2.45, -0.08),
new THREE.Vector3(-0.07, 3.3, -0.03),
new THREE.Vector3(-0.05, 4.05, 0),
new THREE.Vector3(0, 4.52, 0.025),
new THREE.Vector3(0, 4.72, 0.035)
]);
const tubeSamples = tubeCurve.getPoints(92);
const tubeSegments = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.075, 1, 0.075),
tubeMaterial,
tubeSamples.length - 1
);
const upAxis = new THREE.Vector3(0, 1, 0);
for (let i = 0; i < tubeSamples.length - 1; i++) {
const start = tubeSamples[i];
const end = tubeSamples[i + 1];
const direction = end.clone().sub(start);
const length = direction.length();
const midpoint = start.clone().add(end).multiplyScalar(0.5);
instanceDummy.position.copy(midpoint);
instanceDummy.quaternion.setFromUnitVectors(
upAxis,
direction.clone().normalize()
);
instanceDummy.scale.set(1, length + 0.018, 1);
instanceDummy.updateMatrix();
tubeSegments.setMatrixAt(i, instanceDummy.matrix);
}
instanceDummy.scale.set(1, 1, 1);
tubeSegments.instanceMatrix.needsUpdate = true;
tubeSegments.castShadow = true;
scene.add(tubeSegments);
// Pickup foot and visible coupling near the housing.
addBox(
0.18,
0.13,
0.18,
0.18,
0.41,
0.02,
tubeMaterial
);
addBox(
0.22,
0.23,
0.22,
0,
4.58,
0.025,
hingeMaterial
);
// ------------------------------------------------------------
// Opaque pump housing with a small front/right cutaway
// ------------------------------------------------------------
// Back panel.
addBox(1.48, 0.84, 0.18, 0, 5.13, -0.39, housingMaterial);
// Solid left wall.
addBox(0.17, 0.84, 0.86, -0.65, 5.13, 0, housingMaterial);
// Right wall is interrupted near the front to form a cutaway.
addBox(0.17, 0.84, 0.4, 0.65, 5.13, -0.21, housingMaterial);
addBox(0.17, 0.19, 0.34, 0.65, 4.79, 0.32, housingMaterial);
addBox(0.17, 0.17, 0.34, 0.65, 5.48, 0.32, housingMaterial);
// Top and bottom.
addBox(1.48, 0.17, 0.88, 0, 5.55, 0, housingMaterial);
addBox(1.48, 0.16, 0.86, 0, 4.72, 0, housingMaterial);
// Front frame. The center-right opening exposes the hinge.
addBox(1.32, 0.18, 0.14, 0, 5.42, 0.45, housingMaterial);
addBox(0.18, 0.49, 0.14, -0.57, 5.09, 0.45, housingMaterial);
addBox(1.32, 0.15, 0.14, 0, 4.81, 0.45, housingMaterial);
// Thin contrasting cut edges.
addBox(0.045, 0.45, 0.045, -0.465, 5.11, 0.535, housingEdgeMaterial);
addBox(0.98, 0.045, 0.045, 0.015, 5.325, 0.535, housingEdgeMaterial);
addBox(0.045, 0.16, 0.045, 0.555, 5.13, 0.535, housingEdgeMaterial);
// Pump piston visible through the cutaway.
addBox(
0.27,
0.43,
0.27,
-0.3,
5.13,
0.06,
housingEdgeMaterial
);
// Small internal spring made from closely spaced cubes.
for (let i = 0; i < 9; i++) {
addBox(
0.085,
0.07,
0.085,
0.4 + (i % 2 ? 0.035 : -0.035),
4.92 + i * 0.057,
0.1,
hingeMaterial
);
}
// Horizontal hinge pin.
const hinge = new THREE.Mesh(
new THREE.CylinderGeometry(0.115, 0.115, 1.12, 16),
hingeMaterial
);
hinge.position.set(0, 4.91, 0.54);
hinge.rotation.z = Math.PI / 2;
hinge.castShadow = true;
scene.add(hinge);
// Dense voxel detailing around the hinge.
for (let i = -5; i <= 5; i++) {
addBox(
0.075,
0.075,
0.075,
i * 0.105,
4.91 + Math.sin(i * 0.72) * 0.075,
0.55,
i % 2 === 0 ? hingeMaterial : housingEdgeMaterial
);
}
for (let i = 0; i < 5; i++) {
addBox(
0.075,
0.075,
0.075,
-0.43 + i * 0.205,
4.79 + (i % 2) * 0.025,
0.51,
housingEdgeMaterial
);
}
// ------------------------------------------------------------
// Curved voxel trigger and hinge pivot
// ------------------------------------------------------------
const triggerPivot = new THREE.Group();
triggerPivot.position.set(0, 4.91, 0.57);
scene.add(triggerPivot);
const triggerCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(0, -0.025, 0.015),
new THREE.Vector3(0, -0.2, 0.22),
new THREE.Vector3(0, -0.43, 0.39),
new THREE.Vector3(0, -0.67, 0.42),
new THREE.Vector3(0, -0.87, 0.27)
]);
const triggerCount = 29;
const trigger = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.35, 0.12, 0.155),
triggerMaterial,
triggerCount
);
for (let i = 0; i < triggerCount; i++) {
const t = i / (triggerCount - 1);
const point = triggerCurve.getPoint(t);
const tangent = triggerCurve.getTangent(t).normalize();
instanceDummy.position.copy(point);
instanceDummy.quaternion.setFromUnitVectors(upAxis, tangent);
instanceDummy.scale.set(1, 1, 1);
instanceDummy.updateMatrix();
trigger.setMatrixAt(i, instanceDummy.matrix);
}
trigger.instanceMatrix.needsUpdate = true;
trigger.castShadow = true;
trigger.receiveShadow = true;
triggerPivot.add(trigger);
// Larger contact pad at the bottom of the trigger.
addBox(
0.39,
0.18,
0.23,
0,
-0.84,
0.25,
triggerHighlightMaterial,
triggerPivot
);
// Invisible, but raycastable, trigger hit volume.
const triggerHitMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
});
const triggerHitbox = new THREE.Mesh(
new THREE.BoxGeometry(0.68, 1.08, 0.76),
triggerHitMaterial
);
triggerHitbox.position.set(0, -0.43, 0.29);
triggerHitbox.userData.trigger = true;
triggerPivot.add(triggerHitbox);
// Force-entry arrow, visible while the trigger is moving.
const forceOrigin = new THREE.Vector3(0.46, -0.45, 0.72);
const forceDirection = forceOrigin.clone().negate().normalize();
const forceArrow = new THREE.ArrowHelper(
forceDirection,
forceOrigin,
0.74,
0xff6738,
0.18,
0.11
);
forceArrow.visible = false;
forceArrow.line.material.transparent = true;
forceArrow.cone.material.transparent = true;
forceArrow.line.material.depthTest = false;
forceArrow.cone.material.depthTest = false;
forceArrow.renderOrder = 20;
triggerPivot.add(forceArrow);
const forceLight = new THREE.PointLight(0xff7b3d, 0, 2.2, 2);
forceLight.position.set(0, 4.93, 0.62);
scene.add(forceLight);
// ------------------------------------------------------------
// Faceted adjustable front nozzle
// ------------------------------------------------------------
const nozzleGroup = new THREE.Group();
nozzleGroup.position.set(0, 5.2, 0.48);
scene.add(nozzleGroup);
const nozzleCollar = new THREE.Mesh(
new THREE.CylinderGeometry(0.34, 0.34, 0.22, 8),
housingEdgeMaterial
);
nozzleCollar.rotation.x = Math.PI / 2;
nozzleCollar.position.z = 0.11;
nozzleCollar.castShadow = true;
nozzleGroup.add(nozzleCollar);
const nozzleTip = new THREE.Mesh(
new THREE.CylinderGeometry(0.25, 0.18, 0.46, 8),
triggerMaterial
);
nozzleTip.rotation.x = Math.PI / 2;
nozzleTip.position.z = 0.42;
nozzleTip.castShadow = true;
nozzleGroup.add(nozzleTip);
const nozzleAperture = new THREE.Mesh(
new THREE.CircleGeometry(0.105, 16),
new THREE.MeshBasicMaterial({ color: 0x18232a })
);
nozzleAperture.position.z = 0.657;
nozzleGroup.add(nozzleAperture);
// Collar grip facets.
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * Math.PI * 2;
addBox(
0.075,
0.11,
0.16,
Math.cos(angle) * 0.31,
Math.sin(angle) * 0.31,
0.13,
i % 2 === 0 ? housingMaterial : triggerHighlightMaterial,
nozzleGroup
);
}
// Small index mark makes the swivel setting readable.
addBox(
0.11,
0.1,
0.13,
0,
0.35,
0.12,
triggerHighlightMaterial,
nozzleGroup
);
const nozzlePickMeshes = [];
nozzleGroup.traverse((object) => {
if (object.isMesh) {
object.userData.nozzle = true;
nozzlePickMeshes.push(object);
}
});
// ------------------------------------------------------------
// Interaction state
// ------------------------------------------------------------
let nozzleAngle = 0;
let triggerTarget = 0;
let triggerAmount = 0;
let spaceHeld = false;
let drag = null;
const nozzleSlider = document.querySelector("#nozzleAngle");
const nozzleValue = document.querySelector("#nozzleValue");
const squeezeButton = document.querySelector("#squeezeButton");
const resetButton = document.querySelector("#resetButton");
function setNozzleAngle(radians) {
nozzleAngle = THREE.MathUtils.clamp(radians, -0.96, 0.96);
nozzleGroup.rotation.y = nozzleAngle;
const degrees = Math.round(THREE.MathUtils.radToDeg(nozzleAngle));
nozzleSlider.value = String(degrees);
nozzleValue.textContent = `${degrees}°`;
}
nozzleSlider.addEventListener("input", () => {
setNozzleAngle(
THREE.MathUtils.degToRad(Number(nozzleSlider.value))
);
});
squeezeButton.addEventListener("pointerdown", (event) => {
event.preventDefault();
squeezeButton.setPointerCapture(event.pointerId);
triggerTarget = 0.26;
});
squeezeButton.addEventListener("pointerup", () => {
triggerTarget = 0;
});
squeezeButton.addEventListener("pointercancel", () => {
triggerTarget = 0;
});
resetButton.addEventListener("click", () => {
cameraRadius = 10.7;
cameraTheta = 0.64;
cameraPhi = 1.25;
updateCamera();
setNozzleAngle(0);
triggerTarget = 0;
});
window.addEventListener("keydown", (event) => {
if (event.code !== "Space") return;
if (event.target instanceof HTMLInputElement) return;
event.preventDefault();
spaceHeld = true;
triggerTarget = 0.26;
});
window.addEventListener("keyup", (event) => {
if (event.code !== "Space") return;
spaceHeld = false;
triggerTarget = 0;
});
// ------------------------------------------------------------
// Custom pointer interaction and camera controls
// ------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
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) => {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
const triggerHits = raycaster.intersectObject(triggerHitbox, false);
const nozzleHits = raycaster.intersectObjects(nozzlePickMeshes, false);
if (triggerHits.length) {
drag = {
mode: "trigger",
startY: event.clientY
};
triggerTarget = 0;
} else if (nozzleHits.length) {
drag = {
mode: "nozzle",
startX: event.clientX,
startAngle: nozzleAngle
};
} else {
drag = {
mode: "orbit",
startX: event.clientX,
startY: event.clientY,
startTheta: cameraTheta,
startPhi: cameraPhi
};
}
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = drag.mode === "orbit" ? "grabbing" : "pointer";
});
canvas.addEventListener("pointermove", (event) => {
if (!drag) {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
const overTrigger = raycaster.intersectObject(triggerHitbox, false);
const overNozzle = raycaster.intersectObjects(nozzlePickMeshes, false);
canvas.style.cursor =
overTrigger.length || overNozzle.length ? "pointer" : "grab";
return;
}
if (drag.mode === "orbit") {
const dx = event.clientX - drag.startX;
const dy = event.clientY - drag.startY;
cameraTheta = drag.startTheta - dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(
drag.startPhi + dy * 0.005,
0.48,
1.48
);
updateCamera();
} else if (drag.mode === "trigger") {
const upwardPixels = drag.startY - event.clientY;
triggerTarget = THREE.MathUtils.clamp(
upwardPixels / 105,
0,
0.26
);
} else if (drag.mode === "nozzle") {
const dx = event.clientX - drag.startX;
setNozzleAngle(drag.startAngle + dx * 0.007);
}
});
function endPointerInteraction(event) {
if (!drag) return;
if (drag.mode === "trigger") {
triggerTarget = 0;
}
drag = null;
canvas.style.cursor = "grab";
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endPointerInteraction);
canvas.addEventListener("pointercancel", endPointerInteraction);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.008,
7.2,
16
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
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();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
triggerAmount = THREE.MathUtils.damp(
triggerAmount,
triggerTarget,
13,
delta
);
// Positive X rotation pulls the trigger inward toward the hinge.
triggerPivot.rotation.x = triggerAmount;
const forceVisibility = THREE.MathUtils.clamp(
triggerAmount / 0.08,
0,
1
);
forceArrow.visible = forceVisibility > 0.015;
forceArrow.line.material.opacity = forceVisibility;
forceArrow.cone.material.opacity = forceVisibility;
hingeMaterial.emissiveIntensity = 0.12 + triggerAmount * 5.2;
forceLight.intensity = triggerAmount * 4.5;
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```