前言:
如何使用虚幻自带的绘画自定义顶点的函数FSlateDrawElement::MakeCustomVerts()。
直接上代码演示:
void SDrawWidget::TestCustomVerts(FSlateWindowElementList& outDrawElements, const FPaintGeometry& PaintGeometry, int32 LayerId) const
{
//获得一个笔刷
const FSlateBrush* MyBrush = FCoreStyle::Get().GetBrush("ColorSpectrum.Spectrum");
FSlateResourceHandle Handle = FSlateApplication::Get().GetRenderer()->GetResourceHandle(*MyBrush);
//顶点数组
TArray<FSlateVertex> vertexs;
//渲染索引
TArray<SlateIndex> indexs;
//需要画的三个顶点
FSlateVertex v1;
FSlateVertex v2;
FSlateVertex v3;
vertexs.Add(v1);
vertexs.Add(v2);
vertexs.Add(v3);
//绘画顶点的顺序
indexs.Add(0);
indexs.Add(1);
indexs.Add(2);
//顶点的UV
for (FSlateVertex& tempvertex:vertexs)
{
tempvertex.TexCoords[0] = 0.0f;
tempvertex.TexCoords[1] = 0.0f;
tempvertex.TexCoords[2] = 0.0f;
tempvertex.TexCoords[3] = 0.0f;
tempvertex.MaterialTexCoords = FVector2D(0.0f,0.0f);
tempvertex.PixelSize[0] = 0.0f;
tempvertex.PixelSize[1] = 0.0f;
}
//给顶点颜色
vertexs[0].Color = FColor::Blue;
vertexs[1].Color = FColor::Red;
vertexs[2].Color = FColor::Green;
//下面是给三个顶点位置
vertexs[0].Position = PaintGeometry.GetAccumulatedRenderTransform().TransformPoint(FVector2D(0,0));
vertexs[1].Position = PaintGeometry.GetAccumulatedRenderTransform().TransformPoint(FVector2D(1000,500));
vertexs[2].Position = PaintGeometry.GetAccumulatedRenderTransform().TransformPoint(FVector2D(300,300));
//绘画函数,上面代码全是为了构造数据
FSlateDrawElement::MakeCustomVerts(outDrawElements, LayerId, Handle, vertexs, indexs, nullptr, 0, 0);
}
最后在Slate重写的OnPainth函数中调用就行了
TestCustomVerts(OutDrawElements, AllottedGeometry.ToPaintGeometry(), LayerId);