UE4运行时的顶点绘制

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、运行

 

 

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嘿克不黑

你的鼓励是我的最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值