153 lines
5.7 KiB
JavaScript
153 lines
5.7 KiB
JavaScript
// node test/view.test.mjs —— 用相机几何把「画面」采样一遍,确认相机确实看着地形
|
||
// 不依赖 GPU:自己算射线,和网格三角形求交。
|
||
import { World } from '../js/world.js'
|
||
import { dirFromAngles } from '../js/player.js'
|
||
|
||
const world = new World(20260101)
|
||
const CX = 16, CZ = 16
|
||
const R = 4
|
||
for (let cz = -R; cz <= R; cz++) for (let cx = -R; cx <= R; cx++) {
|
||
const c = world.chunkAt(cx, cz)
|
||
world.generate(c)
|
||
}
|
||
|
||
// 收集三角形(世界坐标)
|
||
let tris = []
|
||
function collectTris() {
|
||
tris = []
|
||
for (let cz = -R; cz <= R; cz++) for (let cx = -R; cx <= R; cx++) {
|
||
const c = world.chunkAt(cx, cz)
|
||
const { solid } = world.buildMesh(c)
|
||
const p = solid.pos, ix = solid.idx
|
||
for (let t = 0; t < ix.length; t += 3) {
|
||
const a = ix[t], b = ix[t + 1], d = ix[t + 2]
|
||
tris.push([
|
||
p[a * 3] + cx * CX, p[a * 3 + 1], p[a * 3 + 2] + cz * CZ,
|
||
p[b * 3] + cx * CX, p[b * 3 + 1], p[b * 3 + 2] + cz * CZ,
|
||
p[d * 3] + cx * CX, p[d * 3 + 1], p[d * 3 + 2] + cz * CZ,
|
||
])
|
||
}
|
||
}
|
||
}
|
||
collectTris()
|
||
console.log(`三角形总数 ${tris.length}`)
|
||
|
||
function intersect(o, dir) {
|
||
let best = Infinity
|
||
for (const [ax, ay, az, bx, by, bz, cx2, cy2, cz2] of tris) {
|
||
const e1x = bx - ax, e1y = by - ay, e1z = bz - az
|
||
const e2x = cx2 - ax, e2y = cy2 - ay, e2z = cz2 - az
|
||
const px = dir.y * e2z - dir.z * e2y
|
||
const py = dir.z * e2x - dir.x * e2z
|
||
const pz = dir.x * e2y - dir.y * e2x
|
||
const det = e1x * px + e1y * py + e1z * pz
|
||
if (Math.abs(det) < 1e-9) continue
|
||
const inv = 1 / det
|
||
const tx = o.x - ax, ty = o.y - ay, tz = o.z - az
|
||
const u = (tx * px + ty * py + tz * pz) * inv
|
||
if (u < 0 || u > 1) continue
|
||
const qx = ty * e1z - tz * e1y
|
||
const qy = tz * e1x - tx * e1z
|
||
const qz = tx * e1y - ty * e1x
|
||
const v = (dir.x * qx + dir.y * qy + dir.z * qz) * inv
|
||
if (v < 0 || u + v > 1) continue
|
||
const tHit = (e2x * qx + e2y * qy + e2z * qz) * inv
|
||
if (tHit > 1e-4 && tHit < best) best = tHit
|
||
}
|
||
return best
|
||
}
|
||
|
||
function sample(camPos, yaw, pitch, W = 48, H = 30) {
|
||
const fovY = (74 * Math.PI) / 180
|
||
const f = dirFromAngles(pitch, yaw)
|
||
// right = normalize(cross(f, up)), up' = cross(right, f)
|
||
let rx = f.z * 0 - f.y * 0, ry = 0, rz = 0
|
||
rx = f.y * 0 - f.z * 1
|
||
ry = f.z * 0 - f.x * 0
|
||
rz = f.x * 1 - f.y * 0
|
||
// cross(f, up=(0,1,0)) = (f.y*0 - f.z*1, f.z*0 - f.x*0, f.x*1 - f.y*0)
|
||
const rl = Math.hypot(rx, ry, rz) || 1
|
||
rx /= rl; ry /= rl; rz /= rl
|
||
const ux = ry * f.z - rz * f.y
|
||
const uy = rz * f.x - rx * f.z
|
||
const uz = rx * f.y - ry * f.x
|
||
const aspect = W / H
|
||
const tanY = Math.tan(fovY / 2)
|
||
const tanX = tanY * aspect
|
||
const hits = []
|
||
for (let j = 0; j < H; j++) {
|
||
for (let i = 0; i < W; i++) {
|
||
const sx = (i + 0.5) / W * 2 - 1
|
||
const sy = 1 - (j + 0.5) / H * 2
|
||
const dx = f.x + rx * tanX * sx + ux * tanY * sy
|
||
const dy = f.y + ry * tanX * sx + uy * tanY * sy
|
||
const dz = f.z + rz * tanX * sx + uz * tanY * sy
|
||
const l = Math.hypot(dx, dy, dz)
|
||
const t = intersect(camPos, { x: dx / l, y: dy / l, z: dz / l })
|
||
if (t === Infinity) { hits.push(null); continue }
|
||
// 沿射线稍微往里一点,取那个方块
|
||
const bx = Math.floor(camPos.x + (dx / l) * (t + 0.01))
|
||
const by = Math.floor(camPos.y + (dy / l) * (t + 0.01))
|
||
const bz = Math.floor(camPos.z + (dz / l) * (t + 0.01))
|
||
hits.push({ t, id: world.get(bx, by, bz) })
|
||
}
|
||
}
|
||
const solidHits = hits.filter(Boolean)
|
||
const ids = {}
|
||
for (const h of solidHits) ids[h.id] = (ids[h.id] || 0) + 1
|
||
return {
|
||
coverage: solidHits.length / hits.length,
|
||
unique: Object.keys(ids).length,
|
||
ids,
|
||
near: solidHits.length ? Math.min(...solidHits.map((h) => h.t)) : null,
|
||
far: solidHits.length ? Math.max(...solidHits.map((h) => h.t)) : null,
|
||
}
|
||
}
|
||
|
||
let pass = 0, fail = 0
|
||
const ok = (c, label, extra = '') => { if (c) { pass++; console.log('✓', label) } else { fail++; console.log('✗', label, extra) } }
|
||
|
||
// 站在地表,水平看
|
||
const gy = world.heightAt(8, 8)
|
||
const cam = { x: 8.5, y: gy + 1.6, z: 8.5 }
|
||
|
||
const front = sample(cam, 0, -0.08)
|
||
console.log('正前方', JSON.stringify(front))
|
||
ok(front.coverage > 0.5, `画面大部分被地形填满(${(front.coverage * 100).toFixed(0)}%)`)
|
||
ok(front.near !== null && front.near > 0.1, `最近命中 ${front.near?.toFixed(2)} 不在相机内部`)
|
||
|
||
// 抬头看天:应该几乎打不到东西
|
||
const up = sample(cam, 0, 1.2)
|
||
ok(up.coverage < 0.2, `抬头看天几乎全空(覆盖 ${(up.coverage * 100).toFixed(0)}%)`)
|
||
|
||
// 低头看脚:一定打得到,而且很近
|
||
const down = sample(cam, 0, -1.3)
|
||
console.log('低头', JSON.stringify(down))
|
||
ok(down.coverage > 0.95, `低头全被地面填满(${(down.coverage * 100).toFixed(0)}%)`)
|
||
ok(down.near < 2, `脚下距离 ${down.near?.toFixed(2)} < 2`)
|
||
|
||
// 转一圈,任何方向都不该是空白
|
||
let worst = 1
|
||
for (let k = 0; k < 8; k++) {
|
||
const s = sample(cam, (k * Math.PI) / 4, -0.1)
|
||
worst = Math.min(worst, s.coverage)
|
||
}
|
||
ok(worst > 0.3, `360° 每个方向都有东西(最差覆盖 ${(worst * 100).toFixed(0)}%)`)
|
||
|
||
// 高空俯瞰:能看到多种方块
|
||
const high = sample({ x: 8.5, y: 55, z: 8.5 }, 0, -1.2)
|
||
ok(high.unique >= 2, `俯瞰能看到多种方块(${Object.keys(high.ids).length} 种)`)
|
||
|
||
// 挖掉脚下一块后,朝下射线应打得更远
|
||
const gy2 = world.heightAt(40, 40)
|
||
const cam2 = { x: 40.5, y: gy2 + 1.6, z: 40.5 }
|
||
const straightDown = -Math.PI / 2
|
||
const before = sample(cam2, 0, straightDown, 1, 1).near
|
||
world.set(40, gy2, 40, 0)
|
||
collectTris()
|
||
const after = sample(cam2, 0, straightDown, 1, 1).near
|
||
ok(Math.abs(after - before - 1) < 0.25, `挖掉方块后垂直看到深一格(${before?.toFixed(2)} → ${after?.toFixed(2)})`)
|
||
|
||
console.log(`\n${pass} 通过 / ${fail} 失败`)
|
||
process.exit(fail ? 1 : 0)
|