// Voxel grid world: storage, collision queries and an instanced soft-block mesh. import * as THREE from 'three'; import { sharedLight } from '../render/fur'; export const enum B { AIR = 0, GRASS, PINK, CHOCO, SUGAR, CASTLE, PATH, COOKIE, MINT, CLOUD, GOLD, BERRY, LEMON } /** [topColor, sideColor] for each block type. */ const COLORS: Record = { [B.GRASS]: ['#9fe6a0', '#e8b98a'], [B.PINK]: ['#ffb3d1', '#f59bbd'], [B.CHOCO]: ['#8a5a3c', '#734a31'], [B.SUGAR]: ['#fff3d6', '#f5e2b8'], [B.CASTLE]: ['#e6d4ff', '#d2bcf7'], [B.PATH]: ['#fff0c9', '#f0d9a5'], [B.COOKIE]: ['#e8b98a', '#d9a574'], [B.MINT]: ['#bff5e4', '#a3e6d2'], [B.CLOUD]: ['#ffffff', '#f3ecff'], [B.GOLD]: ['#ffd95a', '#f2c23a'], [B.BERRY]: ['#ff8fb1', '#ff7aa2'], [B.LEMON]: ['#fff59a', '#f7e874'], }; export class VoxelWorld { readonly sx: number; readonly sy: number; readonly sz: number; readonly data: Uint8Array; /** Bounce strength per block type (trampolines are props, but blocks can bounce too). */ constructor(sx: number, sy: number, sz: number) { this.sx = sx; this.sy = sy; this.sz = sz; this.data = new Uint8Array(sx * sy * sz); } /** World x,z are centred: block (i,k) covers x in [i - sx/2, i - sx/2 + 1). */ get ox() { return this.sx / 2; } get oz() { return this.sz / 2; } idx(i: number, j: number, k: number) { return i + this.sx * (k + this.sz * j); } get(i: number, j: number, k: number): number { if (i < 0 || k < 0 || i >= this.sx || k >= this.sz || j >= this.sy) return 0; if (j < 0) return B.COOKIE; return this.data[this.idx(i, j, k)]; } set(i: number, j: number, k: number, t: number) { if (i < 0 || k < 0 || j < 0 || i >= this.sx || k >= this.sz || j >= this.sy) return; this.data[this.idx(i, j, k)] = t; } solidAt(x: number, y: number, z: number): boolean { return this.get(Math.floor(x + this.ox), Math.floor(y), Math.floor(z + this.oz)) !== 0; } /** Height of the highest solid top at or below y (search down). -Infinity if none. */ groundBelow(x: number, z: number, y: number): number { const i = Math.floor(x + this.ox), k = Math.floor(z + this.oz); if (i < 0 || k < 0 || i >= this.sx || k >= this.sz) return -Infinity; let j = Math.min(this.sy - 1, Math.floor(y)); for (; j >= 0; j--) if (this.data[this.idx(i, j, k)] !== 0) return j + 1; return -Infinity; } /** Top surface of the column (ignores overhangs). */ topAt(x: number, z: number): number { return this.groundBelow(x, z, this.sy); } blockTop(x: number, z: number): number { const i = Math.floor(x + this.ox), k = Math.floor(z + this.oz); for (let j = this.sy - 1; j >= 0; j--) { const t = this.get(i, j, k); if (t) return t; } return 0; } inside(x: number, z: number, margin = 0): boolean { return x > -this.ox + margin && x < this.ox - margin && z > -this.oz + margin && z < this.oz - margin; } fillColumn(i: number, k: number, h: number, top: number, below = B.COOKIE) { for (let j = 0; j < h; j++) this.set(i, j, k, j === h - 1 ? top : below); } box(i0: number, j0: number, k0: number, i1: number, j1: number, k1: number, t: number) { for (let j = j0; j <= j1; j++) for (let k = k0; k <= k1; k++) for (let i = i0; i <= i1; i++) this.set(i, j, k, t); } /** Build one instanced mesh of all exposed blocks. Soft chamfered cubes, top/side colours. */ buildMesh(): THREE.InstancedMesh { const exposed: number[] = []; const { sx, sy, sz } = this; for (let j = 0; j < sy; j++) for (let k = 0; k < sz; k++) for (let i = 0; i < sx; i++) { const t = this.data[this.idx(i, j, k)]; if (!t) continue; if (!this.get(i + 1, j, k) || !this.get(i - 1, j, k) || !this.get(i, j + 1, k) || (j > 0 && !this.get(i, j - 1, k)) || !this.get(i, j, k + 1) || !this.get(i, j, k - 1)) exposed.push(i, j, k, t); } const n = exposed.length / 4; const geo = chamferBox(0.1); const mat = blockMaterial(); const mesh = new THREE.InstancedMesh(geo, mat, n); const m = new THREE.Matrix4(); const top = new Float32Array(n * 3), side = new Float32Array(n * 3), cover = new Float32Array(n); const c = new THREE.Color(); for (let q = 0; q < n; q++) { const i = exposed[q * 4], j = exposed[q * 4 + 1], k = exposed[q * 4 + 2], t = exposed[q * 4 + 3]; m.makeTranslation(i - this.ox + 0.5, j + 0.5, k - this.oz + 0.5); mesh.setMatrixAt(q, m); const [tc, sc] = COLORS[t] ?? ['#ff00ff', '#ff00ff']; // tiny per-block variation keeps the voxels hand-made rather than flat const v = 0.94 + ((i * 73856093 ^ j * 19349663 ^ k * 83492791) >>> 0) % 100 / 100 * 0.1; c.set(tc).multiplyScalar(v); top.set([c.r, c.g, c.b], q * 3); c.set(sc).multiplyScalar(v); side.set([c.r, c.g, c.b], q * 3); cover[q] = this.get(i, j + 1, k) ? 1 : 0; } geo.setAttribute('aTop', new THREE.InstancedBufferAttribute(top, 3)); geo.setAttribute('aSide', new THREE.InstancedBufferAttribute(side, 3)); geo.setAttribute('aCover', new THREE.InstancedBufferAttribute(cover, 1)); mesh.instanceMatrix.needsUpdate = true; mesh.frustumCulled = false; return mesh; } } function chamferBox(b: number): THREE.BufferGeometry { // A cube with bevelled edges built from a low-poly rounded box. const g = new THREE.BoxGeometry(1, 1, 1, 2, 2, 2); const p = g.getAttribute('position') as THREE.BufferAttribute; const v = new THREE.Vector3(); for (let i = 0; i < p.count; i++) { v.fromBufferAttribute(p, i); const ax = Math.abs(v.x), ay = Math.abs(v.y), az = Math.abs(v.z); // pull mid-edge vertices in to bevel the corners const edges = (ax > 0.49 ? 1 : 0) + (ay > 0.49 ? 1 : 0) + (az > 0.49 ? 1 : 0); if (edges >= 2) v.multiplyScalar(1 - b * (edges === 3 ? 0.9 : 0.6)); p.setXYZ(i, v.x, v.y, v.z); } g.computeVertexNormals(); return g; } function blockMaterial(): THREE.ShaderMaterial { return new THREE.ShaderMaterial({ uniforms: { ...sharedLight }, vertexShader: /* glsl */` attribute vec3 aTop; attribute vec3 aSide; attribute float aCover; varying vec3 vCol; varying vec3 vN; varying vec3 vWorld; varying vec3 vLocal; void main(){ vec4 w = modelMatrix * instanceMatrix * vec4(position,1.0); vWorld = w.xyz; vN = normalize(mat3(modelMatrix) * mat3(instanceMatrix) * normal); vLocal = position; float isTop = step(0.6, normal.y) * (1.0 - aCover); // frosting drips: the top colour spills a little way down the sides float drip = (1.0 - aCover) * step(0.32 + 0.1*sin(position.x*9.0 + position.z*7.0 + instanceMatrix[3].x*3.1), position.y); vCol = mix(aSide, aTop, max(isTop, drip)); gl_Position = projectionMatrix * viewMatrix * w; }`, fragmentShader: /* glsl */` uniform vec3 uSunDir; uniform vec3 uSunColor; uniform vec3 uSky; uniform vec3 uGround; uniform vec3 uFogColor; uniform float uFogNear; uniform float uFogFar; varying vec3 vCol; varying vec3 vN; varying vec3 vWorld; varying vec3 vLocal; void main(){ vec3 N = normalize(vN); float diff = clamp((dot(N,uSunDir)+0.35)/1.35,0.0,1.0); vec3 amb = mix(uGround,uSky,N.y*0.5+0.5)*0.6; // soft edge darkening gives each block a pillowy look float edge = max(max(abs(vLocal.x),abs(vLocal.y)),abs(vLocal.z)); float e2 = (abs(vLocal.x)>0.44?1.0:0.0)+(abs(vLocal.y)>0.44?1.0:0.0)+(abs(vLocal.z)>0.44?1.0:0.0); vec3 c = vCol * (amb + uSunColor*diff*0.75) * (1.0 - 0.07*step(1.5,e2)); float fog = smoothstep(uFogNear, uFogFar, length(cameraPosition - vWorld)); gl_FragColor = vec4(mix(c,uFogColor,fog),1.0); #include }`, }); }