cione-comp-lib
Version:
`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`
77 lines (76 loc) • 2.7 kB
JavaScript
/* @license
* Copyright 2021 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const ensureUniqueNames = (variantNames) => {
const uniqueNames = [];
const knownNames = /* @__PURE__ */ new Set();
for (const name of variantNames) {
let uniqueName = name;
let suffix = 0;
while (knownNames.has(uniqueName)) {
uniqueName = name + "." + ++suffix;
}
knownNames.add(uniqueName);
uniqueNames.push(uniqueName);
}
return uniqueNames;
};
const mappingsArrayToTable = (extensionDef) => {
const table = /* @__PURE__ */ new Map();
for (const mapping of extensionDef.mappings) {
for (const variant of mapping.variants) {
table.set(variant, { material: null, gltfMaterialIndex: mapping.material });
}
}
return table;
};
class GLTFMaterialsVariantsExtension {
constructor(parser) {
this.parser = parser;
this.name = "KHR_materials_variants";
}
afterRoot(gltf) {
const parser = this.parser;
const json = parser.json;
if (json.extensions === void 0 || json.extensions[this.name] === void 0) {
return null;
}
const extensionDef = json.extensions[this.name];
const variantsDef = extensionDef.variants || [];
const variants = ensureUniqueNames(variantsDef.map((v) => v.name));
for (const scene of gltf.scenes) {
scene.traverse((object) => {
const mesh = object;
if (!mesh.isMesh) {
return;
}
const association = parser.associations.get(mesh);
if (association == null || association.meshes == null || association.primitives == null) {
return;
}
const meshDef = json.meshes[association.meshes];
const primitivesDef = meshDef.primitives;
const primitiveDef = primitivesDef[association.primitives];
const extensionsDef = primitiveDef.extensions;
if (!extensionsDef || !extensionsDef[this.name]) {
return;
}
mesh.userData.variantMaterials = mappingsArrayToTable(extensionsDef[this.name]);
});
}
gltf.userData.variants = variants;
return Promise.resolve();
}
}
export { GLTFMaterialsVariantsExtension as default };