282 lines
8.5 KiB
JavaScript
282 lines
8.5 KiB
JavaScript
/* global THREE */
|
|
|
|
const CubePlanetRes = 15
|
|
const blankGeom = new THREE.BufferGeometry()
|
|
|
|
class PlanetGenerator {
|
|
constructor (radius) {
|
|
this.radius = radius
|
|
}
|
|
|
|
getHeight (v) {
|
|
return 0
|
|
}
|
|
|
|
getBiome (v) {
|
|
return 0
|
|
}
|
|
}
|
|
|
|
class CubePlanetIndexBuffer {
|
|
constructor (fanTop = false, fanRight = false, fanLeft = false, fanBottom = false) {
|
|
const indices = []
|
|
for (let y = 0; y < CubePlanetRes - 1; y++) {
|
|
let slantLeft = (y % 2) === 0
|
|
for (let x = 0; x < CubePlanetRes - 1; x++) {
|
|
const topLeft = (y * CubePlanetRes) + x
|
|
const topRight = topLeft + 1
|
|
const bottomLeft = ((y + 1) * CubePlanetRes) + x
|
|
const bottomRight = bottomLeft + 1
|
|
|
|
let tri1 = slantLeft ? [topLeft, bottomLeft, bottomRight] : [topLeft, bottomLeft, topRight]
|
|
let tri2 = slantLeft ? [topLeft, bottomRight, topRight] : [bottomLeft, bottomRight, topRight]
|
|
|
|
if (fanTop && y === 0) {
|
|
if (x % 2 === 0) {
|
|
tri2 = [topLeft, bottomRight, topRight + 1]
|
|
} else {
|
|
tri1 = null
|
|
}
|
|
}
|
|
|
|
if (fanRight && x === CubePlanetRes - 2) {
|
|
if (y % 2 === 0) {
|
|
tri2 = [topRight, bottomLeft, bottomRight + CubePlanetRes]
|
|
} else {
|
|
tri2 = null
|
|
}
|
|
}
|
|
|
|
if (fanBottom && y === CubePlanetRes - 2) {
|
|
if (x % 2 === 0) {
|
|
tri2 = [bottomLeft, bottomRight + 1, topRight]
|
|
} else {
|
|
tri1 = null
|
|
}
|
|
}
|
|
|
|
if (fanLeft && x === 0) {
|
|
if (y % 2 === 0) {
|
|
tri1 = [topLeft, bottomLeft + CubePlanetRes, bottomRight]
|
|
} else {
|
|
tri1 = null
|
|
}
|
|
}
|
|
|
|
// faster than concat :p
|
|
if (tri1) indices.push(tri1[0], tri1[1], tri1[2])
|
|
if (tri2) indices.push(tri2[0], tri2[1], tri2[2])
|
|
|
|
slantLeft = !slantLeft
|
|
}
|
|
}
|
|
this.length = indices.length
|
|
this.indices = new THREE.Uint16BufferAttribute(indices, 1)
|
|
}
|
|
}
|
|
|
|
const CubePlanetIndexBuffers = {
|
|
base: new CubePlanetIndexBuffer(),
|
|
fixT: new CubePlanetIndexBuffer(true, false, false, false),
|
|
fixTR: new CubePlanetIndexBuffer(true, true, false, false),
|
|
fixTL: new CubePlanetIndexBuffer(true, false, true, false),
|
|
fixB: new CubePlanetIndexBuffer(false, false, false, true),
|
|
fixBR: new CubePlanetIndexBuffer(false, true, false, true),
|
|
fixBL: new CubePlanetIndexBuffer(false, false, true, true),
|
|
fixR: new CubePlanetIndexBuffer(false, true, false, false),
|
|
fixL: new CubePlanetIndexBuffer(false, false, true, false)
|
|
}
|
|
|
|
class CubePlanetBufferGeometry extends THREE.BufferGeometry {
|
|
constructor (fnode) {
|
|
super()
|
|
|
|
const vertices = []
|
|
const normals = []
|
|
const uvs = []
|
|
|
|
const radius = fnode.root.generator.radius
|
|
const divisionLevel = Math.pow(2, fnode.level)
|
|
|
|
for (let i = 0, vertexPointer = 0; i < CubePlanetRes; i++) {
|
|
for (let j = 0; j < CubePlanetRes; j++, vertexPointer++) {
|
|
// Vertex index (0 - 1)
|
|
const iindex = i / (CubePlanetRes - 1)
|
|
const jindex = j / (CubePlanetRes - 1)
|
|
|
|
// From the left and forward vectors, we can calculate an oriented vertex
|
|
const iv = fnode.left.clone().multiplyScalar(iindex).multiplyScalar(radius).divideScalar(divisionLevel)
|
|
const jv = fnode.forward.clone().multiplyScalar(jindex).multiplyScalar(radius).divideScalar(divisionLevel)
|
|
|
|
// Add the scaled left and forward to the centered origin
|
|
const vertex = fnode.relPos.clone().add(iv.add(jv))
|
|
|
|
// Normalize and multiply by radius to create a spherical mesh
|
|
const normal = vertex.normalize()
|
|
const pointHeight = fnode.root.generator.getHeight(normal)
|
|
const pos = normal.clone().multiplyScalar(pointHeight * 10 + radius)
|
|
|
|
// const pointBiome = fnode.root.generator.getBiome(pointHeight, normal)
|
|
|
|
vertices[vertexPointer * 3] = pos.x
|
|
vertices[vertexPointer * 3 + 1] = pos.y
|
|
vertices[vertexPointer * 3 + 2] = pos.z
|
|
normals[vertexPointer * 3] = normal.x
|
|
normals[vertexPointer * 3 + 1] = normal.y
|
|
normals[vertexPointer * 3 + 2] = normal.z
|
|
uvs[vertexPointer * 2] = i * (1 / CubePlanetRes)
|
|
uvs[vertexPointer * 2 + 1] = j * (1 / CubePlanetRes)
|
|
|
|
if (i === Math.floor(CubePlanetRes / 2) && j === Math.floor(CubePlanetRes / 2)) {
|
|
fnode.center = pos
|
|
}
|
|
}
|
|
}
|
|
|
|
this.setIndex(CubePlanetIndexBuffers.base.indices)
|
|
this.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3))
|
|
this.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3))
|
|
this.setAttribute('uv', new THREE.Float32BufferAttribute(uvs, 3))
|
|
}
|
|
}
|
|
|
|
class CubePlanetNode extends THREE.Mesh {
|
|
constructor (root, index, level, position, normal) {
|
|
super()
|
|
|
|
this.root = root
|
|
|
|
this.index = index
|
|
this.level = level
|
|
|
|
this.relPos = position
|
|
this.normal = normal
|
|
|
|
this.left = new THREE.Vector3(normal.y, normal.z, normal.x)
|
|
this.forward = normal.clone().cross(this.left)
|
|
|
|
if (level === 0) {
|
|
this.relPos.sub(this.left.clone().multiplyScalar(root.generator.radius / 2))
|
|
this.relPos.sub(this.forward.clone().multiplyScalar(root.generator.radius / 2))
|
|
}
|
|
|
|
if (root.material) this.material = root.material
|
|
|
|
this.generated = false
|
|
this.generate()
|
|
}
|
|
|
|
generate () {
|
|
if (this.generated) return
|
|
this.geometry = new CubePlanetBufferGeometry(this)
|
|
this.generated = true
|
|
}
|
|
|
|
isLeaf () {
|
|
return !this.children.length
|
|
}
|
|
|
|
merge () {
|
|
if (this.isLeaf()) return
|
|
|
|
for (const i in this.children) {
|
|
const ch = this.children[i]
|
|
|
|
ch.merge()
|
|
ch.dispose()
|
|
}
|
|
|
|
this.children = []
|
|
this.generate()
|
|
}
|
|
|
|
subdivide () {
|
|
if (this.level === (this.root.generator.maxLOD || 8)) return
|
|
|
|
const lv = this.level + 1
|
|
const stepLeft = this.left.clone().multiplyScalar(this.root.generator.radius / Math.pow(2, lv))
|
|
const stepForward = this.forward.clone().multiplyScalar(this.root.generator.radius / Math.pow(2, lv))
|
|
|
|
// Top left corner
|
|
this.add(new CubePlanetNode(this.root, 0, lv, this.relPos.clone(), this.normal))
|
|
// Top right corner
|
|
this.add(new CubePlanetNode(this.root, 1, lv, this.relPos.clone().add(stepForward), this.normal))
|
|
// Bottom right corner
|
|
this.add(new CubePlanetNode(this.root, 2, lv, this.relPos.clone().add(stepLeft.clone().add(stepForward)), this.normal))
|
|
// Bottom left corner
|
|
this.add(new CubePlanetNode(this.root, 3, lv, this.relPos.clone().add(stepLeft), this.normal))
|
|
|
|
this.dispose()
|
|
}
|
|
|
|
dispose () {
|
|
if (!this.generated) return
|
|
this.geometry.dispose()
|
|
this.geometry = blankGeom
|
|
this.generated = false
|
|
}
|
|
|
|
setMaterial (mat) {
|
|
this.material = mat
|
|
for (const i in this.children) {
|
|
this.children[i].setMaterial(mat)
|
|
}
|
|
}
|
|
|
|
update (camera, dt) {
|
|
if (!this.center) return
|
|
const camToOrigin = camera.position.clone().distanceTo(this.center)
|
|
const divisionLevel = Math.pow(2, this.level)
|
|
const splitDistance = this.root.generator.radius / divisionLevel
|
|
|
|
if (camToOrigin < splitDistance * 5 && this.children.length === 0) {
|
|
this.subdivide()
|
|
return
|
|
} else if (camToOrigin > splitDistance * 5.5 && this.children.length > 0) {
|
|
this.merge()
|
|
return
|
|
}
|
|
|
|
if (this.children.length > 0) {
|
|
for (const i in this.children) {
|
|
this.children[i].update(camera, dt)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
class CubePlanet extends THREE.Object3D {
|
|
constructor (origin, generator) {
|
|
super()
|
|
this.position.copy(origin)
|
|
this.generator = generator
|
|
|
|
this.radius = generator.radius
|
|
const hs = generator.radius / 2
|
|
|
|
this.add(new CubePlanetNode(this, 0, 0, new THREE.Vector3(0, 0, -hs), new THREE.Vector3(0, 0, -1)))
|
|
this.add(new CubePlanetNode(this, 1, 0, new THREE.Vector3(0, 0, hs), new THREE.Vector3(0, 0, 1)))
|
|
|
|
this.add(new CubePlanetNode(this, 2, 0, new THREE.Vector3(-hs, 0, 0), new THREE.Vector3(-1, 0, 0)))
|
|
this.add(new CubePlanetNode(this, 3, 0, new THREE.Vector3(hs, 0, 0), new THREE.Vector3(1, 0, 0)))
|
|
|
|
this.add(new CubePlanetNode(this, 4, 0, new THREE.Vector3(0, hs, 0), new THREE.Vector3(0, 1, 0)))
|
|
this.add(new CubePlanetNode(this, 5, 0, new THREE.Vector3(0, -hs, 0), new THREE.Vector3(0, -1, 0)))
|
|
}
|
|
|
|
update (camera, dt) {
|
|
for (const i in this.children) {
|
|
this.children[i].update(camera, dt)
|
|
}
|
|
}
|
|
|
|
setMaterial (mat) {
|
|
this.material = mat
|
|
for (const i in this.children) {
|
|
this.children[i].setMaterial(mat)
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = { PlanetGenerator, CubePlanet, CubePlanetNode }
|