void XXX::DrawInRenderThread(
FRHICommandListImmediate& RHICmdList,
ERHIFeatureLevel::Type FeatureLevel
)
{
check(IsInRenderingThread());
// clearColors是图像清除时的底色
TArray<FColor> clearColors;
for (uint32 Row = 0; Row < cameraInfo->height; ++Row)
{
for (uint32 Col = 0; Col < cameraInfo->width; ++Col)
{
clearColors.Add(FColor::Black);
}
}
// 用于绘制的纹理
UTexture2D* tex = m_texture;
FTextureReferenceRHIRef MyTextureRHI = tex->TextureReference.TextureReferenceRHI;
FRHITexture* TexRef = MyTextureRHI->GetTextureReference()->GetReferencedTexture();
FRHITexture2D* TexRef2D = (FRHITexture2D*)TexRef;
uint32 LolStride = 0;
int width = tex->PlatformData->SizeX;
int height = tex->PlatformData->SizeY;
int x = 30;
int y = 50;
int radius = 20;
// 清空代码
// FUpdateTextureRegion2D region = FUpdateTextureRegion2D(0, 0, 0, 0, width, height);
// RHIUpdateTexture2D(TexRef2D, 0, region, LolStride, (uint8*)clearColors.GetData());
// 绘制代码
TArray<FColor> colors;
int rowMin = FMath::Max(0, y - radius);
int rowMax = FMath::Min(height, y + radius);
int colMin = FMath::Max(0, x - radius);
int colMax = FMath::Min(width, x + radius);
int radius2 = radius * radius;
// 根据直径画圆
for (int row = rowMin; row <= rowMax; row++)
{
for (int col = colMin; col <= colMax; col++)
{
int rowDis = row - y;
int colDis = col - x;
int distance = rowDis * rowDis + colDis * colDis;
if (distance < radius2)
{
colors.Add(FColor::Black);
}
else {
colors.Add(FColor::Green);
}
}
}
FUpdateTextureRegion2D region = FUpdateTextureRegion2D(colMin, rowMin, 0, 0, colMax - colMin, rowMax - rowMin);
RHIUpdateTexture2D(TexRef2D, 0, region, LolStride, (uint8*)colors.GetData());
}
// 调用
void XXX::Tick(float DeltaTime)
{
UWorld* World = pThis->GetWorld();
ERHIFeatureLevel::Type FeatureLevel = World->Scene->GetFeatureLevel();
// 绘图
ENQUEUE_RENDER_COMMAND(CaptureCommand)(
[FeatureLevel](FRHICommandListImmediate& RHICmdList)
{
pThis->DrawInRenderThread
(
RHICmdList,
FeatureLevel
);
}
);
}
UE4纹理Texture2D绘制
最新推荐文章于 2024-08-16 18:12:04 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)