PLY 生成 StaticMesh

PLY 生成 StaticMesh

PLY

PLY,多边形文件格式,表示存储描述为多边形集合的图形对象的 3D 文件格式。这种文件格式的目的是建立一种简单易用的文件类型,该文件类型足够通用,可用于各种模型。 PLY 文件格式有 ASCII 和二进制格式,用于紧凑存储和快速保存和加载。

PLY 格式的对象由一组顶点、面和其他元素以及可以附加到这些元素的颜色和法线方向等属性来描述。

由 PLY 格式表示的对象可以是各种来源的结果,例如手动数字化对象、建模应用程序中的多边形对象、范围数据、立方体中的三角形、地形数据和辐射模型。

ply
format ascii 1.0           {
    
     ascii/binary, format version number }
comment made by Greg Turk  {
    
     comments keyword specified, like all lines }
comment this file is a cube
element vertex 8           {
    
     define "vertex" element, 8 of them in file }
property float x           {
    
     vertex contains float "x" coordinate }
property float y           {
    
     y coordinate is also a vertex property }
property float z           {
    
     z coordinate, too }
element face 6             {
    
     there are 6 "face" elements in the file }
property list uchar int vertex_index {
    
     "vertex_indices" is a list of ints }
end_header                 {
    
     delimits the end of the header }
0 0 0                      {
    
     start of vertex list }
0 0 1
0 1 1
0 1 0
1 0 0
1 0 1
1 1 1
1 1 0
4 0 1 2 3                  {
    
     start of face list }
4 7 6 5 4
4 0 4 5 1
4 1 5 6 2
4 2 6 7 3
4 3 7 4 0

在这里插入图片描述

PLY2StaticMesh

	if (inMeshPath.Len() <= 3)
	{
    
    
		return;
	}
	FString FileName=FPaths::GetBaseFilename(inMeshPath);
	FString RawData;
	TArray<FString> DataLineArr;
	int32 EndHeaderIndex = 0;
	int32 VertexCount = 0;
	int32 TriangleCount = 0;

	TArray<uint8> FileData;
	bool isOk = FFileHelper::LoadFileToArray(FileData, *inMeshPath);
	if (!isOk)
	{
    
    
		return;
	}
	FFileHelper::BufferToString(RawData, FileData.GetData(), FileData.Num());
	RawData.ParseIntoArray(DataLineArr, TEXT("\n"));

	FVector3f newVertex;
	FIntVector newTriangle;
	TArray<uint8> LineData;
	int32 LineNum = 0;
	FString strData;

	if (DataLineArr.Num() < 15)
	{
    
    
		return;
	}

	for (size_t i = 0; i < DataLineArr.Num(); ++i)
	{
    
    
		if (DataLineArr[i].StartsWith(TEXT("element vertex")))
		{
    
    
			VertexCount = FCString::Atoi(*DataLineArr[i].RightChop(14));
		}
		else if (DataLineArr[i].StartsWith(TEXT("element face")))
		{
    
    
			TriangleCount = FCString::Atoi(*DataLineArr[i].RightChop(12));
		}
		else if (DataLineArr[i].StartsWith(TEXT("end_header")))
		{
    
    
			EndHeaderIndex = i + 1;
			break;
		}
	}


	TArray<FVector3f>Normals,Tangents;
	TArray<int32>Triangles;
	TArray<FVector2f>UV;
	TArray<FVector3f>Vertices;

	for (size_t i = EndHeaderIndex; i < EndHeaderIndex + VertexCount; i++)
	{
    
    
		TArray<FString> Items;
		DataLineArr[i].ParseIntoArray(Items, TEXT(" "));

		newVertex.X = FCString::Atod(*Items[0]) - WorldCenterLocation.X;
		newVertex.Y = FCString::Atod(*Items[1]) - WorldCenterLocation.Y;
		newVertex.Z = FCString::Atod(*Items[2]) - WorldCenterLocation.Z;

		newVertex *= FVector3f(100, -100, 100);

		Vertices.Add(newVertex);
		UV.Add(FVector2f::Zero());
		Normals.Add(FVector3f::UpVector);
		Tangents.Add(FVector3f::ForwardVector);
	}

	for (size_t i = EndHeaderIndex + VertexCount; i < EndHeaderIndex + VertexCount + TriangleCount; i++)
	{
    
    
		TArray<FString> Items;
		DataLineArr[i].ParseIntoArray(Items, TEXT(" "));

		newTriangle.X = FCString::Atoi(*Items[1]);
		newTriangle.Y = FCString::Atoi(*Items[2]);
		newTriangle.Z = FCString::Atoi(*Items[3]);

		Triangles.Add(newTriangle.X);
		Triangles.Add(newTriangle.Y);
		Triangles.Add(newTriangle.Z);
	}
	//to static mesh asset->见参考链接
	

在这里插入图片描述

参考

  1. OpenCTM Mesh 转换为UE4 静态资源Asset

猜你喜欢

转载自blog.csdn.net/mrbaolong/article/details/131097246