Back to the app
The code
Cat Island by GPT-6.2 Sol
Exactly as the AI wrote it. Shown as text, so nothing runs on this page.
index.html
works · auto-checked (Checked by an automated pass. Not hand-played yet.)<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>Miso Island</title>
<style>
* { box-sizing: border-box; }
:root { font-family: "Trebuchet MS", system-ui, sans-serif; color-scheme: light; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; overscroll-behavior: none; }
body { background: #c0e0cb; }
button { font: inherit; color: inherit; }
canvas { display: block; }
[hidden] { display: none !important; }
.sr { position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
#island {
--ink: #283f36;
--muted: #53695a;
--paper: #fff9e6;
--card: #fbf4ddf5;
--edge: #697e6466;
--shadow: #34524a34;
--accent: #f4cf7d;
--active-ink: #423b2b;
--hover: #eae8cd;
position: relative; isolation: isolate; width: 100%; height: 100%; overflow: hidden;
color: var(--ink); user-select: none; -webkit-user-select: none;
}
#island[data-time="sunset"] { --paper: #fff0da; --card: #fff0dcf5; --edge: #93665563; --shadow: #67405638; --hover: #f7dfc7; --accent: #f3b38c; }
#island[data-time="night"] {
--ink: #fff2d5; --muted: #d0d6ce; --paper: #27374a; --card: #263548f5;
--edge: #bacad05c; --shadow: #10152480; --accent: #e8c884; --hover: #35475a;
}
.sky, .sky-layer, #sky-art, #stage, #stage > canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.sky-layer { transition: opacity .85s ease; pointer-events: none; }
.sky-day {
background: radial-gradient(ellipse at 48% 64%, #fff1bc70, transparent 63%),
linear-gradient(175deg, #99cfc6 0%, #c5e4cf 48%, #e3eac6 74%, #f2e4ba 100%);
}
.sky-sunset {
opacity: 0;
background: radial-gradient(ellipse at 72% 53%, #ffe0a7 0%, #ffe0a75c 30%, transparent 61%),
linear-gradient(170deg, #a28fc0 0%, #efafa8 43%, #ffd2a7 73%, #cfaab0 100%);
}
.sky-night {
opacity: 0;
background: radial-gradient(ellipse at 61% 62%, #59849353, transparent 66%),
linear-gradient(174deg, #172840 0%, #2b415d 49%, #455773 77%, #585e75 100%);
}
[data-time="sunset"] .sky-sunset, [data-time="night"] .sky-night { opacity: 1; }
#sky-art { pointer-events: none; }
#stage { touch-action: none; cursor: grab; outline: none; }
#stage.grabbing { cursor: grabbing; }
#stage > canvas, .pixel { image-rendering: pixelated; }
.hud-top {
position: absolute; z-index: 3;
top: max(26px, env(safe-area-inset-top)); left: max(32px, env(safe-area-inset-left)); right: max(32px, env(safe-area-inset-right));
display: flex; justify-content: space-between; align-items: flex-start; gap: 14px; pointer-events: none;
}
.brand { display: flex; align-items: center; gap: 14px; }
.brand-seal {
display: grid; place-items: center; flex: none; width: 56px; height: 56px;
border: 1px solid #5f785878; border-radius: 15px; background: #fff3ce;
box-shadow: 0 3px 0 #5e7f5d41, inset 0 0 0 3px #fff8e6;
}
.brand-seal canvas { width: 43px; height: 43px; }
h1 { margin: 0; }
#wordmark { width: clamp(190px, 21vw, 262px); height: auto; }
[data-time="night"] #wordmark { filter: brightness(0) invert(.96) sepia(.26); }
.subtitle { margin: 7px 0 0 1px; color: var(--muted); font: 700 10px/1 "Courier New", monospace; letter-spacing: .14em; text-transform: uppercase; }
.panel {
background: var(--card); border: 1px solid var(--edge); border-radius: 15px;
box-shadow: 0 3px 0 var(--shadow), 0 9px 24px #263a3610;
transition: background .38s, border-color .38s; pointer-events: auto;
}
.choices { display: flex; gap: 4px; padding: 5px; }
.choice, .square, .dismiss {
display: flex; align-items: center; justify-content: center; border: 1px solid transparent;
background: transparent; cursor: pointer; padding: 0; touch-action: manipulation;
-webkit-tap-highlight-color: transparent;
}
.choice { min-width: 79px; min-height: 43px; border-radius: 10px; padding: 0 10px; gap: 7px; font-size: 13px; font-weight: 800; }
.choice.is-on { background: var(--accent); border-color: #9e744d62; color: var(--active-ink); box-shadow: inset 0 -2px #94683929; }
.choice:active, .square:active, .dismiss:active { transform: translateY(1px); }
button:focus-visible { outline: 2px solid #d88648; outline-offset: 2px; }
@media (hover: hover) {
.choice:not(.is-on):hover, .square:hover, .dismiss:hover { background: var(--hover); }
}
.symbol { width: 17px; height: 17px; flex: none; }
[data-time="night"] .symbol { filter: brightness(0) invert(.91) sepia(.16); }
[data-time="night"] .choice.is-on .symbol { filter: none; }
#places, #cat-marker { pointer-events: none; }
.place, #cat-marker {
position: absolute; top: 0; left: 0; display: flex; align-items: center; justify-content: center; gap: 7px;
border: 1px solid var(--edge); border-radius: 9px; background: var(--paper);
box-shadow: 0 2px 0 var(--shadow); white-space: nowrap; font-size: 12px; font-weight: 800; line-height: 1;
padding: 7px 9px; opacity: 0; transition: opacity .25s, background .38s;
will-change: transform;
}
.place::after, #cat-marker::after {
content: ""; position: absolute; top: 100%; left: calc(50% - 3px); width: 6px; height: 6px;
border-right: 1px solid var(--edge); border-bottom: 1px solid var(--edge); background: var(--paper);
transform: translateY(-3px) rotate(45deg);
}
.place .symbol { width: 13px; height: 13px; }
#cat-marker { padding: 7px 10px; background: var(--accent); color: var(--active-ink); z-index: 1; }
#cat-marker::after { background: var(--accent); }
.place.visible, #cat-marker.visible { opacity: 1; }
.hud-bottom {
position: absolute; z-index: 3;
left: max(32px, env(safe-area-inset-left)); right: max(32px, env(safe-area-inset-right));
bottom: max(25px, env(safe-area-inset-bottom));
display: flex; justify-content: space-between; align-items: end; gap: 12px; pointer-events: none;
}
#resident {
min-height: 63px; display: flex; align-items: center; gap: 10px; padding: 7px 12px 7px 7px;
width: max-content; max-width: 67vw;
}
.portrait-pad {
display: grid; place-items: center; width: 46px; height: 46px; flex: none; border-radius: 10px;
background: #e9d8ae; box-shadow: inset 0 -2px #8d724130;
}
#portrait { width: 40px; height: 40px; }
.resident-text { min-width: 0; }
#resident-name { display: block; font-size: 15px; font-weight: 900; white-space: nowrap; }
#resident-status { display: block; margin-top: 3px; color: var(--muted); font-size: 12px; white-space: nowrap; }
.dismiss { width: 44px; height: 44px; border-radius: 9px; margin: -3px -8px -3px 0; flex: none; }
.guide {
position: absolute; left: 50%; bottom: 1px; transform: translateX(-50%);
display: flex; align-items: center; gap: 13px; min-height: 41px; padding: 7px 13px;
border: 1px solid var(--edge); border-radius: 12px; background: var(--card);
font-size: 12px; color: var(--muted); white-space: nowrap;
}
.guide span { display: flex; align-items: center; gap: 6px; }
.guide i { width: 1px; height: 14px; background: var(--edge); }
.guide .symbol { width: 14px; height: 14px; }
.touch-only { display: none !important; }
.nav { display: flex; align-items: center; padding: 4px; gap: 1px; margin-left: auto; }
.square { width: 43px; min-height: 44px; border-radius: 9px; }
.nav-divider { width: 1px; height: 23px; margin: 0 2px; background: var(--edge); }
#tour {
position: absolute; z-index: 2; top: max(30px, env(safe-area-inset-top)); left: 50%; transform: translateX(-50%);
padding: 8px 12px; border: 1px solid var(--edge); border-radius: 20px; background: var(--card);
color: var(--muted); font: 700 11px/1 "Trebuchet MS", sans-serif; pointer-events: none; white-space: nowrap;
}
@media (pointer: coarse) {
.touch-only { display: flex !important; }
.mouse-only { display: none !important; }
}
@media (max-width: 760px) {
.hud-top { top: max(16px, env(safe-area-inset-top)); left: max(16px, env(safe-area-inset-left)); right: max(16px, env(safe-area-inset-right)); }
.hud-bottom { bottom: max(15px, env(safe-area-inset-bottom)); left: max(14px, env(safe-area-inset-left)); right: max(14px, env(safe-area-inset-right)); }
.guide { bottom: calc(100% + 10px); min-height: 34px; }
#tour { top: 86px; }
}
@media (max-width: 570px) {
.hud-top { flex-direction: column; align-items: center; gap: 14px; }
.brand { gap: 11px; }
.brand-seal { width: 47px; height: 47px; border-radius: 12px; }
.brand-seal canvas { width: 36px; height: 36px; }
#wordmark { width: 201px; }
.subtitle { font-size: 9px; }
.choice { min-width: 79px; min-height: 44px; padding: 0 8px; }
#tour { top: 140px; }
.place { font-size: 10px; gap: 4px; padding: 5px 6px; }
.place .symbol { width: 11px; height: 11px; }
#resident { padding-right: 7px; gap: 7px; min-height: 55px; }
.portrait-pad { width: 36px; height: 36px; }
#portrait { width: 32px; height: 32px; }
#resident-name { font-size: 13px; }
#resident-status { font-size: 11px; }
.nav { padding: 3px; }
.square { width: 41px; }
}
@media (max-width: 350px) {
.hud-bottom { left: 8px; right: 8px; gap: 4px; }
.dismiss { width: 37px; margin-right: -6px; }
.nav-divider { margin: 0; }
}
@media (max-height: 485px) and (min-width: 571px) {
.hud-top { top: 10px; }
.hud-bottom { bottom: 9px; }
.brand-seal { width: 43px; height: 43px; }
.brand-seal canvas { width: 34px; height: 34px; }
#wordmark { width: 190px; }
#tour { top: 13px; }
}
</style>
</head>
<body>
<main id="island" data-time="day">
<div class="sky" aria-hidden="true">
<div class="sky-layer sky-day"></div>
<div class="sky-layer sky-sunset"></div>
<div class="sky-layer sky-night"></div>
<canvas id="sky-art"></canvas>
</div>
<div id="stage" aria-label="Interactive three-dimensional cat town" aria-describedby="controls-help">
<canvas id="world" aria-hidden="true"></canvas>
<canvas id="software" aria-hidden="true" hidden></canvas>
<div id="places" aria-hidden="true">
<div class="place" id="market-label"><canvas class="pixel symbol" data-icon="fish"></canvas>Fish Market</div>
<div class="place" id="park-label"><canvas class="pixel symbol" data-icon="yarn"></canvas>Yarn Park</div>
<div class="place" id="pond-label"><canvas class="pixel symbol" data-icon="reed"></canvas>Lily Pond</div>
</div>
<div id="cat-marker" aria-hidden="true"></div>
</div>
<header class="hud-top">
<div class="brand">
<div class="brand-seal"><canvas class="pixel" id="seal" aria-hidden="true"></canvas></div>
<div>
<h1><span class="sr">Miso Island</span><canvas class="pixel" id="wordmark" aria-hidden="true"></canvas></h1>
<p class="subtitle">A cat town in the clouds</p>
</div>
</div>
<div class="choices panel" role="group" aria-label="Time of day">
<button type="button" class="choice is-on" data-look="day" aria-pressed="true"><canvas class="pixel symbol" data-icon="sun" aria-hidden="true"></canvas>Day</button>
<button type="button" class="choice" data-look="sunset" aria-pressed="false"><canvas class="pixel symbol" data-icon="horizon" aria-hidden="true"></canvas>Sunset</button>
<button type="button" class="choice" data-look="night" aria-pressed="false"><canvas class="pixel symbol" data-icon="moon" aria-hidden="true"></canvas>Night</button>
</div>
</header>
<div id="tour" hidden>Tour playing · Touch or click to take over</div>
<footer class="hud-bottom">
<div class="panel" id="resident">
<div class="portrait-pad"><canvas class="pixel" id="portrait" aria-hidden="true"></canvas></div>
<div class="resident-text" aria-live="polite">
<strong id="resident-name">12 residents</strong><span id="resident-status">Tap a cat to follow</span>
</div>
<button class="dismiss" type="button" id="unfollow" aria-label="Stop following" title="Stop following" hidden><canvas class="pixel symbol" data-icon="close" aria-hidden="true"></canvas></button>
</div>
<div class="guide" aria-hidden="true">
<span><canvas class="pixel symbol" data-icon="orbit"></canvas>Drag to turn</span><i></i>
<span class="mouse-only"><canvas class="pixel symbol" data-icon="zoom"></canvas>Scroll to zoom</span>
<span class="touch-only"><canvas class="pixel symbol" data-icon="zoom"></canvas>Pinch to zoom</span>
</div>
<div class="nav panel" role="group" aria-label="Camera controls">
<button class="square" type="button" id="zoom-out" title="Zoom out" aria-label="Zoom out"><canvas class="pixel symbol" data-icon="minus" aria-hidden="true"></canvas></button>
<button class="square" type="button" id="zoom-in" title="Zoom in" aria-label="Zoom in"><canvas class="pixel symbol" data-icon="plus" aria-hidden="true"></canvas></button>
<div class="nav-divider"></div>
<button class="square" type="button" id="reset-view" title="Reset view" aria-label="Reset view"><canvas class="pixel symbol" data-icon="frame" aria-hidden="true"></canvas></button>
</div>
</footer>
<p class="sr" id="controls-help">Drag to orbit the island. Scroll or pinch to zoom. Tap or click any cat to follow it.</p>
</main>
<script type="text/plain" id="vertex-source">#version 300 es
precision highp float;
layout(location=0) in vec3 aVertex;
layout(location=1) in vec3 aNormal;
layout(location=2) in vec3 aCenter;
layout(location=3) in vec3 aSize;
layout(location=4) in vec4 aMaterial;
layout(location=5) in vec4 aRotation;
layout(location=6) in float aFaces;
uniform vec3 uRight, uUp, uBack, uTarget;
uniform vec3 uShadowRight, uShadowUp, uLight;
uniform vec2 uScale;
uniform bool uShadowPass;
out vec3 vWorld;
flat out vec3 vNormal;
flat out vec4 vMaterial;
vec3 rotate(vec3 p) {
p = vec3(p.x, p.y*aRotation.z-p.z*aRotation.w, p.y*aRotation.w+p.z*aRotation.z);
return vec3(p.x*aRotation.x+p.z*aRotation.y, p.y, p.z*aRotation.x-p.x*aRotation.y);
}
void main() {
vWorld = rotate(aVertex*aSize) + aCenter;
vNormal = rotate(aNormal);
vMaterial = aMaterial;
if ((int(aFaces) & (1 << (gl_VertexID/6))) == 0) {
gl_Position = vec4(2., 2., 2., 1.);
} else if (uShadowPass) {
gl_Position = vec4(dot(vWorld,uShadowRight)/17., dot(vWorld,uShadowUp)/17., -dot(vWorld,uLight)/37., 1.);
} else {
vec3 p = vWorld-uTarget;
gl_Position = vec4(dot(p,uRight)*uScale.x, dot(p,uUp)*uScale.y, -dot(p,uBack)/60., 1.);
}
}
</script>
<script type="text/plain" id="shadow-source">#version 300 es
precision highp float;
out vec4 outColor;
void main() {
float depth = gl_FragCoord.z*255.;
outColor = vec4(floor(depth)/255., fract(depth), 0., 1.);
}
</script>
<script type="text/plain" id="color-source">#version 300 es
precision highp float;
in vec3 vWorld;
flat in vec3 vNormal;
flat in vec4 vMaterial;
uniform vec3 uShadowRight, uShadowUp, uLight;
uniform vec2 uTone;
uniform sampler2D uShadow;
uniform float uShadowReady;
uniform vec3 uLamps[8];
out vec4 outColor;
float litAt(vec2 p, float depth) {
vec2 encoded = texture(uShadow,p).rg;
return step(depth, encoded.x+encoded.y/255.);
}
float sunlight(vec3 n) {
if (uShadowReady < .5) return 1.;
vec3 p = vWorld+n*.026;
vec2 uv = vec2(dot(p,uShadowRight),dot(p,uShadowUp))/34.+.5;
if (any(lessThan(uv,vec2(.003))) || any(greaterThan(uv,vec2(.997)))) return 1.;
float depth = .5-dot(p,uLight)/74.-.00065;
const float spread = .00145;
return .25*(litAt(uv+vec2(spread,spread),depth)+litAt(uv+vec2(-spread,spread),depth)
+litAt(uv+vec2(spread,-spread),depth)+litAt(uv-vec2(spread),depth));
}
void main() {
vec3 n = normalize(vNormal);
vec3 pigment = vMaterial.rgb;
float kind = vMaterial.a;
float highFace = max(n.y,0.);
float ray = max(dot(uLight,n),0.);
float warm = uTone.x;
float night = uTone.y;
float sun = kind == 4. || ray < .03 ? 1. : sunlight(n);
float shade = .68+.34*ray*mix(.31,1.,sun)+.105*highFace;
vec3 dayColor = pigment*shade + vec3(.03,.034,.024)*(1.-ray);
vec3 duskColor = pigment*vec3(1.10,.87,.81)*(.64+.42*ray*mix(.38,1.,sun)+.09*highFace)+vec3(.058,.028,.033);
vec3 nightColor = pigment*vec3(.38,.49,.72)*(.76+.20*ray+.17*highFace)+vec3(.028,.042,.070);
vec3 col = mix(mix(dayColor,duskColor,warm),nightColor,night);
if (kind == 4.) {
vec3 cloudDay = pigment*(.88+.12*highFace);
vec3 cloudDusk = pigment*vec3(.97,.78,.82)+vec3(.05,.019,.033);
vec3 cloudNight = pigment*vec3(.33,.39,.56)+vec3(.035,.040,.070);
col = mix(mix(cloudDay,cloudDusk,warm),cloudNight,night);
} else if (night > .01) {
float glow = 0.;
for (int i=0; i<8; i++) {
vec3 delta = vWorld-uLamps[i];
float pool = max(0.,1.-length(delta)/2.35);
glow += pool*pool;
}
col += night*min(glow,1.)*(vec3(.23,.125,.034)+pigment*vec3(.24,.12,.04));
}
if (kind == 1.) col = mix(col,vec3(1.,.76,.31)*(.86+.14*highFace),night*.98);
if (kind == 2.) col = mix(col,vec3(1.,.71,.27),.13+.87*night);
if (kind == 3.) col = mix(col,mix(pigment,pigment*vec3(.44,.66,.86),night),.28);
if (kind == 5.) col = mix(col,vec3(.81,.89,.83),.60);
outColor = vec4(clamp(col,0.,1.),1.);
}
</script>
<script>
"use strict";
const byId = id => document.getElementById(id);
const app = byId("island");
const stage = byId("stage");
const TAU = Math.PI * 2;
const clamp = (v, low, high) => Math.max(low, Math.min(high, v));
const mix = (a, b, t) => a + (b - a) * t;
const smooth = t => { t = clamp(t, 0, 1); return t * t * (3 - 2 * t); };
const range = (v, from, to) => smooth((v - from) / (to - from));
const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
const cross = (a, b) => [a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0]];
const normalize = v => { const length = Math.hypot(...v); return v.map(n => n / length); };
let seed = 33853;
function random() {
seed = (Math.imul(1664525, seed) + 1013904223) >>> 0;
return seed / 4294967296;
}
function hash(x, z, salt = 0) {
let n = Math.imul(x + salt * 27, 374761393) + Math.imul(z + salt * 13, 668265263);
n = Math.imul(n ^ (n >>> 13), 1274126177);
return ((n ^ (n >>> 16)) >>> 0) / 4294967296;
}
const choose = (items, fraction = random()) => items[Math.floor(fraction * items.length)];
const font = {
M: ["10001","11011","11111","10101","10001","10001","10001"],
I: ["111","010","010","010","010","010","111"],
S: ["01111","10000","10000","01110","00001","00001","11110"],
O: ["01110","11011","10001","10001","10001","11011","01110"],
L: ["10000","10000","10000","10000","10000","10000","11111"],
A: ["01110","11011","10001","11111","10001","10001","10001"],
N: ["10001","11001","11001","10101","10011","10011","10001"],
D: ["11110","11011","10001","10001","10001","11011","11110"]
};
const icons = {
sun: ["000010000","010010010","000111000","001000100","111000111","001000100","000111000","010010010","000010000"],
horizon: ["000010000","010000010","000111000","001000100","111111111","000000000","011111110","000000000","001111100"],
moon: ["001110001","011000011","110000001","110000000","110000010","110000110","111001100","011111000","001110000"],
fish: ["000000000","000100000","001111001","010111011","111111111","011111011","001110001","000100000","000000000"],
yarn: ["001111000","011001100","110111010","101101010","101011010","110110100","011111100","000001010","000000101"],
reed: ["000001000","001001010","001001010","001001010","000001000","010101010","001111100","000000000","011111110"],
orbit: ["000000000","010000010","110000011","111111111","010000010","000000000","001111100","010000010","000000000"],
zoom: ["001110000","011011000","110001100","100000100","110001100","011011000","001111100","000000110","000000011"],
plus: ["000000000","000010000","000010000","000010000","011111110","000010000","000010000","000010000","000000000"],
minus: ["000000000","000000000","000000000","000000000","011111110","000000000","000000000","000000000","000000000"],
close: ["000000000","010000010","001000100","000101000","000010000","000101000","001000100","010000010","000000000"],
frame: ["111000111","100000001","100000001","000000000","000010000","000000000","100000001","100000001","111000111"]
};
function drawBits(ctx, bits, x, y, color) {
ctx.fillStyle = color;
bits.forEach((row, iy) => {
for (let ix = 0; ix < row.length; ix++) if (row[ix] === "1") ctx.fillRect(x + ix, y + iy, 1, 1);
});
}
const title = byId("wordmark");
const letters = Array.from("MISO ISLAND");
title.width = letters.reduce((sum, ch) => sum + (ch === " " ? 4 : font[ch][0].length + 1), -1);
title.height = 7;
let pen = 0;
const titleContext = title.getContext("2d");
for (const ch of letters) {
if (ch === " ") { pen += 4; continue; }
drawBits(titleContext, font[ch], pen, 0, "#294536");
pen += font[ch][0].length + 1;
}
document.querySelectorAll("[data-icon]").forEach(canvas => {
canvas.width = canvas.height = 9;
drawBits(canvas.getContext("2d"), icons[canvas.dataset.icon], 0, 0, "#425448");
});
const pigments = new Map();
function rgb(hex) {
if (!pigments.has(hex)) {
const n = Number.parseInt(hex.slice(1), 16);
pigments.set(hex, [(n >> 16) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255]);
}
return pigments.get(hex);
}
const fixed = [];
const lamps = [];
const occluders = [];
const stride = 15;
function box(list, x, y, z, w, h, d, paint, kind = 0, yaw = 0, pitch = 0, faces = 63) {
if (!faces) return;
const [r, g, b] = rgb(paint);
list.push([x,y,z,w,h,d,r,g,b,kind,Math.cos(yaw),Math.sin(yaw),Math.cos(pitch),Math.sin(pitch),faces]);
}
const block = (...values) => box(fixed, ...values);
function pack(list) {
const result = new Float32Array(list.length * stride);
for (let i = 0; i < list.length; i++) result.set(list[i], i * stride);
return result;
}
const stoneRows = [
[-5,3],[-7,5],[-8,7],[-9,8],[-9,9],[-10,9],[-10,10],[-10,10],[-10,10],
[-10,10],[-9,10],[-9,10],[-9,9],[-8,9],[-8,8],[-6,7],[-4,4]
];
function land(x, z) {
const row = stoneRows[z + 8];
return Boolean(row && x >= row[0] && x <= row[1]);
}
function lake(x, z) {
return ((x-5.65)/3.22)**2 + ((z-3.36)/2.43)**2 < 1 || (x === 6 && (z === 6 || z === 7));
}
const grass = ["#86b972","#83b36d","#92bd75","#99c47a","#8ab974","#8fbd76"];
const lakeColors = ["#57bfba","#56bab9","#56c4bf","#53bfc1","#60c8c0"];
const layerEdges = [-.24, -.66, -1.25, -1.93, -2.71, -3.51, -4.36, -5.23, -6.08, -6.88, -7.55];
const rockColors = [
["#bf926d","#cb9c6e","#c2936b"],["#b48167","#b88767","#c6926b"],
["#d09e75","#c79c78","#bf8f70"],["#aa795f","#af8067","#b98a70"],
["#9a6f60","#a77964","#9c7364"],["#ac7e6c","#a67566","#a47768"],
["#88655e","#926e65","#90685e"],["#7d625c","#8b6a62","#8c6561"],
["#735a57","#7d6059","#805e5a"],["#695553","#745654","#715958"]
];
const shrinking = [0, .55, .92, 1.45, 2.02, 2.62, 3.45, 4.3, 5.0, 5.8];
const earth = new Map();
const findColumn = (x, z) => earth.get(x + "," + z);
const hasRock = (x, z, layer) => (findColumn(x,z)?.levels || 0) > layer;
for (let z = -8; z <= 8; z++) for (let x = -10; x <= 10; x++) {
if (!land(x,z)) continue;
let rim = 100;
for (let dz = -7; dz <= 7; dz++) for (let dx = -7; dx <= 7; dx++) {
if (!land(x+dx,z+dz)) rim = Math.min(rim, Math.hypot(dx,dz));
}
const depth = rim + hash(x,z,7)*.69 + Math.sin(x*.69+z*.39)*.23;
const levels = shrinking.filter(threshold => depth >= threshold).length;
earth.set(x + "," + z, {x,z,levels,rim});
}
for (const {x,z,levels,rim} of earth.values()) {
const water = lake(x,z);
const cap = water ? -.105 : 0;
let topFaces = 4;
if (!land(x+1,z) || (!water && lake(x+1,z))) topFaces |= 1;
if (!land(x-1,z) || (!water && lake(x-1,z))) topFaces |= 2;
if (!land(x,z+1) || (!water && lake(x,z+1))) topFaces |= 16;
if (!land(x,z-1) || (!water && lake(x,z-1))) topFaces |= 32;
block(x,(cap-.24)/2,z,1,cap+.24,1,choose(water ? lakeColors : grass,hash(x,z,2)),water ? 3 : 0,0,0,topFaces);
for (let l = 0; l < levels; l++) {
const top = layerEdges[l];
const bottom = layerEdges[l+1];
let faces = 0;
if (!hasRock(x+1,z,l)) faces |= 1;
if (!hasRock(x-1,z,l)) faces |= 2;
if (!hasRock(x,z+1,l)) faces |= 16;
if (!hasRock(x,z-1,l)) faces |= 32;
if (l+1 === levels) faces |= 8;
block(x,(top+bottom)/2,z,1,top-bottom,1,choose(rockColors[l],hash(x,z,l+11)),0,0,0,faces);
if (l < 6 && (faces & 16) && hash(x,z,l+21) > .46) {
const band = choose(["#9d715c","#dcac7b","#bd8c72"],hash(x,z,l+41));
block(x+hash(x,z,l+2)*.46-.23,bottom+(top-bottom)*.69,z+.505,.29+hash(x,z,l)*.51,.073,.027,band);
}
if (l < 6 && (faces & 1) && hash(x,z,l+31) > .58) {
block(x+.507,bottom+(top-bottom)*.42,z-.09,.032,.078,.55,choose(["#a87962","#d4a77a"],hash(x,z,l+39)));
}
}
if (water) {
if (hash(x,z,17) > .44) block(x+.09,-.099,z-.17,.39,.009,.115,choose(["#7fd1c6","#42afa9","#83d5cb"],hash(x,z,19)),3);
if (hash(x,z,28) > .7) block(x-.21,-.097,z+.20,.24,.012,.072,"#9cdddd",3);
} else if (rim < 1.5 && hash(x,z,13) > .3) {
block(x-.15,.003,z+.15,.20,.009,.11,"#a2c77e");
block(x+.25,.003,z-.27,.14,.009,.08,"#73a967");
}
}
const paths = [
[[-8.6,.9],[-6.4,.36],[-4.65,-.26],[-2.4,-.56],[-.25,-.68],[2.8,-.8],[5.7,-1.17],[8.25,-.62],[9.15,.79],[9.04,2.61]],
[[-5.27,.06],[-5.8,1.11],[-6.0,2.47],[-6.52,4.54],[-5.9,5.7],[-3.0,5.8],[-1.38,5.11]],
[[-.95,-.56],[-.28,1.14],[.35,3.45],[.1,4.44],[-1.38,5.11]],
[[.12,1.34],[1.23,2.63],[2.74,2.63]],
[[-.22,-.69],[-.30,-2.94]], [[5.66,-1.2],[5.86,-2.76]]
];
function pathDistance(x, z) {
let nearest = Infinity;
for (const way of paths) for (let i = 1; i < way.length; i++) {
const [ax,az] = way[i-1], [bx,bz] = way[i];
const dx = bx-ax, dz = bz-az;
const t = clamp(((x-ax)*dx+(z-az)*dz)/(dx*dx+dz*dz),0,1);
nearest = Math.min(nearest, Math.hypot(x-ax-dx*t,z-az-dz*t));
}
return nearest;
}
const pathColors = ["#ded29d","#e8dba7","#e4d5a1","#d7c991","#e5d6a2"];
for (let iz = -16; iz <= 16; iz++) for (let ix = -21; ix <= 21; ix++) {
const x = ix/2 + .25, z = iz/2 + .25;
const tx = Math.floor(x+.5), tz = Math.floor(z+.5);
if (!land(tx,tz) || lake(tx,tz)) continue;
const distance = pathDistance(x,z);
const widePlaza = Math.hypot((x+1.61)*.75,(z+.32)*1.18) < 1.38;
if (distance > (.48+hash(ix,iz,6)*.24) && !widePlaza) continue;
const tint = hash(ix,iz,5);
block(x,.007,z,.501,.016,.501,choose(pathColors,tint));
if (tint > .76) block(x+.115,.017,z+.045,.17,.013,.095,"#caba86");
if (tint < .27) block(x-.12,.019,z-.11,.14,.014,.10,"#f6e3b3");
}
const wood = "#895940";
const darkWood = "#674c41";
const sandTrim = "#f7deb0";
function windowAt(x,y,z,w,h,side = false) {
const add = (u,v,depth,wide,tall,thick,color,kind = 0) => {
if (side) block(x+depth,y+v,z+u,thick,tall,wide,color,kind);
else block(x+u,y+v,z+depth,wide,tall,thick,color,kind);
};
add(0,0,.029,w+.20,h+.21,.13,darkWood);
add(0,0,.104,w,h,.036,"#679799",1);
add(0,0,.130,.052,h,.042,"#ecd3a5");
add(0,0,.133,w,.046,.044,"#ecd3a5");
add(-w*.16,h*.26,.129,w*.26,.042,.021,"#9ec4b9",1);
add(0,-h/2-.10,.10,w+.30,.105,.24,"#d9ac7b");
}
function cottage({x,z,w,d,h,wall,trim,roof,door,market = false}) {
const front = z+d/2;
block(x,.115,z,w+.12,.23,d+.13,"#b58e72");
block(x,(h+.16)/2,z,w,h-.16,d,wall);
occluders.push([x-w/2,.17,z-d/2,x+w/2,h,z+d/2]);
block(x,.24,front,w+.075,.105,.11,trim);
block(x+w/2,.24,z,.11,.105,d,trim);
for (const side of [-1,1]) {
block(x+side*(w/2-.11),h*.52,front,.19,h-.24,.13,trim);
block(x+w/2,h*.52,z+side*(d/2-.11),.13,h-.24,.19,trim);
}
if (market) {
block(x,.93,front+.018,w-.81,1.42,.105,"#6d5241");
block(x,1.02,front+.083,w-1.04,1.19,.045,"#506f67",1);
block(x,.64,front+.11,w-.73,.23,.19,darkWood);
block(x,1.72,front+.11,w-.71,.14,.19,trim);
for (const u of [-.96,0,.96]) block(x+u,1.14,front+.126,.087,1.10,.094,"#a36b4c");
} else {
const dx=x+w*.21;
block(dx,.73,front+.04,.87,1.40,.13,darkWood);
block(dx,.69,front+.118,.69,1.25,.058,door);
block(dx,.98,front+.158,.39,.38,.026,"#6a9291",1);
block(dx,.98,front+.177,.044,.37,.026,trim);
block(dx+.22,.58,front+.177,.075,.075,.065,"#f2c477");
block(dx,.085,front+.25,1.04,.17,.55,"#ccaf91");
windowAt(x-w*.23,1.25,front,.68,.64);
block(x-w*.23-.49,1.24,front+.066,.18,.82,.102,door);
block(x-w*.23+.49,1.24,front+.066,.18,.82,.102,door);
}
windowAt(x+w/2,1.25,z+.28,.77,.65,true);
block(x+w/2+.06,1.91,z-.79,.13,.19,.34,trim);
const half = (d+.64)/2;
const rows = 6;
const tileDepth = half/rows;
const rise = .207;
const roofWidth = w+.67;
const seams = Math.ceil(roofWidth/.62);
for (let row = 0; row < rows; row++) {
if (row > 0) block(x,h+row*rise-.088,z,w-.04,.22,Math.max(.12,d-2*row*tileDepth),wall);
for (const side of [-1,1]) {
const zz = z + side*(half-(row+.5)*tileDepth);
for (let tile = 0; tile < seams; tile++) {
const xx = x-roofWidth/2+(tile+.5)*roofWidth/seams;
const tint = choose(roof,hash(tile+side*21,row,Math.floor((x+9)*4)));
block(xx,h+.067+row*rise,zz,roofWidth/seams+.003,.147,tileDepth+.019,tint);
if ((tile+row)%3 === 0) block(xx+.12,h+.147+row*rise,zz+.033,.165,.014,.072,roof[2]);
}
}
}
block(x,h+1.20,z,roofWidth+.04,.185,.247,roof[0]);
block(x,h+1.30,z,roofWidth-.14,.028,.102,roof[2]);
block(x,h-.047,front+.30,roofWidth+.05,.14,.11,roof[0]);
const chimneyX = x+w*.32, chimneyZ=z-.57;
block(chimneyX,h+1.13,chimneyZ,.43,1.12,.47,"#bd816d");
block(chimneyX,h+1.69,chimneyZ,.55,.16,.56,"#e3b090");
block(chimneyX,h+1.777,chimneyZ,.31,.025,.30,darkWood);
const roofHeight = zz => h+.148 + clamp(Math.floor((half-Math.abs(zz-z))/tileDepth),0,rows-1)*rise;
return {x,z,front,w,roofHeight};
}
function awning(x, z, w, height, depth, solid, ivory, stripes) {
const sections = 6;
const sw = w/stripes;
for (let stripe = 0; stripe < stripes; stripe++) {
const xx = x-w/2+(stripe+.5)*sw;
const shade = stripe%2 ? ivory : solid;
for (let section = 0; section < sections; section++) {
block(xx,height-section*.069,z+(section+.5)*depth/sections,sw+.004,.129,depth/sections+.023,shade);
}
block(xx,height-.41,z+depth-.026,sw+.002,.19,.14,shade);
block(xx,height-.515,z+depth-.025,sw*.78,.082,.13,shade);
}
block(x,height-.42,z+depth-.14,w+.085,.055,.078,"#916c53");
}
function fish(x,y,z,shade,scale = 1) {
block(x,y,z,.37*scale,.112*scale,.165*scale,shade);
block(x+.02*scale,y+.049*scale,z,.18*scale,.035*scale,.11*scale,"#e5e0bd");
block(x-.215*scale,y,z,.08*scale,.088*scale,.119*scale,shade);
block(x-.263*scale,y,z,.07*scale,.075*scale,.225*scale,shade);
block(x+.127*scale,y,z+.088*scale,.038*scale,.036*scale,.018*scale,"#3a504e");
}
function fishCrate(x,y,z,scale = 1,full = true) {
block(x,y+.12*scale,z,.65*scale,.24*scale,.53*scale,"#b67d50");
for (const side of [-1,1]) {
block(x+side*.317*scale,y+.23*scale,z,.058*scale,.12*scale,.58*scale,"#e0ae6c");
block(x,y+.23*scale,z+side*.26*scale,.67*scale,.116*scale,.056*scale,"#d29d62");
}
block(x,y+.129*scale,z+.271*scale,.59*scale,.048*scale,.019*scale,"#f4c681");
if (full) {
fish(x,y+.258*scale,z+.105*scale,"#5caab2",scale*.89);
fish(x+.014*scale,y+.271*scale,z-.08*scale,"#8abdb7",scale*.91);
}
}
const market = cottage({
x:-5.45,z:-4.52,w:4.09,d:3.50,h:2.48,wall:"#f3d6aa",trim:"#f8e3b7",
roof:["#b95d58","#ce7364","#de8970","#c7695c","#d47b68"],market:true
});
awning(market.x,market.front+.065,4.48,2.43,1.53,"#c9605c","#fff0cb",10);
block(market.x,.46,market.front+.81,3.48,.83,.65,"#a66e4e");
block(market.x,.879,market.front+.82,3.68,.141,.77,"#dbac72");
for (let s = -1; s <= 1; s++) {
block(market.x+s*1.12,.46,market.front+1.143,.087,.81,.041,"#d49b63");
fishCrate(market.x+s*.95,.95,market.front+.84,.96,true);
}
block(market.x,2.995,market.front+.41,1.57,.64,.19,"#654d43");
block(market.x,2.995,market.front+.52,1.41,.50,.051,"#547e71");
icons.fish.forEach((row, iy) => {
for (let ix = 0; ix < row.length; ix++) if (row[ix] === "1") {
block(market.x+(ix-4)*.101,2.995+(4-iy)*.071,market.front+.554,.098,.068,.053,
iy < 4 ? "#f5dd9e" : "#e6c57f");
}
});
fishCrate(-3.25,.025,-1.78,.98,false);
fishCrate(-3.16,.31,-1.79,.86,true);
fishCrate(-8.23,.02,-1.68,1.03,false);
const stallX=-8.03, stallZ=-.27;
for (const sx of [-1,1]) for (const sz of [-1,1]) {
block(stallX+sx*1.13,.91,stallZ+sz*.61,.122,1.82,.127,"#986849");
}
block(stallX,.44,stallZ+.25,2.62,.73,.75,"#a87752");
block(stallX,.83,stallZ+.26,2.73,.13,.82,"#dbac76");
block(stallX,.41,stallZ+.64,2.39,.10,.045,"#dfac71");
awning(stallX,stallZ-.78,2.86,2.15,1.78,"#4c9e8e","#f7e8bc",8);
fishCrate(stallX-.77,.90,stallZ+.28,.86,true);
fishCrate(stallX,.90,stallZ+.25,.86,true);
fishCrate(stallX+.76,.90,stallZ+.27,.83,true);
const blueHouse = cottage({
x:-.56,z:-4.88,w:3.44,d:3.23,h:2.77,wall:"#f4e1b7",trim:"#fff0cb",
roof:["#587c94","#678da5","#85a6b6","#5a829d","#7398aa"],door:"#658a83"
});
const greenHouse = cottage({
x:5.28,z:-4.60,w:3.11,d:3.14,h:2.22,wall:"#eac499",trim:"#f9e0b4",
roof:["#5f8a65","#70996c","#90af7b","#71966a","#7ca477"],door:"#a86b54"
});
function lantern(x,z,height = 1.48) {
block(x,.085,z,.37,.17,.34,"#a69073");
block(x,height*.49,z,.12,height*.88,.127,"#725441");
block(x,height-.23,z,.29,.074,.30,"#5c5342");
block(x,height-.093,z,.205,.263,.205,"#f9c984",2);
for (const dx of [-1,1]) for (const dz of [-1,1]) {
block(x+dx*.102,height-.091,z+dz*.103,.038,.27,.038,"#745c42");
}
block(x,height+.067,z,.292,.082,.29,"#5b5341");
block(x,height+.135,z,.188,.081,.19,"#76644c");
lamps.push([x,height-.09,z]);
}
lantern(-6.96,.94);
lantern(-2.88,-.92);
lantern(2.18,.48);
lantern(8.74,-.27);
lantern(-.58,4.95,1.34);
lamps.push([market.x,1.1,market.front+.22],[blueHouse.x-.82,1.22,blueHouse.front+.12],[greenHouse.x-.72,1.15,greenHouse.front+.19]);
const bridgeZ = 2.64;
const bridgeMiddle = 5.69;
const bridgeDeck = x => .12+.58*Math.max(0,1-Math.abs(x-bridgeMiddle)/3.39);
for (let i = 0; i < 17; i++) {
const x=2.45+i*.408;
const y=bridgeDeck(x);
block(x,y-.077,bridgeZ,.414,.147,1.22,i%3 ? "#bd8455" : "#cb9563");
block(x,y-.013,bridgeZ,.337,.033,1.064,i%3 ? "#d2a36d" : "#e0af75");
for (const sign of [-1,1]) {
block(x,y-.13,bridgeZ+sign*.542,.417,.16,.14,"#9b6647");
block(x,y+.365,bridgeZ+sign*.626,.429,.078,.099,"#a9724f");
if (i%4 === 0 || i===16) {
block(x,y+.154,bridgeZ+sign*.626,.102,.516,.117,"#8d604a");
block(x,y+.420,bridgeZ+sign*.626,.147,.066,.146,"#e0a677");
}
}
}
function lily(x,z,s,flower) {
block(x,-.086,z,.44*s,.026,.34*s,"#5a9b72",3);
block(x-.045*s,-.083,z+.18*s,.31*s,.031,.13*s,"#6ca979",3);
if (flower) {
block(x,-.017,z,.19*s,.10,.17*s,"#e9a4aa");
block(x+.111*s,.028,z,.085*s,.081,.12*s,"#ffe2bf");
block(x-.094*s,.034,z+.05*s,.12*s,.085,.11*s,"#f4bfba");
block(x,.077,z,.11*s,.066,.105*s,"#f8ce7a");
}
}
lily(3.59,3.81,1,true);
lily(4.20,4.08,.77,false);
lily(6.82,4.59,1.08,true);
lily(7.47,4.10,.85,false);
lily(7.8,1.70,.99,true);
lily(4.50,1.59,.77,false);
function reeds(x,z) {
for (let i=0;i<5;i++) {
const height = .24+random()*.39;
const xx = x + random()*.42-.21, zz = z + random()*.30-.15;
block(xx,height/2-.035,zz,.035,height,.038,choose(["#6e9d64","#4f8665","#84ae6c"]));
if (i%2) block(xx,height-.045,zz,.078,.132,.072,"#9d704e");
}
}
reeds(3.12,4.47); reeds(8.60,3.61); reeds(7.18,5.42); reeds(5.45,5.38);
reeds(3.80,1.18); reeds(6.65,1.24);
for (let s=0;s<14;s++) {
const x = 5.15+random()*1.2, z=5.25+random()*.65;
if (lake(Math.round(x),Math.round(z))) continue;
block(x,.035,z,.22,.075,.18,choose(["#b7c0a3","#c1bd9f","#9ead93"]));
}
block(6,-.127,7.44,1.005,.14,.33,"#70ccce",3);
for (let section=0;section<9;section++) {
const y=-.46-section*.66;
const z=7.555+section*.037;
const width=1.015-section*.039;
block(6+.024*Math.sin(section),y,z,width,.70,.156,choose(["#51b2c1","#60c3cd","#58bdcb"],hash(section,2,1)),3);
block(5.65+section*.02,y,z+.085,.126,.69,.025,"#99dedd",3);
block(6.26-section*.012,y-.08,z+.093,.087,.48,.026,"#6dd1d6",3);
}
function bench(x,z,yaw=0) {
const c=Math.cos(yaw), s=Math.sin(yaw);
const piece=(u,y,v,w,h,d,color) => block(x+u*c+v*s,y,z+v*c-u*s,w,h,d,color,0,yaw);
for (const end of [-1,1]) {
piece(end*.59,.21,0,.115,.43,.42,"#7d5944");
piece(end*.61,.48,-.205,.087,.52,.095,"#805a43");
}
for (const v of [-.129,.015,.159]) piece(0,.415,v,1.54,.085,.126,"#c48c5c");
piece(0,.64,-.22,1.53,.132,.087,"#d09966");
}
bench(-5.6,1.12,-.04);
bench(-1.13,5.91,Math.PI);
bench(3.03,-1.01,Math.PI/2);
for (let i=0;i<10;i++) {
const x=-7.34+i*.63, z=6.29+(i<2 ? -.18 : 0);
block(x,.34,z,.131,.68,.148,"#f2d7a5");
block(x,.695,z,.090,.060,.09,"#f9e2b3");
if (i<9) {
block(x+.298,.233,z,.62,.087,.085,"#e3c492");
block(x+.299,.462,z,.62,.093,.085,"#ebce9d");
}
}
for (let i=0;i<5;i++) {
block(10.04,.285,-1.64+i*.60,.14,.57,.12,"#e6c78f");
if (i<4) block(10.04,.39,-1.35+i*.60,.095,.09,.63,"#ebd2a2");
}
function voxelShape(cx,cy,cz,unit,cells,color) {
const key=(i,j,k)=>i+","+j+","+k;
const present=new Set(cells.map(c=>key(...c)));
const neighbors=[[1,0,0],[-1,0,0],[0,1,0],[0,-1,0],[0,0,1],[0,0,-1]];
for (const [i,j,k] of cells) {
let mask=0;
neighbors.forEach(([a,b,c],side) => { if (!present.has(key(i+a,j+b,k+c))) mask |= 1<<side; });
block(cx+i*unit,cy+j*unit,cz+k*unit,unit,unit,unit,color(i,j,k),0,0,0,mask);
}
}
function yarn(x,z,radius,palette,angle) {
const step=radius/4.35;
const cells=[];
for (let j=-4;j<=4;j++) for (let k=-4;k<=4;k++) for (let i=-4;i<=4;i++) {
if (i*i+j*j+k*k <= 19.2) cells.push([i,j,k]);
}
voxelShape(x,step*4.5,z,step,cells,(i,j,k) => {
const winding = Math.floor((i*.79+j*.94+k*.68+Math.sin(k*.82)*.84)*1.2);
return palette[((winding%palette.length)+palette.length)%palette.length];
});
for (let i=0;i<22;i++) {
const dist=radius*.65+i*.070;
const a=angle+Math.sin(i*.25)*.29;
block(x+Math.cos(a)*dist,.041,z+Math.sin(a)*dist,.148,.067,.135,palette[i%2]);
}
}
yarn(-4.72,3.56,1.01,["#cd7190","#e88b9e","#f2a1a6","#df829b","#bd6886"],2.54);
yarn(-2.46,4.26,.80,["#9a83b5","#b29ac9","#c9add2","#a58aba"],-.35);
yarn(-5.00,5.26,.68,["#d6a14d","#edbb65","#f7cd77","#e2ae55"],.29);
const leafSets = [
["#559363","#6ca465","#7ab571","#8abe76","#97c67d"],
["#c78288","#da9696","#edaaa4","#f2b8a9","#f8c9b4"],
["#b29b54","#c9ac5c","#ddb963","#e6c773","#edd284"]
];
function tree(x,z,size,type,turn) {
block(x,.64*size,z,.31*size,1.28*size,.34*size,"#826247");
block(x+.072*size,1.49*size,z-.021,.25*size,1.23*size,.25*size,"#967253");
block(x-.27*size,1.51*size,z+.06*size,.52*size,.21*size,.20*size,"#977351",0,turn);
const cells=[];
for (let k=-3;k<=3;k++) for (let i=-3;i<=3;i++) for (let j=-2;j<=3;j++) {
const sphere=(i/3.28)**2+(k/2.85)**2+((j-.25)/2.7)**2;
if (sphere < .87+hash(i+j*7,k,turn*11+type)*.33) cells.push([i,j,k]);
}
const shades=leafSets[type];
voxelShape(x,2.62*size,z,.43*size,cells,(i,j,k) => {
const color=clamp(Math.floor((j+2)*.49+hash(i+j*7,k,13+type)*2.8),0,4);
return shades[color];
});
}
tree(-8.18,3.95,1.03,0,1);
tree(-8.55,-4.70,.81,0,2);
tree(2.29,-6.26,1.05,1,3);
tree(8.68,-3.41,.88,2,4);
tree(-2.66,-7.17,.73,0,5);
function tuft(x,z,flowers) {
const height=.14+random()*.13;
const color=choose(["#609e68","#6da666","#7bad63"]);
block(x,height/2,z,.055,height,.051,color);
block(x-.073,height*.31,z+.028,.047,height*.64,.044,color);
block(x+.073,height*.39,z-.026,.045,height*.79,.044,color);
if (flowers) {
const petal=choose(["#eac6bd","#e9a8aa","#f4d88d","#f4e2b2","#c6a7c0"]);
block(x,height+.025,z,.15,.052,.102,petal);
block(x,height+.026,z,.103,.056,.15,petal);
block(x,height+.06,z,.054,.041,.053,"#edb76e");
}
}
for (const {x,z,rim} of earth.values()) {
if (lake(x,z) || hash(x,z,49)<.60) continue;
const dx=(hash(x,z,52)-.5)*.64, dz=(hash(z,x,54)-.5)*.60;
if (pathDistance(x+dx,z+dz)<.83) continue;
if (rim<1.57 || (z>3 && x<-1) || (z<-.75 && x>6.4)) {
tuft(x+dx,z+dz,hash(x,z,23)>.38);
if (hash(x,z,59)>.54) tuft(x+dx-.20,z+dz+.27,hash(z,x,25)>.52);
}
}
for (const [x,z,n] of [[-6.71,6.49,4],[-2.02,6.54,5],[-8.56,2.03,4],[1.22,5.61,5],[7.28,-1.92,3],[1.68,-2.68,3]]) {
for (let i=0;i<n;i++) tuft(x+random()*.69-.33,z+random()*.36-.18,true);
}
for (const [x,z] of [[-8.95,5.12],[-7.06,5.39],[-7.25,-5.96],[1.73,6.28],[8.73,5.17]]) {
block(x,.109,z,.083,.221,.087,"#e7cda7");
block(x,.237,z,.228,.103,.26,"#bb7560");
block(x+.032,.296,z-.02,.072,.031,.07,"#f6d8aa");
}
for (const [x,z,depth] of [[-6,7.52,2.2],[-2,8.52,2.5],[3.67,8.52,2],[9.52,4.13,1.58],[10.53,1.62,1.62]]) {
for (let i=0;i<depth/.24;i++) {
const sway=Math.sin(i*.43)*.11;
block(x+sway,-.15-i*.23,z,.121,.25,.098,choose(["#608e65","#739d69","#7ca468"]));
if (i%2===0) block(x+sway+(i%4===0 ? .10 : -.10),-.18-i*.23,z+.024,.166,.115,.137,"#89ad6e");
}
}
block(-2.27,.24,-1.38,.12,.48,.12,"#956b4e");
block(-2.27,.52,-1.38,.42,.23,.13,"#bd9363");
block(-2.27,.52,-1.301,.26,.045,.036,"#eee0aa");
block(-2.28,.443,-1.302,.075,.11,.034,"#eee0aa");
const catspecs = [
{name:"Miso", mayor:true, fur:"#e89c54", light:"#ffe3b5", detail:"#bc6d3c", eyes:"#526451", yaw:.51,
path:[[-1.39,.49,"idle",1.8],[-3.41,.04],[-5.42,-.32,"shop",3.3],[-6.56,.74],[-5.64,1.53,"stretch",2.4],[-3.1,.92],[-1.18,.62,"idle",2.1]]},
{name:"Mochi", fur:"#a8aab1", light:"#f0e9cf", detail:"#797f91", eyes:"#536d61", yaw:2.26,
path:[[-7.52,.91,"shop",3.4],[-6.2,.84],[-6.2,2.15,"stretch",1.9],[-6.61,3.28],[-6.91,2.0],[-7.51,.87,"shop",2.8]]},
{name:"Tuna", fur:"#8eaaa4", light:"#e2ddbb", detail:"#557b7b", eyes:"#596451", yaw:1.6,
path:[[4.41,bridgeZ],[8.99,bridgeZ,"idle",1.1],[9.18,1.23],[8.43,-.43],[6.75,-.83,"stretch",2.1],[2.66,-.42],[1.0,1.37],[2.43,bridgeZ]]},
{name:"Bean", fur:"#555469", light:"#ede7d4", detail:"#414354", eyes:"#8c9e6c", yaw:.65,
path:[[.73,-1.26],[1.13,-.21],[.36,1.13],[.60,3.72,"idle",2.6],[-.24,4.22],[-.36,2.41],[-.87,.58],[-2.53,-.22,"shop",2.2]]},
{name:"Peach", fur:"#efc2a0", light:"#ffe5c8", detail:"#c98166", eyes:"#5d6670", yaw:.5, patch:true,
path:[[-3.23,5.76,"sleep",6.6],[-2.35,5.68],[-1.3,5.17,"stretch",2.0],[-1.04,4.07],[-1.38,2.81],[-2.99,2.03],[-3.22,3.26],[-3.58,5.53]]},
{name:"Nori", fur:"#6a595d", light:"#e2bc99", detail:"#514a58", eyes:"#d2bd76", yaw:1.4, chaser:true,
path:[[-3.4,1.84]]},
{name:"Kumo", fur:"#838fa9", light:"#e0e0d3", detail:"#626c89", eyes:"#657077", yaw:.52, roof:market,
path:[[-6.02,-3.84,"sleep",7.8],[-5.04,-3.84,"stretch",2.8],[-5.57,-3.84,"sleep",7.4]]},
{name:"Sesame", fur:"#dec0a0", light:"#f1d7b6", detail:"#906b58", eyes:"#6b7967", yaw:1.02, roof:greenHouse, patch:true,
path:[[4.62,-3.92,"sleep",4.7],[5.34,-3.92,"stretch",2.8],[4.49,-3.92,"sleep",8.1]]},
{name:"Yuzu", fur:"#f0d498", light:"#f9e5b7", detail:"#c6a16a", eyes:"#81906c", yaw:-.25,
path:[[-.74,-2.53,"idle",2.0],[-.20,-1.49],[1.88,-1.02],[3.97,-1.21],[5.77,-2.39,"stretch",2.3],[5.25,-1.32],[1.71,-.56],[-3.73,-.48,"shop",2.2]]},
{name:"Fig", fur:"#987c96", light:"#e6c6bb", detail:"#765d7e", eyes:"#889c78", yaw:-.82,
path:[[9.13,4.34,"stretch",2.3],[9.20,3.41],[8.98,bridgeZ],[2.63,bridgeZ],[1.63,2.59,"idle",1.8],[2.6,bridgeZ],[9.14,bridgeZ]]},
{name:"Biscuit", fur:"#d1a271", light:"#f7dfac", detail:"#ab7852", eyes:"#657862", yaw:.18,
path:[[-6.48,4.92],[-6.13,5.77],[-5.93,6.02,"sleep",4.9],[-6.35,5.11],[-6.52,3.07],[-5.65,2.55,"stretch",2.5],[-6.62,3.13]]},
{name:"Fern", fur:"#a4a18b", light:"#e0d7b8", detail:"#787e6e", eyes:"#7c9870", yaw:-1.1, patch:true,
path:[[6.63,-1.61],[4.99,-.70],[3.12,-.59,"idle",2.1],[1.25,-.19],[-.23,.64],[-1.22,2.31,"stretch",2.0],[-1.07,1.0],[.36,-.49],[5.71,-.77]]}
];
function floorOf(cat) {
if (cat.roof) return cat.roof.roofHeight(cat.z);
if (Math.abs(cat.z-bridgeZ)<.57 && cat.x>=2.40 && cat.x<=9.18) return bridgeDeck(cat.x)+.014;
return .024;
}
const cats = catspecs.map((spec,index) => {
const [x,z,pose="walk",wait=0]=spec.path[0];
const cat={...spec,index,x,z,pose,wait,next:1,age:0,gait:index*.39,scale:.92+hash(index,4,8)*.17,speed:1.02+hash(index,7,3)*.39};
cat.y=floorOf(cat);
return cat;
});
const mayor=cats[0];
const chaseCat=cats[5];
function portrait(canvas,cat) {
canvas.width=canvas.height=22;
const ctx=canvas.getContext("2d");
const draw=(x,y,w,h,color)=>{ctx.fillStyle=color;ctx.fillRect(x,y,w,h);};
draw(4,6,14,12,cat.fur);
draw(3,10,16,6,cat.fur);
draw(4,5,5,5,cat.fur);
draw(13,5,5,5,cat.fur);
draw(4,4,3,2,cat.fur);
draw(15,4,3,2,cat.fur);
draw(5,6,2,3,"#d48176");
draw(15,6,2,3,"#d48176");
draw(6,16,10,3,cat.light);
draw(8,14,6,4,cat.light);
draw(6,11,2,2,"#3d4245");
draw(14,11,2,2,"#3d4245");
draw(10,14,2,1,"#ac6a69");
draw(3,14,3,1,cat.light);
draw(16,14,3,1,cat.light);
if (cat.patch) draw(12,8,3,2,cat.detail);
if (cat.mayor) {
draw(5,6,12,2,"#364e42");
draw(8,2,7,4,"#425a4c");
draw(8,5,7,1,"#ddb66b");
draw(9,2,4,1,"#63806a");
}
}
portrait(byId("seal"),mayor);
portrait(byId("portrait"),mayor);
function butterflyPath(index,t) {
if (index===0) return [-3.21+Math.cos(t*1.59)*1.36, .87+Math.sin(t*2.37)*.26, 1.84+Math.sin(t*1.59)*.65];
if (index===1) return [-6.12+Math.cos(t*.87)*.98, 1.13+Math.sin(t*1.96+.6)*.32,3.71+Math.sin(t*.87)*1.06];
return [2.45+Math.cos(t*.68)*.87, .73+Math.sin(t*2.15)*.24, 4.19+Math.sin(t*.68)*.95];
}
function setPose(cat,pose) {
if (cat.pose!==pose) { cat.pose=pose; cat.age=0; }
}
function turnCat(cat,angle,dt) {
cat.yaw+=Math.atan2(Math.sin(angle-cat.yaw),Math.cos(angle-cat.yaw))*Math.min(1,dt*8.5);
}
function goTo(cat,x,z,speed,dt) {
const dx=x-cat.x,dz=z-cat.z;
const distance=Math.hypot(dx,dz);
if (distance<.024) return true;
const step=Math.min(distance,speed*dt);
turnCat(cat,Math.atan2(dx,dz),dt);
cat.x+=dx/distance*step;
cat.z+=dz/distance*step;
cat.gait+=step*(cat.pose==="chase" ? 9 : 11);
return step===distance;
}
function moveCats(dt,t) {
for (const cat of cats) {
cat.age+=dt;
if (cat.chaser) {
const chaseTime=(t+1.2)%10.6;
if (chaseTime<7.3) {
const goal=butterflyPath(0,t-.31);
setPose(cat,"chase");
goTo(cat,goal[0],goal[2],2.6,dt);
} else {
setPose(cat,chaseTime<9.4 ? "stretch" : "idle");
turnCat(cat,.48,dt*.47);
}
} else if (cat.wait>0) {
cat.wait-=dt;
if (cat.pose==="shop" && cat.age>.8) turnCat(cat,cat.x<-6.6 ? -2.7 : Math.PI+.13,dt*.6);
if (cat.pose==="sleep" || cat.pose==="stretch") turnCat(cat,.53+Math.sin(cat.index)*.32,dt*.46);
} else {
const [x,z,pose,wait]=cat.path[cat.next];
setPose(cat,"walk");
if (goTo(cat,x,z,cat.speed*(cat.roof ? .6 : 1),dt)) {
cat.next=(cat.next+1)%cat.path.length;
cat.wait=wait || 0;
if (pose) setPose(cat,pose);
}
}
cat.y=mix(cat.y,floorOf(cat),Math.min(1,dt*11));
}
}
function catModel(list,cat,t) {
const {fur,light,detail,eyes}=cat;
const moving=cat.pose==="walk"||cat.pose==="chase";
const snooze=cat.pose==="sleep";
const stretching=cat.pose==="stretch";
const stretch=stretching ? Math.sin(Math.PI*clamp(cat.age/2.4,0,1)) : 0;
const breath=snooze ? Math.sin(t*2+cat.index)*.026 : 0;
const bounce=moving ? Math.abs(Math.sin(cat.gait))*.059 : breath;
const c=Math.cos(cat.yaw),s=Math.sin(cat.yaw),size=cat.scale;
const add=(u,y,v,w,h,d,color,turn=0,pitch=0,kind=0) => box(list,
cat.x+(u*c+v*s)*size,cat.y+y*size,cat.z+(v*c-u*s)*size,w*size,h*size,d*size,color,kind,cat.yaw+turn,pitch
);
if (snooze) {
add(-.022,.267+bounce,-.133,.67,.39,.916,fur);
add(-.061,.371+bounce,-.25,.57,.257,.59,fur);
add(-.231,.242,-.028,.183,.245,.43,detail);
add(.222,.151,.27,.204,.163,.304,light);
add(-.20,.137,.262,.207,.148,.27,light);
for (let i=0;i<6;i++) {
const a=-2.1+i*.56;
add(.30+Math.cos(a)*.223,.123+Math.sin(i*.43)*.05,.028+Math.sin(a)*.41,
.154,.144,.199,i===5 ? light : i%2===0 ? detail : fur,a);
}
} else {
add(0,.462+bounce+stretch*.054,-.119,.576,.415,.871,fur,0,-stretch*.21);
add(0,.551+bounce,-.147,.50,.26,.75,fur,0,-stretch*.19);
add(0,.402+bounce-stretch*.135,.267+stretch*.16,.432,.394,.303,light,0,-stretch*.18);
for (const side of [-1,1]) for (const end of [-1,1]) {
const phase=cat.gait+(side===end ? 0 : Math.PI);
const length=moving ? (cat.pose==="chase" ? .225 : .146) : 0;
const slide=Math.sin(phase)*length+(end>0 ? stretch*.36 : -stretch*.08);
const lift=moving ? Math.max(0,Math.cos(phase))*.116 : 0;
const z=end===1 ? .182 : -.382;
add(side*.192,.214+lift,z+slide,.172,.312,.192,fur,0,end*stretch*.15);
add(side*.192,.083+lift,z+slide+.017,.178,.134,.219,light);
}
let tx=.054,tz=-.55;
for (let i=0;i<5;i++) {
tx+=Math.sin(t*3.2+i*.42+cat.index)*.054;
tz-=.107;
add(tx,.43+i*.079+bounce+stretch*.043,tz,.13,.143,.184,
i===4 ? detail : (i%2===0 ? fur : detail),0,-.48);
}
}
if (!snooze) {
for (let i=0;i<3;i++) {
if (cat.patch && i===1) continue;
add((i%2 ? .17 : -.06),.674+bounce,-.41+i*.154,.22,.028,.070,detail);
}
}
const hx=snooze ? .16 : 0;
const hy=snooze ? .338+bounce : .699+bounce-stretch*.245+Math.sin(t*3+cat.index)*.017;
const hz=snooze ? .306 : .410+stretch*.21;
add(hx,hy,hz,.589,.476,.515,fur);
add(hx,hy-.139,hz+.041,.499,.188,.493,light);
add(hx,hy+.231,hz-.035,.444,.072,.369,fur);
for (const side of [-1,1]) {
add(hx+side*.221,hy+.251,hz-.046,.165,.212,.170,detail);
add(hx+side*.238,hy+.38,hz-.058,.134,.091,.137,fur);
add(hx+side*.22,hy+.262,hz+.05,.090,.129,.038,"#d58b81");
add(hx+side*.126,hy-.101,hz+.267,.162,.104,.070,light);
const blink=snooze || (t+cat.index*.47)%6.1<.12;
add(hx+side*.159,hy+.022,hz+.265,.108,blink ? .031 : .109,.024,blink ? detail : "#435050");
if (!blink) {
add(hx+side*.154,hy+.029,hz+.279,.057,.072,.014,eyes);
add(hx+side*.151,hy+.052,hz+.288,.026,.024,.011,"#fdf0cf");
}
add(hx+side*.239,hy-.064,hz+.26,.076,.038,.033,cat.patch ? detail : "#d9957d");
}
if (cat.patch) add(hx+.201,hy+.144,hz+.253,.155,.112,.024,detail);
add(hx,hy-.07,hz+.304,.089,.063,.044,"#ad6e68");
add(hx,hy-.118,hz+.304,.036,.050,.029,"#92645e");
if (cat.mayor) {
add(hx,hy+.290,hz-.044,.549,.066,.433,"#344941",.08);
add(hx-.023,hy+.405,hz-.054,.311,.221,.283,"#43584b",.08);
add(hx-.023,hy+.334,hz-.054,.324,.048,.297,"#e5b664",.08);
add(hx-.085,hy+.521,hz-.062,.173,.027,.193,"#64806a",.08);
add(0,.475+bounce-stretch*.13,.337+stretch*.146,.388,.087,.23,"#ba625b");
add(0,.430+bounce-stretch*.13,.456+stretch*.14,.073,.081,.044,"#f2c470");
}
if (snooze) {
for (let i=0;i<2;i++) {
const phase=(t*.64+i*.61+cat.index*.29)%1.38;
const sz=.058+phase*.024;
const x=.31+phase*.12,y=.93+phase*.44,z=.29;
add(x,y+sz,z,sz*2.25,sz*.42,.029,"#e7eed5",0,0,5);
add(x,y-sz,z,sz*2.25,sz*.42,.029,"#e7eed5",0,0,5);
add(x,y,z,sz*.39,sz*2.12,.030,"#e7eed5",0,-.65,5);
}
}
}
const cloudSpecs = [
[-13.3,-4.5,-4.1,1.22,.27],
[-11.5,-5.9,4.1,1.19,.29],
[-7.8,-7.0,9.0,.86,.21],
[.60,-8.7,8.9,1.03,.32],
[9.2,-8.14,5.17,1.08,.29],
[14.7,-5.78,3.1,1.18,.28],
[13.35,-4.6,-5.5,.96,.17],
[4.76,-6.45,-11.42,.72,.18],
[-8.20,-6.48,-11.1,.83,.20],
[-18.10,-5.58,1.40,.64,.24],
[19.5,-7.5,9.2,.68,.23]
];
function cloudModel(list,x,y,z,s,index) {
const shade = ["#e0e7d9","#eef1dd","#f6f3df","#f8f4e2"];
const pieces = [
[0,-.11,0,3.00,.49,1.39,0],[-.07,.16,.008,4.05,.47,1.63,1],
[-1.87,.255,-.11,.94,.37,1.09,1],[1.80,.22,.09,1.20,.39,1.14,1],
[-.80,.47,-.04,1.90,.59,1.42,2],[.43,.46,.11,1.59,.54,1.39,2],
[-.57,.86,-.16,1.21,.28,.94,3],[.91,.56,.01,.88,.37,1.11,2],
[.09,.13,.78,2.40,.24,.43,1]
];
for (const [u,v,w,a,b,c,p] of pieces) box(list,x+u*s,y+v*s,z+w*s,a*s,b*s,c*s,shade[p],4,(index%3-1)*.09);
}
function animatedScenery(list,t) {
cloudSpecs.forEach(([x,y,z,scale,speed], index) => {
const shift=(x+23+t*speed)%46-23;
cloudModel(list,shift,y+Math.sin(t*.39+index)*.16,z,scale,index);
});
for (let i=0;i<25;i++) {
const phase=(t*(.83+hash(i,8,4)*.33)+i*.378)%1;
const y=-.21-phase*8.21;
const x=6+(hash(i,8,5)-.5)*(.81+phase*.23);
const z=7.662+phase*.39+Math.max(0,phase-.72)*hash(i,2,4);
const long=phase<.77 ? .26+hash(i,2,6)*.26 : .075;
box(list,x,y,z,.035+hash(i,2,4)*.081,long,.055,choose(["#a1e0df","#79cfd5","#63baca"],hash(i,2,7)),3);
}
for (let i=0;i<10;i++) {
const p=(t*.54+i*.173)%1;
const a=i*2.399;
box(list,6+Math.cos(a)*p*1.07,-6.50-p*1.50,7.99+Math.sin(a)*p*.51,.071+p*.123,.059+p*.048,.077+p*.08,"#c5e6de",3);
}
const ripples=[[3.75,2.02],[6.59,3.81],[5.06,4.76],[6.17,5.73],[7.35,1.46],[8.03,3.40],[4.09,3.54]];
ripples.forEach(([x,z],i) => {
const shine=.13+(.5+.5*Math.sin(t*2.1+i*2.4))*.28;
box(list,x+Math.sin(t*.65+i)*.12,-.088,z,shine,.013,.046,i%2 ? "#93ddcf" : "#b1e4d7",3);
});
const colors=[["#e6a351","#f7cd78"],["#bc91c0","#e4b5cb"],["#e6a9a1","#fae1aa"]];
for (let i=0;i<3;i++) {
const [x,y,z]=butterflyPath(i,t);
const flap=.022+.087*Math.abs(Math.cos(t*17+i));
const turn=t*.84+i;
box(list,x,y,z,.043,.052,.132,"#775e63",0,turn);
for (const side of [-1,1]) {
box(list,x+side*flap*.88,y+.019,z,flap*1.4,.045,.13,colors[i][0],0,turn+side*.15);
box(list,x+side*flap*1.58,y+.043,z-.016,flap*.72,.042,.092,colors[i][1],0,turn+side*.28);
}
}
}
const corners = [
[-.5,-.5,-.5],[.5,-.5,-.5],[.5,.5,-.5],[-.5,.5,-.5],
[-.5,-.5,.5],[.5,-.5,.5],[.5,.5,.5],[-.5,.5,.5]
];
const sides = [
{n:[1,0,0],c:[1,2,6,5]}, {n:[-1,0,0],c:[4,7,3,0]},
{n:[0,1,0],c:[7,6,2,3]}, {n:[0,-1,0],c:[0,1,5,4]},
{n:[0,0,1],c:[4,5,6,7]}, {n:[0,0,-1],c:[1,0,3,2]}
];
function hardware(canvas) {
const gl=canvas.getContext("webgl2",{alpha:true,premultipliedAlpha:false,antialias:false,depth:true});
if (!gl) return null;
function shader(type,source) {
const compiled=gl.createShader(type);
gl.shaderSource(compiled,source.trim());
gl.compileShader(compiled);
if (!gl.getShaderParameter(compiled,gl.COMPILE_STATUS)) throw new Error(gl.getShaderInfoLog(compiled));
return compiled;
}
const vert=shader(gl.VERTEX_SHADER,byId("vertex-source").textContent);
function program(id) {
const frag=shader(gl.FRAGMENT_SHADER,byId(id).textContent);
const p=gl.createProgram();
gl.attachShader(p,vert);
gl.attachShader(p,frag);
gl.linkProgram(p);
if (!gl.getProgramParameter(p,gl.LINK_STATUS)) throw new Error(gl.getProgramInfoLog(p));
gl.deleteShader(frag);
const uniforms={};
for (const name of ["uRight","uUp","uBack","uTarget","uShadowRight","uShadowUp","uLight","uScale","uShadowPass","uTone","uShadow","uShadowReady","uLamps[0]"]) {
uniforms[name]=gl.getUniformLocation(p,name);
}
return {p,uniforms};
}
const shading=program("color-source");
const shadowing=program("shadow-source");
const vertices=[];
for (const side of sides) for (const i of [0,1,2,0,2,3]) vertices.push(...corners[side.c[i]],...side.n);
const cube=gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER,cube);
gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(vertices),gl.STATIC_DRAW);
function batch() {
const vao=gl.createVertexArray(),buffer=gl.createBuffer();
gl.bindVertexArray(vao);
gl.bindBuffer(gl.ARRAY_BUFFER,cube);
for (let i=0;i<2;i++) {
gl.enableVertexAttribArray(i);
gl.vertexAttribPointer(i,3,gl.FLOAT,false,24,i*12);
}
gl.bindBuffer(gl.ARRAY_BUFFER,buffer);
[[3,0],[3,3],[4,6],[4,10],[1,14]].forEach(([size,offset],i) => {
const location=i+2;
gl.enableVertexAttribArray(location);
gl.vertexAttribPointer(location,size,gl.FLOAT,false,stride*4,offset*4);
gl.vertexAttribDivisor(location,1);
});
gl.bindVertexArray(null);
return {vao,buffer};
}
const still=batch(), moving=batch();
gl.bindBuffer(gl.ARRAY_BUFFER,still.buffer);
gl.bufferData(gl.ARRAY_BUFFER,pack(fixed),gl.STATIC_DRAW);
const resolution=768;
const target=gl.createFramebuffer();
const texture=gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D,texture);
gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA8,resolution,resolution,0,gl.RGBA,gl.UNSIGNED_BYTE,null);
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER,gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_S,gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_T,gl.CLAMP_TO_EDGE);
const depth=gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER,depth);
gl.renderbufferStorage(gl.RENDERBUFFER,gl.DEPTH_COMPONENT16,resolution,resolution);
gl.bindFramebuffer(gl.FRAMEBUFFER,target);
gl.framebufferTexture2D(gl.FRAMEBUFFER,gl.COLOR_ATTACHMENT0,gl.TEXTURE_2D,texture,0);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER,gl.DEPTH_ATTACHMENT,gl.RENDERBUFFER,depth);
const shadowReady=gl.checkFramebufferStatus(gl.FRAMEBUFFER)===gl.FRAMEBUFFER_COMPLETE;
gl.bindFramebuffer(gl.FRAMEBUFFER,null);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
gl.enable(gl.CULL_FACE);
let shadowLight=null;
let shadowRight,shadowUp;
function draw(vao,count) {
gl.bindVertexArray(vao);
gl.drawArraysInstanced(gl.TRIANGLES,0,36,count);
}
return {
resize(w,h) {
const ratio=Math.min(1,1360/w,960/h);
canvas.width=Math.max(1,Math.round(w*ratio));
canvas.height=Math.max(1,Math.round(h*ratio));
},
render(camera,tone,light,actors) {
if (!shadowLight || dot(shadowLight,light)<.9995) {
shadowLight=light;
shadowRight=normalize(cross([0,1,0],shadowLight));
shadowUp=cross(shadowLight,shadowRight);
if (shadowReady) {
gl.bindFramebuffer(gl.FRAMEBUFFER,target);
gl.viewport(0,0,resolution,resolution);
gl.clearColor(1,1,0,1);
gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT);
gl.useProgram(shadowing.p);
const u=shadowing.uniforms;
gl.uniform1i(u.uShadowPass,1);
gl.uniform3fv(u.uShadowRight,shadowRight);
gl.uniform3fv(u.uShadowUp,shadowUp);
gl.uniform3fv(u.uLight,shadowLight);
draw(still.vao,fixed.length);
gl.bindFramebuffer(gl.FRAMEBUFFER,null);
}
}
gl.viewport(0,0,canvas.width,canvas.height);
gl.clearColor(0,0,0,0);
gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT);
gl.useProgram(shading.p);
const u=shading.uniforms;
gl.uniform1i(u.uShadowPass,0);
gl.uniform3fv(u.uRight,camera.right);
gl.uniform3fv(u.uUp,camera.up);
gl.uniform3fv(u.uBack,camera.back);
gl.uniform3fv(u.uTarget,camera.target);
gl.uniform2f(u.uScale,2*camera.scale/camera.width,2*camera.scale/camera.height);
gl.uniform3fv(u.uShadowRight,shadowRight);
gl.uniform3fv(u.uShadowUp,shadowUp);
gl.uniform3fv(u.uLight,shadowLight);
gl.uniform2fv(u.uTone,tone);
gl.uniform1f(u.uShadowReady,shadowReady ? 1 : 0);
gl.uniform3fv(u["uLamps[0]"],lamps.flat());
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D,texture);
gl.uniform1i(u.uShadow,0);
gl.bindBuffer(gl.ARRAY_BUFFER,moving.buffer);
gl.bufferData(gl.ARRAY_BUFFER,pack(actors),gl.DYNAMIC_DRAW);
draw(still.vao,fixed.length);
draw(moving.vao,actors.length);
}
};
}
function software(canvas) {
const ctx=canvas.getContext("2d");
let w=1,h=1,image,pixels,depth,last=-Infinity;
const littleEndian = new Uint8Array(new Uint32Array([1]).buffer)[0]===1;
const paint=(r,g,b) => littleEndian ? (255<<24)|(b<<16)|(g<<8)|r : (r<<24)|(g<<16)|(b<<8)|255;
function triangle(a,b,c,color) {
const left=Math.max(0,Math.floor(Math.min(a[0],b[0],c[0])));
const right=Math.min(w-1,Math.ceil(Math.max(a[0],b[0],c[0])));
const top=Math.max(0,Math.floor(Math.min(a[1],b[1],c[1])));
const bottom=Math.min(h-1,Math.ceil(Math.max(a[1],b[1],c[1])));
if (left>right||top>bottom) return;
const ex=b[0]-a[0],ey=b[1]-a[1],fx=c[0]-a[0],fy=c[1]-a[1];
const full=ex*fy-ey*fx;
if (Math.abs(full)<.0001) return;
const stepU=fy/full,stepV=-ey/full,downU=-fx/full,downV=ex/full;
let startU=(left+.5-a[0])*stepU+(top+.5-a[1])*downU;
let startV=(left+.5-a[0])*stepV+(top+.5-a[1])*downV;
const bz=b[2]-a[2],cz=c[2]-a[2];
for (let y=top;y<=bottom;y++) {
let u=startU,v=startV,k=y*w+left;
for (let x=left;x<=right;x++,k++) {
if (u>=-.00001 && v>=-.00001 && u+v<=1.00001) {
const d=a[2]+u*bz+v*cz;
if (d>=depth[k]) { depth[k]=d; pixels[k]=color; }
}
u+=stepU; v+=stepV;
}
startU+=downU; startV+=downV;
}
}
function rotate(x,y,z,c,s,p,q) {
const yy=y*p-z*q,zz=y*q+z*p;
return [x*c+zz*s,yy,zz*c-x*s];
}
function draw(scene,camera,tone,light) {
const sx=w/camera.width,sy=h/camera.height;
const [warm,night]=tone;
for (const item of scene) {
const [x,y,z,ww,hh,dd,r,g,b,kind,c,s,p,q,faces]=item;
const center=[x-camera.target[0],y-camera.target[1],z-camera.target[2]];
const size=camera.scale;
const ox=w/2+dot(center,camera.right)*size*sx;
const oy=h/2-dot(center,camera.up)*size*sy;
const radius=(ww+hh+dd)*size*.51;
if (ox+radius*sx<0 || ox-radius*sx>w || oy+radius*sy<0 || oy-radius*sy>h) continue;
const oz=dot(center,camera.back);
let points=null;
for (let f=0;f<6;f++) {
if (!(faces&(1<<f))) continue;
const side=sides[f];
const n=rotate(...side.n,c,s,p,q);
if (dot(n,camera.back)<=0) continue;
if (!points) points=corners.map(([i,j,k]) => {
const v=rotate(i*ww,j*hh,k*dd,c,s,p,q);
return [ox+dot(v,camera.right)*size*sx,oy-dot(v,camera.up)*size*sy,oz+dot(v,camera.back)];
});
const ray=Math.max(0,dot(n,light)),high=Math.max(0,n[1]);
const lit=.69+.34*ray+.103*high;
let rr=mix(mix(r*lit+.02,r*lit*1.10+.032,warm),r*.41*(.80+.24*high)+.03,night);
let gg=mix(mix(g*lit+.026,g*lit*.88+.03,warm),g*.51*(.80+.24*high)+.05,night);
let bb=mix(mix(b*lit+.02,b*lit*.84+.023,warm),b*.73*(.80+.24*high)+.077,night);
if (kind===4) {
rr=mix(r*(.88+.12*high),r*.35+.034,night);
gg=mix(g*(.88+.12*high)*(1-warm*.20),g*.39+.040,night);
bb=mix(b*(.88+.12*high)*(1-warm*.15),b*.56+.065,night);
} else if (night>.01) {
let glow=0;
for (const lp of lamps) glow+=Math.max(0,1-Math.hypot(x-lp[0],y-lp[1],z-lp[2])/2.3)**2;
rr+=night*glow*.28; gg+=night*glow*.16; bb+=night*glow*.034;
}
if (kind===1 || kind===2) {
const emission=kind===2 ? .16+.84*night : night;
rr=mix(rr,1,emission); gg=mix(gg,.74,emission); bb=mix(bb,.30,emission);
}
if (kind===5) { rr=mix(rr,.81,.62); gg=mix(gg,.89,.62); bb=mix(bb,.83,.62); }
const color=paint(Math.round(clamp(rr,0,1)*255),Math.round(clamp(gg,0,1)*255),Math.round(clamp(bb,0,1)*255));
const [a,b1,c1,d]=side.c.map(i=>points[i]);
triangle(a,b1,c1,color);
triangle(a,c1,d,color);
}
}
}
return {
resize(width,height) {
const scale=Math.min(.94,840/width,650/height);
w=canvas.width=Math.max(1,Math.round(width*scale));
h=canvas.height=Math.max(1,Math.round(height*scale));
image=ctx.createImageData(w,h);
pixels=new Uint32Array(image.data.buffer);
depth=new Float32Array(w*h);
last=-Infinity;
},
render(camera,tone,light,actors,t) {
if (t-last<.03) return;
last=t;
pixels.fill(0);
depth.fill(-1000);
draw(fixed,camera,tone,light);
draw(actors,camera,tone,light);
ctx.putImageData(image,0,0);
}
};
}
const worldCanvas=byId("world");
const softCanvas=byId("software");
let renderer;
try { renderer=hardware(worldCanvas); } catch { renderer=null; }
function useSoftware() {
worldCanvas.hidden=true;
softCanvas.hidden=false;
renderer=software(softCanvas);
}
if (!renderer) useSoftware();
worldCanvas.addEventListener("webglcontextlost",event=>{
event.preventDefault();
useSoftware();
resize();
});
const skyCanvas=byId("sky-art");
const sky=skyCanvas.getContext("2d");
const skyStars=Array.from({length:72},(_,i)=>[hash(i,11,2),.035+hash(i,12,4)*.73,hash(i,17,4),hash(i,4,19)]);
let width=1,height=1;
function pixelOrb(x,y,r,cell,color,cut=0) {
sky.fillStyle=color;
const n=Math.floor(r/cell);
for (let iy=-n;iy<=n;iy++) for (let ix=-n;ix<=n;ix++) {
if (ix*ix+iy*iy>n*n) continue;
if (cut && (ix-n*.44)**2+(iy+n*.29)**2<(n*.84)**2) continue;
sky.fillRect(Math.round(x)+ix*cell,Math.round(y)+iy*cell,cell,cell);
}
}
function drawSky(t,warm,night) {
sky.clearRect(0,0,width,height);
if (night<.998) {
const size=clamp(width*.031,18,55);
const x=mix(width*.847,width*.806,warm);
const y=mix(height*.222,height*.452,warm);
const halo=sky.createRadialGradient(x,y,0,x,y,size*3.0);
halo.addColorStop(0,"rgba(255,239,177,.53)");
halo.addColorStop(.42,"rgba(255,239,177,.20)");
halo.addColorStop(1,"rgba(255,239,177,0)");
sky.globalAlpha=1-night;
sky.fillStyle=halo;
sky.fillRect(x-size*3,y-size*3,size*6,size*6);
pixelOrb(x,y,size*(1+warm*.12),size>34 ? 5 : 3,warm>.55 ? "#ffdc9b" : "#fff0bb");
for (const [u,v,k,l] of skyStars.slice(0,22)) {
sky.globalAlpha=(1-night)*(.31+k*.31);
const xx=Math.round(u*width),yy=Math.round((v+.08)*height);
sky.fillStyle="#fff9e1";
const p=l>.73 ? 3 : 2;
sky.fillRect(xx,yy,p,p);
if (l>.81) {
sky.fillRect(xx-p,yy,p*3,p);
sky.fillRect(xx,yy-p,p,p*3);
}
}
}
if (night>.005) {
const x=width*.845,y=height*.20,r=clamp(width*.027,19,43);
sky.globalAlpha=night;
const halo=sky.createRadialGradient(x,y,0,x,y,r*3.75);
halo.addColorStop(0,"rgba(201,231,218,.24)");
halo.addColorStop(1,"rgba(201,231,218,0)");
sky.fillStyle=halo;
sky.fillRect(x-r*4,y-r*4,r*8,r*8);
pixelOrb(x,y,r,r>29 ? 4 : 3,"#fff0c0",1);
for (const [u,v,k,l] of skyStars) {
sky.globalAlpha=night*(.35+k*.41+.12*Math.sin(t*(1.1+l*2.5)+u*29));
const xx=Math.round(u*width),yy=Math.round(v*height);
const p=l>.84 ? 3 : 2;
sky.fillStyle=l>.61 ? "#fff3c5" : "#c9e4e1";
sky.fillRect(xx,yy,p,p);
if (l>.93) {
sky.fillRect(xx-p,yy,p*3,p);
sky.fillRect(xx,yy-p,p,p*3);
}
}
}
sky.globalAlpha=1;
}
const defaultYaw=.57, defaultTilt=.64;
let yaw=defaultYaw,tilt=defaultTilt,zoom=1;
let wantYaw=yaw,wantTilt=tilt,wantZoom=zoom;
let target=[0,-1.1,0];
let camera=null;
let look="day",warm=0,night=0;
let follow=null;
const marker=byId("cat-marker");
const modeButtons=Array.from(document.querySelectorAll("[data-look]"));
const followButton=byId("unfollow");
const placeLabels=[
{element:byId("market-label"),world:[-6.28,4.40,-3.43]},
{element:byId("park-label"),world:[-6.02,1.42,5.0]},
{element:byId("pond-label"),world:[9.39,.95,4.80]}
];
function resize() {
width=Math.max(1,app.clientWidth);
height=Math.max(1,app.clientHeight);
skyCanvas.width=Math.round(width);
skyCanvas.height=Math.round(height);
renderer.resize(width,height);
}
window.addEventListener("resize",resize);
if ("ResizeObserver" in window) new ResizeObserver(resize).observe(app);
resize();
function setLook(next) {
if (next===look) return;
look=next;
app.dataset.time=next;
for (const button of modeButtons) {
const selected=button.dataset.look===next;
button.classList.toggle("is-on",selected);
button.setAttribute("aria-pressed",String(selected));
}
}
const descriptions={walk:"Wandering the island",idle:"Taking in the view",shop:"Shopping for fish",sleep:"Curled up for a nap",stretch:"Having a big stretch",chase:"Chasing butterflies"};
function residentStatus() {
const element=byId("resident-status");
const message=follow ? descriptions[follow.pose] : "Tap a cat to follow";
if (element.textContent!==message) element.textContent=message;
}
function setFollow(cat) {
if (follow===cat) return;
follow=cat;
const name=cat ? (cat.mayor ? "Mayor " : "")+cat.name : "12 residents";
byId("resident-name").textContent=name;
marker.textContent=cat ? name : "";
followButton.hidden=!cat;
portrait(byId("portrait"),cat || mayor);
residentStatus();
}
function resetView() {
setFollow(null);
const nearestTurn=Math.round((wantYaw-defaultYaw)/TAU);
wantYaw=defaultYaw+nearestTurn*TAU;
wantTilt=defaultTilt;
wantZoom=1;
}
function changeZoom(multiplier) {
wantZoom=clamp(wantZoom*multiplier,.64,2.54);
}
function makeCamera(dt) {
const step=1-Math.exp(-dt*11);
yaw=mix(yaw,wantYaw,step);
tilt=mix(tilt,wantTilt,step);
zoom=mix(zoom,wantZoom,step);
const sy=Math.sin(yaw),cy=Math.cos(yaw),se=Math.sin(tilt),ce=Math.cos(tilt);
const right=[cy,0,-sy],up=[-sy*se,ce,-cy*se],back=[sy*ce,se,cy*ce];
const tight=width<570;
const base=Math.min((width-(tight ? 5 : 52))/23.0,(height-(height<485 ? 72 : 132))/17.15);
const scale=Math.max(1,base*zoom);
const focus=follow ? [follow.x*.87,follow.y+.41,follow.z*.87] : [0,-1.10,0];
target=target.map((v,i)=>mix(v,focus[i],1-Math.exp(-dt*(follow ? 3.7 : 5.2))));
const raise=(height*(tight ? -.003 : .025))/scale;
const anchor=target.map((v,i)=>v+up[i]*raise);
return {width,height,scale,right,up,back,target:anchor};
}
function project(x,y,z) {
const p=[x-camera.target[0],y-camera.target[1],z-camera.target[2]];
return [width/2+dot(p,camera.right)*camera.scale,height/2-dot(p,camera.up)*camera.scale,dot(p,camera.back)];
}
function covered(x,y,z) {
const start=[x,y,z];
for (const bounds of occluders) {
let enter=.19,exit=70;
for (let i=0;i<3 && enter<=exit;i++) {
const direction=camera.back[i];
if (Math.abs(direction)<.00001) {
if (start[i]<bounds[i] || start[i]>bounds[i+3]) exit=-1;
} else {
const a=(bounds[i]-start[i])/direction,b=(bounds[i+3]-start[i])/direction;
enter=Math.max(enter,Math.min(a,b));
exit=Math.min(exit,Math.max(a,b));
}
}
if (enter<=exit) return true;
}
return false;
}
function anchorLabel(label,point,show) {
const [x,y]=project(...point);
const visible=show && x>18 && x<width-18 && y>43 && y<height-83;
label.classList.toggle("visible",visible);
if (visible) label.style.transform="translate("+Math.round(x)+"px,"+Math.round(y)+"px) translate(-50%,-100%)";
}
function refreshLabels() {
const relative=yaw-defaultYaw;
const overview=!follow&&zoom<1.23&&Math.cos(relative)>.77;
for (const {element,world} of placeLabels) anchorLabel(element,world,overview);
if (follow) {
const h=follow.pose==="sleep" ? 1.11 : follow.mayor ? 1.54 : 1.32;
anchorLabel(marker,[follow.x,follow.y+h*follow.scale,follow.z],!covered(follow.x,follow.y+.71,follow.z));
residentStatus();
} else marker.classList.remove("visible");
}
function selectAt(x,y,touch) {
if (!camera) return;
const bounds=stage.getBoundingClientRect();
x-=bounds.left; y-=bounds.top;
let candidate=null,best=Infinity;
for (const cat of cats) {
const [cx,cy,depth]=project(cat.x,cat.y+(cat.pose==="sleep" ? .37 : .59)*cat.scale,cat.z+.04);
const radius=Math.max(touch ? 21 : 13, camera.scale*cat.scale*.65);
const distance=Math.hypot(x-cx,y-cy);
const score=distance/radius-depth*.006;
if (distance<radius && score<best && !covered(cat.x,cat.y+.56,cat.z)) {
candidate=cat; best=score;
}
}
if (candidate) setFollow(candidate);
else if (follow) setFollow(null);
}
let demo=location.hash.toLowerCase()==="#demo";
const tour=byId("tour");
tour.hidden=!demo;
let tourPart=-1;
function stopTour(event) {
if (demo && (!event || event.isTrusted)) {
demo=false;
tour.hidden=true;
}
}
app.addEventListener("pointerdown",stopTour,true);
app.addEventListener("touchstart",stopTour,{passive:true,capture:true});
window.addEventListener("hashchange",()=>{
demo=location.hash.toLowerCase()==="#demo";
tour.hidden=!demo;
tourPart=-1;
demoStart=null;
});
function runTour(t) {
const beat=t%20;
const part=beat<3.8 ? 0 : beat<8 ? 1 : beat<12.1 ? 2 : beat<17.1 ? 3 : 4;
if (part!==tourPart) {
tourPart=part;
setLook(part===2 ? "sunset" : part===3 ? "night" : "day");
setFollow(part===1 ? mayor : part===2 ? chaseCat : null);
}
if (part===0) {
wantYaw=mix(.57,.32,range(beat,0,3.8));
wantTilt=mix(.64,.71,range(beat,0,3.8));
wantZoom=mix(1,1.045,range(beat,0,3.8));
} else if (part===1) {
wantYaw=mix(.32,.58,range(beat,3.8,8));
wantTilt=mix(.71,.60,range(beat,3.8,8));
wantZoom=mix(1.25,1.42,range(beat,3.8,8));
} else if (part===2) {
wantYaw=mix(.58,.35,range(beat,8,12.1));
wantTilt=mix(.60,.73,range(beat,8,12.1));
wantZoom=1.32;
} else if (part===3) {
wantYaw=mix(.35,.91,range(beat,12.1,17.1));
wantTilt=mix(.73,.67,range(beat,12.1,17.1));
wantZoom=mix(1.05,.98,range(beat,12.1,17.1));
} else {
wantYaw=mix(.91,.57,range(beat,17.1,20));
wantTilt=mix(.67,.64,range(beat,17.1,20));
wantZoom=mix(.98,1,range(beat,17.1,20));
}
}
for (const button of modeButtons) button.addEventListener("click",()=>{stopTour();setLook(button.dataset.look);});
byId("zoom-in").addEventListener("click",()=>{stopTour();changeZoom(1.23);});
byId("zoom-out").addEventListener("click",()=>{stopTour();changeZoom(1/1.23);});
byId("reset-view").addEventListener("click",()=>{stopTour();resetView();});
followButton.addEventListener("click",()=>{stopTour();setFollow(null);});
const pointers=new Map();
let dragged=false,gestureStart=null,pinch=0;
const gap=()=>{
const [a,b]=pointers.values();
return Math.hypot(a.x-b.x,a.y-b.y);
};
stage.addEventListener("pointerdown",event=>{
if (event.pointerType==="mouse" && event.button!==0) return;
event.preventDefault();
const p={x:event.clientX,y:event.clientY};
pointers.set(event.pointerId,p);
stage.setPointerCapture(event.pointerId);
if (pointers.size===1) { dragged=false; gestureStart={...p,t:performance.now()}; }
else { pinch=gap(); dragged=true; }
});
stage.addEventListener("pointermove",event=>{
const previous=pointers.get(event.pointerId);
if (!previous) return;
const p={x:event.clientX,y:event.clientY};
pointers.set(event.pointerId,p);
if (pointers.size>1) {
const distance=gap();
if (pinch>1 && distance>1) changeZoom(distance/pinch);
pinch=distance;
return;
}
if (!dragged && Math.hypot(p.x-gestureStart.x,p.y-gestureStart.y)>5) dragged=true;
if (dragged) {
stage.classList.add("grabbing");
wantYaw-=(p.x-previous.x)*.0095;
wantTilt=clamp(wantTilt+(p.y-previous.y)*.007,.25,1.15);
}
});
function endPointer(event) {
if (!pointers.has(event.pointerId)) return;
const tap=!dragged && pointers.size===1 && event.type==="pointerup" && performance.now()-gestureStart.t<750;
pointers.delete(event.pointerId);
pinch=pointers.size>1 ? gap() : 0;
if (!pointers.size) stage.classList.remove("grabbing");
if (tap) selectAt(event.clientX,event.clientY,event.pointerType==="touch");
}
stage.addEventListener("pointerup",endPointer);
stage.addEventListener("pointercancel",endPointer);
stage.addEventListener("lostpointercapture",endPointer);
stage.addEventListener("wheel",event=>{
event.preventDefault();
stopTour(event);
const delta=event.deltaY*(event.deltaMode===1 ? 16 : event.deltaMode===2 ? height : 1);
changeZoom(Math.exp(clamp(-delta*.00145,-.56,.56)));
},{passive:false});
let startTime=null,lastTime=null,demoStart=null;
const actors=[];
function frame(now) {
if (startTime===null) startTime=now;
const t=(now-startTime)/1000;
const dt=lastTime===null ? 1/60 : Math.min((now-lastTime)/1000,.045);
lastTime=now;
if (demo) {
if (demoStart===null) demoStart=t;
runTour(t-demoStart);
}
const colorStep=1-Math.exp(-dt*4.0);
warm=mix(warm,look==="sunset" ? 1 : 0,colorStep);
night=mix(night,look==="night" ? 1 : 0,colorStep);
moveCats(dt,t);
camera=makeCamera(dt);
const light=normalize([mix(-.64,-.91,warm)+night*.93,mix(.94,.45,warm)-night*.22,mix(.48,.33,warm)-night*.75]);
actors.length=0;
animatedScenery(actors,t);
for (const cat of cats) catModel(actors,cat,t);
drawSky(t,warm,night);
renderer.render(camera,[warm,night],light,actors,t);
refreshLabels();
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
</script>
</body>
</html>
