diff --git a/src/engine/components/planet/index.js b/src/engine/components/planet/index.js index 6d0ae77..483733b 100644 --- a/src/engine/components/planet/index.js +++ b/src/engine/components/planet/index.js @@ -7,33 +7,35 @@ class CubeFace { this.parent = parent this.children = [] - this.position = pos this.normal = normal this.resolution = resolution this.radius = radius - this.level = 0 + this.level = level this.generated = false - // Calculate left and forward vectors from the normal + // 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 + this.transform = mat4.create() mat4.fromTranslation(this.transform, this.position) + // 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 cpoint = subv3(this.position, mulv3(this.left, this.radius / 2)) - cpoint = subv3(cpoint, mulv3(this.forward, this.radius / 2)) - let VERTICES = this.resolution - let count = VERTICES * VERTICES + let count = (VERTICES - 1) * (VERTICES - 1) let vertices = new Array(count * 3) // let normals = new Array(count * 3) let textureCoords = new Array(count * 2) @@ -41,15 +43,16 @@ class CubeFace { for (let i = 0, vertexPointer = 0; i < VERTICES; i++) { for (let j = 0; j < VERTICES; j++, vertexPointer++) { - let isize = i / VERTICES * this.radius - let jsize = j / VERTICES * this.radius + // 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(this.left, isize) - let jv = mulv3(this.forward, jsize) + 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(cpoint.slice(0), addv3(iv, jv)) + 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) @@ -60,8 +63,8 @@ class CubeFace { // normals[vertexPointer * 3] = normal[0] // normals[vertexPointer * 3 + 1] = normal[1] // normals[vertexPointer * 3 + 2] = normal[2] - textureCoords[vertexPointer * 2] = j / (VERTICES - 1) - textureCoords[vertexPointer * 2 + 1] = i / (VERTICES - 1) + textureCoords[vertexPointer * 2] = j * (1 / VERTICES) + textureCoords[vertexPointer * 2 + 1] = i * (1 / VERTICES) } } @@ -100,17 +103,17 @@ class CubePlanet { this.resolution = resolution this.radius = radius - let hs = resolution / 2 + 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, [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, [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 + 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 ] } diff --git a/src/engine/utility.js b/src/engine/utility.js index ab6081a..b7df9d1 100644 --- a/src/engine/utility.js +++ b/src/engine/utility.js @@ -53,7 +53,7 @@ export function divv3 (one, two) { } export function normalv3 (vec) { - let res = [] + let res = vec vec3.normalize(res, vec) return res } diff --git a/src/index.js b/src/index.js index 6b4836f..e472639 100644 --- a/src/index.js +++ b/src/index.js @@ -34,11 +34,11 @@ async function pipeline () { terrain.setMaterial(material) // Create and initialize the camera - let cam = new Camera([-100.0, 1.0, 0.0]) + let cam = new Camera([-60.0, 1.0, 0.0]) cam.updateProjection(game.gl) // Planet test - let planet = new CubePlanet([0.0, 0.0, 0.0], 16, 32) + let planet = new CubePlanet([0.0, 0.0, 0.0], 16, 36) // Update function for camera game.addUpdateFunction(function (dt) {