215 lines
6.5 KiB
JavaScript
215 lines
6.5 KiB
JavaScript
import { Mesh } from '../../mesh'
|
|
import { BoundingBox } from '../../mesh/aabb'
|
|
import { mat4, vec3 } from 'gl-matrix'
|
|
import { subv3, mulv3, addv3, normalv3, crossv3 } from '../../utility'
|
|
|
|
const lodMax = 8
|
|
|
|
class CubeFace {
|
|
constructor (parent, level, pos, normal, resolution, radius, generator) {
|
|
this.parent = parent
|
|
this.children = []
|
|
|
|
this.normal = normal
|
|
this.resolution = resolution
|
|
this.radius = radius
|
|
|
|
this.level = level
|
|
|
|
this.generated = false
|
|
this.generator = generator
|
|
|
|
// Calculate left (x) and forward (z) vectors from the normal (y)
|
|
this.left = [normal[1], normal[2], normal[0]]
|
|
this.forward = crossv3(normal, this.left)
|
|
|
|
this.position = pos
|
|
|
|
// Center the face
|
|
this.position = subv3(this.position, mulv3(this.left, this.radius / 2))
|
|
this.position = subv3(this.position, mulv3(this.forward, this.radius / 2))
|
|
|
|
this.generate()
|
|
}
|
|
|
|
generate () {
|
|
if (this.generated) return
|
|
|
|
let VERTICES = this.resolution
|
|
let count = VERTICES * VERTICES
|
|
let vertices = new Array(count * 3)
|
|
// let normals = new Array(count * 3)
|
|
let textureCoords = new Array(count * 2)
|
|
let indices = new Array(6 * (VERTICES - 1) * (VERTICES - 1))
|
|
|
|
for (let i = 0, vertexPointer = 0; i < VERTICES; i++) {
|
|
for (let j = 0; j < VERTICES; j++, vertexPointer++) {
|
|
// Vertex index (0 - 1)
|
|
let iindex = i / (VERTICES - 1)
|
|
let jindex = j / (VERTICES - 1)
|
|
|
|
// From the left and forward vectors, we can calculate an oriented vertex
|
|
let iv = mulv3(mulv3(this.left, iindex), this.radius)
|
|
let jv = mulv3(mulv3(this.forward, jindex), this.radius)
|
|
|
|
// Add the scaled left and forward to the centered origin
|
|
let vertex = addv3(this.position, addv3(iv, jv))
|
|
|
|
// Normalize and multiply by radius to create a spherical mesh
|
|
let pos = mulv3(normalv3(vertex), this.radius)
|
|
|
|
vertices[vertexPointer * 3] = pos[0]
|
|
vertices[vertexPointer * 3 + 1] = pos[1]
|
|
vertices[vertexPointer * 3 + 2] = pos[2]
|
|
// normals[vertexPointer * 3] = normal[0]
|
|
// normals[vertexPointer * 3 + 1] = normal[1]
|
|
// normals[vertexPointer * 3 + 2] = normal[2]
|
|
textureCoords[vertexPointer * 2] = j * (1 / VERTICES)
|
|
textureCoords[vertexPointer * 2 + 1] = i * (1 / VERTICES)
|
|
}
|
|
}
|
|
|
|
for (let gz = 0, pointer = 0; gz < VERTICES - 1; gz++) {
|
|
for (let gx = 0; gx < VERTICES - 1; gx++) {
|
|
let topLeft = (gz * VERTICES) + gx
|
|
let topRight = topLeft + 1
|
|
let bottomLeft = ((gz + 1) * VERTICES) + gx
|
|
let bottomRight = bottomLeft + 1
|
|
indices[pointer++] = topLeft
|
|
indices[pointer++] = bottomLeft
|
|
indices[pointer++] = topRight
|
|
indices[pointer++] = topRight
|
|
indices[pointer++] = bottomLeft
|
|
indices[pointer++] = bottomRight
|
|
}
|
|
}
|
|
|
|
this.mesh = Mesh.construct(window.gl, vertices, indices, textureCoords)
|
|
this.bounds = BoundingBox.fromMesh(this.mesh)
|
|
this.generated = true
|
|
}
|
|
|
|
dispose () {
|
|
this.mesh = null
|
|
this.generated = false
|
|
}
|
|
|
|
merge () {
|
|
if (!this.children.length) return
|
|
|
|
for (let i in this.children) {
|
|
let ch = this.children[i]
|
|
|
|
ch.merge()
|
|
ch.dispose()
|
|
}
|
|
|
|
this.children = []
|
|
}
|
|
|
|
subdivide () {
|
|
if (this.level === lodMax) return
|
|
|
|
let subPos = this.position
|
|
subPos = addv3(subPos, mulv3(this.left, this.radius / 2))
|
|
subPos = addv3(subPos, mulv3(this.forward, this.radius / 2))
|
|
|
|
let stepLeft = mulv3(this.left, this.radius / 4)
|
|
let stepForward = mulv3(this.forward, this.radius / 4)
|
|
let hs = this.radius / 2
|
|
let lv = this.level + 1
|
|
|
|
this.children = [
|
|
new CubeFace(this, lv, addv3(subv3(subPos, stepLeft), stepForward), this.normal, this.resolution, hs, this.generator),
|
|
new CubeFace(this, lv, addv3(addv3(subPos, stepLeft), stepForward), this.normal, this.resolution, hs, this.generator),
|
|
new CubeFace(this, lv, subv3(subv3(subPos, stepLeft), stepForward), this.normal, this.resolution, hs, this.generator),
|
|
new CubeFace(this, lv, subv3(addv3(subPos, stepLeft), stepForward), this.normal, this.resolution, hs, this.generator)
|
|
]
|
|
|
|
this.dispose()
|
|
}
|
|
|
|
draw (gl, shader) {
|
|
if (!this.mesh) {
|
|
for (let i in this.children) {
|
|
this.children[i].draw(gl, shader)
|
|
}
|
|
return
|
|
}
|
|
|
|
this.mesh.prepare(gl, shader)
|
|
this.mesh.draw(gl, shader)
|
|
}
|
|
}
|
|
|
|
class CubePlanet {
|
|
constructor (origin, resolution, radius, generator) {
|
|
this.origin = origin
|
|
this.resolution = resolution
|
|
this.radius = radius
|
|
|
|
this.transform = mat4.create()
|
|
mat4.fromTranslation(this.transform, origin)
|
|
|
|
let hs = radius / 2
|
|
|
|
this.faces = [
|
|
new CubeFace(this, 0, [0, 0, -hs], [0, 0, -1], resolution, radius, generator), // front
|
|
new CubeFace(this, 0, [0, 0, hs], [0, 0, 1], resolution, radius, generator), // back
|
|
|
|
new CubeFace(this, 0, [-hs, 0, 0], [-1, 0, 0], resolution, radius, generator), // left
|
|
new CubeFace(this, 0, [hs, 0, 0], [1, 0, 0], resolution, radius, generator), // right
|
|
|
|
new CubeFace(this, 0, [0, hs, 0], [0, 1, 0], resolution, radius, generator), // top
|
|
new CubeFace(this, 0, [0, -hs, 0], [0, -1, 0], resolution, radius, generator) // bottom
|
|
]
|
|
|
|
this.faces[0].subdivide()
|
|
this.faces[0].children[0].subdivide()
|
|
}
|
|
|
|
getActiveQuadTreeFaces (face, active) {
|
|
if (!face.children.length) {
|
|
active.push(face)
|
|
} else {
|
|
for (let f = 0; f < face.children.length; f++) {
|
|
this.getActiveQuadTreeFaces(face.children[f], active)
|
|
}
|
|
}
|
|
}
|
|
|
|
update (f, camera) {
|
|
let activeTree = []
|
|
this.getActiveQuadTreeFaces(this.faces[f], activeTree)
|
|
|
|
for (let a = 0; a < activeTree.length; a++) {
|
|
let dist = vec3.distance(camera.pos, activeTree[a].bounds.center)
|
|
|
|
if (dist > activeTree[a].radius) {
|
|
if (dist <= this.radius * 2.0 || activeTree[a].parent === this) {
|
|
activeTree[a].merge()
|
|
activeTree[a].generate()
|
|
} else {
|
|
activeTree[a].merge()
|
|
activeTree[a].parent.merge()
|
|
activeTree[a].parent.generate()
|
|
}
|
|
} else if (activeTree[a].radius > this.resolution && activeTree[a].level < lodMax) {
|
|
activeTree[a].subdivide()
|
|
}
|
|
}
|
|
}
|
|
|
|
draw (gl, shader) {
|
|
// Set model transform matrix uniform
|
|
const transformLocation = shader.getUniformLocation(gl, 'uModelMatrix')
|
|
gl.uniformMatrix4fv(transformLocation, false, this.transform)
|
|
|
|
for (let i in this.faces) {
|
|
this.faces[i].draw(gl, shader)
|
|
}
|
|
}
|
|
}
|
|
|
|
export { CubePlanet, CubeFace }
|