Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- generateRoofsOld(tiles, name, yOff, W) {
- let materials = [];
- let materialMap = {};
- let geometry = new THREE.Geometry();
- let curId = 0;
- for (let x = 0; x <= wSIZE; x++) {
- for (let y = 0; y <= wSIZE; y++) {
- let tile = tiles[x][y];
- tile.x = x;
- tile.y = y;
- if (!tile.buildings) continue;
- if (!tile.buildings[name]) continue;
- if (!tile.buildings[name].roof) continue;
- // roof drawing is complicated.
- // this impl is actually broken due to the left edge of the map not knowing about the neighbor
- let left = x > 0 ? x - 1 : x;
- let right = x + 1;
- let up = y > 0 ? y - 1 : y;
- let down = y + 1;
- let elevated = 0;
- let v0; let e0 = false;
- if (this.isRoof(tiles[left][up], name) && this.isRoof(tiles[x][up], name) && this.isRoof(tiles[left][y], name)) {
- v0 = new THREE.Vector3(x, tile.elevation + yOff + ROOF_HEIGHT, y);
- elevated++;
- e0 = true;
- } else {
- v0 = new THREE.Vector3(x, tile.elevation + yOff, y);
- }
- let v1; let e1 = false;
- if (this.isRoof(tiles[right][up], name) && this.isRoof(tiles[x][up], name) && this.isRoof(tiles[right][y], name)) {
- v1 = new THREE.Vector3(x + 1, tiles[right][y].elevation + yOff + ROOF_HEIGHT, y);
- elevated++;
- e1 = true;
- } else {
- v1 = new THREE.Vector3(x + 1, tiles[right][y].elevation + yOff, y);
- }
- let v2; let e2 = false;
- if (this.isRoof(tiles[right][down], name) && this.isRoof(tiles[right][y], name) && this.isRoof(tiles[x][down], name)) {
- v2 = new THREE.Vector3(x + 1, tiles[right][down].elevation + yOff + ROOF_HEIGHT, y + 1);
- elevated++;
- e2 = true;
- } else {
- v2 = new THREE.Vector3(x + 1, tiles[right][down].elevation + yOff, y + 1);
- }
- let v3; let e3 = false;
- if (this.isRoof(tiles[left][down], name) && this.isRoof(tiles[left][y], name) && this.isRoof(tiles[x][down], name)) {
- v3 = new THREE.Vector3(x, tiles[x][down].elevation + yOff + ROOF_HEIGHT, y + 1);
- elevated++;
- e3 = true;
- } else {
- v3 = new THREE.Vector3(x, tiles[x][down].elevation + yOff, y + 1);
- }
- //let geometry = elevated == 4 ? geometries[roofType + '-top'] : geometries[roofType + '-side']
- let tt = this.roofs[tile.buildings[name].roof.type];
- let materialIndex = this.materialIndex(
- materials, materialMap, 'basic',
- elevated == 4 ? tt.top : tt.side);
- geometry.vertices.push(v0);
- let A = curId++;
- geometry.vertices.push(v1);
- let B = curId++;
- geometry.vertices.push(v2);
- let C = curId++;
- geometry.vertices.push(v3);
- let D = curId++;
- if (elevated == 0) {
- // do whatever
- geometry.faces.push(face(A, B, C, materialIndex));
- geometry.faceVertexUvs[0].push([u0, u1, u2]);
- geometry.faces.push(face(C, D, A, materialIndex));
- geometry.faceVertexUvs[0].push([u2, u3, u0]);
- } else if (elevated == 1) {
- // outer corner
- if (e0) {
- geometry.faces.push(face(A, B, C, materialIndex));
- geometry.faceVertexUvs[0].push([u2, u1, u0]);
- geometry.faces.push(face(C, D, A, materialIndex));
- geometry.faceVertexUvs[0].push([u1, u0, u3]);
- } else if (e1) {
- geometry.faces.push(face(B, C, D, materialIndex));
- geometry.faceVertexUvs[0].push([u2, u1, u0]);
- geometry.faces.push(face(B, D, A, materialIndex));
- geometry.faceVertexUvs[0].push([u3, u1, u0]);
- } else if (e2) {
- geometry.faces.push(face(A, B, C, materialIndex));
- geometry.faceVertexUvs[0].push([u0, u1, u2]);
- geometry.faces.push(face(C, D, A, materialIndex));
- geometry.faceVertexUvs[0].push([u3, u0, u1]);
- } else { // e3
- geometry.faces.push(face(B, C, D, materialIndex));
- geometry.faceVertexUvs[0].push([u1, u0, u3]);
- geometry.faces.push(face(B, D, A, materialIndex));
- geometry.faceVertexUvs[0].push([u0, u2, u1]);
- }
- } else if (elevated == 2) {
- if (e0 && e1) {
- geometry.faces.push(face(A, B, C, materialIndex));
- geometry.faceVertexUvs[0].push([u3, u2, u1]);
- geometry.faces.push(face(C, D, A, materialIndex));
- geometry.faceVertexUvs[0].push([u1, u0, u3]);
- } else if (e1 && e2) {
- geometry.faces.push(face(A, B, C, materialIndex));
- geometry.faceVertexUvs[0].push([u1, u2, u3]);
- geometry.faces.push(face(C, D, A, materialIndex));
- geometry.faceVertexUvs[0].push([u3, u0, u1]);
- } else if (e2 && e3) {
- geometry.faces.push(face(A, B, C, materialIndex));
- geometry.faceVertexUvs[0].push([u1, u0, u3]);
- geometry.faces.push(face(C, D, A, materialIndex));
- geometry.faceVertexUvs[0].push([u3, u2, u1]);
- } else if (e3 && e0) {
- geometry.faces.push(face(A, B, C, materialIndex));
- geometry.faceVertexUvs[0].push([u3, u0, u1]);
- geometry.faces.push(face(C, D, A, materialIndex));
- geometry.faceVertexUvs[0].push([u1, u2, u3]);
- } else if (e0 && e2) {
- console.log("Have not implemented roofs at " + x + "," + y);
- } else if (e1 && e3) {
- console.log("Have not implemented roofs at " + x + "," + y);
- } else {
- console.log("Weird roofs at " + x + "," + y);
- }
- } else if (elevated == 3) {
- if (!e0) {
- geometry.faces.push(face(A, B, C, materialIndex));
- geometry.faceVertexUvs[0].push([u1, u2, u3]);
- geometry.faces.push(face(C, D, A, materialIndex));
- geometry.faceVertexUvs[0].push([u2, u3, u0]);
- } else if (!e1) {
- geometry.faces.push(face(B, C, D, materialIndex));
- geometry.faceVertexUvs[0].push([u1, u2, u3]);
- geometry.faces.push(face(B, D, A, materialIndex));
- geometry.faceVertexUvs[0].push([u0, u2, u3]);
- } else if (!e2) {
- geometry.faces.push(face(A, B, C, materialIndex));
- geometry.faceVertexUvs[0].push([u3, u2, u1]);
- geometry.faces.push(face(C, D, A, materialIndex));
- geometry.faceVertexUvs[0].push([u0, u3, u2]);
- } else if (!e3) {
- geometry.faces.push(face(B, C, D, materialIndex));
- geometry.faceVertexUvs[0].push([u2, u3, u0]);
- geometry.faces.push(face(B, D, A, materialIndex));
- geometry.faceVertexUvs[0].push([u3, u1, u2]);
- }
- } else {
- // do whatever
- geometry.faces.push(face(A, B, C, materialIndex));
- geometry.faceVertexUvs[0].push([u0, u1, u2]);
- geometry.faces.push(face(C, D, A, materialIndex));
- geometry.faceVertexUvs[0].push([u2, u3, u0]);
- }
- }
- }
- let mesh = new THREE.Mesh(geometry, materials);
- mesh.matrixAutoUpdate = false;
- W.add(mesh);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement