Prompt specification
Build or customize the Three.js 3D scene "Apartment cat exploration route" 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: Apartment cat exploration route
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/apartment-cat-exploration-route-x0667
Scene Description Prompt:
A stylized cat explores boxes and furniture, with whiskers touching nearby obstacles. One HTML file using Three.js from cdnjs.
Working Three.js Starter (apartment-cat-exploration-route-x0667):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Whisker Wanderer</title>
<style>
:root {
color-scheme: light;
--ink: #49382f;
--cream: #fffaf0;
--glass: rgba(255, 250, 240, 0.86);
--accent: #d97855;
}
* {
box-sizing: border-box;
}
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #eee5d7;
color: var(--ink);
font-family: Inter, ui-rounded, "SF Pro Rounded", system-ui, sans-serif;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 5;
background: var(--glass);
border: 1px solid rgba(93, 67, 48, 0.13);
box-shadow: 0 12px 38px rgba(68, 43, 29, 0.13);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#title {
top: 22px;
left: 24px;
padding: 15px 19px 14px;
border-radius: 20px;
pointer-events: none;
}
#title h1 {
margin: 0 0 3px;
font-size: clamp(20px, 2.5vw, 30px);
letter-spacing: -0.04em;
}
#title p {
margin: 0;
color: #876f60;
font-size: 12px;
letter-spacing: 0.04em;
}
#controls {
top: 22px;
right: 24px;
width: 220px;
padding: 14px;
border-radius: 18px;
}
.control-title {
margin-bottom: 10px;
color: #9a7d6a;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.key-row {
display: flex;
align-items: center;
gap: 8px;
margin: 7px 0;
font-size: 12px;
color: #715c50;
}
kbd {
min-width: 25px;
padding: 4px 6px;
border: 1px solid rgba(77, 54, 39, 0.15);
border-bottom-width: 2px;
border-radius: 7px;
background: rgba(255,255,255,0.72);
color: #5a4438;
font: 700 10px/1 system-ui, sans-serif;
text-align: center;
}
.buttons {
display: flex;
gap: 7px;
margin-top: 12px;
}
button {
flex: 1;
padding: 9px 8px;
border: 0;
border-radius: 10px;
background: #6f8f7b;
color: white;
font: 700 11px/1.1 system-ui, sans-serif;
cursor: pointer;
box-shadow: 0 4px 12px rgba(65, 93, 76, 0.22);
transition: transform 0.15s, filter 0.15s;
}
button:hover {
filter: brightness(1.06);
transform: translateY(-1px);
}
button:active {
transform: translateY(1px);
}
button.secondary {
background: #a77c65;
}
button.off {
background: #9d8c82;
}
#status {
position: fixed;
z-index: 5;
left: 50%;
bottom: 22px;
display: flex;
align-items: center;
gap: 9px;
padding: 10px 15px;
border-radius: 999px;
background: rgba(255, 250, 240, 0.9);
border: 1px solid rgba(93, 67, 48, 0.12);
box-shadow: 0 9px 28px rgba(68, 43, 29, 0.12);
transform: translateX(-50%);
color: #806a5e;
font-size: 12px;
font-weight: 650;
white-space: nowrap;
pointer-events: none;
}
#status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #b9aaa0;
transition: 0.2s;
}
#status.touching #status-dot {
background: #efb849;
box-shadow: 0 0 0 5px rgba(239, 184, 73, 0.17);
animation: pulse 1.1s infinite;
}
@keyframes pulse {
50% { transform: scale(1.25); }
}
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: #eee5d7;
transition: opacity 0.5s;
pointer-events: none;
}
#loading.hidden {
opacity: 0;
}
.loader-cat {
width: 58px;
height: 48px;
margin: 0 auto 15px;
border-radius: 45% 45% 40% 40%;
background: #d88945;
position: relative;
animation: bob 0.8s ease-in-out infinite alternate;
}
.loader-cat::before,
.loader-cat::after {
content: "";
position: absolute;
top: -17px;
width: 0;
height: 0;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
border-bottom: 25px solid #d88945;
}
.loader-cat::before { left: 0; transform: rotate(-14deg); }
.loader-cat::after { right: 0; transform: rotate(14deg); }
#loading p {
text-align: center;
color: #866d5e;
font-size: 12px;
letter-spacing: 0.08em;
}
@keyframes bob {
to { transform: translateY(-8px) rotate(2deg); }
}
#touch-pad {
display: none;
position: fixed;
z-index: 7;
left: 18px;
bottom: 20px;
grid-template-columns: repeat(3, 42px);
grid-template-rows: repeat(2, 42px);
gap: 5px;
}
#touch-pad button {
padding: 0;
background: rgba(255,250,240,0.9);
color: #654d40;
border: 1px solid rgba(80,55,40,0.15);
box-shadow: 0 5px 16px rgba(68,43,29,0.14);
font-size: 19px;
}
@media (max-width: 720px) {
#title {
top: 12px;
left: 12px;
padding: 12px 15px;
}
#controls {
top: auto;
right: 12px;
bottom: 68px;
width: 178px;
}
.key-row {
display: none;
}
#status {
bottom: 18px;
left: auto;
right: 12px;
transform: none;
max-width: calc(100vw - 185px);
overflow: hidden;
text-overflow: ellipsis;
}
}
@media (pointer: coarse) {
#touch-pad { display: grid; }
#status { left: 178px; transform: none; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D cat exploring a miniature room"></canvas>
<section id="title" class="panel">
<h1>Whisker Wanderer</h1>
<p>A tiny explorer in a sunlit room</p>
</section>
<section id="controls" class="panel">
<div class="control-title">Explore</div>
<div class="key-row"><kbd>WASD</kbd><span>Guide the cat</span></div>
<div class="key-row"><kbd>Drag</kbd><span>Orbit the room</span></div>
<div class="key-row"><kbd>Scroll</kbd><span>Zoom closer</span></div>
<div class="buttons">
<button id="roam-button" type="button">Auto roam</button>
<button id="reset-button" class="secondary" type="button">Reset view</button>
</div>
</section>
<div id="status" aria-live="polite">
<span id="status-dot"></span>
<span id="status-text">Whiskers are listening…</span>
</div>
<div id="touch-pad" aria-label="Movement controls">
<span></span><button data-key="KeyW" type="button">↑</button><span></span>
<button data-key="KeyA" type="button">←</button>
<button data-key="KeyS" type="button">↓</button>
<button data-key="KeyD" type="button">→</button>
</div>
<div id="loading">
<div>
<div class="loader-cat"></div>
<p>WAKING THE CAT…</p>
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene and camera
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.13;
const scene = new THREE.Scene();
scene.background = new THREE.Color("#eee5d7");
scene.fog = new THREE.Fog("#eee5d7", 14, 29);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraTarget = new THREE.Vector3(0, 0.55, 0);
const cameraGoal = {
azimuth: 0.69,
polar: 0.96,
radius: 13.8
};
const cameraCurrent = { ...cameraGoal };
function updateCamera(dt) {
const damping = 1 - Math.exp(-8 * dt);
cameraCurrent.azimuth = THREE.MathUtils.lerp(
cameraCurrent.azimuth, cameraGoal.azimuth, damping
);
cameraCurrent.polar = THREE.MathUtils.lerp(
cameraCurrent.polar, cameraGoal.polar, damping
);
cameraCurrent.radius = THREE.MathUtils.lerp(
cameraCurrent.radius, cameraGoal.radius, damping
);
const { azimuth, polar, radius } = cameraCurrent;
camera.position.set(
cameraTarget.x + radius * Math.sin(polar) * Math.sin(azimuth),
cameraTarget.y + radius * Math.cos(polar),
cameraTarget.z + radius * Math.sin(polar) * Math.cos(azimuth)
);
camera.lookAt(cameraTarget);
}
// Custom pointer camera controls, implemented without addons.
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraGoal.azimuth -= dx * 0.006;
cameraGoal.polar = THREE.MathUtils.clamp(
cameraGoal.polar - dy * 0.005,
0.48,
1.34
);
});
function stopDragging() {
dragging = false;
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraGoal.radius = THREE.MathUtils.clamp(
cameraGoal.radius + event.deltaY * 0.008,
7.8,
19
);
}, { passive: false });
function resetCamera() {
cameraGoal.azimuth = 0.69;
cameraGoal.polar = 0.96;
cameraGoal.radius = 13.8;
}
canvas.addEventListener("dblclick", resetCamera);
document.querySelector("#reset-button").addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
const material = (color, roughness = 0.72, extra = {}) =>
new THREE.MeshStandardMaterial({ color, roughness, ...extra });
const M = {
wall: material("#efe3d2"),
wallTrim: material("#d8c4ad"),
floor: material("#cda77d"),
platform: material("#b98d68"),
rug: material("#c77e65"),
rugInner: material("#e6b49a"),
sage: material("#89a58e"),
sageDark: material("#66816e"),
sageLight: material("#a8bea6"),
coral: material("#d98573"),
coralDark: material("#b76559"),
wood: material("#aa7048"),
woodLight: material("#c99262"),
box: material("#c6945b"),
boxDark: material("#a87542"),
tape: material("#e7c78f"),
cream: material("#f8ead5"),
white: material("#fff8eb"),
navy: material("#344c5d"),
blue: material("#91b7c5", 0.55, {
emissive: "#6f9cac",
emissiveIntensity: 0.18
}),
leaf: material("#628565"),
leafLight: material("#83a276"),
pot: material("#b86f55"),
cat: material("#d88945"),
catDark: material("#a85c35"),
catCream: material("#f8dfbd"),
eye: material("#27211e", 0.3),
pink: material("#d77f83"),
yarn: material("#8b9eb2"),
yarnPink: material("#d88891")
};
function mesh(geometry, mat, parent = scene) {
const object = new THREE.Mesh(geometry, mat);
object.castShadow = true;
object.receiveShadow = true;
parent.add(object);
return object;
}
function box(parent, w, h, d, mat, x = 0, y = 0, z = 0) {
const object = mesh(new THREE.BoxGeometry(w, h, d), mat, parent);
object.position.set(x, y, z);
return object;
}
function cylinder(parent, rTop, rBottom, h, mat, x = 0, y = 0, z = 0, segments = 20) {
const object = mesh(
new THREE.CylinderGeometry(rTop, rBottom, h, segments),
mat,
parent
);
object.position.set(x, y, z);
return object;
}
function sphere(parent, radius, mat, x = 0, y = 0, z = 0, width = 24, height = 16) {
const object = mesh(
new THREE.SphereGeometry(radius, width, height),
mat,
parent
);
object.position.set(x, y, z);
return object;
}
function roundedShape(width, height, radius) {
const x = -width / 2;
const y = -height / 2;
const shape = new THREE.Shape();
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
return shape;
}
// -------------------------------------------------------------------------
// Room shell and lighting
// -------------------------------------------------------------------------
box(scene, 13, 0.24, 9.6, M.platform, 0, -0.15, 0);
box(scene, 12.8, 3.15, 0.18, M.wall, 0, 1.48, -4.78);
box(scene, 0.18, 3.15, 9.55, M.wall, -6.43, 1.48, 0);
box(scene, 12.8, 0.13, 0.08, M.wallTrim, 0, 0.08, -4.66);
box(scene, 0.08, 0.13, 9.55, M.wallTrim, -6.31, 0.08, 0);
const hemi = new THREE.HemisphereLight("#fff4df", "#9c765d", 2.05);
scene.add(hemi);
const sun = new THREE.DirectionalLight("#ffe7b8", 3.25);
sun.position.set(-5.5, 10, 5.8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 9;
sun.shadow.camera.bottom = -9;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 25;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight("#bdd4dc", 0.8);
fill.position.set(7, 5, -4);
scene.add(fill);
const warmPoint = new THREE.PointLight("#ffd498", 0.65, 15, 2);
warmPoint.position.set(-1.5, 4, 2);
scene.add(warmPoint);
// Window and simple wall decorations.
box(scene, 2.15, 1.45, 0.08, M.woodLight, -3.9, 1.95, -4.65);
box(scene, 1.92, 1.22, 0.09, M.blue, -3.9, 1.95, -4.59);
box(scene, 0.075, 1.25, 0.11, M.white, -3.9, 1.95, -4.53);
box(scene, 1.95, 0.075, 0.11, M.white, -3.9, 1.95, -4.53);
box(scene, 2.35, 0.12, 0.34, M.cream, -3.9, 1.15, -4.48);
box(scene, 0.08, 1.02, 0.08, M.wood, -0.2, 1.95, -4.63);
box(scene, 0.9, 0.72, 0.07, M.wood, -0.2, 1.95, -4.58);
box(scene, 0.72, 0.55, 0.08, M.cream, -0.2, 1.95, -4.53);
// Soft rug.
const rug = mesh(
new THREE.ShapeGeometry(roundedShape(5.1, 3.5, 0.42)),
M.rug
);
rug.rotation.x = -Math.PI / 2;
rug.position.set(-0.75, 0.015, 0.55);
rug.castShadow = false;
const rugInner = mesh(
new THREE.ShapeGeometry(roundedShape(4.55, 2.95, 0.32)),
M.rugInner
);
rugInner.rotation.x = -Math.PI / 2;
rugInner.position.set(-0.75, 0.022, 0.55);
rugInner.castShadow = false;
// -------------------------------------------------------------------------
// Collidable furniture and props
// -------------------------------------------------------------------------
const obstacles = [];
function addObstacle(name, minX, minZ, maxX, maxZ, maxY = 2) {
obstacles.push({
name,
box: new THREE.Box3(
new THREE.Vector3(minX, 0, minZ),
new THREE.Vector3(maxX, maxY, maxZ)
)
});
}
// Sofa.
const sofa = new THREE.Group();
sofa.position.set(-3.55, 0, -2.85);
scene.add(sofa);
box(sofa, 3.35, 0.4, 1.25, M.sageDark, 0, 0.38, 0);
box(sofa, 3.35, 1.08, 0.28, M.sage, 0, 1.02, -0.5);
box(sofa, 0.34, 0.72, 1.25, M.sage, -1.66, 0.69, 0);
box(sofa, 0.34, 0.72, 1.25, M.sage, 1.66, 0.69, 0);
for (const x of [-0.77, 0.77]) {
const cushion = box(sofa, 1.45, 0.28, 0.98, M.sageLight, x, 0.72, 0.04);
cushion.rotation.x = -0.015;
}
for (const x of [-1.35, 1.35]) {
for (const z of [-0.42, 0.42]) {
cylinder(sofa, 0.08, 0.06, 0.25, M.wood, x, 0.15, z, 12);
}
}
const pillow = box(sofa, 0.7, 0.66, 0.2, M.cream, -0.92, 1.03, -0.29);
pillow.rotation.z = 0.12;
pillow.rotation.x = -0.08;
addObstacle("sage sofa", -5.35, -3.65, -1.72, -2.05, 1.65);
// Armchair.
const chair = new THREE.Group();
chair.position.set(3.75, 0, -2.75);
scene.add(chair);
box(chair, 1.7, 0.38, 1.5, M.coralDark, 0, 0.4, 0);
box(chair, 1.68, 1.18, 0.3, M.coral, 0, 1.05, -0.6);
box(chair, 0.32, 0.76, 1.5, M.coral, -0.83, 0.72, 0);
box(chair, 0.32, 0.76, 1.5, M.coral, 0.83, 0.72, 0);
box(chair, 1.18, 0.27, 1.18, M.coral, 0, 0.71, 0.03);
for (const x of [-0.62, 0.62]) {
for (const z of [-0.47, 0.47]) {
cylinder(chair, 0.08, 0.06, 0.27, M.wood, x, 0.16, z, 12);
}
}
addObstacle("coral armchair", 2.72, -3.67, 4.78, -1.83, 1.7);
// Coffee table.
const table = new THREE.Group();
table.position.set(-1.15, 0, -0.2);
scene.add(table);
box(table, 2.45, 0.18, 1.28, M.woodLight, 0, 0.8, 0);
box(table, 2.2, 0.09, 1.04, M.wood, 0, 0.67, 0);
for (const x of [-0.92, 0.92]) {
for (const z of [-0.4, 0.4]) {
cylinder(table, 0.08, 0.065, 0.67, M.wood, x, 0.35, z, 12);
}
}
const book = box(table, 0.55, 0.08, 0.38, M.navy, -0.52, 0.94, 0.04);
book.rotation.y = 0.18;
cylinder(table, 0.13, 0.11, 0.22, M.cream, 0.65, 0.99, -0.02, 18);
cylinder(table, 0.09, 0.09, 0.015, M.boxDark, 0.65, 1.105, -0.02, 18);
addObstacle("coffee table", -2.63, -1.05, 0.33, 0.65, 0.95);
// Low bookcase against the back wall.
const bookcase = new THREE.Group();
bookcase.position.set(0.72, 0, -4.08);
scene.add(bookcase);
box(bookcase, 2.65, 0.15, 0.58, M.wood, 0, 0.18, 0);
box(bookcase, 2.65, 0.15, 0.58, M.wood, 0, 1.48, 0);
box(bookcase, 0.14, 1.35, 0.58, M.wood, -1.26, 0.82, 0);
box(bookcase, 0.14, 1.35, 0.58, M.wood, 1.26, 0.82, 0);
const bookColors = [M.navy, M.coral, M.sageDark, M.cream, M.boxDark];
for (let i = 0; i < 9; i++) {
const h = 0.32 + (i % 3) * 0.07;
box(
bookcase,
0.14,
h,
0.31,
bookColors[i % bookColors.length],
-0.95 + i * 0.22,
0.26 + h / 2,
0.02
);
}
addObstacle("bookcase", -0.68, -4.43, 2.11, -3.73, 1.6);
// Closed cardboard box near the cat's initial position.
const closedBox = new THREE.Group();
closedBox.position.set(1.75, 0, 1.35);
scene.add(closedBox);
box(closedBox, 1, 1.12, 1, M.box, 0, 0.56, 0);
box(closedBox, 0.19, 0.012, 1.01, M.tape, 0, 1.126, 0);
box(closedBox, 0.19, 1.13, 0.012, M.tape, 0, 0.565, 0.507);
box(closedBox, 0.012, 0.76, 0.16, M.boxDark, 0.507, 0.4, 0.16);
addObstacle("cardboard box", 1.25, 0.85, 2.25, 1.85, 1.16);
// Open cardboard box.
const openBox = new THREE.Group();
openBox.position.set(3.45, 0, 2.55);
scene.add(openBox);
box(openBox, 1.25, 0.1, 1.1, M.boxDark, 0, 0.08, 0);
box(openBox, 1.25, 0.82, 0.09, M.box, 0, 0.45, -0.505);
box(openBox, 1.25, 0.82, 0.09, M.box, 0, 0.45, 0.505);
box(openBox, 0.09, 0.82, 1.1, M.box, -0.58, 0.45, 0);
box(openBox, 0.09, 0.82, 1.1, M.box, 0.58, 0.45, 0);
const flapA = box(openBox, 1.18, 0.07, 0.48, M.box, 0, 0.86, 0.64);
flapA.rotation.x = -0.62;
const flapB = box(openBox, 1.18, 0.07, 0.48, M.box, 0, 0.86, -0.64);
flapB.rotation.x = 0.62;
const yarn = sphere(openBox, 0.25, M.yarn, -0.19, 0.35, 0.04);
yarn.scale.set(1, 0.96, 1);
const yarnWrap = mesh(
new THREE.TorusGeometry(0.255, 0.018, 5, 32),
M.yarnPink,
openBox
);
yarnWrap.position.copy(yarn.position);
yarnWrap.rotation.set(0.65, 0.2, 0.5);
addObstacle("open cardboard box", 2.825, 2, 4.075, 3.1, 0.95);
// A second closed box.
const littleBox = new THREE.Group();
littleBox.position.set(-4.55, 0, 1.75);
littleBox.rotation.y = -0.12;
scene.add(littleBox);
box(littleBox, 1.05, 0.9, 1.1, M.box, 0, 0.45, 0);
box(littleBox, 0.18, 0.012, 1.11, M.tape, 0, 0.906, 0);
addObstacle("small cardboard box", -5.13, 1.14, -3.97, 2.36, 0.94);
// Plant.
const plant = new THREE.Group();
plant.position.set(5.35, 0, -3.85);
scene.add(plant);
cylinder(plant, 0.31, 0.24, 0.58, M.pot, 0, 0.29, 0, 18);
for (let i = 0; i < 7; i++) {
const angle = (i / 7) * Math.PI * 2;
const leaf = sphere(
plant,
0.28,
i % 2 ? M.leaf : M.leafLight,
Math.cos(angle) * 0.22,
0.82 + (i % 3) * 0.12,
Math.sin(angle) * 0.22
);
leaf.scale.set(0.5, 1.45, 0.28);
leaf.rotation.z = Math.cos(angle) * 0.52;
leaf.rotation.x = Math.sin(angle) * 0.52;
}
// -------------------------------------------------------------------------
// Stylized cat
// -------------------------------------------------------------------------
const cat = new THREE.Group();
cat.position.set(-0.15, 0.025, 1.35);
cat.rotation.y = Math.PI / 2;
scene.add(cat);
const body = sphere(cat, 1, M.cat, 0, 0.69, 0);
body.scale.set(0.56, 0.45, 0.87);
const belly = sphere(cat, 1, M.catCream, 0, 0.58, 0.04);
belly.scale.set(0.42, 0.34, 0.69);
for (const z of [-0.38, -0.1, 0.18]) {
const stripe = mesh(
new THREE.TorusGeometry(0.44, 0.035, 6, 28),
M.catDark,
cat
);
stripe.position.set(0, 0.72, z);
stripe.scale.set(1.19, 0.94, 1);
}
const head = new THREE.Group();
head.position.set(0, 1.12, 0.72);
cat.add(head);
const skull = sphere(head, 0.52, M.cat);
skull.scale.set(1, 0.92, 0.92);
for (const side of [-1, 1]) {
const ear = mesh(
new THREE.ConeGeometry(0.245, 0.48, 3),
M.cat,
head
);
ear.position.set(side * 0.33, 0.47, -0.015);
ear.rotation.z = side * -0.18;
const innerEar = mesh(
new THREE.ConeGeometry(0.145, 0.29, 3),
M.pink,
head
);
innerEar.position.set(side * 0.33, 0.48, 0.09);
innerEar.rotation.z = side * -0.18;
innerEar.scale.z = 0.45;
}
for (const side of [-1, 1]) {
const muzzle = sphere(head, 0.23, M.catCream, side * 0.13, -0.14, 0.37);
muzzle.scale.set(1, 0.82, 0.72);
const eye = sphere(head, 0.087, M.eye, side * 0.205, 0.075, 0.415);
eye.scale.set(0.82, 1, 0.48);
const highlight = sphere(
head,
0.019,
M.white,
side * 0.205 - 0.018,
0.102,
0.454,
10,
8
);
highlight.castShadow = false;
}
const nose = sphere(head, 0.066, M.pink, 0, -0.055, 0.52);
nose.scale.set(1.2, 0.78, 0.72);
const mouth = mesh(
new THREE.TorusGeometry(0.075, 0.011, 5, 16, Math.PI),
M.catDark,
head
);
mouth.position.set(0, -0.12, 0.505);
mouth.rotation.z = Math.PI;
for (const x of [-0.18, 0, 0.18]) {
const foreheadStripe = sphere(head, 0.1, M.catDark, x, 0.37, 0.28);
foreheadStripe.scale.set(0.55, 1.25, 0.38);
foreheadStripe.rotation.z = x * 1.3;
}
const legs = [];
for (const z of [0.43, -0.48]) {
for (const x of [-0.33, 0.33]) {
const leg = new THREE.Group();
leg.position.set(x, 0.45, z);
cat.add(leg);
cylinder(leg, 0.105, 0.12, 0.38, M.cat, 0, -0.16, 0, 12);
const paw = sphere(leg, 0.145, M.catCream, 0, -0.35, 0.035);
paw.scale.set(1, 0.68, 1.15);
legs.push(leg);
}
}
// Segmented, animated tail.
const tailRoot = new THREE.Group();
tailRoot.position.set(0, 0.79, -0.67);
cat.add(tailRoot);
const tailSegments = [];
let tailParent = tailRoot;
for (let i = 0; i < 7; i++) {
const segment = new THREE.Group();
tailParent.add(segment);
const piece = cylinder(
segment,
0.105 - i * 0.007,
0.12 - i * 0.007,
0.34,
i % 2 ? M.cat : M.catDark,
0,
0,
-0.16,
12
);
piece.rotation.x = Math.PI / 2;
tailSegments.push(segment);
segment.position.z = i === 0 ? 0 : -0.32;
tailParent = segment;
}
// Whiskers are updated by raycasting against nearby obstacle boxes.
const whiskerDefinitions = [];
const whiskerMaterial = new THREE.LineBasicMaterial({
color: "#4b342b",
transparent: true,
opacity: 0.94
});
const contactMaterial = new THREE.MeshBasicMaterial({
color: "#ffe185"
});
const contactRingMaterial = new THREE.MeshBasicMaterial({
color: "#ffcb58",
transparent: true,
opacity: 0.88,
side: THREE.DoubleSide,
depthWrite: false
});
for (const side of [-1, 1]) {
whiskerDefinitions.push(
{
origin: new THREE.Vector3(side * 0.28, -0.015, 0.39),
direction: new THREE.Vector3(side * 1, 0.045, 0.2).normalize(),
length: 0.91
},
{
origin: new THREE.Vector3(side * 0.29, -0.07, 0.38),
direction: new THREE.Vector3(side * 0.72, 0.11, 0.7).normalize(),
length: 0.85
},
{
origin: new THREE.Vector3(side * 0.29, -0.12, 0.35),
direction: new THREE.Vector3(side * 0.36, 0.18, 0.94).normalize(),
length: 0.76
}
);
}
for (const definition of whiskerDefinitions) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(12), 3)
);
const line = new THREE.Line(geometry, whiskerMaterial);
line.frustumCulled = false;
scene.add(line);
const marker = mesh(
new THREE.SphereGeometry(0.035, 10, 8),
contactMaterial
);
marker.visible = false;
marker.castShadow = false;
const ring = mesh(
new THREE.RingGeometry(0.055, 0.082, 24),
contactRingMaterial
);
ring.visible = false;
ring.castShadow = false;
ring.receiveShadow = false;
definition.line = line;
definition.marker = marker;
definition.ring = ring;
}
// -------------------------------------------------------------------------
// Navigation: a small A* grid lets the cat route around furniture.
// -------------------------------------------------------------------------
const NAV = {
minX: -5.75,
maxX: 5.75,
minZ: -4.18,
maxZ: 4.18,
step: 0.35
};
const navColumns = Math.floor((NAV.maxX - NAV.minX) / NAV.step) + 1;
const navRows = Math.floor((NAV.maxZ - NAV.minZ) / NAV.step) + 1;
const CAT_RADIUS = 0.36;
function worldIsFree(x, z) {
if (
x < NAV.minX || x > NAV.maxX ||
z < NAV.minZ || z > NAV.maxZ
) return false;
for (const obstacle of obstacles) {
const b = obstacle.box;
if (
x > b.min.x - CAT_RADIUS && x < b.max.x + CAT_RADIUS &&
z > b.min.z - CAT_RADIUS && z < b.max.z + CAT_RADIUS
) return false;
}
return true;
}
function cellIsFree(cx, cz) {
if (cx < 0 || cz < 0 || cx >= navColumns || cz >= navRows) return false;
return worldIsFree(
NAV.minX + cx * NAV.step,
NAV.minZ + cz * NAV.step
);
}
function worldToCell(x, z) {
return {
cx: THREE.MathUtils.clamp(
Math.round((x - NAV.minX) / NAV.step),
0,
navColumns - 1
),
cz: THREE.MathUtils.clamp(
Math.round((z - NAV.minZ) / NAV.step),
0,
navRows - 1
)
};
}
function cellToWorld(cx, cz) {
return {
x: NAV.minX + cx * NAV.step,
z: NAV.minZ + cz * NAV.step
};
}
function nearestFreeCell(cx, cz) {
if (cellIsFree(cx, cz)) return { cx, cz };
for (let radius = 1; radius < 20; radius++) {
for (let dz = -radius; dz <= radius; dz++) {
for (let dx = -radius; dx <= radius; dx++) {
if (Math.abs(dx) !== radius && Math.abs(dz) !== radius) continue;
if (cellIsFree(cx + dx, cz + dz)) {
return { cx: cx + dx, cz: cz + dz };
}
}
}
}
return null;
}
function segmentIsClear(a, b) {
const distance = Math.hypot(b.x - a.x, b.z - a.z);
const steps = Math.max(1, Math.ceil(distance / 0.13));
for (let i = 0; i <= steps; i++) {
const t = i / steps;
const x = THREE.MathUtils.lerp(a.x, b.x, t);
const z = THREE.MathUtils.lerp(a.z, b.z, t);
if (!worldIsFree(x, z)) return false;
}
return true;
}
function findPath(startWorld, goalWorld) {
const start = nearestFreeCell(
...Object.values(worldToCell(startWorld.x, startWorld.z))
);
const goal = nearestFreeCell(
...Object.values(worldToCell(goalWorld.x, goalWorld.z))
);
if (!start || !goal) return [];
const total = navColumns * navRows;
const gScore = new Float64Array(total);
gScore.fill(Infinity);
const cameFrom = new Int32Array(total);
cameFrom.fill(-1);
const closed = new Uint8Array(total);
const open = [];
const index = (cx, cz) => cz * navColumns + cx;
const heuristic = (a, b) =>
Math.hypot(a.cx - b.cx, a.cz - b.cz);
const startIndex = index(start.cx, start.cz);
const goalIndex = index(goal.cx, goal.cz);
gScore[startIndex] = 0;
open.push({ index: startIndex, f: heuristic(start, goal) });
const directions = [
[-1, 0], [1, 0], [0, -1], [0, 1],
[-1, -1], [1, -1], [-1, 1], [1, 1]
];
while (open.length) {
open.sort((a, b) => a.f - b.f);
const current = open.shift();
if (closed[current.index]) continue;
if (current.index === goalIndex) break;
closed[current.index] = 1;
const cx = current.index % navColumns;
const cz = Math.floor(current.index / navColumns);
for (const [dx, dz] of directions) {
const nx = cx + dx;
const nz = cz + dz;
if (!cellIsFree(nx, nz)) continue;
if (dx !== 0 && dz !== 0) {
if (!cellIsFree(cx + dx, cz) || !cellIsFree(cx, cz + dz)) {
continue;
}
}
const nextIndex = index(nx, nz);
if (closed[nextIndex]) continue;
const movement = dx !== 0 && dz !== 0 ? Math.SQRT2 : 1;
const tentative = gScore[current.index] + movement;
if (tentative < gScore[nextIndex]) {
cameFrom[nextIndex] = current.index;
gScore[nextIndex] = tentative;
open.push({
index: nextIndex,
f: tentative + heuristic({ cx: nx, cz: nz }, goal)
});
}
}
}
if (cameFrom[goalIndex] === -1 && goalIndex !== startIndex) return [];
const reversed = [];
let currentIndex = goalIndex;
while (currentIndex !== -1) {
const cx = currentIndex % navColumns;
const cz = Math.floor(currentIndex / navColumns);
reversed.push(cellToWorld(cx, cz));
currentIndex = cameFrom[currentIndex];
}
const raw = reversed.reverse();
raw.unshift({ x: startWorld.x, z: startWorld.z });
// Remove unnecessary grid points where the cat can travel in a straight line.
const simplified = [];
let anchor = { x: startWorld.x, z: startWorld.z };
let cursor = 0;
while (cursor < raw.length) {
let furthest = cursor;
for (let candidate = raw.length - 1; candidate > cursor; candidate--) {
if (segmentIsClear(anchor, raw[candidate])) {
furthest = candidate;
break;
}
}
simplified.push(raw[furthest]);
anchor = raw[furthest];
cursor = furthest + 1;
}
return simplified;
}
const goals = [
[-4.85, -0.55], [-2.55, 1.15], [-0.1, -1.65],
[2.25, -0.2], [4.85, -0.25], [4.85, 1.25],
[2.3, 3.65], [-0.65, 3.7], [-4.9, 3.45],
[-2.7, -3.7], [1.55, -2.25], [-3.8, 0.25]
];
let autoMode = true;
let autoDelay = 2.4;
let currentPath = [];
let firstGoal = true;
const roamButton = document.querySelector("#roam-button");
function setAutoMode(enabled) {
autoMode = enabled;
roamButton.textContent = enabled ? "Auto roam" : "Manual mode";
roamButton.classList.toggle("off", !enabled);
if (!enabled) {
currentPath = [];
} else {
autoDelay = 0.15;
}
}
roamButton.addEventListener("click", () => setAutoMode(!autoMode));
// -------------------------------------------------------------------------
// Input and movement
// -------------------------------------------------------------------------
const pressed = new Set();
const movementKeys = [
"KeyW", "KeyA", "KeyS", "KeyD",
"ArrowUp", "ArrowLeft", "ArrowDown", "ArrowRight"
];
window.addEventListener("keydown", (event) => {
if (!movementKeys.includes(event.code)) return;
event.preventDefault();
pressed.add(event.code);
if (autoMode) setAutoMode(false);
});
window.addEventListener("keyup", (event) => {
pressed.delete(event.code);
});
document.querySelectorAll("#touch-pad button").forEach((button) => {
const code = button.dataset.key;
button.addEventListener("pointerdown", (event) => {
event.preventDefault();
event.stopPropagation();
pressed.add(code);
if (autoMode) setAutoMode(false);
button.setPointerCapture(event.pointerId);
});
const release = () => pressed.delete(code);
button.addEventListener("pointerup", release);
button.addEventListener("pointercancel", release);
});
function blockedAt(x, z) {
return !worldIsFree(x, z);
}
function moveCat(dx, dz) {
const nextX = cat.position.x + dx;
if (!blockedAt(nextX, cat.position.z)) cat.position.x = nextX;
const nextZ = cat.position.z + dz;
if (!blockedAt(cat.position.x, nextZ)) cat.position.z = nextZ;
}
function shortestAngleDifference(from, to) {
return Math.atan2(Math.sin(to - from), Math.cos(to - from));
}
const velocity = new THREE.Vector3();
const desiredVelocity = new THREE.Vector3();
let walkPhase = 0;
let actualSpeed = 0;
function updateMovement(dt) {
desiredVelocity.set(0, 0, 0);
const up = pressed.has("KeyW") || pressed.has("ArrowUp");
const down = pressed.has("KeyS") || pressed.has("ArrowDown");
const left = pressed.has("KeyA") || pressed.has("ArrowLeft");
const right = pressed.has("KeyD") || pressed.has("ArrowRight");
if (up || down || left || right) {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
const cameraRight = new THREE.Vector3(
-forward.z,
0,
forward.x
);
if (up) desiredVelocity.add(forward);
if (down) desiredVelocity.sub(forward);
if (right) desiredVelocity.add(cameraRight);
if (left) desiredVelocity.sub(cameraRight);
if (desiredVelocity.lengthSq() > 0) {
desiredVelocity.normalize().multiplyScalar(2.35);
}
} else if (autoMode) {
if (currentPath.length === 0) {
autoDelay -= dt;
if (autoDelay <= 0) {
let goal;
if (firstGoal) {
goal = { x: 4.75, z: 1.2 };
firstGoal = false;
} else {
do {
goal = {
x: goals[Math.floor(Math.random() * goals.length)][0],
z: goals[Math.floor(Math.random() * goals.length)][1]
};
} while (
Math.hypot(
goal.x - cat.position.x,
goal.z - cat.position.z
) < 1
);
}
currentPath = findPath(cat.position, goal);
autoDelay = 0.35 + Math.random() * 0.9;
}
} else {
const target = currentPath[0];
const dx = target.x - cat.position.x;
const dz = target.z - cat.position.z;
const distance = Math.hypot(dx, dz);
if (distance < 0.13) {
currentPath.shift();
} else {
desiredVelocity.set(dx / distance, 0, dz / distance);
desiredVelocity.multiplyScalar(1.38);
}
}
}
const step = desiredVelocity.clone().multiplyScalar(dt);
const oldPosition = cat.position.clone();
moveCat(step.x, step.z);
velocity.subVectors(cat.position, oldPosition).divideScalar(Math.max(dt, 0.001));
actualSpeed = velocity.length();
if (velocity.lengthSq() > 0.001) {
const targetAngle = Math.atan2(velocity.x, velocity.z);
cat.rotation.y += shortestAngleDifference(cat.rotation.y, targetAngle)
* Math.min(1, dt * 9);
}
walkPhase += actualSpeed * dt * 5.2;
}
// -------------------------------------------------------------------------
// Whisker contact detection
// -------------------------------------------------------------------------
const status = document.querySelector("#status");
const statusText = document.querySelector("#status-text");
const ray = new THREE.Ray();
const hitPoint = new THREE.Vector3();
let previousStatus = "";
function updateWhiskers(elapsed) {
const touchedObjects = new Set();
let contactCount = 0;
for (const whisker of whiskerDefinitions) {
const origin = head.localToWorld(whisker.origin.clone());
const direction = whisker.direction.clone().transformDirection(head.matrixWorld);
direction.normalize();
ray.set(origin, direction);
let nearestDistance = whisker.length;
let hitObstacle = null;
for (const obstacle of obstacles) {
const intersection = ray.intersectBox(obstacle.box, hitPoint);
if (!intersection) continue;
const distance = origin.distanceTo(intersection);
if (distance < nearestDistance) {
nearestDistance = distance;
hitObstacle = obstacle;
}
}
const end = origin.clone().addScaledVector(direction, nearestDistance);
const point1 = origin.clone().lerp(end, 0.3);
const point2 = origin.clone().lerp(end, 0.7);
point1.y -= 0.015 + Math.sin(elapsed * 2.1) * 0.006;
point2.y -= 0.022 + Math.sin(elapsed * 2.1 + 0.7) * 0.008;
const positions = whisker.line.geometry.attributes.position.array;
positions.set([
origin.x, origin.y, origin.z,
point1.x, point1.y, point1.z,
point2.x, point2.y, point2.z,
end.x, end.y, end.z
]);
whisker.line.geometry.attributes.position.needsUpdate = true;
whisker.line.geometry.computeBoundingSphere();
whisker.marker.visible = Boolean(hitObstacle);
whisker.ring.visible = Boolean(hitObstacle);
if (hitObstacle) {
touchedObjects.add(hitObstacle.name);
contactCount++;
const markerPosition = end.clone().addScaledVector(direction, 0.008);
whisker.marker.position.copy(markerPosition);
whisker.ring.position.copy(markerPosition);
whisker.ring.quaternion.copy(camera.quaternion);
const pulse = 1 + Math.sin(elapsed * 5) * 0.18;
whisker.marker.scale.setScalar(pulse);
whisker.ring.scale.setScalar(0.9 + Math.sin(elapsed * 4) * 0.15);
}
}
const nextStatus = contactCount
? `Whisker touch · ${[...touchedObjects].join(", ")}`
: "Whiskers are listening…";
if (nextStatus !== previousStatus) {
statusText.textContent = nextStatus;
status.classList.toggle("touching", contactCount > 0);
previousStatus = nextStatus;
}
}
// -------------------------------------------------------------------------
// Ambient dust and animation
// -------------------------------------------------------------------------
const dustCount = 70;
const dustPositions = new Float32Array(dustCount * 3);
const dustBase = [];
for (let i = 0; i < dustCount; i++) {
const x = -6 + Math.random() * 12;
const y = 0.35 + Math.random() * 3.8;
const z = -4.2 + Math.random() * 8.2;
dustPositions.set([x, y, z], i * 3);
dustBase.push({ x, y, z, phase: Math.random() * Math.PI * 2 });
}
const dustGeometry = new THREE.BufferGeometry();
dustGeometry.setAttribute(
"position",
new THREE.BufferAttribute(dustPositions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: "#fff3cf",
size: 0.035,
transparent: true,
opacity: 0.56,
depthWrite: false
})
);
scene.add(dust);
const eyes = head.children.filter(
(child) => child.material === M.eye
);
function animateCat(dt, elapsed) {
const speedRatio = THREE.MathUtils.clamp(actualSpeed / 1.6, 0, 1);
for (let i = 0; i < legs.length; i++) {
const offset = i < 2 ? 0 : Math.PI;
const mirrored = i % 2 ? Math.PI : 0;
legs[i].rotation.x =
Math.sin(walkPhase + offset + mirrored) * 0.57 * speedRatio;
}
cat.position.y = 0.025 + Math.abs(Math.sin(walkPhase * 2)) * 0.035 * speedRatio;
head.rotation.y = Math.sin(elapsed * 0.62) * 0.08;
head.rotation.x = Math.sin(elapsed * 0.43) * 0.025;
tailSegments.forEach((segment, i) => {
segment.rotation.y =
Math.sin(elapsed * 2.2 - i * 0.48) * (0.055 + i * 0.012);
segment.rotation.x =
0.1 + i * 0.035 + Math.sin(elapsed * 1.7 - i * 0.4) * 0.045;
});
const blinkPhase = elapsed % 4.8;
const blink = blinkPhase > 4.55
? Math.abs(Math.sin((blinkPhase - 4.55) * 18))
: 1;
for (const eye of eyes) {
eye.scale.y = Math.max(0.06, blink);
}
const positions = dustGeometry.attributes.position.array;
for (let i = 0; i < dustCount; i++) {
const base = dustBase[i];
positions[i * 3] = base.x + Math.sin(elapsed * 0.25 + base.phase) * 0.12;
positions[i * 3 + 1] = base.y + Math.sin(elapsed * 0.42 + base.phase) * 0.15;
positions[i * 3 + 2] = base.z + Math.cos(elapsed * 0.2 + base.phase) * 0.1;
}
dustGeometry.attributes.position.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Main loop and resize
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
updateCamera(dt);
updateMovement(dt);
animateCat(dt, elapsed);
cat.updateMatrixWorld(true);
updateWhiskers(elapsed);
renderer.render(scene, camera);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
updateCamera(1);
cat.updateMatrixWorld(true);
updateWhiskers(0);
renderer.render(scene, camera);
requestAnimationFrame(() => {
document.querySelector("#loading").classList.add("hidden");
});
animate();
</script>
</body>
</html>
```