Prompt specification
Build or customize the Three.js 3D scene "Stylized Head Looking at Three Targets" 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: Stylized Head Looking at Three Targets
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/stylized-head-looking-at-three-targets-x3725
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows gaze direction toward selectable targets. Model a simple head with visible pupils and three target markers. Align eye directions with selected targets while respecting a bounded schematic eye-rotation range. Stylized solid forms.
Working Three.js Starter (stylized-head-looking-at-three-targets-x3725):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#071318" />
<title>Gaze Lab — Selectable Target Tracking</title>
<style>
:root {
--bg: #071318;
--panel: rgba(11, 27, 32, 0.82);
--panel-border: rgba(184, 255, 234, 0.14);
--text: #eef8f5;
--muted: #8ba49f;
--accent: #68e0d0;
--target-color: #68e0d0;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 35%, #12272b 0%, #09181d 45%, #061116 100%);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#app {
position: relative;
width: 100%;
height: 100%;
isolation: isolate;
}
#scene {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
#markerLayer {
position: absolute;
inset: 0;
pointer-events: none;
overflow: hidden;
}
.marker-label {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -50%);
padding: 7px 10px;
border: 1px solid color-mix(in srgb, var(--c) 48%, transparent);
border-radius: 999px;
background: rgba(5, 15, 18, 0.78);
color: var(--c);
box-shadow:
0 8px 30px rgba(0, 0, 0, 0.28),
0 0 20px color-mix(in srgb, var(--c) 12%, transparent);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.16em;
white-space: nowrap;
pointer-events: auto;
cursor: pointer;
opacity: 0.62;
transition:
opacity 180ms ease,
transform 180ms ease,
background 180ms ease;
}
.marker-label.active {
opacity: 1;
background: rgba(6, 18, 21, 0.94);
box-shadow:
0 8px 30px rgba(0, 0, 0, 0.3),
0 0 24px color-mix(in srgb, var(--c) 20%, transparent);
}
.marker-label:hover {
opacity: 1;
}
.topbar {
position: absolute;
top: 0;
left: 0;
right: 0;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 24px;
padding: 26px 30px;
pointer-events: none;
z-index: 5;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
}
.brand-mark {
position: relative;
width: 42px;
height: 42px;
border: 1px solid rgba(104, 224, 208, 0.35);
border-radius: 50%;
box-shadow: inset 0 0 18px rgba(104, 224, 208, 0.08);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
top: 50%;
left: 50%;
border-radius: 50%;
transform: translate(-50%, -50%);
}
.brand-mark::before {
width: 14px;
height: 14px;
background: var(--accent);
box-shadow: 0 0 18px rgba(104, 224, 208, 0.7);
}
.brand-mark::after {
width: 31px;
height: 31px;
border: 1px dashed rgba(104, 224, 208, 0.45);
}
.eyebrow {
margin: 0 0 4px;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.24em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(20px, 2.2vw, 30px);
font-weight: 620;
letter-spacing: -0.035em;
}
.subtitle {
margin: 5px 0 0;
color: var(--muted);
font-size: 12px;
letter-spacing: 0.02em;
}
.range-badge {
padding: 10px 13px;
border: 1px solid var(--panel-border);
border-radius: 12px;
background: rgba(8, 23, 27, 0.66);
backdrop-filter: blur(14px);
text-align: right;
}
.range-badge strong {
display: block;
margin-top: 3px;
color: var(--text);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.08em;
}
.range-badge span {
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.telemetry {
position: absolute;
top: 112px;
left: 30px;
width: 236px;
padding: 17px;
border: 1px solid var(--panel-border);
border-radius: 17px;
background: var(--panel);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(18px);
pointer-events: none;
z-index: 4;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 16px;
}
.panel-heading span:first-child {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.tracking-state {
display: flex;
align-items: center;
gap: 6px;
color: var(--target-color);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
}
.tracking-state::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
.target-name {
margin: 0;
font-size: 24px;
font-weight: 620;
letter-spacing: -0.035em;
}
.target-zone {
margin: 4px 0 17px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.angle-row {
margin-top: 14px;
}
.angle-label {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 8px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.angle-value {
color: var(--text);
font-variant-numeric: tabular-nums;
font-size: 11px;
letter-spacing: 0;
}
.track {
position: relative;
height: 4px;
border-radius: 999px;
background: rgba(255, 255, 255, 0.08);
}
.track::before {
content: "";
position: absolute;
top: -3px;
bottom: -3px;
left: 50%;
width: 1px;
background: rgba(255, 255, 255, 0.16);
}
.needle {
position: absolute;
top: 50%;
left: 50%;
width: 9px;
height: 9px;
border: 2px solid #0a191d;
border-radius: 50%;
background: var(--target-color);
box-shadow: 0 0 12px var(--target-color);
transform: translate(-50%, -50%);
transition: background 180ms ease;
}
.track-labels {
display: flex;
justify-content: space-between;
margin-top: 6px;
color: #526a67;
font-size: 8px;
font-variant-numeric: tabular-nums;
}
.envelope {
margin-top: 18px;
padding-top: 14px;
border-top: 1px solid rgba(255, 255, 255, 0.07);
color: #708985;
font-size: 9px;
line-height: 1.6;
}
.envelope strong {
color: #a7c0bb;
font-weight: 700;
}
.controls {
position: absolute;
left: 50%;
bottom: 24px;
display: flex;
flex-direction: column;
align-items: center;
gap: 12px;
transform: translateX(-50%);
z-index: 6;
}
.control-hint {
color: #7e9994;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
pointer-events: none;
text-shadow: 0 2px 8px #000;
}
.target-selector {
display: flex;
gap: 8px;
padding: 8px;
border: 1px solid var(--panel-border);
border-radius: 17px;
background: rgba(8, 23, 27, 0.82);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(18px);
}
.target-button {
min-width: 132px;
padding: 11px 14px;
border: 1px solid transparent;
border-radius: 11px;
background: transparent;
color: var(--muted);
text-align: left;
cursor: pointer;
transition:
color 180ms ease,
background 180ms ease,
border-color 180ms ease,
transform 180ms ease;
}
.target-button:hover {
color: var(--text);
background: rgba(255, 255, 255, 0.035);
transform: translateY(-1px);
}
.target-button.active {
color: var(--text);
border-color: rgba(255, 255, 255, 0.1);
background: rgba(255, 255, 255, 0.07);
}
.button-top {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 4px;
font-size: 11px;
font-weight: 780;
letter-spacing: 0.08em;
}
.button-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--c);
box-shadow: 0 0 9px var(--c);
}
.button-sub {
padding-left: 15px;
color: #66807b;
font-size: 8px;
font-weight: 750;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.keycap {
float: right;
padding: 1px 5px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 4px;
color: #6f8984;
font-size: 8px;
line-height: 1.5;
}
@media (max-width: 760px) {
.topbar {
padding: 18px;
}
.brand-mark {
width: 36px;
height: 36px;
}
.subtitle,
.range-badge {
display: none;
}
.telemetry {
top: 86px;
left: 18px;
width: 178px;
padding: 13px;
}
.target-name {
font-size: 19px;
}
.target-zone {
margin-bottom: 12px;
}
.ranges {
display: none;
}
.envelope {
margin-top: 12px;
padding-top: 10px;
}
.controls {
left: 12px;
right: 12px;
bottom: 12px;
transform: none;
}
.target-selector {
width: 100%;
}
.target-button {
min-width: 0;
flex: 1;
padding: 10px 8px;
}
.button-sub {
display: none;
}
.control-hint {
font-size: 8px;
}
.marker-label {
padding: 5px 7px;
font-size: 8px;
}
}
</style>
</head>
<body>
<main id="app">
<canvas id="scene" aria-label="Interactive 3D gaze tracking demonstration"></canvas>
<div id="markerLayer"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<p class="eyebrow">Ocular Systems / 03</p>
<h1>Gaze Lab</h1>
<p class="subtitle">Convergent gaze mapped to selectable spatial targets</p>
</div>
</div>
<div class="range-badge">
<span>Schematic eye envelope</span>
<strong>±52° HORIZONTAL / ±30° VERTICAL</strong>
</div>
</header>
<aside class="telemetry" aria-live="polite">
<div class="panel-heading">
<span>Live gaze vector</span>
<span id="trackingState" class="tracking-state">LOCKED</span>
</div>
<h2 id="targetName" class="target-name">B · BRAVO</h2>
<p id="targetZone" class="target-zone">Upper right</p>
<div class="ranges">
<div class="angle-row">
<div class="angle-label">
<span>Horizontal</span>
<span id="yawValue" class="angle-value">+0.0°</span>
</div>
<div class="track">
<span id="yawNeedle" class="needle"></span>
</div>
<div class="track-labels">
<span>−52°</span>
<span>0°</span>
<span>+52°</span>
</div>
</div>
<div class="angle-row">
<div class="angle-label">
<span>Vertical</span>
<span id="pitchValue" class="angle-value">+0.0°</span>
</div>
<div class="track">
<span id="pitchNeedle" class="needle"></span>
</div>
<div class="track-labels">
<span>−30°</span>
<span>0°</span>
<span>+30°</span>
</div>
</div>
</div>
<div class="envelope">
<strong>Target tracking enabled.</strong><br />
Pupils converge independently while local rotation stays inside the bounded envelope.
</div>
</aside>
<section class="controls" aria-label="Target controls">
<div class="control-hint">Drag to orbit · Scroll to zoom · Select a beacon</div>
<div class="target-selector">
<button class="target-button" data-target="0" aria-pressed="false">
<span class="button-top">
<span class="button-dot" style="--c:#ff7e67"></span>
A · ALPHA
<span class="keycap">1</span>
</span>
<span class="button-sub">Upper left</span>
</button>
<button class="target-button active" data-target="1" aria-pressed="true">
<span class="button-top">
<span class="button-dot" style="--c:#68e0d0"></span>
B · BRAVO
<span class="keycap">2</span>
</span>
<span class="button-sub">Upper right</span>
</button>
<button class="target-button" data-target="2" aria-pressed="false">
<span class="button-top">
<span class="button-dot" style="--c:#ffc76a"></span>
C · CHARLIE
<span class="keycap">3</span>
</span>
<span class="button-sub">Lower right</span>
</button>
</div>
</section>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const markerLayer = document.querySelector("#markerLayer");
const buttons = [...document.querySelectorAll(".target-button")];
const yawValue = document.querySelector("#yawValue");
const pitchValue = document.querySelector("#pitchValue");
const yawNeedle = document.querySelector("#yawNeedle");
const pitchNeedle = document.querySelector("#pitchNeedle");
const targetName = document.querySelector("#targetName");
const targetZone = document.querySelector("#targetZone");
const trackingState = document.querySelector("#trackingState");
const MAX_YAW = THREE.MathUtils.degToRad(52);
const MAX_PITCH = THREE.MathUtils.degToRad(30);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071318);
scene.fog = new THREE.Fog(0x071318, 13, 28);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
60
);
const orbit = {
theta: 0,
phi: 1.4,
radius: 10.5,
goalTheta: 0,
goalPhi: 1.4,
goalRadius: 10.5,
focus: new THREE.Vector3(-0.1, 1.55, 0.65)
};
function updateCamera(instant = false) {
const smoothing = instant ? 1 : 0.12;
orbit.theta = THREE.MathUtils.lerp(
orbit.theta,
orbit.goalTheta,
smoothing
);
orbit.phi = THREE.MathUtils.lerp(
orbit.phi,
orbit.goalPhi,
smoothing
);
orbit.radius = THREE.MathUtils.lerp(
orbit.radius,
orbit.goalRadius,
smoothing
);
const sinPhi = Math.sin(orbit.phi);
const cosPhi = Math.cos(orbit.phi);
camera.position.set(
orbit.focus.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.focus.y + orbit.radius * cosPhi,
orbit.focus.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.focus);
}
updateCamera(true);
// Lighting
scene.add(new THREE.HemisphereLight(0xbcece3, 0x152026, 1.45));
const keyLight = new THREE.DirectionalLight(0xfff0db, 3.2);
keyLight.position.set(4.5, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -4;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 22;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x62d9d2, 1.35);
rimLight.position.set(-5, 4, -4);
scene.add(rimLight);
const faceLight = new THREE.PointLight(0xffc9a6, 18, 9, 2);
faceLight.position.set(-1.2, 3.1, 4.2);
scene.add(faceLight);
// Materials
const skinMaterial = new THREE.MeshStandardMaterial({
color: 0xd7a07b,
roughness: 0.76,
metalness: 0
});
const skinShadowMaterial = new THREE.MeshStandardMaterial({
color: 0x9e674f,
roughness: 0.86,
metalness: 0
});
const hairMaterial = new THREE.MeshStandardMaterial({
color: 0x17272c,
roughness: 0.88,
metalness: 0
});
const jacketMaterial = new THREE.MeshStandardMaterial({
color: 0x193d42,
roughness: 0.83,
metalness: 0.02
});
const eyeWhiteMaterial = new THREE.MeshStandardMaterial({
color: 0xf5f1e9,
roughness: 0.48,
metalness: 0
});
const pupilMaterial = new THREE.MeshStandardMaterial({
color: 0x03080a,
roughness: 0.28,
metalness: 0.02
});
const glintMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff
});
const sphereGeometry = new THREE.SphereGeometry(1, 40, 28);
function addEllipsoid(
parent,
material,
position,
scale,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(sphereGeometry, material);
mesh.position.set(...position);
mesh.scale.set(...scale);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
// Ground and pedestal
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x0a1b20,
roughness: 0.94,
metalness: 0
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.03;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(30, 30, 0x315a5d, 0x18363a);
grid.position.y = -0.015;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
const pedestalMaterial = new THREE.MeshStandardMaterial({
color: 0x10272c,
roughness: 0.58,
metalness: 0.16
});
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(1.42, 1.55, 0.18, 64),
pedestalMaterial
);
pedestal.position.set(-0.55, 0.07, 0);
pedestal.castShadow = true;
pedestal.receiveShadow = true;
scene.add(pedestal);
const pedestalTop = new THREE.Mesh(
new THREE.CylinderGeometry(1.23, 1.35, 0.08, 64),
new THREE.MeshStandardMaterial({
color: 0x17343a,
roughness: 0.48,
metalness: 0.12
})
);
pedestalTop.position.set(-0.55, 0.19, 0);
pedestalTop.castShadow = true;
pedestalTop.receiveShadow = true;
scene.add(pedestalTop);
const pedestalRing = new THREE.Mesh(
new THREE.TorusGeometry(1.26, 0.018, 10, 80),
new THREE.MeshBasicMaterial({
color: 0x68e0d0,
transparent: true,
opacity: 0.42
})
);
pedestalRing.rotation.x = Math.PI / 2;
pedestalRing.position.set(-0.55, 0.235, 0);
scene.add(pedestalRing);
// Head and shoulders
const head = new THREE.Group();
head.position.set(-0.55, 1.62, 0);
scene.add(head);
addEllipsoid(
head,
jacketMaterial,
[0, -1.42, -0.04],
[1.55, 0.48, 0.78]
);
const neck = new THREE.Mesh(
new THREE.CylinderGeometry(0.34, 0.42, 1.05, 32),
skinShadowMaterial
);
neck.position.set(0, -1.03, -0.02);
neck.castShadow = true;
neck.receiveShadow = true;
head.add(neck);
addEllipsoid(
head,
hairMaterial,
[0, 0.02, -0.12],
[1.08, 1.22, 0.88]
);
addEllipsoid(
head,
skinMaterial,
[0, -0.01, 0],
[1.01, 1.16, 0.9]
);
// Ears
for (const side of [-1, 1]) {
addEllipsoid(
head,
skinMaterial,
[side * 1.01, -0.08, -0.02],
[0.19, 0.3, 0.17]
);
addEllipsoid(
head,
skinShadowMaterial,
[side * 1.045, -0.08, 0.105],
[0.09, 0.17, 0.045]
);
}
// Brows
const leftBrow = addEllipsoid(
head,
hairMaterial,
[-0.37, 0.43, 0.82],
[0.19, 0.025, 0.035]
);
leftBrow.rotation.z = 0.13;
const rightBrow = addEllipsoid(
head,
hairMaterial,
[0.37, 0.43, 0.82],
[0.19, 0.025, 0.035]
);
rightBrow.rotation.z = -0.13;
// Eye sockets and visible eyes
const eyeDefinitions = [
{ x: -0.36, y: 0.16, z: 0.81 },
{ x: 0.36, y: 0.16, z: 0.81 }
];
const eyeGroups = [];
eyeDefinitions.forEach((eye) => {
addEllipsoid(
head,
skinShadowMaterial,
[eye.x, eye.y, eye.z - 0.045],
[0.31, 0.18, 0.04]
);
addEllipsoid(
head,
eyeWhiteMaterial,
[eye.x, eye.y, eye.z],
[0.265, 0.145, 0.065]
);
const pupilGroup = new THREE.Group();
const pupil = new THREE.Mesh(sphereGeometry, pupilMaterial);
pupil.scale.set(0.074, 0.087, 0.03);
pupil.castShadow = false;
pupilGroup.add(pupil);
const glint = new THREE.Mesh(
new THREE.SphereGeometry(0.017, 16, 12),
glintMaterial
);
glint.position.set(-0.022, 0.029, 0.03);
pupilGroup.add(glint);
head.add(pupilGroup);
eyeGroups.push({
definition: eye,
group: pupilGroup,
currentYaw: 0,
currentPitch: 0,
goalYaw: 0,
goalPitch: 0,
clamped: false
});
});
// Nose
addEllipsoid(
head,
skinMaterial,
[0, -0.08, 0.92],
[0.145, 0.225, 0.17]
);
addEllipsoid(
head,
skinShadowMaterial,
[0, -0.22, 0.91],
[0.11, 0.045, 0.07]
);
// Solid curved mouth
const mouthCurve = new THREE.QuadraticBezierCurve3(
new THREE.Vector3(-0.2, -0.48, 0.875),
new THREE.Vector3(0, -0.535, 0.925),
new THREE.Vector3(0.2, -0.48, 0.875)
);
const mouth = new THREE.Mesh(
new THREE.TubeGeometry(mouthCurve, 24, 0.016, 8, false),
new THREE.MeshStandardMaterial({
color: 0x6f3a38,
roughness: 0.9
})
);
mouth.castShadow = true;
head.add(mouth);
// Target definitions
const targetDefinitions = [
{
id: "A",
name: "ALPHA",
zone: "Upper left",
color: 0xff7e67,
css: "#ff7e67",
position: new THREE.Vector3(-2.45, 2.75, 2.45)
},
{
id: "B",
name: "BRAVO",
zone: "Upper right",
color: 0x68e0d0,
css: "#68e0d0",
position: new THREE.Vector3(1.2, 2.95, 3.0)
},
{
id: "C",
name: "CHARLIE",
zone: "Lower right",
color: 0xffc76a,
css: "#ffc76a",
position: new THREE.Vector3(2.0, 1.05, 3.1)
}
];
const targets = [];
const pickables = [];
targetDefinitions.forEach((definition, index) => {
const group = new THREE.Group();
group.position.copy(definition.position);
scene.add(group);
const markerMaterial = new THREE.MeshStandardMaterial({
color: definition.color,
emissive: definition.color,
emissiveIntensity: 0.48,
roughness: 0.34,
metalness: 0.12
});
const haloMaterial = new THREE.MeshBasicMaterial({
color: definition.color,
transparent: true,
opacity: 0.1,
side: THREE.DoubleSide,
depthWrite: false
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.29, 0.045, 16, 64),
markerMaterial
);
ring.castShadow = true;
group.add(ring);
const core = new THREE.Mesh(
new THREE.SphereGeometry(0.105, 24, 16),
markerMaterial
);
core.castShadow = true;
group.add(core);
const halo = new THREE.Mesh(
new THREE.RingGeometry(0.36, 0.385, 64),
haloMaterial
);
group.add(halo);
const tickGeometry = new THREE.BoxGeometry(0.034, 0.105, 0.034);
for (let tickIndex = 0; tickIndex < 4; tickIndex++) {
const tick = new THREE.Mesh(tickGeometry, markerMaterial);
const angle = tickIndex * Math.PI * 0.5;
tick.position.set(
Math.sin(angle) * 0.39,
Math.cos(angle) * 0.39,
0
);
tick.rotation.z = -angle;
group.add(tick);
}
group.traverse((object) => {
if (object.isMesh) {
object.userData.targetIndex = index;
pickables.push(object);
}
});
const label = document.createElement("button");
label.className = "marker-label";
label.style.setProperty("--c", definition.css);
label.textContent = `${definition.id} · ${definition.name}`;
label.setAttribute("aria-label", `Select target ${definition.id}, ${definition.name}`);
label.addEventListener("pointerdown", (event) => event.stopPropagation());
label.addEventListener("click", () => selectTarget(index));
markerLayer.appendChild(label);
targets.push({
definition,
group,
markerMaterial,
haloMaterial,
label,
hovered: false,
displayedScale: 0.96
});
});
// Central gaze ray
const gazePositions = new Float32Array(6);
const gazeGeometry = new THREE.BufferGeometry();
gazeGeometry.setAttribute(
"position",
new THREE.BufferAttribute(gazePositions, 3)
);
const gazeMaterial = new THREE.LineDashedMaterial({
color: 0x68e0d0,
dashSize: 0.12,
gapSize: 0.09,
transparent: true,
opacity: 0.55,
depthWrite: false
});
const gazeLine = new THREE.Line(gazeGeometry, gazeMaterial);
gazeLine.frustumCulled = false;
scene.add(gazeLine);
const arrowMaterial = new THREE.MeshBasicMaterial({
color: 0x68e0d0,
transparent: true,
opacity: 0.78
});
const gazeArrow = new THREE.Mesh(
new THREE.ConeGeometry(0.055, 0.16, 16),
arrowMaterial
);
scene.add(gazeArrow);
let selectedIndex = 1;
function selectTarget(index) {
selectedIndex = index;
const definition = targetDefinitions[index];
document.documentElement.style.setProperty(
"--target-color",
definition.css
);
targetName.textContent = `${definition.id} · ${definition.name}`;
targetZone.textContent = definition.zone;
buttons.forEach((button, buttonIndex) => {
const active = buttonIndex === index;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
targets.forEach((target, targetIndex) => {
target.label.classList.toggle("active", targetIndex === index);
});
arrowMaterial.color.setHex(definition.color);
gazeMaterial.color.setHex(definition.color);
}
buttons.forEach((button) => {
button.addEventListener("click", () => {
selectTarget(Number(button.dataset.target));
});
});
selectTarget(selectedIndex);
// Custom pointer camera controls
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = false;
let pointerMoved = false;
let lastPointerX = 0;
let lastPointerY = 0;
let downPointerX = 0;
let downPointerY = 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;
}
function getHoveredTarget(event) {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(pickables, false);
return intersections.length ? intersections[0].object.userData.targetIndex : -1;
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointerDown = true;
pointerMoved = false;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
downPointerX = event.clientX;
downPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (pointerDown) {
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
const totalDistance = Math.hypot(
event.clientX - downPointerX,
event.clientY - downPointerY
);
if (totalDistance > 4) pointerMoved = true;
orbit.goalTheta -= deltaX * 0.005;
orbit.goalPhi = THREE.MathUtils.clamp(
orbit.goalPhi + deltaY * 0.0045,
0.72,
1.56
);
} else {
const hoveredIndex = getHoveredTarget(event);
targets.forEach((target, index) => {
target.hovered = index === hoveredIndex;
});
canvas.style.cursor = hoveredIndex >= 0 ? "pointer" : "grab";
}
});
function endPointer(event) {
if (!pointerDown) return;
pointerDown = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
if (!pointerMoved) {
const hitIndex = getHoveredTarget(event);
if (hitIndex >= 0) selectTarget(hitIndex);
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.goalRadius = THREE.MathUtils.clamp(
orbit.goalRadius * Math.exp(event.deltaY * 0.001),
7.2,
15
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
orbit.goalTheta = 0;
orbit.goalPhi = 1.4;
orbit.goalRadius = 10.5;
});
window.addEventListener("keydown", (event) => {
if (["1", "2", "3"].includes(event.key)) {
selectTarget(Number(event.key) - 1);
}
if (event.key === "ArrowRight" || event.key === "ArrowDown") {
selectTarget((selectedIndex + 1) % targets.length);
}
if (event.key === "ArrowLeft" || event.key === "ArrowUp") {
selectTarget(
(selectedIndex - 1 + targets.length) % targets.length
);
}
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
const clock = new THREE.Clock();
const worldUp = new THREE.Vector3(0, 1, 0);
const targetLocal = new THREE.Vector3();
const pupilWorldA = new THREE.Vector3();
const pupilWorldB = new THREE.Vector3();
const gazeStart = new THREE.Vector3();
const gazeEnd = new THREE.Vector3();
const gazeDirection = new THREE.Vector3();
function formatAngle(value) {
const degrees = THREE.MathUtils.radToDeg(value);
const sign = degrees >= 0 ? "+" : "−";
return `${sign}${Math.abs(degrees).toFixed(1)}°`;
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const damping = 1 - Math.exp(-delta * 8);
updateCamera();
const selectedTarget = targets[selectedIndex];
const selectedDefinition = selectedTarget.definition;
head.updateMatrixWorld(true);
targetLocal.copy(selectedDefinition.position);
head.worldToLocal(targetLocal);
let anyClamped = false;
eyeGroups.forEach((eye) => {
const dx = targetLocal.x - eye.definition.x;
const dy = targetLocal.y - eye.definition.y;
const dz = targetLocal.z - eye.definition.z;
const desiredYaw = Math.atan2(dx, dz);
const desiredPitch = Math.atan2(dy, Math.hypot(dx, dz));
eye.goalYaw = THREE.MathUtils.clamp(
desiredYaw,
-MAX_YAW,
MAX_YAW
);
eye.goalPitch = THREE.MathUtils.clamp(
desiredPitch,
-MAX_PITCH,
MAX_PITCH
);
eye.clamped =
Math.abs(desiredYaw - eye.goalYaw) > 0.0001 ||
Math.abs(desiredPitch - eye.goalPitch) > 0.0001;
anyClamped ||= eye.clamped;
eye.currentYaw = THREE.MathUtils.lerp(
eye.currentYaw,
eye.goalYaw,
damping
);
eye.currentPitch = THREE.MathUtils.lerp(
eye.currentPitch,
eye.goalPitch,
damping
);
const cosPitch = Math.cos(eye.currentPitch);
const directionX = Math.sin(eye.currentYaw) * cosPitch;
const directionY = Math.sin(eye.currentPitch);
const directionZ = Math.cos(eye.currentYaw) * cosPitch;
eye.group.position.set(
eye.definition.x + directionX * 0.115,
eye.definition.y + directionY * 0.078,
eye.definition.z +
0.054 +
directionZ * 0.015 -
Math.abs(directionX) * 0.014
);
eye.group.rotation.z = -eye.currentYaw * 0.07;
});
// Update central gaze visualization.
eyeGroups[0].group.getWorldPosition(pupilWorldA);
eyeGroups[1].group.getWorldPosition(pupilWorldB);
gazeStart.copy(pupilWorldA).add(pupilWorldB).multiplyScalar(0.5);
gazeEnd.copy(selectedDefinition.position);
gazeDirection.copy(gazeEnd).sub(gazeStart).normalize();
gazePositions[0] = gazeStart.x;
gazePositions[1] = gazeStart.y;
gazePositions[2] = gazeStart.z;
gazePositions[3] = gazeEnd.x;
gazePositions[4] = gazeEnd.y;
gazePositions[5] = gazeEnd.z;
gazeGeometry.attributes.position.needsUpdate = true;
gazeLine.computeLineDistances();
gazeArrow.position
.copy(gazeEnd)
.addScaledVector(gazeDirection, -0.24);
gazeArrow.quaternion.setFromUnitVectors(worldUp, gazeDirection);
// Target billboard behavior and hover/selection feedback.
targets.forEach((target, index) => {
target.group.quaternion.copy(camera.quaternion);
const selected = index === selectedIndex;
const desiredScale = selected ? 1.12 : target.hovered ? 1.05 : 0.96;
target.displayedScale = THREE.MathUtils.lerp(
target.displayedScale,
desiredScale,
damping
);
target.group.scale.setScalar(target.displayedScale);
target.markerMaterial.emissiveIntensity = selected
? 1.35
: target.hovered
? 0.85
: 0.4;
target.haloMaterial.opacity = selected
? 0.3
: target.hovered
? 0.18
: 0.075;
});
// Project target labels into screen space.
targets.forEach((target) => {
const projected = target.definition.position.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
target.label.style.display = visible ? "block" : "none";
if (visible) {
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
target.label.style.left = `${x}px`;
target.label.style.top = `${y - 30}px`;
}
});
// Telemetry
const averageYaw =
(eyeGroups[0].currentYaw + eyeGroups[1].currentYaw) * 0.5;
const averagePitch =
(eyeGroups[0].currentPitch + eyeGroups[1].currentPitch) * 0.5;
yawValue.textContent = formatAngle(averageYaw);
pitchValue.textContent = formatAngle(averagePitch);
const yawPercent =
((averageYaw + MAX_YAW) / (MAX_YAW * 2)) * 100;
const pitchPercent =
((MAX_PITCH - averagePitch) / (MAX_PITCH * 2)) * 100;
yawNeedle.style.left = `${THREE.MathUtils.clamp(yawPercent, 0, 100)}%`;
pitchNeedle.style.top = `${THREE.MathUtils.clamp(pitchPercent, 0, 100)}%`;
trackingState.textContent = anyClamped ? "LIMITED" : "LOCKED";
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```