Prompt specification
Build or customize the Three.js 3D scene "Observer Cone Behind a Pillar" 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: Observer Cone Behind a Pillar
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/observer-cone-behind-a-pillar-x3461
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring line of sight, occlusion, and watcher visibility. Model an observer token, target, and opaque pillar with a visible viewing direction. Show blocked and unblocked positions using actual segment intersection. Low-poly.
Working Three.js Starter (observer-cone-behind-a-pillar-x3461):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Line of Sight — Three.js</title>
<style>
:root {
color-scheme: dark;
--bg: #071019;
--panel: rgba(10, 19, 29, 0.88);
--panel-border: rgba(170, 220, 255, 0.13);
--text: #edf8ff;
--muted: #8da4b5;
--cyan: #55d8ff;
--green: #70f0b4;
--red: #ff657a;
--gold: #ffc861;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#stage {
position: fixed;
inset: 0;
overflow: hidden;
}
#scene {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.panel {
position: absolute;
z-index: 10;
top: 22px;
left: 22px;
width: min(330px, calc(100vw - 44px));
max-height: calc(100vh - 44px);
overflow: auto;
padding: 22px;
border: 1px solid var(--panel-border);
border-radius: 22px;
background:
linear-gradient(145deg, rgba(255, 255, 255, 0.045), transparent 45%),
var(--panel);
box-shadow:
0 22px 70px rgba(0, 0, 0, 0.38),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
scrollbar-width: thin;
scrollbar-color: #304657 transparent;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 10px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 14px var(--cyan);
content: "";
}
h1 {
margin: 0;
font-size: 30px;
font-weight: 720;
letter-spacing: -0.045em;
}
.intro {
margin: 10px 0 18px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.status-card {
padding: 15px;
border: 1px solid rgba(112, 240, 180, 0.2);
border-radius: 15px;
background: rgba(112, 240, 180, 0.065);
transition:
border-color 180ms ease,
background 180ms ease;
}
.status-card[data-state="blocked"] {
border-color: rgba(255, 101, 122, 0.24);
background: rgba(255, 101, 122, 0.07);
}
.status-heading {
display: flex;
align-items: center;
gap: 9px;
font-size: 13px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.status-dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 15px rgba(112, 240, 180, 0.8);
}
[data-state="blocked"] .status-dot {
background: var(--red);
box-shadow: 0 0 15px rgba(255, 101, 122, 0.8);
}
#statusDescription {
margin-top: 8px;
color: #a9bbc8;
font-size: 12px;
line-height: 1.45;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 10px;
}
.metric {
padding: 10px;
border: 1px solid rgba(255, 255, 255, 0.055);
border-radius: 11px;
background: rgba(0, 0, 0, 0.14);
}
.metric span {
display: block;
color: #708899;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.metric strong {
display: block;
margin-top: 4px;
color: #dcecf6;
font-size: 13px;
font-variant-numeric: tabular-nums;
}
.section {
margin-top: 20px;
padding-top: 18px;
border-top: 1px solid rgba(255, 255, 255, 0.07);
}
.section-title {
margin-bottom: 10px;
color: #dcebf4;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
min-height: 40px;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 11px;
background: rgba(255, 255, 255, 0.035);
color: #bed0dc;
cursor: pointer;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.055em;
text-transform: uppercase;
transition:
border-color 150ms ease,
background 150ms ease,
color 150ms ease,
transform 150ms ease;
}
button:hover {
border-color: rgba(85, 216, 255, 0.35);
background: rgba(85, 216, 255, 0.08);
color: #effaff;
transform: translateY(-1px);
}
button[aria-pressed="true"] {
border-color: rgba(85, 216, 255, 0.45);
background: rgba(85, 216, 255, 0.14);
color: #dff7ff;
}
#blockedButton[aria-pressed="true"] {
border-color: rgba(255, 101, 122, 0.42);
background: rgba(255, 101, 122, 0.12);
color: #ffd5dc;
}
#clearButton[aria-pressed="true"] {
border-color: rgba(112, 240, 180, 0.42);
background: rgba(112, 240, 180, 0.12);
color: #d5ffeb;
}
#sweepButton {
width: 100%;
margin-top: 8px;
}
.hint {
margin: 9px 0 0;
color: #718a9b;
font-size: 11px;
line-height: 1.45;
}
.range-control {
margin-top: 13px;
}
.range-label {
display: flex;
justify-content: space-between;
margin-bottom: 7px;
color: #a9bdca;
font-size: 11px;
}
.range-label output {
color: var(--cyan);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
appearance: none;
border-radius: 99px;
background: #233746;
cursor: pointer;
accent-color: var(--cyan);
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
appearance: none;
border: 3px solid #0c1822;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 0 1px rgba(85, 216, 255, 0.55);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 3px solid #0c1822;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 0 1px rgba(85, 216, 255, 0.55);
}
.method-note {
margin-top: 18px;
padding: 11px;
border: 1px solid rgba(85, 216, 255, 0.11);
border-radius: 11px;
background: rgba(85, 216, 255, 0.035);
color: #7794a6;
font-size: 10px;
line-height: 1.5;
}
.method-note strong {
color: #a9c7d7;
}
.legend {
position: absolute;
z-index: 8;
top: 24px;
right: 24px;
display: flex;
align-items: center;
gap: 16px;
padding: 10px 14px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 999px;
background: rgba(8, 17, 26, 0.72);
color: #92a8b7;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.055em;
text-transform: uppercase;
backdrop-filter: blur(12px);
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
}
.legend-line {
width: 19px;
height: 3px;
border-radius: 99px;
background: var(--green);
box-shadow: 0 0 9px rgba(112, 240, 180, 0.65);
}
.legend-line.blocked {
background: var(--red);
box-shadow: 0 0 9px rgba(255, 101, 122, 0.65);
}
.scene-hint {
position: absolute;
z-index: 8;
right: 24px;
bottom: 24px;
padding: 10px 14px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 12px;
background: rgba(8, 17, 26, 0.72);
color: #7890a1;
font-size: 10px;
line-height: 1.6;
backdrop-filter: blur(12px);
}
.scene-hint strong {
color: #b8cbd7;
}
#resetCamera {
position: absolute;
z-index: 9;
right: 24px;
bottom: 79px;
min-height: 34px;
padding: 7px 12px;
background: rgba(8, 17, 26, 0.78);
backdrop-filter: blur(12px);
}
#labels {
position: absolute;
z-index: 5;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
transform: translate(-50%, -50%);
white-space: nowrap;
color: #bcd2df;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-shadow: 0 2px 8px #000;
transition: opacity 120ms ease;
}
.world-label::before {
display: inline-block;
width: 5px;
height: 5px;
margin-right: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 8px currentColor;
content: "";
vertical-align: 1px;
}
.world-label.observer {
color: var(--cyan);
}
.world-label.target {
color: var(--gold);
}
.world-label.pillar {
color: #c5ad91;
}
.world-label.pad-label {
padding: 5px 8px;
border: 1px solid currentColor;
border-radius: 6px;
background: rgba(7, 16, 25, 0.82);
font-size: 8px;
}
.world-label.pad-label.blocked {
color: var(--red);
}
.world-label.pad-label.clear {
color: var(--green);
}
#loading {
position: absolute;
z-index: 30;
inset: 0;
display: grid;
place-items: center;
background: var(--bg);
color: var(--cyan);
font-size: 12px;
font-weight: 750;
letter-spacing: 0.14em;
text-transform: uppercase;
}
@media (max-width: 700px) {
.panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
max-height: 43vh;
padding: 17px;
border-radius: 17px;
}
h1 {
font-size: 24px;
}
.intro {
display: none;
}
.legend {
top: auto;
right: 12px;
bottom: 64px;
padding: 8px 10px;
}
.scene-hint {
right: 12px;
bottom: 12px;
max-width: 230px;
}
#resetCamera {
right: 12px;
bottom: 112px;
}
.world-label.pad-label {
display: none;
}
}
</style>
</head>
<body>
<div id="stage">
<canvas id="scene" aria-label="Interactive line-of-sight demonstration"></canvas>
<div id="labels"></div>
<aside class="panel">
<div class="eyebrow">Visibility laboratory</div>
<h1>Line of sight</h1>
<p class="intro">
Move the observer between marked positions or drag it across the arena.
The sight segment is tested against the opaque pillar's actual geometry.
</p>
<div id="statusCard" class="status-card" data-state="visible" aria-live="polite">
<div class="status-heading">
<span class="status-dot"></span>
<span id="statusTitle">Visible</span>
</div>
<div id="statusDescription">No opaque mesh intersects the sight segment.</div>
</div>
<div class="metrics">
<div class="metric">
<span>Segment length</span>
<strong id="distanceValue">—</strong>
</div>
<div class="metric">
<span>First mesh hit</span>
<strong id="hitValue">None</strong>
</div>
</div>
<div class="section">
<div class="section-title">Observer position</div>
<div class="button-row">
<button id="blockedButton" aria-pressed="true">Blocked</button>
<button id="clearButton" aria-pressed="false">Clear</button>
</div>
<button id="sweepButton" aria-pressed="false">Auto sweep</button>
<p class="hint">
Drag the cyan observer to test any location. The direction arrow always
points toward the target.
</p>
</div>
<div class="section">
<div class="section-title">Obstacle profile</div>
<div class="range-control">
<div class="range-label">
<span>Pillar width</span>
<output id="widthOutput">100%</output>
</div>
<input
id="widthSlider"
type="range"
min="70"
max="130"
value="100"
step="1"
/>
</div>
<div class="range-control">
<div class="range-label">
<span>Pillar height</span>
<output id="heightOutput">100%</output>
</div>
<input
id="heightSlider"
type="range"
min="70"
max="130"
value="100"
step="1"
/>
</div>
</div>
<div class="method-note">
<strong>Finite segment–mesh test.</strong> A Three.js ray is limited to the
observer-to-target segment and intersected against the pillar's real
triangle geometry.
</div>
</aside>
<div class="legend" aria-hidden="true">
<div class="legend-item">
<span class="legend-line"></span>
Visible
</div>
<div class="legend-item">
<span class="legend-line blocked"></span>
Blocked
</div>
</div>
<button id="resetCamera">Reset camera</button>
<div class="scene-hint">
<strong>Drag scene</strong> to orbit · <strong>Scroll</strong> to zoom<br />
<strong>Drag observer</strong> to place · <strong>Click pads</strong> to preset
</div>
<div id="loading">Loading visibility laboratory…</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, camera, and scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const stage = document.querySelector("#stage");
const labelLayer = document.querySelector("#labels");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(stage.clientWidth, stage.clientHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.Fog(0x071019, 18, 34);
const camera = new THREE.PerspectiveCamera(
42,
stage.clientWidth / stage.clientHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0.35, 0.75, 0.1);
const cameraState = {
radius: 15,
azimuth: 0.72,
polar: 0.91
};
function updateCamera() {
const sinPolar = Math.sin(cameraState.polar);
const cosPolar = Math.cos(cameraState.polar);
camera.position.set(
cameraTarget.x +
cameraState.radius * sinPolar * Math.sin(cameraState.azimuth),
cameraTarget.y + cameraState.radius * cosPolar,
cameraTarget.z +
cameraState.radius * sinPolar * Math.cos(cameraState.azimuth)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xa6dcff, 0x17212a, 1.65);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xd9f1ff, 3.1);
keyLight.position.set(5, 11, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x497dff, 1.1);
rimLight.position.set(-7, 5, -7);
scene.add(rimLight);
const targetLight = new THREE.PointLight(0xffc861, 7, 6, 2);
targetLight.position.set(4.1, 2.1, -0.4);
scene.add(targetLight);
// -------------------------------------------------------------------------
// Arena
// -------------------------------------------------------------------------
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x101d28,
roughness: 0.91,
metalness: 0.08
});
const floor = new THREE.Mesh(
new THREE.BoxGeometry(13.4, 0.18, 9.2),
floorMaterial
);
floor.position.set(0, -0.1, 0);
floor.receiveShadow = true;
scene.add(floor);
const underFloor = new THREE.Mesh(
new THREE.BoxGeometry(14.4, 0.12, 10.2),
new THREE.MeshStandardMaterial({
color: 0x071019,
roughness: 1
})
);
underFloor.position.set(0, -0.22, 0);
scene.add(underFloor);
const gridVertices = [];
for (let x = -6; x <= 6; x += 1) {
gridVertices.push(x, 0.006, -4.3, x, 0.006, 4.3);
}
for (let z = -4; z <= 4; z += 1) {
gridVertices.push(-6.3, 0.006, z, 6.3, 0.006, z);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridVertices, 3)
);
const grid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x4a7188,
transparent: true,
opacity: 0.16
})
);
scene.add(grid);
const borderGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-6.35, 0.018, -4.35),
new THREE.Vector3(6.35, 0.018, -4.35),
new THREE.Vector3(6.35, 0.018, 4.35),
new THREE.Vector3(-6.35, 0.018, 4.35),
new THREE.Vector3(-6.35, 0.018, -4.35)
]);
scene.add(
new THREE.Line(
borderGeometry,
new THREE.LineBasicMaterial({
color: 0x55d8ff,
transparent: true,
opacity: 0.26
})
)
);
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const observerDarkMaterial = new THREE.MeshStandardMaterial({
color: 0x173245,
roughness: 0.58,
metalness: 0.38,
flatShading: true
});
const observerBodyMaterial = new THREE.MeshStandardMaterial({
color: 0x28b7df,
roughness: 0.4,
metalness: 0.22,
flatShading: true
});
const observerLightMaterial = new THREE.MeshStandardMaterial({
color: 0xc8f2ff,
roughness: 0.38,
metalness: 0.12,
flatShading: true
});
const lensMaterial = new THREE.MeshStandardMaterial({
color: 0x07131c,
emissive: 0x0c79a0,
emissiveIntensity: 0.55,
roughness: 0.2,
metalness: 0.6,
flatShading: true
});
const stoneMaterial = new THREE.MeshStandardMaterial({
color: 0xa88968,
roughness: 0.93,
metalness: 0.02,
flatShading: true
});
const stoneDarkMaterial = new THREE.MeshStandardMaterial({
color: 0x715945,
roughness: 0.95,
metalness: 0.02,
flatShading: true
});
const targetGoldMaterial = new THREE.MeshStandardMaterial({
color: 0xffbd52,
emissive: 0x7a3b05,
emissiveIntensity: 0.34,
roughness: 0.35,
metalness: 0.34,
flatShading: true
});
const targetDarkMaterial = new THREE.MeshStandardMaterial({
color: 0x33291e,
roughness: 0.76,
metalness: 0.32,
flatShading: true
});
// -------------------------------------------------------------------------
// Observer token
// -------------------------------------------------------------------------
const observer = new THREE.Group();
scene.add(observer);
const observerBase = new THREE.Mesh(
new THREE.CylinderGeometry(0.46, 0.55, 0.16, 8),
observerDarkMaterial
);
observerBase.position.y = 0.08;
observerBase.castShadow = true;
observerBase.receiveShadow = true;
observer.add(observerBase);
const observerRing = new THREE.Mesh(
new THREE.TorusGeometry(0.405, 0.032, 6, 20),
observerBodyMaterial
);
observerRing.rotation.x = Math.PI / 2;
observerRing.position.y = 0.175;
observer.add(observerRing);
const observerBody = new THREE.Mesh(
new THREE.CylinderGeometry(0.3, 0.38, 0.52, 6),
observerBodyMaterial
);
observerBody.position.y = 0.43;
observerBody.castShadow = true;
observer.add(observerBody);
const observerCollar = new THREE.Mesh(
new THREE.CylinderGeometry(0.25, 0.34, 0.14, 8),
observerDarkMaterial
);
observerCollar.position.y = 0.75;
observerCollar.castShadow = true;
observer.add(observerCollar);
const observerHeadPivot = new THREE.Group();
observerHeadPivot.position.y = 0.93;
observer.add(observerHeadPivot);
const observerHead = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.34, 1),
observerLightMaterial
);
observerHead.scale.set(1, 0.86, 0.96);
observerHead.castShadow = true;
observerHeadPivot.add(observerHead);
const observerLens = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.105, 1),
lensMaterial
);
observerLens.scale.set(1, 0.72, 0.58);
observerLens.position.set(0, 0.02, 0.285);
observerHeadPivot.add(observerLens);
const eyeFocus = new THREE.Object3D();
eyeFocus.position.set(0, 0.95, 0.32);
observer.add(eyeFocus);
// Transparent pick volume makes the token easy to grab.
const observerPick = new THREE.Mesh(
new THREE.SphereGeometry(0.68, 8, 6),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
})
);
observerPick.position.y = 0.6;
observer.add(observerPick);
// -------------------------------------------------------------------------
// Target token
// -------------------------------------------------------------------------
const targetGroup = new THREE.Group();
targetGroup.position.set(4.1, 0, -0.4);
scene.add(targetGroup);
const targetBase = new THREE.Mesh(
new THREE.CylinderGeometry(0.47, 0.58, 0.18, 8),
targetDarkMaterial
);
targetBase.position.y = 0.09;
targetBase.castShadow = true;
targetBase.receiveShadow = true;
targetGroup.add(targetBase);
const targetRing = new THREE.Mesh(
new THREE.TorusGeometry(0.39, 0.035, 6, 20),
targetGoldMaterial
);
targetRing.rotation.x = Math.PI / 2;
targetRing.position.y = 0.19;
targetGroup.add(targetRing);
const targetStem = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.11, 0.54, 6),
targetDarkMaterial
);
targetStem.position.y = 0.48;
targetStem.castShadow = true;
targetGroup.add(targetStem);
const targetCrystal = new THREE.Mesh(
new THREE.OctahedronGeometry(0.39, 0),
targetGoldMaterial
);
targetCrystal.position.y = 1.04;
targetCrystal.scale.y = 1.16;
targetCrystal.rotation.y = Math.PI / 4;
targetCrystal.castShadow = true;
targetGroup.add(targetCrystal);
const targetFocus = new THREE.Object3D();
targetFocus.position.set(0, 1.04, 0);
targetGroup.add(targetFocus);
// -------------------------------------------------------------------------
// Opaque pillar. Every visible solid part is included in the ray test.
// -------------------------------------------------------------------------
const pillar = new THREE.Group();
pillar.position.set(1.1, 0, -0.2);
scene.add(pillar);
const occluders = [];
const pillarBase = new THREE.Mesh(
new THREE.CylinderGeometry(0.72, 0.79, 0.18, 8),
stoneDarkMaterial
);
pillarBase.position.y = 0.09;
pillarBase.castShadow = true;
pillarBase.receiveShadow = true;
pillar.add(pillarBase);
occluders.push(pillarBase);
const pillarBody = new THREE.Mesh(
new THREE.CylinderGeometry(0.54, 0.62, 1, 8),
stoneMaterial
);
pillarBody.castShadow = true;
pillarBody.receiveShadow = true;
pillar.add(pillarBody);
occluders.push(pillarBody);
const pillarBand = new THREE.Mesh(
new THREE.CylinderGeometry(0.555, 0.59, 0.12, 8),
stoneDarkMaterial
);
pillarBand.castShadow = true;
pillar.add(pillarBand);
occluders.push(pillarBand);
const pillarCap = new THREE.Mesh(
new THREE.CylinderGeometry(0.59, 0.64, 0.14, 8),
stoneDarkMaterial
);
pillarCap.castShadow = true;
pillarCap.receiveShadow = true;
pillar.add(pillarCap);
occluders.push(pillarCap);
const BASE_PILLAR_HEIGHT = 2.4;
function updatePillar() {
const width = Number(widthSlider.value) / 100;
const height = Number(heightSlider.value) / 100;
const worldHeight = BASE_PILLAR_HEIGHT * height;
pillar.scale.x = width;
pillar.scale.z = width;
pillarBody.scale.y = worldHeight;
pillarBody.position.y = 0.18 + worldHeight / 2;
pillarBand.position.y = 0.18 + worldHeight * 0.55;
pillarCap.position.y = 0.18 + worldHeight + 0.07;
widthOutput.value = `${Math.round(width * 100)}%`;
heightOutput.value = `${Math.round(height * 100)}%`;
}
// -------------------------------------------------------------------------
// Preset pads
// -------------------------------------------------------------------------
const blockedPosition = new THREE.Vector3(-2.8, 0, -0.2);
const clearPosition = new THREE.Vector3(1.9, 0, 3.2);
const padTargets = [];
function createPad(position, color, opacity) {
const group = new THREE.Group();
group.position.copy(position);
scene.add(group);
const ring = new THREE.Mesh(
new THREE.RingGeometry(0.62, 0.79, 32),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
side: THREE.DoubleSide,
depthWrite: false
})
);
ring.rotation.x = -Math.PI / 2;
ring.position.y = 0.025;
ring.userData.preset = position.clone();
group.add(ring);
padTargets.push(ring);
const inner = new THREE.Mesh(
new THREE.CircleGeometry(0.61, 32),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: opacity * 0.3,
side: THREE.DoubleSide,
depthWrite: false
})
);
inner.rotation.x = -Math.PI / 2;
inner.position.y = 0.018;
group.add(inner);
return group;
}
createPad(blockedPosition, 0xff657a, 0.34);
createPad(clearPosition, 0x70f0b4, 0.34);
// -------------------------------------------------------------------------
// Sight visualization
// -------------------------------------------------------------------------
function makeTube(radius, opacity) {
const material = new THREE.MeshBasicMaterial({
color: 0x70f0b4,
transparent: true,
opacity,
depthTest: false,
depthWrite: false
});
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, 1, 8),
material
);
mesh.renderOrder = 20;
scene.add(mesh);
return mesh;
}
const sightGuide = makeTube(0.012, 0.17);
const sightBeam = makeTube(0.026, 0.94);
const viewArrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(),
1.3,
0x70f0b4,
0.24,
0.13
);
viewArrow.line.material.transparent = true;
viewArrow.line.material.opacity = 0.95;
viewArrow.line.material.depthTest = false;
viewArrow.line.renderOrder = 21;
viewArrow.cone.material.transparent = true;
viewArrow.cone.material.opacity = 0.95;
viewArrow.cone.material.depthTest = false;
viewArrow.cone.renderOrder = 21;
scene.add(viewArrow);
const hitMarker = new THREE.Mesh(
new THREE.OctahedronGeometry(0.105, 0),
new THREE.MeshBasicMaterial({
color: 0xff657a,
depthTest: false,
depthWrite: false
})
);
hitMarker.renderOrder = 25;
hitMarker.visible = false;
scene.add(hitMarker);
const UP = new THREE.Vector3(0, 1, 0);
function setTube(mesh, start, end) {
const direction = end.clone().sub(start);
const length = direction.length();
if (length < 0.0001) {
mesh.visible = false;
return;
}
mesh.visible = true;
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(UP, direction.normalize());
mesh.scale.set(1, length, 1);
}
// -------------------------------------------------------------------------
// DOM labels projected from world positions
// -------------------------------------------------------------------------
function createWorldLabel(text, className) {
const element = document.createElement("div");
element.className = `world-label ${className}`;
element.textContent = text;
labelLayer.appendChild(element);
return element;
}
const observerLabel = createWorldLabel("OBSERVER", "observer");
const targetLabel = createWorldLabel("TARGET", "target");
const pillarLabel = createWorldLabel("OPAQUE PILLAR", "pillar");
const blockedLabel = createWorldLabel("BLOCKED", "pad-label blocked");
const clearLabel = createWorldLabel("CLEAR", "pad-label clear");
function placeWorldLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
element.style.opacity = visible ? "1" : "0";
element.style.left = `${(projected.x * 0.5 + 0.5) * stage.clientWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * stage.clientHeight}px`;
}
// -------------------------------------------------------------------------
// Actual finite segment–mesh intersection
// -------------------------------------------------------------------------
const sightRay = new THREE.Raycaster();
const rayOrigin = new THREE.Vector3();
const rayDirection = new THREE.Vector3();
const targetPoint = new THREE.Vector3();
const statusCard = document.querySelector("#statusCard");
const statusTitle = document.querySelector("#statusTitle");
const statusDescription = document.querySelector("#statusDescription");
const distanceValue = document.querySelector("#distanceValue");
const hitValue = document.querySelector("#hitValue");
let currentStatus = "";
function updateSight() {
scene.updateMatrixWorld(true);
eyeFocus.getWorldPosition(rayOrigin);
targetFocus.getWorldPosition(targetPoint);
rayDirection.subVectors(targetPoint, rayOrigin);
const segmentLength = rayDirection.length();
if (segmentLength < 0.0001) {
return;
}
rayDirection.normalize();
/*
* This is an actual ray/triangle intersection against the pillar meshes.
* Setting far to the segment length makes it a finite segment test rather
* than an infinite ray. The explicit distance check below reinforces the
* segment boundary.
*/
sightRay.set(rayOrigin, rayDirection);
sightRay.near = 0.001;
sightRay.far = segmentLength;
const intersections = sightRay.intersectObjects(occluders, false);
const hit = intersections.find(
(intersection) =>
intersection.distance >= 0 &&
intersection.distance <= segmentLength + 0.0001
);
const blocked = Boolean(hit);
const color = blocked ? 0xff657a : 0x70f0b4;
sightGuide.material.color.setHex(color);
sightBeam.material.color.setHex(color);
viewArrow.setColor(color);
setTube(sightGuide, rayOrigin, targetPoint);
if (blocked) {
setTube(sightBeam, rayOrigin, hit.point);
hitMarker.position.copy(hit.point);
hitMarker.visible = true;
} else {
setTube(sightBeam, rayOrigin, targetPoint);
hitMarker.visible = false;
}
// Visible viewing direction.
viewArrow.position.copy(rayOrigin).addScaledVector(rayDirection, 0.37);
viewArrow.setDirection(rayDirection);
viewArrow.setLength(
Math.min(1.35, segmentLength * 0.3),
0.23,
0.13
);
if (currentStatus !== (blocked ? "blocked" : "visible")) {
currentStatus = blocked ? "blocked" : "visible";
statusCard.dataset.state = currentStatus;
statusTitle.textContent = blocked ? "Blocked" : "Visible";
statusDescription.textContent = blocked
? "The opaque pillar intersects the observer-to-target segment."
: "No opaque mesh intersects the observer-to-target segment.";
}
distanceValue.textContent = `${segmentLength.toFixed(2)} m`;
hitValue.textContent = hit
? `${hit.distance.toFixed(2)} m`
: "None";
}
// -------------------------------------------------------------------------
// UI and movement state
// -------------------------------------------------------------------------
const blockedButton = document.querySelector("#blockedButton");
const clearButton = document.querySelector("#clearButton");
const sweepButton = document.querySelector("#sweepButton");
const widthSlider = document.querySelector("#widthSlider");
const heightSlider = document.querySelector("#heightSlider");
const widthOutput = document.querySelector("#widthOutput");
const heightOutput = document.querySelector("#heightOutput");
const observerPosition = blockedPosition.clone();
const observerDestination = blockedPosition.clone();
let sweeping = false;
let sweepPhase = 0;
function setPressedButton(active) {
blockedButton.setAttribute("aria-pressed", String(active === "blocked"));
clearButton.setAttribute("aria-pressed", String(active === "clear"));
}
function setPreset(name) {
sweeping = false;
sweepButton.setAttribute("aria-pressed", "false");
sweepButton.textContent = "Auto sweep";
if (name === "blocked") {
observerDestination.copy(blockedPosition);
setPressedButton("blocked");
} else {
observerDestination.copy(clearPosition);
setPressedButton("clear");
}
}
blockedButton.addEventListener("click", () => setPreset("blocked"));
clearButton.addEventListener("click", () => setPreset("clear"));
sweepButton.addEventListener("click", () => {
sweeping = !sweeping;
sweepButton.setAttribute("aria-pressed", String(sweeping));
sweepButton.textContent = sweeping ? "Stop sweep" : "Auto sweep";
if (sweeping) {
sweepPhase = 0;
setPressedButton(null);
}
});
widthSlider.addEventListener("input", updatePillar);
heightSlider.addEventListener("input", updatePillar);
updatePillar();
document.querySelector("#resetCamera").addEventListener("click", () => {
cameraState.radius = 15;
cameraState.azimuth = 0.72;
cameraState.polar = 0.91;
updateCamera();
});
// -------------------------------------------------------------------------
// Custom pointer controls: orbit, zoom, and observer dragging
// -------------------------------------------------------------------------
const pointerRay = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
const dragPoint = new THREE.Vector3();
const dragOffset = new THREE.Vector3();
let pointerDown = false;
let orbiting = false;
let draggingObserver = 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;
pointerRay.setFromCamera(pointer, camera);
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
updatePointer(event);
pointerDown = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
const padHit = pointerRay.intersectObjects(padTargets, false)[0];
if (padHit) {
const preset = padHit.object.userData.preset;
if (preset.equals(blockedPosition)) {
setPreset("blocked");
} else if (preset.equals(clearPosition)) {
setPreset("clear");
}
return;
}
const observerHit = pointerRay.intersectObject(observerPick, false)[0];
if (observerHit) {
draggingObserver = true;
sweeping = false;
sweepButton.setAttribute("aria-pressed", "false");
sweepButton.textContent = "Auto sweep";
setPressedButton(null);
if (pointerRay.ray.intersectPlane(dragPlane, dragPoint)) {
dragOffset.copy(observerPosition).sub(dragPoint);
}
canvas.style.cursor = "grabbing";
} else {
orbiting = true;
canvas.style.cursor = "grabbing";
}
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
updatePointer(event);
if (draggingObserver) {
if (pointerRay.ray.intersectPlane(dragPlane, dragPoint)) {
const next = dragPoint.clone().add(dragOffset);
next.x = THREE.MathUtils.clamp(next.x, -5.6, 5.6);
next.z = THREE.MathUtils.clamp(next.z, -3.75, 3.75);
next.y = 0;
observerPosition.copy(next);
observerDestination.copy(next);
}
return;
}
if (orbiting) {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
cameraState.azimuth -= dx * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar - dy * 0.005,
0.36,
1.35
);
updateCamera();
}
lastPointerX = event.clientX;
lastPointerY = event.clientY;
});
function endPointer(event) {
pointerDown = false;
orbiting = false;
draggingObserver = false;
canvas.style.cursor = "grab";
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.style.cursor = "grab";
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.012,
8.5,
22
);
updateCamera();
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
let loaded = false;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (sweeping) {
sweepPhase = (sweepPhase + delta / 3.2) % 2;
const rawT = sweepPhase <= 1 ? sweepPhase : 2 - sweepPhase;
const easedT = rawT * rawT * (3 - 2 * rawT);
observerPosition.lerpVectors(
blockedPosition,
clearPosition,
easedT
);
observerDestination.copy(observerPosition);
} else {
const damping = 1 - Math.exp(-delta * 8);
observerPosition.lerp(observerDestination, damping);
}
observer.position.copy(observerPosition);
// The model's local +Z axis points toward the target.
const targetHorizontal = targetGroup.position.clone().sub(observerPosition);
observer.rotation.y = Math.atan2(
targetHorizontal.x,
targetHorizontal.z
);
if (hitMarker.visible) {
const pulse = 1 + Math.sin(performance.now() * 0.008) * 0.11;
hitMarker.scale.setScalar(pulse);
}
updateSight();
placeWorldLabel(
observerLabel,
observerPosition.clone().add(new THREE.Vector3(0, 1.65, 0))
);
placeWorldLabel(
targetLabel,
targetGroup.position.clone().add(new THREE.Vector3(0, 1.75, 0))
);
placeWorldLabel(
pillarLabel,
pillar.position.clone().add(
new THREE.Vector3(
0,
0.18 + BASE_PILLAR_HEIGHT * (heightSlider.value / 100) + 0.48,
0
)
)
);
placeWorldLabel(
blockedLabel,
blockedPosition.clone().add(new THREE.Vector3(0, 0.08, 0))
);
placeWorldLabel(
clearLabel,
clearPosition.clone().add(new THREE.Vector3(0, 0.08, 0))
);
renderer.render(scene, camera);
if (!loaded) {
loaded = true;
document.querySelector("#loading")?.remove();
}
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```