RuntimeMeshLibrary.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. // Copyright 2016 Chris Conway (Koderz). All Rights Reserved.
  2. #include "RuntimeMeshComponentPluginPrivatePCH.h"
  3. #include "RuntimeMeshLibrary.h"
  4. #include "MessageLog.h"
  5. #include "UObjectToken.h"
  6. #include "StaticMeshResources.h"
  7. #include "TessellationUtilities.h"
  8. #include "RuntimeMeshBuilder.h"
  9. #include "RuntimeMeshComponent.h"
  10. #define LOCTEXT_NAMESPACE "RuntimeMeshLibrary"
  11. URuntimeMeshLibrary::URuntimeMeshLibrary(const FObjectInitializer& ObjectInitializer)
  12. : Super(ObjectInitializer)
  13. {
  14. }
  15. void URuntimeMeshLibrary::ConvertQuadToTriangles(TArray<int32>& Triangles, int32 Vert0, int32 Vert1, int32 Vert2, int32 Vert3)
  16. {
  17. Triangles.Add(Vert0);
  18. Triangles.Add(Vert1);
  19. Triangles.Add(Vert3);
  20. Triangles.Add(Vert1);
  21. Triangles.Add(Vert2);
  22. Triangles.Add(Vert3);
  23. }
  24. void URuntimeMeshLibrary::CreateGridMeshTriangles(int32 NumX, int32 NumY, bool bWinding, TArray<int32>& Triangles)
  25. {
  26. Triangles.Reset();
  27. if (NumX >= 2 && NumY >= 2)
  28. {
  29. // Build Quads
  30. for (int XIdx = 0; XIdx < NumX - 1; XIdx++)
  31. {
  32. for (int YIdx = 0; YIdx < NumY - 1; YIdx++)
  33. {
  34. const int32 I0 = (XIdx + 0)*NumY + (YIdx + 0);
  35. const int32 I1 = (XIdx + 1)*NumY + (YIdx + 0);
  36. const int32 I2 = (XIdx + 1)*NumY + (YIdx + 1);
  37. const int32 I3 = (XIdx + 0)*NumY + (YIdx + 1);
  38. if (bWinding)
  39. {
  40. ConvertQuadToTriangles(Triangles, I0, I1, I2, I3);
  41. }
  42. else
  43. {
  44. ConvertQuadToTriangles(Triangles, I0, I3, I2, I1);
  45. }
  46. }
  47. }
  48. }
  49. }
  50. void URuntimeMeshLibrary::CreateBoxMesh(FVector BoxRadius, TArray<FVector>& Vertices, TArray<int32>& Triangles, TArray<FVector>& Normals, TArray<FVector2D>& UVs, TArray<FRuntimeMeshTangent>& Tangents)
  51. {
  52. // Generate verts
  53. FVector BoxVerts[8];
  54. BoxVerts[0] = FVector(-BoxRadius.X, BoxRadius.Y, BoxRadius.Z);
  55. BoxVerts[1] = FVector(BoxRadius.X, BoxRadius.Y, BoxRadius.Z);
  56. BoxVerts[2] = FVector(BoxRadius.X, -BoxRadius.Y, BoxRadius.Z);
  57. BoxVerts[3] = FVector(-BoxRadius.X, -BoxRadius.Y, BoxRadius.Z);
  58. BoxVerts[4] = FVector(-BoxRadius.X, BoxRadius.Y, -BoxRadius.Z);
  59. BoxVerts[5] = FVector(BoxRadius.X, BoxRadius.Y, -BoxRadius.Z);
  60. BoxVerts[6] = FVector(BoxRadius.X, -BoxRadius.Y, -BoxRadius.Z);
  61. BoxVerts[7] = FVector(-BoxRadius.X, -BoxRadius.Y, -BoxRadius.Z);
  62. // Generate triangles (from quads)
  63. Triangles.Reset();
  64. const int32 NumVerts = 24; // 6 faces x 4 verts per face
  65. Vertices.Reset();
  66. Vertices.AddUninitialized(NumVerts);
  67. Normals.Reset();
  68. Normals.AddUninitialized(NumVerts);
  69. Tangents.Reset();
  70. Tangents.AddUninitialized(NumVerts);
  71. Vertices[0] = BoxVerts[0];
  72. Vertices[1] = BoxVerts[1];
  73. Vertices[2] = BoxVerts[2];
  74. Vertices[3] = BoxVerts[3];
  75. ConvertQuadToTriangles(Triangles, 0, 1, 2, 3);
  76. Normals[0] = Normals[1] = Normals[2] = Normals[3] = FVector(0, 0, 1);
  77. Tangents[0] = Tangents[1] = Tangents[2] = Tangents[3] = FRuntimeMeshTangent(0.f, -1.f, 0.f);
  78. Vertices[4] = BoxVerts[4];
  79. Vertices[5] = BoxVerts[0];
  80. Vertices[6] = BoxVerts[3];
  81. Vertices[7] = BoxVerts[7];
  82. ConvertQuadToTriangles(Triangles, 4, 5, 6, 7);
  83. Normals[4] = Normals[5] = Normals[6] = Normals[7] = FVector(-1, 0, 0);
  84. Tangents[4] = Tangents[5] = Tangents[6] = Tangents[7] = FRuntimeMeshTangent(0.f, -1.f, 0.f);
  85. Vertices[8] = BoxVerts[5];
  86. Vertices[9] = BoxVerts[1];
  87. Vertices[10] = BoxVerts[0];
  88. Vertices[11] = BoxVerts[4];
  89. ConvertQuadToTriangles(Triangles, 8, 9, 10, 11);
  90. Normals[8] = Normals[9] = Normals[10] = Normals[11] = FVector(0, 1, 0);
  91. Tangents[8] = Tangents[9] = Tangents[10] = Tangents[11] = FRuntimeMeshTangent(-1.f, 0.f, 0.f);
  92. Vertices[12] = BoxVerts[6];
  93. Vertices[13] = BoxVerts[2];
  94. Vertices[14] = BoxVerts[1];
  95. Vertices[15] = BoxVerts[5];
  96. ConvertQuadToTriangles(Triangles, 12, 13, 14, 15);
  97. Normals[12] = Normals[13] = Normals[14] = Normals[15] = FVector(1, 0, 0);
  98. Tangents[12] = Tangents[13] = Tangents[14] = Tangents[15] = FRuntimeMeshTangent(0.f, 1.f, 0.f);
  99. Vertices[16] = BoxVerts[7];
  100. Vertices[17] = BoxVerts[3];
  101. Vertices[18] = BoxVerts[2];
  102. Vertices[19] = BoxVerts[6];
  103. ConvertQuadToTriangles(Triangles, 16, 17, 18, 19);
  104. Normals[16] = Normals[17] = Normals[18] = Normals[19] = FVector(0, -1, 0);
  105. Tangents[16] = Tangents[17] = Tangents[18] = Tangents[19] = FRuntimeMeshTangent(1.f, 0.f, 0.f);
  106. Vertices[20] = BoxVerts[7];
  107. Vertices[21] = BoxVerts[6];
  108. Vertices[22] = BoxVerts[5];
  109. Vertices[23] = BoxVerts[4];
  110. ConvertQuadToTriangles(Triangles, 20, 21, 22, 23);
  111. Normals[20] = Normals[21] = Normals[22] = Normals[23] = FVector(0, 0, -1);
  112. Tangents[20] = Tangents[21] = Tangents[22] = Tangents[23] = FRuntimeMeshTangent(0.f, 1.f, 0.f);
  113. // UVs
  114. UVs.Reset();
  115. UVs.AddUninitialized(NumVerts);
  116. UVs[0] = UVs[4] = UVs[8] = UVs[12] = UVs[16] = UVs[20] = FVector2D(0.f, 0.f);
  117. UVs[1] = UVs[5] = UVs[9] = UVs[13] = UVs[17] = UVs[21] = FVector2D(0.f, 1.f);
  118. UVs[2] = UVs[6] = UVs[10] = UVs[14] = UVs[18] = UVs[22] = FVector2D(1.f, 1.f);
  119. UVs[3] = UVs[7] = UVs[11] = UVs[15] = UVs[19] = UVs[23] = FVector2D(1.f, 0.f);
  120. }
  121. void FindVertOverlaps(int32 TestVertIndex, const IRuntimeMeshVerticesBuilder* Vertices, TArray<int32>& VertOverlaps)
  122. {
  123. // Check if Verts is empty or test is outside range
  124. if (TestVertIndex < Vertices->Length())
  125. {
  126. const FVector TestVert = Vertices->GetPosition(TestVertIndex);
  127. for (int32 VertIdx = 0; VertIdx < Vertices->Length(); VertIdx++)
  128. {
  129. // First see if we overlap, and smoothing groups are the same
  130. if (TestVert.Equals(Vertices->GetPosition(VertIdx)))
  131. {
  132. // If it, so we are at least considered an 'overlap' for normal gen
  133. VertOverlaps.Add(VertIdx);
  134. }
  135. }
  136. }
  137. }
  138. void URuntimeMeshLibrary::CalculateTangentsForMesh(IRuntimeMeshVerticesBuilder* Vertices, const FRuntimeMeshIndicesBuilder* Triangles)
  139. {
  140. if (Vertices->Length() == 0) return;
  141. // Number of triangles
  142. const int32 NumTris = Triangles->TriangleLength();
  143. // Number of verts
  144. const int32 NumVerts = Vertices->Length();
  145. // Map of vertex to triangles in Triangles array
  146. TMultiMap<int32, int32> VertToTriMap;
  147. // Map of vertex to triangles to consider for normal calculation
  148. TMultiMap<int32, int32> VertToTriSmoothMap;
  149. // Normal/tangents for each face
  150. TArray<FVector> FaceTangentX, FaceTangentY, FaceTangentZ;
  151. FaceTangentX.AddUninitialized(NumTris);
  152. FaceTangentY.AddUninitialized(NumTris);
  153. FaceTangentZ.AddUninitialized(NumTris);
  154. // Iterate over triangles
  155. for (int TriIdx = 0; TriIdx < NumTris; TriIdx++)
  156. {
  157. int32 CornerIndex[3];
  158. FVector P[3];
  159. for (int32 CornerIdx = 0; CornerIdx < 3; CornerIdx++)
  160. {
  161. // Find vert index (clamped within range)
  162. int32 VertIndex = FMath::Min(Triangles->GetIndex((TriIdx * 3) + CornerIdx), NumVerts - 1);
  163. CornerIndex[CornerIdx] = VertIndex;
  164. P[CornerIdx] = Vertices->GetPosition(VertIndex);
  165. // Find/add this vert to index buffer
  166. TArray<int32> VertOverlaps;
  167. FindVertOverlaps(VertIndex, Vertices, VertOverlaps);
  168. // Remember which triangles map to this vert
  169. VertToTriMap.AddUnique(VertIndex, TriIdx);
  170. VertToTriSmoothMap.AddUnique(VertIndex, TriIdx);
  171. // Also update map of triangles that 'overlap' this vert (ie don't match UV, but do match smoothing) and should be considered when calculating normal
  172. for (int32 OverlapIdx = 0; OverlapIdx < VertOverlaps.Num(); OverlapIdx++)
  173. {
  174. // For each vert we overlap..
  175. int32 OverlapVertIdx = VertOverlaps[OverlapIdx];
  176. // Add this triangle to that vert
  177. VertToTriSmoothMap.AddUnique(OverlapVertIdx, TriIdx);
  178. // And add all of its triangles to us
  179. TArray<int32> OverlapTris;
  180. VertToTriMap.MultiFind(OverlapVertIdx, OverlapTris);
  181. for (int32 OverlapTriIdx = 0; OverlapTriIdx < OverlapTris.Num(); OverlapTriIdx++)
  182. {
  183. VertToTriSmoothMap.AddUnique(VertIndex, OverlapTris[OverlapTriIdx]);
  184. }
  185. }
  186. }
  187. // Calculate triangle edge vectors and normal
  188. const FVector Edge21 = P[1] - P[2];
  189. const FVector Edge20 = P[0] - P[2];
  190. const FVector TriNormal = (Edge21 ^ Edge20).GetSafeNormal();
  191. // If we have UVs, use those to calc
  192. if (Vertices->HasUVComponent(0))
  193. {
  194. const FVector2D T1 = Vertices->GetUV(CornerIndex[0]);
  195. const FVector2D T2 = Vertices->GetUV(CornerIndex[1]);
  196. const FVector2D T3 = Vertices->GetUV(CornerIndex[2]);
  197. FMatrix ParameterToLocal(
  198. FPlane(P[1].X - P[0].X, P[1].Y - P[0].Y, P[1].Z - P[0].Z, 0),
  199. FPlane(P[2].X - P[0].X, P[2].Y - P[0].Y, P[2].Z - P[0].Z, 0),
  200. FPlane(P[0].X, P[0].Y, P[0].Z, 0),
  201. FPlane(0, 0, 0, 1)
  202. );
  203. FMatrix ParameterToTexture(
  204. FPlane(T2.X - T1.X, T2.Y - T1.Y, 0, 0),
  205. FPlane(T3.X - T1.X, T3.Y - T1.Y, 0, 0),
  206. FPlane(T1.X, T1.Y, 1, 0),
  207. FPlane(0, 0, 0, 1)
  208. );
  209. // Use InverseSlow to catch singular matrices. Inverse can miss this sometimes.
  210. const FMatrix TextureToLocal = ParameterToTexture.Inverse() * ParameterToLocal;
  211. FaceTangentX[TriIdx] = TextureToLocal.TransformVector(FVector(1, 0, 0)).GetSafeNormal();
  212. FaceTangentY[TriIdx] = TextureToLocal.TransformVector(FVector(0, 1, 0)).GetSafeNormal();
  213. }
  214. else
  215. {
  216. FaceTangentX[TriIdx] = Edge20.GetSafeNormal();
  217. FaceTangentY[TriIdx] = (FaceTangentX[TriIdx] ^ TriNormal).GetSafeNormal();
  218. }
  219. FaceTangentZ[TriIdx] = TriNormal;
  220. }
  221. // Arrays to accumulate tangents into
  222. TArray<FVector> VertexTangentXSum, VertexTangentYSum, VertexTangentZSum;
  223. VertexTangentXSum.AddZeroed(NumVerts);
  224. VertexTangentYSum.AddZeroed(NumVerts);
  225. VertexTangentZSum.AddZeroed(NumVerts);
  226. // For each vertex..
  227. for (int VertxIdx = 0; VertxIdx < Vertices->Length(); VertxIdx++)
  228. {
  229. // Find relevant triangles for normal
  230. TArray<int32> SmoothTris;
  231. VertToTriSmoothMap.MultiFind(VertxIdx, SmoothTris);
  232. for (int i = 0; i < SmoothTris.Num(); i++)
  233. {
  234. int32 TriIdx = SmoothTris[i];
  235. VertexTangentZSum[VertxIdx] += FaceTangentZ[TriIdx];
  236. }
  237. // Find relevant triangles for tangents
  238. TArray<int32> TangentTris;
  239. VertToTriMap.MultiFind(VertxIdx, TangentTris);
  240. for (int i = 0; i < TangentTris.Num(); i++)
  241. {
  242. int32 TriIdx = TangentTris[i];
  243. VertexTangentXSum[VertxIdx] += FaceTangentX[TriIdx];
  244. VertexTangentYSum[VertxIdx] += FaceTangentY[TriIdx];
  245. }
  246. }
  247. // Finally, normalize tangents and build output arrays
  248. for (int VertxIdx = 0; VertxIdx < NumVerts; VertxIdx++)
  249. {
  250. FVector& TangentX = VertexTangentXSum[VertxIdx];
  251. FVector& TangentY = VertexTangentYSum[VertxIdx];
  252. FVector& TangentZ = VertexTangentZSum[VertxIdx];
  253. TangentX.Normalize();
  254. TangentZ.Normalize();
  255. // Use Gram-Schmidt orthogonalization to make sure X is orth with Z
  256. TangentX -= TangentZ * (TangentZ | TangentX);
  257. TangentX.Normalize();
  258. Vertices->SetTangents(VertxIdx, TangentX, TangentY, TangentZ);
  259. }
  260. }
  261. void URuntimeMeshLibrary::CalculateTangentsForMesh(const TArray<FVector>& Vertices, const TArray<int32>& Triangles, const TArray<FVector2D>& UVs, TArray<FVector>& Normals, TArray<FRuntimeMeshTangent>& Tangents)
  262. {
  263. FRuntimeMeshComponentVerticesBuilder VerticesBuilder(const_cast<TArray<FVector>*>(&Vertices), &Normals, &Tangents, nullptr, const_cast<TArray<FVector2D>*>(&UVs));
  264. FRuntimeMeshIndicesBuilder IndicesBuilder(const_cast<TArray<int32>*>(&Triangles));
  265. CalculateTangentsForMesh(&VerticesBuilder, &IndicesBuilder);
  266. }
  267. void URuntimeMeshLibrary::GenerateTessellationIndexBuffer(const IRuntimeMeshVerticesBuilder* Vertices, const FRuntimeMeshIndicesBuilder* Indices, FRuntimeMeshIndicesBuilder* OutTessellationIndices)
  268. {
  269. TessellationUtilities::CalculateTessellationIndices(Vertices, Indices, OutTessellationIndices);
  270. }
  271. void URuntimeMeshLibrary::GenerateTessellationIndexBuffer(const TArray<FVector>& Vertices, const TArray<int32>& Triangles, const TArray<FVector2D>& UVs, TArray<FVector>& Normals, TArray<FRuntimeMeshTangent>& Tangents, TArray<int32>& OutTessTriangles)
  272. {
  273. FRuntimeMeshComponentVerticesBuilder VerticesBuilder(const_cast<TArray<FVector>*>(&Vertices), &Normals, &Tangents, nullptr, const_cast<TArray<FVector2D>*>(&UVs));
  274. FRuntimeMeshIndicesBuilder IndicesBuilder(const_cast<TArray<int32>*>(&Triangles));
  275. FRuntimeMeshIndicesBuilder OutIndicesBuilder(&OutTessTriangles);
  276. GenerateTessellationIndexBuffer(&VerticesBuilder, &IndicesBuilder, &OutIndicesBuilder);
  277. }
  278. static int32 GetNewIndexForOldVertIndex(int32 MeshVertIndex, TMap<int32, int32>& MeshToSectionVertMap, const FPositionVertexBuffer* PosBuffer, const FStaticMeshVertexBuffer* VertBuffer, const FColorVertexBuffer* ColorBuffer, IRuntimeMeshVerticesBuilder* Vertices)
  279. {
  280. int32* NewIndexPtr = MeshToSectionVertMap.Find(MeshVertIndex);
  281. if (NewIndexPtr != nullptr)
  282. {
  283. return *NewIndexPtr;
  284. }
  285. else
  286. {
  287. // Copy position
  288. int32 SectionVertIndex = Vertices->MoveNextOrAdd();
  289. Vertices->SetPosition(PosBuffer->VertexPosition(MeshVertIndex));
  290. Vertices->SetNormal(VertBuffer->VertexTangentZ(MeshVertIndex));
  291. Vertices->SetTangent(VertBuffer->VertexTangentX(MeshVertIndex));
  292. if (ColorBuffer && ColorBuffer->GetNumVertices())
  293. {
  294. Vertices->SetColor(ColorBuffer->VertexColor(MeshVertIndex));
  295. }
  296. // copy all uv channels
  297. for (uint32 Index = 0; Index < VertBuffer->GetNumTexCoords(); Index++)
  298. {
  299. Vertices->SetUV(Index, VertBuffer->GetVertexUV(MeshVertIndex, Index));
  300. }
  301. MeshToSectionVertMap.Add(MeshVertIndex, SectionVertIndex);
  302. return SectionVertIndex;
  303. }
  304. }
  305. void URuntimeMeshLibrary::GetSectionFromStaticMesh(UStaticMesh* InMesh, int32 LODIndex, int32 SectionIndex,
  306. IRuntimeMeshVerticesBuilder* Vertices, FRuntimeMeshIndicesBuilder* Triangles, FRuntimeMeshIndicesBuilder* AdjacencyTriangles)
  307. {
  308. if (InMesh)
  309. {
  310. #if !WITH_EDITOR && (ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION >= 13)
  311. if (!InMesh->bAllowCPUAccess)
  312. {
  313. FMessageLog("PIE").Warning()
  314. ->AddToken(FTextToken::Create(LOCTEXT("GetSectionFromStaticMeshStart", "Calling GetSectionFromStaticMesh on")))
  315. ->AddToken(FUObjectToken::Create(InMesh))
  316. ->AddToken(FTextToken::Create(LOCTEXT("GetSectionFromStaticMeshEnd", "but 'Allow CPU Access' is not enabled. This is required for converting StaticMesh to RuntimeMeshComponent in cooked builds.")));
  317. }
  318. else
  319. #endif
  320. #if WITH_EDITOR || (ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION >= 13)
  321. if (InMesh->RenderData != nullptr && InMesh->RenderData->LODResources.IsValidIndex(LODIndex))
  322. {
  323. const FStaticMeshLODResources& LOD = InMesh->RenderData->LODResources[LODIndex];
  324. if (LOD.Sections.IsValidIndex(SectionIndex))
  325. {
  326. // Empty output buffers
  327. Vertices->Reset();
  328. Triangles->Reset();
  329. // Map from vert buffer for whole mesh to vert buffer for section of interest
  330. TMap<int32, int32> MeshToSectionVertMap;
  331. const FStaticMeshSection& Section = LOD.Sections[SectionIndex];
  332. const uint32 OnePastLastIndex = Section.FirstIndex + Section.NumTriangles * 3;
  333. FIndexArrayView Indices = LOD.IndexBuffer.GetArrayView();
  334. // Iterate over section index buffer, copying verts as needed
  335. for (uint32 i = Section.FirstIndex; i < OnePastLastIndex; i++)
  336. {
  337. uint32 MeshVertIndex = Indices[i];
  338. // See if we have this vert already in our section vert buffer, and copy vert in if not
  339. int32 SectionVertIndex = GetNewIndexForOldVertIndex(MeshVertIndex, MeshToSectionVertMap, &LOD.PositionVertexBuffer, &LOD.VertexBuffer, &LOD.ColorVertexBuffer, Vertices);
  340. // Add to index buffer
  341. Triangles->AddIndex(SectionVertIndex);
  342. }
  343. if (AdjacencyTriangles != nullptr)
  344. {
  345. AdjacencyTriangles->Reset();
  346. // Adjacency indices use 12 per triangle instead of 3. So start position and length both need to be multiplied by 4
  347. const uint32 SectionAdjacencyFirstIndex = Section.FirstIndex * 4;
  348. const uint32 SectionAdjacencyOnePastLastIndex = SectionAdjacencyFirstIndex + Section.NumTriangles * (3 * 4);
  349. FIndexArrayView AdjacencyIndices = LOD.AdjacencyIndexBuffer.GetArrayView();
  350. // Iterate over section adjacency index buffer, copying any new verts as needed
  351. for (uint32 i = SectionAdjacencyFirstIndex; i < SectionAdjacencyOnePastLastIndex; i++)
  352. {
  353. uint32 MeshVertIndex = AdjacencyIndices[i];
  354. // See if we have this vert already in our section vert buffer, and copy vert in if not
  355. int32 SectionVertIndex = GetNewIndexForOldVertIndex(MeshVertIndex, MeshToSectionVertMap, &LOD.PositionVertexBuffer, &LOD.VertexBuffer, &LOD.ColorVertexBuffer, Vertices);
  356. // Add to index buffer
  357. AdjacencyTriangles->AddIndex(SectionVertIndex);
  358. }
  359. }
  360. }
  361. }
  362. #endif
  363. }
  364. }
  365. void URuntimeMeshLibrary::GetSectionFromStaticMesh(UStaticMesh* InMesh, int32 LODIndex, int32 SectionIndex, TArray<FVector>& Vertices,
  366. TArray<int32>& Triangles, TArray<FVector>& Normals, TArray<FVector2D>& UVs, TArray<FRuntimeMeshTangent>& Tangents)
  367. {
  368. FRuntimeMeshComponentVerticesBuilder VerticesBuilder(&Vertices, &Normals, &Tangents, nullptr, &UVs);
  369. FRuntimeMeshIndicesBuilder IndicesBuilder(&Triangles);
  370. GetSectionFromStaticMesh(InMesh, LODIndex, SectionIndex, &VerticesBuilder, &IndicesBuilder, nullptr);
  371. }
  372. void URuntimeMeshLibrary::CopyRuntimeMeshFromStaticMeshComponent(UStaticMeshComponent* StaticMeshComp, int32 LODIndex,
  373. URuntimeMeshComponent* RuntimeMeshComp, bool bShouldCreateCollision)
  374. {
  375. #if ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION >= 14
  376. UStaticMesh* StaticMesh = StaticMeshComp->GetStaticMesh();
  377. #else
  378. UStaticMesh* StaticMesh = StaticMeshComp->StaticMesh;
  379. #endif
  380. if (StaticMeshComp != nullptr && StaticMesh != nullptr && RuntimeMeshComp != nullptr)
  381. {
  382. //// MESH DATA
  383. // Make sure LOD index is valid
  384. if (StaticMesh->RenderData == nullptr || !StaticMesh->RenderData->LODResources.IsValidIndex(LODIndex))
  385. {
  386. return;
  387. }
  388. // Get specified LOD
  389. const FStaticMeshLODResources& LOD = StaticMesh->RenderData->LODResources[LODIndex];
  390. #if ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION >= 13
  391. int32 NumSections = StaticMesh->GetNumSections(LODIndex);
  392. #else
  393. int32 NumSections = LOD.Sections.Num();
  394. #endif
  395. for (int32 SectionIndex = 0; SectionIndex < NumSections; SectionIndex++)
  396. {
  397. // Buffers for copying geom data
  398. if (LOD.Sections.IsValidIndex(SectionIndex))
  399. {
  400. int32 NumUVChannels = LOD.GetNumTexCoords();
  401. FRuntimeMeshIndicesBuilder AdjacencyTriangles;
  402. if (NumUVChannels <= 1)
  403. {
  404. FRuntimeMeshPackedVerticesBuilder<FRuntimeMeshVertexSimple> Vertices;
  405. FRuntimeMeshIndicesBuilder Triangles;
  406. // Get geom data from static mesh
  407. GetSectionFromStaticMesh(StaticMesh, LODIndex, SectionIndex, &Vertices, &Triangles, &AdjacencyTriangles);
  408. // Create section using data
  409. RuntimeMeshComp->CreateMeshSection(SectionIndex, *Vertices.GetVertices(), *Triangles.GetIndices(),
  410. bShouldCreateCollision, EUpdateFrequency::Infrequent, ESectionUpdateFlags::MoveArrays);
  411. }
  412. else
  413. {
  414. FRuntimeMeshPackedVerticesBuilder<FRuntimeMeshVertexDualUV> Vertices;
  415. FRuntimeMeshIndicesBuilder Triangles;
  416. // Get geom data from static mesh
  417. GetSectionFromStaticMesh(StaticMesh, LODIndex, SectionIndex, &Vertices, &Triangles, &AdjacencyTriangles);
  418. // Create section using data
  419. RuntimeMeshComp->CreateMeshSection(SectionIndex, *Vertices.GetVertices(), *Triangles.GetIndices(),
  420. bShouldCreateCollision, EUpdateFrequency::Infrequent, ESectionUpdateFlags::MoveArrays);
  421. }
  422. }
  423. }
  424. //// SIMPLE COLLISION
  425. // Clear any existing collision hulls
  426. RuntimeMeshComp->ClearCollisionConvexMeshes();
  427. if (StaticMesh->BodySetup != nullptr)
  428. {
  429. // Iterate over all convex hulls on static mesh..
  430. const int32 NumConvex = StaticMesh->BodySetup->AggGeom.ConvexElems.Num();
  431. for (int ConvexIndex = 0; ConvexIndex < NumConvex; ConvexIndex++)
  432. {
  433. // Copy convex verts to ProcMesh
  434. FKConvexElem& MeshConvex = StaticMesh->BodySetup->AggGeom.ConvexElems[ConvexIndex];
  435. RuntimeMeshComp->AddCollisionConvexMesh(MeshConvex.VertexData);
  436. }
  437. }
  438. //// MATERIALS
  439. for (int32 MatIndex = 0; MatIndex < StaticMeshComp->GetNumMaterials(); MatIndex++)
  440. {
  441. RuntimeMeshComp->SetMaterial(MatIndex, StaticMeshComp->GetMaterial(MatIndex));
  442. }
  443. }
  444. }
  445. #undef LOCTEXT_NAMESPACE