当前位置:   article > 正文

UE5 Runtime 加载obj模型_ue外部加载obj文件,动态显示

ue外部加载obj文件,动态显示
  1. UStaticMesh* FMeshUtils::GetStaticMeshFromObjPath(const FString ObjPath, const FString MeshId, const FString ComponentId)
  2. {
  3. if (ObjPath.IsEmpty() || !FPaths::FileExists(ObjPath))
  4. {
  5. UE_LOG( LogTemp, Error, TEXT("FMeshUtils::GetStaticMeshFromObjPath: %s is empty"), *ObjPath);
  6. return nullptr;
  7. }
  8. TPimplPtr<FObjData> ObjDataPtr = MakePimpl<FObjData>();
  9. ObjDataPtr->ObjFilename = ObjPath;
  10. static UE::MeshUtils::ObjParserUtils::FKeywordMap ObjKeywordMap =
  11. {
  12. { TEXT("o"), UE::MeshUtils::ObjParserUtils::ParseObjectName },
  13. { TEXT("v"), UE::MeshUtils::ObjParserUtils::ParseVertexPosition },
  14. { TEXT("vt"), UE::MeshUtils::ObjParserUtils::ParseTextureCoordinate },
  15. { TEXT("vn"), UE::MeshUtils::ObjParserUtils::ParseNormalVector },
  16. { TEXT("f"), UE::MeshUtils::ObjParserUtils::ParseFace },
  17. { TEXT("g"), UE::MeshUtils::ObjParserUtils::ParseGroup },
  18. { TEXT("mtllib"), UE::MeshUtils::ObjParserUtils::ParseMaterialLib },
  19. { TEXT("usemtl"), UE::MeshUtils::ObjParserUtils::ParseUseMaterial }
  20. };
  21. bool bSuccess = UE::MeshUtils::ObjParserUtils::ParseFile(*ObjDataPtr.Get(), *ObjPath, ObjKeywordMap);
  22. TArray<const FMeshDescription*> MeshDescriptionPointers;
  23. MeshDescriptionPointers.Reserve(ObjDataPtr->Groups.Num());
  24. TArray<FMeshDescription> LodMeshDescriptions;
  25. for (auto It = ObjDataPtr->Groups.CreateIterator(); It; ++It)
  26. {
  27. const FMeshDescription MeshDesc = ObjDataPtr->MakeMeshDescriptionForGroup(It->Key, FTransform::Identity);
  28. LodMeshDescriptions.Add(MeshDesc);
  29. }
  30. for (const FMeshDescription& MeshDescription : LodMeshDescriptions)
  31. {
  32. MeshDescriptionPointers.Add(&MeshDescription);
  33. }
  34. FString SMName = MeshId + "_" + ComponentId;
  35. UStaticMesh* StaticMesh = NewObject<UStaticMesh>(GetTransientPackage(), FName(SMName), RF_Transient);
  36. //-----------Deal Static Mesh Description----------------
  37. UStaticMesh::FBuildMeshDescriptionsParams Params1 = UStaticMesh::FBuildMeshDescriptionsParams();
  38. Params1.bFastBuild = true;
  39. Params1.bBuildSimpleCollision = true;
  40. const UStaticMesh::FBuildMeshDescriptionsParams Params = Params1;
  41. StaticMesh->BuildFromMeshDescriptions(MeshDescriptionPointers,Params);
  42. // -----------Deal Static Material Slot----------------
  43. TArray<FStaticMaterial> InStaticMaterials;
  44. for (int i = 0; i< StaticMesh->GetRenderData()->LODResources[0].Sections.Num(); i++)
  45. {
  46. FStaticMaterial CurrentMaterial = FStaticMaterial();
  47. CurrentMaterial.MaterialInterface = UMaterial::GetDefaultMaterial(MD_Surface);
  48. CurrentMaterial.MaterialSlotName = FName(TEXT("MaterialSlot")+FString::FromInt(i));
  49. CurrentMaterial.UVChannelData.bInitialized = true;
  50. InStaticMaterials.Add(CurrentMaterial);
  51. }
  52. StaticMesh->SetStaticMaterials(InStaticMaterials);
  53. const FSourceMeshDataForDerivedDataTask SourceMeshDataForDerivedDataTask;
  54. FQueuedThreadPool* ThreadPool = FQueuedThreadPool::Allocate();
  55. TArray<FSignedDistanceFieldBuildMaterialData> MaterialBlendModes;
  56. MaterialBlendModes.SetNum(StaticMesh->GetRenderData()->LODResources[0].Sections.Num());
  57. const FBoxSphereBounds Bounds = StaticMesh->GetRenderData()->Bounds;
  58. // //----------Deal SDF----------------------
  59. FDistanceFieldVolumeData* GenDistanceFieldVolumeData = new FDistanceFieldVolumeData();
  60. // MeshUtilities->GenerateSignedDistanceFieldVolumeData(StaticMesh->GetName(), SourceMeshDataForDerivedDataTask, StaticMesh->GetRenderData()->LODResources[0], *ThreadPool, MaterialBlendModes, Bounds, 1.0, true, *GenDistanceFieldVolumeData);
  61. // StaticMesh->GetRenderData()->LODResources[0].DistanceFieldData = GenDistanceFieldVolumeData;
  62. //----------Deal Mesh Cards---------------
  63. FCardRepresentationData* GeneratedCardRepresentation = new FCardRepresentationData();
  64. const FDistanceFieldVolumeData* DistanceFieldVolumeData = GenDistanceFieldVolumeData;
  65. int MaxLumenMeshCards = 12;
  66. MeshUtilities->GenerateCardRepresentationData(StaticMesh->GetName(), SourceMeshDataForDerivedDataTask, StaticMesh->GetRenderData()->LODResources[0], *ThreadPool, MaterialBlendModes, Bounds, DistanceFieldVolumeData, MaxLumenMeshCards,true,*GeneratedCardRepresentation);
  67. StaticMesh->GetRenderData()->LODResources[0].CardRepresentationData = GeneratedCardRepresentation;
  68. return StaticMesh;
  69. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/646615
推荐阅读
  

闽ICP备14008679号