24 const TArray<FProcMeshTangent>& ParamTangents,
25 UMaterialInstance* MaterialInstance )
28 int32 VertexCount = Data.
Vertices.Num();
29 int32 VertexInstanceCount = Data.
Triangles.Num();
30 int32 PolygonCount = Data.
Vertices.Num()/3;
32 FMeshDescription MeshDescription;
33 FStaticMeshAttributes AttributeGetter(MeshDescription);
34 AttributeGetter.Register();
36 TPolygonGroupAttributesRef<FName> PolygonGroupNames = AttributeGetter.GetPolygonGroupMaterialSlotNames();
37 TVertexAttributesRef<FVector> VertexPositions = AttributeGetter.GetVertexPositions();
38 TVertexInstanceAttributesRef<FVector> Tangents = AttributeGetter.GetVertexInstanceTangents();
39 TVertexInstanceAttributesRef<float> BinormalSigns = AttributeGetter.GetVertexInstanceBinormalSigns();
40 TVertexInstanceAttributesRef<FVector> Normals = AttributeGetter.GetVertexInstanceNormals();
41 TVertexInstanceAttributesRef<FVector4> Colors = AttributeGetter.GetVertexInstanceColors();
42 TVertexInstanceAttributesRef<FVector2D> UVs = AttributeGetter.GetVertexInstanceUVs();
45 FPolygonGroupID PolygonGroupForSection;
46 MeshDescription.ReserveNewVertices(VertexCount);
47 MeshDescription.ReserveNewVertexInstances(VertexInstanceCount);
48 MeshDescription.ReserveNewPolygons(PolygonCount);
49 MeshDescription.ReserveNewEdges(PolygonCount * 2);
53 TMap<UMaterialInterface*, FPolygonGroupID> UniqueMaterials;
54 const int32 NumSections = 1;
55 UniqueMaterials.Reserve(1);
56 FPolygonGroupID NewPolygonGroup = MeshDescription.CreatePolygonGroup();
58 if( MaterialInstance !=
nullptr ){
59 UMaterialInterface *Material = MaterialInstance;
60 UniqueMaterials.Add(Material, NewPolygonGroup);
61 PolygonGroupNames[NewPolygonGroup] = Material->GetFName();
63 UE_LOG(LogCarla, Error, TEXT(
"MaterialInstance is nullptr"));
65 PolygonGroupForSection = NewPolygonGroup;
70 int32 NumVertex = Data.
Vertices.Num();
71 TMap<int32, FVertexID> VertexIndexToVertexID;
72 VertexIndexToVertexID.Reserve(NumVertex);
73 for (int32 VertexIndex = 0; VertexIndex < NumVertex; ++VertexIndex)
75 const FVector &Vert = Data.
Vertices[VertexIndex];
76 const FVertexID VertexID = MeshDescription.CreateVertex();
77 VertexPositions[VertexID] = Vert;
78 VertexIndexToVertexID.Add(VertexIndex, VertexID);
83 int32 NumTri = NumIndices / 3;
84 TMap<int32, FVertexInstanceID> IndiceIndexToVertexInstanceID;
85 IndiceIndexToVertexInstanceID.Reserve(NumVertex);
86 for (int32 IndiceIndex = 0; IndiceIndex < NumIndices; IndiceIndex++)
88 const int32 VertexIndex = Data.
Triangles[IndiceIndex];
89 const FVertexID VertexID = VertexIndexToVertexID[VertexIndex];
90 const FVertexInstanceID VertexInstanceID =
91 MeshDescription.CreateVertexInstance(VertexID);
92 IndiceIndexToVertexInstanceID.Add(IndiceIndex, VertexInstanceID);
93 Normals[VertexInstanceID] = Data.
Normals[VertexIndex];
95 if(ParamTangents.Num() == Data.
Vertices.Num())
97 Tangents[VertexInstanceID] = ParamTangents[VertexIndex].TangentX;
98 BinormalSigns[VertexInstanceID] =
99 ParamTangents[VertexIndex].bFlipTangentY ? -1.f : 1.f;
103 Colors[VertexInstanceID] = FLinearColor(0,0,0);
106 UVs.Set(VertexInstanceID, 0, Data.
UV0[VertexIndex]);
108 UVs.Set(VertexInstanceID, 0, FVector2D(0,0));
110 UVs.Set(VertexInstanceID, 1, FVector2D(0,0));
111 UVs.Set(VertexInstanceID, 2, FVector2D(0,0));
112 UVs.Set(VertexInstanceID, 3, FVector2D(0,0));
115 for (int32 TriIdx = 0; TriIdx < NumTri; TriIdx++)
117 FVertexID VertexIndexes[3];
118 TArray<FVertexInstanceID> VertexInstanceIDs;
119 VertexInstanceIDs.SetNum(3);
121 for (int32 CornerIndex = 0; CornerIndex < 3; ++CornerIndex)
123 const int32 IndiceIndex = (TriIdx * 3) + CornerIndex;
124 const int32 VertexIndex = Data.
Triangles[IndiceIndex];
125 VertexIndexes[CornerIndex] = VertexIndexToVertexID[VertexIndex];
126 VertexInstanceIDs[CornerIndex] =
127 IndiceIndexToVertexInstanceID[IndiceIndex];
131 MeshDescription.CreatePolygon(NewPolygonGroup, VertexInstanceIDs);
135 return MeshDescription;
140 const TArray<FProcMeshTangent>& ParamTangents,
141 UMaterialInstance* MaterialInstance,
146 IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
148 UStaticMesh::FBuildMeshDescriptionsParams Params;
149 Params.bBuildSimpleCollision =
true;
151 FString PackageName =
"/Game/CustomMaps/" + MapName +
"/Static/" + FolderName +
"/" + MeshName.ToString();
153 if (!PlatformFile.DirectoryExists(*PackageName))
161 if (Description.Polygons().Num() > 0)
163 UPackage* Package = CreatePackage(*PackageName);
165 UStaticMesh* Mesh = NewObject<UStaticMesh>( Package, MeshName, RF_Public | RF_Standalone);
167 Mesh->InitResources();
169 Mesh->LightingGuid = FGuid::NewGuid();
170 Mesh->StaticMaterials.Add(FStaticMaterial(MaterialInstance));
171 Mesh->BuildFromMeshDescriptions({ &Description }, Params);
172 Mesh->CreateBodySetup();
173 Mesh->BodySetup->CollisionTraceFlag = ECollisionTraceFlag::CTF_UseComplexAsSimple;
174 Mesh->BodySetup->CreatePhysicsMeshes();
176 Mesh->NeverStream =
false;
177 TArray<UObject*> CreatedAssets;
178 CreatedAssets.Add(Mesh);
181 FAssetRegistryModule::AssetCreated(Mesh);
183 Package->MarkPackageDirty();