Prompt specification
Build or customize the Three.js 3D scene "Group-shape exposure sandbox" 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: Group-shape exposure sandbox
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/group-shape-exposure-sandbox-x0704
Scene Description Prompt:
Use Three.js from cdnjs to create configurable penguin-like agents forming circles, rows, and loose clusters; compare geometric boundary exposure under a toy model while explicitly avoiding body-temperature or survival predictions. Deliver one self-contained HTML file with low-poly forms.
Working Three.js Starter (group-shape-exposure-sandbox-x0704):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Boundary Form — Geometric Penguin-Agent Study</title>
<style>
:root {
color-scheme: dark;
--bg: #071017;
--panel: #0c1821;
--panel-2: #101f29;
--line: rgba(177, 215, 225, 0.14);
--text: #e8f2f4;
--muted: #91a8b2;
--cyan: #59dfcc;
--amber: #ffb85c;
--orange: #ff795f;
--violet: #b8a4ff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
min-height: 100%;
margin: 0;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button,
input,
select {
font: inherit;
}
button,
select,
input[type="range"],
input[type="checkbox"] {
cursor: pointer;
}
.app-header {
height: 70px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 0 24px;
border-bottom: 1px solid var(--line);
background:
radial-gradient(circle at 18% 0%, rgba(89, 223, 204, 0.10), transparent 34%),
#09141c;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
position: relative;
width: 35px;
height: 35px;
flex: 0 0 auto;
border: 1px solid rgba(89, 223, 204, 0.55);
border-radius: 50%;
background: rgba(89, 223, 204, 0.07);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
border-radius: 50%;
}
.brand-mark::before {
width: 8px;
height: 8px;
left: 8px;
top: 7px;
background: var(--cyan);
box-shadow: 10px 9px 0 rgba(255, 184, 92, 0.9);
}
.brand-mark::after {
width: 18px;
height: 18px;
left: 8px;
top: 18px;
background: #1a303b;
border: 1px solid rgba(255, 255, 255, 0.15);
}
.brand h1 {
margin: 0;
font-size: 17px;
font-weight: 720;
letter-spacing: 0.01em;
white-space: nowrap;
}
.brand p {
margin: 3px 0 0;
color: var(--muted);
font-size: 12px;
white-space: nowrap;
}
.header-badge {
display: flex;
align-items: center;
gap: 8px;
padding: 7px 10px;
border: 1px solid rgba(89, 223, 204, 0.28);
border-radius: 999px;
color: #a8eee3;
background: rgba(89, 223, 204, 0.07);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
white-space: nowrap;
}
.header-badge::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
}
.app {
display: grid;
grid-template-columns: 318px minmax(0, 1fr);
height: calc(100vh - 70px);
}
.sidebar {
overflow-y: auto;
padding: 18px;
border-right: 1px solid var(--line);
background:
linear-gradient(180deg, rgba(255, 255, 255, 0.018), transparent 30%),
#09141c;
scrollbar-width: thin;
scrollbar-color: #29404b transparent;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0 0 12px;
color: #c9d9de;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.section-title span:last-child {
color: #5f7883;
font-size: 9px;
letter-spacing: 0.08em;
}
.control-section {
margin-bottom: 17px;
padding: 14px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(15, 30, 40, 0.72);
}
.control-section.a {
border-top-color: rgba(89, 223, 204, 0.45);
}
.control-section.b {
border-top-color: rgba(184, 164, 255, 0.45);
}
.control-section.shared {
background: rgba(11, 23, 31, 0.82);
}
.field {
margin-top: 13px;
}
.field:first-of-type {
margin-top: 0;
}
.field-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 7px;
color: #aebfc6;
font-size: 12px;
}
.field-head output {
min-width: 45px;
color: #e6f1f3;
font-variant-numeric: tabular-nums;
text-align: right;
}
select,
input[type="number"] {
width: 100%;
height: 36px;
padding: 0 10px;
border: 1px solid #29404b;
border-radius: 8px;
outline: none;
color: var(--text);
background: #0d1b24;
font-size: 12px;
}
select:focus,
input[type="number"]:focus {
border-color: rgba(89, 223, 204, 0.7);
box-shadow: 0 0 0 2px rgba(89, 223, 204, 0.08);
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 7px 0 2px;
appearance: none;
border-radius: 999px;
outline: none;
background: #263c47;
}
input[type="range"]::-webkit-slider-thumb {
width: 14px;
height: 14px;
appearance: none;
border: 3px solid #10212a;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 0 1px rgba(89, 223, 204, 0.45);
}
input[type="range"]::-moz-range-thumb {
width: 10px;
height: 10px;
border: 3px solid #10212a;
border-radius: 50%;
background: var(--cyan);
}
.b input[type="range"]::-webkit-slider-thumb {
background: var(--violet);
}
.b input[type="range"]::-moz-range-thumb {
background: var(--violet);
}
.toggle-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 12px;
color: #b8c8ce;
font-size: 12px;
}
.toggle-row input {
width: 16px;
height: 16px;
accent-color: var(--cyan);
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 13px;
}
button {
min-height: 35px;
padding: 8px 10px;
border: 1px solid #29404b;
border-radius: 8px;
color: #dce9ec;
background: #10212a;
font-size: 11px;
font-weight: 650;
transition:
border-color 140ms ease,
background 140ms ease,
transform 140ms ease;
}
button:hover {
border-color: rgba(89, 223, 204, 0.55);
background: #142a35;
}
button:active {
transform: translateY(1px);
}
.comparison-card {
margin-bottom: 14px;
padding: 14px;
border: 1px solid rgba(184, 164, 255, 0.23);
border-radius: 13px;
background:
radial-gradient(circle at 100% 0%, rgba(184, 164, 255, 0.10), transparent 48%),
rgba(15, 30, 40, 0.75);
}
.comparison-row {
display: grid;
grid-template-columns: 18px 1fr 48px;
align-items: center;
gap: 8px;
margin-top: 10px;
color: #aebfc6;
font-size: 11px;
}
.comparison-row b {
color: #eff7f8;
font-variant-numeric: tabular-nums;
text-align: right;
}
.compare-track {
height: 7px;
overflow: hidden;
border-radius: 999px;
background: #20343e;
}
.compare-track i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
transition: width 220ms ease;
}
#compareBarA {
background: linear-gradient(90deg, #39bcae, var(--cyan));
}
#compareBarB {
background: linear-gradient(90deg, #897be0, var(--violet));
}
.delta {
display: flex;
align-items: baseline;
justify-content: space-between;
margin-top: 13px;
padding-top: 11px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 11px;
}
.delta strong {
color: #e6dcff;
font-size: 15px;
font-variant-numeric: tabular-nums;
}
.note {
padding: 12px;
border: 1px solid rgba(255, 184, 92, 0.20);
border-radius: 11px;
color: #a9bbc1;
background: rgba(255, 184, 92, 0.045);
font-size: 11px;
line-height: 1.55;
}
.note strong {
color: #f2d2a2;
}
.note p {
margin: 0 0 8px;
}
.note p:last-child {
margin-bottom: 0;
}
.stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #0b1720;
}
.stage canvas {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.divider {
position: absolute;
z-index: 3;
top: 0;
bottom: 0;
left: 50%;
width: 1px;
background: linear-gradient(
transparent,
rgba(170, 210, 220, 0.22) 15%,
rgba(170, 210, 220, 0.22) 85%,
transparent
);
pointer-events: none;
}
.panel-ui {
position: absolute;
z-index: 4;
top: 17px;
width: min(255px, calc(50% - 32px));
pointer-events: none;
}
.panel-ui.a {
left: 16px;
}
.panel-ui.b {
right: 16px;
}
.panel-heading {
display: flex;
align-items: center;
gap: 8px;
margin: 0 0 8px;
color: #a8bec6;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.panel-heading::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 11px rgba(89, 223, 204, 0.65);
}
.panel-ui.b .panel-heading::before {
background: var(--violet);
box-shadow: 0 0 11px rgba(184, 164, 255, 0.65);
}
.metric-card {
padding: 13px;
border: 1px solid rgba(170, 210, 220, 0.13);
border-radius: 12px;
background: rgba(8, 20, 28, 0.76);
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.16);
backdrop-filter: blur(10px);
}
.formation-name {
margin-bottom: 12px;
color: #edf7f8;
font-size: 15px;
font-weight: 700;
}
.primary-metric {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 10px;
}
.primary-metric span {
max-width: 100px;
color: #8fa7b1;
font-size: 10px;
line-height: 1.35;
}
.primary-metric strong {
color: var(--cyan);
font-size: 27px;
line-height: 1;
font-variant-numeric: tabular-nums;
}
.panel-ui.b .primary-metric strong {
color: var(--violet);
}
.metric-bar {
height: 5px;
margin-top: 10px;
overflow: hidden;
border-radius: 999px;
background: #20333d;
}
.metric-bar i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: var(--cyan);
transition: width 220ms ease;
}
.panel-ui.b .metric-bar i {
background: var(--violet);
}
.mini-metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 11px;
padding-top: 10px;
border-top: 1px solid rgba(170, 210, 220, 0.10);
}
.mini-metrics span {
color: #718a95;
font-size: 9px;
line-height: 1.4;
}
.mini-metrics b {
display: block;
margin-top: 2px;
color: #dbe8eb;
font-size: 12px;
font-variant-numeric: tabular-nums;
}
.legend {
position: absolute;
z-index: 4;
left: 50%;
bottom: 18px;
width: min(370px, calc(100% - 32px));
transform: translateX(-50%);
padding: 10px 13px;
border: 1px solid rgba(170, 210, 220, 0.13);
border-radius: 11px;
background: rgba(8, 20, 28, 0.77);
color: #90a8b1;
font-size: 10px;
text-align: center;
pointer-events: none;
backdrop-filter: blur(10px);
}
.legend-title {
margin-bottom: 7px;
color: #c8d7dc;
font-weight: 700;
}
.legend-gradient {
height: 6px;
overflow: hidden;
border-radius: 999px;
background: linear-gradient(90deg, #42d6c4, #f5ca68, #ff795f);
}
.legend-labels {
display: flex;
justify-content: space-between;
margin-top: 5px;
color: #718b95;
font-size: 9px;
}
.gesture-hint {
position: absolute;
z-index: 4;
right: 14px;
bottom: 15px;
color: rgba(174, 200, 208, 0.52);
font-size: 9px;
pointer-events: none;
}
@media (max-width: 900px) {
.app {
grid-template-columns: 280px minmax(0, 1fr);
}
.sidebar {
padding: 13px;
}
.panel-ui {
width: calc(50% - 22px);
}
.panel-ui.a {
left: 10px;
}
.panel-ui.b {
right: 10px;
}
.mini-metrics {
display: none;
}
.gesture-hint {
display: none;
}
}
@media (max-width: 680px) {
body {
overflow: auto;
}
.app-header {
height: auto;
min-height: 70px;
padding: 12px 15px;
}
.brand p {
display: none;
}
.header-badge {
font-size: 8px;
}
.app {
display: block;
height: auto;
}
.sidebar {
overflow: visible;
border-right: 0;
border-bottom: 1px solid var(--line);
}
.stage {
height: 620px;
}
.metric-card {
padding: 9px;
}
.formation-name {
margin-bottom: 8px;
font-size: 12px;
}
.primary-metric strong {
font-size: 21px;
}
.primary-metric span {
font-size: 8px;
}
.legend {
bottom: 12px;
}
}
</style>
</head>
<body>
<header class="app-header">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<h1>Boundary Form</h1>
<p>Comparing geometric exposure across configurable formations</p>
</div>
</div>
<div class="header-badge">GEOMETRY ONLY</div>
</header>
<main class="app">
<aside class="sidebar">
<section class="control-section a">
<div class="section-title">
<span>Formation A</span>
<span>Reference</span>
</div>
<div class="field">
<div class="field-head"><label for="formationA">Formation</label></div>
<select id="formationA">
<option value="circle">Circle</option>
<option value="row">Row</option>
<option value="cluster">Loose clusters</option>
</select>
</div>
<div class="field">
<div class="field-head">
<label for="countA">Agent count</label>
<output id="countAValue">16</output>
</div>
<input id="countA" type="range" min="4" max="30" step="1" value="16">
</div>
<div class="field">
<div class="field-head">
<label id="sizeALabel" for="sizeA">Circle radius</label>
<output id="sizeAValue">4.8</output>
</div>
<input id="sizeA" type="range" min="2" max="8" step="0.1" value="4.8">
</div>
</section>
<section class="control-section b">
<div class="section-title">
<span>Formation B</span>
<span>Comparison</span>
</div>
<div class="field">
<div class="field-head"><label for="formationB">Formation</label></div>
<select id="formationB">
<option value="circle">Circle</option>
<option value="row">Row</option>
<option value="cluster">Loose clusters</option>
</select>
</div>
<div class="field">
<div class="field-head">
<label for="countB">Agent count</label>
<output id="countBValue">16</output>
</div>
<input id="countB" type="range" min="4" max="30" step="1" value="16">
</div>
<div class="field">
<div class="field-head">
<label id="sizeBLabel" for="sizeB">Cluster spread</label>
<output id="sizeBValue">2.2</output>
</div>
<input id="sizeB" type="range" min="1" max="4" step="0.1" value="2.2">
</div>
</section>
<section class="control-section shared">
<div class="section-title">
<span>Shared Model</span>
<span>2D Footprints</span>
</div>
<div class="field">
<div class="field-head">
<label for="agentScale">Agent scale</label>
<output id="agentScaleValue">1.00×</output>
</div>
<input id="agentScale" type="range" min="0.75" max="1.25" step="0.01" value="1">
</div>
<div class="field">
<div class="field-head">
<label for="jitter">Position jitter</label>
<output id="jitterValue">12%</output>
</div>
<input id="jitter" type="range" min="0" max="0.8" step="0.01" value="0.12">
</div>
<div class="field">
<div class="field-head">
<label for="clusterGroups">Cluster groups</label>
<output id="clusterGroupsValue">4</output>
</div>
<input id="clusterGroups" type="range" min="2" max="6" step="1" value="4">
</div>
<div class="field">
<div class="field-head">
<label for="seed">Arrangement seed</label>
<output id="seedValue">7</output>
</div>
<input id="seed" type="number" min="1" max="9999" value="7">
</div>
<div class="button-row">
<button id="randomize" type="button">New seed</button>
<button id="frame" type="button">Frame both</button>
</div>
<div class="toggle-row">
<label for="linkedCounts">Link agent counts</label>
<input id="linkedCounts" type="checkbox" checked>
</div>
<div class="toggle-row">
<label for="halos">Show exposure halos</label>
<input id="halos" type="checkbox" checked>
</div>
</section>
<section class="comparison-card">
<div class="section-title">
<span>Comparison</span>
<span>Open Perimeter</span>
</div>
<div class="comparison-row">
<span>A</span>
<div class="compare-track"><i id="compareBarA"></i></div>
<b id="compareValueA">—</b>
</div>
<div class="comparison-row">
<span>B</span>
<div class="compare-track"><i id="compareBarB"></i></div>
<b id="compareValueB">—</b>
</div>
<div class="delta">
<span>B − A difference</span>
<strong id="compareDelta">—</strong>
</div>
</section>
<section class="note">
<p>
<strong>Model definition.</strong> Geometric boundary exposure is the mean
percentage of an agent’s planar perimeter that is not intersected by another
agent’s equal circular footprint. Visibility is sampled with 72 radial rays.
</p>
<p>
This is a geometry-only visualization. It does not model or predict body
temperature, thermal comfort, mortality, or survival. The penguin-like
silhouettes are decorative.
</p>
</section>
</aside>
<section class="stage" id="stage" aria-label="Interactive formation comparison">
<div class="divider"></div>
<div class="panel-ui a">
<div class="panel-heading">Formation A</div>
<div class="metric-card">
<div class="formation-name" id="nameA">Circle</div>
<div class="primary-metric">
<span>Mean geometric boundary exposure</span>
<strong id="exposureA">—</strong>
</div>
<div class="metric-bar"><i id="barA"></i></div>
<div class="mini-metrics">
<span>Convex hull vertices<b id="hullA">—</b></span>
<span>Median agent exposure<b id="medianA">—</b></span>
</div>
</div>
</div>
<div class="panel-ui b">
<div class="panel-heading">Formation B</div>
<div class="metric-card">
<div class="formation-name" id="nameB">Loose clusters</div>
<div class="primary-metric">
<span>Mean geometric boundary exposure</span>
<strong id="exposureB">—</strong>
</div>
<div class="metric-bar"><i id="barB"></i></div>
<div class="mini-metrics">
<span>Convex hull vertices<b id="hullB">—</b></span>
<span>Median agent exposure<b id="medianB">—</b></span>
</div>
</div>
</div>
<div class="legend">
<div class="legend-title">Per-agent exposure halo</div>
<div class="legend-gradient"></div>
<div class="legend-labels">
<span>More blocked</span>
<span>More open</span>
</div>
</div>
<div class="gesture-hint">Drag: orbit · Shift/right-drag: pan · Wheel: zoom</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 stage = document.getElementById("stage");
const ui = {
formationA: document.getElementById("formationA"),
formationB: document.getElementById("formationB"),
countA: document.getElementById("countA"),
countB: document.getElementById("countB"),
countAValue: document.getElementById("countAValue"),
countBValue: document.getElementById("countBValue"),
sizeA: document.getElementById("sizeA"),
sizeB: document.getElementById("sizeB"),
sizeALabel: document.getElementById("sizeALabel"),
sizeBLabel: document.getElementById("sizeBLabel"),
sizeAValue: document.getElementById("sizeAValue"),
sizeBValue: document.getElementById("sizeBValue"),
agentScale: document.getElementById("agentScale"),
agentScaleValue: document.getElementById("agentScaleValue"),
jitter: document.getElementById("jitter"),
jitterValue: document.getElementById("jitterValue"),
clusterGroups: document.getElementById("clusterGroups"),
clusterGroupsValue: document.getElementById("clusterGroupsValue"),
seed: document.getElementById("seed"),
seedValue: document.getElementById("seedValue"),
linkedCounts: document.getElementById("linkedCounts"),
halos: document.getElementById("halos"),
randomize: document.getElementById("randomize"),
frame: document.getElementById("frame"),
nameA: document.getElementById("nameA"),
nameB: document.getElementById("nameB"),
exposureA: document.getElementById("exposureA"),
exposureB: document.getElementById("exposureB"),
barA: document.getElementById("barA"),
barB: document.getElementById("barB"),
hullA: document.getElementById("hullA"),
hullB: document.getElementById("hullB"),
medianA: document.getElementById("medianA"),
medianB: document.getElementById("medianB"),
compareBarA: document.getElementById("compareBarA"),
compareBarB: document.getElementById("compareBarB"),
compareValueA: document.getElementById("compareValueA"),
compareValueB: document.getElementById("compareValueB"),
compareDelta: document.getElementById("compareDelta")
};
const formationNames = {
circle: "Circle",
row: "Row",
cluster: "Loose clusters"
};
const defaultSizes = {
circle: 4.8,
row: 1.05,
cluster: 2.2
};
const sizeDefinitions = {
circle: {
label: "Circle radius",
min: 2,
max: 8,
step: 0.1
},
row: {
label: "Row spacing",
min: 0.7,
max: 1.8,
step: 0.05
},
cluster: {
label: "Cluster spread",
min: 1,
max: 4,
step: 0.1
}
};
const config = {
a: { formation: "circle", count: 16, size: 4.8 },
b: { formation: "cluster", count: 16, size: 2.2 },
agentScale: 1,
jitter: 0.12,
clusterGroups: 4,
seed: 7,
linkedCounts: true,
halos: true
};
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Two synchronized 3D views of low-poly penguin-like formations"
);
renderer.domElement.tabIndex = 0;
stage.prepend(renderer.domElement);
const orbit = {
yaw: 0.58,
pitch: 0.64,
distance: 23,
target: new THREE.Vector3(0, 0.65, 0)
};
function createCamera() {
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
camera.position.set(12, 12, 17);
return camera;
}
const cameraA = createCamera();
const cameraB = createCamera();
function createScene() {
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0b1720);
scene.fog = new THREE.Fog(0x0b1720, 22, 52);
const hemisphere = new THREE.HemisphereLight(0xcbe9f3, 0x17232b, 2.1);
scene.add(hemisphere);
const key = new THREE.DirectionalLight(0xfff1d7, 3.2);
key.position.set(8, 15, 10);
key.castShadow = true;
key.shadow.mapSize.set(1024, 1024);
key.shadow.camera.left = -20;
key.shadow.camera.right = 20;
key.shadow.camera.top = 20;
key.shadow.camera.bottom = -20;
key.shadow.camera.near = 1;
key.shadow.camera.far = 45;
key.shadow.bias = -0.0008;
scene.add(key);
const fill = new THREE.DirectionalLight(0x77cddd, 1.0);
fill.position.set(-10, 7, -8);
scene.add(fill);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x10232d,
roughness: 0.96,
metalness: 0.02
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(70, 70),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.025;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(50, 50, 0x31505b, 0x1b333d);
grid.position.y = -0.008;
grid.material.transparent = true;
grid.material.opacity = 0.3;
grid.material.depthWrite = false;
scene.add(grid);
const agentRoot = new THREE.Group();
const hullRoot = new THREE.Group();
scene.add(agentRoot, hullRoot);
return {
scene,
agentRoot,
hullRoot,
positions: [],
metrics: null
};
}
const viewA = createScene();
const viewB = createScene();
const geometries = {
body: new THREE.SphereGeometry(1, 8, 5),
head: new THREE.SphereGeometry(1, 7, 5),
belly: new THREE.SphereGeometry(1, 8, 5),
eye: new THREE.SphereGeometry(1, 6, 4),
beak: new THREE.ConeGeometry(0.115, 0.27, 4),
flipper: new THREE.ConeGeometry(0.12, 0.54, 4),
foot: new THREE.ConeGeometry(0.15, 0.31, 4),
halo: new THREE.RingGeometry(0.55, 0.71, 24)
};
const materials = {
body: new THREE.MeshStandardMaterial({
color: 0x20313d,
roughness: 0.88,
metalness: 0.02,
flatShading: true
}),
bodyAlt: new THREE.MeshStandardMaterial({
color: 0x293d49,
roughness: 0.9,
metalness: 0.01,
flatShading: true
}),
belly: new THREE.MeshStandardMaterial({
color: 0xe9dfc7,
roughness: 0.9,
metalness: 0,
flatShading: true
}),
eye: new THREE.MeshStandardMaterial({
color: 0x05090b,
roughness: 0.45,
metalness: 0.02,
flatShading: true
}),
beak: new THREE.MeshStandardMaterial({
color: 0xf4a63b,
roughness: 0.72,
metalness: 0.02,
flatShading: true
}),
foot: new THREE.MeshStandardMaterial({
color: 0xe8892f,
roughness: 0.78,
metalness: 0.01,
flatShading: true
})
};
const lowColor = new THREE.Color(0x42d6c4);
const highColor = new THREE.Color(0xff795f);
function mulberry32(seed) {
let state = seed >>> 0;
return function () {
state += 0x6d2b79f5;
let t = state;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
function formationSeed(side, formation) {
const offset = side === "a" ? 101 : 701;
const formationOffset = {
circle: 17,
row: 53,
cluster: 97
}[formation];
return (
config.seed * 997 +
offset +
formationOffset * 131
);
}
function generatePositions(side) {
const settings = config[side];
const random = mulberry32(formationSeed(side, settings.formation));
const positions = [];
const count = settings.count;
const scale = config.agentScale;
const jitter = config.jitter;
if (settings.formation === "circle") {
const radius = settings.size;
for (let i = 0; i < count; i++) {
const angle =
(i / count) * Math.PI * 2 +
(random() - 0.5) * jitter * 0.16;
const radialOffset =
(random() - 0.5) * jitter * 0.7 * scale;
const r = radius + radialOffset;
positions.push({
x: Math.cos(angle) * r,
z: Math.sin(angle) * r
});
}
} else if (settings.formation === "row") {
const spacing = settings.size * scale;
const center = (count - 1) * 0.5;
for (let i = 0; i < count; i++) {
positions.push({
x: (i - center) * spacing,
z: (random() - 0.5) * jitter * 0.95 * scale
});
}
} else {
const groups = Math.min(config.clusterGroups, count);
const centerRadius = settings.size * 0.95;
const localRadius = settings.size * 0.23 + 0.34 * scale;
const groupRotation = random() * Math.PI * 2;
const goldenAngle = Math.PI * (3 - Math.sqrt(5));
for (let i = 0; i < count; i++) {
const group = i % groups;
const groupAngle =
groupRotation + (group / groups) * Math.PI * 2;
const groupX = Math.cos(groupAngle) * centerRadius;
const groupZ = Math.sin(groupAngle) * centerRadius;
const localAngle =
group * goldenAngle +
i * 2.399963 +
(random() - 0.5) * 0.7;
const localR =
Math.sqrt(random()) * localRadius +
(random() - 0.5) * jitter * localRadius * 0.55;
positions.push({
x:
groupX +
Math.cos(localAngle) * localR +
(random() - 0.5) * jitter * 0.28 * scale,
z:
groupZ +
Math.sin(localAngle) * localR +
(random() - 0.5) * jitter * 0.28 * scale
});
}
}
return positions;
}
function convexHull(points) {
if (points.length <= 2) {
return points.map((_, index) => index);
}
const sorted = points
.map((point, index) => ({ point, index }))
.sort(
(a, b) =>
a.point.x - b.point.x ||
a.point.z - b.point.z
);
const cross = (o, a, b) =>
(a.point.x - o.point.x) * (b.point.z - o.point.z) -
(a.point.z - o.point.z) * (b.point.x - o.point.x);
const lower = [];
for (const item of sorted) {
while (
lower.length >= 2 &&
cross(lower[lower.length - 2], lower[lower.length - 1], item) <= 0
) {
lower.pop();
}
lower.push(item);
}
const upper = [];
for (let i = sorted.length - 1; i >= 0; i--) {
const item = sorted[i];
while (
upper.length >= 2 &&
cross(upper[upper.length - 2], upper[upper.length - 1], item) <= 0
) {
upper.pop();
}
upper.push(item);
}
lower.pop();
upper.pop();
return lower.concat(upper).map((item) => item.index);
}
function hullMetrics(points) {
const hullIndices = convexHull(points);
if (hullIndices.length < 2) {
return {
indices: hullIndices,
perimeter: 0,
area: 0
};
}
let perimeter = 0;
let twiceArea = 0;
for (let i = 0; i < hullIndices.length; i++) {
const a = points[hullIndices[i]];
const b = points[hullIndices[(i + 1) % hullIndices.length]];
perimeter += Math.hypot(b.x - a.x, b.z - a.z);
twiceArea += a.x * b.z - b.x * a.z;
}
return {
indices: hullIndices,
perimeter,
area: Math.abs(twiceArea) * 0.5
};
}
function rayHitsDisk(start, center, direction, radius) {
const vx = center.x - start.x;
const vz = center.z - start.z;
const projection = vx * direction.x + vz * direction.z;
const perpendicularSquared =
vx * vx + vz * vz - projection * projection;
if (perpendicularSquared > radius * radius) {
return false;
}
const halfChord = Math.sqrt(
Math.max(0, radius * radius - perpendicularSquared)
);
const near = projection - halfChord;
const far = projection + halfChord;
// The forward ray intersects the disk.
return far >= 0;
}
function calculateExposure(positions, radius) {
const samples = 72;
const exposure = new Array(positions.length).fill(0);
for (let i = 0; i < positions.length; i++) {
const agent = positions[i];
let openSamples = 0;
for (let sample = 0; sample < samples; sample++) {
const angle = (sample / samples) * Math.PI * 2;
const direction = {
x: Math.cos(angle),
z: Math.sin(angle)
};
const start = {
x: agent.x + direction.x * radius,
z: agent.z + direction.z * radius
};
let blocked = false;
for (let j = 0; j < positions.length; j++) {
if (i === j) continue;
if (
rayHitsDisk(start, positions[j], direction, radius)
) {
blocked = true;
break;
}
}
if (!blocked) openSamples++;
}
exposure[i] = openSamples / samples;
}
const mean =
exposure.reduce((sum, value) => sum + value, 0) /
exposure.length;
const sorted = [...exposure].sort((a, b) => a - b);
const middle = Math.floor(sorted.length / 2);
const median =
sorted.length % 2 === 0
? (sorted[middle - 1] + sorted[middle]) * 0.5
: sorted[middle];
return {
exposure,
mean,
median,
min: sorted[0],
max: sorted[sorted.length - 1]
};
}
function clearOwned(group) {
while (group.children.length) {
const child = group.children.pop();
child.traverse((object) => {
if (object.userData?.ownedGeometry) {
object.geometry?.dispose();
}
if (object.userData?.ownedMaterial) {
object.material?.dispose();
}
});
}
}
function addMesh(parent, geometry, material, position, scale, rotation) {
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(...position);
mesh.scale.set(...scale);
mesh.rotation.set(...rotation);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createPenguin(position, exposure, formation, index) {
const group = new THREE.Group();
group.position.set(position.x, 0, position.z);
group.scale.setScalar(config.agentScale);
const bodyMaterial =
index % 4 === 0 ? materials.bodyAlt : materials.body;
addMesh(
group,
geometries.body,
bodyMaterial,
[0, 0.72, 0],
[0.43, 0.67, 0.36],
[0, 0, 0]
);
addMesh(
group,
geometries.head,
bodyMaterial,
[0, 1.38, 0.015],
[0.39, 0.39, 0.37],
[0, 0, 0]
);
addMesh(
group,
geometries.belly,
materials.belly,
[0, 0.69, 0.305],
[0.285, 0.47, 0.075],
[0, 0, 0]
);
addMesh(
group,
geometries.beak,
materials.beak,
[0, 1.31, 0.59],
[1, 1, 1],
[Math.PI / 2, 0, 0]
);
addMesh(
group,
geometries.eye,
materials.eye,
[-0.145, 1.49, 0.325],
[0.052, 0.058, 0.04],
[0, 0, 0]
);
addMesh(
group,
geometries.eye,
materials.eye,
[0.145, 1.49, 0.325],
[0.052, 0.058, 0.04],
[0, 0, 0]
);
addMesh(
group,
geometries.flipper,
bodyMaterial,
[-0.43, 0.74, 0],
[1, 1, 1],
[0, 0, Math.PI - 0.17]
);
addMesh(
group,
geometries.flipper,
bodyMaterial,
[0.43, 0.74, 0],
[1, 1, 1],
[0, 0, Math.PI + 0.17]
);
addMesh(
group,
geometries.foot,
materials.foot,
[-0.18, 0.08, 0.14],
[1, 1, 1],
[Math.PI / 2, 0, 0]
);
addMesh(
group,
geometries.foot,
materials.foot,
[0.18, 0.08, 0.14],
[1, 1, 1],
[Math.PI / 2, 0, 0]
);
const haloMaterial = new THREE.MeshBasicMaterial({
color: lowColor.clone().lerp(highColor, exposure),
transparent: true,
opacity: 0.78,
depthWrite: false,
side: THREE.DoubleSide
});
haloMaterial.userData.ownedMaterial = true;
const halo = new THREE.Mesh(geometries.halo, haloMaterial);
halo.rotation.x = -Math.PI / 2;
halo.position.y = 0.014;
halo.visible = config.halos;
halo.userData.ownedMaterial = true;
group.add(halo);
if (formation === "circle" || formation === "cluster") {
group.rotation.y = Math.atan2(position.x, position.z);
}
return group;
}
function createHullVisual(points, hullIndices) {
const root = viewA.hullRoot === null ? null : null;
// The caller supplies the appropriate root through the returned object.
return { root, hullIndices };
}
function addHullToScene(sceneData, hull) {
clearOwned(sceneData.hullRoot);
if (hull.indices.length < 2) return;
const points = hull.indices.map(
(index) => sceneData.positions[index]
);
const lineGeometry = new THREE.BufferGeometry().setFromPoints(
points.map(
(point) => new THREE.Vector3(point.x, 0.045, point.z)
)
);
const lineMaterial = new THREE.LineBasicMaterial({
color: 0x5eead4,
transparent: true,
opacity: 0.7
});
const line = new THREE.LineLoop(lineGeometry, lineMaterial);
line.userData.ownedGeometry = true;
line.userData.ownedMaterial = true;
sceneData.hullRoot.add(line);
if (hull.indices.length >= 3) {
const vertices = [];
const center = points.reduce(
(sum, point) => ({
x: sum.x + point.x / points.length,
z: sum.z + point.z / points.length
}),
{ x: 0, z: 0 }
);
for (let i = 0; i < points.length; i++) {
const a = points[i];
const b = points[(i + 1) % points.length];
vertices.push(
center.x, 0.028, center.z,
a.x, 0.028, a.z,
b.x, 0.028, b.z
);
}
const fillGeometry = new THREE.BufferGeometry();
fillGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(vertices, 3)
);
fillGeometry.computeVertexNormals();
const fillMaterial = new THREE.MeshBasicMaterial({
color: 0x59dfcc,
transparent: true,
opacity: 0.055,
depthWrite: false,
side: THREE.DoubleSide
});
const fill = new THREE.Mesh(fillGeometry, fillMaterial);
fill.userData.ownedGeometry = true;
fill.userData.ownedMaterial = true;
sceneData.hullRoot.add(fill);
}
}
function rebuildView(sceneData, side) {
const settings = config[side];
const positions = generatePositions(side);
const radius = 0.5 * config.agentScale;
const exposure = calculateExposure(positions, radius);
const hull = hullMetrics(positions);
clearOwned(sceneData.agentRoot);
positions.forEach((position, index) => {
sceneData.agentRoot.add(
createPenguin(
position,
exposure.exposure[index],
settings.formation,
index
)
);
});
sceneData.positions = positions;
sceneData.metrics = {
...exposure,
hull
};
addHullToScene(sceneData, hull);
}
function updateSizeControl(side) {
const settings = config[side];
const definition = sizeDefinitions[settings.formation];
const input = side === "a" ? ui.sizeA : ui.sizeB;
const label = side === "a" ? ui.sizeALabel : ui.sizeBLabel;
const output = side === "a" ? ui.sizeAValue : ui.sizeBValue;
input.min = definition.min;
input.max = definition.max;
input.step = definition.step;
input.value = settings.size;
label.textContent = definition.label;
output.textContent = settings.size.toFixed(
definition.step < 0.1 ? 2 : 1
);
}
function syncControls() {
ui.formationA.value = config.a.formation;
ui.formationB.value = config.b.formation;
ui.countA.value = config.a.count;
ui.countB.value = config.b.count;
ui.countAValue.textContent = config.a.count;
ui.countBValue.textContent = config.b.count;
updateSizeControl("a");
updateSizeControl("b");
ui.agentScale.value = config.agentScale;
ui.agentScaleValue.textContent = `${config.agentScale.toFixed(2)}×`;
ui.jitter.value = config.jitter;
ui.jitterValue.textContent = `${Math.round(config.jitter * 100)}%`;
ui.clusterGroups.value = config.clusterGroups;
ui.clusterGroupsValue.textContent = config.clusterGroups;
ui.seed.value = config.seed;
ui.seedValue.textContent = config.seed;
ui.linkedCounts.checked = config.linkedCounts;
ui.halos.checked = config.halos;
ui.nameA.textContent = formationNames[config.a.formation];
ui.nameB.textContent = formationNames[config.b.formation];
}
function updateMetrics() {
function updatePanel(side, view) {
const metrics = view.metrics;
if (!metrics) return;
const percent = metrics.mean * 100;
const median = metrics.median * 100;
const exposure = side === "a" ? ui.exposureA : ui.exposureB;
const bar = side === "a" ? ui.barA : ui.barB;
const hull = side === "a" ? ui.hullA : ui.hullB;
const medianElement =
side === "a" ? ui.medianA : ui.medianB;
exposure.textContent = `${percent.toFixed(1)}%`;
bar.style.width = `${percent}%`;
hull.textContent = `${metrics.hull.indices.length} / ${view.positions.length}`;
medianElement.textContent = `${median.toFixed(1)}%`;
}
updatePanel("a", viewA);
updatePanel("b", viewB);
const a = viewA.metrics?.mean || 0;
const b = viewB.metrics?.mean || 0;
ui.compareBarA.style.width = `${a * 100}%`;
ui.compareBarB.style.width = `${b * 100}%`;
ui.compareValueA.textContent = `${(a * 100).toFixed(1)}%`;
ui.compareValueB.textContent = `${(b * 100).toFixed(1)}%`;
const difference = (b - a) * 100;
const sign = difference > 0 ? "+" : difference < 0 ? "−" : "±";
ui.compareDelta.textContent = `${sign}${Math.abs(difference).toFixed(1)} pp`;
}
let dirtyA = true;
let dirtyB = true;
function markDirty(side) {
if (side === "a") dirtyA = true;
if (side === "b") dirtyB = true;
}
function markBothDirty() {
dirtyA = true;
dirtyB = true;
}
ui.formationA.addEventListener("change", () => {
config.a.formation = ui.formationA.value;
config.a.size = defaultSizes[config.a.formation];
syncControls();
markDirty("a");
});
ui.formationB.addEventListener("change", () => {
config.b.formation = ui.formationB.value;
config.b.size = defaultSizes[config.b.formation];
syncControls();
markDirty("b");
});
ui.countA.addEventListener("input", () => {
config.a.count = Number(ui.countA.value);
if (config.linkedCounts) {
config.b.count = config.a.count;
markDirty("b");
}
ui.countAValue.textContent = config.a.count;
ui.countBValue.textContent = config.b.count;
ui.countB.value = config.b.count;
markDirty("a");
});
ui.countB.addEventListener("input", () => {
config.b.count = Number(ui.countB.value);
if (config.linkedCounts) {
config.a.count = config.b.count;
markDirty("a");
}
ui.countBValue.textContent = config.b.count;
ui.countAValue.textContent = config.a.count;
ui.countA.value = config.a.count;
markDirty("b");
});
ui.sizeA.addEventListener("input", () => {
config.a.size = Number(ui.sizeA.value);
ui.sizeAValue.textContent = config.a.size.toFixed(1);
markDirty("a");
});
ui.sizeB.addEventListener("input", () => {
config.b.size = Number(ui.sizeB.value);
ui.sizeBValue.textContent = config.b.size.toFixed(1);
markDirty("b");
});
ui.agentScale.addEventListener("input", () => {
config.agentScale = Number(ui.agentScale.value);
ui.agentScaleValue.textContent = `${config.agentScale.toFixed(2)}×`;
markBothDirty();
});
ui.jitter.addEventListener("input", () => {
config.jitter = Number(ui.jitter.value);
ui.jitterValue.textContent = `${Math.round(config.jitter * 100)}%`;
markBothDirty();
});
ui.clusterGroups.addEventListener("input", () => {
config.clusterGroups = Number(ui.clusterGroups.value);
ui.clusterGroupsValue.textContent = config.clusterGroups;
markBothDirty();
});
ui.seed.addEventListener("input", () => {
const value = Number(ui.seed.value);
config.seed = Number.isFinite(value) ? Math.max(1, value) : 1;
ui.seed.value = config.seed;
ui.seedValue.textContent = config.seed;
markBothDirty();
});
ui.randomize.addEventListener("click", () => {
config.seed = 1 + Math.floor(Math.random() * 9998);
ui.seed.value = config.seed;
ui.seedValue.textContent = config.seed;
markBothDirty();
});
ui.linkedCounts.addEventListener("change", () => {
config.linkedCounts = ui.linkedCounts.checked;
});
ui.halos.addEventListener("change", () => {
config.halos = ui.halos.checked;
for (const view of [viewA, viewB]) {
view.agentRoot.traverse((object) => {
if (object.geometry === geometries.halo) {
object.visible = config.halos;
}
});
}
});
function frameBoth() {
const allPositions = [...viewA.positions, ...viewB.positions];
if (!allPositions.length) return;
const maximumExtent = allPositions.reduce(
(maximum, point) =>
Math.max(maximum, Math.abs(point.x), Math.abs(point.z)),
0
);
orbit.distance = THREE.MathUtils.clamp(
Math.max(12, maximumExtent * 2.7),
12,
45
);
orbit.target.set(0, 0.65, 0);
}
ui.frame.addEventListener("click", frameBoth);
// Custom camera controls: no examples/addons are imported.
let dragging = false;
let panning = false;
let lastX = 0;
let lastY = 0;
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
panning = event.shiftKey || event.button === 2;
lastX = event.clientX;
lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = panning ? "move" : "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
if (panning) {
const panScale = orbit.distance * 0.00125;
const right = new THREE.Vector3(
Math.cos(orbit.yaw),
0,
-Math.sin(orbit.yaw)
);
const forward = new THREE.Vector3(
-Math.sin(orbit.yaw),
0,
-Math.cos(orbit.yaw)
);
orbit.target
.addScaledVector(right, -dx * panScale)
.addScaledVector(forward, -dy * panScale);
orbit.target.x = THREE.MathUtils.clamp(
orbit.target.x,
-10,
10
);
orbit.target.z = THREE.MathUtils.clamp(
orbit.target.z,
-10,
10
);
} else {
orbit.yaw -= dx * 0.006;
orbit.pitch = THREE.MathUtils.clamp(
orbit.pitch + dy * 0.005,
0.18,
1.28
);
}
});
function endDrag(event) {
dragging = false;
panning = false;
renderer.domElement.style.cursor = "grab";
if (event.pointerId !== undefined) {
try {
renderer.domElement.releasePointerCapture(event.pointerId);
} catch {
// Pointer capture may already have been released.
}
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.distance = THREE.MathUtils.clamp(
orbit.distance * Math.exp(event.deltaY * 0.001),
7,
45
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
orbit.yaw = 0.58;
orbit.pitch = 0.64;
orbit.target.set(0, 0.65, 0);
frameBoth();
});
renderer.domElement.style.cursor = "grab";
function updateCamera(camera, aspect) {
camera.aspect = aspect;
camera.updateProjectionMatrix();
const horizontal = orbit.distance * Math.cos(orbit.pitch);
camera.position.set(
orbit.target.x + horizontal * Math.sin(orbit.yaw),
orbit.target.y + orbit.distance * Math.sin(orbit.pitch),
orbit.target.z + horizontal * Math.cos(orbit.yaw)
);
camera.lookAt(orbit.target);
}
function resize() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
renderer.setSize(width, height, false);
}
new ResizeObserver(resize).observe(stage);
resize();
syncControls();
rebuildView(viewA, "a");
rebuildView(viewB, "b");
updateMetrics();
function animate() {
requestAnimationFrame(animate);
if (dirtyA) {
rebuildView(viewA, "a");
dirtyA = false;
}
if (dirtyB) {
rebuildView(viewB, "b");
dirtyB = false;
}
updateMetrics();
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
const half = Math.floor(width / 2);
updateCamera(cameraA, half / height);
updateCamera(cameraB, (width - half) / height);
renderer.setScissorTest(true);
renderer.setViewport(0, 0, half, height);
renderer.setScissor(0, 0, half, height);
renderer.render(viewA.scene, cameraA);
renderer.setViewport(half, 0, width - half, height);
renderer.setScissor(half, 0, width - half, height);
renderer.render(viewB.scene, cameraB);
renderer.setScissorTest(false);
}
animate();
</script>
</body>
</html>
```