in packages/gltf-gen/src/material.ts [88:173]
public serialize(document: GLTF.GlTf, data: Buffer): GLTF.GlTfId {
if (this.cachedSerialId !== undefined) {
return this.cachedSerialId;
}
const mat: GLTF.Material = {
name: this.name,
pbrMetallicRoughness: {
baseColorFactor: this.baseColorFactor.asArray(),
metallicFactor: this.metallicFactor,
roughnessFactor: this.roughnessFactor
}
};
const pbr = mat.pbrMetallicRoughness;
if (this.name) {
mat.name = this.name;
}
if (this.baseColorTexture) {
pbr.baseColorTexture = {
index: this.baseColorTexture.serialize(document, data)
};
if (this.baseColorTexCoord !== 0) {
pbr.baseColorTexture.texCoord = this.baseColorTexCoord;
}
}
if (this.metallicRoughnessTexture) {
pbr.metallicRoughnessTexture = {
index: this.metallicRoughnessTexture.serialize(document, data)
};
if (this.metallicRoughnessTexCoord !== 0) {
pbr.metallicRoughnessTexture.texCoord = this.metallicRoughnessTexCoord;
}
}
if (this.normalTexture) {
mat.normalTexture = {
index: this.normalTexture.serialize(document, data)
};
if (this.normalTexCoord !== 0) {
mat.normalTexture.texCoord = this.normalTexCoord;
}
if (this.normalTexScale !== 1) {
mat.normalTexture.scale = this.normalTexScale;
}
}
if (this.occlusionTexture) {
mat.occlusionTexture = {
index: this.occlusionTexture.serialize(document, data)
};
if (this.occlusionTexCoord !== 0) {
mat.occlusionTexture.texCoord = this.occlusionTexCoord;
}
if (this.occlusionTexStrength !== 1) {
mat.occlusionTexture.strength = this.occlusionTexStrength;
}
}
if (!this.emissiveFactor.equalsFloats(0, 0, 0)) {
mat.emissiveFactor = this.emissiveFactor.asArray();
}
if (this.emissiveTexture) {
mat.emissiveTexture = {
index: this.emissiveTexture.serialize(document, data)
};
if (this.emissiveTexCoord !== 0) {
mat.emissiveTexture.texCoord = this.emissiveTexCoord;
}
}
if (this.alphaMode !== AlphaMode.Opaque) {
mat.alphaMode = this.alphaMode;
mat.alphaCutoff = this.alphaCutoff;
}
if (!document.materials) {
document.materials = [];
}
document.materials.push(mat);
return this.cachedSerialId = document.materials.length - 1;
}