UE4 绘制简单三角形(五)
使用FRawMesh 绘制两个三角形面片,并赋予不同的材质,生成本地资源StaticMesh 。
FString MeshName = "TestRawMesh";
FString PackageName = "/Game/" + MeshName;
UPackage* MeshPackage = CreatePackage(nullptr, *PackageName);
UStaticMesh* StaticMesh = NewObject< UStaticMesh >(MeshPackage, FName(*MeshName), RF_Public | RF_Standalone);
FRawMesh RawMesh;
RawMesh.VertexPositions.Add(FVector(0, 0, 0));
RawMesh.VertexPositions.Add(FVector(100, 0, 0));
RawMesh.VertexPositions.Add(FVector(100, 100, 0));
RawMesh.VertexPositions.Add(FVector(0, 100, 0));
RawMesh.WedgeIndices.Add(0);
RawMesh.WedgeIndices.Add(2);
RawMesh.WedgeIndices.Add(1);
RawMesh.WedgeIndices.Add(0);
RawMesh.WedgeIndices.Add(3);
RawMesh.WedgeIndices.Add(2);
RawMesh.WedgeTexCoords[0].Add(FVector2D(0, 0));
RawMesh.WedgeTexCoords[0].Add(FVector2D(1, 0));
RawMesh.WedgeTexCoords[0].Add(FVector2D(0, 1));
RawMesh.WedgeTexCoords[0].Add(FVector2D(0, 0));
RawMesh.WedgeTexCoords[0].Add(FVector2D(1, 1));
RawMesh.WedgeTexCoords[0].Add(FVector2D(1, 0));
RawMesh.FaceMaterialIndices.Add(0);
RawMesh.FaceMaterialIndices.Add(1);
RawMesh.FaceSmoothingMasks.Add(0);
RawMesh.FaceSmoothingMasks.Add(1);
RawMesh.WedgeTangentX.Add(FVector(1, 0, 0));
RawMesh.WedgeTangentX.Add(FVector(1, 0, 0));
RawMesh.WedgeTangentX.Add(FVector(1, 0, 0));
RawMesh.WedgeTangentX.Add(FVector(1, 0, 0));
RawMesh.WedgeTangentX.Add(FVector(1, 0, 0));
RawMesh.WedgeTangentX.Add(FVector(1, 0, 0));
RawMesh.WedgeTangentY.Add(FVector(0, 1, 0));
RawMesh.WedgeTangentY.Add(FVector(0, 1, 0));
RawMesh.WedgeTangentY.Add(FVector(0, 1, 0));
RawMesh.WedgeTangentY.Add(FVector(0, 1, 0));
RawMesh.WedgeTangentY.Add(FVector(0, 1, 0));
RawMesh.WedgeTangentY.Add(FVector(0, 1, 0));
RawMesh.WedgeTangentZ.Add(FVector(0, 0, 1));
RawMesh.WedgeTangentZ.Add(FVector(0, 0, 1));
RawMesh.WedgeTangentZ.Add(FVector(0, 0, 1));
RawMesh.WedgeTangentZ.Add(FVector(0, 0, 1));
RawMesh.WedgeTangentZ.Add(FVector(0, 1, 0));
RawMesh.WedgeTangentZ.Add(FVector(0, 1, 0));
if (!RawMesh.IsValid())
return;
StaticMesh->PreEditChange(nullptr);
FStaticMeshSourceModel& SrcModel = StaticMesh->AddSourceModel();
SrcModel.SaveRawMesh(RawMesh);
TArray< FText > BuildErrors;
StaticMesh->Build(true, &BuildErrors);
FAssetRegistryModule::AssetCreated(StaticMesh);
参考
- UE4绘制简单三角形(一)
- UE4绘制简单三角形(二)
- UE4绘制简单三角形(三)
- UE4绘制简单三角形(四)
- UE4尝试用C++生成一个最简单的StaticMesh
|