Prompt specification
Build or customize the Three.js 3D scene "Two Players Shared Bridge Contract" 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: Two Players Shared Bridge Contract
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-players-shared-bridge-contract-x3553
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows negotiated agreements between game players. Model two player tokens, separate material piles, and a shared bridge blueprint. Show each required contribution and keep ownership distinct until an agreed transfer occurs. Stylized solid forms.
Working Three.js Starter (two-players-shared-bridge-contract-x3553):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bridge Pact — Negotiated Contributions</title>
<style>
:root {
--ink: #18292d;
--muted: #6f7c7c;
--paper: #f3f0e8;
--glass: rgba(250, 248, 242, 0.9);
--p1: #168c87;
--p1-soft: #d7eeea;
--p2: #dd694f;
--p2-soft: #f8dfd8;
--shared: #b8893f;
--line: rgba(24, 41, 45, 0.13);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8e4da;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button { font: inherit; }
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active { cursor: grabbing; }
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 44%, transparent 40%, rgba(38, 48, 45, 0.08) 100%),
linear-gradient(to bottom, rgba(255,255,255,0.16), transparent 22%);
}
.topbar {
position: fixed;
z-index: 5;
top: 0;
left: 0;
right: 0;
padding: 22px 26px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
}
.brand-mark {
width: 42px;
height: 42px;
border-radius: 13px;
display: grid;
place-items: center;
color: white;
background: var(--ink);
box-shadow: 0 8px 22px rgba(24, 41, 45, 0.18);
}
.brand-mark svg {
width: 25px;
height: 25px;
fill: none;
stroke: currentColor;
stroke-width: 1.8;
stroke-linecap: round;
stroke-linejoin: round;
}
.eyebrow {
margin: 0 0 3px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(20px, 2.2vw, 30px);
line-height: 1;
letter-spacing: -0.04em;
}
.top-right {
display: flex;
align-items: center;
gap: 10px;
pointer-events: auto;
}
.deal-pill {
min-width: 220px;
padding: 10px 14px;
border: 1px solid rgba(255,255,255,0.72);
border-radius: 14px;
background: var(--glass);
box-shadow: 0 9px 30px rgba(30, 43, 41, 0.09);
backdrop-filter: blur(14px);
}
.deal-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
#statusTitle {
font-size: 12px;
font-weight: 800;
letter-spacing: -0.01em;
}
.signatures {
display: flex;
gap: 5px;
}
.signature-dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: #c9ceca;
transition: 0.25s ease;
}
.signature-dot.p1.on { background: var(--p1); }
.signature-dot.p2.on { background: var(--p2); }
#statusDetail {
margin-top: 4px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.icon-button {
width: 42px;
height: 42px;
border: 1px solid rgba(255,255,255,0.8);
border-radius: 13px;
background: var(--glass);
color: var(--ink);
cursor: pointer;
box-shadow: 0 8px 22px rgba(24, 41, 45, 0.09);
backdrop-filter: blur(12px);
transition: transform 0.15s ease, background 0.15s ease;
}
.icon-button:hover {
transform: translateY(-2px);
background: white;
}
.hint {
position: fixed;
z-index: 4;
top: 86px;
left: 50%;
transform: translateX(-50%);
padding: 7px 12px;
border-radius: 999px;
color: rgba(24, 41, 45, 0.67);
background: rgba(248, 246, 239, 0.68);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
pointer-events: none;
backdrop-filter: blur(8px);
}
.contracts {
position: fixed;
z-index: 6;
left: 50%;
bottom: 20px;
width: min(930px, calc(100% - 30px));
transform: translateX(-50%);
display: grid;
grid-template-columns: 1fr 1fr;
gap: 16px;
pointer-events: none;
}
.contract {
--accent: var(--p1);
--accent-soft: var(--p1-soft);
position: relative;
overflow: hidden;
padding: 15px 16px 14px;
border: 1px solid rgba(255,255,255,0.78);
border-top: 3px solid var(--accent);
border-radius: 18px;
background: var(--glass);
box-shadow: 0 16px 45px rgba(24, 41, 45, 0.14);
backdrop-filter: blur(17px);
pointer-events: auto;
transition: border-color 0.25s ease, transform 0.25s ease, box-shadow 0.25s ease;
}
.contract.p2 {
--accent: var(--p2);
--accent-soft: var(--p2-soft);
}
.contract.ready {
box-shadow: 0 17px 50px rgba(24, 41, 45, 0.17),
0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}
.contract.accepted {
border-top-color: var(--accent);
background:
linear-gradient(135deg, color-mix(in srgb, var(--accent-soft) 64%, transparent), transparent 58%),
var(--glass);
}
.contract-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
.player-id {
display: flex;
align-items: center;
gap: 9px;
}
.player-swatch {
width: 12px;
height: 12px;
border-radius: 4px;
background: var(--accent);
box-shadow: 0 0 0 4px var(--accent-soft);
}
.player-name {
font-size: 14px;
font-weight: 850;
letter-spacing: -0.02em;
}
.contract-state {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.required {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-top: 12px;
}
.requirement {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 5px 8px;
border: 1px solid var(--line);
border-radius: 8px;
color: var(--muted);
background: rgba(255,255,255,0.48);
font-size: 10px;
font-weight: 800;
transition: 0.2s ease;
}
.requirement.met {
color: var(--accent);
border-color: color-mix(in srgb, var(--accent) 35%, transparent);
background: var(--accent-soft);
}
.material-icon {
width: 9px;
height: 9px;
border-radius: 2px;
background: #b68145;
}
.material-icon.stone {
border-radius: 50%;
background: #8d999a;
}
.offer-row {
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
gap: 10px;
min-height: 35px;
margin-top: 11px;
padding-top: 10px;
border-top: 1px solid var(--line);
}
.offer-label {
margin-bottom: 6px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.offer-chips {
display: flex;
flex-wrap: wrap;
gap: 5px;
min-height: 26px;
align-items: center;
}
.offer-chip {
padding: 5px 8px;
border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
border-radius: 7px;
color: var(--accent);
background: var(--accent-soft);
font-size: 9px;
font-weight: 850;
cursor: pointer;
}
.offer-chip:hover { filter: brightness(0.97); }
.empty-offer {
color: #9aa3a1;
font-size: 10px;
font-style: italic;
}
.inventory {
display: flex;
gap: 6px;
}
.inventory button {
min-width: 72px;
padding: 8px 9px;
border: 1px solid var(--line);
border-radius: 10px;
color: var(--ink);
background: rgba(255,255,255,0.68);
cursor: pointer;
transition: 0.15s ease;
}
.inventory button:hover:not(:disabled) {
border-color: var(--accent);
transform: translateY(-1px);
}
.inventory button:disabled { opacity: 0.45; cursor: default; }
.inventory strong {
display: block;
font-size: 10px;
line-height: 1.15;
}
.inventory small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.accept {
width: 100%;
margin-top: 11px;
padding: 10px 12px;
border: 0;
border-radius: 10px;
color: white;
background: var(--accent);
box-shadow: 0 6px 15px color-mix(in srgb, var(--accent) 23%, transparent);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
transition: transform 0.15s ease, filter 0.15s ease;
}
.accept:hover:not(:disabled) {
transform: translateY(-1px);
filter: brightness(1.05);
}
.accept:disabled {
color: #8a9491;
background: #dfe2dd;
box-shadow: none;
cursor: not-allowed;
}
.accept.accepted-button {
color: var(--accent);
background: var(--accent-soft);
}
.toast {
position: fixed;
z-index: 20;
left: 50%;
top: 122px;
transform: translate(-50%, -12px);
padding: 10px 15px;
border-radius: 10px;
color: white;
background: rgba(24, 41, 45, 0.91);
box-shadow: 0 10px 30px rgba(24,41,45,0.18);
font-size: 11px;
font-weight: 700;
opacity: 0;
pointer-events: none;
transition: 0.25s ease;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 720px) {
.topbar { padding: 14px; }
.brand-mark { width: 36px; height: 36px; border-radius: 11px; }
.brand-mark svg { width: 21px; height: 21px; }
.eyebrow { font-size: 8px; }
h1 { font-size: 19px; }
.deal-pill { min-width: 0; width: 154px; padding: 8px 10px; }
#statusTitle { font-size: 9px; }
#statusDetail { display: none; }
.icon-button { width: 36px; height: 36px; }
.hint { top: 68px; font-size: 8px; white-space: nowrap; }
.contracts {
bottom: 9px;
width: calc(100% - 14px);
gap: 7px;
}
.contract { padding: 10px; border-radius: 13px; }
.player-name { font-size: 11px; }
.contract-state { display: none; }
.required { margin-top: 8px; gap: 4px; }
.requirement { padding: 4px 6px; font-size: 8px; }
.offer-row { grid-template-columns: 1fr; margin-top: 7px; padding-top: 7px; }
.inventory button { min-width: 0; width: 100%; padding: 6px; }
.inventory strong { font-size: 8px; }
.inventory small { font-size: 7px; }
.accept { margin-top: 7px; padding: 8px 4px; font-size: 8px; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D bridge negotiation scene"></canvas>
<div class="vignette"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<svg viewBox="0 0 32 32">
<path d="M4 23h24M7 23V14m18 9V14M7 15l9-8 9 8M11 16v5m5-5v5m5-5v5" />
</svg>
</div>
<div>
<p class="eyebrow">Cooperative Build / Session 07</p>
<h1>Bridge Pact</h1>
</div>
</div>
<div class="top-right">
<div class="deal-pill">
<div class="deal-row">
<div id="statusTitle">Awaiting contributions</div>
<div class="signatures" aria-label="Agreement progress">
<span class="signature-dot p1" id="dot1"></span>
<span class="signature-dot p2" id="dot2"></span>
</div>
</div>
<div id="statusDetail">Materials remain owned until both players agree.</div>
</div>
<button class="icon-button" id="reset" title="Reset negotiation" aria-label="Reset negotiation">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M3 12a9 9 0 1 0 3-6.7L3 8" />
<path d="M3 3v5h5" />
</svg>
</button>
</div>
</header>
<div class="hint">Click a pile to offer · Click an offered item to return it · Drag to orbit</div>
<section class="contracts">
<article class="contract p1" id="card1">
<div class="contract-head">
<div class="player-id">
<span class="player-swatch"></span>
<span class="player-name">Player 01 / Teal</span>
</div>
<span class="contract-state" id="state1">Not ready</span>
</div>
<div class="required" id="required1"></div>
<div class="offer-row">
<div>
<div class="offer-label">Owned offer</div>
<div class="offer-chips" id="offer1"></div>
</div>
<div class="inventory">
<button id="inv1timber" data-player="p1" data-type="timber">
<strong>TIMBER</strong>
<small id="inv1timberCount">5 available</small>
</button>
<button id="inv1stone" data-player="p1" data-type="stone">
<strong>STONE</strong>
<small id="inv1stoneCount">5 available</small>
</button>
</div>
</div>
<button class="accept" id="accept1">Accept agreement</button>
</article>
<article class="contract p2" id="card2">
<div class="contract-head">
<div class="player-id">
<span class="player-swatch"></span>
<span class="player-name">Player 02 / Coral</span>
</div>
<span class="contract-state" id="state2">Not ready</span>
</div>
<div class="required" id="required2"></div>
<div class="offer-row">
<div>
<div class="offer-label">Owned offer</div>
<div class="offer-chips" id="offer2"></div>
</div>
<div class="inventory">
<button id="inv2timber" data-player="p2" data-type="timber">
<strong>TIMBER</strong>
<small id="inv2timberCount">5 available</small>
</button>
<button id="inv2stone" data-player="p2" data-type="stone">
<strong>STONE</strong>
<small id="inv2stoneCount">5 available</small>
</button>
</div>
</div>
<button class="accept" id="accept2">Accept agreement</button>
</article>
</section>
<div class="toast" id="toast"></div>
<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 renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
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.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e4da);
scene.fog = new THREE.Fog(0xe8e4da, 24, 44);
const camera = new THREE.OrthographicCamera(-10, 10, 6, -6, 0.1, 80);
const cameraTarget = new THREE.Vector3(0, 0.7, 0.15);
let cameraYaw = -0.08;
let cameraElevation = 0.69;
let cameraZoom = 1;
const COLORS = {
ink: 0x18292d,
cream: 0xf3f0e8,
floor: 0xd8d3c7,
p1: 0x168c87,
p1Light: 0x8bcac2,
p2: 0xdd694f,
p2Light: 0xf0ad9b,
timber: 0xb88148,
timberDark: 0x6e4b2c,
stone: 0x8e9b9d,
blueprint: 0x244c5d,
blueprintLight: 0x8ac1cf
};
const players = {
p1: {
id: 'p1',
number: '1',
name: 'PLAYER 01',
color: COLORS.p1,
light: COLORS.p1Light,
required: { timber: 2, stone: 2 },
inventoryTotal: { timber: 5, stone: 5 },
accepted: false
},
p2: {
id: 'p2',
number: '2',
name: 'PLAYER 02',
color: COLORS.p2,
light: COLORS.p2Light,
required: { timber: 1, stone: 3 },
inventoryTotal: { timber: 5, stone: 5 },
accepted: false
}
};
let state = 'negotiating';
let staged = { p1: [], p2: [] };
let transferRecords = [];
let transferStart = 0;
let toastTimer;
const clickableGroups = [];
const ambient = new THREE.HemisphereLight(0xfffbef, 0x7e8986, 2.15);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight(0xfff5df, 3.1);
keyLight.position.set(-7, 13, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.normalBias = 0.03;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xb8d9df, 1.4);
rimLight.position.set(10, 7, -8);
scene.add(rimLight);
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.76,
metalness: 0.02,
flatShading: false,
...options
});
const mats = {
floor: material(COLORS.floor),
cream: material(COLORS.cream),
dark: material(COLORS.ink),
p1: material(COLORS.p1),
p2: material(COLORS.p2),
p1Light: material(COLORS.p1Light),
p2Light: material(COLORS.p2Light),
timber: material(COLORS.timber),
timberDark: material(COLORS.timberDark),
stone: material(COLORS.stone),
blueprint: material(COLORS.blueprint),
blueprintLight: material(COLORS.blueprintLight, {
emissive: COLORS.blueprintLight,
emissiveIntensity: 0.08
})
};
function roundedRectPath(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function makeLabel(mainText, subText = '', accent = '#168c87') {
const labelCanvas = document.createElement('canvas');
labelCanvas.width = 1024;
labelCanvas.height = 256;
const ctx = labelCanvas.getContext('2d');
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
}));
sprite.renderOrder = 20;
function draw(main, sub) {
ctx.clearRect(0, 0, 1024, 256);
roundedRectPath(ctx, 12, 12, 1000, 232, 42);
ctx.fillStyle = 'rgba(248,246,239,0.96)';
ctx.fill();
ctx.lineWidth = 5;
ctx.strokeStyle = accent;
ctx.stroke();
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillStyle = '#18292d';
ctx.font = '800 58px system-ui, sans-serif';
ctx.fillText(main, 512, sub ? 94 : 128, 930);
if (sub) {
ctx.fillStyle = '#6f7c7c';
ctx.font = '700 33px system-ui, sans-serif';
ctx.fillText(sub, 512, 173, 930);
}
texture.needsUpdate = true;
}
draw(mainText, subText);
return {
sprite,
set(main, sub = '') { draw(main, sub); }
};
}
function addBox(parent, size, position, mat, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
mat
);
mesh.position.set(...position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(parent, radii, height, position, mat, segments = 32) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radii[0], radii[1], height, segments),
mat
);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const floor = addBox(scene, [15.5, 0.3, 11.5], [0, -0.22, 0], mats.floor);
floor.receiveShadow = true;
const inset = addBox(scene, [14.6, 0.06, 10.6], [0, -0.045, 0], material(0xe4dfd4));
inset.receiveShadow = true;
// Subtle floor seams.
const seamMat = material(0xcfc9bd);
for (let x = -6; x <= 6; x += 2) {
addBox(scene, [0.018, 0.012, 10.25], [x, 0, 0], seamMat, false);
}
for (let z = -4; z <= 4; z += 2) {
addBox(scene, [14.2, 0.012, 0.018], [0, 0, z], seamMat, false);
}
const playerObjects = {};
const playerLabels = {};
const inventoryVisual = {};
function requirementText(player) {
return `${player.required.timber} TIMBER + ${player.required.stone} STONE`;
}
function createPlayerToken(player, x) {
const group = new THREE.Group();
group.position.set(x, 0, 2.72);
scene.add(group);
const accentMat = material(player.color);
const lightMat = material(player.light);
addCylinder(group, [0.73, 0.78], 0.2, [0, 0.13, 0], lightMat, 48);
addCylinder(group, [0.61, 0.66], 0.15, [0, 0.29, 0], accentMat, 48);
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.35, 0.49, 0.85, 24),
accentMat
);
body.position.y = 0.76;
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const headMat = material(player.color, {
emissive: player.color,
emissiveIntensity: 0.02
});
const head = new THREE.Mesh(new THREE.SphereGeometry(0.37, 28, 18), headMat);
head.position.y = 1.39;
head.castShadow = true;
group.add(head);
const cap = new THREE.Mesh(new THREE.TorusGeometry(0.31, 0.055, 12, 32), mats.cream);
cap.rotation.x = Math.PI / 2;
cap.position.y = 1.27;
cap.castShadow = true;
group.add(cap);
const badge = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.13, 0.04, 24),
mats.cream
);
badge.rotation.x = Math.PI / 2;
badge.position.set(0, 0.84, 0.43);
group.add(badge);
const label = makeLabel(
player.name,
`NEEDS ${requirementText(player)}`,
player.id === 'p1' ? '#168c87' : '#dd694f'
);
label.sprite.scale.set(3.25, 0.81, 1);
label.sprite.position.set(0, 2.18, 0);
group.add(label.sprite);
playerObjects[player.id] = { group, head, headMat, accentMat };
playerLabels[player.id] = label;
}
createPlayerToken(players.p1, -4.25);
createPlayerToken(players.p2, 4.25);
function createMaterialItem(type, ownerId, includeRing = true) {
const group = new THREE.Group();
const owner = players[ownerId];
const ownerColor = owner ? owner.color : COLORS.shared;
if (type === 'timber') {
const beamMat = material(COLORS.timber);
const endMat = material(COLORS.timberDark);
const beam = addBox(group, [0.58, 0.19, 0.24], [0, 0.105, 0], beamMat);
const capA = addBox(group, [0.035, 0.195, 0.245], [-0.275, 0.105, 0], endMat);
const capB = addBox(group, [0.035, 0.195, 0.245], [0.275, 0.105, 0], endMat);
group.userData.parts = { beam, capA, capB };
} else {
const stoneMat = material(COLORS.stone, { flatShading: true });
const stone = new THREE.Mesh(new THREE.IcosahedronGeometry(0.255, 0), stoneMat);
stone.position.y = 0.17;
stone.rotation.set(0.15, 0.4, 0.1);
stone.castShadow = true;
stone.receiveShadow = true;
group.add(stone);
group.userData.parts = { stone };
}
if (includeRing) {
const ring = new THREE.Mesh(
new THREE.RingGeometry(0.31, 0.37, 36),
new THREE.MeshBasicMaterial({
color: ownerColor,
transparent: true,
opacity: 0.58,
side: THREE.DoubleSide,
depthWrite: false,
toneMapped: false
})
);
ring.rotation.x = -Math.PI / 2;
ring.position.y = 0.012;
group.add(ring);
group.userData.ring = ring;
}
return group;
}
function createInventoryZone(player, x) {
const group = new THREE.Group();
group.position.set(x, 0, -1.32);
scene.add(group);
const ownerLight = material(player.light);
addCylinder(group, [1.48, 1.54], 0.12, [0, 0.08, 0], ownerLight, 48);
const visual = { timber: [], stone: [], labels: {} };
const types = ['timber', 'stone'];
types.forEach((type, typeIndex) => {
const localX = typeIndex === 0 ? -0.67 : 0.67;
const pedestalMat = material(player.color, {
transparent: true,
opacity: 0.16,
depthWrite: false
});
addBox(group, [0.9, 0.11, 0.92], [localX, 0.19, 0], pedestalMat, false);
const itemPositions = type === 'timber'
? [
[-0.17, 0.27, -0.13], [0.17, 0.27, -0.13],
[-0.17, 0.49, -0.13], [0.17, 0.49, -0.13],
[0, 0.71, -0.13]
]
: [
[-0.18, 0.28, -0.12], [0.18, 0.28, -0.12],
[-0.18, 0.58, -0.12], [0.18, 0.58, -0.12],
[0, 0.84, -0.12]
];
itemPositions.forEach((position, index) => {
const item = createMaterialItem(type, player.id, false);
item.position.set(...position);
item.rotation.y = index * 0.31;
item.userData.action = 'stage';
item.userData.player = player.id;
item.userData.type = type;
group.add(item);
clickableGroups.push(item);
visual[type].push(item);
});
const label = makeLabel(
`${type.toUpperCase()} 5`,
'CLICK TO OFFER',
player.id === 'p1' ? '#168c87' : '#dd694f'
);
label.sprite.scale.set(1.55, 0.49, 1);
label.sprite.position.set(localX, 1.62, 0.05);
group.add(label.sprite);
visual.labels[type] = label;
});
inventoryVisual[player.id] = visual;
}
createInventoryZone(players.p1, -4.25);
createInventoryZone(players.p2, 4.25);
const trayObjects = {};
function createTray(player, x) {
const group = new THREE.Group();
group.position.set(x, 0, 2.36);
scene.add(group);
const trayMat = material(player.light);
addBox(group, [1.48, 0.13, 1.2], [0, 0.11, 0], trayMat);
const outline = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(1.48, 0.135, 1.2)),
new THREE.LineBasicMaterial({ color: player.color })
);
outline.position.y = 0.11;
group.add(outline);
const label = makeLabel(
`${player.name} OFFER`,
'OWNERSHIP RETAINED',
player.id === 'p1' ? '#168c87' : '#dd694f'
);
label.sprite.scale.set(2.05, 0.55, 1);
label.sprite.position.set(0, 0.82, 0);
group.add(label.sprite);
trayObjects[player.id] = { group, label };
}
createTray(players.p1, -1.6);
createTray(players.p2, 1.6);
// Shared bridge blueprint table.
const blueprintGroup = new THREE.Group();
scene.add(blueprintGroup);
const boardMat = material(COLORS.blueprint, {
emissive: COLORS.blueprint,
emissiveIntensity: 0.03
});
addBox(blueprintGroup, [4.65, 0.26, 3.25], [0, 0.49, 0], boardMat);
addBox(blueprintGroup, [4.38, 0.055, 2.98], [0, 0.655, 0], material(0x2d596b));
const lineMat = new THREE.MeshBasicMaterial({
color: COLORS.blueprintLight,
transparent: true,
opacity: 0.72,
toneMapped: false
});
function blueprintLine(x, z, width, depth) {
return addBox(blueprintGroup, [width, 0.025, depth], [x, 0.696, z], lineMat, false);
}
function blueprintBeam(x1, z1, x2, z2) {
const length = Math.hypot(x2 - x1, z2 - z1);
const mesh = addBox(
blueprintGroup,
[length, 0.025, 0.035],
[(x1 + x2) / 2, 0.698, (z1 + z2) / 2],
lineMat,
false
);
mesh.rotation.y = -Math.atan2(z2 - z1, x2 - x1);
}
blueprintLine(0, -0.27, 3.72, 0.035);
blueprintLine(0, 0.27, 3.72, 0.035);
[-1.45, -0.72, 0, 0.72, 1.45].forEach((x) => {
blueprintLine(x, 0, 0.035, 1.18);
});
for (let i = 0; i < 4; i++) {
const x1 = -1.45 + i * 0.72;
const x2 = x1 + 0.72;
blueprintBeam(x1, -0.66, x2, 0.66);
blueprintBeam(x1, 0.66, x2, -0.66);
}
// Contribution slots: three timber positions and five stone positions.
const timberSlots = [
new THREE.Vector3(-1.22, 0.74, -1.05),
new THREE.Vector3(0, 0.74, -1.05),
new THREE.Vector3(1.22, 0.74, -1.05)
];
const stoneSlots = [
new THREE.Vector3(-1.55, 0.74, 0.91),
new THREE.Vector3(-0.78, 0.74, 0.91),
new THREE.Vector3(0, 0.74, 0.91),
new THREE.Vector3(0.78, 0.74, 0.91),
new THREE.Vector3(1.55, 0.74, 0.91)
];
const slotMat = new THREE.MeshBasicMaterial({
color: 0x91c6d0,
transparent: true,
opacity: 0.52,
side: THREE.DoubleSide,
toneMapped: false
});
timberSlots.forEach((slot) => {
const slotMesh = new THREE.Mesh(new THREE.RingGeometry(0.25, 0.32, 4), slotMat);
slotMesh.rotation.x = -Math.PI / 2;
slotMesh.rotation.z = Math.PI / 4;
slotMesh.position.copy(slot);
blueprintGroup.add(slotMesh);
});
stoneSlots.forEach((slot) => {
const slotMesh = new THREE.Mesh(new THREE.RingGeometry(0.2, 0.25, 28), slotMat);
slotMesh.rotation.x = -Math.PI / 2;
slotMesh.position.copy(slot);
blueprintGroup.add(slotMesh);
});
const blueprintLabel = makeLabel(
'SHARED BRIDGE BLUEPRINT',
'8 CONTRIBUTION SLOTS • COMMON PROJECT',
'#b8893f'
);
blueprintLabel.sprite.scale.set(3.5, 0.78, 1);
blueprintLabel.sprite.position.set(0, 2.08, 0.08);
blueprintGroup.add(blueprintLabel.sprite);
// Small solid agreement indicator above the blueprint.
const agreementNodes = {};
const agreementWires = {};
function createAgreementNode(id, x, color) {
const node = new THREE.Mesh(
new THREE.CylinderGeometry(0.14, 0.14, 0.11, 24),
material(color)
);
node.rotation.x = Math.PI / 2;
node.position.set(x, 1.22, 0.25);
blueprintGroup.add(node);
agreementNodes[id] = node;
}
createAgreementNode('p1', -0.28, COLORS.p1);
createAgreementNode('p2', 0.28, COLORS.p2);
const wire = addBox(
blueprintGroup,
[0.56, 0.035, 0.035],
[0, 1.22, 0.25],
new THREE.MeshBasicMaterial({ color: 0x8fa09d }),
false
);
agreementWires.wire = wire;
const requirementEls = {};
function buildRequirementUI(player) {
const container = document.querySelector(`#required${player.number}`);
container.innerHTML = '';
requirementEls[player.id] = {};
Object.entries(player.required).forEach(([type, count]) => {
const el = document.createElement('span');
el.className = 'requirement';
el.innerHTML = `
<span class="material-icon ${type === 'stone' ? 'stone' : ''}"></span>
${count} ${type.toUpperCase()}
`;
container.appendChild(el);
requirementEls[player.id][type] = el;
});
}
buildRequirementUI(players.p1);
buildRequirementUI(players.p2);
function stagedCounts(playerId) {
return staged[playerId].reduce((counts, item) => {
counts[item.userData.type]++;
return counts;
}, { timber: 0, stone: 0 });
}
function availableCount(playerId, type) {
return players[playerId].inventoryTotal[type] - stagedCounts(playerId)[type];
}
function contractReady(player) {
const counts = stagedCounts(player.id);
return Object.keys(player.required).every(
type => counts[type] === player.required[type]
);
}
function updateInventoryVisibility(player) {
const visual = inventoryVisual[player.id];
Object.keys(visual).forEach(type => {
if (type === 'labels') return;
const available = availableCount(player.id, type);
visual[type].forEach((item, index) => {
item.visible = index < available;
});
visual.labels[type].set(
`${type.toUpperCase()} ${available}`,
'CLICK TO OFFER'
);
});
}
function arrangeStaged(playerId) {
const tray = trayObjects[playerId];
staged[playerId].forEach((item, index) => {
const column = index % 2;
const row = Math.floor(index / 2);
item.position.set(
tray.group.position.x + (column - 0.5) * 0.55,
0.23,
tray.group.position.z + (row - 0.5) * 0.49
);
item.rotation.y = index * 0.42;
item.userData.action = 'unstage';
item.userData.player = playerId;
item.userData.index = index;
});
}
function stageMaterial(playerId, type) {
if (state !== 'negotiating' || players[playerId].accepted) return;
if (availableCount(playerId, type) <= 0) {
showToast('No more materials of that type remain.');
return;
}
const item = createMaterialItem(type, playerId, true);
item.userData.type = type;
item.userData.action = 'unstage';
item.userData.player = playerId;
scene.add(item);
staged[playerId].push(item);
clickableGroups.push(item);
arrangeStaged(playerId);
updateInventoryVisibility(players[playerId]);
updateUI();
}
function unstageMaterial(playerId, type) {
if (state !== 'negotiating' || players[playerId].accepted) return;
const index = staged[playerId].findIndex(item => item.userData.type === type);
if (index < 0) return;
const [item] = staged[playerId].splice(index, 1);
scene.remove(item);
const clickIndex = clickableGroups.indexOf(item);
if (clickIndex >= 0) clickableGroups.splice(clickIndex, 1);
arrangeStaged(playerId);
updateInventoryVisibility(players[playerId]);
updateUI();
}
function updateOfferUI(player) {
const container = document.querySelector(`#offer${player.number}`);
container.innerHTML = '';
const counts = stagedCounts(player.id);
if (counts.timber + counts.stone === 0) {
const empty = document.createElement('span');
empty.className = 'empty-offer';
empty.textContent = 'Nothing offered yet';
container.appendChild(empty);
} else {
Object.entries(counts).forEach(([type, count]) => {
if (!count) return;
const chip = document.createElement('button');
chip.className = 'offer-chip';
chip.textContent = `${type.toUpperCase()} ×${count}`;
chip.title = 'Click to return one item';
chip.addEventListener('click', () => unstageMaterial(player.id, type));
container.appendChild(chip);
});
}
}
function updatePlayerUI(player) {
const number = player.number;
const card = document.querySelector(`#card${number}`);
const stateEl = document.querySelector(`#state${number}`);
const acceptButton = document.querySelector(`#accept${number}`);
const counts = stagedCounts(player.id);
const ready = contractReady(player);
Object.entries(player.required).forEach(([type, required]) => {
requirementEls[player.id][type].classList.toggle('met', counts[type] >= required);
});
card.classList.toggle('ready', ready);
card.classList.toggle('accepted', player.accepted);
if (state === 'shared') {
stateEl.textContent = 'Transferred';
acceptButton.textContent = 'Contribution shared';
} else if (state === 'transferring') {
stateEl.textContent = 'Transferring';
acceptButton.textContent = 'Ownership transferring';
} else if (player.accepted) {
stateEl.textContent = 'Agreed';
acceptButton.textContent = 'Agreed • Awaiting player 02';
} else if (ready) {
stateEl.textContent = 'Ready';
acceptButton.textContent = 'Accept agreement';
} else {
stateEl.textContent = 'Not ready';
acceptButton.textContent = 'Accept agreement';
}
acceptButton.disabled = !ready || player.accepted || state !== 'negotiating';
acceptButton.classList.toggle('accepted-button', player.accepted);
['timber', 'stone'].forEach(type => {
const inventoryButton = document.querySelector(`#inv${number}${type}`);
const countLabel = document.querySelector(`#inv${number}${type}Count`);
const available = availableCount(player.id, type);
countLabel.textContent = `${available} available`;
inventoryButton.disabled = player.accepted || state !== 'negotiating' || available === 0;
});
updateOfferUI(player);
}
function updateUI() {
Object.values(players).forEach(updatePlayerUI);
const agreedCount = Number(players.p1.accepted) + Number(players.p2.accepted);
const bothReady = contractReady(players.p1) && contractReady(players.p2);
document.querySelector('#dot1').classList.toggle('on', players.p1.accepted);
document.querySelector('#dot2').classList.toggle('on', players.p2.accepted);
const statusTitle = document.querySelector('#statusTitle');
const statusDetail = document.querySelector('#statusDetail');
if (state === 'shared') {
statusTitle.textContent = 'Agreement complete';
statusDetail.textContent = 'Eight resources transferred into shared bridge ownership.';
} else if (state === 'transferring') {
statusTitle.textContent = 'Transfer in progress';
statusDetail.textContent = 'Both players agreed. Ownership is moving to the bridge.';
} else if (agreedCount === 1) {
statusTitle.textContent = '1 of 2 agreed';
statusDetail.textContent = 'Offered materials remain owned by their original player.';
} else if (bothReady) {
statusTitle.textContent = 'Both contracts met';
statusDetail.textContent = 'Both players must accept before ownership can transfer.';
} else {
statusTitle.textContent = 'Awaiting contributions';
statusDetail.textContent = 'Materials remain owned until both players agree.';
}
blueprintLabel.set(
state === 'shared' ? 'BRIDGE PROJECT COMPLETE' : 'SHARED BRIDGE BLUEPRINT',
state === 'shared'
? 'CONTRIBUTIONS MERGED • COMMON OWNERSHIP'
: '8 CONTRIBUTION SLOTS • COMMON PROJECT',
state === 'shared' ? '#168c87' : '#b8893f'
);
trayObjects.p1.label.set(
players.p1.accepted ? 'P1 AGREED' : 'P1 OFFER',
players.p1.accepted ? 'AWAITING COUNTERPART' : 'OWNERSHIP RETAINED',
'#168c87'
);
trayObjects.p2.label.set(
players.p2.accepted ? 'P2 AGREED' : 'P2 OFFER',
players.p2.accepted ? 'AWAITING COUNTERPART' : 'OWNERSHIP RETAINED',
'#dd694f'
);
}
function showToast(message) {
const toast = document.querySelector('#toast');
toast.textContent = message;
toast.classList.add('show');
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove('show'), 2200);
}
function applySharedAppearance(item) {
const parts = item.userData.parts;
if (item.userData.type === 'timber') {
parts.beam.material.color.setHex(0xc89455);
parts.capA.material.color.setHex(0x815832);
parts.capB.material.color.setHex(0x815832);
} else {
parts.stone.material.color.setHex(0xaab4b4);
}
if (item.userData.ring) item.userData.ring.visible = false;
item.userData.action = null;
}
function beginTransfer() {
if (state !== 'negotiating') return;
if (!players.p1.accepted || !players.p2.accepted) return;
state = 'transferring';
transferStart = performance.now();
transferRecords = [];
let timberIndex = 0;
let stoneIndex = 0;
let order = 0;
['p1', 'p2'].forEach(playerId => {
staged[playerId].forEach(item => {
const type = item.userData.type;
let target;
if (type === 'timber') {
target = timberSlots[timberIndex++].clone();
} else {
target = stoneSlots[stoneIndex++].clone();
}
transferRecords.push({
item,
start: item.position.clone(),
target,
order: order++,
done: false
});
});
});
updateUI();
showToast('Both players agreed — transferring ownership.');
}
function acceptAgreement(playerId) {
if (state !== 'negotiating' || players[playerId].accepted) return;
if (!contractReady(players[playerId])) {
showToast('Stage the exact required contribution first.');
return;
}
players[playerId].accepted = true;
updateUI();
if (players.p1.accepted && players.p2.accepted) {
beginTransfer();
} else {
showToast(`${players[playerId].name} agreed. Awaiting the other player.`);
}
}
function resetNegotiation() {
state = 'negotiating';
transferRecords = [];
transferStart = 0;
Object.values(players).forEach(player => {
player.accepted = false;
staged[player.id].forEach(item => {
scene.remove(item);
const index = clickableGroups.indexOf(item);
if (index >= 0) clickableGroups.splice(index, 1);
});
staged[player.id] = [];
updateInventoryVisibility(player);
playerObjects[player.id].headMat.emissiveIntensity = 0.02;
});
boardMat.emissiveIntensity = 0.03;
updateUI();
showToast('Negotiation reset. Ownership returned to both players.');
}
document.querySelectorAll('.inventory button').forEach(button => {
button.addEventListener('click', () => {
stageMaterial(button.dataset.player, button.dataset.type);
});
});
document.querySelector('#accept1').addEventListener('click', () => acceptAgreement('p1'));
document.querySelector('#accept2').addEventListener('click', () => acceptAgreement('p2'));
document.querySelector('#reset').addEventListener('click', resetNegotiation);
window.addEventListener('keydown', event => {
if (event.key === '1') acceptAgreement('p1');
if (event.key === '2') acceptAgreement('p2');
if (event.key.toLowerCase() === 'r') resetNegotiation();
});
// Custom camera controls, implemented without Three.js addons.
function updateCamera() {
const radius = 18;
const horizontal = Math.cos(cameraElevation) * radius;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraElevation) * radius,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
camera.zoom = cameraZoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const frustumHeight = Math.max(10.8, 13.7 / aspect);
camera.left = -frustumHeight * aspect / 2;
camera.right = frustumHeight * aspect / 2;
camera.top = frustumHeight / 2;
camera.bottom = -frustumHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
updateCamera();
}
window.addEventListener('resize', resize);
resize();
let pointerDown = false;
let pointerMoved = false;
let pointerStartX = 0;
let pointerStartY = 0;
let startYaw = cameraYaw;
let startElevation = cameraElevation;
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function setPointerFromEvent(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function objectIsVisible(object) {
let current = object;
while (current) {
if (!current.visible) return false;
current = current.parent;
}
return true;
}
function findAction(object) {
let current = object;
while (current) {
if (current.userData.action) return current;
current = current.parent;
}
return null;
}
function pickAction(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(clickableGroups, true);
for (const intersection of intersections) {
if (!objectIsVisible(intersection.object)) continue;
const action = findAction(intersection.object);
if (action) return action;
}
return null;
}
canvas.addEventListener('pointerdown', event => {
if (event.button !== 0) return;
pointerDown = true;
pointerMoved = false;
pointerStartX = event.clientX;
pointerStartY = event.clientY;
startYaw = cameraYaw;
startElevation = cameraElevation;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener('pointermove', event => {
if (pointerDown) {
const dx = event.clientX - pointerStartX;
const dy = event.clientY - pointerStartY;
if (Math.abs(dx) + Math.abs(dy) > 4) pointerMoved = true;
if (pointerMoved) {
cameraYaw = startYaw - dx * 0.005;
cameraElevation = THREE.MathUtils.clamp(
startElevation + dy * 0.004,
0.38,
1.03
);
updateCamera();
canvas.style.cursor = 'grabbing';
}
} else {
const action = pickAction(event);
canvas.style.cursor = action ? 'pointer' : 'grab';
}
});
canvas.addEventListener('pointerup', event => {
if (!pointerDown) return;
pointerDown = false;
if (!pointerMoved) {
const action = pickAction(event);
if (!action) return;
if (action.userData.action === 'stage') {
stageMaterial(action.userData.player, action.userData.type);
} else if (action.userData.action === 'unstage') {
const playerId = action.userData.player;
const type = action.userData.type;
unstageMaterial(playerId, type);
}
}
canvas.style.cursor = 'grab';
});
canvas.addEventListener('pointercancel', () => {
pointerDown = false;
pointerMoved = false;
});
canvas.addEventListener('wheel', event => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom - event.deltaY * 0.0008,
0.78,
1.35
);
updateCamera();
}, { passive: false });
const clock = new THREE.Clock();
function animateTransfer(now) {
if (state !== 'transferring') return;
const elapsed = now - transferStart;
let allDone = true;
transferRecords.forEach(record => {
const localTime = elapsed - record.order * 0.11;
const progress = THREE.MathUtils.clamp(localTime / 1050, 0, 1);
if (progress > 0 && progress < 1) {
const eased = progress < 0.5
? 4 * progress * progress * progress
: 1 - Math.pow(-2 * progress + 2, 3) / 2;
record.item.position.lerpVectors(record.start, record.target, eased);
record.item.position.y += Math.sin(Math.PI * progress) * 1.35;
record.item.rotation.y += 0.035;
}
if (progress >= 1) {
if (!record.done) {
record.done = true;
record.item.position.copy(record.target);
applySharedAppearance(record.item);
}
} else {
allDone = false;
}
});
if (allDone && transferRecords.length) {
state = 'shared';
boardMat.emissive.setHex(0x168c87);
boardMat.emissiveIntensity = 0.18;
updateUI();
showToast('Transfer complete. The bridge materials are now shared.');
}
}
function animate() {
requestAnimationFrame(animate);
const now = performance.now();
const time = clock.getElapsedTime();
Object.values(players).forEach((player, index) => {
const pulse = player.accepted
? 0.17 + Math.sin(time * 3.2 + index) * 0.07
: 0.02;
playerObjects[player.id].headMat.emissiveIntensity = pulse;
});
agreementNodes.p1.scale.setScalar(
players.p1.accepted ? 1.1 + Math.sin(time * 3) * 0.05 : 1
);
agreementNodes.p2.scale.setScalar(
players.p2.accepted ? 1.1 + Math.sin(time * 3 + 1) * 0.05 : 1
);
agreementWires.wire.material.color.setHex(
players.p1.accepted && players.p2.accepted ? 0x168c87 : 0x8fa09d
);
animateTransfer(now);
renderer.render(scene, camera);
}
updateInventoryVisibility(players.p1);
updateInventoryVisibility(players.p2);
updateUI();
animate();
</script>
</body>
</html>
```