1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| import * as THREE from "three";
export default class Wall extends THREE.Mesh { constructor(wallPoints, faceRelation) { super(); this.wallPoints = wallPoints; this.faceRelation = faceRelation; this.init(); } init() { let wallPoints = this.wallPoints; wallPoints.forEach((item) => { item.x = item.x / 100; item.y = item.y / 100; item.z = item.z / 100; });
let faceIndexs = [ [0, 1, 2, 3], [4, 5, 6, 7], [0, 3, 6, 5], [2, 1, 4, 7], [1, 0, 5, 4], [3, 2, 7, 6], ]; let mIndex = []; faceIndexs.forEach((item) => { let faceItem; let isFace = this.faceRelation.some((face) => { faceItem = face; return ( item.includes(face.index[0]) && item.includes(face.index[1]) && item.includes(face.index[2]) && item.includes(face.index[3]) ); }); if (isFace) { mIndex.push(faceItem.panorama); } else { mIndex.push(0); } });
let faces = faceIndexs.map((item, index) => { return [ [wallPoints[item[0]].x, wallPoints[item[0]].z, wallPoints[item[0]].y], [wallPoints[item[1]].x, wallPoints[item[1]].z, wallPoints[item[1]].y], [wallPoints[item[2]].x, wallPoints[item[2]].z, wallPoints[item[2]].y], [wallPoints[item[3]].x, wallPoints[item[3]].z, wallPoints[item[3]].y], ]; });
let positions = []; let uvs = []; let indices = []; let nomarls = []; let faceNormals = [ [0, -1, 0], [0, 1, 0], [-1, 0, 0], [1, 0, 0], [0, 0, 1], [0, 0, -1], ]; let materialGroups = [];
for (let i = 0; i < faces.length; i++) { let point = faces[i]; let facePositions = []; let faceUvs = []; let faceIndices = [];
facePositions.push(...point[0], ...point[1], ...point[2], ...point[3]); faceUvs.push(0, 0, 1, 0, 1, 1, 0, 1); faceIndices.push( 0 + i * 4, 2 + i * 4, 1 + i * 4, 0 + i * 4, 3 + i * 4, 2 + i * 4 );
positions.push(...facePositions); uvs.push(...faceUvs); indices.push(...faceIndices); nomarls.push( ...faceNormals[i], ...faceNormals[i], ...faceNormals[i], ...faceNormals[i] );
materialGroups.push({ start: i * 6, count: 6, materialIndex: i, }); }
let geometry = new THREE.BufferGeometry(); geometry.setAttribute( "position", new THREE.Float32BufferAttribute(positions, 3) ); geometry.setAttribute("uv", new THREE.Float32BufferAttribute(uvs, 2)); geometry.setAttribute( "normal", new THREE.Float32BufferAttribute(nomarls, 3) ); geometry.setIndex(new THREE.BufferAttribute(new Uint16Array(indices), 1)); geometry.groups = materialGroups;
this.geometry = geometry; this.material = mIndex.map((item) => { if (item == 0) { return new THREE.MeshBasicMaterial({ color: 0x333333 }); } else { return item.material; } }); } }
|