void URuntimeMeshLibrary::GetStaticMeshSectionPacked(UStaticMesh* InMesh, int32 LODIndex, int32 SectionIndex, TArray<FRuntimeMeshBlueprintVertexSimple>& Vertices, TArray<int32>& Triangles) { Vertices.Empty(); Triangles.Empty(); GetStaticMeshSection(InMesh, LODIndex, SectionIndex, 1, [&Vertices](FVector Position, FVector TangentX, FVector TangentY, FVector TangentZ) -> int32 { return Vertices.Add(FRuntimeMeshBlueprintVertexSimple(Position, TangentZ, FRuntimeMeshTangent(TangentX, GetBasisDeterminantSign(TangentX, TangentY, TangentZ) < 0), FVector2D::ZeroVector)); }, [&Vertices](int32 Index, int32 UVIndex, FVector2D UV) { check(UVIndex == 0); Vertices[Index].UV0 = UV; }, [&Vertices](int32 Index, FColor Color) { Vertices[Index].Color = Color; }, [&Triangles](int32 Index) { Triangles.Add(Index); }, [](int32 Index) { }); }
void URuntimeMeshLibrary::GetStaticMeshSection(UStaticMesh* InMesh, int32 LODIndex, int32 SectionIndex, TArray<FVector>& Vertices, TArray<int32>& Triangles, TArray<FVector>& Normals, TArray<FVector2D>& UVs, TArray<FColor>& Colors, TArray<FRuntimeMeshTangent>& Tangents) { Vertices.Empty(); Triangles.Empty(); Normals.Empty(); UVs.Empty(); Colors.Empty(); Tangents.Empty(); GetStaticMeshSection(InMesh, LODIndex, SectionIndex, 1, [&Vertices, &Normals, &Tangents](FVector Position, FVector TangentX, FVector TangentY, FVector TangentZ) -> int32 { int32 NewIndex = Vertices.Add(Position); Normals.Add(TangentZ); Tangents.Add(FRuntimeMeshTangent(TangentX, GetBasisDeterminantSign(TangentX, TangentY, TangentZ) < 0)); return NewIndex; }, [&UVs](int32 Index, int32 UVIndex, FVector2D UV) { check(UVIndex == 0); UVs.SetNum(Index + 1); UVs[Index] = UV; }, [&Colors](int32 Index, FColor Color) { Colors.SetNum(Index + 1); Colors[Index] = Color; }, [&Triangles](int32 Index) { Triangles.Add(Index); }, [](int32 Index) { }); }
void URuntimeMeshLibrary::CreateBoxMesh(FVector BoxRadius, TArray<FVector>& Vertices, TArray<int32>& Triangles, TArray<FVector>& Normals, TArray<FVector2D>& UVs, TArray<FRuntimeMeshTangent>& Tangents) { // Generate verts FVector BoxVerts[8]; BoxVerts[0] = FVector(-BoxRadius.X, BoxRadius.Y, BoxRadius.Z); BoxVerts[1] = FVector(BoxRadius.X, BoxRadius.Y, BoxRadius.Z); BoxVerts[2] = FVector(BoxRadius.X, -BoxRadius.Y, BoxRadius.Z); BoxVerts[3] = FVector(-BoxRadius.X, -BoxRadius.Y, BoxRadius.Z); BoxVerts[4] = FVector(-BoxRadius.X, BoxRadius.Y, -BoxRadius.Z); BoxVerts[5] = FVector(BoxRadius.X, BoxRadius.Y, -BoxRadius.Z); BoxVerts[6] = FVector(BoxRadius.X, -BoxRadius.Y, -BoxRadius.Z); BoxVerts[7] = FVector(-BoxRadius.X, -BoxRadius.Y, -BoxRadius.Z); // Generate triangles (from quads) Triangles.Reset(); const int32 NumVerts = 24; // 6 faces x 4 verts per face Vertices.Reset(); Vertices.AddUninitialized(NumVerts); Normals.Reset(); Normals.AddUninitialized(NumVerts); Tangents.Reset(); Tangents.AddUninitialized(NumVerts); Vertices[0] = BoxVerts[0]; Vertices[1] = BoxVerts[1]; Vertices[2] = BoxVerts[2]; Vertices[3] = BoxVerts[3]; ConvertQuadToTriangles(Triangles, 0, 1, 2, 3); Normals[0] = Normals[1] = Normals[2] = Normals[3] = FVector(0, 0, 1); Tangents[0] = Tangents[1] = Tangents[2] = Tangents[3] = FRuntimeMeshTangent(0.f, -1.f, 0.f); Vertices[4] = BoxVerts[4]; Vertices[5] = BoxVerts[0]; Vertices[6] = BoxVerts[3]; Vertices[7] = BoxVerts[7]; ConvertQuadToTriangles(Triangles, 4, 5, 6, 7); Normals[4] = Normals[5] = Normals[6] = Normals[7] = FVector(-1, 0, 0); Tangents[4] = Tangents[5] = Tangents[6] = Tangents[7] = FRuntimeMeshTangent(0.f, -1.f, 0.f); Vertices[8] = BoxVerts[5]; Vertices[9] = BoxVerts[1]; Vertices[10] = BoxVerts[0]; Vertices[11] = BoxVerts[4]; ConvertQuadToTriangles(Triangles, 8, 9, 10, 11); Normals[8] = Normals[9] = Normals[10] = Normals[11] = FVector(0, 1, 0); Tangents[8] = Tangents[9] = Tangents[10] = Tangents[11] = FRuntimeMeshTangent(-1.f, 0.f, 0.f); Vertices[12] = BoxVerts[6]; Vertices[13] = BoxVerts[2]; Vertices[14] = BoxVerts[1]; Vertices[15] = BoxVerts[5]; ConvertQuadToTriangles(Triangles, 12, 13, 14, 15); Normals[12] = Normals[13] = Normals[14] = Normals[15] = FVector(1, 0, 0); Tangents[12] = Tangents[13] = Tangents[14] = Tangents[15] = FRuntimeMeshTangent(0.f, 1.f, 0.f); Vertices[16] = BoxVerts[7]; Vertices[17] = BoxVerts[3]; Vertices[18] = BoxVerts[2]; Vertices[19] = BoxVerts[6]; ConvertQuadToTriangles(Triangles, 16, 17, 18, 19); Normals[16] = Normals[17] = Normals[18] = Normals[19] = FVector(0, -1, 0); Tangents[16] = Tangents[17] = Tangents[18] = Tangents[19] = FRuntimeMeshTangent(1.f, 0.f, 0.f); Vertices[20] = BoxVerts[7]; Vertices[21] = BoxVerts[6]; Vertices[22] = BoxVerts[5]; Vertices[23] = BoxVerts[4]; ConvertQuadToTriangles(Triangles, 20, 21, 22, 23); Normals[20] = Normals[21] = Normals[22] = Normals[23] = FVector(0, 0, -1); Tangents[20] = Tangents[21] = Tangents[22] = Tangents[23] = FRuntimeMeshTangent(0.f, 1.f, 0.f); // UVs UVs.Reset(); UVs.AddUninitialized(NumVerts); UVs[0] = UVs[4] = UVs[8] = UVs[12] = UVs[16] = UVs[20] = FVector2D(0.f, 0.f); UVs[1] = UVs[5] = UVs[9] = UVs[13] = UVs[17] = UVs[21] = FVector2D(0.f, 1.f); UVs[2] = UVs[6] = UVs[10] = UVs[14] = UVs[18] = UVs[22] = FVector2D(1.f, 1.f); UVs[3] = UVs[7] = UVs[11] = UVs[15] = UVs[19] = UVs[23] = FVector2D(1.f, 0.f); }