179 lines
5.6 KiB
JavaScript
179 lines
5.6 KiB
JavaScript
import { BrownianSimplexNoise } from './noise.js'
|
|
|
|
const LEFT = 0
|
|
const RIGHT = 1
|
|
const FRONT = 2
|
|
const BACK = 3
|
|
const TOP = 4
|
|
const BOTTOM = 5
|
|
|
|
class ChunkWorker {
|
|
setNoise (params) {
|
|
if (this.noise) return
|
|
this.noise = new BrownianSimplexNoise(
|
|
params.seed,
|
|
params.amplitude,
|
|
params.period,
|
|
params.persistence,
|
|
params.lacunarity,
|
|
params.octaves
|
|
)
|
|
}
|
|
|
|
getBlockAt (voldata, dims, x, y, z) {
|
|
if (voldata.neighbors) {
|
|
if (x < 0) return this.getBlockAt(voldata.neighbors[RIGHT], dims, dims[0] - 1, y, z)
|
|
if (x >= dims[0]) return this.getBlockAt(voldata.neighbors[LEFT], dims, 0, y, z)
|
|
if (z < 0) return this.getBlockAt(voldata.neighbors[FRONT], dims, x, y, dims[2] - 1)
|
|
if (z >= dims[2]) return this.getBlockAt(voldata.neighbors[BACK], dims, x, y, 0)
|
|
if (y < 0 || y >= dims[1]) return null
|
|
}
|
|
return (voldata.volume ? voldata.volume : voldata)[x + dims[0] * (y + dims[1] * z)]
|
|
}
|
|
|
|
generate (params) {
|
|
const absposi = params.x * params.dims[0]
|
|
const absposj = params.y * params.dims[2]
|
|
|
|
const volume = []
|
|
for (let x = 0; x < params.dims[0]; x++) {
|
|
for (let z = 0; z < params.dims[2]; z++) {
|
|
const height = this.noise.getNoise2D(absposi + x, absposj + z) * 10 + 64
|
|
for (let y = 0; y < params.dims[1]; y++) {
|
|
volume[x + params.dims[0] * (y + params.dims[1] * z)] = (y < height) ? 1 : 0
|
|
}
|
|
}
|
|
}
|
|
|
|
return volume
|
|
}
|
|
|
|
mesh (params) {
|
|
const dims = params.dims
|
|
const vertices = []
|
|
const indices = []
|
|
const normals = []
|
|
|
|
for (let backFace = true, b = false; b !== backFace; backFace = backFace && b, b = !b) {
|
|
// Sweep over 3-axes
|
|
for (let d = 0; d < 3; ++d) {
|
|
let i, j, k, l, w, h, side
|
|
const u = (d + 1) % 3
|
|
const v = (d + 2) % 3
|
|
const x = [0, 0, 0]
|
|
const q = [0, 0, 0]
|
|
|
|
// Here we're keeping track of the side that we're meshing.
|
|
if (d === 0) side = backFace ? LEFT : RIGHT
|
|
else if (d === 1) side = backFace ? BOTTOM : TOP
|
|
else if (d === 2) side = backFace ? BACK : FRONT
|
|
|
|
const mask = new Int32Array(dims[u] * dims[v])
|
|
q[d] = 1
|
|
// Move through the dimension from front to back
|
|
for (x[d] = -1; x[d] < dims[d];) {
|
|
// Compute mask
|
|
let n = 0
|
|
for (x[v] = 0; x[v] < dims[v]; ++x[v]) {
|
|
for (x[u] = 0; x[u] < dims[u]; ++x[u]) {
|
|
const current = this.getBlockAt(params, dims, x[0], x[1], x[2])
|
|
const ajacent = this.getBlockAt(params, dims, x[0] + q[0], x[1] + q[1], x[2] + q[2])
|
|
mask[n++] = ((current && ajacent && current === ajacent)) ? null : (backFace ? ajacent : current)
|
|
}
|
|
}
|
|
|
|
// Increment x[d]
|
|
++x[d]
|
|
|
|
// Generate mesh for mask using lexicographic ordering
|
|
n = 0
|
|
for (j = 0; j < dims[v]; ++j) {
|
|
for (i = 0; i < dims[u];) {
|
|
if (mask[n]) {
|
|
// Compute width
|
|
for (w = 1; mask[n + w] && mask[n + w] === mask[n] && i + w < dims[u]; ++w) {}
|
|
|
|
// Compute height
|
|
let done = false
|
|
for (h = 1; j + h < dims[v]; ++h) {
|
|
for (k = 0; k < w; ++k) {
|
|
if (!mask[n + k + h * dims[u]] || mask[n + k + h * dims[u]] !== mask[n]) {
|
|
done = true
|
|
break
|
|
}
|
|
}
|
|
if (done) break
|
|
}
|
|
|
|
// Create quad
|
|
x[u] = i
|
|
x[v] = j
|
|
|
|
const du = [0, 0, 0]
|
|
du[u] = w
|
|
|
|
const dv = [0, 0, 0]
|
|
dv[v] = h
|
|
|
|
const quad = [
|
|
[x[0], x[1], x[2]],
|
|
[x[0] + du[0], x[1] + du[1], x[2] + du[2]],
|
|
[x[0] + du[0] + dv[0], x[1] + du[1] + dv[1], x[2] + du[2] + dv[2]],
|
|
[x[0] + dv[0], x[1] + dv[1], x[2] + dv[2]]
|
|
]
|
|
|
|
// Add vertices and normals
|
|
const mul = backFace ? -1 : 1
|
|
for (var qindex = 0; qindex < 4; ++qindex) {
|
|
vertices.push(quad[qindex][0], quad[qindex][1], quad[qindex][2])
|
|
normals.push(q[0] * mul, q[1] * mul, q[2] * mul)
|
|
}
|
|
|
|
// Add indices
|
|
const indexi = vertices.length / 3 - 4
|
|
if (backFace) {
|
|
indices.push(indexi + 2, indexi + 1, indexi)
|
|
indices.push(indexi + 3, indexi + 2, indexi)
|
|
} else {
|
|
indices.push(indexi, indexi + 1, indexi + 2)
|
|
indices.push(indexi, indexi + 2, indexi + 3)
|
|
}
|
|
|
|
// Zero-out mask
|
|
for (l = 0; l < h; ++l) {
|
|
for (k = 0; k < w; ++k) {
|
|
mask[n + k + l * dims[u]] = false
|
|
}
|
|
}
|
|
|
|
// Increment counters and continue
|
|
i += w
|
|
n += w
|
|
} else {
|
|
++i
|
|
++n
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return { vertices, indices, normals }
|
|
}
|
|
}
|
|
|
|
/* global self */
|
|
const WORKER = new ChunkWorker()
|
|
self.onmessage = function (e) {
|
|
const data = JSON.parse(e.data)
|
|
if (data.subject === 'gen') {
|
|
WORKER.setNoise(data)
|
|
const chunkData = WORKER.generate(data)
|
|
self.postMessage(JSON.stringify({ subject: 'gen_result', data: chunkData }))
|
|
} else if (data.subject === 'mesh') {
|
|
const meshData = WORKER.mesh(data)
|
|
self.postMessage(JSON.stringify({ subject: 'mesh_data', data: meshData }))
|
|
}
|
|
}
|