UE5 获取各个信息的像素

复制代码
UFUNCTION(BlueprintCallable,BlueprintPure)
	static TArray<FColor> GetMediaTexturePixels(UMediaTexture* MyMediaTexture)
	{
		TArray<FColor> PixelColorsArr;
		UTexture2D* UT2 = MediaTextureToTexture2D(MyMediaTexture);
		const FColor* const Colors = static_cast<FColor*>(UT2->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_ONLY));
		uint16 OutX = UT2->GetSizeX();
		uint16 OutY = UT2->GetSizeY();
		for(int i = 0; i < OutY; i++)
		{
			for(int j = 0; j < OutX; j++)
			{
				FColor PixelColor = Colors[i * OutX + j];
				PixelColorsArr.Add(PixelColor);
			}
		}
		UT2->PlatformData->Mips[0].BulkData.Unlock();
		return PixelColorsArr;
	}
	
	UFUNCTION(BlueprintCallable,BlueprintPure)
	static UTexture2D* MediaTextureToTexture2D(UMediaTexture* MyMediaTexture)
	{
		check(IsInGameThread());
		TArray<FColor> OutColors;
		FMediaTextureResource* MediaTextureResource = static_cast<FMediaTextureResource*>(MyMediaTexture->GetResource());
		MediaTextureResource->ReadPixels(OutColors);

		UTexture2D* NewTexture2D = nullptr;
		void* TextureData = nullptr;
		const int32 ImageWidth = MyMediaTexture->GetWidth();
		const int32 ImageHeight = MyMediaTexture->GetHeight();
		UE_LOG(LogTemp, Warning, TEXT("ImageWidth:%d"), ImageWidth);
		UE_LOG(LogTemp, Warning, TEXT("ImageHeight:%d"), ImageHeight);

		NewTexture2D = UTexture2D::CreateTransient(ImageWidth, ImageHeight, PF_B8G8R8A8, FName(TEXT("测试贴图")));

		FTexture2DMipMap& Mipmap = NewTexture2D->GetPlatformData()->Mips[0];
		TextureData = Mipmap.BulkData.Lock(LOCK_READ_WRITE);

		FMemory::Memcpy(TextureData, OutColors.GetData(), OutColors.Num() * 4);
		Mipmap.BulkData.Unlock();
		NewTexture2D->UpdateResource();
		Mipmap.BulkData.Unlock();
		
		return NewTexture2D;
	}

这里有两个类型的像素

一个是UTexture2D的像素

二是UMediaTexture的像素


复制代码
static FColor GetScreenPositionColor(POINT P)
	{
        Windows::HDC hdc = ::GetDC(NULL);
        COLORREF Color = ::GetPixel(hdc, P.x, P.y);
        int R = GetRValue(Color);
        int G = GetGValue(Color);
        int B = GetBValue(Color);
        ::ReleaseDC(NULL, hdc);
        FColor realColor = FColor(R, G, B);
        
        return realColor;
	}
	UFUNCTION(BlueprintCallable)
	static TArray<FColor> GetScreenColors()
	{
		TArray<FColor> ScreenColors;
		for(int y = 0; y < 1080; y++)
		{
			for(int x = 0; x < 1920; x++)
			{
				ScreenColors.Add(GetScreenPositionColor(POINT{x, y}));
			}
		}
		return ScreenColors;
	}

这里使用Windows的HDC来获取屏幕中所有像素,这个不适合获取屏幕上所有像素,速度很慢,适合做取色器来获取其中鼠标中的一个像素


复制代码
UFUNCTION(BlueprintCallable)
	static TArray<FColor> GetPixelsFromSceneCapture(USceneCaptureComponent2D* scc)
	{
		TArray<FColor> Pixels;
		scc->TextureTarget->GameThread_GetRenderTargetResource()->ReadPixels(Pixels);
		return Pixels;
	}

获取场景捕获器中,捕获的RT中所有的像素

相关推荐
Helix25010 分钟前
JDK高频报错速查手册:javac不是内部命令、内存溢出、版本不兼容等12类问题解决方法
java·开发语言·内存溢出·java异常处理·jdk版本兼容·jdk报错·javac报错
郝学胜-神的一滴1 小时前
Effective STL 条款 07:指针容器销毁前先删除
开发语言·c++·vscode·leetcode·rpc·visual studio
CoderYanger1 小时前
A.每日一题:2243. 计算字符串的数字和
java·开发语言·程序人生·leetcode·面试·蓝桥杯·学习方法
kyriewen2 小时前
我让用户白等两秒,只为一个 16MB 的中文字体
前端·javascript·程序员
摇滚侠2 小时前
《On Java 中文版 基础卷》阅读笔记 接口 19
java·开发语言·笔记
吃我一窝jio3 小时前
Java多线程核心知识点梳理
java·开发语言·笔记
泯泷4 小时前
第二章:你的第一个Lit组件 - 从零到"Hello, World"
前端·javascript·web components
geovindu4 小时前
rust: Timing Functions Pattern
开发语言·后端·设计模式·rust·计时函数模式·性能分析型模式
geovindu5 小时前
python:Lasso
开发语言·后端·python·算法
多加点辣也没关系6 小时前
JavaScript|第42章:安全与最佳实践
开发语言·javascript·安全