mirror of
https://github.com/CesiumGS/obj2gltf.git
synced 2025-02-07 07:22:51 -05:00
Last few tweaks
This commit is contained in:
parent
68582967ac
commit
dc1a1976db
@ -15,9 +15,10 @@ var convert = obj2gltf.convert;
|
||||
var options = {
|
||||
embedImage : false // Don't embed image in the converted glTF
|
||||
}
|
||||
convert('model.obj', 'model.gltf', options, function() {
|
||||
console.log('Converted model');
|
||||
});
|
||||
convert('model.obj', 'model.gltf', options)
|
||||
.then(function() {
|
||||
console.log('Converted model');
|
||||
});
|
||||
```
|
||||
Using obj2gltf as a command-line tool:
|
||||
|
||||
|
@ -37,7 +37,7 @@ function convert(objFile, outputPath, options) {
|
||||
extension = binary ? '.glb' : '.gltf';
|
||||
var gltfFile = path.join(outputPath, modelName + extension);
|
||||
|
||||
parseObj(objFile, inputPath)
|
||||
return parseObj(objFile, inputPath)
|
||||
.then(function(data) {
|
||||
return createGltf(data, inputPath, modelName);
|
||||
})
|
||||
|
377
lib/obj.js
377
lib/obj.js
@ -2,7 +2,6 @@
|
||||
|
||||
var Cesium = require('cesium');
|
||||
var Promise = require('bluebird');
|
||||
var async = require('async');
|
||||
var byline = require('byline');
|
||||
var fs = require('fs-extra');
|
||||
var path = require('path');
|
||||
@ -28,220 +27,222 @@ function parseObj(objFile, inputPath) {
|
||||
});
|
||||
}
|
||||
|
||||
function processObj(objFile, info, materials, images, done) {
|
||||
// A vertex is specified by indexes into each of the attribute arrays,
|
||||
// but these indexes may be different. This maps the separate indexes to a single index.
|
||||
var vertexCache = {};
|
||||
var vertexCount = 0;
|
||||
function processObj(objFile, info, materials, images) {
|
||||
return new Promise(function(resolve) {
|
||||
// A vertex is specified by indexes into each of the attribute arrays,
|
||||
// but these indexes may be different. This maps the separate indexes to a single index.
|
||||
var vertexCache = {};
|
||||
var vertexCount = 0;
|
||||
|
||||
var vertexArray = [];
|
||||
|
||||
var positions = [];
|
||||
var normals = [];
|
||||
var uvs = [];
|
||||
var vertexArray = [];
|
||||
|
||||
var positionMin = [Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE];
|
||||
var positionMax = [-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE];
|
||||
var positions = [];
|
||||
var normals = [];
|
||||
var uvs = [];
|
||||
|
||||
var hasNormals = info.hasNormals;
|
||||
var hasUVs = info.hasUVs;
|
||||
var positionMin = [Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE];
|
||||
var positionMax = [-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE];
|
||||
|
||||
var materialGroups = {}; // Map material to index array
|
||||
var currentIndexArray;
|
||||
var hasNormals = info.hasNormals;
|
||||
var hasUVs = info.hasUVs;
|
||||
|
||||
// Switch to the material-specific index array, or create it if it doesn't exist
|
||||
function useMaterial(material) {
|
||||
if (!defined(materials[material])) {
|
||||
useDefaultMaterial();
|
||||
} else {
|
||||
currentIndexArray = materialGroups[material];
|
||||
if (!defined(currentIndexArray)) {
|
||||
currentIndexArray = [];
|
||||
materialGroups[material] = currentIndexArray;
|
||||
}
|
||||
}
|
||||
}
|
||||
var materialGroups = {}; // Map material to index array
|
||||
var currentIndexArray;
|
||||
|
||||
function useDefaultMaterial() {
|
||||
var defaultMaterial = 'czmDefaultMat';
|
||||
if (!defined(materials[defaultMaterial])) {
|
||||
materials[defaultMaterial] = Material.getDefault();
|
||||
}
|
||||
useMaterial(defaultMaterial);
|
||||
}
|
||||
|
||||
var materialsLength = Object.keys(materials).length;
|
||||
if (materialsLength === 0) {
|
||||
useDefaultMaterial();
|
||||
}
|
||||
|
||||
function getOffset(a, data, components) {
|
||||
var i = parseInt(a);
|
||||
if (i < 0) {
|
||||
// Negative vertex indexes reference the vertices immediately above it
|
||||
return (data.length / components + i) * components;
|
||||
}
|
||||
return (i - 1) * components;
|
||||
}
|
||||
|
||||
function createVertex(p, u, n) {
|
||||
// Positions
|
||||
var pi = getOffset(p, positions, 3);
|
||||
var px = positions[pi + 0];
|
||||
var py = positions[pi + 1];
|
||||
var pz = positions[pi + 2];
|
||||
|
||||
positionMin[0] = Math.min(px, positionMin[0]);
|
||||
positionMin[1] = Math.min(py, positionMin[1]);
|
||||
positionMin[2] = Math.min(pz, positionMin[2]);
|
||||
positionMax[0] = Math.max(px, positionMax[0]);
|
||||
positionMax[1] = Math.max(py, positionMax[1]);
|
||||
positionMax[2] = Math.max(pz, positionMax[2]);
|
||||
vertexArray.push(px, py, pz);
|
||||
|
||||
// Normals
|
||||
if (hasNormals) {
|
||||
var ni = getOffset(n, normals, 3);
|
||||
var nx = normals[ni + 0];
|
||||
var ny = normals[ni + 1];
|
||||
var nz = normals[ni + 2];
|
||||
vertexArray.push(nx, ny, nz);
|
||||
}
|
||||
|
||||
// UVs
|
||||
if (hasUVs) {
|
||||
if (defined(u)) {
|
||||
var ui = getOffset(u, uvs, 2);
|
||||
var ux = uvs[ui + 0];
|
||||
var uy = uvs[ui + 1];
|
||||
// Flip y so 0.0 is the bottom of the image
|
||||
uy = 1.0 - uy;
|
||||
vertexArray.push(ux, uy);
|
||||
// Switch to the material-specific index array, or create it if it doesn't exist
|
||||
function useMaterial(material) {
|
||||
if (!defined(materials[material])) {
|
||||
useDefaultMaterial();
|
||||
} else {
|
||||
// Some objects in the model may not have uvs, fill with 0's for consistency
|
||||
vertexArray.push(0.0, 0.0);
|
||||
currentIndexArray = materialGroups[material];
|
||||
if (!defined(currentIndexArray)) {
|
||||
currentIndexArray = [];
|
||||
materialGroups[material] = currentIndexArray;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function addVertex(v, p, u, n) {
|
||||
var index = vertexCache[v];
|
||||
if (!defined(index)) {
|
||||
index = vertexCount++;
|
||||
vertexCache[v] = index;
|
||||
createVertex(p, u, n);
|
||||
function useDefaultMaterial() {
|
||||
var defaultMaterial = 'czmDefaultMat';
|
||||
if (!defined(materials[defaultMaterial])) {
|
||||
materials[defaultMaterial] = Material.getDefault();
|
||||
}
|
||||
useMaterial(defaultMaterial);
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
var materialsLength = Object.keys(materials).length;
|
||||
if (materialsLength === 0) {
|
||||
useDefaultMaterial();
|
||||
}
|
||||
|
||||
function addFace(v1, p1, u1, n1, v2, p2, u2, n2, v3, p3, u3, n3, v4, p4, u4, n4) {
|
||||
var index1 = addVertex(v1, p1, u1, n1);
|
||||
var index2 = addVertex(v2, p2, u2, n2);
|
||||
var index3 = addVertex(v3, p3, u3, n3);
|
||||
function getOffset(a, data, components) {
|
||||
var i = parseInt(a);
|
||||
if (i < 0) {
|
||||
// Negative vertex indexes reference the vertices immediately above it
|
||||
return (data.length / components + i) * components;
|
||||
}
|
||||
return (i - 1) * components;
|
||||
}
|
||||
|
||||
currentIndexArray.push(index1);
|
||||
currentIndexArray.push(index2);
|
||||
currentIndexArray.push(index3);
|
||||
function createVertex(p, u, n) {
|
||||
// Positions
|
||||
var pi = getOffset(p, positions, 3);
|
||||
var px = positions[pi + 0];
|
||||
var py = positions[pi + 1];
|
||||
var pz = positions[pi + 2];
|
||||
|
||||
positionMin[0] = Math.min(px, positionMin[0]);
|
||||
positionMin[1] = Math.min(py, positionMin[1]);
|
||||
positionMin[2] = Math.min(pz, positionMin[2]);
|
||||
positionMax[0] = Math.max(px, positionMax[0]);
|
||||
positionMax[1] = Math.max(py, positionMax[1]);
|
||||
positionMax[2] = Math.max(pz, positionMax[2]);
|
||||
vertexArray.push(px, py, pz);
|
||||
|
||||
// Normals
|
||||
if (hasNormals) {
|
||||
var ni = getOffset(n, normals, 3);
|
||||
var nx = normals[ni + 0];
|
||||
var ny = normals[ni + 1];
|
||||
var nz = normals[ni + 2];
|
||||
vertexArray.push(nx, ny, nz);
|
||||
}
|
||||
|
||||
// UVs
|
||||
if (hasUVs) {
|
||||
if (defined(u)) {
|
||||
var ui = getOffset(u, uvs, 2);
|
||||
var ux = uvs[ui + 0];
|
||||
var uy = uvs[ui + 1];
|
||||
// Flip y so 0.0 is the bottom of the image
|
||||
uy = 1.0 - uy;
|
||||
vertexArray.push(ux, uy);
|
||||
} else {
|
||||
// Some objects in the model may not have uvs, fill with 0's for consistency
|
||||
vertexArray.push(0.0, 0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function addVertex(v, p, u, n) {
|
||||
var index = vertexCache[v];
|
||||
if (!defined(index)) {
|
||||
index = vertexCount++;
|
||||
vertexCache[v] = index;
|
||||
createVertex(p, u, n);
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
function addFace(v1, p1, u1, n1, v2, p2, u2, n2, v3, p3, u3, n3, v4, p4, u4, n4) {
|
||||
var index1 = addVertex(v1, p1, u1, n1);
|
||||
var index2 = addVertex(v2, p2, u2, n2);
|
||||
var index3 = addVertex(v3, p3, u3, n3);
|
||||
|
||||
// Triangulate if the face is a quad
|
||||
if (defined(v4)) {
|
||||
var index4 = addVertex(v4, p4, u4, n4);
|
||||
currentIndexArray.push(index1);
|
||||
currentIndexArray.push(index2);
|
||||
currentIndexArray.push(index3);
|
||||
currentIndexArray.push(index4);
|
||||
|
||||
// Triangulate if the face is a quad
|
||||
if (defined(v4)) {
|
||||
var index4 = addVertex(v4, p4, u4, n4);
|
||||
currentIndexArray.push(index1);
|
||||
currentIndexArray.push(index3);
|
||||
currentIndexArray.push(index4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// v float float float
|
||||
var vertexPattern = /v( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/;
|
||||
// v float float float
|
||||
var vertexPattern = /v( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/;
|
||||
|
||||
// vn float float float
|
||||
var normalPattern = /vn( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/;
|
||||
// vn float float float
|
||||
var normalPattern = /vn( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/;
|
||||
|
||||
// vt float float
|
||||
var uvPattern = /vt( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/;
|
||||
// vt float float
|
||||
var uvPattern = /vt( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/;
|
||||
|
||||
// f vertex vertex vertex ...
|
||||
var facePattern1 = /f( +-?\d+)\/?( +-?\d+)\/?( +-?\d+)\/?( +-?\d+)?\/?/;
|
||||
// f vertex vertex vertex ...
|
||||
var facePattern1 = /f( +-?\d+)\/?( +-?\d+)\/?( +-?\d+)\/?( +-?\d+)?\/?/;
|
||||
|
||||
// f vertex/uv vertex/uv vertex/uv ...
|
||||
var facePattern2 = /f( +(-?\d+)\/(-?\d+)\/?)( +(-?\d+)\/(-?\d+)\/?)( +(-?\d+)\/(-?\d+)\/?)( +(-?\d+)\/(-?\d+)\/?)?/;
|
||||
// f vertex/uv vertex/uv vertex/uv ...
|
||||
var facePattern2 = /f( +(-?\d+)\/(-?\d+)\/?)( +(-?\d+)\/(-?\d+)\/?)( +(-?\d+)\/(-?\d+)\/?)( +(-?\d+)\/(-?\d+)\/?)?/;
|
||||
|
||||
// f vertex/uv/normal vertex/uv/normal vertex/uv/normal ...
|
||||
var facePattern3 = /f( +(-?\d+)\/(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+)\/(-?\d+))?/;
|
||||
// f vertex/uv/normal vertex/uv/normal vertex/uv/normal ...
|
||||
var facePattern3 = /f( +(-?\d+)\/(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+)\/(-?\d+))?/;
|
||||
|
||||
// f vertex//normal vertex//normal vertex//normal ...
|
||||
var facePattern4 = /f( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))?/;
|
||||
// f vertex//normal vertex//normal vertex//normal ...
|
||||
var facePattern4 = /f( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))?/;
|
||||
|
||||
var stream = byline(fs.createReadStream(objFile, {encoding: 'utf8'}));
|
||||
stream.on('data', function(line) {
|
||||
line = line.trim();
|
||||
var result;
|
||||
if ((line.length === 0) || (line.charAt(0) === '#')) {
|
||||
// Don't process empty lines or comments
|
||||
} else if ((result = vertexPattern.exec(line)) !== null) {
|
||||
positions.push(
|
||||
parseFloat(result[1]),
|
||||
parseFloat(result[2]),
|
||||
parseFloat(result[3])
|
||||
);
|
||||
} else if ((result = normalPattern.exec(line) ) !== null) {
|
||||
var nx = parseFloat(result[1]);
|
||||
var ny = parseFloat(result[2]);
|
||||
var nz = parseFloat(result[3]);
|
||||
var normal = Cartesian3.normalize(new Cartesian3(nx, ny, nz), new Cartesian3());
|
||||
normals.push(normal.x, normal.y, normal.z);
|
||||
} else if ((result = uvPattern.exec(line)) !== null) {
|
||||
uvs.push(
|
||||
parseFloat(result[1]),
|
||||
parseFloat(result[2])
|
||||
);
|
||||
} else if ((result = facePattern1.exec(line)) !== null) {
|
||||
addFace(
|
||||
result[1], result[1], undefined, undefined,
|
||||
result[2], result[2], undefined, undefined,
|
||||
result[3], result[3], undefined, undefined,
|
||||
result[4], result[4], undefined, undefined
|
||||
);
|
||||
} else if ((result = facePattern2.exec(line)) !== null) {
|
||||
addFace(
|
||||
result[1], result[2], result[3], undefined,
|
||||
result[4], result[5], result[6], undefined,
|
||||
result[7], result[8], result[9], undefined,
|
||||
result[10], result[11], result[12], undefined
|
||||
);
|
||||
} else if ((result = facePattern3.exec(line)) !== null) {
|
||||
addFace(
|
||||
result[1], result[2], result[3], result[4],
|
||||
result[5], result[6], result[7], result[8],
|
||||
result[9], result[10], result[11], result[12],
|
||||
result[13], result[14], result[15], result[16]
|
||||
);
|
||||
} else if ((result = facePattern4.exec(line)) !== null) {
|
||||
addFace(
|
||||
result[1], result[2], undefined, result[3],
|
||||
result[4], result[5], undefined, result[6],
|
||||
result[7], result[8], undefined, result[9],
|
||||
result[10], result[11], undefined, result[12]
|
||||
);
|
||||
} else if (/^usemtl /.test(line)) {
|
||||
var materialName = line.substring(7).trim();
|
||||
useMaterial(materialName);
|
||||
}
|
||||
});
|
||||
var stream = byline(fs.createReadStream(objFile, {encoding: 'utf8'}));
|
||||
stream.on('data', function (line) {
|
||||
line = line.trim();
|
||||
var result;
|
||||
if ((line.length === 0) || (line.charAt(0) === '#')) {
|
||||
// Don't process empty lines or comments
|
||||
} else if ((result = vertexPattern.exec(line)) !== null) {
|
||||
positions.push(
|
||||
parseFloat(result[1]),
|
||||
parseFloat(result[2]),
|
||||
parseFloat(result[3])
|
||||
);
|
||||
} else if ((result = normalPattern.exec(line) ) !== null) {
|
||||
var nx = parseFloat(result[1]);
|
||||
var ny = parseFloat(result[2]);
|
||||
var nz = parseFloat(result[3]);
|
||||
var normal = Cartesian3.normalize(new Cartesian3(nx, ny, nz), new Cartesian3());
|
||||
normals.push(normal.x, normal.y, normal.z);
|
||||
} else if ((result = uvPattern.exec(line)) !== null) {
|
||||
uvs.push(
|
||||
parseFloat(result[1]),
|
||||
parseFloat(result[2])
|
||||
);
|
||||
} else if ((result = facePattern1.exec(line)) !== null) {
|
||||
addFace(
|
||||
result[1], result[1], undefined, undefined,
|
||||
result[2], result[2], undefined, undefined,
|
||||
result[3], result[3], undefined, undefined,
|
||||
result[4], result[4], undefined, undefined
|
||||
);
|
||||
} else if ((result = facePattern2.exec(line)) !== null) {
|
||||
addFace(
|
||||
result[1], result[2], result[3], undefined,
|
||||
result[4], result[5], result[6], undefined,
|
||||
result[7], result[8], result[9], undefined,
|
||||
result[10], result[11], result[12], undefined
|
||||
);
|
||||
} else if ((result = facePattern3.exec(line)) !== null) {
|
||||
addFace(
|
||||
result[1], result[2], result[3], result[4],
|
||||
result[5], result[6], result[7], result[8],
|
||||
result[9], result[10], result[11], result[12],
|
||||
result[13], result[14], result[15], result[16]
|
||||
);
|
||||
} else if ((result = facePattern4.exec(line)) !== null) {
|
||||
addFace(
|
||||
result[1], result[2], undefined, result[3],
|
||||
result[4], result[5], undefined, result[6],
|
||||
result[7], result[8], undefined, result[9],
|
||||
result[10], result[11], undefined, result[12]
|
||||
);
|
||||
} else if (/^usemtl /.test(line)) {
|
||||
var materialName = line.substring(7).trim();
|
||||
useMaterial(materialName);
|
||||
}
|
||||
});
|
||||
|
||||
stream.on('end', function() {
|
||||
done({
|
||||
vertexCount : vertexCount,
|
||||
vertexArray : vertexArray,
|
||||
positionMin : positionMin,
|
||||
positionMax : positionMax,
|
||||
hasUVs : hasUVs,
|
||||
hasNormals : hasNormals,
|
||||
materialGroups : materialGroups,
|
||||
materials : materials,
|
||||
images : images
|
||||
stream.on('end', function () {
|
||||
resolve({
|
||||
vertexCount: vertexCount,
|
||||
vertexArray: vertexArray,
|
||||
positionMin: positionMin,
|
||||
positionMax: positionMax,
|
||||
hasUVs: hasUVs,
|
||||
hasNormals: hasNormals,
|
||||
materialGroups: materialGroups,
|
||||
materials: materials,
|
||||
images: images
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ var gltfFile = './specs/data/BoxTextured/BoxTextured.gltf';
|
||||
|
||||
describe('convert', function() {
|
||||
it('converts an obj to gltf', function(done) {
|
||||
var spy = spyOn(gltfPipeline, 'processJSONToDisk').and.callFake(function() {
|
||||
var spy = spyOn(gltfPipeline, 'processJSONToDisk').and.callFake(function(gltf, outputPath, options, callback) {
|
||||
return;
|
||||
});
|
||||
expect(convert(objFile, gltfFile, {})
|
||||
|
Loading…
x
Reference in New Issue
Block a user