Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     generateRoofsOld(tiles, name, yOff, W) {
  2.         let materials = [];
  3.         let materialMap = {};
  4.  
  5.         let geometry = new THREE.Geometry();
  6.         let curId = 0;
  7.         for (let x = 0; x <= wSIZE; x++) {
  8.             for (let y = 0; y <= wSIZE; y++) {
  9.                 let tile = tiles[x][y];
  10.                 tile.x = x;
  11.                 tile.y = y;
  12.    
  13.                 if (!tile.buildings) continue;
  14.                 if (!tile.buildings[name]) continue;
  15.                 if (!tile.buildings[name].roof) continue;
  16.    
  17.                 // roof drawing is complicated.
  18.                 // this impl is actually broken due to the left edge of the map not knowing about the neighbor
  19.                
  20.                 let left = x > 0 ? x - 1 : x;
  21.                 let right = x + 1;
  22.                 let up = y > 0 ? y - 1 : y;
  23.                 let down = y + 1;
  24.    
  25.                 let elevated = 0;
  26.    
  27.                 let v0; let e0 = false;
  28.                 if (this.isRoof(tiles[left][up], name) && this.isRoof(tiles[x][up], name) && this.isRoof(tiles[left][y], name)) {
  29.                     v0 = new THREE.Vector3(x, tile.elevation + yOff + ROOF_HEIGHT, y);
  30.                     elevated++;
  31.                     e0 = true;
  32.                 } else {
  33.                     v0 = new THREE.Vector3(x, tile.elevation + yOff, y);
  34.                 }
  35.    
  36.                 let v1; let e1 = false;
  37.                 if (this.isRoof(tiles[right][up], name) && this.isRoof(tiles[x][up], name) && this.isRoof(tiles[right][y], name)) {
  38.                     v1 = new THREE.Vector3(x + 1, tiles[right][y].elevation + yOff + ROOF_HEIGHT, y);
  39.                     elevated++;
  40.                     e1 = true;
  41.                 } else {
  42.                     v1 = new THREE.Vector3(x + 1, tiles[right][y].elevation + yOff, y);
  43.                 }
  44.    
  45.                 let v2; let e2 = false;
  46.                 if (this.isRoof(tiles[right][down], name) && this.isRoof(tiles[right][y], name) && this.isRoof(tiles[x][down], name)) {
  47.                     v2 = new THREE.Vector3(x + 1, tiles[right][down].elevation + yOff + ROOF_HEIGHT, y + 1);
  48.                     elevated++;
  49.                     e2 = true;
  50.                 } else {
  51.                     v2 = new THREE.Vector3(x + 1, tiles[right][down].elevation + yOff, y + 1);
  52.                 }
  53.    
  54.                 let v3; let e3 = false;
  55.                 if (this.isRoof(tiles[left][down], name) && this.isRoof(tiles[left][y], name) && this.isRoof(tiles[x][down], name)) {
  56.                     v3 = new THREE.Vector3(x, tiles[x][down].elevation + yOff + ROOF_HEIGHT, y + 1);
  57.                     elevated++;
  58.                     e3 = true;
  59.                 } else {
  60.                     v3 = new THREE.Vector3(x, tiles[x][down].elevation + yOff, y + 1);
  61.                 }
  62.    
  63.                 //let geometry = elevated == 4 ? geometries[roofType + '-top'] : geometries[roofType + '-side']
  64.                 let tt = this.roofs[tile.buildings[name].roof.type];
  65.                 let materialIndex = this.materialIndex(
  66.                     materials, materialMap, 'basic',
  67.                     elevated == 4 ? tt.top : tt.side);
  68.    
  69.                 geometry.vertices.push(v0);
  70.                 let A = curId++;
  71.    
  72.                 geometry.vertices.push(v1);
  73.                 let B = curId++;
  74.    
  75.                 geometry.vertices.push(v2);
  76.                 let C = curId++;
  77.    
  78.                 geometry.vertices.push(v3);
  79.                 let D = curId++;
  80.    
  81.                 if (elevated == 0) {
  82.                     // do whatever
  83.                     geometry.faces.push(face(A, B, C, materialIndex));
  84.                     geometry.faceVertexUvs[0].push([u0, u1, u2]);
  85.    
  86.                     geometry.faces.push(face(C, D, A, materialIndex));
  87.                     geometry.faceVertexUvs[0].push([u2, u3, u0]);
  88.                 } else if (elevated == 1) {
  89.                     // outer corner
  90.                     if (e0) {
  91.                         geometry.faces.push(face(A, B, C, materialIndex));
  92.                         geometry.faceVertexUvs[0].push([u2, u1, u0]);
  93.    
  94.                         geometry.faces.push(face(C, D, A, materialIndex));
  95.                         geometry.faceVertexUvs[0].push([u1, u0, u3]);
  96.                     } else if (e1) {
  97.                         geometry.faces.push(face(B, C, D, materialIndex));
  98.                         geometry.faceVertexUvs[0].push([u2, u1, u0]);
  99.    
  100.                         geometry.faces.push(face(B, D, A, materialIndex));
  101.                         geometry.faceVertexUvs[0].push([u3, u1, u0]);
  102.                     } else if (e2) {
  103.                         geometry.faces.push(face(A, B, C, materialIndex));
  104.                         geometry.faceVertexUvs[0].push([u0, u1, u2]);
  105.    
  106.                         geometry.faces.push(face(C, D, A, materialIndex));
  107.                         geometry.faceVertexUvs[0].push([u3, u0, u1]);
  108.                     } else { // e3
  109.                         geometry.faces.push(face(B, C, D, materialIndex));
  110.                         geometry.faceVertexUvs[0].push([u1, u0, u3]);
  111.    
  112.                         geometry.faces.push(face(B, D, A, materialIndex));
  113.                         geometry.faceVertexUvs[0].push([u0, u2, u1]);
  114.                     }
  115.                 } else if (elevated == 2) {
  116.                     if (e0 && e1) {
  117.                         geometry.faces.push(face(A, B, C, materialIndex));
  118.                         geometry.faceVertexUvs[0].push([u3, u2, u1]);
  119.    
  120.                         geometry.faces.push(face(C, D, A, materialIndex));
  121.                         geometry.faceVertexUvs[0].push([u1, u0, u3]);
  122.                     } else if (e1 && e2) {
  123.                         geometry.faces.push(face(A, B, C, materialIndex));
  124.                         geometry.faceVertexUvs[0].push([u1, u2, u3]);
  125.    
  126.                         geometry.faces.push(face(C, D, A, materialIndex));
  127.                         geometry.faceVertexUvs[0].push([u3, u0, u1]);
  128.                     } else if (e2 && e3) {
  129.                         geometry.faces.push(face(A, B, C, materialIndex));
  130.                         geometry.faceVertexUvs[0].push([u1, u0, u3]);
  131.    
  132.                         geometry.faces.push(face(C, D, A, materialIndex));
  133.                         geometry.faceVertexUvs[0].push([u3, u2, u1]);
  134.                     } else if (e3 && e0) {
  135.                         geometry.faces.push(face(A, B, C, materialIndex));
  136.                         geometry.faceVertexUvs[0].push([u3, u0, u1]);
  137.    
  138.                         geometry.faces.push(face(C, D, A, materialIndex));
  139.                         geometry.faceVertexUvs[0].push([u1, u2, u3]);
  140.                     } else if (e0 && e2) {
  141.                         console.log("Have not implemented roofs at " + x + "," + y);
  142.                     } else if (e1 && e3) {
  143.                         console.log("Have not implemented roofs at " + x + "," + y);
  144.                     } else {
  145.                         console.log("Weird roofs at " + x + "," + y);
  146.                     }
  147.    
  148.                 } else if (elevated == 3) {
  149.                     if (!e0) {
  150.                         geometry.faces.push(face(A, B, C, materialIndex));
  151.                         geometry.faceVertexUvs[0].push([u1, u2, u3]);
  152.    
  153.                         geometry.faces.push(face(C, D, A, materialIndex));
  154.                         geometry.faceVertexUvs[0].push([u2, u3, u0]);
  155.                     } else if (!e1) {
  156.                         geometry.faces.push(face(B, C, D, materialIndex));
  157.                         geometry.faceVertexUvs[0].push([u1, u2, u3]);
  158.    
  159.                         geometry.faces.push(face(B, D, A, materialIndex));
  160.                         geometry.faceVertexUvs[0].push([u0, u2, u3]);
  161.                     } else if (!e2) {
  162.                         geometry.faces.push(face(A, B, C, materialIndex));
  163.                         geometry.faceVertexUvs[0].push([u3, u2, u1]);
  164.    
  165.                         geometry.faces.push(face(C, D, A, materialIndex));
  166.                         geometry.faceVertexUvs[0].push([u0, u3, u2]);
  167.                     } else if (!e3) {
  168.                         geometry.faces.push(face(B, C, D, materialIndex));
  169.                         geometry.faceVertexUvs[0].push([u2, u3, u0]);
  170.    
  171.                         geometry.faces.push(face(B, D, A, materialIndex));
  172.                         geometry.faceVertexUvs[0].push([u3, u1, u2]);
  173.                     }
  174.                 } else {
  175.                     // do whatever
  176.                     geometry.faces.push(face(A, B, C, materialIndex));
  177.                     geometry.faceVertexUvs[0].push([u0, u1, u2]);
  178.    
  179.                     geometry.faces.push(face(C, D, A, materialIndex));
  180.                     geometry.faceVertexUvs[0].push([u2, u3, u0]);
  181.                 }
  182.             }
  183.         }
  184.  
  185.         let mesh = new THREE.Mesh(geometry, materials);
  186.         mesh.matrixAutoUpdate = false;
  187.         W.add(mesh);
  188.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement