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 eaca567
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions lib/createGltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,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 +236,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 +261,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 +277,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 +308,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 +426,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 eaca567

Please sign in to comment.