How does the "Import Buffer as Texture 2D" node work?

The title explains it. This is the node:
image

I can’t figure out how it works. I am using it to try and save and load a render target, but I just don’t know how this node works. As in: how do I structure the input buffer array?

Having used other rendering APIs in the past, such as SFML, I assumed it was RGBA RGBA RGBA… for as many pixels as there are, but apparently that just gives out an empty image. (invalid object reference)

i think the buffer is a byte array for what is " file like" data.
in other words similar to a binary jpeg.

check the comment on " compress image" and " decompress image" https://github.com/EpicGames/UnrealEngine/blob/a3cb3d8fdec1fc32f071ae7d22250f33f80b21c4/Engine/Source/Runtime/Engine/Public/ImageUtils.h#L130

i dont see an export equivalent for the import buffer as texture 2d
i think you might get more luck using “ExportTexture2D” and " ImportFileAsTexture2D" (both manipulates a file directly and uses a filename).

The problem with using a file is that I would like to have all save data in one single file. I will have a look at the source code, otherwise, I have a slower, alternative method… But it’s quite slow…

Solved. What I am trying to do is save a render target to a save file and load it back in. The render target uses 8 bit RGB (24 bits per pixel) and will always be square. To save, I just use the “Read Render Target” node: it’s slow but it works. I save the output using a SaveGame object.

To load it back in, I created a C++ function which looks as follows:
.h:

[...]
	UFUNCTION(BlueprintCallable, Category = "Rendering")
	static TArray<uint8> getBufferFromColorArray(TArray<FColor> arr);
[...]

.cpp:

#define add(num) working.Add(num)
#define addInt(num, size) working.Append(intToBytes(num, size))
TArray<uint8> UCPP_BPFuncLib::getBufferFromColorArray(TArray<FColor> arr)
{
	int size = sqrt(arr.Num());

	TArray<uint8> working;
	//header
	add(66);
	add(77);
	//size
	add(0);
	add(0);
	add(0);
	add(0);
	//app info
	add(0);
	add(0);
	add(0);
	add(0);
	//start address
	add(54);
	add(0);
	add(0);
	add(0);

	//BITMAPINFOHEADER
	//size of header
	add(40);
	add(0);
	add(0);
	add(0);
	//image size
	addInt(size, 4);
	addInt(size, 4);
	//color planes
	addInt(1, 2);
	//color depth
	addInt(24, 2);
	//compression
	addInt(0, 4);
	//dummy int
	addInt(0, 4);
	//resolution
	addInt(0, 4);
	addInt(0, 4);
	//colors in color pallette
	addInt(0, 4);
	//important colors
	addInt(0, 4);
	//image buffer
	size = arr.Num();
	for (int i = 0; i < size; i++) {
		add(arr[i].B);
		add(arr[i].G);
		add(arr[i].R);
	}
	//set real file size
	size = working.Num();
	auto tmp = intToBytes(size, size);
	for (int i = 0; i < 4; i++) {
		working[i + 2] = tmp[i];
	}
	return working;
}

This pretty much just turns the FColor buffer saved into a uint8 (byte) buffer array which can be used in the “Import Buffer as Texture 2D” node.

I then use this image as a parameter of a UI material (“Tex”) as such:


(The reason why the V component in the texture coordinate is flipped is due to how bitmaps are saved. Otherwise the image gets flipped.)
I then copy the material to the render target and it’s done!

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.