a.prototype.getUVAndNormal = function(vertices){
// vectices = [x,y,z,x,y,z,x,y,z...]三个数据为一个顶点
// count 顶点数量
let count = vertices.length/3, normals = [], uvs = []
// 三个顶点构成一个三角形,组成三角网格
for (let i = 0, max = count; i < max; i += 3){
let index1 = 3*i, index2 = 3*(i+1), index3 = 3*(i+2);
// 构造三角形的三个顶点坐标
let vert1 = new Vector3(vertices[index1], vertices[index1+1],vertices[index1+2])
let vert2 = new Vector3(vertices[index2], vertices[index2+1],vertices[index2+2])
let vert3 = new Vector3(vertices[index3], vertices[index3+1],vertices[index3+2])
let noraml = Vector3.Cross(
new Vector3(vert2.x-vert1.x, vert2.y-vert1.y, vert2.z-vert1.z),
new vector3(vert3.x-vert2.x, vert3.y-vert2.y, vert3.z-vert2.z)
).normalize();
normals.push(noraml.x, noraml.y, noraml.z,noraml.x, noraml.y, noraml.z,noraml.x, noraml.y, noraml.z);
if (Math.abs(noraml.x) > Math.abs(noraml.y) && Math.abs(noraml.x) > Math.abs(noraml.z)) {
uvs.push(vert1.z, vert1.y, vert2.z, vert2.y, vert3.z, vert3.y);
} else if (Math.abs(noraml.y) > Math.abs(noraml.x) && Math.abs(noraml.y) > Math.abs(noraml.z)) {
uvs.push(vert1.x, vert1.z, vert2.x, vert2.z, vert3.x, vert3.z);
} else {
uvs.push(vert1.x, vert1.y, vert2.x, vert2.y, vert3.x, vert3.y);
}
}
return {normals:normals, uvs:uvs}
};
得到了UV值就可以进行贴图了