unreal 使用材质 c++

   目前未找到直接手动创建完整材质的办法,感觉不太方便。 目前的办法是:在编辑器中创建需要的材质比如 顶点颜色材质、颜色材质、图片材质等。并将输入的数据添加到参数,这样可以在程序运行中修改顶点颜色,材质颜色,或者材质图片。

  顶点颜色材质:

    

   颜色材质创建:

  

 动态修改颜色:

  FString strMaterial = TEXT("/Game/StarterContent/Materials/M_Color.M_Color");

    ((AUeCustomActor*)m_pUeActor)->LoadSpecialMaterial(strMaterial, pColorMaterial, false);

    FLinearColor linearColor(color.m_fR, color.m_fG, color.m_fB, color.m_fA);
    
 
    UMaterialInstanceDynamic* pDynamicMaterial = UMaterialInstanceDynamic::Create(pColorMaterial, nullptr);// new UMaterialInstanceDynamic();
 
    pDynamicMaterial->SetVectorParameterValue(FName("BaseColor"), linearColor);

    pProcMesh->SetMaterial(nSection, pDynamicMaterial);

 图片材质 未测试。这种应该是可以冬天替换,比如用于播放视频。


  补充 LoadSpecialMaterial:

  
void AUeCustomActor::LoadSpecialMaterial(const FString& MaterialName, UMaterial*& Material, bool bCheckUsage)
{
    // only bother with materials that aren't already loaded
    if (Material == NULL)
    {
        // find or load the object
        Material = LoadObject<UMaterial>(NULL, *MaterialName, NULL, LOAD_None, NULL);

        if (!Material)
        {
#if !WITH_EDITORONLY_DATA
            UE_LOG(LogEngine, Log, TEXT("ERROR: Failed to load special material '%s'. This will probably have bad consequences (depending on its use)"), *MaterialName);
#else
            UE_LOG(LogEngine, Fatal, TEXT("Failed to load special material '%s'"), *MaterialName);
#endif
        }
        // if the material wasn't marked as being a special engine material, then not all of the shaders
        // will have been compiled on it by this point, so we need to compile them and alert the use
        // to set the bit
        else if (!Material->bUsedAsSpecialEngineMaterial && bCheckUsage)
        {
#if !WITH_EDITOR
            // consoles must have the flag set properly in the editor
            UE_LOG(LogEngine, Fatal, TEXT("The special material (%s) was not marked with bUsedAsSpecialEngineMaterial. Make sure this flag is set in the editor, save the package, and compile shaders for this platform"), *MaterialName);
#else
            Material->bUsedAsSpecialEngineMaterial = true;
            Material->MarkPackageDirty();

            // make sure all necessary shaders for the default are compiled, now that the flag is set
            Material->PostEditChange();

            FMessageDialog::Open(EAppMsgType::Ok, FText::Format(NSLOCTEXT("Engine", "SpecialMaterialConfiguredIncorrectly", "The special material ({0}) has not been marked with bUsedAsSpecialEngineMaterial.\nThis will prevent shader precompiling properly, so the flag has been set automatically.\nMake sure to save the package and distribute to everyone using this material."), FText::FromString(MaterialName)));
#endif
        }
    }
}

猜你喜欢

转载自blog.csdn.net/zsyddl2/article/details/80652956