2021SC@SDUSC
预处理函数prepare()。
void TextureGenerator::prepare()
{
if (nullptr == m_snapshot)
return;
std::map<QUuid, QUuid> updatedMaterialIdMap;
std::map<QUuid, bool> updatedCountershadedMap;
for (const auto &partIt: m_snapshot->parts) {
QUuid materialId;
auto materialIdIt = partIt.second.find("materialId");
if (materialIdIt != partIt.second.end())
materialId = QUuid(materialIdIt->second);
QUuid partId = QUuid(partIt.first);
updatedMaterialIdMap.insert({partId, materialId});
updatedCountershadedMap.insert({partId,
isTrueValueString(valueOfKeyInMapOrEmpty(partIt.second, "countershaded"))});
}
for (const auto &bmeshNode: m_object->nodes) {
bool countershaded = bmeshNode.countershaded;
auto findUpdatedCountershadedMap = updatedCountershadedMap.find(bmeshNode.mirrorFromPartId.isNull() ? bmeshNode.partId : bmeshNode.mirrorFromPartId);
if (findUpdatedCountershadedMap != updatedCountershadedMap.end())
countershaded = findUpdatedCountershadedMap->second;
if (countershaded)
m_countershadedPartIds.insert(bmeshNode.partId);
for (size_t i = 0; i < (int)TextureType::Count - 1; ++i) {
TextureType forWhat = (TextureType)(i + 1);
MaterialTextures materialTextures;
QUuid materialId = bmeshNode.materialId;
auto findUpdatedMaterialIdResult = updatedMaterialIdMap.find(bmeshNode.mirrorFromPartId.isNull() ? bmeshNode.partId : bmeshNode.mirrorFromPartId);
if (findUpdatedMaterialIdResult != updatedMaterialIdMap.end())
materialId = findUpdatedMaterialIdResult->second;
float tileScale = 1.0;
initializeMaterialTexturesFromSnapshot(*m_snapshot, materialId, materialTextures, tileScale);
const QImage *image = materialTextures.textureImages[i];
if (nullptr != image) {
if (TextureType::BaseColor == forWhat)
addPartColorMap(bmeshNode.partId, image, tileScale);
else if (TextureType::Normal == forWhat)
addPartNormalMap(bmeshNode.partId, image, tileScale);
else if (TextureType::Metallic == forWhat)
addPartMetalnessMap(bmeshNode.partId, image, tileScale);
else if (TextureType::Roughness == forWhat)
addPartRoughnessMap(bmeshNode.partId, image, tileScale);
else if (TextureType::AmbientOcclusion == forWhat)
addPartAmbientOcclusionMap(bmeshNode.partId, image, tileScale);
}
}
}
}
这一部分主要遍历快照中的parts成员,更新materialIdMap。遍历m_object下的bmesh节点,读取并添加颜色、法线、金属度、粗糙度和AO贴图。