Prompt specification
Build or customize the Three.js 3D scene "Water oil and surfactant concept tray" 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: Water oil and surfactant concept tray
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/water-oil-and-surfactant-concept-tray-x1978
Scene Description Prompt:
Please use Three.js from cdnjs for a cutaway scene based on this idea: water oil and surfactant concept tray. Arrange separate liquid regions and schematic amphiphilic molecules at their interface; label the molecular scale illustrative and avoid claiming all surfactants form identical structures. Return one self-contained HTML file.
Working Three.js Starter (water-oil-and-surfactant-concept-tray-x1978):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Water, Oil & Surfactant — Cutaway Concept Tray</title>
<style>
:root {
color-scheme: dark;
--bg: #07111d;
--panel: rgba(9, 22, 36, 0.82);
--panel-strong: rgba(9, 22, 36, 0.94);
--line: rgba(170, 218, 244, 0.18);
--text: #e9f7ff;
--muted: #9cb8c9;
--water: #39b9f2;
--oil: #f1aa43;
--head: #9cecff;
--tail: #ff8275;
--accent: #89e7ff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 52% 35%, rgba(36, 90, 116, 0.22), transparent 38%),
radial-gradient(circle at 80% 75%, rgba(177, 104, 33, 0.10), transparent 35%),
linear-gradient(145deg, #0a1826 0%, var(--bg) 58%, #050b13 100%);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(16px) saturate(125%);
-webkit-backdrop-filter: blur(16px) saturate(125%);
}
.intro {
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 21px 22px 20px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.eyebrow::before {
width: 22px;
height: 1px;
content: "";
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
h1 {
margin: 0;
font-size: clamp(27px, 3.1vw, 40px);
font-weight: 660;
line-height: 1.02;
letter-spacing: -0.045em;
}
h1 span {
color: var(--accent);
font-weight: 420;
}
.intro p {
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.scale-badge {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 16px;
padding: 7px 9px;
border: 1px solid rgba(137, 231, 255, 0.22);
border-radius: 999px;
background: rgba(76, 189, 224, 0.08);
color: #bcefff;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.scale-badge::before {
width: 6px;
height: 6px;
border-radius: 50%;
content: "";
background: var(--accent);
box-shadow: 0 0 10px var(--accent);
}
.controls {
top: 22px;
right: 22px;
width: 244px;
padding: 15px;
}
.control-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 11px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.control-heading span:last-child {
color: var(--accent);
}
.control-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.control-button {
min-height: 36px;
padding: 8px 10px;
border: 1px solid rgba(165, 216, 242, 0.16);
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
color: #d7ebf6;
cursor: pointer;
font-size: 11px;
font-weight: 650;
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
.control-button:hover {
border-color: rgba(137, 231, 255, 0.48);
background: rgba(83, 193, 225, 0.10);
}
.control-button:active {
transform: translateY(1px);
}
.control-button[aria-pressed="true"] {
border-color: rgba(137, 231, 255, 0.38);
background: rgba(64, 185, 219, 0.14);
color: #e7fbff;
}
.control-button.wide {
grid-column: 1 / -1;
}
.gesture-note {
margin: 11px 2px 0;
color: #829eae;
font-size: 10px;
line-height: 1.5;
}
.legend {
bottom: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 15px 17px;
}
.legend-title {
margin-bottom: 11px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.legend-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px 15px;
}
.legend-item {
display: flex;
align-items: center;
gap: 9px;
min-width: 0;
color: #c9dde8;
font-size: 11px;
line-height: 1.25;
}
.swatch {
flex: 0 0 auto;
width: 13px;
height: 13px;
border-radius: 4px;
box-shadow: 0 0 12px rgba(255, 255, 255, 0.07);
}
.swatch.water {
background: linear-gradient(135deg, #56d2ff, #126da8);
}
.swatch.oil {
background: linear-gradient(135deg, #ffd07a, #b96820);
}
.swatch.head {
border-radius: 50%;
background: var(--head);
box-shadow: 0 0 12px rgba(156, 236, 255, 0.55);
}
.swatch.tail {
width: 6px;
height: 17px;
margin: 0 4px;
border-radius: 999px;
background: var(--tail);
transform: rotate(16deg);
}
.caution {
right: 22px;
bottom: 22px;
width: min(430px, calc(100vw - 44px));
padding: 15px 17px;
border-color: rgba(241, 170, 67, 0.23);
background: rgba(30, 24, 17, 0.78);
}
.caution-heading {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
color: #ffd797;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.caution-heading::before {
width: 7px;
height: 7px;
border: 1px solid currentColor;
border-radius: 50%;
content: "";
}
.caution p {
margin: 0;
color: #c8bda9;
font-size: 11px;
line-height: 1.55;
}
#leaders {
position: fixed;
inset: 0;
z-index: 3;
width: 100%;
height: 100%;
overflow: visible;
pointer-events: none;
}
#leaders line {
stroke: rgba(169, 224, 246, 0.42);
stroke-width: 1;
stroke-dasharray: 3 4;
}
#leaders circle {
fill: #b9efff;
stroke: rgba(185, 239, 255, 0.4);
stroke-width: 4;
paint-order: stroke;
}
.scene-label {
position: fixed;
z-index: 4;
transform: translate(-50%, -50%);
padding: 7px 10px;
border: 1px solid rgba(169, 224, 246, 0.22);
border-radius: 8px;
background: rgba(7, 17, 29, 0.82);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.24);
color: #e5f6ff;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.015em;
white-space: nowrap;
pointer-events: none;
}
.scene-label::before {
display: inline-block;
width: 6px;
height: 6px;
margin-right: 7px;
border-radius: 50%;
content: "";
background: var(--label-color, #8fe8ff);
box-shadow: 0 0 9px var(--label-color, #8fe8ff);
}
.status {
position: fixed;
left: 50%;
bottom: 24px;
z-index: 5;
transform: translateX(-50%);
padding: 7px 12px;
border: 1px solid rgba(165, 216, 242, 0.14);
border-radius: 999px;
background: rgba(7, 17, 29, 0.68);
color: #88a8b9;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.14em;
text-transform: uppercase;
pointer-events: none;
}
#loading {
position: fixed;
inset: 0;
z-index: 30;
display: grid;
place-items: center;
background: #07111d;
color: #aeeeff;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.15em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.intro {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 15px 16px;
}
h1 {
font-size: 27px;
}
.intro p {
display: none;
}
.scale-badge {
margin-top: 11px;
}
.controls {
top: auto;
right: 12px;
bottom: 12px;
width: 208px;
}
.gesture-note {
display: none;
}
.legend {
bottom: 12px;
left: 12px;
width: calc(100vw - 244px);
min-width: 142px;
padding: 12px;
}
.legend-grid {
grid-template-columns: 1fr;
}
.legend-item:nth-child(n + 3) {
display: none;
}
.caution {
right: 12px;
bottom: 145px;
width: min(390px, calc(100vw - 24px));
padding: 11px 13px;
}
.caution p {
font-size: 10px;
}
.status {
display: none;
}
.scene-label {
font-size: 9px;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive 3D cutaway of water, oil, and surfactant at an interface"></main>
<svg id="leaders" aria-hidden="true"></svg>
<div
id="label-water"
class="scene-label"
style="--label-color: #39b9f2"
>Water-rich phase</div>
<div
id="label-oil"
class="scene-label"
style="--label-color: #f1aa43"
>Oil-rich phase</div>
<div
id="label-interface"
class="scene-label"
style="--label-color: #d8f7ff"
>Oil–water interface</div>
<div
id="label-molecule"
class="scene-label"
style="--label-color: #ff8275"
>Representative amphiphile motif</div>
<section class="panel intro" aria-labelledby="page-title">
<div class="eyebrow">Concept tray · Cutaway 01</div>
<h1 id="page-title">Water <span>/</span> Oil <span>/</span> Surfactant</h1>
<p>
Separate water-rich and oil-rich regions meet at a planar interface.
Schematic amphiphiles are positioned with water-affine heads and
oil-affine tails.
</p>
<div class="scale-badge">Molecular scale · illustrative</div>
</section>
<section class="panel controls" aria-label="Scene controls">
<div class="control-heading">
<span>Scene controls</span>
<span>Live</span>
</div>
<div class="control-grid">
<button id="toggle-cutaway" class="control-button" type="button" aria-pressed="true">
Cutaway on
</button>
<button id="toggle-molecules" class="control-button" type="button" aria-pressed="true">
Molecules on
</button>
<button id="reset-view" class="control-button wide" type="button">
Reset camera
</button>
</div>
<p class="gesture-note">
Drag to orbit · wheel/pinch to zoom · arrow keys rotate · double-click to reset
</p>
</section>
<section class="panel legend" aria-label="Color legend">
<div class="legend-title">Reading the tray</div>
<div class="legend-grid">
<div class="legend-item">
<span class="swatch water"></span>
<span>Water-rich phase</span>
</div>
<div class="legend-item">
<span class="swatch oil"></span>
<span>Oil-rich phase</span>
</div>
<div class="legend-item">
<span class="swatch head"></span>
<span>Illustrative polar head</span>
</div>
<div class="legend-item">
<span class="swatch tail"></span>
<span>Illustrative nonpolar tail</span>
</div>
</div>
</section>
<aside class="panel caution" aria-label="Scientific interpretation note">
<div class="caution-heading">Schematic, not a universal structure</div>
<p>
The molecular scale and packing are illustrative. This scene shows one
common schematic motif; surfactants do not all form identical structures.
Headgroups, tail number and length, branching, charge, and packing vary
with chemistry and conditions.
</p>
</aside>
<div class="status">Interactive 3D · schematic · not to scale</div>
<div id="loading">Preparing the concept tray…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const host = document.getElementById("scene");
const loading = document.getElementById("loading");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.16;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Orbitable 3D cutaway showing water, oil, and schematic amphiphiles"
);
host.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x07111d, 0.027);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
80
);
const desiredCamera = {
theta: 0.67,
phi: 1.05,
radius: 15.8,
target: new THREE.Vector3(0, -0.32, 0)
};
const currentCamera = {
theta: desiredCamera.theta,
phi: desiredCamera.phi,
radius: desiredCamera.radius,
target: desiredCamera.target.clone()
};
function applyCameraState() {
const sinPhi = Math.sin(currentCamera.phi);
camera.position.set(
currentCamera.target.x +
currentCamera.radius * sinPhi * Math.sin(currentCamera.theta),
currentCamera.target.y +
currentCamera.radius * Math.cos(currentCamera.phi),
currentCamera.target.z +
currentCamera.radius * sinPhi * Math.cos(currentCamera.theta)
);
camera.lookAt(currentCamera.target);
}
applyCameraState();
const hemisphere = new THREE.HemisphereLight(0xc8edff, 0x07101a, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.0);
keyLight.position.set(7, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const coolRim = new THREE.DirectionalLight(0x58cfff, 1.15);
coolRim.position.set(-8, 5, -5);
scene.add(coolRim);
const warmRim = new THREE.PointLight(0xffb24c, 18, 15, 2);
warmRim.position.set(0, 3.5, -3.2);
scene.add(warmRim);
const tray = new THREE.Group();
scene.add(tray);
const phase = {
width: 8.6,
depth: 5.6,
bottom: -2.45,
interface: -0.25,
oilTop: 1.35,
rim: 1.66
};
const stage = new THREE.Mesh(
new THREE.CylinderGeometry(7.7, 7.9, 0.14, 96),
new THREE.MeshStandardMaterial({
color: 0x0b1825,
roughness: 0.82,
metalness: 0.08
})
);
stage.position.y = -2.91;
stage.receiveShadow = true;
scene.add(stage);
const grid = new THREE.GridHelper(22, 44, 0x234354, 0x142a39);
grid.position.y = -2.82;
grid.material.transparent = true;
grid.material.opacity = 0.18;
scene.add(grid);
const baseMaterial = new THREE.MeshPhysicalMaterial({
color: 0x142b3b,
roughness: 0.22,
metalness: 0.18,
transparent: true,
opacity: 0.96,
clearcoat: 0.7,
clearcoatRoughness: 0.28
});
const base = new THREE.Mesh(
new THREE.BoxGeometry(9.25, 0.20, 6.25),
baseMaterial
);
base.position.set(0, -2.55, 0);
base.castShadow = true;
base.receiveShadow = true;
tray.add(base);
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0x244657,
roughness: 0.25,
metalness: 0.62
});
const rimGeometryLong = new THREE.BoxGeometry(9.1, 0.09, 0.11);
const rimGeometrySide = new THREE.BoxGeometry(0.11, 0.09, 6.1);
const backRim = new THREE.Mesh(rimGeometryLong, rimMaterial);
backRim.position.set(0, phase.rim, -3.0);
tray.add(backRim);
const frontRim = new THREE.Mesh(rimGeometryLong, rimMaterial);
frontRim.position.set(0, phase.rim, 3.0);
tray.add(frontRim);
const leftRim = new THREE.Mesh(rimGeometrySide, rimMaterial);
leftRim.position.set(-4.5, phase.rim, 0);
tray.add(leftRim);
const rightRim = new THREE.Mesh(rimGeometrySide, rimMaterial);
rightRim.position.set(4.5, phase.rim, 0);
tray.add(rightRim);
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: 0xa8dded,
roughness: 0.12,
metalness: 0,
transparent: true,
opacity: 0.13,
side: THREE.DoubleSide,
depthWrite: false
});
const wallHeight = phase.rim - phase.bottom;
const wallCenter = (phase.rim + phase.bottom) / 2;
const backWall = new THREE.Mesh(
new THREE.BoxGeometry(9.0, wallHeight, 0.075),
glassMaterial
);
backWall.position.set(0, wallCenter, -3.0);
tray.add(backWall);
const leftWall = new THREE.Mesh(
new THREE.BoxGeometry(0.075, wallHeight, 6.0),
glassMaterial
);
leftWall.position.set(-4.5, wallCenter, 0);
tray.add(leftWall);
const rightWall = new THREE.Mesh(
new THREE.BoxGeometry(0.075, wallHeight, 6.0),
glassMaterial
);
rightWall.position.set(4.5, wallCenter, 0);
tray.add(rightWall);
const frontWall = new THREE.Mesh(
new THREE.BoxGeometry(9.0, wallHeight, 0.075),
glassMaterial
);
frontWall.position.set(0, wallCenter, 3.0);
frontWall.visible = false;
tray.add(frontWall);
const waterVolumeMaterial = new THREE.MeshPhysicalMaterial({
color: 0x1585bd,
roughness: 0.2,
metalness: 0.02,
transparent: true,
opacity: 0.40,
side: THREE.DoubleSide,
depthWrite: false
});
const oilVolumeMaterial = new THREE.MeshPhysicalMaterial({
color: 0xe39a36,
roughness: 0.24,
metalness: 0,
transparent: true,
opacity: 0.38,
side: THREE.DoubleSide,
depthWrite: false
});
const waterHeight = phase.interface - phase.bottom;
const oilHeight = phase.oilTop - phase.interface;
const waterVolume = new THREE.Mesh(
new THREE.BoxGeometry(phase.width, waterHeight, phase.depth),
waterVolumeMaterial
);
waterVolume.position.set(
0,
phase.bottom + waterHeight / 2,
0
);
waterVolume.renderOrder = 0;
tray.add(waterVolume);
const oilVolume = new THREE.Mesh(
new THREE.BoxGeometry(phase.width, oilHeight, phase.depth),
oilVolumeMaterial
);
oilVolume.position.set(
0,
phase.interface + oilHeight / 2,
0
);
oilVolume.renderOrder = 0;
tray.add(oilVolume);
const waterCutMaterial = new THREE.MeshStandardMaterial({
color: 0x0d79b7,
roughness: 0.33,
metalness: 0.04,
transparent: true,
opacity: 0.72,
side: THREE.DoubleSide,
depthWrite: false
});
const oilCutMaterial = new THREE.MeshStandardMaterial({
color: 0xd58b2f,
roughness: 0.34,
metalness: 0.02,
transparent: true,
opacity: 0.66,
side: THREE.DoubleSide,
depthWrite: false
});
const frontWater = new THREE.Mesh(
new THREE.PlaneGeometry(phase.width, waterHeight),
waterCutMaterial
);
frontWater.position.set(0, phase.bottom + waterHeight / 2, 2.81);
frontWater.renderOrder = 1;
tray.add(frontWater);
const frontOil = new THREE.Mesh(
new THREE.PlaneGeometry(phase.width, oilHeight),
oilCutMaterial
);
frontOil.position.set(0, phase.interface + oilHeight / 2, 2.81);
frontOil.renderOrder = 1;
tray.add(frontOil);
const sideWater = new THREE.Mesh(
new THREE.PlaneGeometry(phase.depth, waterHeight),
waterCutMaterial
);
sideWater.rotation.y = Math.PI / 2;
sideWater.position.set(4.31, phase.bottom + waterHeight / 2, 0);
sideWater.renderOrder = 1;
tray.add(sideWater);
const sideOil = new THREE.Mesh(
new THREE.PlaneGeometry(phase.depth, oilHeight),
oilCutMaterial
);
sideOil.rotation.y = Math.PI / 2;
sideOil.position.set(4.31, phase.interface + oilHeight / 2, 0);
sideOil.renderOrder = 1;
tray.add(sideOil);
const oilSurface = new THREE.Mesh(
new THREE.PlaneGeometry(phase.width, phase.depth),
new THREE.MeshPhysicalMaterial({
color: 0xf0ac4a,
roughness: 0.16,
transparent: true,
opacity: 0.48,
side: THREE.DoubleSide,
depthWrite: false
})
);
oilSurface.rotation.x = -Math.PI / 2;
oilSurface.position.y = phase.oilTop;
oilSurface.renderOrder = 1;
tray.add(oilSurface);
const interfaceSurface = new THREE.Mesh(
new THREE.PlaneGeometry(phase.width, phase.depth),
new THREE.MeshBasicMaterial({
color: 0xb8f2ff,
transparent: true,
opacity: 0.13,
side: THREE.DoubleSide,
depthWrite: false
})
);
interfaceSurface.rotation.x = -Math.PI / 2;
interfaceSurface.position.y = phase.interface;
interfaceSurface.renderOrder = 2;
tray.add(interfaceSurface);
const boundaryMaterial = new THREE.MeshBasicMaterial({
color: 0xc8f5ff,
transparent: true,
opacity: 0.66
});
const frontBoundary = new THREE.Mesh(
new THREE.BoxGeometry(phase.width, 0.025, 0.028),
boundaryMaterial
);
frontBoundary.position.set(0, phase.interface, 2.84);
frontBoundary.renderOrder = 4;
tray.add(frontBoundary);
const sideBoundary = new THREE.Mesh(
new THREE.BoxGeometry(0.028, 0.025, phase.depth),
boundaryMaterial
);
sideBoundary.position.set(4.34, phase.interface, 0);
sideBoundary.renderOrder = 4;
tray.add(sideBoundary);
const vesselEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(9.0, wallHeight, 6.0)),
new THREE.LineBasicMaterial({
color: 0x9ed6e8,
transparent: true,
opacity: 0.18
})
);
vesselEdges.position.set(0, wallCenter, 0);
tray.add(vesselEdges);
const moleculeRoot = new THREE.Group();
tray.add(moleculeRoot);
const headMaterials = [
new THREE.MeshStandardMaterial({
color: 0xa9f0ff,
emissive: 0x075f78,
emissiveIntensity: 0.42,
roughness: 0.28,
metalness: 0.04
}),
new THREE.MeshStandardMaterial({
color: 0x75dfff,
emissive: 0x064e68,
emissiveIntensity: 0.36,
roughness: 0.3,
metalness: 0.04
}),
new THREE.MeshStandardMaterial({
color: 0xd5f8ff,
emissive: 0x176375,
emissiveIntensity: 0.28,
roughness: 0.26,
metalness: 0.02
})
];
const tailMaterial = new THREE.MeshStandardMaterial({
color: 0xff8275,
emissive: 0x41100d,
emissiveIntensity: 0.22,
roughness: 0.42,
metalness: 0.02
});
const bondMaterial = new THREE.MeshStandardMaterial({
color: 0xf2f7f8,
roughness: 0.32,
metalness: 0.05
});
const up = new THREE.Vector3(0, 1, 0);
const segmentDirection = new THREE.Vector3();
const segmentMidpoint = new THREE.Vector3();
function addSegment(parent, start, end, radius, material) {
segmentDirection.subVectors(end, start);
const length = segmentDirection.length();
segmentMidpoint.addVectors(start, end).multiplyScalar(0.5);
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius * 0.92, length, 8),
material
);
mesh.position.copy(segmentMidpoint);
mesh.quaternion.setFromUnitVectors(up, segmentDirection.normalize());
mesh.castShadow = true;
mesh.renderOrder = 3;
parent.add(mesh);
return mesh;
}
function addJoint(parent, point, material, radius = 0.053) {
const joint = new THREE.Mesh(
new THREE.SphereGeometry(radius, 10, 8),
material
);
joint.position.copy(point);
joint.castShadow = true;
joint.renderOrder = 3;
parent.add(joint);
}
function createAmphiphile(variant, headIndex) {
const molecule = new THREE.Group();
const headGeometry =
variant % 3 === 0
? new THREE.SphereGeometry(0.135, 14, 10)
: new THREE.IcosahedronGeometry(0.14, 1);
const head = new THREE.Mesh(headGeometry, headMaterials[headIndex]);
head.position.set(0, -0.17, 0);
head.castShadow = true;
head.renderOrder = 4;
molecule.add(head);
const points = [];
const tailLength = 0.78 + (variant % 4) * 0.075;
if (variant % 3 === 0) {
points.push(
new THREE.Vector3(0, 0.01, 0),
new THREE.Vector3(0.015, 0.26, 0),
new THREE.Vector3(-0.025, 0.53, 0.015),
new THREE.Vector3(0.035, tailLength, 0)
);
} else if (variant % 3 === 1) {
points.push(
new THREE.Vector3(0, 0.01, 0),
new THREE.Vector3(-0.075, 0.25, 0),
new THREE.Vector3(0.075, 0.49, 0.025),
new THREE.Vector3(0.015, tailLength, -0.015)
);
} else {
points.push(
new THREE.Vector3(0, 0.01, 0),
new THREE.Vector3(0.005, 0.27, 0),
new THREE.Vector3(0.035, 0.55, 0.015),
new THREE.Vector3(0.13, tailLength, 0.01)
);
}
addSegment(
molecule,
new THREE.Vector3(0, -0.06, 0),
points[0],
0.033,
bondMaterial
);
for (let i = 0; i < points.length - 1; i++) {
addSegment(molecule, points[i], points[i + 1], 0.038, tailMaterial);
if (i > 0) addJoint(molecule, points[i], tailMaterial);
}
if (variant % 3 === 2) {
const branchStart = points[1].clone();
const branchEnd = new THREE.Vector3(-0.20, 0.49, 0.025);
addSegment(molecule, branchStart, branchEnd, 0.032, tailMaterial);
addJoint(molecule, branchEnd, tailMaterial, 0.047);
}
const scale = 0.88 + ((variant * 37) % 19) / 100;
molecule.scale.setScalar(scale);
return molecule;
}
let moleculeIndex = 0;
const rows = [-2.12, -0.72, 0.72, 2.08];
const columns = [-3.48, -2.32, -1.16, 0.0, 1.16, 2.32, 3.48];
for (let row = 0; row < rows.length; row++) {
for (let column = 0; column < columns.length; column++) {
const molecule = createAmphiphile(
moleculeIndex,
moleculeIndex % headMaterials.length
);
molecule.position.set(
columns[column] + (((moleculeIndex * 17) % 11) - 5) * 0.012,
phase.interface + (((moleculeIndex * 7) % 5) - 2) * 0.007,
rows[row] + (((moleculeIndex * 13) % 9) - 4) * 0.014
);
molecule.rotation.x = (((moleculeIndex * 23) % 17) - 8) * 0.012;
molecule.rotation.y = (((moleculeIndex * 11) % 19) - 9) * 0.009;
molecule.rotation.z = (((moleculeIndex * 29) % 23) - 11) * 0.010;
moleculeRoot.add(molecule);
moleculeIndex++;
}
}
const labelDefinitions = [
{
element: document.getElementById("label-water"),
anchor: new THREE.Vector3(-2.75, -1.32, 2.86),
offset: new THREE.Vector2(-105, 34)
},
{
element: document.getElementById("label-oil"),
anchor: new THREE.Vector3(2.55, 0.72, 2.86),
offset: new THREE.Vector2(106, -35)
},
{
element: document.getElementById("label-interface"),
anchor: new THREE.Vector3(-0.65, phase.interface, 2.47),
offset: new THREE.Vector2(-118, -62)
},
{
element: document.getElementById("label-molecule"),
anchor: new THREE.Vector3(2.75, -0.12, 2.08),
offset: new THREE.Vector2(135, 39)
}
];
const svgNS = "http://www.w3.org/2000/svg";
for (const definition of labelDefinitions) {
const group = document.createElementNS(svgNS, "g");
const line = document.createElementNS(svgNS, "line");
const circle = document.createElementNS(svgNS, "circle");
circle.setAttribute("r", "2.5");
group.append(line, circle);
document.getElementById("leaders").appendChild(group);
definition.line = line;
definition.circle = circle;
}
const projected = new THREE.Vector3();
function updateLabels() {
for (const definition of labelDefinitions) {
projected.copy(definition.anchor).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
definition.element.style.display !== "none";
definition.element.style.opacity = visible ? "1" : "0";
definition.line.style.opacity = visible ? "1" : "0";
definition.circle.style.opacity = visible ? "1" : "0";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
const labelX = x + definition.offset.x;
const labelY = y + definition.offset.y;
definition.element.style.left = `${labelX}px`;
definition.element.style.top = `${labelY}px`;
definition.line.setAttribute("x1", x);
definition.line.setAttribute("y1", y);
definition.line.setAttribute("x2", labelX);
definition.line.setAttribute("y2", labelY);
definition.circle.setAttribute("cx", x);
definition.circle.setAttribute("cy", y);
}
}
function resetView() {
const compact = window.innerWidth / window.innerHeight < 1;
desiredCamera.theta = 0.67;
desiredCamera.phi = 1.05;
desiredCamera.radius = compact ? 18.7 : 15.8;
desiredCamera.target.set(0, -0.32, 0);
}
function clampCamera() {
desiredCamera.phi = THREE.MathUtils.clamp(desiredCamera.phi, 0.32, 1.40);
desiredCamera.radius = THREE.MathUtils.clamp(desiredCamera.radius, 10.5, 24);
}
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.focus({ preventScroll: true });
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
desiredCamera.theta -= deltaX * 0.0052;
desiredCamera.phi -= deltaY * 0.0048;
clampCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredCamera.radius *= Math.exp(event.deltaY * 0.001);
clampCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
renderer.domElement.addEventListener("keydown", (event) => {
const keyStep = 0.08;
if (event.key === "ArrowLeft") {
desiredCamera.theta -= keyStep;
} else if (event.key === "ArrowRight") {
desiredCamera.theta += keyStep;
} else if (event.key === "ArrowUp") {
desiredCamera.phi -= keyStep;
} else if (event.key === "ArrowDown") {
desiredCamera.phi += keyStep;
} else if (event.key === "+" || event.key === "=") {
desiredCamera.radius -= 0.6;
} else if (event.key === "-" || event.key === "_") {
desiredCamera.radius += 0.6;
} else if (event.key === "Home") {
resetView();
} else {
return;
}
event.preventDefault();
clampCamera();
});
const cutawayButton = document.getElementById("toggle-cutaway");
const moleculesButton = document.getElementById("toggle-molecules");
const resetButton = document.getElementById("reset-view");
let cutawayOn = true;
let moleculesOn = true;
cutawayButton.addEventListener("click", () => {
cutawayOn = !cutawayOn;
frontWall.visible = !cutawayOn;
cutawayButton.setAttribute("aria-pressed", String(cutawayOn));
cutawayButton.textContent = cutawayOn ? "Cutaway on" : "Cutaway off";
});
moleculesButton.addEventListener("click", () => {
moleculesOn = !moleculesOn;
moleculeRoot.visible = moleculesOn;
moleculesButton.setAttribute("aria-pressed", String(moleculesOn));
moleculesButton.textContent = moleculesOn ? "Molecules on" : "Molecules off";
document
.getElementById("label-molecule")
.style.setProperty("display", moleculesOn ? "block" : "none");
});
resetButton.addEventListener("click", resetView);
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
renderer.setAnimationLoop(() => {
const damping = 0.085;
currentCamera.theta = THREE.MathUtils.lerp(
currentCamera.theta,
desiredCamera.theta,
damping
);
currentCamera.phi = THREE.MathUtils.lerp(
currentCamera.phi,
desiredCamera.phi,
damping
);
currentCamera.radius = THREE.MathUtils.lerp(
currentCamera.radius,
desiredCamera.radius,
damping
);
currentCamera.target.lerp(desiredCamera.target, damping);
applyCameraState();
updateLabels();
renderer.render(scene, camera);
});
renderer.render(scene, camera);
requestAnimationFrame(() => loading.classList.add("hidden"));
</script>
</body>
</html>
```