obj2gltf/lib/createGltf.js

398 lines
12 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
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
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) {
meshIndex = addMesh(gltf, materials, bufferState, uint32Indices, meshes[0], options);
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];
meshIndex = addMesh(gltf, materials, bufferState, uint32Indices, mesh, options);
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
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;
}
function getMaterial(gltf, materials, materialName, options) {
2017-04-18 11:56:08 -04:00
if (!defined(materialName)) {
// Create a default material if the primitive does not specify one
materialName = 'default';
}
2017-05-04 17:58:13 -04:00
var i;
var material;
var materialsLength = materials.length;
2017-05-04 17:58:13 -04:00
for (i = 0; i < materialsLength; ++i) {
if (materials[i].name === materialName) {
material = materials[i];
break;
2017-04-18 11:56:08 -04:00
}
}
if (!defined(material)) {
material = getDefaultMaterial(options);
material.name = materialName;
}
2017-05-04 17:58:13 -04:00
var materialIndex;
materialsLength = gltf.materials.length;
for (i = 0; i < materialsLength; ++i) {
if (gltf.materials[i].name === materialName) {
materialIndex = i;
break;
}
}
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;
}
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;
}
function addPrimitive(gltf, materials, bufferState, uint32Indices, mesh, primitive, index, options) {
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;
var materialIndex = getMaterial(gltf, materials, primitive.material, options);
return {
attributes : attributes,
indices : indexAccessorIndex,
material : materialIndex,
mode : WebGLConstants.TRIANGLES
};
}
2017-04-18 11:56:08 -04:00
function addMesh(gltf, materials, bufferState, uint32Indices, mesh, options) {
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) {
gltfPrimitives.push(addPrimitive(gltf, materials, bufferState, uint32Indices, mesh, primitives[i], i, options));
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
}