Mike0021's picture
Publish Phase A cleared procedural Three.js 3D scenes dataset (26 rows with validated GLBs, renders, code, and scrubbed traces)
59c7fd7 verified
Raw History Blame Contribute Delete
16.3 kB
import * as THREE from 'three';
import { grassSet, grassClumpSet, dirtSet, cloudSet, fbm, rng } from './textures.js';
const smooth = (a, b, x) => {
const t = Math.min(Math.max((x - a) / (b - a), 0), 1);
return t * t * (3 - 2 * t);
};
export function makeGroundHeight() {
return function groundH(x, z) {
const r = Math.hypot(x, z);
const t = smooth(4.2, 17.5, r);
let h = 1.75 * (1 - t);
if (r > 5.2) {
h += (fbm(x * 0.055 + 11, z * 0.055 + 4, 4) - 0.5) * 0.55 * smooth(5.2, 14, r);
h += (fbm(x * 0.22 + 3, z * 0.22, 2) - 0.5) * 0.10 * smooth(5.5, 11, r);
}
return h;
};
}
export function buildEnvironment(scene, renderer, groundH) {
const sunDir = new THREE.Vector3(-0.55, 0.62, 0.56).normalize();
/* ------------------------- sky dome ------------------------- */
const skyMat = new THREE.ShaderMaterial({
side: THREE.BackSide, depthWrite: false, fog: false,
uniforms: {
top: { value: new THREE.Color(0x3f6f9e) },
mid: { value: new THREE.Color(0x9dc0d6) },
bottom: { value: new THREE.Color(0xf1ddb9) },
sunDir: { value: sunDir.clone() },
sunCol: { value: new THREE.Color(0xffe7c0) }
},
vertexShader: `
varying vec3 vDir;
void main(){
vDir = normalize((modelMatrix * vec4(position,1.0)).xyz);
gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
}`,
fragmentShader: `
varying vec3 vDir;
uniform vec3 top, mid, bottom, sunDir, sunCol;
void main(){
float h = clamp(vDir.y*0.5+0.5, 0.0, 1.0);
vec3 col = mix(bottom, mid, smoothstep(0.46, 0.62, h));
col = mix(col, top, smoothstep(0.60, 0.95, h));
float d = max(dot(normalize(vDir), normalize(sunDir)), 0.0);
col += sunCol * (pow(d, 800.0)*4.0 + pow(d, 24.0)*0.22 + pow(d, 4.0)*0.06);
gl_FragColor = vec4(col, 1.0);
}`
});
const sky = new THREE.Mesh(new THREE.SphereGeometry(420, 40, 24), skyMat);
sky.name = 'sky';
scene.add(sky);
scene.fog = new THREE.Fog(0xc8d3cf, 55, 240);
/* ------------------------- ground ------------------------- */
const GSIZE = 150, GSEG = 220;
const gGeo = new THREE.PlaneGeometry(GSIZE, GSIZE, GSEG, GSEG);
gGeo.rotateX(-Math.PI / 2);
const pos = gGeo.attributes.position;
const colors = [];
const cTmp = new THREE.Color();
for (let i = 0; i < pos.count; i++) {
const x = pos.getX(i), z = pos.getZ(i);
pos.setY(i, groundH(x, z));
const n = fbm(x * 0.06 + 5, z * 0.06 + 9, 3);
const n2 = fbm(x * 0.4 + 1, z * 0.4 + 2, 2);
cTmp.setHSL(0.205 + n * 0.05, 0.30 + n2 * 0.18, 0.44 + n * 0.24);
colors.push(cTmp.r, cTmp.g, cTmp.b);
}
gGeo.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3));
gGeo.computeVertexNormals();
const grass = grassSet();
grass.map.repeat.set(30, 30);
const groundMat = new THREE.MeshStandardMaterial({
map: grass.map, vertexColors: true, roughness: 0.97, metalness: 0.0,
color: 0xcfd6b4
});
const ground = new THREE.Mesh(gGeo, groundMat);
ground.name = 'ground';
ground.receiveShadow = true;
scene.add(ground);
/* ------------------------- dirt path ------------------------- */
const pathPts = [
[-34, 16], [-24, 12.5], [-15.5, 9.0], [-9.0, 4.8], [-6.0, -1.0],
[-7.0, -8.0], [-11.5, -15], [-19, -21], [-30, -25]
].map(([x, z]) => new THREE.Vector3(x, 0, z));
const pathCurve = new THREE.CatmullRomCurve3(pathPts);
const pathSamples = [];
for (let i = 0; i <= 120; i++) pathSamples.push(pathCurve.getPoint(i / 120));
const distToPath = (x, z) => {
let d = 1e9;
for (let i = 0; i < pathSamples.length; i++) {
const p = pathSamples[i];
const dd = (p.x - x) * (p.x - x) + (p.z - z) * (p.z - z);
if (dd < d) d = dd;
}
return Math.sqrt(d);
};
const N = 220, halfW = 1.35;
const pPos = [], pUV = [], pIdx = [];
for (let i = 0; i <= N; i++) {
const t = i / N;
const p = pathCurve.getPoint(t);
const tan = pathCurve.getTangent(t);
const side = new THREE.Vector3(-tan.z, 0, tan.x).normalize();
const w = halfW * (0.82 + 0.30 * fbm(t * 9, 3.7, 2));
for (const s of [-1, 1]) {
const q = p.clone().add(side.clone().multiplyScalar(w * s));
const y = groundH(q.x, q.z) + 0.13 + 0.012 * Math.sin(t * 60 + s);
pPos.push(q.x, y, q.z);
pUV.push(s === -1 ? 0 : 1, t * 18);
}
if (i < N) {
const a = i * 2;
pIdx.push(a, a + 1, a + 2, a + 1, a + 3, a + 2);
}
}
const pGeo = new THREE.BufferGeometry();
pGeo.setAttribute('position', new THREE.Float32BufferAttribute(pPos, 3));
pGeo.setAttribute('uv', new THREE.Float32BufferAttribute(pUV, 2));
pGeo.setIndex(pIdx);
pGeo.computeVertexNormals();
const dirt = dirtSet();
const pathMat = new THREE.MeshStandardMaterial({
map: dirt.map, roughness: 1, metalness: 0, color: 0xf5e3c0,
polygonOffset: true, polygonOffsetFactor: -2, polygonOffsetUnits: -2
});
const path = new THREE.Mesh(pGeo, pathMat);
path.name = 'path';
path.receiveShadow = true;
scene.add(path);
/* ------------------------- grass tufts ------------------------- */
function tuftGeometry() {
const pos = [], nor = [], idx = [];
const blades = 5;
for (let b = 0; b < blades; b++) {
const a = b * Math.PI * 2 / blades + b * 0.7;
const dx = Math.cos(a), dz = Math.sin(a);
const h = 0.42 + 0.30 * ((b * 37) % 10) / 10;
const w = 0.032;
const base = b * 3;
pos.push(-w * dz, 0, w * dx);
pos.push(w * dz, 0, -w * dx);
pos.push(dx * 0.16, h, dz * 0.16);
for (let k = 0; k < 3; k++) nor.push(0, 0.7, 0);
idx.push(base, base + 1, base + 2);
}
const g = new THREE.BufferGeometry();
g.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3));
g.setAttribute('normal', new THREE.Float32BufferAttribute(nor, 3));
g.setIndex(idx);
return g;
}
const tuftMat = new THREE.MeshStandardMaterial({ color: 0x93a571, roughness: 1, side: THREE.DoubleSide });
const COUNT = 4400;
const tufts = new THREE.InstancedMesh(tuftGeometry(), tuftMat, COUNT);
tufts.castShadow = false; tufts.receiveShadow = true;
const dum = new THREE.Object3D();
const rnd = rng(404);
const col = new THREE.Color();
for (let i = 0; i < COUNT; i++) {
let x, z, r, tries = 0;
do {
r = 4.2 + Math.pow(rnd(), 0.62) * 26;
const a = rnd() * Math.PI * 2;
x = Math.cos(a) * r; z = Math.sin(a) * r;
tries++;
} while (tries < 8 && (fbm(x * 0.5, z * 0.5, 2) < 0.18 || distToPath(x, z) < 1.85));
const y = groundH(x, z);
const s = 0.32 + rnd() * 0.5;
dum.position.set(x, y, z);
dum.rotation.set(0, rnd() * 6.28, (rnd() - 0.5) * 0.15);
dum.scale.set(s, s * (0.8 + rnd() * 0.7), s);
dum.updateMatrix();
tufts.setMatrixAt(i, dum.matrix);
col.setHSL(0.23 + rnd() * 0.05, 0.48 + rnd() * 0.18, 0.085 + rnd() * 0.085);
tufts.setColorAt(i, col);
}
tufts.instanceMatrix.needsUpdate = true;
if (tufts.instanceColor) tufts.instanceColor.needsUpdate = true;
scene.add(tufts);
// layered grass clump cards near the mound
const clumpTex = grassClumpSet();
function clumpGeometry() {
const g = new THREE.BufferGeometry();
const p = [], n = [], u = [], ix = [];
const addQuad = (rot, off) => {
const cs = Math.cos(rot), sn = Math.sin(rot);
const w = 0.5;
const pts = [[-w, 0], [w, 0], [w, 1], [-w, 1]];
const base = off;
for (const [px, py] of pts) {
p.push(px * cs, py, px * sn);
n.push(0, 1, 0);
u.push((px / w + 1) * 0.5, py);
}
ix.push(base, base + 1, base + 2, base, base + 2, base + 3);
};
addQuad(0, 0); addQuad(Math.PI / 2, 4);
g.setAttribute('position', new THREE.Float32BufferAttribute(p, 3));
g.setAttribute('normal', new THREE.Float32BufferAttribute(n, 3));
g.setAttribute('uv', new THREE.Float32BufferAttribute(u, 2));
g.setIndex(ix);
return g;
}
const clumpMat = new THREE.MeshStandardMaterial({
map: clumpTex, alphaTest: 0.42, side: THREE.DoubleSide,
roughness: 1, metalness: 0, color: 0xffffff, emissive: 0x14200c, emissiveIntensity: 0.25
});
const CLUMPS = 1500;
const clumps = new THREE.InstancedMesh(clumpGeometry(), clumpMat, CLUMPS);
clumps.castShadow = false; clumps.receiveShadow = true;
for (let i = 0; i < CLUMPS; i++) {
let x, z, r;
do {
r = 4.0 + Math.pow(rnd(), 0.8) * 18;
const a = rnd() * Math.PI * 2;
x = Math.cos(a) * r; z = Math.sin(a) * r;
} while (distToPath(x, z) < 1.75);
const y = groundH(x, z) + 0.02;
const s = 0.28 + rnd() * 0.36;
dum.position.set(x, y, z);
dum.rotation.set(0, rnd() * 6.28, 0);
dum.scale.set(s * (0.8 + rnd() * 0.6), s * (0.7 + rnd() * 0.55), s);
dum.updateMatrix();
clumps.setMatrixAt(i, dum.matrix);
col.setHSL(0.23 + rnd() * 0.05, 0.28 + rnd() * 0.16, 0.48 + rnd() * 0.22);
clumps.setColorAt(i, col);
}
clumps.instanceMatrix.needsUpdate = true;
if (clumps.instanceColor) clumps.instanceColor.needsUpdate = true;
scene.add(clumps);
// wildflowers
const flowerGeo = new THREE.IcosahedronGeometry(0.045, 0);
const flowers = new THREE.InstancedMesh(flowerGeo, new THREE.MeshStandardMaterial({ roughness: 0.8, emissive: 0x222211, emissiveIntensity: 0.2 }), 300);
for (let i = 0; i < 300; i++) {
let r, a, x, z;
do { r = 4.6 + rnd() * 10; a = rnd() * Math.PI * 2; x = Math.cos(a) * r; z = Math.sin(a) * r; } while (distToPath(x, z) < 1.8);
dum.position.set(x, groundH(x, z) + 0.18 + rnd() * 0.2, z);
dum.rotation.set(0, rnd() * 6.28, 0);
dum.scale.setScalar(0.36 + rnd() * 0.55);
dum.updateMatrix(); flowers.setMatrixAt(i, dum.matrix);
col.setHSL(rnd() < 0.65 ? 0.13 : 0.0, 0.68, 0.55 + rnd() * 0.2);
flowers.setColorAt(i, col);
}
flowers.instanceMatrix.needsUpdate = true;
scene.add(flowers);
/* ------------------------- rocks & cobbles ------------------------- */
const rockGeo = new THREE.DodecahedronGeometry(0.22, 0);
// give the stone a lumpy silhouette
{
const p = rockGeo.attributes.position;
for (let i = 0; i < p.count; i++) {
const f = 0.82 + hashish(p.getX(i), p.getY(i), p.getZ(i)) * 0.4;
p.setXYZ(i, p.getX(i) * f, p.getY(i) * f * 0.6, p.getZ(i) * f);
}
rockGeo.computeVertexNormals();
}
function hashish(x, y, z) {
const n = Math.sin(x * 91.7 + y * 43.1 + z * 17.3) * 43758.5;
return n - Math.floor(n);
}
const rockMat = new THREE.MeshStandardMaterial({ color: 0x6d6559, roughness: 0.95, flatShading: true });
const rocks = new THREE.InstancedMesh(rockGeo, rockMat, 360);
const rnd2 = rng(9001);
for (let i = 0; i < 360; i++) {
let x, z;
if (i < 320) {
// double row of cobbles edging the dirt path
const sideIdx = i < 160 ? -1 : 1;
const k = i % 160;
const t = 0.10 + (k / 160) * 0.76;
const p = pathCurve.getPoint(t), tan = pathCurve.getTangent(t);
const side = new THREE.Vector3(-tan.z, 0, tan.x);
const off = (1.48 + 0.25 * Math.sin(k * 1.7) + rnd2() * 0.22) * sideIdx;
x = p.x + side.x * off; z = p.z + side.z * off;
} else {
const r = 4 + rnd2() * 10, a = rnd2() * 6.283;
x = Math.cos(a) * r; z = Math.sin(a) * r;
}
dum.position.set(x, groundH(x, z) + 0.035, z);
dum.rotation.set(rnd2() * 0.3, rnd2() * 6.28, rnd2() * 0.3);
dum.scale.set(0.30 + rnd2() * 0.42, 0.22 + rnd2() * 0.22, 0.30 + rnd2() * 0.42);
dum.updateMatrix(); rocks.setMatrixAt(i, dum.matrix);
col.setHSL(0.09, 0.05 + rnd2() * 0.08, 0.28 + rnd2() * 0.16);
rocks.setColorAt(i, col);
}
rocks.instanceMatrix.needsUpdate = true;
rocks.castShadow = true; rocks.receiveShadow = true;
scene.add(rocks);
/* ------------------------- fence along the path ------------------------- */
const fenceMat = new THREE.MeshStandardMaterial({ color: 0x5c4a34, roughness: 0.95 });
const fence = new THREE.Group();
fence.name = 'fence';
const postGeo = new THREE.BoxGeometry(0.13, 1.2, 0.13);
const posts = [];
for (let i = 0; i < 11; i++) {
const t = 0.20 + i * 0.052;
const p = pathCurve.getPoint(t), tan = pathCurve.getTangent(t);
const side = new THREE.Vector3(-tan.z, 0, tan.x).normalize();
const q = p.clone().add(side.multiplyScalar(-2.35));
const y = groundH(q.x, q.z);
const mm = new THREE.Mesh(postGeo, fenceMat);
mm.position.set(q.x, y + 0.55, q.z);
mm.rotation.y = Math.atan2(tan.x, tan.z);
mm.castShadow = true; mm.receiveShadow = true;
fence.add(mm);
posts.push(new THREE.Vector3(q.x, y, q.z));
}
for (let i = 0; i < posts.length - 1; i++) {
for (const h of [0.45, 0.92]) {
const a = posts[i].clone().setY(posts[i].y + h);
const b = posts[i + 1].clone().setY(posts[i + 1].y + h);
const dir = new THREE.Vector3().subVectors(b, a);
const rail = new THREE.Mesh(new THREE.BoxGeometry(0.07, 0.13, dir.length()), fenceMat);
rail.position.copy(a).add(b).multiplyScalar(0.5);
rail.quaternion.setFromUnitVectors(new THREE.Vector3(0, 0, 1), dir.normalize());
rail.castShadow = true; fence.add(rail);
}
}
scene.add(fence);
/* ------------------------- distant trees ------------------------- */
const treeGroup = new THREE.Group();
treeGroup.name = 'trees';
const trunkMat = new THREE.MeshStandardMaterial({ color: 0x3d2f22, roughness: 1 });
const leafMats = [
new THREE.MeshStandardMaterial({ color: 0x3c5b33, roughness: 1, flatShading: true }),
new THREE.MeshStandardMaterial({ color: 0x4a6b3a, roughness: 1, flatShading: true }),
new THREE.MeshStandardMaterial({ color: 0x33502e, roughness: 1, flatShading: true })
];
const trnd = rng(707);
for (let i = 0; i < 22; i++) {
const a = trnd() * Math.PI * 2;
const r = 34 + trnd() * 26;
const x = Math.cos(a) * r, z = Math.sin(a) * r;
const y = groundH(x, z);
const h = 3.5 + trnd() * 4.5;
const trunk = new THREE.Mesh(new THREE.CylinderGeometry(0.16, 0.32, h, 6), trunkMat);
trunk.position.set(x, y + h / 2, z);
treeGroup.add(trunk);
const blobs = 5 + Math.floor(trnd() * 4);
for (let b = 0; b < blobs; b++) {
const rad = 0.9 + trnd() * 1.45;
const c = new THREE.Mesh(new THREE.IcosahedronGeometry(rad, 1), leafMats[i % 3]);
c.position.set(x + (trnd() - 0.5) * rad * 2.6, y + h * (0.6 + trnd() * 0.42), z + (trnd() - 0.5) * rad * 2.6);
c.scale.set(1, 0.8 + trnd() * 0.4, 1);
c.rotation.set(trnd() * 3, trnd() * 3, trnd() * 3);
treeGroup.add(c);
}
}
scene.add(treeGroup);
/* ------------------------- clouds ------------------------- */
const cloudTex = cloudSet();
const clouds = new THREE.Group();
clouds.name = 'clouds';
const crnd = rng(303);
for (let i = 0; i < 18; i++) {
const mat = new THREE.SpriteMaterial({ map: cloudTex, transparent: true, opacity: 0.34 + crnd() * 0.34, depthWrite: false, fog: false });
const sp = new THREE.Sprite(mat);
const a = i < 11 ? (-1.15 + crnd() * 2.3) : crnd() * Math.PI * 2;
const r = 150 + crnd() * 130;
sp.position.set(Math.sin(a) * r, 42 + crnd() * 62, Math.cos(a) * r);
const s = 58 + crnd() * 85;
sp.scale.set(s, s * (0.3 + crnd() * 0.2), 1);
clouds.add(sp);
}
scene.add(clouds);
/* ------------------------- lighting ------------------------- */
const hemi = new THREE.HemisphereLight(0xbcd6ee, 0x5b5a3c, 0.45);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffddaa, 3.1);
sun.position.copy(sunDir).multiplyScalar(46);
sun.castShadow = true;
sun.shadow.mapSize.set(3072, 3072);
sun.shadow.camera.near = 4;
sun.shadow.camera.far = 120;
const sc = 26;
sun.shadow.camera.left = -sc; sun.shadow.camera.right = sc;
sun.shadow.camera.top = sc; sun.shadow.camera.bottom = -sc;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.02;
sun.shadow.radius = 2.2;
scene.add(sun);
scene.add(sun.target);
const fill = new THREE.DirectionalLight(0x9fc3e0, 0.7);
fill.position.set(26, 16, -22);
scene.add(fill);
const rim = new THREE.DirectionalLight(0xffd2a0, 0.8);
rim.position.set(10, 8, -38);
scene.add(rim);
return { sun, hemi, fill, rim, groundH, pathCurve, sky, treeGroup };
}