1、创建一个空的C++虚幻项目。
2、添加一个Blueprint function library 。
3、 添加依赖
4、在刚刚创建的Blueprint function library头文件添加如下内容。
UFUNCTION(BlueprintCallable, Category = VertexPainting)
static void PaintSMVertices(UStaticMeshComponent* SMComp);
5、在刚刚创建的Blueprint function library源文件添加如下内容。
void UMyBlueprintFunctionLibrary::PaintSMVertices(UStaticMeshComponent* SMComp)
{
if (!SMComp) return;
//Get the static mesh that we're going to paint
UStaticMesh* SM = SMComp->GetStaticMesh();
if (SM)
{
//Get the vertex buffer from the 1st lod
//FPositionVertexBuffer* PositionVertexBuffer = &SM->RenderData->LODResources[0].VertexBuffers.PositionVertexBuffer;
//Make sure that we have at least 1 LOD
SMComp->SetLODDataCount(1, SMComp->LODData.Num());
FStaticMeshComponentLODInfo* LODInfo = &SMComp->LODData[0]; //We're going to modify the 1st LOD only
//Empty the painted vertices and assign a new color vertex buffer which will contain the new colors for each vertex
LODInfo->PaintedVertices.Empty();
LODInfo->OverrideVertexColors = new FColorVertexBuffer();
//We're going to use the LODResources to get the total number of vertices that the provided mesh has
FStaticMeshLODResources& LodResources = SM->RenderData->LODResources[0];
//Creating a color array
TArray<FColor> RandomColorArray;
//Since we know beforehand the number of elements we might as well reserve the memory now
RandomColorArray.Reserve(LodResources.GetNumVertices() - 1);
for (int32 i = 0; i < LodResources.GetNumVertices(); i++)
{
//Generate a random color for the current vertex
RandomColorArray.Add(FColor::MakeRandomColor());
}
//Initialize the new vertex colros with the array we created above
LODInfo->OverrideVertexColors->InitFromColorArray(RandomColorArray);
//Initialize resource and mark render state of object as dirty in order for the engine to re-render it
BeginInitResource(LODInfo->OverrideVertexColors);
SMComp->MarkRenderStateDirty();
}
}
6、编译
7、打开关卡蓝图
8、在场景里面拖入一个Actor,比如Cylinder,然后把这个玩意拖入刚刚打卡的蓝图里面。最后添加如下内容。
9、新建一个材质,然后双击打开,添加如下内容,然后把材质赋给我们拖入场景的Actor上面。
10、运行