Dust3D开源项目分析——渲染与材质部分 | 纹理生成 Part2

本文深入分析了Dust3D开源项目的渲染与材质部分,特别是纹理生成的第二部分。通过预处理函数prepare(),文章详细介绍了如何遍历模型的各个部分,更新materialIdMap,并逐一读取和添加颜色、法线、金属度、粗糙度和环境光遮蔽(AO)贴图。
摘要由CSDN通过智能技术生成

 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贴图。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值