Files

224 lines
8.7 KiB
JavaScript

// 区块世界:地形生成 + 面剔除网格 + 顶点 AO
import * as THREE from '../vendor/three.module.min.js'
import { fbm2, fbm3 } from './noise.js'
import { AIR, BLOCKS, faceTile, tileUV, isOpaque, isSolid } from './blocks.js'
export const CX = 16, CZ = 16, CY = 64, SEA = 18
const idx = (x, y, z) => (y * CZ + z) * CX + x
// 每个面:法线、四个角、切线符号(算 AO 用)、uv、明暗
const FACES = [
{ dir: [0, 1, 0], corners: [[0, 1, 0], [0, 1, 1], [1, 1, 1], [1, 1, 0]], signs: [[-1, -1], [-1, 1], [1, 1], [1, -1]], uvs: [[0, 0], [0, 1], [1, 1], [1, 0]], shade: 1.0 },
{ dir: [0, -1, 0], corners: [[0, 0, 1], [0, 0, 0], [1, 0, 0], [1, 0, 1]], signs: [[-1, 1], [-1, -1], [1, -1], [1, 1]], uvs: [[0, 1], [0, 0], [1, 0], [1, 1]], shade: 0.52 },
{ dir: [0, 0, 1], corners: [[0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1]], signs: [[-1, -1], [1, -1], [1, 1], [-1, 1]], uvs: [[0, 1], [1, 1], [1, 0], [0, 0]], shade: 0.86 },
{ dir: [0, 0, -1], corners: [[1, 0, 0], [0, 0, 0], [0, 1, 0], [1, 1, 0]], signs: [[-1, -1], [1, -1], [1, 1], [-1, 1]], uvs: [[0, 1], [1, 1], [1, 0], [0, 0]], shade: 0.86 },
{ dir: [1, 0, 0], corners: [[1, 0, 1], [1, 0, 0], [1, 1, 0], [1, 1, 1]], signs: [[-1, -1], [1, -1], [1, 1], [-1, 1]], uvs: [[0, 1], [1, 1], [1, 0], [0, 0]], shade: 0.72 },
{ dir: [-1, 0, 0], corners: [[0, 0, 0], [0, 0, 1], [0, 1, 1], [0, 1, 0]], signs: [[-1, -1], [1, -1], [1, 1], [-1, 1]], uvs: [[0, 1], [1, 1], [1, 0], [0, 0]], shade: 0.72 },
]
// AO 采样用的两个切轴(对应 signs 的两个分量)
const TANGENTS = [[1, 0, 0], [0, 1, 0], [0, 0, 1]].map((a, i) => a)
function axisPair(dir) {
if (dir[0]) return [[0, 1, 0], [0, 0, 1]]
if (dir[1]) return [[1, 0, 0], [0, 0, 1]]
return [[1, 0, 0], [0, 1, 0]]
}
export class World {
constructor(seed = 20260101) {
this.seed = seed >>> 0
this.chunks = new Map()
this.edits = new Map() // "x,y,z" -> id
}
key(cx, cz) { return cx + ',' + cz }
chunkAt(cx, cz) {
let c = this.chunks.get(this.key(cx, cz))
if (!c) {
c = { cx, cz, data: new Uint8Array(CX * CY * CZ), generated: false, solid: null, water: null, dirty: true }
this.chunks.set(this.key(cx, cz), c)
}
return c
}
get(x, y, z) {
if (y < 0 || y >= CY) return AIR
const cx = Math.floor(x / CX), cz = Math.floor(z / CZ)
const c = this.chunkAt(cx, cz)
if (!c.generated) this.generate(c)
return c.data[idx(x - cx * CX, y, z - cz * CZ)]
}
set(x, y, z, id) {
if (y < 0 || y >= CY) return
const cx = Math.floor(x / CX), cz = Math.floor(z / CZ)
const c = this.chunkAt(cx, cz)
if (!c.generated) this.generate(c)
const lx = x - cx * CX, lz = z - cz * CZ
if (c.data[idx(lx, y, lz)] === id) return
c.data[idx(lx, y, lz)] = id
c.dirty = true
// 边界方块要重建邻居
if (lx === 0) this.mark(cx - 1, cz)
if (lx === CX - 1) this.mark(cx + 1, cz)
if (lz === 0) this.mark(cx, cz - 1)
if (lz === CZ - 1) this.mark(cx, cz + 1)
this.edits.set(`${x},${y},${z}`, id)
}
mark(cx, cz) {
const c = this.chunks.get(this.key(cx, cz))
if (c && c.generated) c.dirty = true
}
heightAt(wx, wz) {
const n = fbm2(this.seed, wx * 0.042, wz * 0.042, 4)
const m = fbm2(this.seed + 917, wx * 0.011, wz * 0.011, 3)
let h = SEA + 2 + (n - 0.5) * 20 + (m - 0.5) * 14
return Math.max(3, Math.min(CY - 12, Math.floor(h)))
}
generate(c) {
const { cx, cz, data } = c
const baseX = cx * CX, baseZ = cz * CZ
for (let lx = 0; lx < CX; lx++) {
for (let lz = 0; lz < CZ; lz++) {
const wx = baseX + lx, wz = baseZ + lz
const h = this.heightAt(wx, wz)
const snowy = h > 42
const sandy = h <= SEA + 1
for (let y = 0; y <= h; y++) {
let id = 3
if (y === 0) id = 11
else if (y === h) id = sandy ? 4 : snowy ? 12 : 1
else if (y >= h - 3) id = sandy ? 4 : 2
// 高处岩石裸露
else if (y > 34 && y > h - 1) id = 3
data[idx(lx, y, lz)] = id
}
for (let y = h + 1; y < SEA; y++) data[idx(lx, y, lz)] = 5
// 树
if (!sandy && !snowy && h > SEA && h < 40) {
const r = this.hashCell(wx, wz)
if (r < 0.012 && this.roomForTree(baseX, baseZ, lx, lz, h)) this.plantTree(data, lx, lz, h)
}
}
}
c.generated = true
c.dirty = true
}
hashCell(x, z) {
let h = this.seed ^ Math.imul(x, 374761393) ^ Math.imul(z, 668265263)
h = Math.imul(h ^ (h >>> 13), 1274126177)
return ((h ^ (h >>> 16)) >>> 0) / 4294967296
}
roomForTree(baseX, baseZ, lx, lz, h) {
if (lx < 2 || lx > CX - 3 || lz < 2 || lz > CZ - 3) return false
for (let x = lx - 1; x <= lx + 1; x++) for (let z = lz - 1; z <= lz + 1; z++) {
if (this.hashCell(baseX + x, baseZ + z) < 0.012) return false
}
return true
}
plantTree(data, lx, lz, h) {
const trunk = 4 + Math.floor(this.hashCell(lx * 31, lz * 17) * 3)
for (let i = 1; i <= trunk; i++) data[idx(lx, h + i, lz)] = 6
const top = h + trunk
for (let dy = -2; dy <= 1; dy++) {
const r = dy <= -1 ? 2 : 1
for (let dx = -r; dx <= r; dx++) for (let dz = -r; dz <= r; dz++) {
if (dx === 0 && dz === 0 && dy < 0) continue
if (Math.abs(dx) === r && Math.abs(dz) === r && this.hashCell(lx + dx, lz + dz) > 0.55) continue
const x = lx + dx, y = top + dy, z = lz + dz
if (x < 0 || x >= CX || z < 0 || z >= CZ || y >= CY) continue
if (!data[idx(x, y, z)]) data[idx(x, y, z)] = 7
}
}
}
// 应用存档里的改动
applyEdits() {
for (const [k, id] of this.edits) {
const [x, y, z] = k.split(',').map(Number)
const cx = Math.floor(x / CX), cz = Math.floor(z / CZ)
if (Math.abs(cx) > 64 || Math.abs(cz) > 64) continue
const c = this.chunkAt(cx, cz)
if (!c.generated) this.generate(c)
c.data[idx(x - cx * CX, y, z - cz * CZ)] = id
c.dirty = true
}
}
buildMesh(c) {
const solid = { pos: [], uv: [], col: [], idx: [] }
const water = { pos: [], uv: [], col: [], idx: [] }
const data = c.data
for (let y = 0; y < CY; y++) {
for (let z = 0; z < CZ; z++) {
for (let x = 0; x < CX; x++) {
const id = data[idx(x, y, z)]
if (!id) continue
const isWater = id === 5
const target = isWater ? water : solid
for (let f = 0; f < 6; f++) {
const F = FACES[f]
const nx = x + F.dir[0], ny = y + F.dir[1], nz = z + F.dir[2]
const nb = this.getLocal(c, nx, ny, nz)
if (isWater) { if (nb !== AIR) continue }
else if (isOpaque(nb)) continue
const t = tileUV(faceTile(id, f === 0 ? 0 : f === 1 ? 1 : 2))
const vBase = target.pos.length / 3
const [ua, va] = axisPair(F.dir)
for (let k = 0; k < 4; k++) {
const cn = F.corners[k]
target.pos.push(x + cn[0], y + cn[1], z + cn[2])
const uv = F.uvs[k]
target.uv.push(t.u0 + (t.u1 - t.u0) * uv[0], t.v0 + (t.v1 - t.v0) * uv[1])
// AO:沿两个切轴看相邻方块是否遮挡
let ao = 1
if (!isWater) {
const axis = F.dir.findIndex((v) => v !== 0)
const t1 = (axis + 1) % 3, t2 = (axis + 2) % 3
const cn0 = F.corners[k]
const s1 = cn0[t1] * 2 - 1, s2 = cn0[t2] * 2 - 1
const p = (dx, dy, dz) => this.getLocal(c, nx + dx, ny + dy, nz + dz)
const b1 = [0, 0, 0], b2 = [0, 0, 0], bc = [0, 0, 0]
b1[t1] = s1; b2[t2] = s2; bc[t1] = s1; bc[t2] = s2
const o1 = isOpaque(p(b1[0], b1[1], b1[2])) ? 1 : 0
const o2 = isOpaque(p(b2[0], b2[1], b2[2])) ? 1 : 0
const oc = isOpaque(p(bc[0], bc[1], bc[2])) ? 1 : 0
const o = o1 && o2 ? 3 : o1 + o2 + oc
ao = [1, 0.84, 0.7, 0.56][o]
}
const sh = (isWater ? 0.95 : F.shade) * ao
target.col.push(sh, sh, sh)
}
target.idx.push(vBase, vBase + 1, vBase + 2, vBase, vBase + 2, vBase + 3)
}
}
}
}
return { solid, water }
}
getLocal(c, x, y, z) {
if (y < 0 || y >= CY) return 0
if (x >= 0 && x < CX && z >= 0 && z < CZ) return c.data[idx(x, y, z)]
return this.get(x + c.cx * CX, y, z + c.cz * CZ)
}
}
export function geometryOf(bucket) {
const g = new THREE.BufferGeometry()
g.setAttribute('position', new THREE.Float32BufferAttribute(bucket.pos, 3))
g.setAttribute('uv', new THREE.Float32BufferAttribute(bucket.uv, 2))
g.setAttribute('color', new THREE.Float32BufferAttribute(bucket.col, 3))
g.setIndex(bucket.idx)
g.computeBoundingSphere()
return g
}