mirror of
https://github.com/CesiumGS/obj2gltf.git
synced 2024-11-27 10:30:16 -05:00
cleanup edits
This commit is contained in:
parent
9da383e434
commit
563d5243a2
207
lib/loadObj.js
207
lib/loadObj.js
@ -100,12 +100,11 @@ function loadObj(objPath, options) {
|
||||
var lineBuffer = '';
|
||||
|
||||
// Used for parsing face data
|
||||
var faceVertices = []; // names of vertices for caching
|
||||
var facePositions = []; // indices into position array
|
||||
var faceUvs = []; // indices into uv array
|
||||
var faceNormals = []; // indices into normal array
|
||||
var faceVertices = [];
|
||||
var facePositions = [];
|
||||
var faceUvs = [];
|
||||
var faceNormals = [];
|
||||
|
||||
var positions3D = [];
|
||||
var vertexIndices = [];
|
||||
|
||||
function getName(name) {
|
||||
@ -152,95 +151,6 @@ function loadObj(objPath, options) {
|
||||
primitive.material = getName(name);
|
||||
}
|
||||
|
||||
var intPoint = new Cartesian3();
|
||||
var xAxis = Cesium.Cartesian3.UNIT_X.clone();
|
||||
var yAxis = Cesium.Cartesian3.UNIT_Y.clone();
|
||||
var zAxis = Cesium.Cartesian3.UNIT_Z.clone();
|
||||
var origin = new Cartesian3();
|
||||
var normal = new Cartesian3();
|
||||
var ray = new Ray();
|
||||
var plane = new Plane(Cesium.Cartesian3.UNIT_X, 0);
|
||||
|
||||
function projectTo2D(positions) {
|
||||
var i;
|
||||
var positions2D = new Array(positions.length);
|
||||
var obb = OrientedBoundingBox.fromPoints(positions);
|
||||
var halfAxes = obb.halfAxes;
|
||||
Matrix3.getColumn(halfAxes, 0, xAxis);
|
||||
Matrix3.getColumn(halfAxes, 1, yAxis);
|
||||
Matrix3.getColumn(halfAxes, 2, zAxis);
|
||||
|
||||
var xMag = Cartesian3.magnitude(xAxis);
|
||||
var yMag = Cartesian3.magnitude(yAxis);
|
||||
var zMag = Cartesian3.magnitude(zAxis);
|
||||
var min = Math.min(xMag, yMag, zMag);
|
||||
|
||||
// If all the points are on a line, just remove one of the zero dimensions
|
||||
if (xMag === 0 && (yMag === 0 || zMag === 0)) {
|
||||
for (i = 0; i < positions.length; i++) {
|
||||
positions2D[i] = new Cartesian2(positions[i].y, positions[i].z);
|
||||
}
|
||||
return positions2D;
|
||||
} else if (yMag === 0 && zMag === 0) {
|
||||
for (i = 0; i < positions.length; i++) {
|
||||
positions2D[i] = new Cartesian2(positions[i].x, positions[i].y);
|
||||
}
|
||||
return positions2D;
|
||||
}
|
||||
|
||||
var center = obb.center;
|
||||
var planeXAxis;
|
||||
var planeYAxis;
|
||||
if (min === xMag) {
|
||||
if (!xAxis.equals(Cartesian3.ZERO)) {
|
||||
Cartesian3.add(center, xAxis, origin);
|
||||
Cartesian3.normalize(xAxis, normal);
|
||||
}
|
||||
planeXAxis = Cartesian3.normalize(yAxis, yAxis);
|
||||
planeYAxis = Cartesian3.normalize(zAxis, zAxis);
|
||||
} else if (min === yMag) {
|
||||
if (!yAxis.equals(Cartesian3.ZERO)) {
|
||||
Cartesian3.add(center, yAxis, origin);
|
||||
Cartesian3.normalize(yAxis, normal);
|
||||
}
|
||||
planeXAxis = Cartesian3.normalize(xAxis, xAxis);
|
||||
planeYAxis = Cartesian3.normalize(zAxis, zAxis);
|
||||
} else {
|
||||
if (!zAxis.equals(Cartesian3.ZERO)) {
|
||||
Cartesian3.add(center, zAxis, origin);
|
||||
Cartesian3.normalize(zAxis, normal);
|
||||
}
|
||||
planeXAxis = Cartesian3.normalize(xAxis, xAxis);
|
||||
planeYAxis = Cartesian3.normalize(yAxis, yAxis);
|
||||
}
|
||||
|
||||
if (min === 0) {
|
||||
normal = Cartesian3.cross(planeXAxis, planeYAxis, normal);
|
||||
normal = Cartesian3.normalize(normal, normal);
|
||||
}
|
||||
|
||||
Plane.fromPointNormal(origin, normal, plane);
|
||||
ray.direction = normal;
|
||||
|
||||
for (i = 0; i < positions.length; i++) {
|
||||
ray.origin = positions[i];
|
||||
|
||||
var intersectionPoint = IntersectionTests.rayPlane(ray, plane, intPoint);
|
||||
|
||||
if (!defined(intersectionPoint)) {
|
||||
Cartesian3.negate(ray.direction, ray.direction);
|
||||
intersectionPoint = IntersectionTests.rayPlane(ray, plane, intPoint);
|
||||
}
|
||||
var v = Cartesian3.subtract(intersectionPoint, origin, intersectionPoint);
|
||||
var x = Cartesian3.dot(planeXAxis, v);
|
||||
var y = Cartesian3.dot(planeYAxis, v);
|
||||
|
||||
positions2D[i] = new Cartesian2(x, y);
|
||||
}
|
||||
|
||||
return positions2D;
|
||||
}
|
||||
|
||||
function getOffset(a, attributeData, components) {
|
||||
var i = parseInt(a);
|
||||
if (i < 0) {
|
||||
@ -301,6 +211,106 @@ function loadObj(objPath, options) {
|
||||
return index;
|
||||
}
|
||||
|
||||
// Given a set of 3D points, project them onto whichever axis will produce the least distortion.
|
||||
var scratchIntersectionPoint = new Cartesian3();
|
||||
var scratchXAxis = new Cartesian3();
|
||||
var scratchYAxis = new Cartesian3();
|
||||
var scratchZAxis = new Cartesian3();
|
||||
var scratchOrigin = new Cartesian3();
|
||||
var scratchNormal = new Cartesian3();
|
||||
var scratchRay = new Ray();
|
||||
var scratchPlane = new Plane(Cesium.Cartesian3.UNIT_X, 0);
|
||||
var scratchPositions2D = [new Cartesian2(), new Cartesian2(), new Cartesian2()];
|
||||
function projectTo2D(positions) {
|
||||
var positions2D = [];
|
||||
var obb = OrientedBoundingBox.fromPoints(positions);
|
||||
var halfAxes = obb.halfAxes;
|
||||
Matrix3.getColumn(halfAxes, 0, scratchXAxis);
|
||||
Matrix3.getColumn(halfAxes, 1, scratchYAxis);
|
||||
Matrix3.getColumn(halfAxes, 2, scratchZAxis);
|
||||
|
||||
var xMag = Cartesian3.magnitude(scratchXAxis);
|
||||
var yMag = Cartesian3.magnitude(scratchYAxis);
|
||||
var zMag = Cartesian3.magnitude(scratchZAxis);
|
||||
var min = Math.min(xMag, yMag, zMag);
|
||||
|
||||
var i;
|
||||
// If all the points are on a line, just remove one of the zero dimensions
|
||||
if (xMag === 0 && (yMag === 0 || zMag === 0)) {
|
||||
for (i = 0; i < positions.length; i++) {
|
||||
if (i === scratchPositions2D.length) {
|
||||
scratchPositions2D.push(new Cartesian2());
|
||||
}
|
||||
positions2D[i] = new Cartesian2.fromElements(positions[i].y, positions[i].z, scratchPositions2D[i]);
|
||||
}
|
||||
return positions2D;
|
||||
} else if (yMag === 0 && zMag === 0) {
|
||||
for (i = 0; i < positions.length; i++) {
|
||||
if (i === scratchPositions2D.length) {
|
||||
scratchPositions2D.push(new Cartesian2());
|
||||
}
|
||||
positions2D[i] = new Cartesian2.fromElements(positions[i].x, positions[i].y, scratchPositions2D[i]);
|
||||
}
|
||||
return positions2D;
|
||||
}
|
||||
|
||||
var center = obb.center;
|
||||
var planeXAxis;
|
||||
var planeYAxis;
|
||||
if (min === xMag) {
|
||||
if (!scratchXAxis.equals(Cartesian3.ZERO)) {
|
||||
Cartesian3.add(center, scratchXAxis, scratchOrigin);
|
||||
Cartesian3.normalize(scratchXAxis, scratchNormal);
|
||||
}
|
||||
planeXAxis = Cartesian3.normalize(scratchYAxis, scratchYAxis);
|
||||
planeYAxis = Cartesian3.normalize(scratchZAxis, scratchZAxis);
|
||||
} else if (min === yMag) {
|
||||
if (!scratchYAxis.equals(Cartesian3.ZERO)) {
|
||||
Cartesian3.add(center, scratchYAxis, scratchOrigin);
|
||||
Cartesian3.normalize(scratchYAxis, scratchNormal);
|
||||
}
|
||||
planeXAxis = Cartesian3.normalize(scratchXAxis, scratchXAxis);
|
||||
planeYAxis = Cartesian3.normalize(scratchZAxis, scratchZAxis);
|
||||
} else {
|
||||
if (!scratchZAxis.equals(Cartesian3.ZERO)) {
|
||||
Cartesian3.add(center, scratchZAxis, scratchOrigin);
|
||||
Cartesian3.normalize(scratchZAxis, scratchNormal);
|
||||
}
|
||||
planeXAxis = Cartesian3.normalize(scratchXAxis, scratchXAxis);
|
||||
planeYAxis = Cartesian3.normalize(scratchYAxis, scratchYAxis);
|
||||
}
|
||||
|
||||
if (min === 0) {
|
||||
scratchNormal = Cartesian3.cross(planeXAxis, planeYAxis, scratchNormal);
|
||||
scratchNormal = Cartesian3.normalize(scratchNormal, scratchNormal);
|
||||
}
|
||||
|
||||
Plane.fromPointNormal(scratchOrigin, scratchNormal, scratchPlane);
|
||||
scratchRay.direction = scratchNormal;
|
||||
|
||||
for (i = 0; i < positions.length; i++) {
|
||||
scratchRay.origin = positions[i];
|
||||
|
||||
var intersectionPoint = IntersectionTests.rayPlane(scratchRay, scratchPlane, scratchIntersectionPoint);
|
||||
|
||||
if (!defined(intersectionPoint)) {
|
||||
Cartesian3.negate(scratchRay.direction, scratchRay.direction);
|
||||
intersectionPoint = IntersectionTests.rayPlane(scratchRay, scratchPlane, scratchIntersectionPoint);
|
||||
}
|
||||
var v = Cartesian3.subtract(intersectionPoint, scratchOrigin, intersectionPoint);
|
||||
var x = Cartesian3.dot(planeXAxis, v);
|
||||
var y = Cartesian3.dot(planeYAxis, v);
|
||||
|
||||
if (i === scratchPositions2D.length) {
|
||||
scratchPositions2D.push(new Cartesian2());
|
||||
}
|
||||
|
||||
positions2D[i] = new Cartesian2.fromElements(x, y, scratchPositions2D[i]);
|
||||
}
|
||||
|
||||
return positions2D;
|
||||
}
|
||||
|
||||
function get3DPoint(index, result) {
|
||||
var pi = getOffset(index, positions, 3);
|
||||
var px = positions.get(pi + 0);
|
||||
@ -365,7 +375,7 @@ function loadObj(objPath, options) {
|
||||
}
|
||||
}
|
||||
|
||||
var scratchNormal = new Cartesian3();
|
||||
var scratchPositions3D = [new Cartesian3(), new Cartesian3(), new Cartesian3()];
|
||||
function addFace(vertices, positions, uvs, normals) {
|
||||
var isWindingCorrect = true;
|
||||
var faceNormal;
|
||||
@ -383,7 +393,7 @@ function loadObj(objPath, options) {
|
||||
var index3 = addVertex(vertices[2], positions[2], uvs[2], normals[2]);
|
||||
addTriangle(index1, index2, index3, isWindingCorrect);
|
||||
} else { // Triangulate if the face is not a triangle
|
||||
positions3D.length = 0;
|
||||
var positions3D = [];
|
||||
vertexIndices.length = 0;
|
||||
|
||||
var i;
|
||||
@ -392,7 +402,10 @@ function loadObj(objPath, options) {
|
||||
vertexIndices.push(index);
|
||||
|
||||
// Collect the vertex positions as 3D points
|
||||
positions3D.push(get3DPoint(positions[i], new Cartesian3()));
|
||||
if (i === scratchPositions3D.length) {
|
||||
scratchPositions3D.push(new Cartesian3());
|
||||
}
|
||||
positions3D.push(get3DPoint(positions[i], scratchPositions3D[i]));
|
||||
}
|
||||
|
||||
var positions2D = projectTo2D(positions3D);
|
||||
|
Loading…
Reference in New Issue
Block a user