Prompt specification
Build or customize the Three.js 3D scene "Schoolyard Chalk Games Diorama" 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: Schoolyard Chalk Games Diorama
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/schoolyard-chalk-games-diorama-x3619
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows hopscotch and patterned foot placement. Build a small schoolyard with several original pavement games and children represented by simple characters. Show different movement patterns and shared play space without cultural stereotyping. Voxel style.
Working Three.js Starter (schoolyard-chalk-games-diorama-x3619):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Voxel Schoolyard — Shared Pavement Play</title>
<style>
:root{
--panel: rgba(20,26,38,.78);
--panel2: rgba(255,255,255,.10);
--ink: #f4f7fb;
--accent: #ffd166;
}
*{box-sizing:border-box}
html,body{margin:0;height:100%;overflow:hidden;background:#8ecdf0;
font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;}
#app{position:fixed;inset:0}
canvas{display:block;width:100%!important;height:100%!important;image-rendering:pixelated;image-rendering:crisp-edges;}
.panel{
position:fixed;background:var(--panel);color:var(--ink);
border:2px solid var(--panel2);border-radius:10px;padding:10px 12px;
backdrop-filter: blur(6px); box-shadow:0 6px 18px rgba(0,0,0,.28);
font-size:12px;line-height:1.45;user-select:none;
}
#title{left:12px;top:12px;max-width:250px}
#title h1{margin:0 0 6px;font-size:15px;letter-spacing:.06em;color:var(--accent)}
#title p{margin:0;opacity:.85}
#legend{left:12px;bottom:12px;max-width:250px}
#legend b{color:var(--accent)}
#legend ul{margin:4px 0 0;padding-left:14px}
#legend li{margin:2px 0}
#ctrls{right:12px;top:12px;text-align:right}
#ctrls .row{display:flex;gap:6px;justify-content:flex-end;flex-wrap:wrap;margin-bottom:6px}
button{
font:inherit;font-size:11px;color:var(--ink);background:rgba(255,255,255,.08);
border:2px solid var(--panel2);border-radius:7px;padding:5px 9px;cursor:pointer;
}
button:hover{background:rgba(255,255,255,.2)}
button.on{background:var(--accent);color:#20180a;border-color:var(--accent)}
input[type=range]{width:120px;vertical-align:middle}
#hint{right:12px;bottom:12px;text-align:right;opacity:.85;max-width:230px}
#fps{position:fixed;right:12px;bottom:12px;display:none}
@media (max-width:760px){
#legend{display:none}
#title{max-width:180px}
#hint{display:none}
}
</style>
</head>
<body>
<div id="app"></div>
<div class="panel" id="title">
<h1>VOXEL SCHOOLYARD</h1>
<p>Shared pavement play: hopping, weaving, grid sprints, spirals, rings, and foot-pattern trails. Everyone takes turns.</p>
</div>
<div class="panel" id="legend">
<b>Courts in the yard</b>
<ul>
<li>Hopscotch Court (turn-taking)</li>
<li>Grid Sprint (checker court)</li>
<li>Spiral Steps (precision walk)</li>
<li>Chalk Rings (orbit + spin)</li>
<li>Weave Run (cone slalom)</li>
<li>Step Trail (footprint pattern)</li>
<li>Cross-Step Patch (cross + hop)</li>
</ul>
</div>
<div class="panel" id="ctrls">
<div class="row">
<button id="bOverview" class="on">Overview</button>
<button id="bFollow">Follow Hopper</button>
<button id="bTop">Top View</button>
</div>
<div class="row">
<button id="bRepaint">Repaint Chalk</button>
<button id="bLabels" class="on">Names</button>
</div>
<div class="row">
<label>speed <input id="speed" type="range" min="0.3" max="2" step="0.1" value="1"></label>
</div>
</div>
<div class="panel" id="hint">
drag = orbit · wheel = zoom · right-drag / shift-drag = pan
</div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
/* ============================================================
0. CORE SETUP
============================================================ */
const container = document.getElementById('app');
const renderer = new THREE.WebGLRenderer({ antialias:false, powerPreference:'high-performance' });
renderer.setPixelRatio(1);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFShadowMap; // hard edges = crisp voxel look
renderer.outputColorSpace = THREE.SRGBColorSpace;
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x8ecdf0);
scene.fog = new THREE.Fog(0x8ecdf0, 90, 190);
const camera = new THREE.PerspectiveCamera(52, 1, 0.5, 400);
const PIX = 2; // render at 1/2 res => chunky pixels
/* ---------- deterministic randomness ---------- */
function mulberry32(a){return function(){a|=0;a=a+0x6D2B79F5|0;let t=Math.imul(a^a>>>15,1|a);t=t+Math.imul(t^t>>>7,61|t)^t;return((t^t>>>14)>>>0)/4294967296;};}
const rnd = mulberry32(20240917);
const pick = arr => arr[Math.floor(rnd()*arr.length)];
/* ---------- material + geometry caches ---------- */
const matCache = new Map(), geoCache = new Map();
function mat(color, extra={}){
const key = color+'|'+JSON.stringify(extra);
if(!matCache.has(key)) matCache.set(key, new THREE.MeshLambertMaterial({ color, ...extra }));
return matCache.get(key);
}
function geo(w,h,d){
const key = w+','+h+','+d;
if(!geoCache.has(key)) geoCache.set(key, new THREE.BoxGeometry(w,h,d));
return geoCache.get(key);
}
function box(w,h,d,color,x=0,y=0,z=0,parent=scene,extra={}){
const m = new THREE.Mesh(geo(w,h,d), mat(color, extra));
m.position.set(x,y,z); m.castShadow=true; m.receiveShadow=true;
parent.add(m); return m;
}
const V = (x,y,z)=>new THREE.Vector3(x,y,z);
/* ---------- lights ---------- */
scene.add(new THREE.HemisphereLight(0xdff1ff, 0x6f9a52, 0.85));
const sun = new THREE.DirectionalLight(0xfff3d6, 1.15);
sun.position.set(26, 42, 18);
sun.castShadow = true;
sun.shadow.mapSize.set(2048,2048);
sun.shadow.camera.left=-48; sun.shadow.camera.right=48;
sun.shadow.camera.top=48; sun.shadow.camera.bottom=-48;
sun.shadow.camera.near=1; sun.shadow.camera.far=140;
sun.shadow.bias = -0.0008;
scene.add(sun); scene.add(sun.target);
/* ============================================================
1. GROUND, YARD, BUILDINGS
============================================================ */
const G = 0.2; // pavement surface height
const GRASS = 0x6fae4a;
box(160, 1.0, 160, GRASS, 0, -0.5, 0); // grass top at y = 0
// grass tufts (instanced)
{
const N=900, im = new THREE.InstancedMesh(geo(0.28,1,0.28), mat(0xffffff), N);
const m4=new THREE.Matrix4(), q=new THREE.Quaternion(), s=new THREE.Vector3(), p=new THREE.Vector3();
let k=0;
for(let i=0;i<N;i++){
const x=(rnd()*2-1)*46, z=(rnd()*2-1)*46;
if(Math.abs(x)<16 && Math.abs(z)<11) continue; // keep pavement clear
const h=0.25+rnd()*0.55;
p.set(x,h/2,z); s.set(1,h,1); q.setFromAxisAngle(new THREE.Vector3(0,1,0), rnd()*Math.PI);
m4.compose(p,q,s); im.setMatrixAt(k,m4);
im.setColorAt(k, new THREE.Color(pick([0x63a845,0x76b852,0x5c9c3e,0x83c25c])));
k++;
}
im.count=k; im.instanceMatrix.needsUpdate=true; im.instanceColor.needsUpdate=true;
im.castShadow=true; scene.add(im);
}
// pavement tiles (instanced, slight color variation = voxel texture)
{
const TS=1.25, NX=24, NZ=16, N=NX*NZ;
const im = new THREE.InstancedMesh(geo(TS,0.2,TS), mat(0xffffff), N);
const m4=new THREE.Matrix4(), q=new THREE.Quaternion(), s=new THREE.Vector3(1,1,1), p=new THREE.Vector3();
const base=[0x9aa3a9,0x949da3,0xa1a8ad,0x8f989e,0xa6adb2];
let k=0;
for(let i=0;i<NX;i++) for(let j=0;j<NZ;j++){
const x=-15+TS/2+i*TS, z=-10+TS/2+j*TS;
p.set(x,0.1,z); q.identity(); s.set(1,1,1);
m4.compose(p,q,s); im.setMatrixAt(k,m4);
let c = base[Math.floor(rnd()*base.length)];
if(i===0||j===0||i===NX-1||j===NZ-1) c=0x7f878c; // curb edge
im.setColorAt(k,new THREE.Color(c)); k++;
}
im.instanceMatrix.needsUpdate=true; im.instanceColor.needsUpdate=true;
im.receiveShadow=true; im.castShadow=true; scene.add(im);
}
/* ---------- school building ---------- */
{
const B = new THREE.Group(); scene.add(B);
box(32, 7.5, 9, 0xdcc9a4, 0, 3.75, -26, B);
box(33, 0.9, 10.5, 0x8c4a3c, 0, 8.0, -26, B);
box(33, 0.4, 10.5, 0x6f382f, 0, 8.6, -26, B);
// windows
for(let i=0;i<9;i++){
const x=-13.5+i*3.4;
for(let r=0;r<2;r++){
box(1.5,1.9,0.25, 0x3f6f9c, x, 2.0+r*3.0, -21.35, B);
box(1.7,0.25,0.35, 0xf2f4f2, x, 1.0+r*3.0, -21.3, B);
box(1.7,0.22,0.35, 0xf2f4f2, x, 2.95+r*3.0, -21.3, B);
}
}
// door + steps
box(2.4,3.2,0.4, 0x8b5e3c, 0, 1.6, -21.3, B);
box(0.3,3.4,0.5, 0x6d4527, -1.35, 1.7, -21.2, B);
box(0.3,3.4,0.5, 0x6d4527, 1.35, 1.7, -21.2, B);
box(4.0,0.35,1.2, 0xb9b2a4, 0, 0.15, -20.8, B);
box(5.0,0.35,1.9, 0xc3bcae, 0, -0.15, -20.2, B);
// clock
box(1.6,1.6,0.4, 0xf3ead4, 0, 6.2, -21.2, B);
box(0.14,0.6,0.12, 0x333333, 0, 6.35, -20.95, B);
box(0.45,0.14,0.12, 0x333333, 0.18, 6.2, -20.95, B);
// flag
box(0.18,5.0,0.18, 0xdddddd, 10, 10.6, -26, B);
box(2.2,1.4,0.12, 0xff8a5c, 11.2, 12.1, -26, B);
}
/* ---------- fence ---------- */
function fenceRun(x1,z1,x2,z2){
const len = Math.hypot(x2-x1, z2-z1), n = Math.round(len/2.4);
for(let i=0;i<=n;i++){
const t=i/n, x=x1+(x2-x1)*t, z=z1+(z2-z1)*t;
box(0.28,2.2,0.28, 0xb9b1a2, x, 1.1, z);
box(0.42,0.24,0.42, 0xd6cec0, x, 2.28, z);
}
const ang = Math.atan2(x2-x1, z2-z1);
for(const h of [0.75, 1.6]){
const rail = box(0.14,0.2,len, 0xc8c0b1, (x1+x2)/2, h, (z1+z2)/2);
rail.rotation.y = ang;
}
}
fenceRun(-24,-18.5, 24,-18.5);
fenceRun(-24,-18.5, -24, 16);
fenceRun( 24,-18.5, 24, 16);
/* ---------- trees & bushes ---------- */
function tree(x,z,s=1){
const T=new THREE.Group(); T.position.set(x,0,z); T.scale.setScalar(s); scene.add(T);
box(0.9,3.2,0.9, 0x7a5230, 0, 1.6, 0, T);
box(0.55,1.0,0.55, 0x7a5230, 0, 3.3, 0, T);
const leaf=[0x3f8f4a,0x4c9c52,0x358541,0x59a95c];
box(3.6,1.6,3.6, pick(leaf), 0, 4.3, 0, T);
box(2.6,1.4,2.6, pick(leaf), 0.6, 5.5, -0.4, T);
box(2.2,1.2,2.2, pick(leaf), -0.8, 5.4, 0.7, T);
box(2.0,1.0,2.0, pick(leaf), 0.1, 6.3, 0.1, T);
}
[[-20,-14,1.2],[-15.5,-13,0.95],[17,-13,1.1],[21,-6,1.25],[-21,2,1.0],[19,9,1.15],[-16,12,0.9],[13,14,1.05]]
.forEach(t=>tree(t[0],t[1],t[2]));
for(let i=0;i<26;i++){
const x=(rnd()*2-1)*26, z=(rnd()*2-1)*22;
if(Math.abs(x)<16&&Math.abs(z)<11) continue;
box(0.9+rnd()*0.8,0.6+rnd()*0.4,0.9+rnd()*0.8, pick([0x3f8f4a,0x4c9c52,0x358541]), x, 0.3, z);
}
/* ---------- benches (shared social space) ---------- */
function bench(x,z,rot=0){
const B=new THREE.Group(); B.position.set(x,0,z); B.rotation.y=rot; scene.add(B);
box(3.2,0.22,0.9, 0xa9793f, 0, 0.75, 0, B);
box(3.2,0.9,0.2, 0xa9793f, 0, 1.3, -0.4, B);
box(0.3,0.75,0.8, 0x5b5b5b, -1.3, 0.38, 0, B);
box(0.3,0.75,0.8, 0x5b5b5b, 1.3, 0.38, 0, B);
}
bench(-6.5, 12.4, 0); bench(-2.6, 12.4, 0); bench(9.5, 12.6, 0);
/* ---------- voxel cones ---------- */
function cone(x,z){
const C=new THREE.Group(); C.position.set(x,G,z); scene.add(C);
box(0.62,0.12,0.62, 0xe8622a, 0, 0.06, 0, C);
box(0.46,0.42,0.46, 0xf07c3a, 0, 0.3, 0, C);
box(0.30,0.34,0.30, 0xffa35c, 0, 0.68, 0, C);
box(0.16,0.2,0.16, 0xffc48c, 0, 0.94, 0, C);
}
/* ---------- sky decor ---------- */
{
const sunCube = box(7,7,7, 0xffe066, -70, 46, -80); sunCube.castShadow=false;
const cloudMat = 0xffffff;
[[30,38,-70,1.6],[-40,34,-60,1.2],[10,44,-95,1.9],[55,30,-45,1.0]].forEach(c=>{
const g=new THREE.Group(); g.position.set(c[0],c[1],c[2]); g.scale.setScalar(c[3]); scene.add(g);
box(9,2.6,4.5,cloudMat,0,0,0,g).castShadow=false;
box(4.5,2.2,4.0,cloudMat,3.2,1.2,0,g).castShadow=false;
box(3.6,2.0,3.6,cloudMat,-3.4,1.0,0.4,g).castShadow=false;
});
}
/* ============================================================
2. CHALK SYSTEM (instanced) + courts & paths
============================================================ */
const CHALK_MAX = 5000;
const chalkMesh = new THREE.InstancedMesh(geo(1,1,1), new THREE.MeshBasicMaterial({color:0xffffff}), CHALK_MAX);
chalkMesh.count = 0;
chalkMesh.castShadow=false; chalkMesh.receiveShadow=false;
scene.add(chalkMesh);
const chalkRecs = []; // {i, key, color}
const _m4=new THREE.Matrix4(), _q=new THREE.Quaternion(), _s=new THREE.Vector3(), _p=new THREE.Vector3(), _axis=new THREE.Vector3(0,1,0);
const PALETTE = {
white: 0xf7fbff, blue: 0x8ecae6, coral: 0xffb4a2, yellow: 0xffe066,
green: 0xb5e48c, purple: 0xc77dff, orange: 0xffb703, pink: 0xffc2d1
};
let paletteKeys = ['white','blue','coral','yellow','green','purple'];
function chalk(x,y,z,w,h,d,key='white',yaw=0,colorOverride=null){
const i = chalkMesh.count++;
_p.set(x,y,z); _s.set(w,h,d); _q.setFromAxisAngle(_axis,yaw);
_m4.compose(_p,_q,_s);
chalkMesh.setMatrixAt(i,_m4);
const c = new THREE.Color(colorOverride!==null?colorOverride:PALETTE[key]);
chalkMesh.setColorAt(i,c);
chalkRecs.push({ i, key, color:c.clone() });
return i;
}
function chalkRect(cx,cz,w,d,key='white',t=0.13,y=G+0.03){
chalk(cx,y,cz-d/2+t/2, w,0.05,t, key);
chalk(cx,y,cz+d/2-t/2, w,0.05,t, key);
chalk(cx-w/2+t/2,y,cz, t,0.05,d-2*t, key);
chalk(cx+w/2-t/2,y,cz, t,0.05,d-2*t, key);
}
function chalkMark(cx,cz,key,angle=0){ // small footprint: sole + heel
const t=0.09;
chalk(cx+Math.sin(angle)*0.12, G+0.03, cz+Math.cos(angle)*0.12, 0.16,0.05,0.30, key, angle);
chalk(cx-Math.sin(angle)*0.17, G+0.03, cz-Math.cos(angle)*0.17, 0.13,0.05,0.16, key, angle);
}
function chalkDot(cx,cz,key='white',s=0.22){
chalk(cx, G+0.03, cz, s,0.05,s, key);
}
function chalkRing(cx,cz,r,key='white',block=0.34){
const n = Math.max(8, Math.round(2*Math.PI*r/block));
for(let i=0;i<n;i++){
const a=i/n*Math.PI*2;
chalk(cx+Math.cos(a)*r, G+0.03, cz+Math.sin(a)*r, block*0.9,0.05,block*0.9, key);
}
}
/* ---------- pixel-font digits ---------- */
const DIGITS = {
'0':['111','101','101','101','111'],'1':['010','110','010','010','111'],
'2':['111','001','111','100','111'],'3':['111','001','111','001','111'],
'4':['101','101','111','001','001'],'5':['111','100','111','001','111'],
'6':['111','100','111','101','111'],'7':['111','001','010','010','010'],
'8':['111','101','111','101','111'],'9':['111','101','111','001','111']
};
const digitTexCache={};
function digitTexture(n){
if(digitTexCache[n]) return digitTexCache[n];
const S=4, c=document.createElement('canvas'); c.width=3*S; c.height=5*S;
const x=c.getContext('2d'); x.fillStyle='#ffffff';
DIGITS[String(n)].forEach((row,r)=>{ for(let i=0;i<3;i++) if(row[i]==='1') x.fillRect(i*S,r*S,S,S); });
const t=new THREE.CanvasTexture(c);
t.magFilter = THREE.NearestFilter; t.minFilter = THREE.NearestFilter;
t.colorSpace = THREE.SRGBColorSpace;
digitTexCache[n]=t; return t;
}
function numberPlane(n,x,z,scale=1){
const g = new THREE.PlaneGeometry(0.45*scale, 0.75*scale);
const m = new THREE.Mesh(g, new THREE.MeshBasicMaterial({map:digitTexture(n), transparent:true, depthWrite:false}));
m.rotation.x = -Math.PI/2; m.position.set(x, G+0.045, z); scene.add(m); return m;
}
/* ============================================================
3. COURTS + PATHS
============================================================ */
const courts = {};
/* --- Hopscotch Court (turn taking) --- */
const HOP = { x:-11, size:1.5, rows:[] };
{
const zs=[-9.2,-7.5,-5.8,-4.1,-2.4];
const offs=[[0],[-0.85,0.85],[0],[-0.85,0.85],[0]];
let n=1;
zs.forEach((z,r)=>{
offs[r].forEach(o=>{
HOP.rows.push({x:HOP.x+o, z});
chalkRect(HOP.x+o, z, HOP.size, HOP.size, 'white', 0.14);
numberPlane(n, HOP.x+o, z+0.42, 0.85); n++;
});
});
// court border
chalkRect(HOP.x, -5.8, 4.1, 9.6, 'coral', 0.16);
const fwd = HOP.rows.map(r=>V(r.x,G,r.z));
const path = fwd.concat(fwd.slice(0,-1).reverse());
courts.hop = { path, owner:null, marks:[] };
}
/* --- Grid Sprint (checker court) --- */
const GRD = { cx:0, cz:4, n:6, pitch:1.5 };
{
const cell=(i,j)=>V(GRD.cx+(i-(GRD.n-1)/2)*GRD.pitch, G, GRD.cz+(j-(GRD.n-1)/2)*GRD.pitch);
for(let i=0;i<GRD.n;i++) for(let j=0;j<GRD.n;j++){
const p=cell(i,j);
chalkRect(p.x,p.z,1.4,1.4, ((i+j)%2===0)?'white':'blue', 0.1);
}
chalkRect(GRD.cx,GRD.cz, GRD.n*GRD.pitch, GRD.n*GRD.pitch, 'yellow', 0.18);
const route=[[0,0],[5,0],[5,5],[0,5],[2,5],[2,2],[0,2],[0,3],[3,3],[3,1],[1,1],[1,4],[4,4],[4,0],[0,0]];
courts.grid = { path: route.map(([i,j])=>cell(i,j)), owner:null, marks:[] };
}
/* --- Spiral Steps (precision walk) --- */
const SPI = { cx:9, cz:3 };
{
const pts=[];
for(let t=0;t<=1.0001;t+=0.052){
const ang = -Math.PI/2 + t*Math.PI*4.2;
const rad = 0.85 + t*2.85;
const x = Math.round((SPI.cx+Math.cos(ang)*rad)/1.2)*1.2;
const z = Math.round((SPI.cz+Math.sin(ang)*rad)/1.2)*1.2;
pts.push(V(x,G,z));
}
courts.spiral = { path: pts, owner:null, marks:[] };
// chalk squares along the spiral
for(let i=0;i<pts.length;i++){
const p=pts[i];
chalkRect(p.x,p.z,1.05,1.05, i%3===0?'purple':'white', 0.12);
}
chalkRing(SPI.cx,SPI.cz,0.55,'purple',0.26);
}
/* --- Chalk Rings (orbit + spin) --- */
const RING = { cx:10.5, cz:-4.5 };
{
chalkRing(RING.cx,RING.cz,1.45,'coral',0.3);
chalkRing(RING.cx,RING.cz,2.55,'orange',0.3);
chalkRing(RING.cx,RING.cz,3.65,'yellow',0.3);
chalkDot(RING.cx,RING.cz,'red' in PALETTE?'red':'coral',0.55);
const pts=[V(RING.cx,G,RING.cz)];
const N=20;
for(let i=0;i<N;i++){
const a=i/N*Math.PI*2;
pts.push(V(RING.cx+Math.cos(a)*3.1, G, RING.cz+Math.sin(a)*3.1));
}
courts.rings = { path: pts, owner:null, marks:[] };
}
/* --- Weave Run (cone slalom) --- */
{
const pts=[];
for(let i=0;i<6;i++){
const x = (i%2===0)? -13.2 : -8.8;
const z = 1.0 + i*1.65;
pts.push(V(x,G,z)); cone(x,z);
}
const path = pts.concat(pts.slice(0,-1).reverse());
courts.weave = { path, owner:null, marks:[] };
chalkRect(-11, 5.2, 5.6, 9.6, 'green', 0.16);
}
/* --- Step Trail (footprint pattern walk) --- */
{
const pts=[], marks=[];
const N=26;
for(let i=0;i<N;i++){
const t=i/(N-1);
const x = -4.6 + t*8.8;
const z = -8.4 + Math.sin(t*Math.PI*2.1)*0.55;
const dir = V(1,0, Math.cos(t*Math.PI*2.1)*0.55*2.1*Math.PI*8.8/8.8).normalize();
const right = V(dir.z,0,-dir.x);
const side = (i%2===0)? -1 : 1; // alternate left / right foot
const p = V(x + right.x*0.18*side, G, z + right.z*0.18*side);
pts.push(p);
const idx = chalkMark(p.x,p.z, i%2===0?'pink':'blue', Math.atan2(dir.x,dir.z));
marks.push(idx);
}
courts.trail = { path: pts, owner:null, marks };
chalkRect(0.1,-8.4,10.2,2.4,'pink',0.14);
}
/* --- Cross-Step Patch (cross steps + hop) --- */
{
const c = V(-1,G,-4.2);
const seq = [ V(c.x, G, c.z-0.95), // 0: LEFT foot forward
V(c.x+0.95, G, c.z), // 1: RIGHT foot side
V(c.x+0.3, G, c.z-0.72), // 2: LEFT foot crosses over
V(c.x-0.95,G, c.z), // 3: RIGHT foot side
V(c.x, G, c.z) ]; // 4: HOP on both feet
const keys=['pink','blue','pink','blue','yellow'];
const marks = seq.map((p,i)=> chalkMark(p.x,p.z,keys[i],0));
chalkRing(c.x,c.z,0.42,'yellow',0.22);
chalkRect(c.x,c.z,3.0,3.0,'purple',0.14);
courts.cross = { path: seq, owner:null, marks, hopAt:4 };
}
/* ============================================================
4. CHARACTERS (simple voxel kids)
============================================================ */
const SHIRTS=[0xef5350,0x42a5f5,0x66bb6a,0xffca28,0xab47bc,0xff7043,0x26c6da,0xec407a,0x8d6e63,0x5c6bc0,0x26a69a];
const SKINS =[0xf3c9a0,0xdda06d,0xb97a4e,0x8d5a3b,0xf7ddc0,0xc98a55];
const HAIRS =[0x2b2118,0x141210,0x8d5524,0xe0ac69,0x4a3728,0x2f2f2f,0x7b4b2a,0xb98a4e];
const PANTS=[0x37474f,0x455a64,0x5d4037,0x37474f,0x4e342e,0x546e7a];
function makeChild(p){
const g=new THREE.Group(); const body=new THREE.Group(); g.add(body);
const T=p.shirt,S=p.skin,H=p.hair,P=p.pants;
box(0.56,0.62,0.34, T, 0,1.05,0, body);
box(0.52,0.20,0.34, P, 0,0.68,0, body);
box(0.18,0.12,0.18, S, 0,1.40,0, body);
box(0.46,0.44,0.44, S, 0,1.66,0, body);
box(0.48,0.16,0.48, H, 0,1.88,-0.02, body);
box(0.08,0.10,0.04, 0x2b2b2b, -0.11,1.68,0.22, body);
box(0.08,0.10,0.04, 0x2b2b2b, 0.11,1.68,0.22, body);
box(0.10,0.05,0.04, 0x7a4a3a, 0,1.55,0.225, body);
const armL=new THREE.Group(); armL.position.set(-0.35,1.30,0); body.add(armL);
box(0.15,0.46,0.16,T,0,-0.23,0,armL); box(0.16,0.13,0.16,S,0,-0.50,0,armL);
const armR=new THREE.Group(); armR.position.set(0.35,1.30,0); body.add(armR);
box(0.15,0.46,0.16,T,0,-0.23,0,armR); box(0.16,0.13,0.16,S,0,-0.50,0,armR);
const legL=new THREE.Group(); legL.position.set(-0.13,0.62,0); body.add(legL);
box(0.18,0.50,0.20,P,0,-0.25,0,legL); box(0.21,0.11,0.30,0x37474f,0,-0.55,0.05,legL);
const legR=new THREE.Group(); legR.position.set(0.13,0.62,0); body.add(legR);
box(0.18,0.50,0.20,P,0,-0.25,0,legR); box(0.21,0.11,0.30,0x37474f,0,-0.55,0.05,legL===legR?legR:legR);
return { group:g, body, armL, armR, legL, legR };
}
function makeLabel(text){
const c=document.createElement('canvas'); c.width=128; c.height=56;
const x=c.getContext('2d');
x.fillStyle='rgba(255,255,255,0.88)';
x.beginPath(); x.roundRect ? x.roundRect(6,10,116,36,10) : x.rect(6,10,116,36);
x.fill();
x.fillStyle='#26303c'; x.font='bold 26px monospace'; x.textAlign='center'; x.textBaseline='middle';
x.fillText(text,64,29);
const tex=new THREE.CanvasTexture(c); tex.magFilter=THREE.NearestFilter; tex.colorSpace=THREE.SRGBColorSpace;
const s=new THREE.Sprite(new THREE.SpriteMaterial({map:tex, transparent:true, depthWrite:false}));
s.scale.set(1.5,0.66,1); return s;
}
/* ============================================================
5. AGENTS
============================================================ */
const agents=[];
class Agent{
constructor(o){
this.name=o.name; this.mode=o.mode||'walk'; this.court=o.court;
this.pal={shirt:o.shirt, skin:o.skin, hair:o.hair, pants:o.pants};
this.model=makeChild(this.pal);
this.model.group.position.copy(o.path[0]);
scene.add(this.model.group);
this.label=makeLabel(o.name); this.label.position.set(0,2.25,0);
this.model.group.add(this.label);
this.path=o.path; this.baseY=o.baseY!==undefined?o.baseY:o.path[0].y;
this.speed=o.speed||2.2;
this.i=0; this.j=1%o.path.length; this.t=0;
this.pos=o.path[0].clone(); this.yaw=0;
this.phase=0; this.pause=0; this.cool=0; this.lapCount=0;
this.dwell=o.dwell||0; this.marks=o.marks||[];
this.stepDur=1/Math.max(0.4,this.speed);
this.updateDur();
}
updateDur(){
const a=this.path[this.i], b=this.path[this.j];
const d=a.distanceTo(b);
let sp=this.speed;
if(this.mode==='hop') sp=this.speed*0.55;
if(this.mode==='step'||this.mode==='trail') sp=this.speed*0.6;
this.dur=Math.max(0.22, d/Math.max(0.6,sp));
}
setYawTo(from,to){
const dx=to.x-from.x, dz=to.z-from.z;
if(dx*dx+dz*dz>1e-6){
let target=Math.atan2(dx,dz);
let diff=target-this.yaw;
while(diff>Math.PI) diff-=Math.PI*2;
while(diff<-Math.PI) diff+=Math.PI*2;
this.yaw+=diff*0.25;
}
}
land(){
this.i=this.j; this.j=(this.j+1)%this.path.length;
if(this.j===0) this.lapCount++;
this.t=0; this.updateDur();
if(this.dwell>0) this.pause=this.dwell;
// foot-plant highlight on chalk marks
if(this.marks.length && this.marks[this.i]!==undefined) this.flash(this.marks[this.i]);
// release shared court after a full round
if(this.court && this.court.owner===this && this.j===0) this.court.owner=null;
}
flash(idx){
const rec=chalkRecs[idx]; if(!rec) return;
const c=new THREE.Color(rec.color).lerp(new THREE.Color(0xffffff),0.85);
chalkMesh.setColorAt(idx,c);
chalkMesh.instanceColor.needsUpdate=true;
this.restoreAt=performance.now()+260;
this.restoreIdx=idx;
}
update(dt, time){
// restore flashed chalk
if(this.restoreAt && performance.now()>this.restoreAt){
const rec=chalkRecs[this.restoreIdx];
if(rec){ chalkMesh.setColorAt(this.restoreIdx, rec.color); chalkMesh.instanceColor.needsUpdate=true; }
this.restoreAt=0;
}
if(this.cool>0) this.cool-=dt;
if(this.mode==='idle'){ this.idlePose(time); return; }
// shared-space courtesy: yield when someone else is right there
if(this.cool<=0 && this.pause<=0){
for(const a of agents){
if(a===this || a.mode==='idle') continue;
const dx=a.pos.x-this.pos.x, dz=a.pos.z-this.pos.z;
if(dx*dx+dz*dz < 0.62 && a.pause<=0){
this.pause=0.5; this.cool=1.4; this.yielding=true; break;
}
}
}
if(this.yielding && this.pause<=0) this.yielding=false;
// court turn-taking
if(this.court){
if(this.court.owner && this.court.owner!==this){ this.pause=0.25; this.idlePose(time); return; }
if(!this.court.owner) this.court.owner=this;
}
if(this.pause>0){ this.pause-=dt; this.idlePose(time); return; }
const from=this.path[this.i], to=this.path[this.j];
this.t+=dt/this.dur;
if(this.t>=1) this.land();
const a=this.path[this.i], b=this.path[this.j];
const s=Math.min(1,this.t);
const p=a.clone().lerp(b,s);
this.setYawTo(a,b);
const fwd=V(Math.sin(this.yaw),0,Math.cos(this.yaw));
const right=V(fwd.z,0,-fwd.x);
// foot placement: place the correct foot exactly on the chalk mark
if(this.mode==='trail'||this.mode==='step'){
const leftFoot = (this.i%2===0);
const off = leftFoot ? 0.13 : -0.13;
p.addScaledVector(right, off);
}
let y = this.baseY;
if(this.mode==='hop'){
const arc=Math.sin(Math.PI*Math.min(1,this.t));
y += arc*1.15;
this.phase += dt*2;
} else if(this.mode==='step'||this.mode==='trail'){
this.phase = (this.i + Math.min(1,this.t))*Math.PI;
y += Math.abs(Math.sin(this.phase))*0.03;
} else {
this.phase += dt*(this.mode==='run'?9.5:6.5);
y += Math.abs(Math.sin(this.phase))*0.05;
}
p.y=y;
this.pos.copy(p);
this.model.group.position.copy(p);
this.model.group.rotation.y=this.yaw;
// poses
const M=this.model;
if(this.mode==='hop'){
const tuck=Math.sin(Math.PI*Math.min(1,this.t));
M.legL.rotation.x=-0.95*tuck; M.legR.rotation.x=-0.35*tuck;
M.armL.rotation.x=-2.2*tuck; M.armR.rotation.x=-2.0*tuck;
M.armL.rotation.z=0.25; M.armR.rotation.z=-0.25;
} else if(this.mode==='step'||this.mode==='trail'){
const sw=Math.sin(this.phase)*0.62;
M.legL.rotation.x=sw; M.legR.rotation.x=-sw;
M.armL.rotation.x=-sw*0.55; M.armR.rotation.x=sw*0.55;
M.armL.rotation.z=0.12; M.armR.rotation.z=-0.12;
} else {
const sw=Math.sin(this.phase)*(this.mode==='run'?0.85:0.6);
M.legL.rotation.x=sw; M.legR.rotation.x=-sw;
M.armL.rotation.x=-sw*0.8; M.armR.rotation.x=sw*0.8;
M.armL.rotation.z=0.1; M.armR.rotation.z=-0.1;
}
// subtle lean
const lean = (this.mode==='run')?0.14:0.05;
M.body.rotation.x = lean*Math.sin(this.phase);
M.body.position.y = Math.abs(Math.sin(this.phase))*0.04;
}
idlePose(time){
const M=this.model;
const s=Math.sin(time*1.6+this.phase);
M.legL.rotation.x*=0.9; M.legR.rotation.x*=0.9;
M.armL.rotation.x = s*0.35-0.15; M.armR.rotation.x = -s*0.35-0.15;
M.armL.rotation.z=0.1+0.2*(0.5+0.5*s); M.armR.rotation.z=-0.1-0.2*(0.5-0.5*s);
M.body.rotation.x = 0.03*s;
M.body.position.y = Math.abs(s)*0.02;
}
}
function randomPal(){ return { shirt:pick(SHIRTS), skin:pick(SKINS), hair:pick(HAIRS), pants:pick(PANTS) }; }
/* --- kids --- */
const hopperA = new Agent({ name:'Ari', mode:'hop', path:courts.hop.path, court:courts.hop, speed:2.6, dwell:0.22, ...randomPal() });
const hopperB = new Agent({ name:'Bo', mode:'hop', path:courts.hop.path, court:courts.hop, speed:2.5, dwell:0.26, ...randomPal() });
const runner = new Agent({ name:'Dara', mode:'run', path:courts.grid.path, speed:4.4, dwell:0.18, ...randomPal() });
const spinner = new Agent({ name:'Emi', mode:'step', path:courts.spiral.path, speed:2.0, marks:[], ...randomPal() });
const circler = new Agent({ name:'Ivo', mode:'run', path:courts.rings.path, speed:3.1, dwell:0.4, ...randomPal() });
const weaver = new Agent({ name:'Jules',mode:'run', path:courts.weave.path, speed:4.0, ...randomPal() });
const walker = new Agent({ name:'Kit', mode:'trail',path:courts.trail.path, speed:2.4, marks:courts.trail.marks, ...randomPal() });
const crosser = new Agent({ name:'Lena', mode:'step', path:courts.cross.path, speed:2.0, marks:courts.cross.marks, ...randomPal() });
const talker1 = new Agent({ name:'Mira', mode:'idle', path:[V(-6.2,0,11.6)], baseY:0, ...randomPal() });
const talker2 = new Agent({ name:'Niko', mode:'idle', path:[V(-4.3,0,11.7)], baseY:0, ...randomPal() });
agents.push(hopperA,hopperB,runner,spinner,circler,weaver,walker,crosser,talker1,talker2);
// turn talker2 to face talker1
talker2.yaw = Math.PI;
/* --- ball + chaser --- */
const ball = new THREE.Group();
ball.position.set(5.6,G+0.22,8.0);
box(0.42,0.42,0.42,0xf7f7f7,0,0,0,ball);
box(0.46,0.16,0.46,0xe63946,0,0,0,ball);
box(0.16,0.46,0.46,0xe63946,0,0,0,ball);
scene.add(ball);
const ballState={ v:new THREE.Vector3(2.2,0,-1.4), life:0 };
const chaser = new Agent({ name:'Ola', mode:'idle', path:[V(5.6,G,9.4)], speed:3.6, ...randomPal() });
agents.push(chaser);
function updateBall(dt){
ballState.v.y -= 9.8*dt;
ball.position.addScaledVector(ballState.v, dt);
if(ball.position.y < G+0.22){
ball.position.y = G+0.22;
ballState.v.y = Math.abs(ballState.v.y)*0.55;
if(ballState.v.y<0.4) ballState.v.y=0;
}
const fr=0.35;
ballState.v.x -= ballState.v.x*fr*dt; ballState.v.z -= ballState.v.z*fr*dt;
ball.rotation.x += ballState.v.z*dt*1.6;
ball.rotation.z -= ballState.v.x*dt*1.6;
// keep inside the yard
const lim=14.2;
if(Math.abs(ball.position.x)>lim){ ball.position.x=Math.sign(ball.position.x)*lim; ballState.v.x*=-0.6; }
if(Math.abs(ball.position.z)>lim){ ball.position.z=Math.sign(ball.position.z)*lim; ballState.v.z*=-0.6; }
// chaser kicks it
const dx=ball.position.x-chaser.pos.x, dz=ball.position.z-chaser.pos.z;
const d=Math.hypot(dx,dz);
if(d>1.2){
chaser.mode='walk'; chaser.speed=3.6;
chaser.pos.x += dx/d*Math.min(d, 3.6*dt);
chaser.pos.z += dz/d*Math.min(d, 3.6*dt);
chaser.yaw = Math.atan2(dx,dz);
chaser.model.group.position.copy(chaser.pos);
chaser.model.group.rotation.y=chaser.yaw;
chaser.phase += dt*9;
const sw=Math.sin(chaser.phase)*0.75;
chaser.model.legL.rotation.x=sw; chaser.model.legR.rotation.x=-sw;
chaser.model.armL.rotation.x=-sw*0.8; chaser.model.armR.rotation.x=sw*0.8;
} else {
chaser.mode='idle';
const a=Math.random()*Math.PI*2;
ballState.v.set(Math.cos(a)*5.2, 3.0, Math.sin(a)*5.2);
}
}
/* ============================================================
6. CAMERA CONTROLS (custom, no addons)
============================================================ */
const cam = {
target:new THREE.Vector3(0,0,0), tTarget:new THREE.Vector3(0,0,0),
az:-0.55, tAz:-0.55, pol:0.95, tPol:0.95, dist:48, tDist:48,
mode:'overview', lastInput:0
};
function applyCamera(){
cam.az += (cam.tAz - cam.az )*0.12;
cam.pol += (cam.tPol - cam.pol )*0.12;
cam.dist += (cam.tDist - cam.dist)*0.10;
cam.target.lerp(cam.tTarget, 0.10);
const sp=Math.sin(cam.pol), cp=Math.cos(cam.pol);
camera.position.set(
cam.target.x + cam.dist*sp*Math.sin(cam.az),
cam.target.y + cam.dist*cp,
cam.target.z + cam.dist*sp*Math.cos(cam.az)
);
camera.lookAt(cam.target);
}
let dragging=false, panning=false, lastX=0, lastY=0;
const el=renderer.domElement;
el.addEventListener('contextmenu', e=>e.preventDefault());
el.addEventListener('pointerdown', e=>{
dragging=true; panning = e.button===2 || e.shiftKey;
lastX=e.clientX; lastY=e.clientY; cam.lastInput=performance.now();
el.setPointerCapture(e.pointerId);
});
el.addEventListener('pointermove', e=>{
if(!dragging) return;
const dx=e.clientX-lastX, dy=e.clientY-lastY; lastX=e.clientX; lastY=e.clientY;
cam.lastInput=performance.now();
if(panning){
const right=V(Math.cos(cam.az),0,-Math.sin(cam.az));
const fwd=V(Math.sin(cam.az),0,Math.cos(cam.az));
cam.tTarget.addScaledVector(right,-dx*0.035).addScaledVector(fwd,-dy*0.035);
cam.tTarget.x=Math.max(-30,Math.min(30,cam.tTarget.x));
cam.tTarget.z=Math.max(-24,Math.min(24,cam.tTarget.z));
} else {
cam.tAz -= dx*0.005;
cam.tPol = Math.max(0.06, Math.min(1.42, cam.tPol - dy*0.004));
}
});
window.addEventListener('pointerup', ()=>{dragging=false; panning=false;});
el.addEventListener('wheel', e=>{
e.preventDefault();
cam.tDist = Math.max(9, Math.min(85, cam.tDist + Math.sign(e.deltaY)*3.2));
cam.lastInput=performance.now();
}, {passive:false});
// pinch zoom (touch)
let pinchDist=null;
el.addEventListener('touchstart', e=>{
if(e.touches.length===2){
pinchDist=Math.hypot(e.touches[0].clientX-e.touches[1].clientX, e.touches[0].clientY-e.touches[1].clientY);
}
},{passive:true});
el.addEventListener('touchmove', e=>{
if(e.touches.length===2 && pinchDist){
const d=Math.hypot(e.touches[0].clientX-e.touches[1].clientX, e.touches[0].clientY-e.touches[1].clientY);
cam.tDist=Math.max(9,Math.min(85, cam.tDist + (pinchDist-d)*0.06));
pinchDist=d; cam.lastInput=performance.now();
}
},{passive:true});
function setMode(m){
cam.mode=m;
document.querySelectorAll('#ctrls button').forEach(b=>b.classList.remove('on'));
if(m==='overview') document.getElementById('bOverview').classList.add('on');
if(m==='follow') document.getElementById('bFollow').classList.add('on');
if(m==='top') document.getElementById('bTop').classList.add('on');
}
document.getElementById('bOverview').onclick=()=>setMode('overview');
document.getElementById('bFollow').onclick=()=>setMode('follow');
document.getElementById('bTop').onclick=()=>setMode('top');
let labelsOn=true;
document.getElementById('bLabels').onclick=function(){
labelsOn=!labelsOn; this.classList.toggle('on',labelsOn);
agents.forEach(a=>a.label.visible=labelsOn);
};
document.getElementById('bRepaint').onclick=()=>{
paletteKeys=['white','blue','coral','yellow','green','purple','orange','pink'];
paletteKeys.sort(()=>Math.random()-0.5);
let k=0;
chalkRecs.forEach(r=>{
const key = r.key==='white' ? 'white' : paletteKeys[k++ % paletteKeys.length];
const c=new THREE.Color(PALETTE[key]);
chalkMesh.setColorAt(r.i,c); r.color=c.clone();
});
chalkMesh.instanceColor.needsUpdate=true;
};
let timeScale=1;
document.getElementById('speed').oninput=e=>{timeScale=parseFloat(e.target.value);};
/* ============================================================
7. RESIZE + LOOP
============================================================ */
function resize(){
const w=window.innerWidth, h=window.innerHeight;
renderer.setSize(Math.max(2,Math.floor(w/PIX)), Math.max(2,Math.floor(h/PIX)), false);
camera.aspect=w/h; camera.updateProjectionMatrix();
}
window.addEventListener('resize', resize);
resize();
chalkMesh.instanceMatrix.needsUpdate=true;
if(chalkMesh.instanceColor) chalkMesh.instanceColor.needsUpdate=true;
const clock=new THREE.Clock();
function animate(){
requestAnimationFrame(animate);
const dt=Math.min(0.05, clock.getDelta())*timeScale;
const time=clock.elapsedTime;
for(const a of agents) a.update(dt,time);
updateBall(dt);
// camera behaviour per mode
if(cam.mode==='overview'){
cam.tTarget.set(0,0,0); cam.tDist=48; cam.tPol=0.95;
if(performance.now()-cam.lastInput>3500) cam.tAz+=0.0022;
} else if(cam.mode==='follow'){
cam.tTarget.copy(hopperA.pos); cam.tTarget.y=0.6;
cam.tDist=15; cam.tPol=1.05;
} else if(cam.mode==='top'){
cam.tTarget.set(0,0,0); cam.tDist=62; cam.tPol=0.07;
}
applyCamera();
renderer.render(scene,camera);
}
animate();
</script>
</body>
</html>
```