UE怎么在C++里设置 procedural mesh里的顶点颜色
时间: 2024-03-21 17:38:32 浏览: 276
在C++中设置Procedural Mesh的顶点颜色可以通过以下步骤完成:
1. 定义一个顶点结构体,包含顶点的位置和颜色信息:
```c++
struct FPMVertex
{
FVector Position;
FColor Color;
};
```
2. 创建一个顶点缓冲区,并将顶点数据写入其中:
```c++
TArray<FPMVertex> Vertices;
//添加顶点数据
//...
ProceduralMesh->CreateMeshSection_LinearColor(0, Vertices, TArray<int32>(), TArray<FVector>(), TArray<FVector2D>(), TArray<FLinearColor>(), TArray<FProcMeshTangent>(), true);
```
3. 在Procedural Mesh的渲染函数中设置顶点颜色:
```c++
void AMyProceduralMeshActor::GenerateMeshGeometry()
{
TArray<FPMVertex> Vertices;
//添加顶点数据
//...
ProceduralMesh->ClearAllMeshSections();
ProceduralMesh->CreateMeshSection_LinearColor(0, Vertices, TArray<int32>(), TArray<FVector>(), TArray<FVector2D>(), TArray<FLinearColor>(), TArray<FProcMeshTangent>(), true);
}
void AMyProceduralMeshActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
//设置顶点颜色
UProceduralMeshComponent::FProcMeshSection& Section = ProceduralMesh->GetProcMeshSection(0);
for (int i = 0; i < Section.VertexColors.Num(); i++)
{
Section.VertexColors[i] = FLinearColor(1.0f, 0.0f, 0.0f, 1.0f);
}
ProceduralMesh->UpdateMeshSection(0);
}
```
在上述代码中,我们在Tick函数中设置了Procedural Mesh的顶点颜色为红色。你可以根据需要修改颜色值。
阅读全文