This commit is contained in:
Sean Lilley 2018-10-31 20:47:52 -04:00
parent a55ea69661
commit eaca567a25

View File

@ -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) {
@ -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);
@ -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;
@ -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 = {};
@ -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++;
}
var splitMaterialName = getSplitMaterialName(originalMaterialName, primitiveInfo, primitiveInfoByMaterial);
primitive.material = splitMaterialName;
primitiveInfoByMaterial[splitMaterialName] = primitiveInfo;
primitive.material = materialName;
primitiveInfoByMaterial[materialName] = 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);
}
}
}
@ -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,