update dependencies and ditch the planet shit
This commit is contained in:
parent
7502888d03
commit
8c43cd72bd
16
package.json
16
package.json
@ -16,19 +16,19 @@
|
||||
"author": "Evert \"Diamond\" Prants <evert@lunasqu.ee>",
|
||||
"license": "LGPL-3.0-or-later",
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.1.6",
|
||||
"@babel/plugin-transform-runtime": "^7.1.0",
|
||||
"@babel/preset-env": "^7.1.6",
|
||||
"babel-loader": "^8.0.4",
|
||||
"express": "^4.16.4",
|
||||
"@babel/core": "^7.7.4",
|
||||
"@babel/plugin-transform-runtime": "^7.7.4",
|
||||
"@babel/preset-env": "^7.7.4",
|
||||
"babel-loader": "^8.0.6",
|
||||
"express": "^4.17.1",
|
||||
"html-webpack-plugin": "^3.2.0",
|
||||
"standard": "^12.0.1",
|
||||
"webpack": "^4.26.0",
|
||||
"webpack": "^4.41.2",
|
||||
"webpack-command": "^0.4.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.1.5",
|
||||
"@babel/runtime": "^7.7.4",
|
||||
"gl-matrix": "^2.8.1",
|
||||
"open-simplex-noise": "^1.5.0"
|
||||
"open-simplex-noise": "^1.7.0"
|
||||
}
|
||||
}
|
||||
|
@ -1,214 +0,0 @@
|
||||
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 }
|
10
src/index.js
10
src/index.js
@ -7,8 +7,6 @@ import { Terrain } from './engine/components/terrain'
|
||||
import { SimplexHeightMap } from './engine/components/terrain/heightmap'
|
||||
import { Material } from './engine/mesh/material'
|
||||
|
||||
import { CubePlanet } from './engine/components/planet'
|
||||
|
||||
let game = new Engine()
|
||||
let env = new Environment()
|
||||
|
||||
@ -37,9 +35,6 @@ async function pipeline () {
|
||||
let cam = new Camera([-200.0, 1.0, 0.0])
|
||||
cam.updateProjection(game.gl)
|
||||
|
||||
// Planet test
|
||||
let planet = new CubePlanet([0.0, 0.0, 0.0], 16, 512)
|
||||
|
||||
// Update function for camera
|
||||
let face = 0
|
||||
game.addUpdateFunction(function (dt) {
|
||||
@ -62,7 +57,6 @@ async function pipeline () {
|
||||
// TESTING: Move model forward
|
||||
// t = t + 0.1
|
||||
// entity.setPosition([t, 0.0, 0.0])
|
||||
planet.update(face++, cam)
|
||||
if (face > 5) face = 0
|
||||
})
|
||||
|
||||
@ -71,8 +65,7 @@ async function pipeline () {
|
||||
shader.use(gl)
|
||||
cam.draw(gl, shader)
|
||||
entity.draw(gl, shader)
|
||||
planet.draw(gl, shader)
|
||||
/*
|
||||
|
||||
// Use terrain shader
|
||||
terrainShader.use(gl)
|
||||
|
||||
@ -84,7 +77,6 @@ async function pipeline () {
|
||||
|
||||
// Draw terrain
|
||||
terrain.draw(gl, terrainShader)
|
||||
*/
|
||||
})
|
||||
|
||||
game.startGameLoop()
|
||||
|
Loading…
Reference in New Issue
Block a user