Prompt specification
Build or customize the Three.js 3D scene "Meadow grazing patchwork" 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: Meadow grazing patchwork
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/meadow-grazing-patchwork-x0739
Scene Description Prompt:
Show stylized herbivores grazing bounded meadow paths; a time slider reveals grazed and ungrazed grass heights. One HTML file using Three.js from cdnjs.
Working Three.js Starter (meadow-grazing-patchwork-x0739):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Pasture — A Day of Grazing</title>
<style>
:root {
--ink: #17382f;
--muted: #5d7469;
--panel: rgba(250, 249, 239, 0.9);
--line: rgba(35, 70, 56, 0.16);
--accent: #e5a348;
--shadow: 0 18px 50px rgba(28, 59, 48, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dcebe4;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.brand {
position: fixed;
top: 28px;
left: 30px;
width: min(390px, calc(100vw - 60px));
pointer-events: none;
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.35);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: #567264;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 25px;
height: 2px;
content: "";
background: var(--accent);
border-radius: 3px;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(34px, 4.4vw, 60px);
font-weight: 500;
line-height: 0.95;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 350px;
margin: 13px 0 0;
color: #526b60;
font-size: 14px;
line-height: 1.55;
}
.legend {
position: fixed;
top: 28px;
right: 28px;
width: 245px;
padding: 18px;
border: 1px solid rgba(255, 255, 255, 0.6);
border-radius: 18px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.legend-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 13px;
}
.legend-title {
font-size: 12px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.mini-button {
padding: 6px 9px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(255, 255, 255, 0.58);
color: var(--ink);
font: inherit;
font-size: 10px;
font-weight: 750;
cursor: pointer;
transition: background 160ms ease, transform 160ms ease;
}
.mini-button:hover {
background: white;
transform: translateY(-1px);
}
.gradient {
height: 9px;
overflow: hidden;
border-radius: 999px;
background: linear-gradient(90deg, #b4a15d 0%, #7da34e 45%, #3f7c3b 100%);
box-shadow: inset 0 0 0 1px rgba(25, 55, 40, 0.1);
}
.gradient.heatmap {
background: linear-gradient(90deg, #5b3a91 0%, #ec8c5a 52%, #f4d35e 100%);
}
.legend-labels {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: var(--muted);
font-size: 10px;
}
.stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 16px;
padding-top: 14px;
border-top: 1px solid var(--line);
}
.stat strong {
display: block;
font-family: Georgia, "Times New Roman", serif;
font-size: 22px;
font-weight: 500;
letter-spacing: -0.03em;
}
.stat span {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.timeline {
position: fixed;
left: 50%;
bottom: 24px;
width: min(820px, calc(100vw - 38px));
padding: 15px 18px 13px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 20px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
transform: translateX(-50%);
}
.timeline-top {
display: flex;
align-items: center;
gap: 13px;
}
.play {
flex: 0 0 auto;
display: grid;
width: 43px;
height: 43px;
place-items: center;
border: 0;
border-radius: 50%;
background: var(--ink);
color: #fffdf2;
font-size: 16px;
cursor: pointer;
box-shadow: 0 6px 16px rgba(23, 56, 47, 0.22);
transition: transform 160ms ease, background 160ms ease;
}
.play:hover {
background: #285748;
transform: scale(1.04);
}
.clock-wrap {
flex: 0 0 88px;
}
.clock {
display: block;
font-family: Georgia, "Times New Roman", serif;
font-size: 25px;
line-height: 1;
font-variant-numeric: tabular-nums;
}
.clock-caption {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.slider-wrap {
flex: 1;
min-width: 0;
}
input[type="range"] {
width: 100%;
height: 20px;
margin: 0;
appearance: none;
-webkit-appearance: none;
background: transparent;
cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
height: 5px;
border-radius: 999px;
background:
linear-gradient(
90deg,
#e5a348 0%,
#e5a348 var(--progress, 0%),
rgba(41, 74, 61, 0.15) var(--progress, 0%),
rgba(41, 74, 61, 0.15) 100%
);
}
input[type="range"]::-moz-range-track {
height: 5px;
border-radius: 999px;
background: rgba(41, 74, 61, 0.15);
}
input[type="range"]::-moz-range-progress {
height: 5px;
border-radius: 999px;
background: #e5a348;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
margin-top: -6px;
appearance: none;
-webkit-appearance: none;
border: 3px solid #fffaf0;
border-radius: 50%;
background: #e5a348;
box-shadow: 0 2px 8px rgba(23, 56, 47, 0.28);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #fffaf0;
border-radius: 50%;
background: #e5a348;
box-shadow: 0 2px 8px rgba(23, 56, 47, 0.28);
}
.timeline-bottom {
display: flex;
justify-content: space-between;
margin: 6px 0 0 144px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.04em;
}
.hint {
position: fixed;
right: 28px;
bottom: 29px;
color: rgba(31, 67, 55, 0.72);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.04em;
pointer-events: none;
}
.loading {
position: fixed;
inset: 0;
display: grid;
place-items: center;
background: #dcebe4;
color: var(--ink);
font-family: Georgia, "Times New Roman", serif;
font-size: 20px;
transition: opacity 400ms ease;
pointer-events: none;
}
.loading.hidden {
opacity: 0;
}
@media (max-width: 760px) {
.brand {
top: 19px;
left: 20px;
}
.subtitle {
display: none;
}
.legend {
top: 18px;
right: 18px;
width: 190px;
padding: 13px;
}
.legend-title {
font-size: 10px;
}
.stats {
display: none;
}
.timeline {
bottom: 12px;
width: calc(100vw - 24px);
padding: 12px;
}
.timeline-top {
gap: 10px;
}
.clock-wrap {
flex-basis: 65px;
}
.clock {
font-size: 21px;
}
.timeline-bottom {
margin-left: 116px;
}
.timeline-bottom span:nth-child(even) {
display: none;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<header class="brand">
<div class="eyebrow">Field study 07 · Grazing ecology</div>
<h1>Pasture,<br />in progress.</h1>
<p class="subtitle">
Follow a bounded meadow through one grazing day. Move the timeline to
reveal where the herbivores have shortened the grass canopy.
</p>
</header>
<aside class="legend" aria-label="Grass height legend">
<div class="legend-head">
<div class="legend-title">Grass canopy</div>
<button
id="viewMode"
class="mini-button"
type="button"
aria-pressed="false"
title="Toggle natural colors and height heat map"
>
Natural color
</button>
</div>
<div id="gradient" class="gradient"></div>
<div class="legend-labels">
<span>Grazed · short</span>
<span>Ungrazed · tall</span>
</div>
<div class="stats">
<div class="stat">
<strong id="meanHeight">—</strong>
<span>Mean canopy</span>
</div>
<div class="stat">
<strong id="grazedArea">—</strong>
<span>Grazed cover</span>
</div>
</div>
</aside>
<section class="timeline" aria-label="Grazing timeline">
<div class="timeline-top">
<button
id="play"
class="play"
type="button"
aria-label="Play grazing day"
title="Play grazing day"
>
▶
</button>
<div class="clock-wrap">
<span id="clock" class="clock">06:00</span>
<span class="clock-caption">Meadow time</span>
</div>
<div class="slider-wrap">
<input
id="time"
type="range"
min="0"
max="100"
step="0.1"
value="0"
aria-label="Time of day"
/>
</div>
</div>
<div class="timeline-bottom">
<span>06:00</span>
<span>09:00</span>
<span>12:00</span>
<span>15:00</span>
<span>18:00</span>
</div>
</section>
<div class="hint">Drag to orbit · Shift-drag to pan · Scroll to zoom</div>
<div id="loading" class="loading">Growing the meadow…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Utilities and renderer
// -------------------------------------------------------------------------
const sceneHost = document.getElementById("scene");
const loading = document.getElementById("loading");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdcebe4);
scene.fog = new THREE.Fog(0xdcebe4, 34, 72);
const camera = new THREE.PerspectiveCamera(
39,
innerWidth / innerHeight,
0.1,
140
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D meadow with grazing animals"
);
sceneHost.appendChild(renderer.domElement);
let seed = 1707;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function range(min, max) {
return min + (max - min) * random();
}
function smooth01(value) {
const x = THREE.MathUtils.clamp(value, 0, 1);
return x * x * (3 - 2 * x);
}
const hemi = new THREE.HemisphereLight(0xf4fbf4, 0x56724b, 2.1);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffefd0, 3.15);
sun.position.set(13, 21, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -20;
sun.shadow.camera.right = 20;
sun.shadow.camera.top = 20;
sun.shadow.camera.bottom = -20;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.bias = -0.00045;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9bc9df, 0.65);
fill.position.set(-12, 8, -10);
scene.add(fill);
// -------------------------------------------------------------------------
// Meadow terrain, paths, fence, and vegetation
// -------------------------------------------------------------------------
const meadowMaterial = new THREE.MeshStandardMaterial({
color: 0x719b59,
roughness: 1,
flatShading: true
});
const earthMaterial = new THREE.MeshStandardMaterial({
color: 0x9b8057,
roughness: 1,
flatShading: true
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(160, 160),
new THREE.MeshStandardMaterial({
color: 0x91aa7d,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.22;
ground.receiveShadow = true;
scene.add(ground);
const earthBase = new THREE.Mesh(
new THREE.CylinderGeometry(11.25, 11.8, 1.2, 64),
earthMaterial
);
earthBase.scale.set(1.08, 1, 0.83);
earthBase.position.y = -0.61;
earthBase.receiveShadow = true;
earthBase.castShadow = true;
scene.add(earthBase);
const meadow = new THREE.Mesh(
new THREE.CircleGeometry(10.2, 96),
meadowMaterial
);
meadow.rotation.x = -Math.PI / 2;
meadow.scale.set(1.06, 0.79, 1);
meadow.position.y = 0.012;
meadow.receiveShadow = true;
scene.add(meadow);
function makeClosedRoute(points) {
return new THREE.CatmullRomCurve3(
points.map(([x, z]) => new THREE.Vector3(x, 0, z)),
true,
"catmullrom",
0.28
);
}
function makeOpenRoute(points) {
return new THREE.CatmullRomCurve3(
points.map(([x, z]) => new THREE.Vector3(x, 0, z)),
false,
"catmullrom",
0.3
);
}
const outerLoopPoints = [];
for (let i = 0; i < 18; i++) {
const a = (i / 18) * Math.PI * 2;
outerLoopPoints.push([
Math.cos(a) * 9.05,
Math.sin(a) * 6.68
]);
}
const pathDefinitions = [
{
curve: makeClosedRoute(outerLoopPoints),
width: 1.05
},
{
curve: makeOpenRoute([
[-7.6, 0.8],
[-5.2, -0.2],
[-2.6, 0.45],
[0, -0.25],
[2.7, 0.6],
[5.1, -0.1],
[7.7, 0.85]
]),
width: 0.92
},
{
curve: makeOpenRoute([
[-0.7, -6.7],
[0.25, -4.4],
[-0.25, -2],
[0.65, 0.1],
[-0.15, 2.3],
[0.5, 4.6],
[-0.5, 6.8]
]),
width: 0.9
}
];
function createRibbon(curve, width, y, segments = 150) {
const positions = [];
const uvs = [];
const indices = [];
const tangent = new THREE.Vector3();
const side = new THREE.Vector3();
const point = new THREE.Vector3();
for (let i = 0; i <= segments; i++) {
const t = i / segments;
curve.getPointAt(t, point);
curve.getTangentAt(t, tangent);
side.set(-tangent.z, 0, tangent.x).normalize();
positions.push(
point.x + side.x * width * 0.5,
y,
point.z + side.z * width * 0.5,
point.x - side.x * width * 0.5,
y,
point.z - side.z * width * 0.5
);
uvs.push(0, t * 8, 1, t * 8);
if (i < segments) {
const a = i * 2;
const b = a + 1;
const c = a + 2;
const d = a + 3;
indices.push(a, c, b, b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute("uv", new THREE.Float32BufferAttribute(uvs, 2));
geometry.setIndex(indices);
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
color: 0xd8ba80,
roughness: 1,
side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(geometry, material);
mesh.receiveShadow = true;
scene.add(mesh);
}
const pathSamples = [];
const stoneTransforms = [];
for (const definition of pathDefinitions) {
createRibbon(definition.curve, definition.width, 0.035);
const samples = [];
const point = new THREE.Vector3();
const tangent = new THREE.Vector3();
const side = new THREE.Vector3();
for (let i = 0; i <= 140; i++) {
const t = i / 140;
definition.curve.getPointAt(t, point);
definition.curve.getTangentAt(t, tangent);
side.set(-tangent.z, 0, tangent.x).normalize();
samples.push({
x: point.x,
z: point.z,
width: definition.width
});
if (i % 7 === 0) {
for (const sign of [-1, 1]) {
stoneTransforms.push({
x: point.x + side.x * definition.width * 0.56 * sign,
z: point.z + side.z * definition.width * 0.56 * sign,
y: 0.075,
scale: range(0.7, 1.35),
rotation: range(0, Math.PI * 2),
tint: range(0, 1)
});
}
}
}
pathSamples.push(samples);
}
const stoneGeometry = new THREE.DodecahedronGeometry(0.13, 0);
const stoneMaterial = new THREE.MeshStandardMaterial({
color: 0xb8ad92,
roughness: 1,
flatShading: true
});
const stones = new THREE.InstancedMesh(
stoneGeometry,
stoneMaterial,
stoneTransforms.length
);
const dummy = new THREE.Object3D();
const stoneColor = new THREE.Color();
stoneTransforms.forEach((stone, index) => {
dummy.position.set(stone.x, stone.y, stone.z);
dummy.rotation.set(0.15, stone.rotation, 0.08);
dummy.scale.setScalar(stone.scale);
dummy.updateMatrix();
stones.setMatrixAt(index, dummy.matrix);
stoneColor
.set(0xc5b99b)
.lerp(new THREE.Color(0x8f927f), stone.tint * 0.55);
stones.setColorAt(index, stoneColor);
});
stones.castShadow = true;
stones.receiveShadow = true;
scene.add(stones);
// A continuous rustic fence makes the meadow explicitly bounded.
const woodMaterial = new THREE.MeshStandardMaterial({
color: 0x8e6542,
roughness: 0.95,
flatShading: true
});
const postGeometry = new THREE.CylinderGeometry(0.09, 0.12, 1.25, 7);
const railGeometry = new THREE.CylinderGeometry(1, 1, 1, 7);
const fencePoints = [];
for (let i = 0; i < 40; i++) {
const a = (i / 40) * Math.PI * 2;
fencePoints.push(
new THREE.Vector3(Math.cos(a) * 11.05, 0, Math.sin(a) * 8.48)
);
}
const up = new THREE.Vector3(0, 1, 0);
function addBeam(a, b, radius) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const beam = new THREE.Mesh(railGeometry, woodMaterial);
beam.position.copy(a).add(b).multiplyScalar(0.5);
beam.scale.set(radius, length, radius);
beam.quaternion.setFromUnitVectors(up, direction.normalize());
beam.castShadow = true;
beam.receiveShadow = true;
scene.add(beam);
}
fencePoints.forEach((point, index) => {
const post = new THREE.Mesh(postGeometry, woodMaterial);
post.position.set(point.x, 0.625, point.z);
post.rotation.y = range(0, Math.PI);
post.castShadow = true;
post.receiveShadow = true;
scene.add(post);
const next = fencePoints[(index + 1) % fencePoints.length];
addBeam(
new THREE.Vector3(point.x, 0.52, point.z),
new THREE.Vector3(next.x, 0.52, next.z),
0.045
);
addBeam(
new THREE.Vector3(point.x, 0.96, point.z),
new THREE.Vector3(next.x, 0.96, next.z),
0.04
);
});
// Low-poly trees outside the fence add scale and frame the meadow.
const trunkGeometry = new THREE.CylinderGeometry(0.14, 0.2, 1.4, 7);
const treeGeometry = new THREE.ConeGeometry(1, 2.4, 8);
const trunkMaterial = new THREE.MeshStandardMaterial({
color: 0x76543c,
roughness: 1,
flatShading: true
});
const treeMaterials = [
new THREE.MeshStandardMaterial({
color: 0x426e4d,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x557e50,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x6b8b55,
roughness: 1,
flatShading: true
})
];
for (let i = 0; i < 18; i++) {
const angle = (i / 18) * Math.PI * 2 + range(-0.08, 0.08);
const radius = range(12.3, 14.2);
const tree = new THREE.Group();
tree.position.set(
Math.cos(angle) * radius,
-1.15,
Math.sin(angle) * radius * 0.82
);
tree.scale.setScalar(range(0.8, 1.5));
const trunk = new THREE.Mesh(trunkGeometry, trunkMaterial);
trunk.position.y = 0.7;
trunk.castShadow = true;
tree.add(trunk);
for (let layer = 0; layer < 3; layer++) {
const crown = new THREE.Mesh(
treeGeometry,
treeMaterials[(i + layer) % treeMaterials.length]
);
crown.position.y = 1.7 + layer * 0.76;
crown.scale.setScalar(1 - layer * 0.17);
crown.rotation.y = layer * 0.45;
crown.castShadow = true;
tree.add(crown);
}
scene.add(tree);
}
// A few soft, distant clouds.
const cloudMaterial = new THREE.MeshStandardMaterial({
color: 0xf7fbf5,
roughness: 1,
flatShading: true
});
const cloudGeometry = new THREE.SphereGeometry(1, 10, 7);
for (let i = 0; i < 5; i++) {
const cloud = new THREE.Group();
cloud.position.set(-15 + i * 8, range(10, 14), -22 - range(0, 8));
for (let j = 0; j < 4; j++) {
const puff = new THREE.Mesh(cloudGeometry, cloudMaterial);
puff.position.set(j * 1.25, Math.sin(j) * 0.35, range(-0.4, 0.4));
puff.scale.set(range(1.1, 1.8), range(0.55, 0.9), 0.8);
cloud.add(puff);
}
scene.add(cloud);
}
// -------------------------------------------------------------------------
// Grass placement and tuft geometry
// -------------------------------------------------------------------------
function distanceToPathsSquared(x, z) {
let minimum = Infinity;
for (const samples of pathSamples) {
for (let i = 0; i < samples.length - 1; i++) {
const a = samples[i];
const b = samples[i + 1];
const dx = b.x - a.x;
const dz = b.z - a.z;
const lengthSquared = dx * dx + dz * dz;
const t = lengthSquared
? THREE.MathUtils.clamp(
((x - a.x) * dx + (z - a.z) * dz) / lengthSquared,
0,
1
)
: 0;
const px = a.x + dx * t;
const pz = a.z + dz * t;
const distanceSquared = (x - px) ** 2 + (z - pz) ** 2;
const clearance = (a.width + b.width) * 0.28;
minimum = Math.min(
minimum,
Math.max(0, distanceSquared - clearance * clearance)
);
}
}
return minimum;
}
const GRASS_COUNT = 2200;
const grassPositions = [];
const grassBaseHeights = new Float32Array(GRASS_COUNT);
const grassWidths = new Float32Array(GRASS_COUNT);
const grassRotations = new Float32Array(GRASS_COUNT);
while (grassPositions.length < GRASS_COUNT) {
const angle = range(0, Math.PI * 2);
const radius = Math.sqrt(random());
const x = Math.cos(angle) * radius * 10.15;
const z = Math.sin(angle) * radius * 7.75;
if (distanceToPathsSquared(x, z) < 0.035) continue;
const index = grassPositions.length / 3;
grassPositions.push(x, z);
grassBaseHeights[index] = range(0.42, 0.93);
grassWidths[index] = range(1.05, 2.25);
grassRotations[index] = range(0, Math.PI * 2);
}
function createTuftGeometry() {
const positions = [];
const indices = [];
function addBlade(angle, width) {
const blade = [
[-width, 0, 0],
[width, 0, 0],
[-width * 0.62, 0.56, 0.025],
[width * 0.62, 0.56, 0.025],
[width * 0.08, 1, 0.085]
];
const base = positions.length / 3;
const cosine = Math.cos(angle);
const sine = Math.sin(angle);
for (const [x, y, z] of blade) {
positions.push(
x * cosine + z * sine,
y,
-x * sine + z * cosine
);
}
indices.push(
base,
base + 1,
base + 2,
base + 1,
base + 3,
base + 2,
base + 2,
base + 3,
base + 4
);
}
addBlade(0, 0.075);
addBlade(Math.PI / 3, 0.07);
addBlade((Math.PI * 2) / 3, 0.065);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
const grassMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
side: THREE.DoubleSide
});
const grass = new THREE.InstancedMesh(
createTuftGeometry(),
grassMaterial,
GRASS_COUNT
);
grass.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
grass.receiveShadow = true;
grass.castShadow = false;
scene.add(grass);
// -------------------------------------------------------------------------
// Stylized herbivores
// -------------------------------------------------------------------------
const animals = [];
function material(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.9,
flatShading: true,
...extra
});
}
function makeLeg(x, z, mat, length = 0.42, radius = 0.07) {
const leg = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius * 1.12, length, 7),
mat
);
leg.position.set(x, length * 0.5, z);
leg.castShadow = true;
return leg;
}
function makeSheep() {
const group = new THREE.Group();
const wool = material(0xf4efe2);
const dark = material(0x4d514c);
const body = new THREE.Mesh(
new THREE.SphereGeometry(0.5, 12, 8),
wool
);
body.scale.set(0.92, 0.72, 1.25);
body.position.y = 0.65;
body.castShadow = true;
group.add(body);
const tuftPositions = [
[-0.3, 0.82, -0.42],
[0.3, 0.85, -0.35],
[-0.35, 0.75, 0.15],
[0.36, 0.78, 0.12],
[0, 0.9, 0.38]
];
for (const [x, y, z] of tuftPositions) {
const puff = new THREE.Mesh(
new THREE.SphereGeometry(0.27, 9, 7),
wool
);
puff.position.set(x, y, z);
puff.castShadow = true;
group.add(puff);
}
group.add(
makeLeg(-0.29, -0.4, dark),
makeLeg(0.29, -0.4, dark),
makeLeg(-0.29, 0.42, dark),
makeLeg(0.29, 0.42, dark)
);
const headPivot = new THREE.Group();
headPivot.position.set(0, 0.72, 0.57);
group.add(headPivot);
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.3, 10, 8),
dark
);
head.position.set(0, -0.03, 0.27);
head.scale.set(0.72, 1.12, 0.82);
head.castShadow = true;
headPivot.add(head);
for (const sign of [-1, 1]) {
const ear = new THREE.Mesh(
new THREE.SphereGeometry(0.13, 7, 5),
dark
);
ear.scale.set(1.6, 0.48, 0.65);
ear.position.set(sign * 0.25, 0.09, 0.18);
headPivot.add(ear);
}
return { group, headPivot };
}
function makeCow() {
const group = new THREE.Group();
const hide = material(0xe9e3d4);
const patch = material(0x3b403d);
const pink = material(0xb98378);
const body = new THREE.Mesh(
new THREE.SphereGeometry(0.58, 13, 9),
hide
);
body.scale.set(0.95, 0.73, 1.3);
body.position.y = 0.75;
body.castShadow = true;
group.add(body);
const patches = [
[-0.5, 0.91, 0.12, 0.19, 0.47, 0.62],
[0.5, 0.74, -0.24, 0.18, 0.4, 0.55],
[0.03, 1.08, 0.35, 0.5, 0.12, 0.42]
];
for (const [x, y, z, sx, sy, sz] of patches) {
const spot = new THREE.Mesh(
new THREE.SphereGeometry(0.4, 9, 7),
patch
);
spot.position.set(x, y, z);
spot.scale.set(sx, sy, sz);
group.add(spot);
}
group.add(
makeLeg(-0.36, -0.48, hide, 0.52, 0.09),
makeLeg(0.36, -0.48, hide, 0.52, 0.09),
makeLeg(-0.36, 0.52, hide, 0.52, 0.09),
makeLeg(0.36, 0.52, hide, 0.52, 0.09)
);
const headPivot = new THREE.Group();
headPivot.position.set(0, 0.85, 0.82);
group.add(headPivot);
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.38, 10, 8),
pink
);
head.scale.set(0.8, 1, 0.86);
head.position.set(0, -0.08, 0.23);
head.castShadow = true;
headPivot.add(head);
for (const sign of [-1, 1]) {
const ear = new THREE.Mesh(
new THREE.ConeGeometry(0.14, 0.38, 6),
hide
);
ear.rotation.z = sign * Math.PI * 0.5;
ear.position.set(sign * 0.34, 0.06, 0.1);
headPivot.add(ear);
const horn = new THREE.Mesh(
new THREE.ConeGeometry(0.065, 0.4, 6),
material(0xc8b47b)
);
horn.position.set(sign * 0.22, 0.35, 0.12);
horn.rotation.z = sign * -0.35;
headPivot.add(horn);
}
return { group, headPivot };
}
function makeDeer() {
const group = new THREE.Group();
const fur = material(0xb47c48);
const light = material(0xd8ae72);
const body = new THREE.Mesh(
new THREE.SphereGeometry(0.46, 12, 8),
fur
);
body.scale.set(0.78, 0.82, 1.18);
body.position.y = 0.92;
body.castShadow = true;
group.add(body);
group.add(
makeLeg(-0.24, -0.38, fur, 0.66, 0.055),
makeLeg(0.24, -0.38, fur, 0.66, 0.055),
makeLeg(-0.24, 0.4, fur, 0.66, 0.055),
makeLeg(0.24, 0.4, fur, 0.66, 0.055)
);
const headPivot = new THREE.Group();
headPivot.position.set(0, 1.02, 0.58);
group.add(headPivot);
const neck = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.17, 0.65, 8),
fur
);
neck.position.set(0, 0.22, 0.1);
neck.rotation.x = -0.32;
headPivot.add(neck);
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.22, 10, 7),
light
);
head.scale.set(0.72, 0.8, 1.35);
head.position.set(0, 0.52, 0.29);
head.castShadow = true;
headPivot.add(head);
for (const sign of [-1, 1]) {
const ear = new THREE.Mesh(
new THREE.ConeGeometry(0.09, 0.28, 5),
fur
);
ear.rotation.z = sign * Math.PI * 0.5;
ear.position.set(sign * 0.2, 0.6, 0.25);
headPivot.add(ear);
}
const antlerMaterial = material(0x75543b);
for (const sign of [-1, 1]) {
const antler = new THREE.Mesh(
new THREE.CylinderGeometry(0.025, 0.045, 0.65, 6),
antlerMaterial
);
antler.position.set(sign * 0.13, 0.98, 0.2);
antler.rotation.z = sign * -0.38;
headPivot.add(antler);
for (let branch = 0; branch < 2; branch++) {
const twig = new THREE.Mesh(
new THREE.CylinderGeometry(0.017, 0.027, 0.35, 5),
antlerMaterial
);
twig.position.set(
sign * (0.21 + branch * 0.03),
1.05 + branch * 0.16,
0.19
);
twig.rotation.z = sign * -0.8;
headPivot.add(twig);
}
}
return { group, headPivot };
}
const sheepRoute = makeClosedRoute([
[-5.2, -2.8],
[-4.2, 1.0],
[-1.4, 3.5],
[2.0, 2.8],
[4.6, 0.3],
[3.7, -3.0],
[0.2, -4.4],
[-3.0, -3.6]
]);
const cowRoute = makeClosedRoute([
[-6.8, -1.1],
[-5.7, 3.2],
[-1.5, 4.4],
[3.2, 3.2],
[6.6, 0.5],
[5.3, -3.8],
[0.7, -5.2],
[-4.6, -4.3]
]);
const deerRoute = makeClosedRoute([
[-6.5, 0.2],
[-4.2, -3.9],
[0.7, -5.0],
[5.6, -2.5],
[6.4, 1.7],
[2.1, 4.7],
[-3.1, 4.3]
]);
function addAnimal(type, route, phase, speed, scale = 1) {
const model =
type === "sheep"
? makeSheep()
: type === "cow"
? makeCow()
: makeDeer();
model.group.scale.setScalar(scale);
model.group.traverse((child) => {
if (child.isMesh) child.castShadow = true;
});
scene.add(model.group);
animals.push({
type,
route,
phase,
speed,
group: model.group,
headPivot: model.headPivot
});
}
for (let i = 0; i < 5; i++) {
addAnimal(
"sheep",
sheepRoute,
i / 5 + range(-0.045, 0.045),
range(0.78, 0.98),
range(0.85, 1.03)
);
}
for (let i = 0; i < 3; i++) {
addAnimal(
"cow",
cowRoute,
i / 3 + range(-0.06, 0.06),
range(0.52, 0.66),
range(0.92, 1.06)
);
}
for (let i = 0; i < 2; i++) {
addAnimal(
"deer",
deerRoute,
i / 2 + range(-0.08, 0.08),
range(0.9, 1.08),
range(0.9, 1.02)
);
}
// -------------------------------------------------------------------------
// Grazing simulation
// -------------------------------------------------------------------------
function animalState(animal, time) {
const u = THREE.MathUtils.euclideanModulo(animal.phase + time * animal.speed, 1);
const position = animal.route.getPointAt(u);
const tangent = animal.route.getTangentAt(u).normalize();
const wave = Math.sin(
time * Math.PI * 18 + animal.phase * Math.PI * 2
);
const down = smooth01((wave + 0.28) / 0.9);
return { position, tangent, down };
}
const FRAME_COUNT = 145;
const heightFrames = new Float32Array(FRAME_COUNT * GRASS_COUNT);
const pressure = new Float32Array(GRASS_COUNT);
for (let frame = 0; frame < FRAME_COUNT; frame++) {
const time = frame / (FRAME_COUNT - 1);
for (const animal of animals) {
const state = animalState(animal, time);
if (state.down < 0.12) continue;
const radius = animal.type === "cow" ? 1.28 : 1.0;
const radiusSquared = radius * radius;
const rate = animal.type === "cow" ? 0.018 : 0.023;
for (let i = 0; i < GRASS_COUNT; i++) {
const dx = grassPositions[i * 3] - state.position.x;
const dz = grassPositions[i * 3 + 1] - state.position.z;
const distanceSquared = dx * dx + dz * dz;
if (distanceSquared < radiusSquared) {
const falloff = 1 - Math.sqrt(distanceSquared) / radius;
pressure[i] += falloff * state.down * rate;
}
}
}
const offset = frame * GRASS_COUNT;
for (let i = 0; i < GRASS_COUNT; i++) {
const remaining = Math.max(0.12, 1 - pressure[i]);
heightFrames[offset + i] = grassBaseHeights[i] * remaining;
}
}
const lowNatural = new THREE.Color(0xb4a15d);
const midNatural = new THREE.Color(0x7da34e);
const highNatural = new THREE.Color(0x3f7c3b);
const lowHeat = new THREE.Color(0x5b3a91);
const midHeat = new THREE.Color(0xec8c5a);
const highHeat = new THREE.Color(0xf4d35e);
const workingColor = new THREE.Color();
let heatMap = false;
let currentTime = 0;
let playing = false;
function colorForHeight(remaining, target) {
if (heatMap) {
if (remaining < 0.52) {
target.copy(lowHeat).lerp(midHeat, remaining / 0.52);
} else {
target.copy(midHeat).lerp(
highHeat,
(remaining - 0.52) / 0.48
);
}
} else {
if (remaining < 0.45) {
target.copy(lowNatural).lerp(midNatural, remaining / 0.45);
} else {
target.copy(midNatural).lerp(
highNatural,
(remaining - 0.45) / 0.55
);
}
}
}
function updateAnimalPose(animal, time) {
const state = animalState(animal, time);
animal.group.position.set(
state.position.x,
0.035 + Math.sin(time * 220 + animal.phase * 9) * 0.006,
state.position.z
);
const target = state.position.clone().add(state.tangent);
animal.group.lookAt(target);
const grazingAngle =
animal.type === "deer" ? 0.83 : 0.98;
animal.headPivot.rotation.x =
0.08 + state.down * grazingAngle;
}
function applyTime(time) {
const scaled = time * (FRAME_COUNT - 1);
const frame = Math.min(FRAME_COUNT - 1, Math.floor(scaled));
const nextFrame = Math.min(FRAME_COUNT - 1, frame + 1);
const mix = scaled - frame;
const offset = frame * GRASS_COUNT;
const nextOffset = nextFrame * GRASS_COUNT;
let heightSum = 0;
let grazedCount = 0;
for (let i = 0; i < GRASS_COUNT; i++) {
const height = THREE.MathUtils.lerp(
heightFrames[offset + i],
heightFrames[nextOffset + i],
mix
);
const remaining = height / grassBaseHeights[i];
heightSum += height;
if (remaining < 0.5) grazedCount++;
dummy.position.set(
grassPositions[i * 3],
0.043,
grassPositions[i * 3 + 1]
);
dummy.rotation.set(0, grassRotations[i], 0);
dummy.scale.set(
grassWidths[i],
Math.max(0.035, height),
grassWidths[i]
);
dummy.updateMatrix();
grass.setMatrixAt(i, dummy.matrix);
colorForHeight(remaining, workingColor);
grass.setColorAt(i, workingColor);
}
grass.instanceMatrix.needsUpdate = true;
if (grass.instanceColor) grass.instanceColor.needsUpdate = true;
for (const animal of animals) {
updateAnimalPose(animal, time);
}
const minutes = 360 + time * 720;
const hour = Math.floor(minutes / 60);
const minute = Math.floor(minutes % 60);
document.getElementById("clock").textContent =
`${String(hour).padStart(2, "0")}:${String(minute).padStart(2, "0")}`;
const meanCentimeters = Math.round((heightSum / GRASS_COUNT) * 100);
const grazedPercent = Math.round((grazedCount / GRASS_COUNT) * 100);
document.getElementById("meanHeight").textContent =
`${meanCentimeters} cm`;
document.getElementById("grazedArea").textContent =
`${grazedPercent}%`;
}
applyTime(0);
grass.computeBoundingSphere();
// -------------------------------------------------------------------------
// Direct camera controls — no addons or example modules
// -------------------------------------------------------------------------
const cameraState = {
theta: 0.67,
phi: 0.93,
radius: 27,
target: new THREE.Vector3(0, 0.45, 0)
};
function updateCamera() {
const offset = new THREE.Vector3().setFromSphericalCoords(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.copy(cameraState.target).add(offset);
camera.lookAt(cameraState.target);
camera.updateMatrixWorld();
}
function resetCamera() {
cameraState.theta = 0.67;
cameraState.phi = 0.93;
cameraState.radius = 27;
cameraState.target.set(0, 0.45, 0);
updateCamera();
}
updateCamera();
let dragging = false;
let panMode = false;
let previousX = 0;
let previousY = 0;
let activePointer = null;
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
panMode = event.shiftKey || event.button === 2;
previousX = event.clientX;
previousY = event.clientY;
activePointer = event.pointerId;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== activePointer) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
if (panMode) {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
const right = new THREE.Vector3()
.crossVectors(forward, up)
.normalize();
const screenUp = new THREE.Vector3()
.crossVectors(right, forward)
.normalize();
const scale = cameraState.radius * 0.0011;
cameraState.target.addScaledVector(right, -dx * scale);
cameraState.target.addScaledVector(screenUp, dy * scale);
} else {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.27,
1.38
);
}
updateCamera();
});
function endDrag(event) {
if (event.pointerId !== activePointer) return;
dragging = false;
panMode = false;
activePointer = null;
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
14,
43
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// -------------------------------------------------------------------------
// UI and animation
// -------------------------------------------------------------------------
const timeInput = document.getElementById("time");
const playButton = document.getElementById("play");
const viewModeButton = document.getElementById("viewMode");
const gradient = document.getElementById("gradient");
function updateRangeProgress() {
const progress = Number(timeInput.value);
timeInput.style.setProperty("--progress", `${progress}%`);
}
function updatePlayButton() {
playButton.textContent = playing ? "Ⅱ" : "▶";
playButton.setAttribute(
"aria-label",
playing ? "Pause grazing day" : "Play grazing day"
);
playButton.title = playing ? "Pause grazing day" : "Play grazing day";
}
timeInput.addEventListener("input", () => {
currentTime = Number(timeInput.value) / 100;
playing = false;
updatePlayButton();
updateRangeProgress();
applyTime(currentTime);
});
playButton.addEventListener("click", () => {
playing = !playing;
updatePlayButton();
});
viewModeButton.addEventListener("click", () => {
heatMap = !heatMap;
gradient.classList.toggle("heatmap", heatMap);
viewModeButton.textContent = heatMap ? "Height map" : "Natural color";
viewModeButton.setAttribute("aria-pressed", String(heatMap));
applyTime(currentTime);
});
updateRangeProgress();
let lastTime = performance.now();
let lastSimulationUpdate = 0;
function animate(now) {
const delta = Math.min(0.05, (now - lastTime) / 1000);
lastTime = now;
if (playing && now - lastSimulationUpdate > 30) {
currentTime = (currentTime + delta / 34) % 1;
timeInput.value = String(currentTime * 100);
updateRangeProgress();
applyTime(currentTime);
lastSimulationUpdate = now;
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
playing = !playing;
updatePlayButton();
}
if (event.key.toLowerCase() === "r") {
resetCamera();
}
});
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 450);
});
</script>
</body>
</html>
```