赞
踩
- UStaticMesh* FMeshUtils::GetStaticMeshFromObjPath(const FString ObjPath, const FString MeshId, const FString ComponentId)
- {
- if (ObjPath.IsEmpty() || !FPaths::FileExists(ObjPath))
- {
- UE_LOG( LogTemp, Error, TEXT("FMeshUtils::GetStaticMeshFromObjPath: %s is empty"), *ObjPath);
- return nullptr;
- }
-
- TPimplPtr<FObjData> ObjDataPtr = MakePimpl<FObjData>();
- ObjDataPtr->ObjFilename = ObjPath;
- static UE::MeshUtils::ObjParserUtils::FKeywordMap ObjKeywordMap =
- {
- { TEXT("o"), UE::MeshUtils::ObjParserUtils::ParseObjectName },
- { TEXT("v"), UE::MeshUtils::ObjParserUtils::ParseVertexPosition },
- { TEXT("vt"), UE::MeshUtils::ObjParserUtils::ParseTextureCoordinate },
- { TEXT("vn"), UE::MeshUtils::ObjParserUtils::ParseNormalVector },
- { TEXT("f"), UE::MeshUtils::ObjParserUtils::ParseFace },
- { TEXT("g"), UE::MeshUtils::ObjParserUtils::ParseGroup },
- { TEXT("mtllib"), UE::MeshUtils::ObjParserUtils::ParseMaterialLib },
- { TEXT("usemtl"), UE::MeshUtils::ObjParserUtils::ParseUseMaterial }
- };
-
- bool bSuccess = UE::MeshUtils::ObjParserUtils::ParseFile(*ObjDataPtr.Get(), *ObjPath, ObjKeywordMap);
-
- TArray<const FMeshDescription*> MeshDescriptionPointers;
- MeshDescriptionPointers.Reserve(ObjDataPtr->Groups.Num());
- TArray<FMeshDescription> LodMeshDescriptions;
- for (auto It = ObjDataPtr->Groups.CreateIterator(); It; ++It)
- {
- const FMeshDescription MeshDesc = ObjDataPtr->MakeMeshDescriptionForGroup(It->Key, FTransform::Identity);
- LodMeshDescriptions.Add(MeshDesc);
- }
- for (const FMeshDescription& MeshDescription : LodMeshDescriptions)
- {
- MeshDescriptionPointers.Add(&MeshDescription);
- }
-
- FString SMName = MeshId + "_" + ComponentId;
- UStaticMesh* StaticMesh = NewObject<UStaticMesh>(GetTransientPackage(), FName(SMName), RF_Transient);
-
- //-----------Deal Static Mesh Description----------------
- UStaticMesh::FBuildMeshDescriptionsParams Params1 = UStaticMesh::FBuildMeshDescriptionsParams();
- Params1.bFastBuild = true;
- Params1.bBuildSimpleCollision = true;
- const UStaticMesh::FBuildMeshDescriptionsParams Params = Params1;
- StaticMesh->BuildFromMeshDescriptions(MeshDescriptionPointers,Params);
-
- // -----------Deal Static Material Slot----------------
- TArray<FStaticMaterial> InStaticMaterials;
- for (int i = 0; i< StaticMesh->GetRenderData()->LODResources[0].Sections.Num(); i++)
- {
- FStaticMaterial CurrentMaterial = FStaticMaterial();
- CurrentMaterial.MaterialInterface = UMaterial::GetDefaultMaterial(MD_Surface);
- CurrentMaterial.MaterialSlotName = FName(TEXT("MaterialSlot")+FString::FromInt(i));
- CurrentMaterial.UVChannelData.bInitialized = true;
- InStaticMaterials.Add(CurrentMaterial);
- }
- StaticMesh->SetStaticMaterials(InStaticMaterials);
-
- const FSourceMeshDataForDerivedDataTask SourceMeshDataForDerivedDataTask;
- FQueuedThreadPool* ThreadPool = FQueuedThreadPool::Allocate();
- TArray<FSignedDistanceFieldBuildMaterialData> MaterialBlendModes;
- MaterialBlendModes.SetNum(StaticMesh->GetRenderData()->LODResources[0].Sections.Num());
- const FBoxSphereBounds Bounds = StaticMesh->GetRenderData()->Bounds;
-
- // //----------Deal SDF----------------------
- FDistanceFieldVolumeData* GenDistanceFieldVolumeData = new FDistanceFieldVolumeData();
- // MeshUtilities->GenerateSignedDistanceFieldVolumeData(StaticMesh->GetName(), SourceMeshDataForDerivedDataTask, StaticMesh->GetRenderData()->LODResources[0], *ThreadPool, MaterialBlendModes, Bounds, 1.0, true, *GenDistanceFieldVolumeData);
- // StaticMesh->GetRenderData()->LODResources[0].DistanceFieldData = GenDistanceFieldVolumeData;
-
- //----------Deal Mesh Cards---------------
- FCardRepresentationData* GeneratedCardRepresentation = new FCardRepresentationData();
- const FDistanceFieldVolumeData* DistanceFieldVolumeData = GenDistanceFieldVolumeData;
- int MaxLumenMeshCards = 12;
- MeshUtilities->GenerateCardRepresentationData(StaticMesh->GetName(), SourceMeshDataForDerivedDataTask, StaticMesh->GetRenderData()->LODResources[0], *ThreadPool, MaterialBlendModes, Bounds, DistanceFieldVolumeData, MaxLumenMeshCards,true,*GeneratedCardRepresentation);
- StaticMesh->GetRenderData()->LODResources[0].CardRepresentationData = GeneratedCardRepresentation;
-
- return StaticMesh;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。