obj2gltf/lib/createGltf.js

474 lines
15 KiB
JavaScript
Raw Normal View History

2017-03-13 15:28:51 -04:00
'use strict';
2016-07-22 14:09:13 -04:00
var Cesium = require('cesium');
2017-07-19 17:56:24 -04:00
var getBufferPadded = require('./getBufferPadded');
var getDefaultMaterial = require('./loadMtl').getDefaultMaterial;
var Texture = require('./Texture');
2016-07-22 14:09:13 -04:00
2018-10-17 22:59:54 -04:00
var defaultValue = Cesium.defaultValue;
2016-06-09 13:33:08 -04:00
var defined = Cesium.defined;
var WebGLConstants = Cesium.WebGLConstants;
2015-10-16 17:32:23 -04:00
module.exports = createGltf;
2017-03-13 15:28:51 -04:00
/**
* Create a glTF from obj data.
*
* @param {Object} objData An object containing an array of nodes containing geometry information and an array of materials.
* @param {Object} options The options object passed along from lib/obj2gltf.js
2017-04-18 11:56:08 -04:00
* @returns {Object} A glTF asset.
2017-03-13 15:28:51 -04:00
*
* @private
*/
2017-04-18 11:56:08 -04:00
function createGltf(objData, options) {
2017-03-13 15:28:51 -04:00
var nodes = objData.nodes;
var materials = objData.materials;
var name = objData.name;
2015-10-16 17:32:23 -04:00
2018-10-17 22:59:54 -04:00
// Split materials used by primitives with different types of attributes
materials = splitIncompatibleMaterials(nodes, materials, options);
2016-06-09 13:33:08 -04:00
var gltf = {
2017-04-18 11:56:08 -04:00
accessors : [],
2016-06-09 13:33:08 -04:00
asset : {},
2017-04-18 11:56:08 -04:00
buffers : [],
bufferViews : [],
2017-05-04 15:39:01 -04:00
extensionsUsed : [],
extensionsRequired : [],
2017-04-18 11:56:08 -04:00
images : [],
materials : [],
meshes : [],
nodes : [],
samplers : [],
scene : 0,
scenes : [],
textures : []
2016-06-09 13:33:08 -04:00
};
gltf.asset = {
2017-03-13 15:28:51 -04:00
generator : 'obj2gltf',
2017-04-18 11:56:08 -04:00
version: '2.0'
2016-06-09 13:33:08 -04:00
};
2017-04-18 11:56:08 -04:00
gltf.scenes.push({
2017-03-13 15:28:51 -04:00
nodes : []
2017-04-18 11:56:08 -04:00
});
var bufferState = {
2017-07-19 17:56:24 -04:00
positionBuffers : [],
normalBuffers : [],
uvBuffers : [],
2017-04-18 11:56:08 -04:00
indexBuffers : [],
2017-07-19 17:56:24 -04:00
positionAccessors : [],
normalAccessors : [],
uvAccessors : [],
indexAccessors : []
2016-06-09 13:33:08 -04:00
};
2017-04-18 11:56:08 -04:00
var uint32Indices = requiresUint32Indices(nodes);
2016-06-09 13:33:08 -04:00
2017-04-18 11:56:08 -04:00
var nodesLength = nodes.length;
for (var i = 0; i < nodesLength; ++i) {
var node = nodes[i];
var meshes = node.meshes;
var meshesLength = meshes.length;
var meshIndex;
2017-03-13 15:28:51 -04:00
2017-04-18 11:56:08 -04:00
if (meshesLength === 1) {
2018-10-17 22:59:54 -04:00
meshIndex = addMesh(gltf, materials, bufferState, uint32Indices, meshes[0]);
addNode(gltf, node.name, meshIndex, undefined);
2017-04-18 11:56:08 -04:00
} else {
// Add meshes as child nodes
var parentIndex = addNode(gltf, node.name);
for (var j = 0; j < meshesLength; ++j) {
var mesh = meshes[j];
2018-10-17 22:59:54 -04:00
meshIndex = addMesh(gltf, materials, bufferState, uint32Indices, mesh);
2017-04-18 11:56:08 -04:00
addNode(gltf, mesh.name, meshIndex, parentIndex);
2017-03-13 15:28:51 -04:00
}
2017-04-18 11:56:08 -04:00
}
2016-06-09 13:33:08 -04:00
}
2015-10-16 17:32:23 -04:00
if (gltf.images.length > 0) {
2017-04-18 11:56:08 -04:00
gltf.samplers.push({
2017-03-13 15:28:51 -04:00
magFilter : WebGLConstants.LINEAR,
2017-07-17 14:38:20 -04:00
minFilter : WebGLConstants.NEAREST_MIPMAP_LINEAR,
2017-03-13 15:28:51 -04:00
wrapS : WebGLConstants.REPEAT,
wrapT : WebGLConstants.REPEAT
2017-04-18 11:56:08 -04:00
});
2017-03-13 15:28:51 -04:00
}
addBuffers(gltf, bufferState, name);
if (options.specularGlossiness) {
gltf.extensionsUsed.push('KHR_materials_pbrSpecularGlossiness');
gltf.extensionsRequired.push('KHR_materials_pbrSpecularGlossiness');
} else if (options.materialsCommon) {
gltf.extensionsUsed.push('KHR_materials_common');
gltf.extensionsRequired.push('KHR_materials_common');
}
2017-04-18 11:56:08 -04:00
return gltf;
}
2017-07-19 17:56:24 -04:00
function addBufferView(gltf, buffers, accessors, byteStride, target) {
var length = buffers.length;
if (length === 0) {
return;
}
var bufferViewIndex = gltf.bufferViews.length;
var previousBufferView = gltf.bufferViews[bufferViewIndex - 1];
var byteOffset = defined(previousBufferView) ? previousBufferView.byteOffset + previousBufferView.byteLength : 0;
var byteLength = 0;
for (var i = 0; i < length; ++i) {
var accessor = gltf.accessors[accessors[i]];
accessor.bufferView = bufferViewIndex;
accessor.byteOffset = byteLength;
byteLength += buffers[i].length;
}
gltf.bufferViews.push({
name : 'bufferView_' + bufferViewIndex,
buffer : 0,
byteLength : byteLength,
byteOffset : byteOffset,
byteStride : byteStride,
target : target
});
}
2017-04-18 11:56:08 -04:00
function addBuffers(gltf, bufferState, name) {
addBufferView(gltf, bufferState.positionBuffers, bufferState.positionAccessors, 12, WebGLConstants.ARRAY_BUFFER);
addBufferView(gltf, bufferState.normalBuffers, bufferState.normalAccessors, 12, WebGLConstants.ARRAY_BUFFER);
2017-07-19 17:56:24 -04:00
addBufferView(gltf, bufferState.uvBuffers, bufferState.uvAccessors, 8, WebGLConstants.ARRAY_BUFFER);
addBufferView(gltf, bufferState.indexBuffers, bufferState.indexAccessors, undefined, WebGLConstants.ELEMENT_ARRAY_BUFFER);
2017-04-18 11:56:08 -04:00
var buffers = [];
2017-07-19 17:56:24 -04:00
buffers = buffers.concat(bufferState.positionBuffers, bufferState.normalBuffers, bufferState.uvBuffers, bufferState.indexBuffers);
var buffer = getBufferPadded(Buffer.concat(buffers));
2017-04-18 11:56:08 -04:00
gltf.buffers.push({
name : name,
2017-07-19 17:56:24 -04:00
byteLength : buffer.length,
2017-04-18 11:56:08 -04:00
extras : {
_obj2gltf : {
source : buffer
2017-03-13 15:28:51 -04:00
}
2015-10-16 17:32:23 -04:00
}
2017-04-18 11:56:08 -04:00
});
}
function addTexture(gltf, texture) {
var imageName = texture.name;
var textureName = texture.name;
2017-04-18 11:56:08 -04:00
var imageIndex = gltf.images.length;
var textureIndex = gltf.textures.length;
2016-06-09 13:33:08 -04:00
2017-04-18 11:56:08 -04:00
gltf.images.push({
name : imageName,
extras : {
_obj2gltf : texture
2016-06-09 13:33:08 -04:00
}
2017-04-18 11:56:08 -04:00
});
gltf.textures.push({
name : textureName,
sampler : 0,
source : imageIndex
});
return textureIndex;
2017-04-18 11:56:08 -04:00
}
function getTexture(gltf, texture) {
var textureIndex;
var name = texture.name;
2017-04-18 11:56:08 -04:00
var textures = gltf.textures;
var length = textures.length;
for (var i = 0; i < length; ++i) {
if (textures[i].name === name) {
textureIndex = i;
break;
2017-04-18 11:56:08 -04:00
}
2016-06-09 13:33:08 -04:00
}
2017-04-18 11:56:08 -04:00
if (!defined(textureIndex)) {
textureIndex = addTexture(gltf, texture);
2017-04-18 11:56:08 -04:00
}
return {
index : textureIndex
};
2017-04-18 11:56:08 -04:00
}
2017-03-13 15:28:51 -04:00
2018-10-17 22:59:54 -04:00
function cloneMaterial(material, removeTextures) {
if (material === null || typeof material !== 'object') {
return material;
} else if (material instanceof Texture) {
if (removeTextures) {
return undefined;
}
return material;
} else if (Array.isArray(material)) {
var length = material.length;
var clonedArray = new Array(length);
for (var i = 0; i < length; ++i) {
clonedArray[i] = cloneMaterial(material[i], removeTextures);
}
return clonedArray;
}
var clonedObject = {};
for (var name in material) {
if (material.hasOwnProperty(name)) {
clonedObject[name] = cloneMaterial(material[name], removeTextures);
}
}
return clonedObject;
}
function resolveTextures(gltf, material) {
for (var name in material) {
if (material.hasOwnProperty(name)) {
var property = material[name];
if (property instanceof Texture) {
material[name] = getTexture(gltf, property);
} else if (!Array.isArray(property) && (typeof property === 'object')) {
resolveTextures(gltf, property);
}
2017-05-04 15:39:01 -04:00
}
}
}
function addMaterial(gltf, material) {
resolveTextures(gltf, material);
2017-04-18 11:56:08 -04:00
var materialIndex = gltf.materials.length;
gltf.materials.push(material);
2017-04-18 11:56:08 -04:00
return materialIndex;
}
2018-10-17 22:59:54 -04:00
function getMaterialByName(materials, materialName) {
var materialsLength = materials.length;
2018-10-17 22:59:54 -04:00
for (var i = 0; i < materialsLength; ++i) {
if (materials[i].name === materialName) {
2018-10-17 22:59:54 -04:00
return materials[i];
2017-04-18 11:56:08 -04:00
}
}
2018-10-17 22:59:54 -04:00
}
2017-04-18 11:56:08 -04:00
2018-10-17 22:59:54 -04:00
function getMaterialIndex(materials, materialName) {
var materialsLength = materials.length;
for (var i = 0; i < materialsLength; ++i) {
if (materials[i].name === materialName) {
return i;
2017-05-04 17:58:13 -04:00
}
}
2018-10-17 22:59:54 -04:00
}
function getMaterial(gltf, materials, materialName) {
var material = getMaterialByName(materials, materialName);
var materialIndex = getMaterialIndex(gltf.materials, materialName);
2017-04-18 11:56:08 -04:00
if (!defined(materialIndex)) {
materialIndex = addMaterial(gltf, material);
2017-04-18 11:56:08 -04:00
}
return materialIndex;
}
2018-10-17 22:59:54 -04:00
function primitiveInfoMatch(a, b) {
return a.hasUvs === b.hasUvs &&
a.hasNormals === b.hasNormals;
}
function splitIncompatibleMaterials(nodes, materials, options) {
var splitMaterials = [];
var primitiveInfoByMaterial = {};
var nodesLength = nodes.length;
for (var i = 0; i < nodesLength; ++i) {
var meshes = nodes[i].meshes;
var meshesLength = meshes.length;
for (var j = 0; j < meshesLength; ++j) {
var primitives = meshes[j].primitives;
var primitivesLength = primitives.length;
for (var k = 0; k < primitivesLength; ++k) {
var primitive = primitives[k];
var hasUvs = primitive.uvs.length > 0;
var hasNormals = primitive.normals.length > 0;
var primitiveInfo = {
hasUvs : hasUvs,
hasNormals : hasNormals
};
var originalMaterialName = defaultValue(primitive.material, 'default');
var materialName = originalMaterialName;
var suffix = 2;
while (defined(primitiveInfoByMaterial[materialName])) {
if (primitiveInfoMatch(primitiveInfo, primitiveInfoByMaterial[materialName])) {
break;
}
materialName = originalMaterialName + '-' + suffix++;
}
primitive.material = materialName;
primitiveInfoByMaterial[materialName] = primitiveInfo;
var material = getMaterialByName(splitMaterials, materialName);
if (defined(material)) {
continue;
}
material = getMaterialByName(materials, originalMaterialName);
if (defined(material)) {
material = cloneMaterial(material, !hasUvs);
} else {
material = getDefaultMaterial(options);
}
material.name = materialName;
splitMaterials.push(material);
}
}
}
return splitMaterials;
}
2017-07-19 17:56:24 -04:00
function addVertexAttribute(gltf, array, components, name) {
2017-04-18 11:56:08 -04:00
var count = array.length / components;
var minMax = array.getMinMax(components);
var type = (components === 3 ? 'VEC3' : 'VEC2');
var accessor = {
2017-05-04 17:58:13 -04:00
name : name,
2017-04-18 11:56:08 -04:00
componentType : WebGLConstants.FLOAT,
count : count,
min : minMax.min,
max : minMax.max,
type : type
2017-03-13 15:28:51 -04:00
};
2017-04-18 11:56:08 -04:00
var accessorIndex = gltf.accessors.length;
gltf.accessors.push(accessor);
return accessorIndex;
}
2017-07-19 17:56:24 -04:00
function addIndexArray(gltf, array, uint32Indices, name) {
2017-04-18 11:56:08 -04:00
var componentType = uint32Indices ? WebGLConstants.UNSIGNED_INT : WebGLConstants.UNSIGNED_SHORT;
var count = array.length;
var minMax = array.getMinMax(1);
var accessor = {
2017-05-04 17:58:13 -04:00
name : name,
2017-04-18 11:56:08 -04:00
componentType : componentType,
count : count,
min : minMax.min,
max : minMax.max,
type : 'SCALAR'
2017-03-13 15:28:51 -04:00
};
2017-04-18 11:56:08 -04:00
var accessorIndex = gltf.accessors.length;
gltf.accessors.push(accessor);
return accessorIndex;
}
function requiresUint32Indices(nodes) {
var nodesLength = nodes.length;
for (var i = 0; i < nodesLength; ++i) {
var meshes = nodes[i].meshes;
var meshesLength = meshes.length;
for (var j = 0; j < meshesLength; ++j) {
var primitives = meshes[j].primitives;
var primitivesLength = primitives.length;
for (var k = 0; k < primitivesLength; ++k) {
// Reserve the 65535 index for primitive restart
var vertexCount = primitives[k].positions.length / 3;
if (vertexCount > 65534) {
return true;
}
2017-04-18 11:56:08 -04:00
}
}
}
return false;
}
2018-10-17 22:59:54 -04:00
function addPrimitive(gltf, materials, bufferState, uint32Indices, mesh, primitive, index) {
var hasPositions = primitive.positions.length > 0;
var hasNormals = primitive.normals.length > 0;
var hasUVs = primitive.uvs.length > 0;
2017-04-18 11:56:08 -04:00
2017-07-19 17:56:24 -04:00
var accessorIndex;
2017-04-18 11:56:08 -04:00
var attributes = {};
if (hasPositions) {
accessorIndex = addVertexAttribute(gltf, primitive.positions, 3, mesh.name + '_' + index + '_positions');
2017-07-19 17:56:24 -04:00
attributes.POSITION = accessorIndex;
bufferState.positionBuffers.push(primitive.positions.toFloatBuffer());
2017-07-19 17:56:24 -04:00
bufferState.positionAccessors.push(accessorIndex);
2017-04-18 11:56:08 -04:00
}
if (hasNormals) {
accessorIndex = addVertexAttribute(gltf, primitive.normals, 3, mesh.name + '_' + index + '_normals');
2017-07-19 17:56:24 -04:00
attributes.NORMAL = accessorIndex;
bufferState.normalBuffers.push(primitive.normals.toFloatBuffer());
2017-07-19 17:56:24 -04:00
bufferState.normalAccessors.push(accessorIndex);
2017-04-18 11:56:08 -04:00
}
if (hasUVs) {
accessorIndex = addVertexAttribute(gltf, primitive.uvs, 2, mesh.name + '_' + index + '_texcoords');
2017-07-19 17:56:24 -04:00
attributes.TEXCOORD_0 = accessorIndex;
bufferState.uvBuffers.push(primitive.uvs.toFloatBuffer());
2017-07-19 17:56:24 -04:00
bufferState.uvAccessors.push(accessorIndex);
2017-04-18 11:56:08 -04:00
}
var indexAccessorIndex = addIndexArray(gltf, primitive.indices, uint32Indices, mesh.name + '_' + index + '_indices');
var indexBuffer = uint32Indices ? primitive.indices.toUint32Buffer() : primitive.indices.toUint16Buffer();
bufferState.indexBuffers.push(indexBuffer);
bufferState.indexAccessors.push(indexAccessorIndex);
2017-04-18 11:56:08 -04:00
// Unload resources
primitive.positions = undefined;
primitive.normals = undefined;
primitive.uvs = undefined;
primitive.indices = undefined;
2018-10-17 22:59:54 -04:00
var materialIndex = getMaterial(gltf, materials, primitive.material);
return {
attributes : attributes,
indices : indexAccessorIndex,
material : materialIndex,
mode : WebGLConstants.TRIANGLES
};
}
2017-04-18 11:56:08 -04:00
2018-10-17 22:59:54 -04:00
function addMesh(gltf, materials, bufferState, uint32Indices, mesh) {
2017-04-18 11:56:08 -04:00
var gltfPrimitives = [];
var primitives = mesh.primitives;
var primitivesLength = primitives.length;
for (var i = 0; i < primitivesLength; ++i) {
2018-10-17 22:59:54 -04:00
gltfPrimitives.push(addPrimitive(gltf, materials, bufferState, uint32Indices, mesh, primitives[i], i));
2017-04-18 11:56:08 -04:00
}
var gltfMesh = {
name : mesh.name,
primitives : gltfPrimitives
};
var meshIndex = gltf.meshes.length;
gltf.meshes.push(gltfMesh);
return meshIndex;
}
function addNode(gltf, name, meshIndex, parentIndex) {
var node = {
name : name,
mesh : meshIndex
};
var nodeIndex = gltf.nodes.length;
gltf.nodes.push(node);
if (defined(parentIndex)) {
var parentNode = gltf.nodes[parentIndex];
if (!defined(parentNode.children)) {
parentNode.children = [];
}
parentNode.children.push(nodeIndex);
} else {
gltf.scenes[gltf.scene].nodes.push(nodeIndex);
}
return nodeIndex;
2015-10-16 17:32:23 -04:00
}