Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Nov 1, 2018
1 parent a55ea69 commit a79a953
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions lib/createGltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ function createGltf(objData, options) {
var nodes = objData.nodes;
var materials = objData.materials;
var name = objData.name;
console.time('split');

// Split materials used by primitives with different types of attributes
materials = splitIncompatibleMaterials(nodes, materials, options);

console.timeEnd('split');

var gltf = {
accessors : [],
asset : {},
Expand Down Expand Up @@ -199,7 +202,7 @@ function getTexture(gltf, texture) {
}

function cloneMaterial(material, removeTextures) {
if (material === null || typeof material !== 'object') {
if (typeof material !== 'object') {
return material;
} else if (material instanceof Texture) {
if (removeTextures) {
Expand Down Expand Up @@ -236,7 +239,7 @@ function resolveTextures(gltf, material) {
}
}

function addMaterial(gltf, material) {
function addGltfMaterial(gltf, material) {
resolveTextures(gltf, material);
var materialIndex = gltf.materials.length;
gltf.materials.push(material);
Expand All @@ -261,12 +264,12 @@ function getMaterialIndex(materials, materialName) {
}
}

function getMaterial(gltf, materials, materialName) {
function getOrCreateGltfMaterial(gltf, materials, materialName) {
var material = getMaterialByName(materials, materialName);
var materialIndex = getMaterialIndex(gltf.materials, materialName);

if (!defined(materialIndex)) {
materialIndex = addMaterial(gltf, material);
materialIndex = addGltfMaterial(gltf, material);
}

return materialIndex;
Expand All @@ -277,6 +280,18 @@ function primitiveInfoMatch(a, b) {
a.hasNormals === b.hasNormals;
}

function getSplitMaterialName(originalMaterialName, primitiveInfo, primitiveInfoByMaterial) {
var splitMaterialName = originalMaterialName;
var suffix = 2;
while (defined(primitiveInfoByMaterial[splitMaterialName])) {
if (primitiveInfoMatch(primitiveInfo, primitiveInfoByMaterial[splitMaterialName])) {
break;
}
splitMaterialName = originalMaterialName + '-' + suffix++;
}
return splitMaterialName;
}

function splitIncompatibleMaterials(nodes, materials, options) {
var splitMaterials = [];
var primitiveInfoByMaterial = {};
Expand All @@ -296,31 +311,23 @@ function splitIncompatibleMaterials(nodes, materials, options) {
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 splitMaterialName = getSplitMaterialName(originalMaterialName, primitiveInfo, primitiveInfoByMaterial);
primitive.material = splitMaterialName;
primitiveInfoByMaterial[splitMaterialName] = primitiveInfo;

var material = getMaterialByName(splitMaterials, materialName);
if (defined(material)) {
var splitMaterial = getMaterialByName(splitMaterials, splitMaterialName);
if (defined(splitMaterial)) {
continue;
}

material = getMaterialByName(materials, originalMaterialName);
if (defined(material)) {
material = cloneMaterial(material, !hasUvs);
var originalMaterial = getMaterialByName(materials, originalMaterialName);
if (defined(originalMaterial)) {
splitMaterial = cloneMaterial(originalMaterial, !hasUvs);
} else {
material = getDefaultMaterial(options);
splitMaterial = getDefaultMaterial(options);
}
material.name = materialName;
splitMaterials.push(material);
splitMaterial.name = splitMaterialName;
splitMaterials.push(splitMaterial);
}
}
}
Expand Down Expand Up @@ -422,7 +429,7 @@ function addPrimitive(gltf, materials, bufferState, uint32Indices, mesh, primiti
primitive.uvs = undefined;
primitive.indices = undefined;

var materialIndex = getMaterial(gltf, materials, primitive.material);
var materialIndex = getOrCreateGltfMaterial(gltf, materials, primitive.material);

return {
attributes : attributes,
Expand Down

0 comments on commit a79a953

Please sign in to comment.